Where to call gluLookAt() in OpenGL project?

55 views Asked by At

I understand how the function itself works and I know it should work in principle, but for some reason I can't to seem to get it right.

Here is my project so far:

#include <stdio.h>
#include "glut.h"
#include <iostream>

GLfloat p1[3] = { -0.5, -0.5, -0.5 };
GLfloat p2[3] = { -0.5, -0.5, 0.5 };
GLfloat p3[3] = { -0.5, 0.5, -0.5 };
GLfloat p4[3] = { -0.5, 0.5, 0.5 };
GLfloat p5[3] = { 0.5, -0.5, -0.5 };
GLfloat p6[3] = { 0.5, -0.5, 0.5 };
GLfloat p7[3] = { 0.5, 0.5, -0.5 };
GLfloat p8[3] = { 0.5, 0.5, 0.5 };

void display(void) {
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glShadeModel(GL_SMOOTH);

    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
    
//position 1 I tried
    gluLookAt(
        0, 0, -1, 
        0, 0, 0,
        0, 1, 0);

    glBegin(GL_QUADS);

    // Front
    glColor3f(1.0, 0.0, 0.0); // Red
    glVertex3fv(p1);
    glVertex3fv(p3);
    glVertex3fv(p7);
    glVertex3fv(p5);
    // Back
    glColor3f(1.0, 1.0, 1.0); // White
    glVertex3fv(p4);
    glVertex3fv(p8);
    glVertex3fv(p6);
    glVertex3fv(p2);
    // Top
    glColor3f(0.0, 1.0, 0.0); // Green
    glVertex3fv(p5);
    glVertex3fv(p6);
    glVertex3fv(p8);
    glVertex3fv(p7);
    // Bottom
    glColor3f(1.0, 0.0, 1.0); // Purple
    glVertex3fv(p3);
    glVertex3fv(p1);
    glVertex3fv(p2);
    glVertex3fv(p4);
    // Right
    glColor3f(0.0, 0.0, 1.0); // Blue
    glVertex3fv(p7);
    glVertex3fv(p8);
    glVertex3fv(p4);
    glVertex3fv(p3);
    // Left
    glColor3f(0.0, 1.0, 1.0); // Cyan
    glVertex3fv(p2);
    glVertex3fv(p6);
    glVertex3fv(p5);
    glVertex3fv(p1);

    glEnd();
    glFlush();
    glutSwapBuffers();
}

int main(int argc, char** argv)
{
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB | GLUT_DEPTH);

    glutInitWindowPosition(200, 100);
    glutInitWindowSize(500, 500);
    glutCreateWindow("Cube");
    glOrtho(-((GLdouble)500) / 2, ((GLdouble)500) / 2, ((GLdouble)500) / 2, -((GLdouble)500) / 2, -1.0, 1.0);
    glClearColor(0.0, 0.0, 0.0, 0.0);
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
    
//second position I tried
    //gluLookAt(
        //0, 5, 10,  
        //0, 0, 0,
        //0, 1, 0);

    glEnable(GL_DEPTH_TEST);
    glDepthFunc(GL_LESS);
    glClearDepth(1.0f);
    glDepthMask(true);

    glutIdleFunc(spin);
    glutDisplayFunc(display);

    glutMainLoop();
    return 0;
}

With the "camera" at [0, 0, -1] I see the red face of the cube. And at [0, 0, 1] I see the white one (the backside), which is weird? can someone explain, why that is happening and where gluLookAt() works?

0

There are 0 answers