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

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

## 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.vertCount; j++)
{
glLineWidth(1.5);
glBegin(GL_LINE_LOOP);

glVertex2f(shapes.vertices[j].x, shapes.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]

1. 1
Rutin
26
2. 2
3. 3
JoeJ
20
4. 4
5. 5

• 9
• 9
• 9
• 46
• 41
• ### Forum Statistics

• Total Topics
631750
• Total Posts
3002063
×