如何用索引顶点为三角形着色



我有一个用

绘制四面体的应用程序
glDrawElements(GL_TRIANGLES,...)

我现在想为四面体的脸上涂上单个扁平颜色。

如果我正确理解,我必须三份我的4个顶点,以便每个脸都有"唯一"的顶点,可以分配相同的颜色值。

目前,我处理这些顶点及其指数

    glGenBuffers(1, &position_buffer);
    glBindBuffer(GL_ARRAY_BUFFER, position_buffer);
    glBufferData(GL_ARRAY_BUFFER,
                 sizeof(vertex_positions),
                 vertex_positions,
                 GL_STATIC_DRAW);
    glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, NULL);
    glEnableVertexAttribArray(0);
    glGenBuffers(1, &index_buffer);
    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, index_buffer);
    glBufferData(GL_ELEMENT_ARRAY_BUFFER,
                 sizeof(vertex_indices),
                 vertex_indices,
                 GL_STATIC_DRAW);

在顶点着色器中使用

访问顶点
 in vec4 position;

我应该在额外的 GL_ARRAY_BUFFER中传递颜色吗?

如果是,我可以使用相同的GL_ELEMENT_ARRAY_BUFFER(并使用glEnableVertexAttribArray(1)(吗?

如何从顶点着色器内部访问颜色?

我应该在附加的 GL_ARRAY_BUFFER中传递颜色吗?

通常,您必须可能性,

您有2个缓冲区,一个分离的顶点缓冲区和颜色缓冲区:

GLuint vertex_attr_inx = 0;
GLuint color_attr_inx = 1;
GLfloat vertex_positions[] = .... ; // x0, y0, z0, x1, y1, z1, ...
GLfloat vertex_colors[]    = .... ; // R0, G0, B0, A0, R1, G1, B1, A1, ... 
GLuint vbo[2];
glGenBuffers(2, vbo);
glBindBuffer(GL_ARRAY_BUFFER, vbo[0]);
glBufferData(GL_ARRAY_BUFFER, sizeof(vertex_positions), vertex_positions, GL_STATIC_DRAW);
glVertexAttribPointer(vertex_attr_inx, 3, GL_FLOAT, GL_FALSE, 0, NULL);
glEnableVertexAttribArray(vertex_attr_inx);
glBindBuffer(GL_ARRAY_BUFFER, vbo[1]);
glBufferData(GL_ARRAY_BUFFER, sizeof(vertex_colors), vertex_colors, GL_STATIC_DRAW);
glVertexAttribPointer(color_attr_inx, 4, GL_FLOAT, GL_FALSE, 0, NULL);
glEnableVertexAttribArray(color_attr_inx);

,或者您有一个针对顶点psoity和颜色属性的组合缓冲液:

GLfloat vertex_attributes[] = ; // x0, y0, z0, R0, G0, B0, A0, x1, y1, z1, R1, G1, B1, A1, ...
GLsizei attrSize  = 7*sizeof(float); // 7 -> x, y, z, R, G, B, A
GLsizei colorOffs = 3*sizeof(float);
GLuint vbo;
glGenBuffers(1, &vbo);
glBindBuffer(GL_ARRAY_BUFFER, vbo);
glBufferData(GL_ARRAY_BUFFER, sizeof(vertex_attributes), vertex_attributes, GL_STATIC_DRAW);
glVertexAttribPointer(vertex_attr_inx, 3, GL_FLOAT, GL_FALSE, attrSize, 0);
glEnableVertexAttribArray(vertex_attr_inx);
glVertexAttribPointer(color_attr_inx, 4, GL_FLOAT, GL_FALSE, attrSize, colorOffs);
glEnableVertexAttribArray(color_attr_inx);


在顶点着色器中,您必须使用2个属性。

我建议使用布局位置来指定属性索引(需要至少GLSL版本3.30;应该是这样,因为您将问题标记为" OpenGL-4"(:

#version 330
layout(location = 0) in vec4 position;
layout(location = 1) in vec4 color;

当然,您还可以查询属性位置,以适用于openGL/glsl的早期版本(blow OpenGL 3.3和GLSL 3.30(:

in vec4 position;
in vec4 color;

GLuint vertex_attr_inx = glGetAttribLocation( shaderProgram, "position" );
GLuint color_attr_inx  = glGetAttribLocation( shaderProgram, "color" );



如果是,我可以使用相同的 GL_ELEMENT_ARRAY_BUFFER

在两种情况下,索引缓冲区(GL_ELEMNT_ARRAY_BUFFER(都是相同的。您可以使用问题的代码。

glGenBuffers(1, &index_buffer);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, index_buffer);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(vertex_indices), vertex_indices, GL_STATIC_DRAW);

最新更新