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

时间:2018-02-14 12:25:00

标签: opengl-4

我有一个用

绘制四面体的应用程序
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))吗?

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

1 个答案:

答案 0 :(得分:2)

  

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

一般来说,你必须有可能,

你有两个缓冲区,一个独立的顶点缓冲区和颜色缓冲区:

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);

或者你有一个顶点参数和颜色属性的组合缓冲区:

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(吹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);
相关问题