Jump to content
  • Advertisement
Sign in to follow this  
CyanPrime

OpenGL glOrtho - how does it work?

This topic is 2682 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

I'm trying to get it so I can draw on openGL's screen like a normal sdl screen, so 1 unit in GL space is 1 pixel. I heard you can use glOrtho to do this, but all my attempts have failed. This is what I got so far:


void InitGL(int Width, int Height) // We call this right after our OpenGL window is created.
{

glViewport(0, 0, Width, Height);
glClearColor(0.0f, 0.0f, 0.0f, 0.0f); // This Will Clear The Background Color To Black
glClearDepth(1.0); // Enables Clearing Of The Depth Buffer
glDepthFunc(GL_LESS); // The Type Of Depth Test To Do
glEnable(GL_DEPTH_TEST); // Enables Depth Testing
//glShadeModel(GL_SMOOTH); // Enables Smooth Color Shading
glEnable(GL_TEXTURE_2D); // Enable Texture Mapping ( NEW )
glShadeModel(GL_FLAT);
glMatrixMode(GL_PROJECTION);
glEnable(GL_BLEND);
glBlendFunc(GL_ONE , GL_ONE_MINUS_SRC_ALPHA);
glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);

glLoadIdentity(); // Reset The Projection Matrix

gluPerspective(45.0f,(GLfloat)Width/(GLfloat)Height,0.1f,1000.0f); // Calculate The Aspect Ratio Of The Window

glMatrixMode(GL_MODELVIEW);
glOrtho(0, Width, 0, Height, 0.0f, 100.0f);
}


and this is where I draw:


void DrawGLScene()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // Clear The Screen And The Depth Buffer
glLoadIdentity(); // Reset The View

glTranslatef(-1.5f,0.0f,-995.0f); // Move Left 1.5 Units And Into The Screen 6.0

// draw a square (quadrilateral)
glBindTexture(GL_TEXTURE_2D, tilemap);
glBegin(GL_QUADS); // start drawing a polygon (4 sided)
// Front Face

for(int x = 0; x < lv.width; x++){
for(int y = 0; y < lv.height; y++){
if((x * lv.tileSize) - worldX > -lv.tileSize && (y * lv.tileSize) - worldY > -lv.tileSize &&
(x * lv.tileSize) - worldX < 640 && (y * lv.tileSize) - worldY < 480){
int tileXCoord = 0;
int tileYCoord = 0;
tilemapWidth = 192;
// glGetTexLevelParameteriv(GL_TEXTURE_2D, 0, GL_TEXTURE_WIDTH, &tilemapWidth);
int tileSheetWidth = tilemapWidth / lv.tileSize;

if (lv.tile[x][y] != 0)
{
tileXCoord = lv.tile[x][y] % tileSheetWidth;
tileYCoord = lv.tile[x][y] / tileSheetWidth;
}

glTexCoord2f(tileXCoord* (1.0f/(tilemapWidth/lv.tileSize)) + 0.05, (tileYCoord + 1) * (1.0f/(tilemapWidth/lv.tileSize)) - 0.05); glVertex3f(x * lv.tileSize, y * lv.tileSize, 1.0f); // Bottom Left Of The Texture and Quad
glTexCoord2f((tileXCoord + 1) * (1.0f/(tilemapWidth/lv.tileSize)) - 0.05, (tileYCoord + 1) * (1.0f/(tilemapWidth/lv.tileSize)) - 0.05); glVertex3f((x + 1) * lv.tileSize, y * lv.tileSize, 1.0f); // Bottom Right Of The Texture and Quad
glTexCoord2f((tileXCoord + 1) * (1.0f/(tilemapWidth/lv.tileSize)) - 0.05, tileYCoord* (1.0f/(tilemapWidth/lv.tileSize)) + 0.05); glVertex3f((x + 1) * lv.tileSize, (y + 1) * lv.tileSize, 1.0f); // Top Right Of The Texture and Quad
glTexCoord2f(tileXCoord* (1.0f/(tilemapWidth/lv.tileSize)) + 0.05, tileYCoord* (1.0f/(tilemapWidth/lv.tileSize)) + 0.05); glVertex3f(x * lv.tileSize, (y + 1) * lv.tileSize, 1.0f); // Top Left Of The Texture and Quad

/*
glTexCoord2f(0.0f, 1.0f); glVertex3f(-1.0f, -1.0f, 1.0f); // Bottom Left Of The Texture and Quad
glTexCoord2f(1.0f, 1.0f); glVertex3f( 1.0f, -1.0f, 1.0f); // Bottom Right Of The Texture and Quad
glTexCoord2f(1.0f, 0.0f); glVertex3f( 1.0f, 1.0f, 1.0f); // Top Right Of The Texture and Quad
glTexCoord2f(0.0f, 0.0f); glVertex3f(-1.0f, 1.0f, 1.0f); // Top Left Of The Texture and Quad
*/
// apply_surface(tileXCoord * lv.tileSize, tileYCoord * lv.tileSize, lv.tileSize, lv.tileSize, (x * lv.tileSize) - worldX, (y * lv.tileSize) - worldY, tilemap, screen);
}
}
}
glEnd(); // done with the polygon

// swap buffers to display, since we're double buffered.
SDL_GL_SwapBuffers();
}


Can anyone tell me what I'm doing wrong?

Share this post


Link to post
Share on other sites
Advertisement
GL gives you two matrices - the projection matrix (decides how things get 'warped' onto the screen), and the modelview matrix (decides where to place objects).

For your projection matrix you usually either choose an Ortho (2d view) or a Perspective (3d view).

For the modelview matrix, you'll use loadidentity, translate, etc to choose where objects appear.

What you're doing is, you're setting the projection matrix to a perspective view. Then you're setting the modelview matrix to an ortho view. Then you're overwriting the modelview with identity. Then you're adding transformations to it with glTranslatef.
The result is your ortho view is erased, and you've still got your perspective view.

You want something like:--Once:
glMatrixMode(GL_PROJECTION);//configuring projection matrix now
glLoadIdentity();//reset matrix
glOrtho(0, Width, 0, Height, 0.0f, 100.0f);//set a 2d projection matrix
//N.B. gluPerspective is not used!

--Every frame:
glMatrixMode(GL_MODELVIEW);//configuring modelview matrix now
glLoadIdentity();//reset matrix
glTranslatef(...);//position the camera/object

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!