opengl glut make hud - c++

I am creating a 3d asteroid game where you navigate a spaceship through some asteroids.When i click the asteroid i destroy it, and i want before the asteroid is being destroyed to add a hud like you see in sci-fi movies with target locked and stuff like that.So select asteroid, animate the hud, destroy asteroid.What is the best approach in achieving this ? Should i simply create some planes and render them visible only when i need to, or is there another approach like when you create text and you set up a new projection to render the text over the main window.

Woah, woah, you're tackling several problems at once here.
First you must determine the on-screen position (and maybe the bounds) of the asteroid. You do this by mimicking the vertex transformation pipeline on the barycenter position of the asteroid. The usual way is
p_clip = Projection · (Modelview · p)
p_ndc = p_ndc / p_ndc.w
Drawing the HUD overlay requires to get a newbe-misconception out of the way. If you followed one of the usual, bad tutorials, then you'll find the projection matrix setup in the window reshape function. That's not where it belongs.
If you put the whole viewport and projection setup into the drawing function, things become obvious. You can set and reset the viewport and projection as often as required. So first draw the scene using your usual projection and viewport settings. Then you clear the depth buffer and switch to a projection suitable for rendering the overlay.

Related

How to render objects and side panel using opengl?

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);

Generic picking solution for 3D scenes with vertex-shader-based geometry deformation applied

I'm trying to implement a navigation technique for 3D scenes (in OpenSceneGraph with OpenGL). Among other things the user should be able to click on an scene object on the screen to move towards it.
The navigation technique should be integrated into another project which uses a vertex shader to apply a global deformation to the scene geometry. And here is the problem: Since the geometry is deformed using a vertex shader, it is not straight forward to un-project the mouse cursor position to the world coordinates of the spot the user actually selected. But I need those coordinates to perform the proper camera movement in my navigation technique.
One way of performing this un-projection would be to modify the vertex shader (used for the deformation) to let it also store the vertex' original position and normal in separate textures. Afterwards one could read those textures at the mouse position to get the desired values.
Now, as I said, the vertex shader belongs to another project which I actually don't want to touch. One goal of my navigation technique is to be as generic as possible to be easily integrated into other projects as well.
So here is the question: Is there any feature in OpenSceneGraph or OpenGL that I did not consider so far? Anything that allows me to get the world coordinates of a fragment, independently of the vertex shader coder?
Well, you could always do an OpenGL selection operation:
http://www.glprogramming.com/red/chapter13.html
Alternately, you could rasterize to a very small (1px*1px) framebuffer where the user clicked, read back the z-buffer and unproject the Z value you got into world space.

What are good reasons to enable 2D projection with cocos2d-iphone?

In cocos2d-iphone the default projection type is "3D" projection. But you can also set the projection to "2D" like so:
[[CCDirector sharedDirector] setProjection:CCDirectorProjection2D];
Behind the scenes the 3D projection uses perspective projection whereas 2D projection is the OpenGL orthographic projection. The technical details about these two projection modes can be reviewed here, that's not what I'm interested in.
What are the benefits and drawbacks of 2D projection for cocos2d users? What are good reasons to switch to 2D projection?
Personally I've used 2D projection to be able to use depth buffering for isometric tilemaps. Isometric tilemaps require this for correct z ordering of tiles and objects on the tilemap.
I've also used 2D projection with depth buffering in non-tilemap projects to get complete z order control via the vertexZ property. This project used a pseudo isometric display where the vertexZ of an object is based on its Y coordinate.
That means I've been using 2D projection only to be able to use the vertexZ property, which also requires enabling depth buffering. Are there any other reasons one might want to switch to 2D projection?
Switching to 2D projection is a life saver in the following scenario:
You create a big CCRenderTexture
You draw a bunch of stuff on it, either using [... visit] or OpenGL drawing functions
You add the render texture to your layer, e.g., in order for the things you drew in point 2. to serve as the background for your game.
With 3D projection, the texture will be rendered with vertical and/or horizontal fault lines. See e.g., http://www.cocos2d-x.org/boards/6/topics/16197 which is for cocos2d-x but I have observed the same effect also for cocos2d-iphone and setting the projection to 2D got rid of the problem.
I have switched to 2D projection as the only means to resolve font rendering issues with CClabels, both font file and TTF-based labels. This is not always the cause of a font issue, but it has resolved some problems for me when all else failed.

OpenGL C++ Minimap of current setting

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.

Combining OpenGL renderings into one view

I have a simple solid modeling application in which I want to implement several "navigation modes", ways for the user to navigate the camera through 3d space. One of them is the ubiquitous 'drag and pan/rotate' that is used in SketchUp, Blender etc.; I also want to implement something that is more relevant to my specific application. Specifically, I want to implement a mode where the camera floats on a 'ring' above the object being modeled (a building), and always looks at the center of the model; this way, a user can easily 'circle' around the object, a common operation in my application.
So, what I want to do is render the building in my view, and display a torus in the top right of the view, with a small sphere on the torus to represent the camera location. There would be a north arrow in the torus, and the user would drag the camera around the model object by dragging the sphere; moving the sphere would reposition the camera and redraw the scene.
It looks like what I should do is the following: render the 'main view', i.e. the building; then render the torus and sphere (with different perspective settings and lighting) to an offscreen buffer, and blit it from there to my main view.
Then however I get to the hit testing. I want to detect if the user clicks on the sphere, or the torus; from what I understand from OpenGL picking (it seems to be a hard subject :/ ), all picking methods apply only for selecting in one 'scene'. Apart from that, I still want to detect 'normal' picking operations in the building model, obviously.
So, my questions:
How do I render to an offscreen buffer and blit into another OpenGL context (with alpha blending & transparence like for the center of the torus)?
How do I do hit testing in the described scenario?
I don't think you need to do off-screen rendering for this. You should be able to just re-set the camera and viewport and render the overlay after the main scene. You might have issues with Z-ordering and/or buffering, but perhaps the "sub-scene" is simple enough for that not to matter, or you could of course just clear the Z buffer before rendering it.
As far as drawing the torus/sphere goes, create a separate class for that and implement a "draw" method. Have the class contain the location of both the sphere and torus and have draw() render those things on the screen.
Then just call myRing.draw() in your main drawing method and you'll have a sphere and torus!
If you mean you want to have a a circle/ring rendered in 2D (which might be easier) in the top right corner of the window, then the same sort of idea would apply as in your hitbox post (except without that annoying projection calculation!)
Lastly, I'd consider using a function key in combination with mouse drags to implement the functionality you want... E.g. the user holds "shift" and then click-drags the mouse across the screen. These mouse events are caught and the x-delta is used to compute the angle of rotation. The camera's location is updated as this happens and you get a smooth sliding motion :)
I agree with #unwind; you don't need an offscreen buffer. If you want to anyway, search for "render-to-texture".
As for hit testing, The OpenGL FAQ has an entry on it. It describes several solutions: using GL_SELECTION render mode, using gluUnproject() to get a 3D collision ray and a simple 2D solution using unique colors.