Sign in to follow this  
jake_Ghost

problem with glOrtho

Recommended Posts

ive got my glOrtho projection working some what ok, but the rest of teh screen is all screwed up now. When i draw a simple box with a texture on it, it gets all deformed or the depth stuff is all messed up. I dont no how to show a screen shot so unfortunatly i cant show u what i mean. Could someone tell me how to fix this problem? thx in advanced Jake

Share this post


Link to post
Share on other sites
Quote:
Original post by jake_Ghost
ive got my glOrtho projection working some what ok, but the rest of teh screen is all screwed up now. When i draw a simple box with a texture on it, it gets all deformed or the depth stuff is all messed up. I dont no how to show a screen shot so unfortunatly i cant show u what i mean. Could someone tell me how to fix this problem?

thx in advanced
Jake

What are you doing using 3d stuff with an orthographic projection matrix? What you want is a perspective correct projection matrix. Orthographics are for things like grids in level editors, 2d text overlays, etc. They're not used for doing things like 3d simulation.
Take a look at gluPerspective()

Share this post


Link to post
Share on other sites
Quote:
Original post by jake_Ghost
well i am using it for a hud, do u no how to turn off glOrtho after i draw the hud?

Use matrix stacks.


// Normal rendering

glPushMatrix();
glLoadIdentity();
glOrtho( ... );

// Do your ortho stuff here

glPopMatrix();

// Back to Perspectice-Correct stuff

Share this post


Link to post
Share on other sites
Quote:
Original post by jake_Ghost
well i am using it for a hud, do u no how to turn off glOrtho after i draw the hud?


You do something like this...

//All rendering of world, objects, etc.

glMatrixMode(GL_PROJECTION);
glPushMatrix();
glLoadIdentity();
glOrtho(...);

glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
/*Assuming you will not need the current modelview matrix anymore after this
since the hud will be drawn last. If you do need to save the modelview
matrix, just push it before glLoadIdentity() and then pop it later
*/


//...Do HUD/whatever rendering here...

//Here you would pop the modelview matrix if you pushed it earlier

glMatrixMode(GL_PROJECTION);
glPopMatrix();
glMatrixMode(GL_MODELVIEW);






EDIT: Beaten, damn comment was too long :P

Share this post


Link to post
Share on other sites
hmmm i cant get it to work heres the code


int GLDraw :: DrawUI(GLvoid)
{
glMatrixMode(GL_PROJECTION);
glLoadIdentity();

glPushMatrix();
glOrtho(0,1024,0,768,-1.0f,1.0f);

glMatrixMode(GL_MODELVIEW);
glLoadIdentity();

glDisable(GL_DEPTH_TEST);

// draws UI

glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glPopMatrix();

glMatrixMode(GL_MODELVIEW);
glLoadIdentity();

glEnable(GL_DEPTH_TEST);

return TRUE;
}

Share this post


Link to post
Share on other sites
You're clearing your matrix, then storing it in the stack.... You want to store it (glPushMatrix), then clear it (glLoadIdentity).
And you don't need to load identity before popping the matrix.

EDIT:
Here:

glDisable(GL_DEPTH_TEST);

glMatrixMode(GL_PROJECTION);
glPushMatrix();
glLoadIdentity();
glOrtho(0,1024,0,768,-1.0f,1.0f);

glMatrixMode( GL_MODELVIEW );
glPushMatrix();
glLoadIdentity();

// Draw UI

glMatrixMode(GL_MODELVIEW);
glPopMatrix();

glMatrixMode(GL_PROJECTION);
glPopMatrix();

glEnable(GL_DEPTH_TEST);

Share this post


Link to post
Share on other sites
Quote:
Original post by jake_Ghost
still dont work for some reason, it looks like the z range is to small for the object so its squishing it but still losing parts of the image.


Yeah, with ortho the way we said with -1 near and 1 far plane values, that would probably not be enough for a 3d object, so if you are rendering 3d objects in an orthographic view you need to increase the far clipping plane value.

Share this post


Link to post
Share on other sites
Quote:
Original post by jake_Ghost
thats the thing tho, i dont want to render a 3d object in ortho projections, i just want to render the UI in ortho and the 3d in non ortho projection.


Hmm, could you post your complete rendering code? And to post a screenshot you need to upload it somewhere (any web space will do) and then link to it in your post. A screenshot would be good.

Share this post


Link to post
Share on other sites
this is my temp rendering stuff cause when i did it with my normal rendering, it was all messed up so i decided just to draw a textured box and see if it does the same thing


#include <windows.h> // Header File For Windows
#include <stdio.h> // Header File For Standard Input/Output
#include <gl\gl.h> // Header File For The OpenGL32 Library
#include <gl\glu.h> // Header File For The GLu32 Library
#include <gl\glaux.h> // Header File For The Glaux Library
#include "DrawScene.h"
#include "BMP.h"
#include "arb_multisample.h"

int GLDraw :: DrawGLScene(GLvoid) // Here's Where We Do All The Drawing
{
this->Reset();

glPushMatrix();
glTranslatef(0,0,zmove);

glEnable(GL_MULTISAMPLE_ARB);

glRotatef(xrot, 1.0f, 0.0f, 0.0f);
glRotatef(yrot, 0.0f, 1.0f, 0.0f);

this->DrawCube( bmp.GetFileName(intTexNum), 0, 0, 0, 1.0f, 1.0f, 1.0f);

glDisable(GL_MULTISAMPLE_ARB);
glPopMatrix();

return TRUE; // Keep Going
}

int GLDraw :: DrawUI(GLvoid)
{
POINT mousePos;

GetCursorPos(&mousePos);

glDisable(GL_DEPTH_TEST);

glMatrixMode(GL_PROJECTION);

glPushMatrix();
glLoadIdentity();
glOrtho( 0, 1024, 768, 0, -1.0f, 1.0f);

glMatrixMode(GL_MODELVIEW);

glPushMatrix();
glLoadIdentity();

this->MapTex("Data/Interface/ui_Bag-4x4.bmp");

glBegin(GL_QUADS);
glTexCoord2f( 0.0f, 1.0f); glVertex3f( mousePos.x, mousePos.y, 0.0);
glTexCoord2f( 0.0f, 0.0f); glVertex3f( mousePos.x, mousePos.y + 256 , 0.0);
glTexCoord2f( 1.0f, 0.0f); glVertex3f( mousePos.x + 256, mousePos.y + 256, 0.0);
glTexCoord2f( 1.0f, 1.0f); glVertex3f( mousePos.x + 256, mousePos.y, 0.0);
glEnd();

glMatrixMode(GL_MODELVIEW);
glPopMatrix();

glMatrixMode(GL_PROJECTION);
glPopMatrix();

glEnable(GL_DEPTH_TEST);

return TRUE; // Keep Going
}

int GLDraw :: DrawLoading(GLvoid)
{
this->Reset();

glDisable(GL_DEPTH_TEST);

glMatrixMode(GL_PROJECTION);

glPushMatrix();
glLoadIdentity();
glOrtho(0,1024,0,768,-1.0f,1.0f);

glMatrixMode( GL_MODELVIEW );

glPushMatrix();
glLoadIdentity();

this->MapTex("Data/Interface/ui_LoadingBar.bmp");

glBegin(GL_QUADS);
glTexCoord2f(0.0f, 1.0f); glVertex3f (256.0,64.0,0.0);
glTexCoord2f(0.0f, 0.0f); glVertex3f (256.0,0.0,0.0);
glTexCoord2f(1.0f, 0.0f); glVertex3f (768.0,0.0,0.0);
glTexCoord2f(1.0f, 1.0f); glVertex3f (768.0,64.0,0.0);
glEnd();

if(LoadAt != 0)
{
this->MapTex("Data/Interface/ui_BarFill.bmp");

// draw main bar
glBegin(GL_QUADS);
glTexCoord2f(0.0f, 1.0f); glVertex3f (286.0,43.0,0.0);
glTexCoord2f(0.0f, 0.0f); glVertex3f (286.0,0.0,0.0);
glTexCoord2f(1.0f, 0.0f); glVertex3f (286 + (int)( (int)(450 / LOADNUM) * LoadAt),0.0,0.0);
glTexCoord2f(1.0f, 1.0f); glVertex3f (286 + (int)( (int)(450 / LOADNUM) * LoadAt),43.0,0.0);
glEnd();
}

glMatrixMode(GL_MODELVIEW);
glPopMatrix();

glMatrixMode(GL_PROJECTION);
glPopMatrix();

glEnable(GL_DEPTH_TEST);

//glDisable(GL_STENCIL_TEST);
return TRUE; // Keep Going
}

GLDraw :: DrawCube(std::string TexName, GLfloat x, GLfloat y, GLfloat z, GLfloat height, GLfloat width, GLfloat depth)
{
this->MapTexType(TexName,FilterType);
glBegin(GL_QUADS);
// Front Face
glTexCoord2f(0.0f, 0.0f); glVertex3f (x-width, y-height, z+depth);
glTexCoord2f(1.0f, 0.0f); glVertex3f (x+width, y-height, z+depth);
glTexCoord2f(1.0f, 1.0f); glVertex3f (x+width, y+height, z+depth);
glTexCoord2f(0.0f, 1.0f); glVertex3f (x-width, y+height, z+depth);
// Back Face
glTexCoord2f(1.0f, 0.0f); glVertex3f (x-width, y-height, z-depth);
glTexCoord2f(1.0f, 1.0f); glVertex3f (x-width, y+height, z-depth);
glTexCoord2f(0.0f, 1.0f); glVertex3f (x+width, y+height, z-depth);
glTexCoord2f(0.0f, 0.0f); glVertex3f (x+width, y-height, z-depth);
// Top Face
glTexCoord2f(0.0f, 1.0f); glVertex3f (x-width, y+height, z-depth);
glTexCoord2f(0.0f, 0.0f); glVertex3f (x-width, y+height, z+depth);
glTexCoord2f(1.0f, 0.0f); glVertex3f (x+width, y+height, z+depth);
glTexCoord2f(1.0f, 1.0f); glVertex3f (x+width, y+height, -1.0f);
// Bottom Face
glTexCoord2f(1.0f, 1.0f); glVertex3f (x-width, y-height, z-depth);
glTexCoord2f(0.0f, 1.0f); glVertex3f (x+width, y-height, z-depth);
glTexCoord2f(0.0f, 0.0f); glVertex3f (x+width, y-height, z+depth);
glTexCoord2f(1.0f, 0.0f); glVertex3f (x-width, y-height, z+depth);
// Right face
glTexCoord2f(1.0f, 0.0f); glVertex3f (x+width, y-height, z-depth);
glTexCoord2f(1.0f, 1.0f); glVertex3f (x+width, y+height, z-depth);
glTexCoord2f(0.0f, 1.0f); glVertex3f (x+width, y+height, z+depth);
glTexCoord2f(0.0f, 0.0f); glVertex3f (x+width, y-height, z+depth);
// Left Face
glTexCoord2f(0.0f, 0.0f); glVertex3f (x-width, y-height, z-depth);
glTexCoord2f(1.0f, 0.0f); glVertex3f (x-width, y-height, z+depth);
glTexCoord2f(1.0f, 1.0f); glVertex3f (x-width, y+height, z+depth);
glTexCoord2f(0.0f, 1.0f); glVertex3f (x-width, y+height, z-depth);
glEnd();
}

GLDraw :: Reset()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
}

void GLDraw::MapTex(const std::string &Filename) const
{
glBindTexture(GL_TEXTURE_2D, bmp.Texture(Filename));
}

void GLDraw::MapTexType(const std::string &Filename, const int TYPE) const
{
glBindTexture(GL_TEXTURE_2D, bmp.Texture(Filename,TYPE));
}




also, when i render everyting w/o the ortho stuff, it looks fine, but as soon as i render the UI it gets messed up.

Share this post


Link to post
Share on other sites
Hrm, that does all look okay. Any chance you could upload a screenshot somewhere? If you don't have any webspace there are free (I think) image hosting sites around, I can't think of any off the top of my head though.

Share this post


Link to post
Share on other sites
alrite thx to all ur help and 1 line more of code the whole messed up thing has been fixed.

every frame i reset the matrix using

glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();

all i had to do was add this

glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();

and now it works lol thx for al ur guys help

Share this post


Link to post
Share on other sites
Quote:
Original post by Optus
It might not actually effect it, but when in Ortho mode try using glVertex2f(x,y) instead of 3f. This is how my ortho system does it, and it all works great.

Calling glVertex2f( x, y ) is equivelent to calling glVertex4f( x, y, 0, 1 ), for the record.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this