PDA

View Full Version : Help setting a orthographic projection matrix?



kiragl
11-05-2010, 12:09 AM
I am having trouble setting a orthographical projection matrix of dimensions 4x2 with (0,0) being in the center, (-2, -1) being at the bottom left corner, and (2, 1) being at the top right corner.

I use `glutInitWindowSize(600, 300);` to initialize the window size.
In my reshape function, I use `glViewport(0, 0, w, h);` to set the viewport.
Also in my reshape function, I use `gluOrtho2D(-(float)w/h, (float)w/h, -2.0, 2.0);` to set the ortho.

However, when I move my mouse around to see the world coordinates, the bottom left corner is (-1, -1) and the top right corner is (1, 1). Is there something I am doing wrong here? I am assuming since I am setting bottom and top to -2 and 2 respectively in the gluOrtho2D call, it should give me the right coordinates.

Please help me out if you see anything that might be wrong.

Here is my code so far, please ignore the arm variables and drawing functions.




#include <stdio.h>
#include <math.h>
#include <stdlib.h>
#include <iostream>

#if 0 /*unix*/
#include <GL/glut.h>
#endif

#if 1 /*apple */
#include <GLUT/glut.h>
#include <OPENGL/gl.h>
#include <OPENGL/glext.h>
#endif

#if 0 /*windows*/
#include <io.h>
#include <fcntl.h>
#include <glut.h>
#endif

int GW, GH;
bool animfore, animupper;
float foreangle, upperangle;

using namespace std;

void drawShoulder();
void drawUpper();
void drawFore();

void display() {
glClear(GL_COLOR_BUFFER_BIT);
glLoadIdentity();

//draw shoulder
glPushMatrix();
drawShoulder();
//draw upper arm
glPushMatrix();
drawUpper();
//draw forearm
glPushMatrix();
drawFore();
glPopMatrix();
glPopMatrix();
glPopMatrix();

glutPostRedisplay();
glutSwapBuffers();
}

void drawShoulder() {
//cout << "Enters" << endl;

glBegin(GL_POLYGON);
glColor3f((float)30/255, (float)0/255, (float)30/255);
glVertex2f(-2.0, -0.4);
glVertex2f(-2.0, -1.0);
glVertex2f(-1.0, -1.0);
glVertex2f(-1.0, -0.4);
glEnd();
}

void drawUpper() {

}

void drawFore() {

}

void reshape(GLsizei w, GLsizei h) {
GW = w;
GW = h;
glViewport(0, 0, w, h);
glLoadIdentity();
gluOrtho2D(-(float)w/h, (float)w/h, -1.0, 1.0);
cout << "Enters" << endl;
}

void keyboard(unsigned char key, int x, int y) {
switch(key) {
case 'q' : case 'Q' :
exit(EXIT_SUCCESS);
break;
}
}

//control arm animations
void idle() {
if(animupper) {

}
if(animfore) {

}
}
float p2w_x(int gx) {
return (float)2.*GW/(GW*GH-GH)*gx-(float)GW/GH;
}

float p2w_y(int gy) {
int py = GH-1-gy;
return (float)2./(GH-1.)*py-1;
}

void mouseMove(int x, int y) {
cout << "(" << p2w_x(x) << "," << p2w_y(y) << ")" << endl;
}

int main(int argc, char **argv) {
// global variable intializations
GW = 600;
GH = 300;
animfore, animupper = true;

glutInit(&amp;argc, argv);
glutInitDisplayMode(GLUT_DOUBLE);

// initialization
glutInitWindowSize(600, 300);
glutInitWindowPosition(100, 100);
glutCreateWindow("Robot Arm");
glClearColor(1.0, 1.0, 1.0, 1.0);

// callback functions
glutDisplayFunc(display);
glutKeyboardFunc(keyboard);
glutReshapeFunc(reshape);
glutIdleFunc(idle);
glutMotionFunc(mouseMove);

glutMainLoop();
}

kiragl
11-05-2010, 12:36 AM
HELP PLEASE!

aqnuep
11-05-2010, 07:26 AM
I think you should simply use it in the following way:

gluOrtho2D( -2.0, 2.0, -1.0, 1.0 );

You can find more info e.g. here: http://www-evasion.imag.fr/~Francois.Faure/enseignement/ressources/openGL/gluOrtho2D.html

aqnuep
11-05-2010, 07:30 AM
Sorry for the double post.
I think your problem is not setting up the ortho but that you don't set it for the projection matrix. You should rewrite your reshape code like this:

glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(-(float)w/h, (float)w/h, -1.0, 1.0);
glMatrixMode(GL_MODELVIEW);

kiragl
11-05-2010, 04:53 PM
This still doesn't help me create a coordinate system where it is 4x2 and centered at (0,0).

Dark Photon
11-07-2010, 01:54 PM
This still doesn't help me create a coordinate system where it is 4x2 and centered at (0,0).

Why do you think not? The solution to your problem was explicitly given above.

Either you want something other than what it sounds like, or you perhaps do not understand the answer (?) Please give us more detail to work with.