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