0

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?

genpfault
  • 51,148
  • 11
  • 85
  • 139
MateMalte
  • 37
  • 4
  • 3
    Before calling `glOrtho` you should set the matrix mode to `GL_PROJECTION`. The initial value is set to `GL_MODELVIEW` (see: [glMatrixMode](https://registry.khronos.org/OpenGL-Refpages/gl2.1/xhtml/glMatrixMode.xml)). – Erdal Küçük Apr 27 '23 at 10:46
  • ah, yes, I was going to say gluLookAt looked okay to me, but since the ortho projection is in the modelview matrix, glLoadIdentity is overwriting it. You can either save the ortho projection in the projection matrix (which is the normal way), *or* you can re-set the ortho projection after glLoadIdentity which clears it – user253751 Apr 28 '23 at 15:55

0 Answers0