0
初始化main.cpp文件中的VAO,並使它們按預期工作。然而,當我完全一樣的初始化代碼移到一個單獨的類,導致無法找到我的制服(?優化掉了)在一個類中創建Opengl,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));
}
中是否引起了MVP統一被刪除類創建VAO,還是別的什麼?
編輯:
我要澄清:對象仍然呈現,但它呈現白色,與MVP統一失蹤。
我甚至在定義多維數據集類之前,初始化了SDL2並創建了opengl上下文。所以它可能不是關於opengl的上下文。 – abc