gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 02:45:16 25 4
gpt4 key购买 nike

我将 [position, colour, normal] 数据交织在一个数组中,然后将其传递给顶点着色器。顶点着色器似乎只获取前两个值[位置和颜色]。是什么阻止它也获取正常数据?

数据存储

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 具有相同的值 - 颜色属性。

我的代码有什么错误?

最佳答案

关于着色器中的属性和属性索引之间的映射没有任何保证。使用 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);

关于c++ - 将多个属性传递给 OpenGL 着色器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24632145/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com