Understanding and Implementing proper projections

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 rendering in one projection but when having to use glUnProject i have to swap the bottom and top values in my glOrtho call in my projection class since y is in the opposite direction. you can see the swap in the projection class. to render my data i have to use a separate projection function otherwise my data would be drawn upside down. 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.

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.

the problem is is that the rubberband and the data are using different projections, that is the top and bottom values are reversed. how would i solve this? thanks SO much in advance for your help!!

well here is the code:

//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 win_x, win_y, win_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, &win_x, &win_y, &win_z);
   
  if(left_button_down)
  { 
     if(!is_dragging)
     { mouse_start_x = win_x; mouse_start_y = win_y; is_dragging = true; }

     else
      { mouse_current_x = win_x; mouse_current_y = win_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);
    glLoadIdentity();	
	glOrtho(-89.7, -89.3, 30.1, 30.7, 1, -1);
    glMatrixMode(GL_MODELVIEW);
	glLoadIdentity();	
}

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

  glPushMatrix();
   proj->setOrthoProj();
   glDisable(GL_LIGHTING);
  
   if(left_button_down)
	mygrid->draw_border();
   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
    glLoadIdentity(); //reset matrix
    glOrtho(-89.7, -89.3, 30.7, 30.1, -1, 1); //set a 2D orthographic custom projection
	
    glMatrixMode(GL_MODELVIEW);
    glPushMatrix();
    glLoadIdentity();
}

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
	glLoadIdentity(); //reset matrix
	
    glOrtho(-89.7, -89.3, 30.1, 30.7, -1, 1); //set a 2D orthographic custom projection
	
	glMatrixMode(GL_MODELVIEW);
	glPushMatrix();
	glLoadIdentity();
}


//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();
  }

here is an image to look at that might help:

http://img524.imageshack.us/my.php?image=rubberband2qp9.jpg

I am not sure to understand your problem, but if you are experiencing problems with the y axis that points down instead of up, you can do something like this after the glOrtho call:

glOrtho(-89.7, -89.3, 30.1, 30.7, -1, 1);
glScalef(1, -1, 1);

If I am right, after this the y axis points down and your are keep quiet and use your data as it is.

Notice that I reverted bottom and top values in glOrtho.

well my problem is, as per the image link that i have there, the data is drawn and must be drawn with the glOrtho of bottom 30.1 and top 30.7 otherwise if you swap those they would be drawn upside down. the rubberband needs to be flipped because after calling gluUnProject the y-axis is upside down so i must flip it by rearranging the top and bottom so in essence the projection definition is different when drawing the data.

therefore if i wish to zoom into my data using the coordinates of the rubberband i must swap the values back when created the changed glOrtho coordinates.

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. i cannot use the bottom and top values for “zooming into my data” because the top and bottom of the data is in the original glOrtho mode

if i used these numbers my new glOrtho would be:

glOrtho(-89.668, -89.504, 30.65, 30.34);

but since we are in the rubberband projection the top and bottom projection of the data is opposite of this. doing a glScale like you suggest doesn’t seem to help…i cannot even see the rubberband being rendered if i do that.

i sure could make and manipulate the different projections but i was wondering if i wasnt 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.

well my problem is, as per the image link that i have there, the data is drawn and must be drawn with the glOrtho of bottom 30.1 and top 30.7 otherwise if you swap those they would be drawn upside down

No, if I swap those values, that would just move a little bottom and top but certainly not invert them. You just move the clipping plane, Y axis is still pointing up.

What is the purpose of gluUnProject in your code? I see that you use this function to calculate win_x, win_y, win_z (strange names for object coordinates) and then use it as screen coordinates setting mouse coordinates. Should you not use gluProject instead to compute screen coordinates?

glScalef(1, -1, 1) invert the y axis so bottom and top are also inverted. That is why I switched bottom and top values.

Also, I suggest you to use just one function setOrthoProj, giving top and bottom values in parameters, but it is just a code detail, that should not affect program at running time.

And if you have not already done this, read the glOrtho spec, this may help you.

No, if I swap those values, that would just move a little bottom and top but certainly not invert them. You just move the clipping plane, Y axis is still pointing up.

well they don’t, everything is swapped along the y-axis and inverted.

What is the purpose of gluUnProject in your code? I see that you use this function to calculate win_x, win_y, win_z (strange names for object coordinates) and then use it as screen coordinates setting mouse coordinates. Should you not use gluProject instead to compute screen coordinates?

i had tried gluProject at first and used win but figured out that i needed to use gluUnProject(i always get confused between the two) and forgot to change my variable names. but gluUnProject is what i need.

Also, I suggest you to use just one function setOrthoProj, giving top and bottom values in parameters, but it is just a code detail, that should not affect program at running time.

i actually have that now but for making my point i didnt put that in there. :slight_smile:

And if you have not already done this, read the glOrtho spec, this may help you.

i will take a look at that again… thanks…

well they don’t, everything is swapped along the y-axis and inverted.

Oh yes! My mistake, I did not realize that the bottom and top are both positive here.

i had tried gluProject at first and used win but figured out that i needed to use gluUnProject(i always get confused between the two) and forgot to change my variable names. but gluUnProject is what i need.

But, are you sure, you figured out correctly? Actually gluProject and gluUnProject does things radically different.

The first transform object coordinates (3D coordinates you give to opengl with glVertex, for example) to screen coordinates. So object coordinates are multiplied by the modelview matrix and the projection matrix, then normalized (perspective division) and finally mapped to screen coordinates.

The second do exactly the opposite transformation. So comparing mouse coordinates (which are in screen coordinates) with 3D coordinates (even if you are using 2D projection) is suspicious indeed.