I create some scene, and i would like to display some static background image, which would not change from how and what I am doing with the scene.
Usually it means that you can skip clearing your color buffer at the beginning of the scene, instead you set default orthographic projection and render a quad with (-1,-1,0), (-1,+1,0), (+1,+1,0), (+1,-1,0) vertices and apply a texture to this quad.
Then you can set necessary perspective projection and render whatever you want in your scene. The quad will serve you as a background.
Related
Suppose you have some objects which are rendered based on camera position and then you have side pannels (some buttons, text, etc.) which are always at the same position on the screen.
How could I achieve this effect with opengl?
I'm not sure what I should be looking for but I have two ideas how this could be done. The first is to draw semi-transparent texture after applying view and projection matrix. The second is to render to texture like here and then draw it on a plane and render also the pannels.
What method is the most efficient and/or what method is usually used by game developers?
glViewport(full_window);
set_projection_and_modelview_for_scene();
draw_scene();
glViewport(sidebar_position);
glScissor(sidebar_position);
glEnable(GL_SCISSOR_TEST);
set_projection_and_modelview_for_sidebar();
draw_sidebar();
glDisable(GL_SCISSOR_TEST);
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.
So in openGL when I call glClearColor() is there a way to set the background color to a texture instead of setting it just to a static color? Or is there another method which can do that besides glClearColor()?
You cannot clear the screen to a texture. You can:
Draw a textured quad the size of the screen.
Blit a texture from an FBO onto the screen.
Either one will work.
I draw a tile map on screen and each tile light(grayscale) in a FBO. All are quads.
I store the view in a Rect. To move I change de Rect, then I do this...
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(getViewRect().left,
getViewRect().left + getViewRect().width,
getViewRect().top + getViewRect().height,
getViewRect().top,
-1,
1);
glMatrixMode(GL_MODELVIEW);
I only draw the tiles inside the Rect.
The problem is the FBO. I have to draw the same tiles( the lights of the tiles), that are visible.
I want to know if there is a better way than, drawing the same tiles to the fbo with the offset of the tiles, drawing a smaller quad on the borders when is not completly visible, and changing texcoord, because when I draw outside the FBO, it draw on the opposite side.
I use FBO, because I apply shader to the lights.
It works perfect if I dont move the view, but if I move I dont know how to draw on the FBO.
You ought to be able to use glScissor to restrict all drawing within the FBO. Perform this operation after calling glBindBuffer(...) each time you bind it.
Hope this helps!
Please have a look at this image.
I'd like to show an clipped detail of the texture while the clipping rect can be animated so I cannot crop the image upfront. The position of the image is animated too.
I'd like to show it in front of a background. The background is a color or a texture itself.
I'd like to blend both the image and the background combined with opacity
< 1.0 to the destination.
The real requirement here is to render it in one step, avoiding a temporary buffer. Obviously a (simple) shader is needed for that.
What I already tried to achieve this:
Rendering the background first and then the image each with opacity < 1. The problem here: It lets the background shine through the image. The background is not allowed to be visible where the image itself is opaque.
It works when rendering both into a temporary buffer using opacity = 1 and then rendering this buffer to destination with opacity < 1, but this needs more (too much) resources.
I can combine two textures (background, image) in a shader, transform the texture coordinates each with a different transformation matrices. The probleme here is, that I'm not able to clip the image. The rendered geometry is a simple rectangle consisting of two triangles.
Can anybody hint me in the right direction?
You're basically trying to render this.
(Image blended with background) blended with destination
The part in parentheses, you can do with a shader, the blending with destination, you have to do with glBlendFunc, since the destination isn't available in the pixel shader.
It sounds like you know how to clip the image in the shader and rotate it by animating texture coordinates.
Let's call your image with the childreb on it ImageA, and the grey square ImageB
You want your shader to produce this at each pixel:
outputColor.rgb = ImageA.rgb * ImageA.a + ImageB.rgb * (1.0 - ImageA.a);
This blends your two images exactly as you want. Now set the alpha output from your pixel shader to be your desired alpha (<1.0)
outputColor.a = <some alpha value>
Then, when you render your quad with your shader, set the blend function as follows.
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
<draw quad>