Home > Back-end >  Drawing a simple rectangle in OpenGL 4
Drawing a simple rectangle in OpenGL 4

Time:10-03

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 deprecated however in OpenGL 4.

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)?

CodePudding user response:

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:

  • The coordinates are specified in pixels relative to the window origin.
  • The rectangle must be axis aligned and cannot be transformed in any way.
  • Most of the per-sample processing is skipped. This includes blending, depth and stencil testing.

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.

  • Related