Search code examples
xcodeopengldepth

Depth using OpenGL in Xcode


My problem is that I have a project in Xcode where I have to use OpenGL to create two atoms, one of them is in the center on the window, and the other is spinning around the first. My problem is that it seems that there are no depth. The spinning atom never pass behind the other.

I have this code:

#include <GLUT/glut.h>
#include <OpenGL/gl.h>
#include <OpenGL/glu.h>

const int W_WIDTH = 500;
const int W_HEIGHT = 500;
GLfloat Rot = 0;

void Display(void) {
    glEnable(GL_DEPTH_TEST);
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glMatrixMode(GL_MODELVIEW); // Se activa la matriz del modelador
    glLoadIdentity(); //Se pone a "0" realmente al 1

    // Boramos la pantalla
    glOrtho(-500.0f, 500.0f, -500.0f, 500.0f, -500.0f, 500.0f);

    //glPushMatrix();
    glTranslatef(0.0f, 0.0f, -100.0f); // Se traslada todo al -100

    // Red Nucleus
    glColor3f(255, 0, 0);
    glutSolidSphere(12.0f, 20, 20); // Se dibuja una esfera


    glPushMatrix();
    // First Electron Orbit
    // Se hace un push copiamos la traslacion de -100 a la Pila
    // Save viewing transformation
    // Rotate by angle of revolution
    //Sumamos a la translacion -100 una rotacion = -100 + rotacion

    glRotatef(Rot, 0.0f, 1.0f, 0.0f);
    // Translate out from origin to orbit distance
    glTranslatef(90.0f, 0.0f, 0.0f); //Sumamos a la -100 + rotacion una nueva traslacion  = -100 + rotacion + 90
    glColor3f(0, 00, 100);
    glutSolidSphere(8.0f, 20, 20); // Draw the electron
    // Se recupera la matriz de la pila quie era -100
    /* Se dibujan los siguiente electrone.*/

    glPopMatrix();

    /*
        glPushMatrix();
        glColor3f(0, 00, 100);
        glRotatef(fAngulo, 0.0f, 1.0f, 0.0f);
        glTranslatef(-90.0f, 0.0f, 0.0f);
        glutSolidSphere(6.0f, 20, 20);
        fAngulo = fAngulo + 0.03;
        glPopMatrix();
    */

    glutSwapBuffers();// Se limpian los buffers
    glFlush();
}

void idle(void) {
    Rot += 0.01;
    if(Rot > 360.0f)
        Rot = 0.0f;
    glutPostRedisplay();
}



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

    glutInit(&argc, argv);
    //Inicializa la ventana en una determinada posicion
    glutInitDisplayMode (GLUT_RGBA | GLUT_DOUBLE); // Nombre de la ventana
    glutInitWindowPosition(0, 0);
    //Inicializa el tamano de la funcion
    glutInitWindowSize (W_WIDTH, W_HEIGHT); //Inicaliza el modeo de display, RGBA y Doble buffer

    glutCreateWindow("Ventana");

    glutDisplayFunc(Display);
    glutIdleFunc(idle);

    glutMainLoop();
    return 0;
}

Solution

  • glutInitDisplayMode (GLUT_RGBA | GLUT_DOUBLE);
    

    How were you planning on doing depth testing without a depth buffer? Slap a GLUT_DEPTH on the back of that OR train.