在main.cpp文件中初始化VAO并使它们按预期工作。但是当我将完全相同的初始化代码移动到一个单独的类时,导致我的制服找不到(优化了吗?)
在main.cpp文件中初始化数组对象时(工作正常):
GLuint createCube(){
GLuint VBO;
GLuint VAO;
GLuint EAO;
GLfloat vertexData[] = {
0.5f, -0.5f, -0.5f,
0.5f, -0.5f, 0.5f,
-0.5f, -0.5f, 0.5f,
-0.5f, -0.5f, -0.5f,
0.5f, 0.5f, -0.5f,
0.5f, 0.5f, 0.5f,
-0.5f, 0.5f, 0.5f,
-0.5f, 0.5f, -0.5f,
};
GLuint elementData[] = {
2-1, 3-1, 4-1,
8-1, 7-1, 6-1,
5-1, 6-1, 2-1,
6-1, 7-1, 3-1,
3-1, 7-1, 8-1,
1-1, 4-1, 8-1,
1-1, 2-1, 4-1,
5-1, 8-1, 6-1,
1-1, 5-1, 2-1,
2-1, 6-1, 3-1,
4-1, 3-1, 8-1,
5-1, 1-1, 8-1,
};
// make and bind the VAO
glGenVertexArrays(1, &VAO);
glBindVertexArray(VAO);
// make and bind the VBO
glGenBuffers(1, &VBO);
glBindBuffer(GL_ARRAY_BUFFER, VBO);
// make and bind the eao
glGenBuffers(1, &EAO);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EAO);
//put data into VBO and EAO
glBufferData(GL_ARRAY_BUFFER, sizeof(vertexData), vertexData, GL_STATIC_DRAW);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(elementData), elementData, GL_STATIC_DRAW);
// connect the xyz to the "vert" attribute of the vertex shader
glEnableVertexAttribArray(render.get_shader()->attrib("vert"));
glVertexAttribPointer(render.get_shader()->attrib("vert"), 3, GL_FLOAT, GL_FALSE, 0, NULL);
// unbind the VAO, EAO, VBO
glBindVertexArray(0);
glBindBuffer(GL_ARRAY_BUFFER, 0);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
return VAO;
}
int main()
{
GLuint VAO = createCube();
glm::mat4 MVP = createMVP();
bool running = true;
while(running){
running = eventHandler();
render.draw(VAO,36,MVP);
ui.swapBuffer();
}
return 0;
}
初始化类中的数组对象时(无法找到我的着色器中的MVP制服,可能会优化出来吗?)
class cube{
public:
cube(Render rendrer)
{
GLuint VBO;
GLuint EAO;
GLfloat vertexData[] = {
0.5f, -0.5f, -0.5f,
0.5f, -0.5f, 0.5f,
-0.5f, -0.5f, 0.5f,
-0.5f, -0.5f, -0.5f,
0.5f, 0.5f, -0.5f,
0.5f, 0.5f, 0.5f,
-0.5f, 0.5f, 0.5f,
-0.5f, 0.5f, -0.5f,
};
GLuint elementData[] = {
2-1, 3-1, 4-1,
8-1, 7-1, 6-1,
5-1, 6-1, 2-1,
6-1, 7-1, 3-1,
3-1, 7-1, 8-1,
1-1, 4-1, 8-1,
1-1, 2-1, 4-1,
5-1, 8-1, 6-1,
1-1, 5-1, 2-1,
2-1, 6-1, 3-1,
4-1, 3-1, 8-1,
5-1, 1-1, 8-1,
};
// make and bind the VAO
glGenVertexArrays(1, &VAO);
glBindVertexArray(VAO);
// make and bind the VBO
glGenBuffers(1, &VBO);
glBindBuffer(GL_ARRAY_BUFFER, VBO);
// make and bind the eao
glGenBuffers(1, &EAO);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EAO);
//put data into VBO and EAO
glBufferData(GL_ARRAY_BUFFER, sizeof(vertexData), vertexData, GL_STATIC_DRAW);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(elementData), elementData, GL_STATIC_DRAW);
// connect the xyz to the "vert" attribute of the vertex shader
glEnableVertexAttribArray(rendrer.get_shader()->attrib("vert"));
glVertexAttribPointer(rendrer.get_shader()->attrib("vert"), 3, GL_FLOAT, GL_FALSE, 0, NULL);
// unbind the VAO, EAO, VBO
glBindVertexArray(0);
glBindBuffer(GL_ARRAY_BUFFER, 0);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
}
GLuint get_VAO() const
{
return VAO;
}
private:
GLuint VAO;
};
int main()
{
cube cube_object(render);
GLuint VAO = cube_object.get_VAO();
glm::mat4 MVP = createMVP();
bool running = true;
while(running){
running = eventHandler();
render.draw(VAO,36,MVP);
ui.swapBuffer();
}
return 0;
}
这是我的顶点着色器:
#version 430
in vec3 vert;
uniform mat4 MVP;
out vec3 vertout;
void main(void)
{
vertout = vert;
gl_Position = MVP*vec4(vert, 1.0);
}
这是我的片段着色器:
#version 430
in vec3 vertout;
out vec4 outcolor;
void main(void)
{
outcolor = mix(vec4(1,0,0,1),vec4(0,1,0,1),dot(vertout, vertout));
}
在类中创建VAO是否会导致MVP统一被删除,还是其他的?
编辑:
我应该澄清一下:对象仍然呈现,但它呈现白色并且缺少MVP制服。
答案 0 :(得分:0)
要使OpenGL操作起作用,必须有一个OpenGL上下文并在该线程上保持最新。如果你的"其他"在没有OpenGL上下文可用的情况下实现类,就好像根本没有创建任何内容一样。更糟糕的是,如果周围没有上下文,glGetError可能会返回GL_NO_ERROR,因此只使用它来检查错误可能不会显示该问题。