2D shapes in a 3D Enviroment.

I have created a very basic 3D cube, which is coloured, I had to use gltranslatef to move the viewpoint back so the cube is in view. Now, I have tried to add a 2d shape to this image, however, nothing appears when I do so. It’s only a simple rectangle. I have tried using gltranslate to move it into view, but doesn’t seem to be working. Does anyone have any ideas please?

Thanx

I’ve looked there…only deals with 2d shapes, or 3d shapes. Not a combination.

I’m not sure how you are drawing your rectangle, but if you are using lighting make sure it has a normal, alsom make sure that it doesn’t point straight down the Z-Axis, then you won’t be able to see it, finally check the depth buffer to make sure its not behind the cube. If you post the code it might help

here is the code.

#include <windows.h> /* must include this before GL/gl.h /
#include <GL/gl.h>
#include <GL/glut.h> /
OpenGL utilities header file */
#include <gl/glaux.h>
#include <stdio.h>
#include <string.h>

float ambientlightsource[] = {0.3, 0.3, 0.45, 1.0};
float source_light[] = {0.9, 0.8, 0.8, 1.0};
float light_pos[] = {7.0, 0.0, 0.0, 1.0};

int DrawGLScene(GLvoid)
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
glTranslatef(0.0f,0.0f, -4.0f);

 glBegin(GL_QUADS); // Start drawing the cube
	 glColor3f(251.0/255.0f,255.0/255.0f,65.0/255.0f);			
 	 glVertex3f( 1.0f, 1.0f,-1.0f);		
 	 glVertex3f(-1.0f, 1.0f,-1.0f);			
	 glVertex3f(-1.0f, 1.0f, 1.0f);			
	 glVertex3f( 1.0f, 1.0f, 1.0f);			
	 glColor3f(255.0/255.0f,106.0/255.0f,8.0/255.0f);			
     glVertex3f( 1.0f,-1.0f, 1.0f);			
	 glVertex3f(-1.0f,-1.0f, 1.0f);			
	 glVertex3f(-1.0f,-1.0f,-1.0f);			
	 glVertex3f( 1.0f,-1.0f,-1.0f);			
	 glColor3f(255.0/255.0f,34.0/255.0f,156.0/255.0f);		
 	glVertex3f( 1.0f, 1.0f, 1.0f);			
	glVertex3f(-1.0f, 1.0f, 1.0f);		
	glVertex3f(-1.0f,-1.0f, 1.0f);		
	glVertex3f( 1.0f,-1.0f, 1.0f);			
	glColor3f(255.0/255.0f,249.0/255.0f,255.0/255.0f);		
	glVertex3f( 1.0f,-1.0f,-1.0f);		
	glVertex3f(-1.0f,-1.0f,-1.0f);		
	glVertex3f(-1.0f, 1.0f,-1.0f);		
	glVertex3f( 1.0f, 1.0f,-1.0f);		
	glColor3f(251.0/255.0f,232.0/255.0f,191.0/255.0f);			
	glVertex3f(-1.0f, 1.0f, 1.0f);			
	glVertex3f(-1.0f, 1.0f,-1.0f);			
	glVertex3f(-1.0f,-1.0f,-1.0f);		
	glVertex3f(-1.0f,-1.0f, 1.0f);			
	glColor3f(115.0/255.0f,255.0/255.0f,254.0/255.0f);		
	glVertex3f( 1.0f, 1.0f,-1.0f);		
	glVertex3f( 1.0f, 1.0f, 1.0f);			
	glVertex3f( 1.0f,-1.0f, 1.0f);			
	glVertex3f( 1.0f,-1.0f,-1.0f);			
glEnd();

return TRUE;

}

void myInit(void)
{

	 glEnable ( GL_DEPTH_TEST );
     glEnable ( GL_LIGHTING );
     glLightModelfv ( GL_LIGHT_MODEL_AMBIENT, ambientlightsource );
     glLightfv ( GL_LIGHT0, GL_DIFFUSE,  source_light );
     glLightfv ( GL_LIGHT0, GL_POSITION, light_pos    );
     glEnable  ( GL_LIGHT0 );
     glEnable ( GL_COLOR_MATERIAL );
     glColorMaterial ( GL_FRONT, GL_AMBIENT_AND_DIFFUSE );
     glEnable     ( GL_CULL_FACE       );
	 glMatrixMode(GL_PROJECTION);
	 gluOrtho2D(0.0, 640.0, 0.0, 480.0);
     glClearColor ( 0.0, 0.0, 0.0, 0.0 );		

}

void RotateXbutton()
{
glColor3f( 1.0, 1.0, 1.0 );
glTranslatef(0.0f, 0.0f, 4.0f);
glBegin(GL_QUADS);
glVertex3i(000, 000, 000);
glVertex3i(640, 000, 000);
glVertex3i(640, 400, 000);
glVertex3i(000, 400, 000);
glFlush();
glEnd();

}

void display(void)
{

glClear ( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
glLightfv ( GL_LIGHT0,GL_POSITION,light_pos ); 	
glLoadIdentity();
glPushMatrix ( );
glTranslatef ( 0.0, 0.0, -4.0 );
DrawGLScene();
glPopMatrix ( );
glLoadIdentity();
RotateXbutton();
glutSwapBuffers ( );

}

void reshape ( int w, int h )
{
glViewport ( 0, 0, w, h );
glMatrixMode ( GL_PROJECTION );
glLoadIdentity ( );
if ( h==0 )
gluPerspective ( 80, ( float ) w, 1.0, 5000.0 );
else
gluPerspective ( 80, ( float ) w / ( float ) h, 1.0, 5000.0 );
glMatrixMode ( GL_MODELVIEW );
glLoadIdentity ( );
}

void main (int argc, char** argv)
{
glutInit(&argc, argv);
glutInitDisplayMode (GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH );
glutInitWindowSize(640, 480);
glutInitWindowPosition(100,150);
glutCreateWindow("");

	glutDisplayFunc(display);
	glutReshapeFunc(reshape);

	myInit();
	glutMainLoop();

}

thanx in advance

I think that when you are calling the command:

glTranslatef(0.0f, 0.0f, 4.0f);

in the RotateXbutton function, you are moving the plane out of your field of view.

Instead, try calling:

glTranslatef(0.0f, 0.0f, -4.0f);

Also, you never define any normals for the rectangle - I would recommend doing that as well.

ok ive changed gltranslate to.

glTranslatef(0.0f, 0.0f, -4.0f);

And now I get a large square behind the cube, but whenever I change the values of the

glVertex3i(640, 000, 000);

I get no change in size at all. What normals do I have to create?

All of the polygons you draw need to have normals, with the cube and square they should be very simple, look at some of the other discussions here, normal calculation is a very common question. What are you changing the values in glVertex3i function to? As a final note, I believe it is better to put glColorMaterial(blah) before glEnable(GL_COLOR_MATERIAL), otherwise you can get weird results

Well, I made the window size 640x480, so I assumed, using glvertex3f, I could plot points of a simple rectangle, on the right side of the screen. However, coz its a 3d world now, it seems to have screwed up a bit. I don’t know much opengl, using normals seems very complicated.

It would be easier on you if you changed your projection matrix for the 2d stuff. Also you might want to disable z buffer testing for the 2d stuff since in your case you want your 2d stuff to be on top.

Try
glMatrixMode( GL_PROJECTION );
glPushMatrix(); //save the previous projection matrix
glOrtho2D( -1.0, 1.0, -1.0, 1.0 ); //this maps the screen so that the upper left corner is -1, 1 and the lower right corner is 1, -1 so it doesnt matter what resolution your app is in
glMatrixMode( GL_MODELVIEW );
glPushMatrix();
glLoadIdentity();
glDisable( GL_DEPTH_TEST ); //diable depth test for this quad
glBegin(GL_QUADS);
glVertex2f(-1, 1);
glVertex2f(-1, -1);
glVertex2f(1, -1);
glVertex2f(1, 1);
glEnd();
glFlush(); //this shouldnt be between the glbegin and glend
glPopMatrix();
glMatrixMode( GL_PROJECTION );
glPopMatrix();
glMatrixMode( GL_MODELVIEW );
glEnable( GL_DEPTH_TEST );

Ooops, this

glMatrixMode( GL_PROJECTION );
glPushMatrix(); //save the previous projection matrix
glOrtho2D( -1.0, 1.0, -1.0, 1.0 ); //this maps the screen so that the upper left corner is -1, 1 and the lower right corner is 1, -1 so it doesnt matter what resolution your app is in

should be this

glMatrixMode( GL_PROJECTION );
glPushMatrix(); //save the previous projection matrix
glLoadIdentity(); //forgot to clear the current projection matrix
glOrtho2D( -1.0, 1.0, -1.0, 1.0 ); //this maps the screen so that the upper left corner is -1, 1 and the lower right corner is 1, -1 so it doesnt matter what resolution your app is in

I have done some testing, and have found out it has something to do with the reshape function.

void
reshape(int w, int h)
{
glViewport(0, 0, w, h);

glMatrixMode(GL_PROJECTION);
glLoadIdentity();
if ( h==0 )
gluPerspective ( 80, ( float ) w, 1.0, 5000.0 );
else
gluPerspective ( 80, ( float ) w / ( float ) h, 1.0, 5000.0 );
//gluOrtho2D(0, w, 0, h); // Y==0 at the bottom
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}

At the moment, it displays the 3d cube fine, but no 2d shape. If i delete the if statement, and bring the gluOrtho2D(0, w, 0, h); into action, then the 2d shape appears and the 3d shape disappears.
Can’t seem to get them on the screen at the same time.

Ah, sorry for not looking at your code a little more. I didnt notice the gluOrtho2D already there.

You are trying to display your 2d object using the projection matrix created by gluPerspective ( 80, ( float ) w / ( float ) h, 1.0, 5000.0 );

Your display function should be

glClear ( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
glLightfv ( GL_LIGHT0,GL_POSITION,light_pos );

glMatrixMode ( GL_PROJECTION );
glLoadIdentity ( );
if ( h==0 )
gluPerspective ( 80, ( float ) w, 1.0, 5000.0 );
else
gluPerspective ( 80, ( float ) w / ( float ) h, 1.0, 5000.0 );
glMatrixMode ( GL_MODELVIEW );
glLoadIdentity ( );

glPushMatrix ( );
glTranslatef ( 0.0, 0.0, -4.0 );
DrawGLScene();
glPopMatrix ( );

glMatrixMode(GL_PROJECTION);
gluOrtho2D(0.0, 640.0, 0.0, 480.0);
glMatrixMode(GL_MODELVIEW); //still translate z to -4
RotateXbutton();
glutSwapBuffers ( );

BTW add,
glMatrixMode ( GL_MODELVIEW );
glLoadIdentity ( );
to your MyInit function at the bottom just in case. You should always change back to the modelview matrix after you are finished with the projection matrix.

[This message has been edited by Kaycee (edited 11-21-2001).]

Well, here is the code so far:

#include <windows.h> /* must include this before GL/gl.h /
#include <GL/gl.h>
#include <GL/glut.h> /
OpenGL utilities header file */
#include <stdio.h>
#include <string.h>

float ambientlightsource[] = {0.3, 0.3, 0.45, 1.0};
float source_light[] = {0.9, 0.8, 0.8, 1.0};
float light_pos[] = {7.0, 0.0, 0.0, 1.0};
float angle1, angle2;
int moving, startx, starty, state;
int w, h;

int cube(GLvoid)
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
glTranslatef(0.0f,0.0f, -4.0f);
glutSolidCube(2.0);
glEnd();

return TRUE;

}

void myInit(void)
{

	 glEnable ( GL_DEPTH_TEST );
     glEnable ( GL_LIGHTING );
     glLightModelfv ( GL_LIGHT_MODEL_AMBIENT, ambientlightsource );
     glLightfv ( GL_LIGHT0, GL_DIFFUSE,  source_light );
     glLightfv ( GL_LIGHT0, GL_POSITION, light_pos    );
     glEnable  ( GL_LIGHT0 );
     glEnable ( GL_COLOR_MATERIAL );
     glColorMaterial ( GL_FRONT, GL_AMBIENT_AND_DIFFUSE );
     glEnable     ( GL_CULL_FACE       );
	 glMatrixMode(GL_PROJECTION);
     glClearColor ( 0.0, 0.0, 0.0, 0.0 );
	 glMatrixMode ( GL_MODELVIEW );
     glLoadIdentity ( );

}

void RotateXbutton()
{
glColor3f( 1.0, 1.0, 1.0 );
glBegin(GL_POLYGON);
glVertex2i(530, 200);
glVertex2i(580, 200);
glVertex2i(580, 220);
glVertex2i(530, 220);
glFlush();
glEnd();
if (mouse == GLUT_LEFT_BUTTON && state == GLUT_DOWN) {
glRotatef(15,1.0,0.0,0.0);
cube();
glutPostRedisplay();
}

}

void display(void)
{

glClear ( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
glLightfv ( GL_LIGHT0,GL_POSITION,light_pos );

glMatrixMode ( GL_PROJECTION );
glLoadIdentity ( );
if ( h==0 )
gluPerspective ( 80, ( float ) w, 1.0, 5000.0 );
else
gluPerspective ( 80, ( float ) w / ( float ) h, 1.0, 5000.0 );
glMatrixMode ( GL_MODELVIEW );
glLoadIdentity ( );

glPushMatrix ( );
glTranslatef ( 0.0, 0.0, -4.0 );
cube();
glPopMatrix ( );

glMatrixMode(GL_PROJECTION);
gluOrtho2D(0.0, 640.0, 0.0, 480.0);
glMatrixMode(GL_MODELVIEW); //still translate z to -4
RotateXbutton();
glutSwapBuffers ( );

}

void
reshape(int w, int h)
{
glViewport(0, 0, w, h);

glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0, w, 0, h); // Y==0 at the bottom
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}

void main (int argc, char** argv)
{
glutInit(&argc, argv);
glutInitDisplayMode (GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH );
glutInitWindowSize(640, 480);
glutInitWindowPosition(100,150);
glutCreateWindow(“2D graphics control of a 3D object”);
glutDisplayFunc(display);
glutReshapeFunc(reshape);
glutMouseFunc (mouse);
myInit();
glutMainLoop();

}

And yet it still won’t display both 2d and 3d graphics. It only does the 2d square at the moment. Its racking my brain.

You forgot to set w and h. In display you have

if ( h==0 )
	gluPerspective ( 80, ( float ) w, 1.0, 5000.0 );
else
	gluPerspective ( 80, ( float ) w / ( float ) h, 1.0, 5000.0 );

but h and w are not set to anything.

if ( h==0 )
gluPerspective ( 80, ( float ) 640, 1.0, 5000.0 );
else
gluPerspective ( 80, ( float ) 640 / ( float ) 480, 1.0, 5000.0 );

and you will see your cube.

Also in display this

glMatrixMode(GL_PROJECTION);
gluOrtho2D(0.0, 640.0, 0.0, 480.0);

should be this

glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0.0, 640.0, 0.0, 480.0);

Remember when you call gluOrtho2D or gluPerspective they multiply the current matrix by the matrix that these functions create.

And do I have to change the reshape function, because I did what you said, and now my 2d shape appears, but no 3d shape.

ok, my display function looks like this.

void display(void)
{

glClear ( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
glLightfv ( GL_LIGHT0,GL_POSITION,light_pos );
glMatrixMode ( GL_PROJECTION );
glLoadIdentity ( );
if ( h==0 )
gluPerspective ( 80, ( float ) 640, 1.0, 5000.0 );
else
gluPerspective ( 80, ( float ) 640 / ( float ) 480, 1.0, 5000.0 );
glMatrixMode ( GL_MODELVIEW );
glLoadIdentity ( );
glPushMatrix ( );
glTranslatef ( 0.0, 0.0, -4.0 );
cube();
glPopMatrix ( );
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0.0, 640.0, 0.0, 480.0);
RotateXbutton();
glutSwapBuffers ( );

}

You dont have to worry about the reshape function now unless you want to save the width and height of the window so you can use it in your display function to set the correct aspect ratio. Next you are clearing the screen 2 times. Once in Display and once in cube(). You should remove that. Plus you are loading the identity matrix again in cube() which negates the gltranslate call in display so you may want to remove that too. There is also a glEnd() call sitting in the cube() function which should not be there and you can remove the glFlush call in RotateXButton because it cant be called within glBegin and glEnd blocks and besides glutSwapBuffers will call it automatically. One more thing in display before the call to RotateXButton you are not calling glMatrixMode( GL_MODELVIEW ); make sure you always change the matrix mode back to modelview after you have set the projection matrix.

So after those changes your cube function will only have glutSolidCube(2.0).

If this doesnt work repost your entire program.

After modifying it, this is the result, and yet still doesn’t seem to work. I’ve probably done something wrong.

#include <windows.h> /* must include this before GL/gl.h /
#include <GL/gl.h>
#include <GL/glut.h> /
OpenGL utilities header file */
#include <stdio.h>
#include <string.h>

float ambientlightsource[] = {0.3, 0.3, 0.45, 1.0};
float source_light[] = {0.9, 0.8, 0.8, 1.0};
float light_pos[] = {7.0, 0.0, 0.0, 1.0};
int w, h;

int cube(GLvoid)
{
glTranslatef(0.0f,0.0f, -4.0f);
glutSolidCube(2.0);
return TRUE;

}

void myInit(void)
{

	 glEnable ( GL_DEPTH_TEST );
     glEnable ( GL_LIGHTING );
     glLightModelfv ( GL_LIGHT_MODEL_AMBIENT, ambientlightsource );
     glLightfv ( GL_LIGHT0, GL_DIFFUSE,  source_light );
     glLightfv ( GL_LIGHT0, GL_POSITION, light_pos    );
     glEnable  ( GL_LIGHT0 );
     glEnable ( GL_COLOR_MATERIAL );
     glColorMaterial ( GL_FRONT, GL_AMBIENT_AND_DIFFUSE );
     glEnable     ( GL_CULL_FACE       );
	 glMatrixMode(GL_PROJECTION);
     glClearColor ( 0.0, 0.0, 0.0, 0.0 );
	 glMatrixMode ( GL_MODELVIEW );
     glLoadIdentity ( );

}

void RotateXbutton()
{
glColor3f( 1.0, 1.0, 1.0 );
glBegin(GL_POLYGON);
glVertex2i(530, 200);
glVertex2i(580, 200);
glVertex2i(580, 220);
glVertex2i(530, 220);
glEnd();
}

void display(void)
{

glClear ( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
glLightfv ( GL_LIGHT0,GL_POSITION,light_pos );
glMatrixMode ( GL_PROJECTION );
glLoadIdentity ( );
if ( h==0 )
gluPerspective ( 80, ( float ) 640, 1.0, 5000.0 );
else
gluPerspective ( 80, ( float ) 640 / ( float ) 480, 1.0, 5000.0 );
glMatrixMode ( GL_MODELVIEW );
glLoadIdentity ( );
glPushMatrix ( );
glTranslatef ( 0.0, 0.0, -4.0 );
cube();
glPopMatrix ( );
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0.0, 640.0, 0.0, 480.0);
glMatrixMode ( GL_MODELVIEW );
glLoadIdentity ( );
RotateXbutton();
glutSwapBuffers ( );

}

void
reshape(int w, int h)
{
glViewport(0, 0, w, h);

glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0, w, 0, h); // Y==0 at the bottom
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}

void main (int argc, char** argv)
{
glutInit(&argc, argv);
glutInitDisplayMode (GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH );
glutInitWindowSize(640, 480);
glutInitWindowPosition(100,150);
glutCreateWindow("");
glutDisplayFunc(display);
glutReshapeFunc(reshape);
myInit();
glutMainLoop();

}

Change this

if ( h==0 )
gluPerspective ( 80, ( float ) 640, 1.0, 5000.0 );
else
gluPerspective ( 80, ( float ) 640 / ( float ) 480, 1.0, 5000.0 );

in display to

gluPerspective ( 80, ( float ) 640 / ( float ) 480, 1.0, 5000.0 );