# OpenGL 2D Projections - am i making things too hard for myself?

## Recommended Posts

hi all...i think i am having a problem understand or maybe just implementing rendering in different projections. below i have some code so you can see what problem i have. basically i am rendering some shapes using GL_LINE_LOOP which are being rendered with glOrtho in the projection class. after reading in the x,y points from a file i conclude what the left, right, top and bottom is so that i am able to draw the shapes. i am specifically giving you those numbers to hopefully give you a better understanding of my problem. in this case the left is -89.7, right is -89.3, bottom is 30.1 and top is 30.7. If i want to draw a rubberband however, i have to use glUnProject and thus the y-axis is upside down so i have to swap the bottom and top values in my glOrtho call in my projection class since y is in the opposite direction. If i don't swap them, as expected, the rubberband would be drawn on the opposite side of the window(depending on where my mouse is dragging). you can see the swap in the projection class itself in the setOrthoProj() function. to render my data i have to use a separate projection function otherwise my data would be drawn upside down. the problem i am having is that when i rubberband over my data i want to be able to "zoom in" whatever i rubberband over. how i want to zoom in of course is figuring out what are the coordinates of the rubberband and make that my new left, right, top, bottom values in my glOrtho call to correctly display the data. the problem is is that the rubberband and the data are using different projections now, that is the top and bottom values are reversed. As an example, let's say my rubberband coordinates are -89.668 for left, -89.504 for the right, 30.65 for the bottom and 30.34 for the top. if i used these numbers my new glOrtho would be: glOrtho(-89.668, -89.504, 30.65, 30.34); the bottom and top values are actually reversed from the data because i had to use gluUnProject as described above. how would i solve this? i am sure that i could make and manipulate the different projections but i was wondering if i wasn't making things hard on myself through a lack of understanding of how all these projections work. in other words is there a way to manipulate these projections without having to make separate functions for each one. it would be nice to have one setOrthoProj function. thanks SO much in advance for your help!! here is the code and an image at the bottom: //in main function

void mouse(int button, int button_state, int x, int y)
{

if(button == GLUT_LEFT_BUTTON && button_state == GLUT_DOWN)
{ left_button_down = true; }
else
{ left_button_down = false; is_dragging = false; }

glutPostRedisplay();
}
void myGlutMotion(int x, int y )
{
GLdouble obj_x, obj_y, obj_z;

GLdouble model_view[16];
glGetDoublev(GL_MODELVIEW_MATRIX, model_view);
GLdouble projection[16];
glGetDoublev(GL_PROJECTION_MATRIX, projection);
GLint viewport[4];
glGetIntegerv(GL_VIEWPORT, viewport);
gluUnProject(x, y, 0, model_view, projection, viewport, &obj_x, &obj_y, &obj_z);

if(left_button_down)
{
if(!is_dragging)
{ mouse_start_x = obj_x; mouse_start_y = obj_y; is_dragging = true; }

else
{ mouse_current_x = obj_x; mouse_current_y = obj_y; }

mygrid->set_mouse_coords(mouse_start_x, mouse_start_y, mouse_current_x, mouse_current_y);
}

glutPostRedisplay();
}

void resize(int w, int h)
{
width = w;
height = h;

if(h == 0)
h = 1;
float ratio = 1.0 * (GLfloat)w / (GLfloat)h;
glMatrixMode(GL_PROJECTION);
glOrtho(-89.7, -89.3, 30.1, 30.7, 1, -1);
glMatrixMode(GL_MODELVIEW);
}

void render()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

glPushMatrix();
proj->setOrthoProj();
glDisable(GL_LIGHTING);

if(left_button_down)
mygrid->draw_border(); //this is the rubberband
else
{
mygrid->draw_border();
mygrid->draw_grid(1, 1);
}
glEnable(GL_LIGHTING);
proj->resetPerspProj();
glPopMatrix();

glPushMatrix();
glColor3f(1, 0, 0);
glDisable(GL_LIGHTING);
proj->setOrthoProj2();
myshapes->renderShapes();
proj->resetPerspProj();
glEnable(GL_LIGHTING);
glPopMatrix();

glutSwapBuffers();

}


//projection class

void projection::setOrthoProj()
{
GLint view[4];

glGetIntegerv(GL_VIEWPORT, view);

// switch to projection mode
glMatrixMode(GL_PROJECTION);
glPushMatrix(); //save previous matrix which contains the settings for the perspective projection
glOrtho(-89.7, -89.3, 30.7, 30.1, -1, 1); //set a 2D orthographic custom projection

glMatrixMode(GL_MODELVIEW);
glPushMatrix();
}

void projection::setOrthoProj2()
{
GLint view[4];

glGetIntegerv(GL_VIEWPORT, view);

// switch to projection mode
glMatrixMode(GL_PROJECTION);
glPushMatrix(); //save previous matrix which contains the settings for the perspective projection

glOrtho(-89.7, -89.3, 30.1, 30.7, -1, 1); //set a 2D orthographic custom projection

glMatrixMode(GL_MODELVIEW);
glPushMatrix();
}


//draw rubberband as part of my draw class.
bool grid::is_border_valid()
{
if(mouse.current_x > mouse.start_x && mouse.current_y > mouse.start_y)
return true;
else
return false;
}

void grid::set_mouse_coords(float s_x, float s_y, float curr_x, float curr_y)
{
mouse.start_x = s_x;
mouse.start_y = s_y;
mouse.current_x = curr_x;
mouse.current_y = curr_y;
}

void grid::draw_border()
{
if(is_border_valid())
{
glColor3f(1, 1, 0);
glBegin(GL_LINE_LOOP);

glVertex2f(mouse.start_x, mouse.current_y);
glVertex2f(mouse.start_x, mouse.start_y);
glVertex2f(mouse.current_x, mouse.start_y);
glVertex2f(mouse.current_x, mouse.current_y);

glEnd();

}
}


//rendering "shapes"
void shapefile::renderShapes()
{
for(int i = 0; i < shapeCount; i++)
{
for(int j = 0; j < shapes[i].vertCount; j++)
{
glLineWidth(1.5);
glBegin(GL_LINE_LOOP);

glVertex2f(shapes[i].vertices[j].x, shapes[i].vertices[j].y);
}
glEnd();
}


Image of rubberband on top of the data: or here is the direct link if the image doesn't show on the post. [Edited by - OpenGL_Guru on August 14, 2008 1:12:13 PM]

## Create an account

Register a new account

• ### Forum Statistics

• Total Topics
627700
• Total Posts
2978695
• ### Similar Content

• A friend of mine and I are making a 2D game engine as a learning experience and to hopefully build upon the experience in the long run.

-What I'm using:
C++;. Since im learning this language while in college and its one of the popular language to make games with why not.     Visual Studios; Im using a windows so yea.     SDL or GLFW; was thinking about SDL since i do some research on it where it is catching my interest but i hear SDL is a huge package compared to GLFW, so i may do GLFW to start with as learning since i may get overwhelmed with SDL.
-Questions
Knowing what we want in the engine what should our main focus be in terms of learning. File managements, with headers, functions ect. How can i properly manage files with out confusing myself and my friend when sharing code. Alternative to Visual studios: My friend has a mac and cant properly use Vis studios, is there another alternative to it?

• Both functions are available since 3.0, and I'm currently using glMapBuffer(), which works fine.
But, I was wondering if anyone has experienced advantage in using glMapBufferRange(), which allows to specify the range of the mapped buffer. Could this be only a safety measure or does it improve performance?
Note: I'm not asking about glBufferSubData()/glBufferData. Those two are irrelevant in this case.
• By xhcao
Before using void glBindImageTexture(    GLuint unit, GLuint texture, GLint level, GLboolean layered, GLint layer, GLenum access, GLenum format), does need to make sure that texture is completeness.
• By cebugdev
hi guys,
are there any books, link online or any other resources that discusses on how to build special effects such as magic, lightning, etc. in OpenGL? i mean, yeah most of them are using particles but im looking for resources specifically on how to manipulate the particles to look like an effect that can be use for games,. i did fire particle before, and I want to learn how to do the other 'magic' as well.
Like are there one book or link(cant find in google) that atleast featured how to make different particle effects in OpenGL (or DirectX)? If there is no one stop shop for it, maybe ill just look for some tips on how to make a particle engine that is flexible enough to enable me to design different effects/magic
let me know if you guys have recommendations.
• By dud3
How do we rotate the camera around x axis 360 degrees, without having the strange effect as in my video below?
Mine behaves exactly the same way spherical coordinates would, I'm using euler angles.
Tried googling, but couldn't find a proper answer, guessing I don't know what exactly to google for, googled 'rotate 360 around x axis', got no proper answers.

References:
Code: https://pastebin.com/Hcshj3FQ
The video shows the difference between blender and my rotation:

• 21
• 14
• 12
• 10
• 12