Délisson Junio
Délisson Junio

Reputation: 1308

OpenGL won't let me draw a thing

I'm facing this weird bug in my code that won't let me draw anything...

void render(void) {
    glClear(GL_COLOR_BUFFER_BIT);

    glLineWidth(4.0);
    glPointSize(4.0);
    glColor3i(0, 1, 1);
    glBegin(GL_POINTS);
        glVertex2i(0, 0);
        glVertex2i(2, 2);
        glVertex2i(3, 3);
    glEnd();
    glutSwapBuffers();
}

int main(int argc, char **argv) {
    int Largura, Altura;

    //width
    Largura = (abs(Plano::MinX) * Plano::EspacoPix) + (abs(Plano::MaxX) * Plano::EspacoPix);
    //height
    Altura  = (abs(Plano::MinY) * Plano::EspacoPix) + (abs(Plano::MaxY) * Plano::EspacoPix);
    // these are cartesian coordinates,
    // at class Plano: enum Dimension {MinY=-50, MaxY=50, MinX=-50, MaxX=50, EspacoPix=5};

    glutInit(&argc, argv);
    glutInitWindowPosition(-1, -1);
    glutInitWindowSize(Largura, Altura);
    glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE);
    glClearColor(1, 1, 1, 1);
    glutCreateWindow("Graphix");
    glMatrixMode(GL_PROJECTION);
    //glEnable(GL_POINT_SMOOTH);
    //glEnable(GL_LINE_SMOOTH);
    gluOrtho2D(Plano::MinX, Plano::MaxX, Plano::MinY, Plano::MaxY); // left, right, bottom, top

    glutDisplayFunc(render);
    glutIdleFunc(render);


    std::cout << "[Dbg] Dimensions:  Height=" << Altura << "px Width=" << Largura << "px " << std::endl;
    std::cout << "[Dbg] Ortho: Left=" << Plano::MinX << ", Right=" << Plano::MaxX << ", Bottom=" << Plano::MinY << ", Top=" << Plano::MaxY << std::endl;
    //glGet with argument GL_COLOR_CLEAR_VALUE
    float cores[4] = {-1, -1, -1, -1}; // defaults
    glGetFloatv(GL_COLOR_CLEAR_VALUE, cores);
    std::cout << "[Dbg] Color Buffer: R=" << cores[0] << " G=" << cores[1] << " B=" << cores[2] << " A=" << cores[3] << std::endl;


    glutMainLoop();
    return(0);
}

All i get is a black screen, and Color buffer, which i though would be 1, 1, 1, 1 is actually 0, 0, 0, 0... And correct me if i'm wrong but the center of the screen is, because of the gluOrtho2D call, x=0, y=0, right? RIGHT? :)

Upvotes: 1

Views: 972

Answers (2)

David C. Bishop
David C. Bishop

Reputation: 6615

glClearColor must be called after glutCreateWindow as the OpenGL context hasn't yet been created.

Also be aware that you are learning out of date OpenGL programming techniques.

glColor, glBegin, glEnd, glVertex*, glColor* glMatrixMode, gluOrtho2D, glu* are all now deprecated functions.

Some people think that they might be eaiser to learn but I don't think it would be by much and thats all stuff you will have to unlearn.

glBegin() and glEnd() cause you to draw objects 1 vertex at a time which is very slow.

The new way involves putting the coordinates into an array of floats and sending it to the graphics card to draw all at once. There's no reason to use that old stuff as everything since OpenGL 1.1 (released in 1997) has had support for Vertex Arrays (although they are now also deprecated and instead are replaced with Vertex Buffer Objects (or VBOs) which are similar in functionality and have been in OpenGL since 1.5 (released in 2005).

I recommend you look at http://www.opengl-tutorial.org/ for newer tutorials that aren't to hard for beginners.

Also my reply here: https://gamedev.stackexchange.com/questions/32876/good-resources-for-learning-modern-opengl-3-0-or-later/32917#32917

Upvotes: 5

TheAmateurProgrammer
TheAmateurProgrammer

Reputation: 9392

I haven't done much OpenGL 2 coding before, but I think you need to call glLoadIdentity() before you call glBegin and you need to call glLoadIdentity() before glMatrixMode.

Upvotes: 0

Related Questions