Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/162.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ OpenGL在显示多个对象时显示白色屏幕_C++_Opengl_Glsl_Glut_Geometry Shader - Fatal编程技术网

C++ OpenGL在显示多个对象时显示白色屏幕

C++ OpenGL在显示多个对象时显示白色屏幕,c++,opengl,glsl,glut,geometry-shader,C++,Opengl,Glsl,Glut,Geometry Shader,我试图在一个简单的立方体上实现法线映射,但由于法线有问题,我想尝试使用几何体着色器来显示它们。在学习learnopengl教程之后,它基本上会调用mesh.render()两次,第一次用于绘制模型,第二次用于显示法线。当我尝试做同样的事情时,我得到了这个 立方体看起来画得很好,但它前面有一个奇怪的白色矩形,我不知道为什么。我不知道这是绘图的问题还是几何体着色器的问题,所以我将两者都发布 我的代码: 这里我基本上是将相同的结构传递给两个缓冲区。 缓冲区在全局结构中表示 这是MyRenderS

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

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

我的代码:


这里我基本上是将相同的结构传递给两个缓冲区。 缓冲区在全局结构中表示

这是MyRenderScene():

和几何阴影:

#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
}


请随时纠正我的一切可能和想象。

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

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

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

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

这不会解决您的问题,但您必须在指定顶点之前绑定缓冲区对象。这意味着do
glBindBuffer(GL_ARRAY_BUFFER,global.VBO)
glvertexattributepointer(…)
之前-问题的原因是,带有几何体着色器的着色器程序无法编译或链接。谢谢!问题恰恰是,事实上,我甚至没有汇编它们,这是我的错误。然后,我按照您的建议更改了代码,现在它已经完全正常工作了。出于好奇,这是不是每次我没有链接着色器或着色器编译不正确时都应该出现的行为?@Fra是的,如果您有兼容配置文件,它就是。在核心概要文件中,根本不会渲染任何内容。我建议验证着色器是否成功编译和链接-请参阅问题的答案。
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();***
#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
}