Sign in to follow this  
OpenGL_Guru

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

Recommended Posts

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




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]

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  

  • Similar Content

    • By povilaslt2
      Hello. I'm Programmer who is in search of 2D game project who preferably uses OpenGL and C++. You can see my projects in GitHub. Project genre doesn't matter (except MMO's :D).
    • By ZeldaFan555
      Hello, My name is Matt. I am a programmer. I mostly use Java, but can use C++ and various other languages. I'm looking for someone to partner up with for random projects, preferably using OpenGL, though I'd be open to just about anything. If you're interested you can contact me on Skype or on here, thank you!
      Skype: Mangodoor408
    • By tyhender
      Hello, my name is Mark. I'm hobby programmer. 
      So recently,I thought that it's good idea to find people to create a full 3D engine. I'm looking for people experienced in scripting 3D shaders and implementing physics into engine(game)(we are going to use the React physics engine). 
      And,ye,no money =D I'm just looking for hobbyists that will be proud of their work. If engine(or game) will have financial succes,well,then maybe =D
      Sorry for late replies.
      I mostly give more information when people PM me,but this post is REALLY short,even for me =D
      So here's few more points:
      Engine will use openGL and SDL for graphics. It will use React3D physics library for physics simulation. Engine(most probably,atleast for the first part) won't have graphical fron-end,it will be a framework . I think final engine should be enough to set up an FPS in a couple of minutes. A bit about my self:
      I've been programming for 7 years total. I learned very slowly it as "secondary interesting thing" for like 3 years, but then began to script more seriously.  My primary language is C++,which we are going to use for the engine. Yes,I did 3D graphics with physics simulation before. No, my portfolio isn't very impressive. I'm working on that No,I wasn't employed officially. If anybody need to know more PM me. 
       
    • By Zaphyk
      I am developing my engine using the OpenGL 3.3 compatibility profile. It runs as expected on my NVIDIA card and on my Intel Card however when I tried it on an AMD setup it ran 3 times worse than on the other setups. Could this be a AMD driver thing or is this probably a problem with my OGL code? Could a different code standard create such bad performance?
    • By Kjell Andersson
      I'm trying to get some legacy OpenGL code to run with a shader pipeline,
      The legacy code uses glVertexPointer(), glColorPointer(), glNormalPointer() and glTexCoordPointer() to supply the vertex information.
      I know that it should be using setVertexAttribPointer() etc to clearly define the layout but that is not an option right now since the legacy code can't be modified to that extent.
      I've got a version 330 vertex shader to somewhat work:
      #version 330 uniform mat4 osg_ModelViewProjectionMatrix; uniform mat4 osg_ModelViewMatrix; layout(location = 0) in vec4 Vertex; layout(location = 2) in vec4 Normal; // Velocity layout(location = 3) in vec3 TexCoord; // TODO: is this the right layout location? out VertexData { vec4 color; vec3 velocity; float size; } VertexOut; void main(void) { vec4 p0 = Vertex; vec4 p1 = Vertex + vec4(Normal.x, Normal.y, Normal.z, 0.0f); vec3 velocity = (osg_ModelViewProjectionMatrix * p1 - osg_ModelViewProjectionMatrix * p0).xyz; VertexOut.velocity = velocity; VertexOut.size = TexCoord.y; gl_Position = osg_ModelViewMatrix * Vertex; } What works is the Vertex and Normal information that the legacy C++ OpenGL code seem to provide in layout location 0 and 2. This is fine.
      What I'm not getting to work is the TexCoord information that is supplied by a glTexCoordPointer() call in C++.
      Question:
      What layout location is the old standard pipeline using for glTexCoordPointer()? Or is this undefined?
       
      Side note: I'm trying to get an OpenSceneGraph 3.4.0 particle system to use custom vertex, geometry and fragment shaders for rendering the particles.
  • Popular Now