Original Post
I'm working on writing a simple graphics engine, and I've got the window creation and setup for OGL working fine, but drawing seems to be another matter. Here's what I'm trying to draw just to make sure that drawing works. I have this drawing, but something is confusing me. Why am I able to see it? I was under the impression that when you draw something at a positive z value, then it is drawn behind the camera, and a negative z value is in front of the camera. I mean, I haven't moved the camera or anything. Here's my OGL init code, and the beginScene and endScene functions: Is it something that I've done in complete ignorance? I am able to see the triangle in a range of about -1 to +1. Any ideas? Because this doens't look different from other OGL init code that I've gotten from tutorials.
while( engine.run() ){
driver->beginScene(); // clears the window with glClear
glLoadIdentity();
glPointSize(5.0f);
glBegin(GL_TRIANGLES);
glColor3f(1.0f, 1.0f, 1.0f);
glVertex3f(0, 0, 1);
glVertex3f(50, 100, 1);
glVertex3f(100, 50, 1);
glEnd();
glFlush();
driver->endScene(); // actually renders the window
}
/**
* Initialises the video driver.
* \param screenSize A dimension2d object containing the height and width of the screen
*/
bool COpenGLDriver::initDriver(const video::dimension2d& screenSize)
{
int nPixelFormat;
static PIXELFORMATDESCRIPTOR pfd = {
sizeof(PIXELFORMATDESCRIPTOR), // size of the structure
1, // version. Always set to 1
PFD_DRAW_TO_WINDOW | // support window
PFD_SUPPORT_OPENGL | // support opengl
PFD_DOUBLEBUFFER, // support double buffering
PFD_TYPE_RGBA, // RGBA color mode
32, // 32-bit color mode
0, 0, 0, 0, 0, 0, // ignore color bits, not used
0, // no alpha buffer
0, // ignore shift bit
0, // no accumulation buffer
0, 0, 0, 0, // ignore accumulation bits
16, // 16-bit z-buffer size
0, // no stencil buffer
0, // no auxillary buffer
PFD_MAIN_PLANE, // main drawing plane
0, // reserved
0, 0, 0 // layer masks ignored
};
// if get hdc fails
if( !(hDC = GetDC(Window)) ){
util::Message::print("COpenGLDriver::initDriver: Cannot create a GL device context.");
return false;
}
// chose best matching pixel format, return index
if( !(nPixelFormat=ChoosePixelFormat(hDC, &pfd)) ){
util::Message::print("COpenGLDriver::initDriver: Cannot find a suitable pixel format.");
return false;
}
// set pixel format to device context
if( !SetPixelFormat(hDC, nPixelFormat, &pfd) ){
util::Message::print("COpenGLDriver::initDriver: Cannot set the pixel format.");
return false;
}
// create rendering context
if( !(hRC=wglCreateContext(hDC)) ){
util::Message::print("COpenGLDriver::initDriver: Cannot create a GL rendering context.");
return false;
}
// activate rendering context
if( !wglMakeCurrent(hDC, hRC) ){
util::Message::print("COpenGLDriver::initDriver: Cannot activate GL rendering context.");
return false;
}
glViewport(0, 0, (int)screenSize.width, (int)screenSize.height); // reset the current opengl viewport
glShadeModel(GL_SMOOTH); // enable smooth shading
glDisable(GL_LIGHTING); // lighting disabled
glFrontFace(GL_CW); //
glEnable(GL_CULL_FACE);
glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST); // really nice perspective calculations
glClearDepth(1.0f); // set gl clear depth
glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_LEQUAL);
return true; // init went well
}
/**
* Does OGL housekeeping, like clearing the screen and depth buffer
* \param c A CColor object that represents the color to clear the screen to. Defaults to black.
*/
void COpenGLDriver::beginScene(video::CColor c = video::CColor(0.0f,0.0f,0.0f))
{
glClearColor( c.getRedFloat(), c.getGreenFloat(),
c.getBlueFloat(), c.getAlphaFloat()
); // Set clear color
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);// Clear Screen And Depth Buffer
}
/**
* Does OGL housekeeping for the end of rendering a frame.
*/
void COpenGLDriver::endScene()
{
SwapBuffers(hDC); // render the scene to the window