Jump to content
  • Advertisement
Sign in to follow this  
mengha

What did I do wrong? (OpenGL)

This topic is 4552 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
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.

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
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!