将多个属性传递给OpenGL着色器

时间:2014-07-08 12:40:57

标签: c++ opengl

我在数组中交错[position,color,normal]数据,然后传递给顶点着色器。顶点着色器似乎只获取前两个值[position和color]。是什么阻止它获取正常数据?

数据存储

struct c_vertex{
GLfloat x;
GLfloat z;
GLfloat y;
GLfloat r;
GLfloat g;
GLfloat b;
GLfloat nx;
GLfloat nz;
GLfloat ny;
};

阵列

c_vertex vertex_store[max_bins][max_bins];

为数组的元素指定了值[正如我后面将要演示的那样正常工作]绘制例程:

glGenVertexArrays (1, &vao);
glBindVertexArray (vao);

glGenBuffers(1,&VertexVBoid);
glBindBuffer (GL_ARRAY_BUFFER, VertexVBoid);
glBufferData(GL_ARRAY_BUFFER,sizeof(vertex_store),&(vertex_store[0][0].x),GL_STATIC_DRAW);

glGenBuffers(1, &IndexVBOID);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, IndexVBOID);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(vertex_order), vertex_order, GL_STATIC_DRAW);

glBindBuffer (GL_ARRAY_BUFFER, VertexVBoid);
glVertexAttribPointer (0, 3, GL_FLOAT, GL_FALSE, sizeof(c_vertex), BUFFER_OFFSET(0));
glVertexAttribPointer (1, 3, GL_FLOAT, GL_FALSE, sizeof(c_vertex), BUFFER_OFFSET(3*sizeof(vertex_store[0][0].x)));
glVertexAttribPointer (2, 3, GL_FLOAT, GL_FALSE, sizeof(c_vertex), BUFFER_OFFSET(6*sizeof(vertex_store[0][0].x)));

glEnableVertexAttribArray (0);
glEnableVertexAttribArray (1);
glEnableVertexAttribArray (2);

glBindVertexArray (vao);

glDrawElements(GL_TRIANGLES, (max_bins-1)*(max_bins-1)*2*3, GL_UNSIGNED_INT, (void *) 0);
glDisableVertexAttribArray (0);
glDisableVertexAttribArray (1);
glDisableVertexAttribArray (2);
};

顶点着色器

attribute vec3 kposition;
attribute vec3 kcolor;
attribute vec3 knormal;

uniform mat4 MVP;
varying vec4 kolor;

void main()
{
    kolor = vec4(knormal,1.0);
    gl_Position = MVP*vec4(kposition,1.0);
}

片段着色器:

varying vec4 kolor;

void main(){
    gl_FragColor=kolor;
}

正常值与颜色值

相同

如果我在

中交换偏移量
glVertexAttribPointer (1, 3, GL_FLOAT, GL_FALSE, sizeof(c_vertex), BUFFER_OFFSET(3*sizeof(vertex_store[0][0].x)));
glVertexAttribPointer (2, 3, GL_FLOAT, GL_FALSE, sizeof(c_vertex), BUFFER_OFFSET(6*sizeof(vertex_store[0][0].x)));

glVertexAttribPointer (1, 3, GL_FLOAT, GL_FALSE, sizeof(c_vertex), BUFFER_OFFSET(6*sizeof(vertex_store[0][0].x)));
glVertexAttribPointer (2, 3, GL_FLOAT, GL_FALSE, sizeof(c_vertex), BUFFER_OFFSET(3*sizeof(vertex_store[0][0].x)));

然后我将正常数据用作着色器中的颜色数据

如果我有偏移量

glVertexAttribPointer (1, 3, GL_FLOAT, GL_FALSE, sizeof(c_vertex), BUFFER_OFFSET(3*sizeof(vertex_store[0][0].x)));
glVertexAttribPointer (2, 3, GL_FLOAT, GL_FALSE, sizeof(c_vertex), BUFFER_OFFSET(6*sizeof(vertex_store[0][0].x)));

如果我从

更改着色器
attribute vec3 kposition;
attribute vec3 kcolor;
attribute vec3 knormal;

attribute vec3 kposition;
attribute vec3 knormal;
attribute vec3 kcolor;

knormal和kcolor具有相同的值 - 颜色属性。

我的代码中有什么错误?

1 个答案:

答案 0 :(得分:3)

无法保证着色器和属性索引中的属性之间的映射。使用glGetAttribLocation查询每个属性的着色器。

GLint positionIndex = glGetAttribLocation(myProgramID, "kposition");
glVertexAttribPointer (positionIndex, 3, GL_FLOAT, GL_FALSE, sizeof(c_vertex), BUFFER_OFFSET(0));
glEnableVertexAttribArray (positionIndex);

GLint colorIndex = glGetAttribLocation(myProgramID, "kcolor");
glVertexAttribPointer (colorIndex, 3, GL_FLOAT, GL_FALSE, sizeof(c_vertex), BUFFER_OFFSET(3*sizeof(vertex_store[0][0].x)));
glEnableVertexAttribArray (colorIndex);

GLint normalIndex = glGetAttribLocation(myProgramID, "knormal");
glVertexAttribPointer (normalIndex, 3, GL_FLOAT, GL_FALSE, sizeof(c_vertex), BUFFER_OFFSET(6*sizeof(vertex_store[0][0].x)));
glEnableVertexAttribArray (normalIndex);