OpenGL 在显示多个对象时显示白屏

OpenGL displaying white screen while displaying multiple objects

我试图在一个简单的立方体上实现法线贴图,但由于我的法线有问题,我想尝试使用几何着色器来显示它们。按照learnopengl教程,基本上调用了两次mesh.render(),第一次绘制模型,第二次显示法线。当我尝试做同样的事情时,我得到了这个

立方体看起来画得不错,但它前面有一个奇怪的白色矩形,我不知道为什么。不知道是画图的问题还是几何着色器的问题,所以我post两个都

我的代码:


glutDisplayFunc(MyRenderScene);    
glEnable(GL_DEPTH_TEST);
glEnable(GL_CULL_FACE);
glCullFace(GL_BACK);
glFrontFace(GL_CW);

glGenVertexArrays(1,&global.gVAO); 
glGenBuffers(1, &global.VBO);
glBindVertexArray(global.VAO);
glBindBuffer(GL_ARRAY_BUFFER, global.VBO);
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);

glGenBuffers(1, &global.IBO);    
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, global.IBO);    
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indices), indices, GL_STATIC_DRAW);

glGenVertexArrays(1,&global.gVAO); 
glGenBuffers(1, &global.gVBO);
glBindVertexArray(global.gVAO);
glBindBuffer(GL_ARRAY_BUFFER, global.gVBO);
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);

glGenBuffers(1, &global.gIBO);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, global.gIBO);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indices), indices, GL_STATIC_DRAW);

在这里,我基本上将相同的结构传递给两个缓冲区。 缓冲区在全局结构中被实例化。

这是 MyRenderScene() :

glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); 

global.shaders.enable(); // glUseProgram
global.shaders.set_sampler(0); // setting textures
global.shaders.set_sampler(1);

global.sceneT.set_camera(
    global.camera.position(),
    global.camera.lookat(),
    global.camera.up()
);

glm::mat4 model = glm::mat4(1.0f);
glm::vec3 vaxis = glm::vec3(0.0,1.0,0.0);
glm::vec3 haxis = glm::vec3(1.0,0.0,0.0);
model = glm::rotate(model,glm::radians(global.gradX),haxis);
model = glm::rotate(model,glm::radians(global.gradY),vaxis);
glm::mat4 projection = glm::perspective(glm::radians(40.0f), (float)global.WINDOW_WIDTH/(float)global.WINDOW_HEIGHT, 0.1f, 100.0f);
glm::mat4 view = glm::lookAt(global.camera.position(),global.camera.lookat()+ global.camera.position(),global.camera.up());

global.shaders.set_model(model);
global.shaders.set_view(view);
global.shaders.set_projection(projection);
global.shaders.set_viewPos(global.camera.position());
global.shaders.set_lightPos(lightPos);
global.shaders.update_uniforms();

glBindVertexArray(global.VAO);   

glEnableVertexAttribArray(0);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), 
    reinterpret_cast<GLvoid*>(offsetof(struct Vertex, position)));
glEnableVertexAttribArray(1);
glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, sizeof(Vertex), 
     reinterpret_cast<GLvoid*>(offsetof(struct Vertex, textcoord)));
glEnableVertexAttribArray(2);
glVertexAttribPointer(2, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), 
     reinterpret_cast<GLvoid*>(offsetof(struct Vertex, normal)));
glEnableVertexAttribArray(3);
glVertexAttribPointer(3, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), 
     reinterpret_cast<GLvoid*>(offsetof(struct Vertex, tangent)));

glBindBuffer(GL_ARRAY_BUFFER, global.VBO);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, global.IBO);

global.brickwall.Bind(GL_TEXTURE0+0); // binding textures
global.brickwall_normals.Bind(GL_TEXTURE0+1);

glDrawElements(GL_TRIANGLES,36,GL_UNSIGNED_INT,0);

glBindVertexArray(0);

glDisableVertexAttribArray(0);
glDisableVertexAttribArray(1);
glDisableVertexAttribArray(2);
glDisableVertexAttribArray(3);

global.geometryShader.enable(); // setting up geometry shader
global.geometryShader.set_projection(projection);
global.geometryShader.set_model(model);
global.geometryShader.set_view(view);
global.geometryShader.update_uniforms();

glBindVertexArray(global.gVAO);

glEnableVertexAttribArray(0);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), 
    reinterpret_cast<GLvoid*>(offsetof(struct Vertex, position)));
glEnableVertexAttribArray(1);
glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, sizeof(Vertex), 
     reinterpret_cast<GLvoid*>(offsetof(struct Vertex, textcoord)));
glEnableVertexAttribArray(2);
glVertexAttribPointer(2, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), 
     reinterpret_cast<GLvoid*>(offsetof(struct Vertex, normal)));
glEnableVertexAttribArray(3);
glVertexAttribPointer(3, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), 
     reinterpret_cast<GLvoid*>(offsetof(struct Vertex, tangent)));

glBindBuffer(GL_ARRAY_BUFFER, global.gVBO);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, global.gIBO);
  
glDrawElements(GL_TRIANGLES,36,GL_UNSIGNED_INT,0);

glBindVertexArray(0);
glDisableVertexAttribArray(0);
glDisableVertexAttribArray(1);
glDisableVertexAttribArray(2);
glDisableVertexAttribArray(3);

glutSwapBuffers();

glutPostRedisplay();***

我什至尝试调用相同的 vertexArrays 和 vertexArrayBuffer 但我得到了相同的结果。 这是顶点着色器:


#version 330 core

layout (location = 0) in vec3 position;
layout (location = 1) in vec2 textcoord;
layout (location = 2) in vec3 normal;
layout (location = 3) in vec3 tangent; 

out VS_OUT {
    vec3 newNormal;
} vs_out;

uniform mat4 view;
uniform mat4 model;

void main() {
    mat3 normalMatrix = mat3(transpose(inverse(view * model)));
    newNormal = vec3(vec4(normalMatrix * normal, 0.0));
    gl_Position = view * model * vec4(position, 1.0);
}

和几何着色器:

#version 330 core

layout (triangles) in;
layout (line_strip, max_vertices = 6) out;

in VS_OUT {
    vec3 newNormal;
} gs_in[];

const float MAGNITUDE = 0.2;

uniform mat4 projection;

void GenerateLine(int index) {
    gl_Position = projection * gl_in[index].gl_Position;
    EmitVertex();
    gl_Position = projection * (gl_in[index].gl_Position + vec4(gs_in[index].newNormal,0.0) * MAGNITUDE);
    EmitVertex();
    EndPrimitive();
}

void main() {
    GenerateLine(0); // first vertex normal
    GenerateLine(1); // second vertex normal
    GenerateLine(2); // third vertex normal
}

请随时纠正我所有可能和想象得到的问题。

问题的原因是,带有几何着色器的着色器程序无法编译或link。因此几何图形是由默认着色器程序绘制的,而不是由带有几何着色器的程序绘制的。

顶点着色器中(至少)有一个错误:

newNormal = vec3(vec4(normalMatrix * normal, 0.0));

vs_out.newNormal = vec3(vec4(normalMatrix * normal, 0.0));