PDA

View Full Version : Working with glFrustum and gluLookAt



raluk22
05-23-2013, 10:38 AM
Hello everybody!

I just started working with OpenGL and I have this problem with glFrustum and gluLookAt. I have to obtain the first image but the best I could get was the second. (image here: fenrir.info.uaic.ro/~raluca.gimbuta/frustum.png)


As you can see I need to get a 1 point perspective, but no matter how I tried I can only get the 3-point perspective on that cube. I read quite a few tutorials and posts about frustum and gluLookAt but none would work for me and I've been stuck on this for 2 days now.

This is my code so far:


glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glFrustum(-5, 5, -5, 5, 5, 100);
gluLookAt(7,7,10,0,0,0,0,1,0);

glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glPushMatrix();
DisplayAxe();
DisplayObiect(); //displays the cube
glPopMatrix();

The cube is of side 5.

Now if anyone could help me or at least give me a hint on how to use glFrustum and gluLookAt to get that 1-point perspective it would be awesome.

Thank you!

uwi2k2
05-23-2013, 10:59 AM
hi,

if the rest of the code is no big secret pls post it.
so i can copy it - modefy it - and post it back ..
thats mutch easyer than anything els :-)

cu
uwi

raluk22
05-23-2013, 11:13 AM
hi,

if the rest of the code is no big secret pls post it.
so i can copy it - modefy it - and post it back ..
thats mutch easyer than anything els :-)

cu
uwi

Thanks a lot :) so this is the whole thing:

The projection() function is where I make the transformation, in Display() case 'p' is where I call it.



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

#include "glut.h"

// window dimension in pixels
#define dim 300

unsigned char prevKey;
GLint k;

GLdouble lat = 5;

void projection();
void DisplayAxe();
void InitObiect();
void DisplayObject();


void Init(void) {
glClearColor(1, 1, 1, 1);


glEnable(GL_DEPTH_TEST);


k = glGenLists(1);
InitObiect();

glMatrixMode(GL_MODELVIEW);
glLoadIdentity();

glMatrixMode(GL_PROJECTION);
glLoadIdentity();
}

void Display()
{
switch(prevKey)
{
case '0':
//reset all
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
DisplayAxe();
break;
case 'A':
DisplayAxe();
break;
case 'C':
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
break;

case 'p':

projection();
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glPushMatrix();


DisplayAxe();

DisplayObject();
glPopMatrix();
break;
default:
break;
}
glutSwapBuffers();
}

void Reshape(int w, int h) {
h = (h == 0) ? 1 : h;
glViewport(0, 0, (GLsizei) w, (GLsizei) h);
}

void KeyboardFunc(unsigned char key, int x, int y) {
prevKey = key;
if (key == 27) // escape
exit(0);
glutPostRedisplay();
}

void MouseFunc(int button, int state, int x, int y) {
}

int main(int argc, char** argv) {

glutInit(&argc, argv);

glutInitWindowSize(dim, dim);

glutInitWindowPosition(100, 100);

glutInitDisplayMode (GL_COLOR_BUFFER_BIT | GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);

glutCreateWindow (argv[0]);

Init();

glutReshapeFunc(Reshape);

glutKeyboardFunc(KeyboardFunc);

glutMouseFunc(MouseFunc);

glutDisplayFunc(Display);
// glutDisplayFunc(DisplayAll);

glutMainLoop();

return 0;
}

void projection() {
glMatrixMode(GL_PROJECTION);
glLoadIdentity();

glFrustum(-5, 5, -5, 5, 5, 100);
gluLookAt(7,7,10,0,0,0,0,1,0);

}

void DisplayAxe() {
int X, Y, Z;
X = Y = 200;
Z = 200;
glLineWidth(2);

// Ox - green
glColor3f(0.1, 1, 0.1);
glBegin(GL_LINE_STRIP);
glVertex3f(0,0,0);
glVertex3f(X,0,0);
glEnd();

// Oy - blue
glColor3f(0.1, 0.1, 1);
glBegin(GL_LINE_STRIP);
glVertex3f(0,0,0);
glVertex3f(0,Y,0);
glEnd();

// Oz - red
glColor3f(1, 0.1, 0.1);
glBegin(GL_LINE_STRIP);
glVertex3f(0,0,0);
glVertex3f(0,0,Z);
glEnd();

glLineWidth(1);
}

void InitObiect() {
glNewList(k, GL_COMPILE);
// face 1
glColor3f(1, 0, 0); // rosu
glBegin(GL_QUADS);
glVertex3d(0, lat, lat);
glVertex3d(lat, lat, lat);
glVertex3d(lat, 0, lat);
glVertex3d(0, 0, lat);
glEnd();
// face 2
glColor3f(1, 1, 0); // galben
glBegin(GL_QUADS);
glVertex3d(lat, 0, 0);
glVertex3d(lat, 0, lat);
glVertex3d(lat, lat, lat);
glVertex3d(lat, lat, 0);
glEnd();
// face 3
glColor3f(0, 1, 0); // verde
glBegin(GL_QUADS);
glVertex3d(0, lat, lat);
glVertex3d(lat, lat, lat);
glVertex3d(lat, lat, 0);
glVertex3d(0, lat, 0);
glEnd();
// face 4
glColor3f(0, 0, 1); // albastru
glBegin(GL_QUADS);
glVertex3d(0, 0, 0);
glVertex3d(lat, 0, 0);
glVertex3d(lat, 0, lat);
glVertex3d(0, 0, lat);
glEnd();
// face 5
glColor3f(1, 0, 1); // magenta
glBegin(GL_QUADS);
glVertex3d(0, 0, lat);
glVertex3d(0, 0, 0);
glVertex3d(0, lat, 0);
glVertex3d(0, lat, lat);
glEnd();
// face 6
glColor3f(0, 1, 1); // cyan
glBegin(GL_QUADS);
glVertex3d(0, lat, 0);
glVertex3d(lat, lat, 0);
glVertex3d(lat, 0, 0);
glVertex3d(0, 0, 0);
glEnd();
glEndList();
}

void DisplayObiect()
{
glCallList(k);
}