PDA

View Full Version : Move camera away of the polygon + Give depth sensation



jasomisc
03-29-2014, 07:23 AM
Hello

Im trying to program a rotating 3d rectangle.

Now i want to get more distance between the camera and the polygon (that has its center in 0,0,0).
Trying with the gluLookAt function but no success.

And another question, dunno if its related or not:
When viewing the rectangle lines in depth, i see them completely parallel instead of giving depth sensation (the same like when viewing a very long highway).

Any advises?

Thanks!!

Here my code:

#include <stdlib.h>
#include <stdio.h>
#include <GL/glut.h>

//void keyboard(unsigned char key, int x, int y);
void display(void);


int main(int argc, char** argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DEPTH | GLUT_RGBA | GLUT_DOUBLE);
glutCreateWindow("GLUT Test");
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glShadeModel(GL_FLAT);
glEnable(GL_DEPTH_TEST);
// glutFullScreen();
// glutKeyboardFunc(&keyboard);
glutDisplayFunc(&display);

glutMainLoop();

return EXIT_SUCCESS;
}

/*
void keyboard(unsigned char key, int x, int y)
{
switch (key)
{
case '\x1B':
exit(EXIT_SUCCESS);
break;
}
}
*/
void drawCube(float angle)
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

// ????????????????
// gluLookAt (0.0, 0.0, -2.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0);

// Why not necessary to avoid rotating other polygons/lines...???
//glPushMatrix();
glLoadIdentity();
glRotatef(angle, 1.0f, 0.0f, 1.0f);

glBegin(GL_QUADS);

//Front
glNormal3f(0.0f, 0.0f, 0.2f);
glColor3f(0.0f, 0.0f, 0.4f);
glVertex3f(-0.7f, -0.05f, 0.3f);
glVertex3f(0.7f, -0.05f, 0.3f);
glVertex3f(0.7f, 0.05f, 0.3f);
glVertex3f(-0.7f, 0.05f, 0.3f);

//Right
glNormal3f(0.2f, 0.0f, 0.0f);
glColor3f(0.0f, 0.0f, 0.2f);
glVertex3f(0.7f, -0.05f, -0.3f);
glVertex3f(0.7f, 0.05f, -0.3f);
glVertex3f(0.7f, 0.05f, 0.3f);
glVertex3f(0.7f, -0.05f, 0.3f);

//Back
glNormal3f(0.0f, 0.0f, -0.2f);
glColor3f(0.0f, 0.0f, 0.4f);
glVertex3f(-0.7f, -0.05f, -0.3f);
glVertex3f(-0.7f, 0.05f, -0.3f);
glVertex3f(0.7f, 0.05f, -0.3f);
glVertex3f(0.7f, -0.05f, -0.3f);

//Left
glNormal3f(-0.2f, 0.0f, 0.0f);
glColor3f(0.0f, 0.0f, 0.2f);
glVertex3f(-0.7f, -0.05f, -0.3f);
glVertex3f(-0.7f, -0.05f, 0.3f);
glVertex3f(-0.7f, 0.05f, 0.3f);
glVertex3f(-0.7f, 0.05f, -0.3f);

//Upper
glNormal3f(0.0f, 0.2f, 0.0f);
glColor3f(0.0f, 0.0f, 0.3f);
glVertex3f(0.7f, 0.05f, 0.3f);
glVertex3f(-0.7f, 0.05f, 0.3f);
glVertex3f(-0.7f, 0.05f, -0.3f);
glVertex3f(0.7f, 0.05f, -0.3f);

//Bottom
glNormal3f(0.0f, 0.2f, 0.0f);
glColor3f(0.0f, 0.0f, 0.5f);
glVertex3f(0.7f, -0.05f, 0.3f);
glVertex3f(-0.7f, -0.05f, 0.3f);
glVertex3f(-0.7f, -0.05f, -0.3f);
glVertex3f(0.7f, -0.05f, -0.3f);

glEnd();
//glPopMatrix();

glEnd();

glFlush();
glutSwapBuffers();


}

void display()
{
float angle;
angle=0;
int cont=0;
drawCube(angle);


while (cont<1000)
{
drawCube(angle);
if (angle<365)
{
angle+=1;
}
else
{
angle=0;
}
cont++;
usleep(2000);
}
exit(0);

}

tonyo_au
03-29-2014, 04:59 PM
You need to setup a perspective projection matrix with gluPerspective rather than the identity matrix

Steven Katic
03-29-2014, 05:44 PM
http://www.glprogramming.com/red/chapter03.html

Your next step should be to read something like this to understand how gluLookAt works. So loop back there to save us re-interating here.
But in summary (you will find) for:

(1) more distance between the camera and the polygon (that has its center in 0,0,0).

Try gluLookAt (0.0, 0.0, 5.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);
That will effectively move the camera 5 units along the z-axis looking down towards origin(0,0,0) with an up vector(0,1,0).
Increasing 5.0 will pull the 'camera back away from the origin'.
A modeling transformation glTranslatef() with parameters (0.0, 0.0, -5.0) should produce exactly the same effect
as gluLookAt (0.0, 0.0, 5.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0). See link for choosing from the two alternative ways of thinking.

(2) When viewing the rectangle lines in depth, i see them completely parallel instead of giving depth sensation (the same like when viewing a very long highway).

You seem to be decribing a desire for a perspective projection. glFrustum() or gluPerspective() with appropriate parameters passed in can provide this effect (in an additional reshape() function). Again, straight forward simple examples are provided in the link above.