PDA

View Full Version : View and transform issues



linhhd
03-03-2015, 09:56 AM
Hi everyone, im very new to opengl so my questions here might be a bit dull, hope you dont mind help me out :D
Thing is, im reading the red book and try to mess up with every code example to gain understanding, and here my mess:



void display(void)
{
/* clear all pixels */
glClear (GL_COLOR_BUFFER_BIT);

/* draw white polygon (rectangle) with corners at
* (-0.5, -0.5, 0.0) and (0.5, 0.5, 0.0)
*/
glColor3f (1.0, 1.0, 1.0);
glLoadIdentity();
gluLookAt(0.0,0.0,2.0, 0.0,0.0,-100.0, 0.0,1.0,0.0);
glBegin(GL_POLYGON);
glVertex2f (-0.5, -0.5);
glVertex2f (0.5, -0.5);
glVertex2f (0.5, 0.5);
glVertex2f (-0.5, 0.5);
glEnd();
glFlush ();
}

void init (void)
{
/* select clearing (background) color */
glClearColor (0.0, 0.0, 0.0, 0.0);

/* initialize viewing values */
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
//glOrtho(0.0, 1.0, 0.0, 1.0, 1.0, -1.0);
//glFrustum(2.0,1.0,-1.0,1.0,1.5,20.0);
gluOrtho2D(-1.0,1.0,-1.0,1.0);
}
int main(int argc, char** argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize (500,450);
glutInitWindowPosition(100,100);
glutCreateWindow("OpenGL");
init();
glutDisplayFunc(display);
glutMainLoop();
return 0;
}

First of all, i use the default camera position and orientation and things went fine but after that, i slightly modified the position
then, the screen is completely black (with all the three projections at the init()), and i cant get it. As i thought, with
gluLookAt(0.0,0.0,2.0, 0.0,0.0,-100.0, 0.0,1.0,0.0); i just moved the camera 2 units along the z-axis while still aiming at (0,0,-100) which supposed to take no effect at all.

Im not very good at English so i might get something wrong from the book, please correct me :confused:

GClements
03-04-2015, 05:54 PM
As i thought, with


gluLookAt(0.0,0.0,2.0, 0.0,0.0,-100.0, 0.0,1.0,0.0);

i just moved the camera 2 units along the z-axis while still aiming at (0,0,-100) which supposed to take no effect at all.

It doesn't change the orientation. It does change the origin. The result is equivalent to glTranslatef(0,0,-2).

Note that your projection matrix is set with gluOrtho2D and your vertices with glVertex2f, which sets the Z coordinate to zero.

Moving the transformation origin by 2 units in the Z direction results in the transformed vertices having a Z coordinate of -2, which is on the wrong side of the near plane (gluOrtho2D() sets the near and far planes at 1). Use glOrtho() if you want to set the near and far planes yourself (as the name implies, gluOrtho2D() is intended for 2D rendering; transforming Z coordinates doesn't make much sense for 2D).

Also, use [CODE] tags for code; [QUOTE] should be reserved for citations.

linhhd
03-04-2015, 09:24 PM
thanks for the reply, and as you saying, i realized that the first 3 parameter in gluLookAt() specify the origin of the coordinate system right?? but still, even if i use glOrtho(), like
glOrtho(-2.0, 2.0, -2.0, 2.0, 1.0, 100.0); which have the viewing volume completely contains my polygon, and...yup, it still remains black, nothing change, maybe i misunderstand the projection too, so could you plz provide some projections that could help reveal my polygon?

P/s: i know that the code i've made makes no sense at all but as i mentioned before, i just mess everything up to get different results to gain understanding

GClements
03-07-2015, 06:46 AM
even if i use glOrtho(), like
glOrtho(-2.0, 2.0, -2.0, 2.0, 1.0, 100.0); which have the viewing volume completely contains my polygon
No it doesn't. glVertex2f() sets Z=0, but the view volume set by that call limits Z to the range -1 to -100.