2

我正在开发一个渲染引擎,到目前为止一切都很好,但我不明白为什么当我进行两次绘图调用以渲染不同的模型时,只有 1 个会出现。

我正在使用 wxWidgets 来处理窗口系统,有问题的代码粘贴在下面。有什么建议么?

主渲染循环

TestShader.Activate();
glUseProgram(TestShader.Program);

ProjectionMatrix = glm::perspective(45.0f, 4.0f / 3.0f, 0.1f, 100.0f);
int projectionMatrixLocation = glGetUniformLocation(TestShader.Program, "ProjectionMatrix");
glUniformMatrix4fv(projectionMatrixLocation, 1, GL_FALSE, glm::value_ptr(ProjectionMatrix));

glm::mat4 ViewMatrix = glm::lookAt(
                            glm::vec3(position),          
                            glm::vec3(position+direction),
                            glm::vec3(up)                 
                       );
int viewMatrixLocation = glGetUniformLocation(TestShader.Program, "ViewMatrix");
glUniformMatrix4fv(viewMatrixLocation, 1, GL_FALSE, glm::value_ptr(ViewMatrix));

TestModel[1].Draw(TestShader, glm::vec3(0,0,-11));
TestModel[0].Draw(TestShader, glm::vec3(0,0,-1));

Refresh(false);

模型绘制功能

void E_MODEL::Draw(EShader Shader, glm::vec3 Location)
{
if (!Registered) glGenVertexArrays(1, &VAO[0]);
glBindVertexArray(VAO[0]);

if (!Registered) glGenBuffers(1, &VBO[0]);
glBindBuffer(GL_ARRAY_BUFFER, VBO[0]);
if (!Registered) glBufferData(GL_ARRAY_BUFFER, Vertices.size() * sizeof(glm::vec3), &Vertices[0], GL_STATIC_DRAW);

if (!Registered) glGenBuffers(1, &VBO[1]);
glBindBuffer(GL_ARRAY_BUFFER, VBO[1]);
if (!Registered) glBufferData(GL_ARRAY_BUFFER, Normals.size() * sizeof(glm::vec3), &Normals[0], GL_STATIC_DRAW);

glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

glEnableVertexAttribArray(0);
glBindBuffer(GL_ARRAY_BUFFER, VBO[0]);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, (void*)0);

glEnableVertexAttribArray(1);
glBindBuffer(GL_ARRAY_BUFFER, VBO[1]);
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 0, (void*)0);


glm::mat4 modelMatrix = glm::translate(glm::mat4(1.0f), Location);

int modelMatrixLocation = glGetUniformLocation(Shader.Program, "modelMatrix");
glUniformMatrix4fv(modelMatrixLocation, 1, GL_FALSE, glm::value_ptr(modelMatrix));
glDrawArrays( GL_TRIANGLES, 0, Vertices.size() );


glDisableVertexAttribArray(0);
glDisableVertexAttribArray(1);

glBindVertexArray(0);

glBindBuffer(GL_ARRAY_BUFFER, 0);

Registered = true;
}

顶点着色器

#version 330 core

// Input vertex data, different for all executions of this shader.
layout(location = 0) in vec3 vertexPosition_modelspace;
layout(location = 1) in vec3 vertexColor;

// Output data ; will be interpolated for each fragment.
out vec3 fragmentColor;
// Values that stay constant for the whole mesh.
uniform mat4 ProjectionMatrix;
uniform mat4 ViewMatrix;
uniform mat4 modelMatrix;

void main(){    

gl_Position =  ProjectionMatrix * ViewMatrix * modelMatrix * vec4(vertexPosition_modelspace,1);

// The color of each vertex will be interpolated
// to produce the color of each fragment
fragmentColor = vertexColor;
}

片段着色器

#version 330 core

// Interpolated values from the vertex shaders
in vec3 fragmentColor;

// Ouput data
out vec3 color;

void main(){

// Output color = color specified in the vertex shader, 
// interpolated between all 3 surrounding vertices
color = fragmentColor;

}
4

2 回答 2

5

glClear清除缓冲区。使用GL_COLOR_BUFFER_BIT标志,您正在清除颜色缓冲区。使用该GL_DEPTH_BUFFER_BIT标志,您正在清除深度缓冲区。通过在每次绘制模型时执行此操作,之前写入的颜色和深度信息都会被清除。这意味着您每次绘制模型时都会擦除图像。

您通常会在每次“绘制”或“呈现”时清除一次缓冲区。也就是你通关一次,抽N次,呈现一次。你现在正在做的事情是清楚的,画的,清楚的,画的……现在。

TL;DR:在所有glClear抽奖之前调用一次,而不是在每次抽签之前调用。

于 2012-11-30T02:04:08.547 回答
1

它是glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);绘图功能的内部吗?也许在您调用绘图函数之前将其移出?

于 2012-11-30T01:56:27.737 回答