Myrium
Myrium

Reputation: 205

OpenGL glDrawElements only Black Screen

i was trying to draw a cube, using the glDrawElements function, but even the simple code below only gives me a black screen. If helps, i'm programming on XCode 6.

//
//  main.cpp
//  Copyright (c) 2014 Guilherme Cardoso. All rights reserved.
//

#include <iostream>
#include <OpenGL/OpenGL.h>
#include <GLUT/GLUT.h>
#include <vector>
#include <math.h>

const GLfloat width = 500;
const GLfloat height = 500;

GLubyte cubeIndices[24] = {0,3,2,1,2,3,7,6
    ,0,4,7,3,1,2,6,5,4,5,6,7,0,1,5,4};

GLfloat vertices[][3] =
{{-1.0,-1.0,-1.0},{1.0,-1.0,-1.0},
    {1.0,1.0,-1.0}, {-1.0,1.0,-1.0}, {-1.0,-1.0,1.0},
    {1.0,-1.0,1.0}, {1.0,1.0,1.0}, {-1.0,1.0,1.0}};

GLfloat colors[][3] =
{{0.0,0.0,0.0},{1.0,0.0,0.0},
    {1.0,1.0,0.0}, {0.0,1.0,0.0}, {0.0,0.0,1.0},
    {1.0,0.0,1.0}, {1.0,1.0,1.0}, {0.0,1.0,1.0}};

void display(){
    glEnableClientState(GL_COLOR_ARRAY);
    glEnableClientState(GL_VERTEX_ARRAY);
    glColorPointer(3, GL_FLOAT, 0, colors);
    glVertexPointer(3, GL_FLOAT, 0, vertices);

    //glDrawArrays(GL_QUADS, 0, 24);
    glDrawElements(GL_QUADS, 24,GL_UNSIGNED_BYTE, cubeIndices);
    glDisableClientState(GL_VERTEX_ARRAY);
    glutSwapBuffers();
}

void mouse(int button, int state, int x, int y){

}

void keyboard(unsigned char key, int x, int y){
    if(key=='q' || key == 'Q') exit(0);
}

void init(){
    glEnable(GL_DEPTH_TEST);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    gluOrtho2D(0,width ,height, 0);
    glMatrixMode(GL_MODELVIEW);

    glClearColor (1.0, 1.0, 1.0,1.0);
    //glColor3f(0.0,0.0,0.0);
}

void idle(){

}

int main(int argc,  char **argv) {
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
    glutInitWindowSize(width, height);
    glutCreateWindow("Assignment 3");
    glutPositionWindow(0, 0);


    glutDisplayFunc(display);
    glutMouseFunc(mouse);
    glutKeyboardFunc(keyboard);
    glutIdleFunc(idle);

    init();
    glutMainLoop();
}

i already checked some tutorials, and is no much different of what i'm doing.

Upvotes: 0

Views: 1924

Answers (2)

jozxyqk
jozxyqk

Reputation: 17266

Actually your code is drawing the cube just fine. Look closely :P

enter image description here

The main issue is your projection. The initial GL viewing volume is a -1 to 1 cube, but the cube you're drawing is -1 to 1. The call to gluOrtho2D defines the projection in OpenGL coordinates, which you make the same as pixels, but since your cube is -1 to 1 it is only two pixels big, the rest being offscreen.

Instead, drop the gluOrtho2D, which sets the Z dimension -1 to 1 and only allows you to set X/Y, and create a slightly bigger projection...

glOrtho(-2, 2, -2, 2, -2, 2);

Note: As @derhass suggests, calling glClear is important especially with depth testing enabled (without it, the cube from the last draw call will hide the updated cube).

Upvotes: 1

derhass
derhass

Reputation: 45322

You never clear the color and especially the depth buffer. You should do that at the beginning of your display() function.

Also, your matrix setup is a bit weird. You set up some ortho projection for the pixels, but try to draw a cube in the range [-1,1], so it will be 2 pixel wide on the screen.

Upvotes: 2

Related Questions