Screen Coordinates

Started by
9 comments, last by gauntalus 17 years, 11 months ago
I was wondering if someone could help me out with an easy way to get the x-y screen coordinates for the current camera orientation in an OpenGL scene. I want to be able to draw quads in x-y coordinates to make creation of a menu system easier.
-------------------------Brutal Chess Check out our Development Blog and our Sourceforge project page.
Advertisement
You need to setup an orthographic projection. Try this:

http://www.zeuscmd.com/tutorials/opengl/07-OrthographicProjection.php
Hey, I took a look at the link but I'm not quite sure how this is going to help me.

At this point I have a camera oriented in a certain way, assume some arbitrary orientation. I want to write a function that makes it so I can do something like the following to draw a quad in the lower left quarter of the screen:

glBegin(GL_QUADS);
glVertex3f( 0.0, 0.0, 0.0);
glVertex3f( 0.5, 0.0, 0.0);
glVertex3f( 0.5, 0.5, 0.0);
glVertex3f( 0.0, 0.5, 0.0);
glEnd();

The link that you sent seems to create an entirely new viewport, but I just want to transform my position and orientation so that I can draw in the current viewport easily.
-------------------------Brutal Chess Check out our Development Blog and our Sourceforge project page.
luridcortex is right: you want to set up an orthographic projection. The setup has nothing to do with your camera orientation or position; depending on how you do it, it either permanently (for that frame) or temporarily overwrites the camera modelview and projection matrices for the purpose of rendering in a 2d plane.

I've seen some code for setting this up posted here on gdnet, but I'm not sure where it is. Meanwhile, here are the functions I currently have doing this:

void Renderer::Begin2DRendering(){    Flush();    GLState::SetMatrixMode(GLState::MATRIXMODE_PROJECTION);    GLState::PushMatrix();    GLState::LoadIdentity();    GLState::Ortho2D(        0.0f,        (float)System::Get().GetWindowWidth(),        0.0f,        (float)System::Get().GetWindowHeight()    );    GLState::SetMatrixMode(GLState::MATRIXMODE_MODELVIEW);    GLState::PushMatrix();    GLState::LoadIdentity();    GLState::SetDepthRange(0.0f, 0.0f);}void Renderer::End2DRendering(){    Flush();    GLState::SetMatrixMode(GLState::MATRIXMODE_PROJECTION);    GLState::PopMatrix();    GLState::SetMatrixMode(GLState::MATRIXMODE_MODELVIEW);    GLState::PopMatrix();    GLState::SetDepthRange(0.0f, 1.0f);}

There's some baggage there, but basically it does what it looks like it does.
Hey, things are working out, /sortof/. I tried out the technique the two of you pointed me to, and I am now able to draw quads in 2D using u-v coordinates. However, when I do this, it clobbers everythign in the frame buffer :p Assuming theres already something in the framebuffer that we want to KEEP there, and then overlay a transparent quad in the lower left of the screen, I have the following code:

glMatrixMode(GL_PROJECTION);glLoadIdentity();gluOrtho2D(0, 1, 0, 1);glMatrixMode(GL_MODELVIEW);glLoadIdentity();glDisable(GL_LIGHTING);glEnable(GL_BLEND);glColor4f(1.0, 1.0, 1.0, 0.3);glBegin(GL_QUADS);	glVertex2f(0.0, 0.0);	glVertex2f(0.5, 0.0);	glVertex2f(0.5, 0.5);	glVertex2f(0.0, 0.5);glEnd();


Instead of overlaying the transparent quad in the lower left of the screen, it clears the framebuffer to black and THEN draws a transparent quad in the lower left :) How is it that I can avoid the framebuffer getting cleared?

Thanks for all your help so far, I appreciate it.
-------------------------Brutal Chess Check out our Development Blog and our Sourceforge project page.
I never used OpenGL before but look into the help for enabling the alpha test!

I'm pretty sure this is your problem.

Edit:
I was googlin around for this, learning opengl a little wont hurt me;)

glAlphaFunc(GL_GREATER, 0);
glEnable(GL_ALPHA_TEST);

I didnt tried this, since i dont code opengl normally, but this should works i think. I also think your texture need to have an alpha channel (but i think you have one with your previous posts).

Hopes this help,
Jonathan


[Edited by - LowRad on May 16, 2006 1:59:45 AM]
Hey, I gave that a try (enabling GL_ALPHA_TEST) but I'm still getting the same result. Its hard to tell, but it appears that the screen gets drawn correctly once (with the scene in the background, and a transparent quad drawn in screen coordinates in the foreground) but in the next frame, the scene goes to black and only the quad in the foreground gets drawn. Here's the current state of the code block:

glMatrixMode(GL_PROJECTION);glPushMatrix();glLoadIdentity();gluOrtho2D(0, 1, 0, 1);glMatrixMode(GL_MODELVIEW);glLoadIdentity();glEnable(GL_ALPHA_TEST);glDisable(GL_LIGHTING);glEnable(GL_BLEND);glColor4f(1.0f, 1.0f, 1.0f, 0.3f);glBegin(GL_QUADS);	glVertex2f(0.1f, 0.1f);	glVertex2f(0.8f, 0.1f);	glVertex2f(0.8f, 0.9f);	glVertex2f(0.1f, 0.9f);glEnd();glPopMatrix();

Any other ideas about what the problem might be?
-------------------------Brutal Chess Check out our Development Blog and our Sourceforge project page.
I think this is why!

The default function is GL_ALWAYS.
From the code it looks like you are popping the wrong matrix. Assuming you wanted to pop the PROJECTION matrix, not the MODELVIEW matrix, try adding this:

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

If you want to use alpha blending you will also need to supply the blend function. This will do:

glBlendFunc(GL_SRC_ALPHA, GL_ON_MINUS_SRC_ALPHA);

Also, make sure you are drawing the transparent quad AFTER you draw everything else, or else depth test will kill whatever is underneath the quad.
deathkrushPS3/Xbox360 Graphics Programmer, Mass Media.Completed Projects: Stuntman Ignition (PS3), Saints Row 2 (PS3), Darksiders(PS3, 360)
Instead of using gluOrtho2D try using glOrtho, that is how I do it. Like this:
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();


Hope that helps.


edit: you don't need blending enabled to be able to draw 2d stuff on your screen, thats not what is causing your screen to be black/mess up the framebuffer (as long as you don't wanna draw textures with alpha transparency covering the entire screen).

This topic is closed to new replies.

Advertisement