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: