OpenGL - 通过instanced属性定义模型矩阵

时间:2016-06-02 17:47:21

标签: c++ opengl opengl-4

我是OpenGL的新手,我尝试使用实例来绘制许多3D矩形。我目前将每个3d矩形定位在不同的位置,它们显示正确。

问题:我现在希望每个3d矩形具有不同的旋转,由我定义并将其传递给顶点着色器。但是,当我这样做时,屏幕上的矩形不会出现

我没有提供完整的代码,因为它相当长。我只是提供了重要的部分,并希望我在某个地方犯了一个愚蠢的错误,你可以指出它。

这是顶点着色器:

#version 410 core
layout (location = 0) in vec3 position;
layout (location = 1) in vec3 offset;
layout (location = 2) in vec2 texCoord;
layout (location = 3) in mat4 instanceMatrix;

uniform mat4 view;
uniform mat4 projection;


out vec2 TexCoord;
void main()
{
    TexCoord = texCoord;
    gl_Position = projection * view * instanceMatrix * vec4(position + offset, 1.0f) ;

}

这是片段着色器:

#version 410 core

out vec4 color;
void main()
{
    color = vec4(1.0f,1.0f,1.0f, 1.0f);
}

以下是我发送数据的C ++代码:

// definition of models variable : std::vector<glm::mat4> models(0);


// Sending the Model matrix for each Rectangle
    GLuint buffer;
    glGenBuffers(1, &buffer);
    glBindBuffer(GL_ARRAY_BUFFER, buffer);
    glBufferData(GL_ARRAY_BUFFER, models.size() * sizeof(glm::mat4), &models[0], GL_STATIC_DRAW);

    GLsizei vec4Size = sizeof(glm::vec4);
    glEnableVertexAttribArray(3);
    glVertexAttribPointer(3, 4, GL_FLOAT, GL_FALSE, 4 * vec4Size, (GLvoid*)0);
    glEnableVertexAttribArray(4);
    glVertexAttribPointer(4, 4, GL_FLOAT, GL_FALSE, 4 * vec4Size, (GLvoid*)(vec4Size));
    glEnableVertexAttribArray(5);
    glVertexAttribPointer(5, 4, GL_FLOAT, GL_FALSE, 4 * vec4Size, (GLvoid*)(2 * vec4Size));
    glEnableVertexAttribArray(6);
    glVertexAttribPointer(6, 4, GL_FLOAT, GL_FALSE, 4 * vec4Size, (GLvoid*)(3 * vec4Size));

    glVertexAttribDivisor(3, 1);
    glVertexAttribDivisor(4, 1);
    glVertexAttribDivisor(5, 1);
    glVertexAttribDivisor(6, 1);

    // unfocus
    glBindBuffer(GL_ARRAY_BUFFER, 0);
    glBindVertexArray(0);

0 个答案:

没有答案