Reputation:
I've been coding in OpenGL for a few months, and have been mainly working in 3D. My init method looks like this:
private void initGl() {
glViewport(0, 0, Display.getWidth(), Display.getHeight());
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
GLU.gluPerspective(45.0f, Display.getWidth() / Display.getHeight(), 1.0f, 100.0f);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
glClearDepth(1.0f);
glDepthFunc(GL_LEQUAL);
glEnable(GL_DEPTH_TEST);
glShadeModel(GL_SMOOTH);
glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
glEnable(GL_FOG);
glFogi(GL_FOG_MODE, GL_EXP2);
glFogf(GL_FOG_DENSITY, density);
glHint(GL_FOG_DENSITY, GL_FASTEST);
glEnable(GL_CULL_FACE);
glCullFace(GL_BACK);
vbo = new VBO();
}
Is all of this necesary? I'm wondering if I call loadIdentity() in the right places, should I be calling it after gluPerspective too? Basically, when is the appropriate time to call loadIdentity()?
Upvotes: 0
Views: 2580
Reputation: 162164
A common misconception of OpenGL newbies is, that OpenGL somehow is "initialized". OpenGL is a state based drawing machine. That means that all those functions in your "init" function set some drawing related state. The peculiar thing about state machines is, that when used practically you set all state everytime you need it, right when you need it. Or in other words: There is no such thing like a "OpenGL initialization" phase. Most of the calls in your "init" function actually belong into the drawing code.
The main exception are OpenGL objects that truly are one-time initialized, like textures or VBOs.
Upvotes: 1