Sign in to follow this  
OpenGL_Guru

gluUnProject question

Recommended Posts

OpenGL_Guru    104
i am trying to draw a rubberband on the screen using glutMotion but i cant seem to get it quite right. when i try to draw the rubberband it seems to be drawn on the opposite(y axis) end. a screenshot is here you obviously cannot see the mouse but i am actually dragging it at the top of the window yet it is being drawn at the bottom of the window. the output of the mouse coordinates let me know that my numbers are correct but perhaps my reshape function isnt correct or something. can anyone offer any help with this? thanks much in advance! below is the relevant code. main.cpp
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);
    //glPushMatrix();
	glLoadIdentity();	
	//glViewport(0, 0, w, h);
	//gluPerspective(45.0, ratio, 0.1, 100.0);
    gluOrtho2D(-89.7, -89.3, 30.1, 30.7);
    //glScalef(1, 1, 1);
	glMatrixMode(GL_MODELVIEW);
    //glPushMatrix();
	glLoadIdentity();	
}


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



render
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(5, 1);
   }
   glEnable(GL_LIGHTING);
   proj->resetPerspProj();
  glPopMatrix();
}




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
	//gluOrtho2D(0, view[2], 0, view[3]); //set a 2D orthographic projection
    glOrtho(-89.7, -89.3, 30.1, 30.7, -1, 1); //set a 2D orthographic custom projection
	//glScalef(1, -1, 1); //invert the y axis, down is positive
	//glTranslatef(0, -view[3], 0); //mover the origin from the bottom left corner to the upper left corner
	glMatrixMode(GL_MODELVIEW);
	glPushMatrix();
	glLoadIdentity();
}

void projection::resetPerspProj()
{
  glMatrixMode(GL_PROJECTION);
  glPopMatrix();
  glMatrixMode(GL_MODELVIEW);
  glPopMatrix();
}





//grid/rubberband 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;

  cout << mouse.start_x << "," << mouse.start_y << "," << mouse.current_x << "," << mouse.current_y << endl;
}

void grid::draw_border()
{
  if(is_border_valid())
  {
      glColor3f(1, 0, 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();

 }
}





Share this post


Link to post
Share on other sites
Lord_Evil    680
The window origin is at the top left corner so y is down. In OpenGL normally it's the other way round, so you should should swap the top and bottom values of your ortho projection.

Share this post


Link to post
Share on other sites
OpenGL_Guru    104
Quote:
Original post by Lord_Evil
The window origin is at the top left corner so y is down. In OpenGL normally it's the other way round, so you should should swap the top and bottom values of your ortho projection.



both my ortho calls or just in my resize()? i swapped around the glOrtho2D call in resize, just swapped 30.1 and 30.7 around and get nothing at all drawn then.....

Share this post


Link to post
Share on other sites
OpenGL_Guru    104
Quote:
Original post by Lord_Evil
Since you call setOrthoProj() each frame anyway, you should swap the values there.


thanks for the help. so that means the data that needs to be drawn with the original view settings i need to set up a separate projection setting in my projection class with the original top and bottom parameters? i say this because otherwise everything will be drawn upside down. thanks again..

Share this post


Link to post
Share on other sites
Lord_Evil    680
Taking a look at your code again, which projection matrix is used for gluUnproject?
If it's the perspective, then your mouse coords can hardly be correct for rendering with ortho projection.

Share this post


Link to post
Share on other sites
OpenGL_Guru    104
Quote:
Original post by Lord_Evil
Taking a look at your code again, which projection matrix is used for gluUnproject?
If it's the perspective, then your mouse coords can hardly be correct for rendering with ortho projection.


can you explain in more detail?

what i was saying was that while swapping my top and bottom in my projection class worked for the rubberband, i have data that corresponded to the original top and bottom settings of the glOrtho call. since i had swapped them that data was being rendered upside down...so i had mentioned making another function inside my projection class with the original top/bottom parameters to be able to draw them right side up.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this