Rendering problems

Started by
2 comments, last by rohde 17 years, 11 months ago
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)).
"We confess our little faults to persuade people that we have no large ones." -Francois de La Rochefoucauld (1613 - 1680). | My blog
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
HellRaiZer
Oh man. I found it. A classical error. I used '=' instead if '==' when setting up the projection.
"We confess our little faults to persuade people that we have no large ones." -Francois de La Rochefoucauld (1613 - 1680). | My blog
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]
"We confess our little faults to persuade people that we have no large ones." -Francois de La Rochefoucauld (1613 - 1680). | My blog

This topic is closed to new replies.

Advertisement