OpenGL: How can I rotate a combination of two objects? - opengl

I'll simplify this problem with one example.
Imagine that I have 3 objects drawn in OpenGl:
A Cube.
A Sphere.
A Pyramid.
I know how to rotate all of them and each of them in the same code. Just by using glPush() and glPop() functions, as you can verify in the code below:
glPush();
AllObjectsRotation();
glPush();
CubeRotation();
CubeDraw();
glPop();
glPush();
SphereRotation();
SphereDraw();
glPop();
glPush();
PyramidRotation();
PyramidDraw();
glPop();
glPop();
The question is: How can I also rotate all combinations of 2 objects? I mean, how can I rotate, in the same code:
The Cube and the Sphere.
The Cube and the Pyramid.
The Sphere and the Pyramid.
Thanks

Related

Taking cube from camera space to clip space, error in my math?

watching Ken Joy's Computer Graphics lectures on youtube. One thing I'm confused about is after he gets the cube from the camera space to clip space, from my calculations the cube doesn't look like that. I expected the cube to look like that pink parallelogram in my picture, if we assume the Z of the front-face of the cube to be -4/3 and the back-face to be -2 then the Ws come out to be 4/3 and 2 respectively. So can someone explain how after multiplying by the viewing matrix, the cube comes out to look like how Ken has it.
Ken's view matrix:
After view matrix has been applied:
What I think the side of the cube should look like(the pink parallelogram) after view matrix has been applied:
my reasoning is, after the perspective divide by W, the blue and green vectors should get truncated to create that pink parallelogram. So I'm struggling to understand this. Thanks in advance.
At Perspective Projection the scene is seen as from of a pinhole camera. The cube on the school board is placed symmetrically around the z axis, in compare to the cube in the illustration which is placed at Y+ (above the axis).
When the z axis intersects the cube, then you can neither see the top, nor the bottom of the cube:
When the cube is lifted up, then you can see the bottom of the cube, too:

opengl ray tracing and mesh

I was able to get a starter code for a ray tracer online and the starter code has two "Geometries":
class sphere
class triangle
I understand the triangle since the code creates a mesh using triangles and gets the intersection between triangles and ray from each pixel. But how does sphere come into play?
So I've done some online researching and a lot of them discuss about triangle intersection and sphere intersection. but how do we use sphere in mesh?
A mesh is a collection of triangles and to render that object using ray tracing, you have to solve lots of ray tracing equations with all the triangles. However, a sphere has a closed form implicit function for which solving the ray intersection is very easy. These two sample object are coming from the fact that we have two ways of representing objects in computer graphics, implicit objects for which we have a closed form implicit functions and polygonal mesh representation for which we have a collection of triangles. Usually, in ray tracing, we have objects such as sphere, cylinder, plane (triangle), and torus for which we have a closed form function and we can find their intersection with rays. For complicated objects like bunny, ray is casted and intersected with a collection of planes (triangles).

Find the vertices on the outline of a 3d model

I have found a lot of materials on how to draw outline around a model with the help of a stencil buffer, like https://www.opengl.org/archives/resources/code/samples/mjktips/StenciledHaloEffect.html
However, this time I need to locate the vertices on the outline, to get their coordinates, but haven't found an elegant way to do so.
I once came across a method that can solve my problem for simple models like cube or sphere. It suggests to check the two triangles that share an edge, if the normals of the two triangles pointing in opposite directions with respect to the camera direction, than the edge they share is on the outline.
The above method works for simple models like sphere or cube, but not for complex models because it might also picked up those edges that inside the outline from the view of the camera.
In conclusion, my objective is to find the coordinates of the vertices on the outline, for example, vertices on the red line in the image which you can find at https://gamedev.stackexchange.com/questions/59361/opengl-get-the-outline-of-multiple-overlapping-objects.
BTW, I am doing this using Ogre.

How to slice a geometry?

Let's say I have this geometry:
glutSolidTeapot(1);
I want to slice it in 8 cubes, for example across the 3 planes (xy), (yz), (xz), to make a 3D puzzle.
How can I clip a geometry?
There is 2 ways of doing this. I'm going to assume you want to slice your geometry into cubes, but other shapes can be done similarly.
1. Slice your triangle mesh
Here you just loop through all your triangles and check in which cube the triangle belongs to. If the triangle intersects multiple cubes you need to split it in multiple triangles. You'll need to do some math for line-plane intersection to get the splits right, but its not very hard.
2. Use opengl clip planes
You can also render your geometry multiple times, but clip only the part you want to be shown on screen. This can be done using glClipPlane (see http://www.opengl.org/sdk/docs/man2/xhtml/glClipPlane.xml). For each cube you'll need 6 clip planes. This method will be slower than the first as gpu needs to consider each triangle for every cube.

calculating normals for quad mesh

I have a struct QUAD that stores 4 pointers to 4 VECTOR3D (which contains 3 floats) so that I can draw the quad mesh.
From what I understand is whenever I draw a mesh, I need normal as well to properly light/shade a mesh and it's relatively easy when it's a mesh laying on a plain, using normal per face.
When I have 2 by 2 quad meshes laying on XZ coordinate and tried to raise it's centre (0,0,0) by a certain point, say (0, 4, 0) it would start to form real 3D shapes, then I need to calculate normals again. I'm having hard time understanding how and what is to be to calculated normals. As expected, the 3D shape shades like it's still a flat mesh, so it does not represent real shape. One of the explanation says I need to calculate normals per vertex instead of per face.
Does it mean I need to calculate normals for all corners of mesh? once i have normals what would i do? I was still using old glBegin glEnd methods but now I feel like i need to use DrawArray method. I'm deeply confused and I'm pretty sure I don't make much sound but i'd much appreciate your help.
If you need flat looking surface then your normals will be normals to the quad plane. If you need "soft looking" surface you need to blend(read this and watch this cool simple video) normals - that will add sort of gradient.