Archived

This topic is now archived and is closed to further replies.

Q: about details mixing 2D and 3D graphics

This topic is 4949 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, I''m working on a game using SDL with OpenGL with C++ under Linux. The main gameplay requires that I''m using my window coordinates to put graphics on the screen. So if my screen is 640x480 pixels, I need to adress pixel (10,10) directly. To achieve this, I''m using the glEnable2D() and glDisable2D() functions that are presented in the sticky post. At the moment I''m experimenting and put a 2D square that''s rotating. But why are my 2D-graphics always on top of the 3D stuff although 2D is drawn before 3D and 3D is "above" 2D on z-axis? Here is some code:
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
glTranslatef(0.0,0.0,-5.0);
glEnable2D();
glPointSize(10);
glColor3f(0.0f, 0.0f, 1.0f);
for (int i=0; i
Even when I change glTranslatef z-axis parameter so that my square rotates in a way that it would go out of the screen, it doesn''t "overwrite" my 2D put_pixel graphics.
So my first question is: why? Is it possible? I''m asking this question, because later I want to have some 3D-Objects making some eye-candy in the game, and of course these 3D-objects (perhaps text) shall be on top of my 2D field.
Second thing is: I just use this glEnable2D/disable2D functions to use the normal "window" coordinates. Is it still possible to define 3D-objects within Enable2D and Disable2D and then later change the camera perspective so that the 2D objects appear 3D?
I mean it like this: During the game the camera kind of looks on the field from the very top. And later I want the view to be changed so that I see the objects from the side (let''s say 45 degrees). And then they shall appear as 3D because I actually defined the objects as 3D.
So here is some code:
void glEnable2D() {
	int vPort[4];

   glGetIntegerv(GL_VIEWPORT, vPort);

   glMatrixMode(GL_PROJECTION);
   glPushMatrix();
   glLoadIdentity();

   glOrtho(0, vPort[2], 0, vPort[3], -1, 1);
   glMatrixMode(GL_MODELVIEW);
   glPushMatrix();
   glLoadIdentity();
}

void glDisable2D() {
   glMatrixMode(GL_PROJECTION);
   glPopMatrix();   
   glMatrixMode(GL_MODELVIEW);
   glPopMatrix();	
}

int initGL(GLvoid) {
	
	//glEnable(GL_TEXTURE_2D);
	glShadeModel(GL_SMOOTH);
	glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
	
	/* Depth buffer setup */
	glClearDepth(1.0f);
	glEnable(GL_DEPTH_TEST);
	glDepthFunc(GL_LEQUAL);
	/* Really Nice Perspective Calculations */
	glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
	return(TRUE);
}

int resizeWindow(int width, int height) {
	/* Height / width ration */
	GLfloat ratio;
	/* Protect against a divide by zero */
	if ( height == 0 ) height = 1;
	ratio = ( GLfloat )width / ( GLfloat )height;
	glViewport( 0, 0, ( GLint )width, ( GLint )height );
	glMatrixMode( GL_PROJECTION );
	glLoadIdentity( );
	gluPerspective( 45.0f, ratio, 0.1f, 100.0f );
	glMatrixMode( GL_MODELVIEW );
	glLoadIdentity( );
	return( TRUE );
}

void put_pixel(GLint x, GLint y) {
	glBegin(GL_POINTS);
		glVertex2i(x, y);
	glEnd();
}
 
Thanks for any help! -akuryoo ... a small leak will sink a great ship ...

Share this post


Link to post
Share on other sites
quote:
Original post by akuryoo
although 2D is drawn before 3D and 3D is "above" 2D on z-axis?

Is your 3d stuff actually above your 2d though? I don''t see anywhere you ''undo'' the -5 translation, so its applied to both 2d and 3d. Stick a push/pop around your 2d stuff and see how that is.

Also its usual to clear the depth buffer in between mixing 2d and 3d, so that which ever one is drawn last is on top, and the two depths don''t interfere.

Share this post


Link to post
Share on other sites
OK, here is a better example that should assure that the 3D object is actually in front of the 2D object. When the second glClear line is uncommented, then the 3D object totally draws over the 2D object. If it is commented, the 2D object is drawn totally over the 3D one. So now I can draw a 3D object over 2D objects, but: Imagine a square rotated by 45 degrees. Imagine the 2D object's z-coordinate (is this possible to set a 2D object's coordinate in "window" coordinates, or somehow else?) to be the same as the rotated squares center. Then the 2D object should cover the square on the first half of the square and after passing the center (breaking through the square) should be covered by the square, right? Is this possible? Or is such a combination impossible?


glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
glTranslatef(0.0,0.0,-1.0);
glEnable2D();
glPointSize(10);
glColor3f(0.0f, 0.0f, 1.0f);
for (int i=0; i put_pixel(snake1.getX(i),snake1.getY(i));
glDisable2D();
//glClear(GL_DEPTH_BUFFER_BIT);
glRotatef(80.0,0.0,1.0,0.0);
glColor3f(1.0f, 0.0f, 0.0f);

glBegin(GL_QUADS);
glVertex2f(-1.0,-1.0);
glVertex2f(1.0,-1.0);
glVertex2f(1.0,1.0);
glVertex2f(-1.0,1.0);
glEnd();

SDL_GL_SwapBuffers();


Thanks again!

-akuryoo

quote:

I don't see anywhere you 'undo' the -5 translation, so its applied to both 2d and 3d. Stick a push/pop around your 2d stuff and see how that is.



This is done by glEnable2D() and glDisable2D(), isn't it?

[edited by - akuryoo on May 31, 2004 1:26:14 PM]

Share this post


Link to post
Share on other sites