PDA

View Full Version : what about coordinate system?



beloni
03-14-2007, 11:34 AM
Hello there,
I'm a beginner in OpenGL programming and I do not understand the OpenGL coordinate system! This is too confuse.
I have got some examples programs with GLUT as auxiliar, but each one of them uses a different coord system.

What should I use? Can I you explain to me some issue?

Thanks and I am sorry for my bad English.

vbovio
03-14-2007, 05:21 PM
Try to ask something more specific, like in one of the examples you mention, what parts of the code you don't understand (regarding to coord system) ?

I also suggest you to read the first 3 chapters of the red book, and maybe look at Nehe tutorials.

beloni
03-15-2007, 12:51 PM
Thanks, I'll try be more specific. Here it goes:

int initialize()
{
glClearColor( 0.0, 0.0, 0.0, 0.0 );
glOrtho( 0.0, 100.0, 0.0, 100.0, 0.0, 100.0 );
}here I set the coordinate system to be between 0 and 100 for the three axis, right?


void reshape( GLsizei w, GLsizei h )
{
if(h == 0) h = 1;

glViewport(0, 0, w, h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();

if( w <= h )
gluOrtho2D (0.0f, 250.0f, 0.0f, 250.0f*h/w);
else
gluOrtho2D (0.0f, 250.0f*w/h, 0.0f, 250.0f);
}but in some programs, a reshape function is defined and the coord system is set by gluOrtho2D. Why?


void reshape(int w, int h)
{
glViewport (0, 0, (GLsizei) w, (GLsizei) h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho( 0.0, 640.0, 480.0, 0.0, 0.0, 1.0 );
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}and here, a GL_MODELVIEW is set too...
Thanks

vbovio
03-15-2007, 03:25 PM
to answer your questions:

1. for x and y axis, yes, the last two params have different meaning, look at the red book.

2. gluOrtho2D defines a 2D view only, while glOrtho and glFrustum are used often to define a 3D view. so what you use, totally depends on what you want.

3. that code also sets a 2D view only, in this case modelview matrix was just set to the default.

again, read the first 3 chapters of the red book, specially the third one, it covers the basics of coordinate system, and viewing.

beloni
03-16-2007, 11:21 AM
Thanks again



... so what you use, totally depends on what you want.
I just want allow user to select a bi-dimensional area using the mouse. For that I am doing:

void init_gl()
{
glClearColor( 0.0, 0.0, 0.0, 0.0 );
glOrtho( 0.0, 640.0, 480.0, 0.0, -1.0, 1.0 );
}
.
.
.
void reshape( int w, int h );
.
.
.
void render_scene();
.
.
.
void mouse_handler( int button, int state, GLsizei x, GLsizei y )
{
if( button == GLUT_LEFT_BUTTON &amp;&amp; state == GLUT_DOWN )
{
rect[0][X] = x;
rect[0][Y] = y;
rect[1][X] = x;
rect[3][Y] = y;
}
}
.
.
.
//glutMotionFunc
void motion_handler( GLsizei x, GLsizei y )
{
if( inside_window(x, y) )
{
//printf( "%d,%d\n", x, y );
rect[1][Y] = y;
rect[2][X] = x;
rect[2][Y] = y;
rect[3][X] = x;
glutPostRedisplay(); // draws the rectangle
}
}that's it