Jump to content
  • Advertisement
Sign in to follow this  
rohde

OpenGL Rendering problems

This topic is 4600 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

Hi y'all I have some old DX code I need to convert to OpenGL. Unfortunately I'm not well aqauinted with OpenGL so I'm reading up on it (Red book, OpenGL Super Bible, and Astle's Beginning OpenGL Game Programming (we must supports the Rhino right [smile])). Anyway, the following code should display a trianlge (it's adapted from Astle's book), but the triangle doesn't show. The window is setup proberly because I can change the background colour just fine. Maybe it has something to do with the transformations? Anyway this is the rendering code:
void opengl_render::render() {
	// Clear the screen and depth buffer
	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
	glLoadIdentity();

	// Move back 5 units and rotate about all 3 axes
	glTranslatef(0.0, 0.0, -5.0f);
	glRotatef(angle_, 1.0f, 0.0f, 0.0f);
	glRotatef(angle_, 0.0f, 1.0f, 0.0f);
	glRotatef(angle_, 0.0f, 0.0f, 1.0f);

	// Lime greenish color
	glColor3f(0.7f, 1.0f, 0.3f);

	// Draw the triangle such that the rotation point is in the center
	glBegin(GL_TRIANGLES);
		glVertex3f(1.0f, -1.0f, 0.0f);
		glVertex3f(-1.0f, -1.0f, 0.0f);
		glVertex3f(0.0f, 1.0f, 0.0f);
	glEnd();	
}
And this sets up the perspective projection (called in response to any WM_SIZE messages):
void opengl_render::setup_projection(int width, int height) {
	// Avoid a divide by zero
	if(height = 0) {
		height = 1;
	}

	// Reset the viewport to new dimensions
	glViewport(0, 0, width, height);
	// Set projection matrix current matrix
	glMatrixMode(GL_PROJECTION);
	// Reset projection matrix
	glLoadIdentity();
	// Calculate aspect ratio of window
	gluPerspective(52.0f, (GLfloat)width/(GLfloat) height, 1.0f, 1000.0f);

	// Set modelview matrix
	glMatrixMode(GL_MODELVIEW);
	// Reset modelview matrix
	glLoadIdentity();

	window_width_	= width;
	window_height_	= height;
}
So, what did I do wrong? I only see a black window (or whatever color I use for the glClearColor (this call is in another method not shown above)).

Share this post


Link to post
Share on other sites
Advertisement
Try disabling back face culling in case the triangle is facing the wrong direction. (call glDisable(GL_CULL_FACE) before rendering the triangle)
I always use gluLookAt() when debugging code, and i always disable back face culling when rendering simple things like quads and triangles. I think it's easier that way.

Hope it helps.

HellRaiZer

Share this post


Link to post
Share on other sites
Oh man. I found it. A classical error. I used '=' instead if '==' when setting up the projection.

Share this post


Link to post
Share on other sites
Quote:
Original post by HellRaiZer
Try disabling back face culling in case the triangle is facing the wrong direction. (call glDisable(GL_CULL_FACE) before rendering the triangle)
I always use gluLookAt() when debugging code, and i always disable back face culling when rendering simple things like quads and triangles. I think it's easier that way.

Hope it helps.

HellRaiZer


Thanks for the hints. I will sure remember that. [smile]

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.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!