I have a game with models and when I render them my CPU usage goes high (about 10% per one model). I use glutMainLoop
which calls DisplayFunc
60 times per one second. I call drawing functions there where is
- glPushMatrix
- glTranslatef, glRotatef, glScalef
- glMaterialfv so materials for texture, I bind my texture
- glBegin for triangles
- then in loop for faces glNormal3f, glTexCoord2f, glVertex3f for triangles
- glEnd
- and glPopMatrix
I don't know what I am doing wrong and I don't have any foothold, so that's why I'm asking here.
Ok, so I made simple correction and is it all right now?
#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);
}