#### Archived

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

# OpenGL 2d and 3d in opengl

This topic is 6927 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hi could someone explain to me how does the opengl interacts with 2d and 3d? For example if i wan''t to make a bar in 2d on the top of the screen, and then a cube rotating, what do i have to do? I know it has something to do with the glortho and glmatrixmode, but i really don''t understand.... thanks Bruno

##### Share on other sites
Well Bruno,

To do that, you have to set an orthographic matrix:

 glMatrixMode(GL_PROJECTION); glLoadIdentity; glOrtho(0, 640, 480, 0, -1, 1); glMatrixMode(GL_MODELVIEW);

glOrtho creates a matrix that''s multiplied by the current matrix. With glOrtho, you can specify vertices as simple 2D coordinates (assigning 0 to the z coordinate, or using glVertex2*); the values that I''ve put in the function are the coordinates of a 640x480 window, adjust them to your window size...
Now you just draw your 2D bar as a textured quad, or something like that, using normal screen coordinates.

Boa sorte (além disso, meu nome é Bruno também, hehehe),
Nicodemus.

##### Share on other sites
I have a question about using glOrtho like that...
If you were to specify, say, glOrtho(0, 100, 100, 0, -1, 1); could you feed coordinates to OpenGL in relative locations? So that you wouldn''t have to write code specific for a given resolution?

Jonathan

##### Share on other sites
Hi
Ois Bruno(Nicodemus)
É fixe haver aqui alguém que fale português

Well, i made what you told me, and i build a little function to draw a simple bar on the screen, and now, the screen just stays black..., no bar and no rotating object.
Below is my function.., do you know what''s wrong?

void barra(GLfloat x, GLfloat y, GLfloat x1, GLfloat y1, GLfloat maxx,GLfloat maxy)
{
glMatrixMode(GL_PROJECTION);
glOrtho(0,maxx,maxy,0,-1,1);
setTexture(1);
glTexCoord2f(1.0f, 1.0f);

glVertex2f(x,y);
glTexCoord2f(0.0f, 1.0f);

glVertex2f(x1,y);

glTexCoord2f(0.0f, 0.0f); ;
glVertex2f(x1,y1);

glTexCoord2f(1.0f, 0.0f);
glVertex2f(x,y1);

glEnd();

glMatrixMode(GL_MODELVIEW);

}

I then call this bar procedure, and after it, i clean everything with the glLoadIdentity, and make my 3d object.
But everything is black..
Thanks

Bruno

##### Share on other sites
just by looking at it, it seems like you are not using glortho correctly..

The glOrtho function multiplies the current matrix by an orthographic matrix.

void glOrtho(
GLdouble left,
GLdouble right,
GLdouble bottom,
GLdouble top,
GLdouble near,
GLdouble far
);

Parameters
left, right
The coordinates for the left and right vertical clipping planes.
bottom, top
The coordinates for the bottom and top horizontal clipping planes.
near, far
The distances to the nearer and farther depth clipping planes. These distances are negative if the plane is to be behind the viewer.
Remarks
The glOrtho function describes a perspective matrix that produces a parallel projection. The (left, bottom, – near) and (right, top, – near) parameters specify the points on the near clipping plane that are mapped to the lower-left and upper-right corners of the window, respectively, assuming that the eye is located at (0, 0, 0). The –far parameter specifies the location of the far clipping plane. Both near and far can be either positive or negative.

##### Share on other sites
I still can''t do it...
As i understand i designed my ortho projection like this:

glMatrixMode(GL_PROJECTION);
glOrtho(0,800,600,0,0.0,1.0);

Now, if i draw a vertex like this: Verfex2f(200,200), he
sould draw something at 200,200 pixel...

then if i would do this:
glMatrixMode(GL_MODELVIEW);

it should go back to 3d coordinates.
Bu my screen is still black.., and the objects are being drawn with another color

thanks
Bruno

##### Share on other sites
Bruno, you''re calling glOrtho and glLoadIdentity in the wrong order. You want to call glLoadIdentity first, and then glOrtho, so that the identity matrix gets multiplied by an orthographic matrix, and voila! you have your projection set up. The way you have it written down, you''re just setting your projection matrix to an identity one, which means nothing''s going to show up at all, I think

Jonathan

##### Share on other sites
The problem could be in the rest of your code. Are you drawing the bar before a call to glClear() for example? It sounds to me like your bar is being drawn, then the screen is cleared, and the color of the bar texture is being used to draw your objects. Try drawing the bar after drawing your objects. Also, if you are using a z-buffer, make sure you disable it before drawing the bar.

Edited by - Wraith on 5/1/00 3:57:47 PM

##### Share on other sites
nope
thanks for the sugestions but it''s not that also..
I was indeed making glLoadIdentity after glOrtho, i corrected the problem, but it''s the same.. all dark
if i put any comments on my bar procedure, the screen is still black, but if i also comment the glMatrixMode(GL_PROJECTION), then i have my textured planes rotating in the center of the screen... I really have no ideia of what is going on
Could someone look at my code??
If someone is willing to see it, please leave the e-mail.
Thanks
Bruno

##### Share on other sites
Hi Bruno,

Sorry if this is wrong, but the obviousness of the problem just jumped out and kicked me in the nuts (so to speak ).

On top of the LoadIdentity problem identified by Jonathan, you are not setting your matrixmode back to modelview before drawing the bar.

void barra(GLfloat x, GLfloat y, GLfloat x1, GLfloat y1, GLfloat maxx,GLfloat maxy){//set projectionglMatrixMode(GL_PROJECTION);glLoadIdentity();glOrtho(0,maxx,maxy,0,-1,1);// set matrixmode to modelview (and clear it)glMatrixMode(GL_MODELVIEW);glLoadIdentity();// Now draw your barsetTexture(1);glBegin(GL_QUADS);glTexCoord2f(1.0f, 1.0f); glVertex2f(x,y);glTexCoord2f(0.0f, 1.0f); glVertex2f(x1,y);glTexCoord2f(0.0f, 0.0f); ;glVertex2f(x1,y1);glTexCoord2f(1.0f, 0.0f); glVertex2f(x,y1);glEnd();}

Hope that helps

-------------
squirrels are a remarkable source of protein...

• 10
• 13
• 57
• 11
• 84