0
#include <GL/gl.h>
#include <GL/glut.h>

void display();
void init();

int main(int argc, char* argv[])
{
    init();

    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB);
    glutInitWindowSize(320, 240);
    glutCreateWindow("Main Window");
    glutDisplayFunc(display);
    glutMainLoop();

    return 0;
}

void init()
{
    glDisable(GL_DEPTH_TEST);
}

void display()
{
    glClearColor(0, 0, 0, 0);
    glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);

    glLoadIdentity();

    glBegin(GL_QUADS);
    glColor3i(255,255,255);
        glVertex2f(10, 10);
        glVertex2f(100, 10);
        glVertex2f(100, 100);
        glVertex2f(10, 100);
    glEnd();

    glutSwapBuffers();
}

Theoretically, this code should draw a white rectangle. But all I see is a black empty screen. What's wrong?

ApprenticeHacker
  • 21,351
  • 27
  • 103
  • 153

1 Answers1

3

Here is my working example with the changes I made noted by comments:

#include <gl/glut.h>
#include <gl/gl.h>

#define WINDOW_WIDTH 320
#define WINDOW_HEIGHT 240

void display();
void init();

int main(int argc, char* argv[])
{
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB);
    glutInitWindowSize(WINDOW_WIDTH, WINDOW_HEIGHT);
    glutCreateWindow("Main Window");
    init(); // changed the init function to come directly before display function
    glutDisplayFunc(display);
    glutMainLoop();

    return 0;
}

void init()
{
    glClearColor(0, 0, 0, 0); // moved this line to be in the init function
    glDisable(GL_DEPTH_TEST);

    // next four lines are new
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    glOrtho(0.0, WINDOW_WIDTH-1, WINDOW_HEIGHT-1, 0, -1.0, 1.0);
    glMatrixMode(GL_MODELVIEW);
}

void display()
{
    glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
    glLoadIdentity();

    glBegin(GL_QUADS);
    glColor3ub(255,255,255); // changed glColor3i to glColor3ub (see below)
        glVertex2f(10, 10);
        glVertex2f(100, 10);
        glVertex2f(100, 100);
        glVertex2f(10, 100);
    glEnd();

    glFlush(); // added this line 
    //glutSwapBuffers(); // removed this line
}

glColor3ub is the function you want to use if you want to provide colors in the 0-255 range.

Hope this helps.

philipvr
  • 5,738
  • 4
  • 32
  • 44
  • See http://www.opengl.org/sdk/docs/man/xhtml/glFlush.xml. It forces the execution of the preceding gl commands. – philipvr Jul 23 '12 at 05:47
  • @BЈовић See above comment, it won't work with without that line. – philipvr Jul 23 '12 at 05:48
  • 1
    On a side note you should put the code found in `init` into the drawing routing, because that's where it actually belongs. – datenwolf Jul 23 '12 at 08:40
  • why subtract `WINDOW_WIDTH` and `WINDOW_HEIGHT` by 1 in `glOrtho`? – ApprenticeHacker Jul 23 '12 at 10:52
  • 1
    Ok, you need because of `GLUT_SINGLE` mode. Then you do not need `glutSwapBuffers();` – BЈовић Jul 23 '12 at 12:27
  • @IntermediateHacker `glOrtho` is used to specify the coordinates of the left, right, bottom, top, nearer, and farther clipping planes (in that order). Coordinates 0 through 319 will give you a width of 320. – philipvr Jul 23 '12 at 14:14