Drawing a 3D cuboid and rotating it in OpenGL - c++

I am trying to draw a 3D cuboid by clicking on one of the corner points and then extending it based on the dimensions provided by the user, and then rotating it about any axis. However, I am not sure about how I can specify the (x, y, z) tuple after the mouse-click, since the output window is on 2D. Also, I cannot understand how to extend the point to form a cuboid.

What you want is called 3D Picking. Usually, this is done using Raycasting.
However, there is an easy solution (with acceptable performance) that involves rendering the scene off-screen on a framebuffer with 32-bit floats for the R/G/B values.
In the shader you use the x, y and z coordinates of that pixel as the color values.
Then, when the user clicks somewhere, you simply read out that pixels color to get its position.

Related

Keeping OpenGL object a fixed size on screen

I'm trying to keep a simple cube a fixed size on screen no matter how far it translates into the scene (slides along the z axis).
I know that using an orthogonal projection, I could draw an object at fixed size, but I'm not interested in just having it look right. I need to read out the x and z coordinates (in world space) at any given time.
So the further along the -z axis the cube translates, the larger are its x and z values getting in order for the cube to still be a defined pixel size on screen (let's say the cube should be 50x50x50 pixel).
I'm not sure how to begin tackling this, any suggestions?

How to use the information in a 4x4 matrix to draw a 2d square in OpenGL?

At the moment I have a function where I pass in a vector2 for position and two int's for the width and height of the image that I want to draw.
It draws a triangle strip and uses the FreeImage library to apply a texture to this square.
However, I have gotten to the point where I want to perform more advanced stuff to these objects, such as setting up Child/Parent relationships, or rotating and scaling the objects.
All of this will become 100x easier if I can figure out how to draw squares with a matrix instead of my current way of doing it.

How to find out how many units across the screen plane in OpenGL

How would one get the relative size of the viewing plane in opengl's own units? I need to find out the width and height in "opengl units". Is there a function which will retrieve this information?
I assume that one unit (let us say 1.0f) in Z would be equivalent to one unit in X, even if conversion to a real measurement system in meaningless.
I know I can get the screen size either by use of GetSystemMetrics(SM_CXSCREEN) or glutGet(GLUT_SCREEN_WIDTH), but this is in pixels.
To handle the graphical window calls, I am using freeglut on non-windows OSes and the WinAPI on Windows.
Assuming you want to draw something like a UI, set your projection matrix to an Orthographic matrix with glOrtho, then you don't have any perspective and have a direct orthographic mapping between world coordinates and screen coordinates. The arguments to your glOrtho call determine how wide/high your view port is in world coordinates.
If you want to draw both a UI and a 3D scene, draw the UI with glOrtho and draw the scene with glPerspective using a clipping mask to make sure you don't ruin your UI.
If on the other hand you want to know the width of the view port in a 3D scene with perspective, so that you know how big to draw your object then you'll have to deal with the perspective projection. You need to know at which Z coordinate you want to know the witdh/height of the view port. You can use gluUnProject to calculate the world coordinate corresponding to a given screen coordinate and Z plane.
However it would probably be better to do it the other way around, always draw your object with a given size and then calculate what your projection matrix should be to have that object appear properly in your view port.

Background image in OpenGL

I'm doing a 3D asteroids game in windows (using OpenGL and GLUT) where you move in space through a bunch of obstacles and survive. I'm looking for a way to set an image background against the boring bg color options. I'm new to OpenGL and all i can think of is to texture map a sphere and set it to a ridiculously large radius. What is the standard way of setting image bg in a 3d game?
The standard method is to draw two texture mapped triangles, whose coordinates are x,y = +-1, z=0, w=1 and where both camera and perspective matrices are set to identity matrix.
Of course in the context of a 'space' game, where one could want the background to rotate, the natural choice is to render a cube with cubemap (perhaps showing galaxies). As the depth buffering is turned off during the background rendering, the cube doesn't even have to be "infinitely" large. A unit cube will do, as there is no way to find out how close the camera is to the object.

Parallel plane in openGL

I'm working with OpenGL, I need to draw a plane in front of a triangle in the three dimensional space. So if one of the triangle points changes, the plane also changes
I have the 3 points, and using cross product, I can get the normal vector, so, to draw the plane, I only need to translate the triangle to the origin of the world in reference of one of the triangle points, translate a distance over the normal, rotate the normal angles in X, Y and Z, and draw the plane.
I need to know how to translate over the normal, and how to rotate the new plane, so, when one of the vertex changes, the normal changes, and the plane also changes.
As I understand, I can use the normal vector in glRotatef(angle, normal[x, y, z]), with angle =0. But the plane doesn't change when I change one of the triangle vertex.
OpenGl is not a scene graph. It will not deal with transforming objects for you. All OpenGL does is render what you tell it to render.
If you tell it to render a vertex (which YOU changed), and do not tell it to change the way it draws the plane, then of course the plane will not change.
Look into scene graphs, and how to do matrix and vector math. A simple scene graph is relatively easy to create.