OpenGL: Keep drawing inside a region on a plane in 3D space - opengl

Step 1: I use glVertex() to draw a shape, say a rectangle, in 3D space.
Step 2: I then draw things inside this rectangle - for example with glutStrokeCharacter() and with glVertex() again.
I need to keep what are drawn in step 2 inside the rectangle drawn in step 1, clipping away anything that goes outside the boundary. Can someone give some hint on how to do this?

Render bounding geometry into stencil buffer.
Enable stencil test
Render regular geometry into color buffer; the stencil test will prevent fragments outside the bounding geometry from writing to the color buffer.

Related

OPENGL draw shapes(like polygon) with holes (any 2D shapes hole)

I want to draw shapes with holes in OpenGL and GLFW3. How can I do this? I don't want to use gluTessBeginPolygon.
this is a rectangle with a rectangle hole in it
If the shape is always the same, then the simplest way is to change how you visualize this. It's not a polygon with a hole, it's 2 (or more) polygons with no holes. Draw that instead:
However, if the shape changes dynamically, calculating this triangulation in code is difficult.
If you can't do this because the hole shape is dynamic then you can use the stencil buffer to prevent OpenGL from drawing where the hole is. Clear the stencil buffer, set the rendering mode so that you only write the stencil, then render the hole. Then set the modes back to normal but set the stencil test so it doesn't draw where the stencil buffer isn't zero, and render the rectangle. Then go back to normal.
If you have a shape with lots of holes (like a chain-link fence) then instead of rendering zillions of vertices, you should use a texture with an alpha channel, and use alpha testing in your shader - use discard; on the transparent pixels so they don't render. The fixed-function version of this is GL_ALPHA_TEST.
If you have a formula to detect whether a pixel is in the hole, you can use discard; as well. Your shader can discard for any reason you like - it doesn't have to be based on the alpha channel of a texture.
What you cannot do is count the number of times you cross the polygon boundary when going from left to right, like a scanline renderer might. That's because OpenGL processes all pixels in parallel - not left-to-right.

Mask a sphere with text

I want to use openGL to draw a hollow sphere. The material properties of sphere is different for front and back surfaces. Now I want to mask the sphere with a text, so that the inner surface becomes visible from the text area. I am not able to understand how to achieve it.
Draw the sphere twice:
glCullFace(GL_FRONT), then draw sphere. This will put all back-facing triangles in the depth and color buffers.
glCullFace(GL_BACK), bind text texture, enable alpha test, draw sphere. Where the alpha test fails the color buffer won't be updated and you'll be able to "see through" to the inside of the sphere.

c++ opengGL: Draw polygon + image processing interior pixels

I am using opengl and c++ doing image processing. The idea is simple, I will load an image, draw a polygon by clicking and then apply an effect (desaturation for instance) only to the pixels in the interior of the polygon shape just created.
Can anyone give me any direction on how to limit the effect to the pixels within the interior of the polygon? Loading the image and drawing the polygon is not a problem
Supposing the following situation :
The picture on which you want to apply the effect takes the whole screen
The picture is rendered using opengl , probably through a simple shader, with the picture passed as a texture
You can do the following approach :
consider the screen as being a big texture
you draw a polygon, which will be rendered on top of the rendered texture
inside the polygon's vertices insert the uv's coresponding to the 2D coordinates on the screen (so from screen space to uv space (0 , 1 ) )
draw the picture normaly
on top of the picture draw your polygon using the same picture as texture, but with a different shader
So instead of trying to desaturate a specific region from your picture, create a polygon on top of that region with the same picture, and desaturate that new polygon.
This would help you in avoiding the stencil buffer.
Another approach would be to create the polygon, but draw it only on the stencil buffer, before the picture is drawn.

Drawing a Circle on a plane, Boolean Subtraction - OpenGL

I'm hoping to draw a plane in OpenGL, using C++, with a hole in the center, much like the green of a golf course for example.
I was wondering what the easiest way to achieve this is?
It's fairly simple to draw a circle and a plane (tutorials all over google will show this for those curious), but I was wondering if there is a boolean subtraction technique like you can get when modelling in 3Ds Max or similar software? Where you create both objects, then take the intersection/union etc to leave a new object/shape? In this case subtract the circle from the plane, creating a hole.
Another way I thought of doing it is giving the circle alpha values and making it transparent, but then of course it still leaves the planes surface visible anyway.
Any help or points in the right direction?
I would avoid messing around with transparency, blending mode, and the like. Just create a mesh with the shape you need and draw it. Remember OpenGL is for graphics, not modelling.
There are a couple ways you could do this. The first way is the one you already stated which is to draw the circle as transparent. The caveat is that you must draw the circle first before you draw the plane so that the alpha blending will blend the circle with the background. Then when you render the plane the parts that are covered by the circle will be discarded in the depth test.
The second method you could try is with texture mapping. You could create a texture that is basically a mask with everything set to opaque white except the circle portion which is set to have an alpha value of 0. In your shader you would then multiply your fragment color by this mask texture color so that the portions where the circle is located are now transparent.
Both of these methods would work with shapes other than a circle as well.
I suggest the stencil buffer. Use the stencil buffer to mark the area where you want the hole to be by masking the color and depth buffers and drawing only to the stencil buffer, then unmask your color and depth, avoid drawing to the stencil buffer, and draw your plane with a stencil function telling OpenGL to discard all pixels where the stencil buffer "markings" are.

Polygon into shader

I'm writing a game. Now there is rendering of the water. I have a polygon:
All the scene is rendered into a single texture and when the water's queue comes I want to pass a complex polygon into the shader. For example, at screen the polygon is red water surface and blue borders. How to pass into shader only the area inside of that polygon? For example, I want to fill everything inside polygon into red color.
Depending on what you’re doing with it, it might be better to render the polygon into a texture by itself and have your shader sample that. If the polygon’s going to be a predictable size, you could use a texture with roughly those dimensions and pass that frame’s position in your scene into the shader too.