Drawing/Translating Order

Started by
2 comments, last by d h k 18 years, 8 months ago
Hello everybody, I am sorry, I couldn't think of anything more descriptive to name this thread. It's all about this: I am writing a small OpenGl program, that loads a height map just like NeHe does it in his tutorial. Then I added camera movement. Works fine after some little tweaking. Just how expected it to be... ;) Now I want to draw a simple cube on the terrain. The player should be allowed to move this box/cube around using keys. Therefore I added x, y and z coordinates for the box and every frame I make sure, that the y coordinate of the box equals the terrain y coordinate, so the box will always be drawn on the terrain. On key press I increase/decrease the x and z coordinate accordingly. My box just won't show up though and I strongly suspect the propblem to be in the order of my draw_scene ( ) function.
 
int draw_scene ( GLvoid )
// draws the scene
{
	// clear the screen and the depth buffer
	glClear ( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );

	// reset the matrix
	glLoadIdentity ( );

	glPushMatrix ( );

	// move to the cube's position
	glTranslatef ( unit[0].x, unit[0].y, unit[0].z );

	// select the cubes texture
	glBindTexture ( GL_TEXTURE_2D, texture[1] );

	// draw the cube
	glBegin ( GL_QUADS );
		
		// Front Face
		glTexCoord2f(0.0f, 0.0f); glVertex3f(-1.0f, -1.0f,  1.0f);	// Bottom Left Of The Texture and Quad
		glTexCoord2f(1.0f, 0.0f); glVertex3f( 1.0f, -1.0f,  1.0f);	// Bottom Right Of The Texture and Quad
		glTexCoord2f(1.0f, 1.0f); glVertex3f( 1.0f,  1.0f,  1.0f);	// Top Right Of The Texture and Quad
		glTexCoord2f(0.0f, 1.0f); glVertex3f(-1.0f,  1.0f,  1.0f);	// Top Left Of The Texture and Quad
		// Back Face
		glTexCoord2f(1.0f, 0.0f); glVertex3f(-1.0f, -1.0f, -1.0f);	// Bottom Right Of The Texture and Quad
		glTexCoord2f(1.0f, 1.0f); glVertex3f(-1.0f,  1.0f, -1.0f);	// Top Right Of The Texture and Quad
		glTexCoord2f(0.0f, 1.0f); glVertex3f( 1.0f,  1.0f, -1.0f);	// Top Left Of The Texture and Quad
		glTexCoord2f(0.0f, 0.0f); glVertex3f( 1.0f, -1.0f, -1.0f);	// Bottom Left Of The Texture and Quad
		// Top Face
		glTexCoord2f(0.0f, 1.0f); glVertex3f(-1.0f,  1.0f, -1.0f);	// Top Left Of The Texture and Quad
		glTexCoord2f(0.0f, 0.0f); glVertex3f(-1.0f,  1.0f,  1.0f);	// Bottom Left Of The Texture and Quad
		glTexCoord2f(1.0f, 0.0f); glVertex3f( 1.0f,  1.0f,  1.0f);	// Bottom Right Of The Texture and Quad
		glTexCoord2f(1.0f, 1.0f); glVertex3f( 1.0f,  1.0f, -1.0f);	// Top Right Of The Texture and Quad
		// Bottom Face
		glTexCoord2f(1.0f, 1.0f); glVertex3f(-1.0f, -1.0f, -1.0f);	// Top Right Of The Texture and Quad
		glTexCoord2f(0.0f, 1.0f); glVertex3f( 1.0f, -1.0f, -1.0f);	// Top Left Of The Texture and Quad
		glTexCoord2f(0.0f, 0.0f); glVertex3f( 1.0f, -1.0f,  1.0f);	// Bottom Left Of The Texture and Quad
		glTexCoord2f(1.0f, 0.0f); glVertex3f(-1.0f, -1.0f,  1.0f);	// Bottom Right Of The Texture and Quad
		// Right face
		glTexCoord2f(1.0f, 0.0f); glVertex3f( 1.0f, -1.0f, -1.0f);	// Bottom Right Of The Texture and Quad
		glTexCoord2f(1.0f, 1.0f); glVertex3f( 1.0f,  1.0f, -1.0f);	// Top Right Of The Texture and Quad
		glTexCoord2f(0.0f, 1.0f); glVertex3f( 1.0f,  1.0f,  1.0f);	// Top Left Of The Texture and Quad
		glTexCoord2f(0.0f, 0.0f); glVertex3f( 1.0f, -1.0f,  1.0f);	// Bottom Left Of The Texture and Quad
		// Left Face
		glTexCoord2f(0.0f, 0.0f); glVertex3f(-1.0f, -1.0f, -1.0f);	// Bottom Left Of The Texture and Quad
		glTexCoord2f(1.0f, 0.0f); glVertex3f(-1.0f, -1.0f,  1.0f);	// Bottom Right Of The Texture and Quad
		glTexCoord2f(1.0f, 1.0f); glVertex3f(-1.0f,  1.0f,  1.0f);	// Top Right Of The Texture and Quad
		glTexCoord2f(0.0f, 1.0f); glVertex3f(-1.0f,  1.0f, -1.0f);	// Top Left Of The Texture and Quad
		
	// finished drawing the cube
	glEnd ( );

	glPopMatrix ( );

	// position the camera
	gluLookAt ( camera.x, camera.y, camera.z, camera.x - 20, camera.y - 5, camera.z - 20, 0, 1, 0 );

	// scale the world
	glScalef ( 0.15f, 0.15f * HEIGHT_RATIO, 0.15f );

	// render the height map
	render_heightmap ( g_HeightMap );
	
	// keep going
	return TRUE;
}


Is it true that there is a mistake in there that would somehow place my box somewhere in infinity etc. or must the mistake be somewhere else. Please respond and I'll post more code when needed. Thanks a lot, Jan. [Edited by - d h k on August 7, 2005 8:14:38 PM]
Advertisement
UPDATE: I just noted I was indeed right. The box gets drawn, but it sticks to the player, just like a weapon in a FPS shooter.

What do I have to change in the order of my function to "unsticky" it and actually have it drawn to the map (unrelated to the current camera position)? While doing previous work I managed to "unsticky" my objects by randomly placing things over each other in the draw_scene ( ) function, but this time I want to do it and actually know what is really going on...

I replied instead of editing to bump this a little... :)
Try moving the gluLookAt() call to right after the first glLoadIdentity(). I'm not sure what the 'scale the world' line does, so I can't tell you where that should go exactly (it may be fine where it is).
Moving that single line did the trick. I think I got it now. :)

Thanks!

This topic is closed to new replies.

Advertisement