What did I do wrong? (OpenGL)

Started by
10 comments, last by TheOther 18 years, 2 months ago
I'm trying to learn OpenGL and am using a couple of books and the NeHe tuts. I'm trying to draw a triangle in the scene, tt displays but only when the z position of the vertex is -1.0f, anything else and nothing is drawn. Can anyone see why that is? I'm not using glut at the moment as I wanted to learn how to setup an OpenGL window myself :) InitOpenGL:

GLvoid InitOpenGL(HWND hWnd, int iWidth, int iHeight, int iDepth)
{
	hDC = GetDC(hWnd);

	PIXELFORMATDESCRIPTOR	pfd;
	
	ZeroMemory(&pfd, sizeof(pfd));

	pfd.nSize = sizeof(pfd);
	pfd.nVersion = 1;
	pfd.dwFlags = PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER | PFD_TYPE_RGBA;
	pfd.iPixelType = PFD_TYPE_RGBA;
	pfd.cColorBits = iDepth;
	pfd.cDepthBits = 16;
	pfd.iLayerType = PFD_MAIN_PLANE;

	int iFormat;

	iFormat = ChoosePixelFormat(hDC, &pfd);

	SetPixelFormat(hDC, iFormat, &pfd);

	hRC = wglCreateContext(hDC);

	wglMakeCurrent(hDC, hRC);

	glShadeModel(GL_SMOOTH);
	glClearColor(0.0f, 0.0f, 0.0f, 0.0f);

	glClearDepth(1.0f);
	glEnable(GL_DEPTH_TEST);
	glDepthFunc(GL_LEQUAL);

	glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
}


ResizeOpenGL:

GLvoid ResizeOpenGL(GLsizei iWidth, GLsizei iHeight)
{
	glViewport(0, 0, iWidth, iHeight);

	glMatrixMode(GL_PROJECTION);
	glLoadIdentity();

	gluPerspective(90.0f, (GLfloat) iWidth / (GLfloat) iHeight, 0.1f, 100.f);

	glMatrixMode(GL_MODELVIEW);
	glLoadIdentity();
}


Drawing Code:


glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

glLoadIdentity();

glColor3f(1.0f, 1.0f, 1.0f);

glBegin(GL_TRIANGLES);						
glVertex3f( 0.0f, 0.5f, -1.0f);				
glVertex3f(-0.5f,-0.5f, -1.0f);				
glVertex3f( 0.5f,-0.5f, -1.0f);				 
glEnd();
			
SwapBuffers(hDC);


Advertisement
because OpenGL uses a right handed system, if you don't draw at some negative value in the z-axis you are either drawing behind yourself or right on top of yourself. drawing at -1 in the z simply puts your verts a little in front of the 'camera' and thus, lets you see what you are drawing. If you put them at -2 in the z axis the triangle would look even smaller.
The following code should work for your new rending code. This will set up a camera looking right at your triangle.

glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt( 0, 0, 1, // where the camera is position
0, 0, -1, // the 3d point the camera is looking at
0, 1, 0 ); // the up vector

glColor3f(1.0f, 1.0f, 1.0f);

glBegin(GL_TRIANGLES);
glVertex3f( 0.0f, 0.5f, -1.0f);
glVertex3f(-0.5f,-0.5f, -1.0f);
glVertex3f( 0.5f,-0.5f, -1.0f);
glEnd();

SwapBuffers(hDC);
Yea, what he said.
Ok, I mean that the triangle appears at -1. If if put -2 or -3 (and so on) I get nothing.
In OpenGL, the default values in glOrtho are as follows:

glOrtho (-1.0, 1.0, -1.0, 1.0, -1.0, 1.0)

So in order for things to be displayed, your world coordinate must be larger. Say for example:

glOrtho (-10.0, 10.0, -10.0, 10.0, -10.0, 10.0)

Hope this would help,
Regards,
Just out of curiosity, is your ResizeOpenGL() function ever actually getting called?
@Ali_B:

So am I in Ortho with this setup? I thought that OpenGL defaulted to "3D". I haven't seen anything in any tuts either.

@jyk:

Its called whenever the Window recieves a WM_SIZE message.
According to your code you're using perspective projection, so the glOrtho() thing may not be relevant. As for ResizeOpenGL(), I understand that it's called in response to an event, but my question is, does that event ever occur? (I don't know much about the Win API - maybe it's obvious that it does.) I ask because I noticed you're not calling it in your init function...
Its called on its own just before InitOpenGL(). Is this wrong?

This topic is closed to new replies.

Advertisement