如何将矩阵数组传递给VertexShader?

时间:2017-03-03 12:03:23

标签: opengl

我正在尝试学习如何使用OpenGL,但是我在尝试将矩阵数组传递给VertexShader时遇到了问题。我认为问题出在VertexShader中,因为似乎模型值中的值正确传递给模型的组件,但由于某种原因,最终只绘制了我创建的三个立方体中的一个。 这是主要代码。

glm::mat4 model[3];
    GLint ModelLocation = glGetUniformLocation(MyShader.program, "model");
    vector<GLfloat> modelvalue;
    const int n = int(sizeof(model[0]) / sizeof(GLfloat));

    for (int i = 0; i < 3; i++) {
        model[i] = glm::translate(model[i], cubePositions[i]);

        for (int j = 0; j < n; j++) {
            modelvalue.push_back(glm::value_ptr(model[i])[j]);
        }

    }
    glUniformMatrix4fv(ModelLocation, 3, GL_FALSE, &modelvalue[0]);

VertexShader:

   #version 400 core
  layout (location = 0) in vec3 Position;
  layout (location = 1) in vec2 texCoord;
  out vec2 TexCoord;
  out vec4 position0;
  out vec4 position1;
  out vec4 position2;
  uniform mat4 model[3];
  uniform mat4 view;
  uniform mat4 projection;
  void main()
 {   
position0 = projection * view * model[0] * vec4(Position,1.0f);
position1 = projection * view * model[1] * vec4(Position,1.0f);
position2 = projection * view * model[2] * vec4(Position,1.0f);
TexCoord = vec2(texCoord.x,1-texCoord.y);
 }

FragmentShader:

   #version 400 core

   layout (location = 0) in vec3 Position;
   layout (location = 1) in vec2 texCoord;


  out vec2 TexCoord;
  out vec4 position0;
  out vec4 position1;
  out vec4 position2;
  uniform mat4 model[3];
  uniform mat4 view;
  uniform mat4 projection;
  void main()
  {   
position0 = projection * view * model[0] * vec4(Position,1.0f);
position1 = projection * view * model[1] * vec4(Position,1.0f);
position2 = projection * view * model[2] * vec4(Position,1.0f);
TexCoord = vec2(texCoord.x,1-texCoord.y);
  }

1 个答案:

答案 0 :(得分:1)

Uniform Blocks可能对您有用。它们本质上是包含统一值数组的缓冲区对象。