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);
Related
As stated in the title, I need to render one part of the teapot in each of the four viewports, and the four parts together are a complete teapot. I can now complete the effect in gluOrtho2D. But in gluPerspective, I cannot use gluLookAt to change the observation position.
This is my result Without gluLookAt:
Did you by chance try to emplace the rendering in each viewport by trying to transform it there using modelview and projection matrices?
If so, here's a hint: Consider why the function glViewport is called that way and not glWindow (nonexistent). Just use glViewport to define the subset of the window you want to render to.
I need some help in surface area selection on a 3d model rendered in opengl by picking points through mouse. I know how to get a point in world coordinate but cant find a way to select an area. Later I need to remesh that selected area and map an image over it which I know.
Well, OpenGL by itself can't help you there. OpenGL is a drawing API. You draw things, but once the drawing commands have been executed all that's left are pixels in a framebuffer and OpenGL has no recollection about the geometry whatsoever.
You can use OpenGL to implement image based area selection algorithms, for example by drawing each face with a unique index color into an off screen framebuffer. Then by looking at what values can be found therein you know which faces are present in a given area.
Later I need to remesh
This is called topology modification and is completely outside the scope of OpenGL.
that selected area and map an image over it which I know
You can use a image based approach for this again, however you must know in which way you want to make images to faces first. If you want to unwrap the mesh, then OpenGL is of no help. However if you want the user to be able to "directly draw" onto the mesh, this can be done by drawing texture coordinates into another off screen framebuffer and by this reverse mapping screen coordinates to texture coordinates.
I'm looking for a way to create a second view from the top of my current 3D scene. I would like to do this as easy as possible. The basic idea is that you have a subwindow that will display a top view of the setting.
I've looked into subwindows in openGL but the problem is you have to redraw everything (basically ending up with 2 scene's with different angle = not good). Also because this will be used in a 3D game called "tower box stacking" (you have to place boxes on top of each other and make a high tower) its impossible to use the subwindows way to do it (since you would get 2 scenes with different blocks/locations/actions/...)
So how can I add a "second camera" to my current scene and then position it on top.
I've looked into subwindows in openGL but the problem is you have to redraw everything (basically ending up with 2 scene's with different angle = not good)
This is actually the one and only way to do this with OpenGL.
So how can I add a "second camera" to my current scene and then position it on top.
OpenGL doesn't have cameras. It doesn't even have a scene. OpenGL merely draws very simple shapes: Points, Lines and Triangles. Above that OpenGL has no understanding of geometry or complex scenes.
Scene management is up to you and drawing multiple views of a scene is up to be implemented by you.
Update: Pseudocode
draw_scene:
for o in objects:
glPushMatrix()
glMultMatrix(o.transform)
o.draw()
glPopMatrix()
render_main_view:
glMatrixMode(GL_PROJECTION)
glLoadIdentity()
glFrustum(...)
glMatrixMode(GL_MODELVIEW)
glLoadIdentity()
glMultMatrix(main_camera_transform)
render_secondary_view:
glMatrixMode(GL_PROJECTION)
glLoadIdentity()
glFrustum(...)
glMatrixMode(GL_MODELVIEW)
glLoadIdentity()
glMultMatrix(secondary_camera_transform)
scissor_viewport(x,y,w,h)
glScissor(x,y,w,h)
glViewport(x,y,w,h)
render:
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
glEnable(GL_SCISSOR_TEST)
scissor_viewport(main_viewport.x,main_viewport.y,main_viewport.w,main_viewport.h)
render_main_view()
glClear(GL_DEPTH_BUFFER_BIT)
scissor_viewport(secondary_viewport.x,secondary_viewport.y,secondary_viewport.w,secondary_viewport.h)
render_secondary_view()
Draw the scene once using your default settings.
Then apply a different view transformation (corresponding to your second "camera"), use glViewport to select a sub-rectangle of the screen and draw the scene again. (Don't forget to reset the glViewport to cover your entire screen again afterwards)
If you want the mini-map to have a different aspect ratio (w/h), then during the second pass you'll need to also change the perspective transformation so that everything looks OK.
Disclaimer: I haven't tried this and it's a suggestion really.
So you say your game is about stacking boxes and you want an overhead view. Why not 'fake' the overhead view? Basically you create a texture that represents the minimap of your game as an orthogonal view. ONLY when a new block gets stacked, you would need to update the texture. To view it in the current position you would then have to set the appropriate texture-coordinates of the 'sub-window' or viewport.
I'm learning about how to use JOGL and OpenGL to render texture-mapped quads. I have a test program and a test quad, and I figured out how to enable GL_BLEND so that I can specify the alpha value of a vertex to make a quad with a sort of gradient... but now I want this to show through to another textured quad at the same position.
Drawing two quads with the same vertex locations didn't work, it only renders the first quad. Is this possible then, or will I need to basically construct a custom texture on-the-fly based on what I want and then draw one quad with this texture? I was really hoping to take advantage of blending in this case...
Have a look at which glDepthFunc you're using, perhaps you're using GL_LESS/GL_GREATER and it could work if you're using GL_LEQUAL/GL_GEQUAL.
Its difficult to make out of the question what exactly you're trying to achieve but here's a try
For transparency to work correctly in OpenGL you need to draw the polygons from the furthest to the nearest to the camera. If you're scene is static this is definitely something you can do. But if it's rotating and moving then this is usually not feasible since you'll have to sort the polygons for each and every frame.
More on this can be found in this FAQ page:
http://www.opengl.org/resources/faq/technical/transparency.htm
For alpha blending, the renderer blends all colors behind the current transparent object (from the camera's point of view) at the time the transparent object is rendered. If the transparent object is rendered first, there is nothing behind it to blend with. If it's rendered second, it will have something to blend it with.
Try rendering your opaque quad first, then rendering your transparent quad second. Plus, make sure your opaque quad is slightly behind your transparent quad (relative to the camera) so you don't get z-buffer striping.
I'd like to try and implement some HCI for my existing OpenGL application. If possible, the menus should appear infront of my 3D graphics which would be in the background.
I was thinking of drawing a square directly in front of the "camera", and then drawing either textures or more primatives on top of the "base" square.
While the menus are active the camera can't move, so that the camera doesn't look away from the menus.
Does this sound far feteched to anyone or am I on the right tracks? How would everyone else do it?
I would just glPushMatrix, glLoadIdentity, do your drawing, then glPopMatrix and not worry about where your camera is.
You'll also need to disable and re-enable depth test, lighting and such
There is the GLUI library to do this (no personal experience)
Or if you are using Qt there are ways of rendering Qt widgets transparently on top of the OpenGL model, there is also beta support for rendering all of Qt in opengl.
You could also do all your 3d Rendering, then switch to orthographic projection and draw all your menu objects. This would be much easier than putting it all on a large billboarded quad as you suggested.
Check out this exerpt, specifically the heading "Projection Transformations".
As stated here, you need to apply a translation of 0.375 in x and y to get pixel perfect alignment:
glViewport(0, 0, width, height);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0, width, 0, height);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glTranslatef(0.375, 0.375, 0.0);
/* render all primitives at integer positions */
The algorithm is simple:
Draw your 3D scene, presumably with depth testing enabled.
Disable depth testing so that your GUI elements will draw over the 3D stuff.
Use glPushMatrix to store you current model view and projection matrices (assuming you want to restore them - otherwise, just trump on them)
Set up your model view and projection matrices as described in the above code
Draw your UI stuff
Use glPushMatrix to restore your pushed matrices (assuming you pushed them)
Doing it like this makes the camera position irrelevant - in fact, as the camera moves, the 3D parts will be affected as normal, but the 2D overlay stays in place. I'm expecting that this is the behaviour you want.