3

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

traveh
  • 2,700
  • 3
  • 27
  • 44
  • 1
    "*This is deprecated however in OpenGL 4.*" No, it has been *removed* from GL 3.2 (core profile) and above. "Deprecated" means "available, but subject to future removal". – Nicol Bolas Oct 02 '22 at 02:20
  • @NicolBolas you are totally right... I corrected it now :) – traveh Oct 02 '22 at 20:51

1 Answers1

3

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.

Yakov Galka
  • 70,775
  • 16
  • 139
  • 220
  • This is great... I actually want to use this for picking with the OpenGL picking "hack" (just to mark a certain rectangle with a special "color"), which makes the pixel coordinates even better for me... and also in this case performance shouldn't really be an issue since it only happens pretty rarely... (right?) – traveh Oct 02 '22 at 06:06
  • @traveh if performance doesn't matter then sure, by all means, use whatever is easiest for you. – Yakov Galka Oct 03 '22 at 03:18