4

I would like to know if it is possible to erase parts of any drawing in OpenGL? Lets say I have drawn two lines with my mouse and those lines are overlapping at some points.

Is it possible to erase just one line? Is there a more or less simple approach?

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
buddy
  • 821
  • 2
  • 12
  • 30

5 Answers5

9

OpenGL does not store what you draw. If you draw a line in OpenGL, then OpenGL will take that line, perform various math operations on it, and write pixels into a framebuffer that makes the shape of a line. OpenGL does not remember that you drew a line; all OpenGL can do is write pixels to the framebuffer.

The general idea is that it is up to the user of OpenGL to remember what they drew. So if you draw two lines, you should remember the coordinates you gave for those two lines. Therefore, if you want to "erase" a line, what you do is clear the screen and redraw everything except that line.

This isn't as silly as it may sound. Many OpenGL applications are constantly redrawing the screen. They show a frame, draw a new frame, then show that frame, etc. This provides the possibility for animation: changing what gets drawn and where it gets drawn from frame to frame. This creates the illusion of movement.

Nicol Bolas
  • 449,505
  • 63
  • 781
  • 982
  • 2
    +1 for the only sensible answer so far ! Given that the question betrays a beginner level understanding of OpenGL, I'm a little shocked to see suggestions of stencil buffers and the like :o – Nicolas Lefebvre Jun 17 '11 at 08:18
  • 1
    @bethor You are absolutely right. @Nicol Bolas This was really the best answer so far. Particulary the fact that you described what happens "behind the scenes" of openGL. Big PLUS :) – buddy Jun 17 '11 at 13:48
1

You can use glLogicOp with GL_XOR, then repaint the line to erase it. It's not a general solution, but it is a good fit for marquee selection or mouse tool overlays, where it was traditionally used. Note that you'll need to either use single-buffering, or copy between the back and the front buffer rather than swapping them.

Yakov Galka
  • 70,775
  • 16
  • 139
  • 220
0

There are two aproaches if I understood you correctly.

  1. Repaint only the elements you need, each element must have a boolean indicating if it will be painted or not.

  2. In case you need to erase exactly one part of the window, use glScissor.

Info:

Now for something new. A wonderful GL command called glScissor(x,y,w,h). What this command does is creates almost what you would call a window. When GL_SCISSOR_TEST is enabled, the only portion of the screen that you can alter is the portion inside the scissor window.

RamonBoza
  • 8,898
  • 6
  • 36
  • 48
0

You need to clear the buffer and redraw the lines you want. For that you probably need to store the line data in some structure.

Daniel
  • 30,896
  • 18
  • 85
  • 139
0

You can try stencil buffer techniques. See J. Carmack technique on shadow volumes (inverse stencil or something).

Coder
  • 3,695
  • 7
  • 27
  • 42