我有模型游戏,当我渲染它们时,我的CPU使用率变高(每个模型约为10%)。我使用glutMainLoop
每秒调用DisplayFunc
60次。我打电话有绘图功能在哪里如何在OpenGL游戏中正确渲染模型(使用纹理网格)而CPU使用率不高?
- glPushMatrix
- 的glTranslatef,glRotatef,glScalef
- glMaterialfv所以对于纹理材质,我结合我的质地
- 在glBegin为三角形
- 然后在循环的面孔glNormal3f ,glTexCoord2f,用于三角形的glVertex3f
- glEnd
- 和glPopMatrix
我不知道我在做什么错,我没有任何立足点,所以这就是为什么我在这里问。
好吧,我做了简单的更正,现在好吗?
#include <GL/glut.h>
#include <gl/glext.h>
void Display();
void Reshape(int width, int height);
const int windowWidth = 480;
const int windowHeight = 270;
GLuint vertexbuffer;
static const GLfloat g_vertex_buffer_data[] =
{
-1.0f, -1.0f, 0.0f,
1.0f, -1.0f, 0.0f,
0.0f, 1.0f, 0.0f,
};
int main(int argc, char* argv[])
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE);
glutInitWindowPosition(100, 100);
glutInitWindowSize(windowWidth, windowHeight);
glutCreateWindow("Modern OpenGL");
glutDisplayFunc(Display);
glutReshapeFunc(Reshape);
glutIdleFunc(Display);
GLfloat reset_ambient[4] = { 0.0f, 0.0f, 0.0f, 1.0f };
glLightModelfv(GL_LIGHT_MODEL_AMBIENT, reset_ambient);
glEnable(GL_LIGHTING);
glShadeModel(GL_SMOOTH);
glEnable(GL_LIGHT0);
glGenBuffers(1, &vertexbuffer);
glutMainLoop();
return 0;
}
void Display()
{
glClearColor(1.0, 1.0, 1.0, 1.0);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
gluLookAt(0,0,10,0,0,0,0,1,0);
GLfloat light0amb[4] = { 1.0, 1.0, 1.0, 1.0 };
glLightfv(GL_LIGHT0, GL_AMBIENT, light0amb);
GLfloat light0dif[4] = { 1.0, 1.0, 1.0, 1.0 };
glLightfv(GL_LIGHT0, GL_DIFFUSE, light0dif);
GLfloat light0spe[4] = { 1.0, 1.0, 1.0, 1.0 };
glLightfv(GL_LIGHT0, GL_SPECULAR, light0spe);
GLfloat light0pos[4] = { 0.0, 0.0, 0.0, 1.0 };
glLightfv(GL_LIGHT0, GL_POSITION, light0pos);
glBindBuffer(GL_ARRAY_BUFFER, vertexbuffer);
glBufferData(GL_ARRAY_BUFFER, sizeof(g_vertex_buffer_data), g_vertex_buffer_data, GL_STATIC_DRAW);
glEnableVertexAttribArray(0);
glBindBuffer(GL_ARRAY_BUFFER, vertexbuffer);
glVertexAttribPointer(
0,
3,
GL_FLOAT,
GL_FALSE,
0,
(void*)0
);
glDrawArrays(GL_TRIANGLES, 0, 3);
glDisableVertexAttribArray(0);
glFlush();
glutSwapBuffers();
}
void Reshape(int width, int height)
{
glutReshapeWindow(windowWidth, windowHeight);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glViewport(0, 0, width, height);
gluPerspective(45.0f, width/(double)height, 0.1f, 1000.0f);
glMatrixMode(GL_MODELVIEW);
}
应停止使用废弃的'glBegin' /'glEnd'和固定管道API并切换到顶点缓冲区和着色器。 [见这个问题。(http://stackoverflow.com/questions/14300569/opengl-glbegin-glend) – VTT
我还算是一个OpenGL小白的,但你在做脸剔除正常吗? https://learnopengl.com/#!Advanced-OpenGL/Face-culling –
@VTT你可以评论我添加了什么? – ground0