Sign in to follow this  

What did I do wrong? (OpenGL)

This topic is 4343 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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);


Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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);

Share this post


Link to post
Share on other sites
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,

Share this post


Link to post
Share on other sites
@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.

Share this post


Link to post
Share on other sites
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...

Share this post


Link to post
Share on other sites
Quote:
Original post by mengha
Its called on its own just before InitOpenGL(). Is this wrong?
You might try calling it after the context has been created, rather than before.

Share this post


Link to post
Share on other sites
You're on the right track...but ignore the comments about glOrtho. You're fine with keeping it to a perspective projection as you've already created it.


//after the call to glHint, call your resizeGL routine
glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);

//your vertices are moving in a counter clockwise manner.
//this is the OpenGL default, but let's make sure it's
//set that way.
glFrontFace(GL_CCW);

//you CAN keep this in a windows callback for a WM_SIZE message
//but I always prefer to make sure it's called here as well...just in
//case the user never generates a WM_SIZE event! :)
ResizeOpenGL( iWidth, iHeight );

}




hth,

Share this post


Link to post
Share on other sites

This topic is 4343 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this