How to delete/erase a specific polygon in openGL? - opengl

Suppose i have drawn a triangle,a cube,a square using draw_triangle,draw_cube and draw_square function respectively.How can I delete a cube using keypress func.?..If i use glClear(GL_COLOR_BUFFER_BIT) entire screen will be erased..How to delete a specific polygon.?

How to delete a specific polygon.?
Don't render it in next frame.
Once you draw a polygon, there is no polygon, just a bunch of pixels in color buffer + values in depth buffer. So you can't "delete" it, and you can't restore previous state of color/depth buffer. So clear screen, redraw the scene without polygon you didn't want.

If you want to clear only a specific part of the screen or any specific image, then redisplay it by using a keyboard interrupt wihout clearing the whole screen.

I think the best solution is to have a boolean variable which indicates whether your object must be displayed or not. Thus, only when a key is pressed, you clear the screen and redraw the entire scene.

maybe something like that:
Render()
{
clear_screen();
setup_camera_and_other_scene_states();
if (cube) drawCube();
if (sphere) drawSphere();
swap_buffers():
}
keyPress()
{
if (presses_some_key) cube = !cube;
}

Related

Reset glViewport after painting on framebuffer

I am painting on a large opengl canvas. At times I need to draw onto some small framebuffers (tiles) and then go back to paint to my canvas. The problem is that when I draw the framebuffers, I obviously change the viewport of the context, so when I go back painting on my canvas obviously the viewport needs to modified. What I am looking for is a way to save and restore the glViewport settings. Something like this in pseudocode:
saveViewport();
drawFramebuffers(); // this change the viewport
restoreViewport();
Is something like this possible?
For Compatibility contexts glPushAttrib()/glPopAttrib() with GL_VIEWPORT_BIT will save/restore the depth range & viewport state.
In addition to #genpfault 's answer, the following also works:
// save viewport
GLint aiViewport[4];
glGetIntegerv(GL_VIEWPORT, aiViewport);
// do your stuff and then restore viewport
glViewport(aiViewport[0], aiViewport[1], (GLsizei)aiViewport[2], (GLsizei)aiViewport[3]);
This was taken from here

Text Clipping with Qt OpenGL

I'm currently working with Qt5.1 and trying to draw some OpenGL stuff within a QGLWidget:
void Widget::paintGL() {
startClipping(10, height()-110,100,100);
qglColor(Qt::red);
glBegin(GL_QUADS);
glVertex2d(0,0);
glVertex2d(500,0);
glVertex2d(500,500);
glVertex2d(0,500);
glEnd();
qglColor(Qt::green);
this->renderText(50, 50, "SCISSOR TEST STRING");
endClipping();
}
The quad gets clipped correctly but the text doesn't.
I tried three ways of implementing the startClipping method: scissor test, setting the viewport to the clipping area and with a stencil buffer.
None of them worked and the whole string was drawn instead of beeing cut off at the edges of the clipping area.
Now my question is: Is this behavior a bug of Qt or is there something, I missed or another possibility I could try??
After a week of trying around, I suddenly found a very simple way to achieve, what I was looking for.
Using a QPainter and it's methods instead of the QGLWidget's renderText() simply makes text clipping work:
QPainter *painter = new QPainter();
painter->begin();
painter->setClipping(true);
painter->setClipPath(...); // or
painter->setClipRect(...); // or
painter->setClipRegion(...);
painter->drawText(...);
painter->end();
As I understand it, this is by design. According to the documentation ( https://qt-project.org/doc/qt-4.8/qglwidget.html#renderText ):
Note: This function clears the stencil buffer.
Note: This function temporarily disables depth-testing when the text is drawn.
However for the 'xyz version' (overloaded function)
Note: If depth testing is enabled before this function is called, then the drawn text will be depth-tested against the models that have already been drawn in the scene. Use glDisable(GL_DEPTH_TEST) before calling this function to annotate the models without depth-testing the text.
So, if you use the second version (by including a z-value, eg 0) in your original code, I think you get what you want. I think you would want to do this if you do a scene that is 'real' 3D (eg, axis labels on a 3D plot).
The documentation does also mention using drawText.

OpenGL Coplanar Surfaces

In my OpenGL problem set, I have drawn a particular polygon and with the mouse selection, I want to draw a square from the place I have selected. The square drawn gets updated rapidly with my change in mouse motion and finally should be affixed to the end point where I release the mouse button.
In my mouse motion routine, I tried doing the glClear(GL_COLOR_BUFFER_BIT); and called a drawSquare function that I have written. The glClear ensures that I have only a unique square drawn and not a blotch of previous images appearing. However, this clearing also clears my initial polygon that I had drawn.
Please advice on how to make sure I have this new square (from mouse) on top of my existing polygon.
P.S : I tried drawing the underlying polygon on the backbuffer and the square on the front, but the glutSwapBuffers() created havoc.
Like many others you seem to mistake OpenGL for a scene managment API, which is plain wrong. OpenGL does nothing else than draw something to the screen. After it's drawn OpenGL completely forgets about the polygon. So what this means is, that you have to draw everything you want to be shown every frame (often after clearing the framebuffer).
So clear the framebuffer, draw your polygon and draw your square. Once the square position (or anything else) changes, clear the framebuffer again, draw the polygon and draw your square. That's the way it works.
And also you should of course not draw anything in the mouse routine, let aside call glClear, which results in exactly the kind of rubbish you're experiencing. Instead update your scene and request the window to redraw itself, which in turn draws the whole scene in the display method:
mouseFunc()
{
updateSquare();
glutPostRedisplay();
}
displayFunc()
{
glClear(...);
drawPolygon();
drawSquare();
glutSwapBuffers();
}
And of course by all means don't draw to the front buffer when using double buffering.

Drawing statically in Open GL

I am developing a paint-like application using C++ and Open GL. But every time i draw objects like circle, lines etc they don't ** stay ** on the page. By this I mean that every new object I draw is getting placed on a blank page. How do I get my drawn objects to persist?
OpenGL has no geometry persistency. Basically it's pencils, brushes and paint, with which you draw on a canvas called the "framebuffer". So after you drawn something and clear the framebuffer, it will not reappear in some magic way.
There are two solutions:
you keep a list of all drawing operations and at each redraw you repaint everything from that list.
After drawing something copy the image in the framebuffer to a texture and instead of glClear you fill the background with that texture.
Both techniques can be combined.
Just don't clear the framebuffer and anything you draw will stay on the screen. This is the same method I use to allow users to draw on my OpenGL models. This is only good for marking up an image, since by using this method you can't erase what you've drawn, unless your method of erasing is to draw using your background color.

Draw Order in OpenGL

I am rendering an OpenGL scene that include some bitmap text. It is my understanding the order I draw things in will determine which items are on top.
However, my bitmap text, even though I draw it last, is not on top!
For instance, I am drawing:
1) Background
2) Buttons
3) Text
All at the same z depth. Buttons are above the background, but text is invisible. It I change the z depth of the text, I can see it, but I then have other problems.
I am using the bitmap text method from Nehe's Tutorials.
How can I make the text visible without changing the z depth?
You can simply disable the z-test via
glDisable (GL_DEPTH_TEST); // or something related..
If you do so the Z of your text-primitives will be ignored. Primitives are drawn in the same order as your call the gl-functions.
Another way would be to set some constant z-offset via glPolygonOffset (not recommended) or set the depth-compare mode to something like GL_LESS_EQUAL (the EQUAL is the important one). That makes sure that primitives drawn with the same depth are rendered ontop of each other.
Hope that helps.
You can also use glDepthFunc (GL_ALWAYS).