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

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.

Related

Qt3D C++ How to shape outline?

Does anyone know how to outline a shape in Qt3D using C++ (not QML).
For example using a cuboid mesh and make it transparent but outline the edges of the shape. Please see pictures attached on what I mean.
To draw the outline of a shape, you can follow these steps:
Draw everything as normal.
Draw only the outline of the selected object.
When drawing the outline, you need to use an outline effect, which can be implemented in two render passes:
Render the geometry to a texture using a simple color shader.
Render to screen using a shader that takes each pixel in the texture and compares the surrounding pixels. If they are equal, we are inside the object and the fragment can be discarded. If they differ, we are on the edge of the object and we should draw the color.

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

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.

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.

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.