0
我在應用程序的幀率上遇到了一些麻煩,希望有人能夠發現可能的原因。OpenGL:Framerate在立方體內部時墜落
我的平均幀速是120左右(這似乎有點低......)。但是當我將相機移動到一個立方體中時,我將繪製速率直線下降到18的低點,並且我的GPU負載高達99%,並且只要我在立方體內就停留在那裏。我有一個相當不錯的顯卡(Radeon 7870 2GB),所以我期望只用一個立方體就可以提供出色的FPS。
如果我刪除只立方體,離開主循環的其餘部分,我得到〜2000年FPS這使我相信,我的實體:: _ doDraw()函數的問題,因爲它永遠不會被調用,而不立方體。
這是動作開始的地方,我有1個相機和1個實體,立方體。
void Renderer::render(sf::Window* renderWindow) {
renderWindow->setActive();
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
for(CameraIterator itr = mCameras.begin(); itr != mCameras.end(); ++itr) {
Camera* c = *itr;
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
sf::Vector2u size = renderWindow->getSize();
GLdouble aspect = double(size.x)/double(size.y);
gluPerspective(90, aspect, 0.01, 10000);
// Setup a viewport -> This should come from the Camera!
glViewport(0, 0, renderWindow->getSize().x, renderWindow->getSize().y);
glTranslatef(c->getPosition().x, c->getPosition().y, c->getPosition().z);
// Draw the scene
for(EntityIterator itr = mEntities.begin(); itr != mEntities.end(); ++itr) {
(*itr)->_doDraw();
}
}
renderWindow->display(); // Swap buffers
}
實體繪圖功能
void Entity::_doDraw() {
if(mPipelineID == -1) return;
glBindProgramPipeline(mPipelineID);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glEnableVertexAttribArray(0);
glBindBuffer(GL_ARRAY_BUFFER, mModel->getBufferID(MODEL_BUFFER_VERTEX));
glVertexAttribPointer(
0, // attribute 0. must match the layout in the shader.
3, // size
GL_FLOAT, // type
GL_FALSE, // normalized?
0, // stride
0 // array buffer offset
);
glEnableVertexAttribArray(1);
glBindBuffer(GL_ARRAY_BUFFER, mModel->getBufferID(MODEL_BUFFER_COLOUR));
glVertexAttribPointer(
1, // attribute 1
3, // size
GL_FLOAT, // type
GL_FALSE, // normalized?
0, // stride
0 // array buffer offset
);
glDrawArrays(GL_TRIANGLES, 0, mModel->getVertexData()->size());
glDisableVertexAttribArray(0);
glDisableVertexAttribArray(1);
glBindBuffer(GL_ARRAY_BUFFER, 0);
}
我2個着色器,以防萬一他們是恰當的。
// VERT
#version 400
layout(location = 0) in vec3 inPosition;
layout(location = 1) in vec3 inColour;
out vec4 gl_Position;
out vec4 outColour;
void main() {
vec4 position = vec4(inPosition.xyz, 1);
gl_Position = gl_ModelViewProjectionMatrix * position;
outColour.xyz = inColour;
}
// FRAG
#version 400
in vec4 inColour;
out vec4 outColour;
void main() {
outColour = inColour;
}
雖然這不是確切的答案,但它導致我的原因。我注意到有一個內存泄漏,或似乎是一個。 事實證明,我正在將立方體推到實體列表的每一幀!所以在60幀後,我有61個正在渲染的魔方副本。我解決了這個問題,現在它以1800英尺/秒的速度舒適地坐着,即使在立方體內部也是如此。 – Mattyspatty