c++openglopengl-4

Drawing a simple rectangle in OpenGL 4


According to this wikibook it used to be possible to draw a simple rectangle as easily as this (after creating and initializing the window):

glColor3f(0.0f, 0.0f, 0.0f);
glRectf(-0.75f,0.75f, 0.75f, -0.75f);

This is has been removed however in OpenGL 3.2 and later versions.

Is there some other simple, quick and dirty, way in OpenGL 4 to draw a rectangle with a fixed color (without using shaders or anything fancy)?


Solution

  • Is there some ... way ... to draw a rectangle ... without using shaders ...?

    Yes. In fact, AFAIK, it is supported on all OpenGL versions in existence: you can draw a solid rectangle by enabling scissor test and clearing the framebuffer:

    glEnable(GL_SCISSOR_TEST);
    glScissor(x, y, width, height);
    glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
    glClear(GL_COLOR_BUFFER_BIT);
    

    This is different from glRect in multiple ways:

    However, I'd rather discourage you from doing this. You're likely to be better off by building a VAO with all the rectangles you want to draw on the screen, then draw them all with a very simple shader.