Camera projection askew - c++

A slightly strange question, perhaps, but I have been brushing up on my DirectX and I've hit an odd problem. I recently finished my camera class and when I run the application to view a cube, I notice that the object being viewed isn't centered on the screen. I've double-checked the initialization variables and the vertex coordinates of the cube and they all seem to check out. I'm using a left-handed coordinate system, with the camera located at (0, 0, 10) and it's direction being (0, 0, -1). The cube itself is 6.0f wide, high and deep and centered at the origin. With all of these parameters, the edge between the two front facing sides of the cube should align perfectly with the center of the screen.
This is my result:
My question is not one that requires code to be posted, so I won't bore you with it until it is necessary. I've been poking at the code for some time and haven't found a possible source yet. My question is more of a theoretical one than anything else, to help me try and understand what is causing this. So, what possible causes could their be for this?
In case it is important, I also calculate my own view matrix, but I've double-checked that code and it seems to check out.
Thank you in advance,
Kevin Delval

The problem was related to rotations, not my matrix math.
Removing the sneaky rotation solved the problem.

Related

Keeping polygon perpendicular to camera

Currently I'm using WebGL for a school project, and am stuck on trying to keep an object perpendicular to the camera while allowing rotation of the rest of the scene.
I currently have a cube and other object on the canvas, and they're being successfully rotated using a quaternion and converting that to a rotation matrix. I would now like to add a simple square within the scene, but have it consistently perpendicular to the camera.
In thinking about this, I've considered an approach using multiple vertex shader programs for the different objects in the scene: 1 for handling the positioning of all objects in the scene that can be rotated, and another vertex shader corresponding to the square that I don't want rotated.
Though, I really don't know if this approach will work as expected, as I am still a novice at WebGL.
Would there be a better approach to this? I greatly apologize if there's a lack of info given, and can give any more info if needed. I've searched quite a bit for something involving this, but have hit a dead end.
Thank you!
If you want a more step by step tutorial: http://www.opengl-tutorial.org/intermediate-tutorials/billboards-particles/billboards/.

FPS camera in c++ maybe using glm:lookat

I have a rather simple setup, a model at 0,0,0, with:
projection = glm::perspective(fov, aspectRatio, near, far);
model = glm::mat4(1.0);
I want a flexible camera, which is, from what I read the best described as FPS camera.
Now I am having:
glm::mat4 CameraMatrix = glm::lookAt(
glm::vec3(cameraPosition.X,cameraPosition.Y,cameraPosition.Z), // camera, in world space
glm::vec3(cameraTarget.X,cameraTarget.Y,cameraTarget.Z), // direction vector eye + Dir
glm::vec3(upVector.X,upVector.Y,upVector.Z)
);
very much as described here: http://www.opengl-tutorial.org/beginners-tutorials/tutorial-6-keyboard-and-mouse/
This is working out fine except one thing, mouse up and down does as intended, but left-right rotation the mouse makes the model rotating around cameraposition and not camera rotating around the model? Did I make something wrong?
My impression is that this requires at least some additional rotation and maybe translation to make it work correctly. If so, I am wondering why this seems to be mentioned nowhere. There are plenty of tutorials using lookat also if referring to "FPS camera" when I google for it. This confuses me a bit.
Now the examples around glm::lookat are rather poor and I read some stuff for gluLookAt in which I heard some voices that at least gluLookAt is no solution for such a task, does the same apply to glm::lookat? And if so, what's the right way to go? I am really not bound to glm:lookat in any way, it's just when I stumbled across it, I was like "HEY! Just what I was looking for" and it seems to be a clean and easy solution.
Maybe someone can shed some light on this and point me into the right direction or tutorial.
it appeared to be that one of my matrices was upside down, causing this problem.
The tutorial mentioned above was very helpful to find out. So if anyone stumbles across such an issue, it can be a simple math problem and indeed glm::lookAt does all I need.

OpenGL: Understanding transformation

I was trying to understand lesson 9 from NEHEs tutorial, which is about bitmaps being moved in 3d space.
the most interesting thing here is to move 2d bitmap texture on a simple quad through 3d space and keep it facing the screen (viewer) all the time. So the bitmap looks 3d but is 2d facing the viewer all the time no matter where it is in the 3d space.
In lesson 9 a list of stars is generated moving in a circle, which looks really nice. To avoid seeing the star from its side the coder is doing some tricky coding to keep the star facing the viewer all the time.
the code for this is as follows: ( the following code is called for each star in a loop)
glLoadIdentity();
glTranslatef(0.0f,0.0f,zoom);
glRotatef(tilt,1.0f,0.0f,0.0f);
glRotatef(star[loop].angle,0.0f,1.0f,0.0f);
glTranslatef(star[loop].dist,0.0f,0.0f);
glRotatef(-star[loop].angle,0.0f,1.0f,0.0f);
glRotatef(-tilt,1.0f,0.0f,0.0f);
After the lines above, the drawing of the star begins. If you check the last two lines, you see that the transformations from line 3 and 4 are just cancelled (like undo). These two lines at the end give us the possibility to get the star facing the viewer all the time. But i dont know why this is working.
And i think this comes from my misunderstanding of how OpenGL really does the transformations.
For me the last two lines are just like undoing what is done before, which for me, doesnt make sense. But its working.
So when i call glTranslatef, i know that the current matrix of the view gets multiplied with the translation values provided with glTranslatef.
In other words "glTranslatef(0.0f,0.0f,zoom);" would move the place where im going to draw my stars into the scene if zoom is negative. OK.
but WHAT exactly is moved here? Is the viewer moved "away" or is there some sort of object coordinate system which gets moved into scene with glTranslatef? Whats happening here?
Then glRotatef, what is rotated here? Again a coordinate system, the viewer itself?
In a real world. I would place the star somewhere in the 3d space, then rotate it in the world space around my worlds origin, then do the moving as the star is moving to the origin and starts at the edge again, then i would do a rotate for the star itself so its facing to the viewer. And i guess this is done here. But how do i rotate first around the worlds origin, then around the star itself? for me it looks like opengl is switching between a world coord system and a object coord system which doesnt really happen as you see.
I dont need to add the rest of the code, because its pretty standard. Simple GL initializing for 3d drawings, the rotating stuff, and then the simple drawing of QUADS with the star texture using blending. Thats it.
Could somebody explain what im misunderstanding here?
Another way of thinking about the gl matrix stack is to walk up it, backwards, from your draw call. In your case, since your draw is the last line, let's step up the code:
1) First, the star is rotated by -tilt around the X axis, with respect to the origin.
2) The star is rotated by -star[loop].angle around the Y axis, with respect to the origin.
3) The star is moved by star[loop].dist down the X axis.
4) The star is rotated by star[loop].angle around the Y axis, with respect to the origin. Since the star is not at the origin any more due to step 3, this rotation both moves the center of the star, AND rotates it locally.
5) The star is rotated by tilt around the X axis, with respect to the origin. (Same note as 4)
6) The star is moved down the Z axis by zoom units.
The trick here is difficult to type in text, but try and picture the sequence of moves. While steps 2 and 4 may seem like they invert each other, the move in between them changes the nature of the rotation. The key phrase is that the rotations are defined around the Origin. Moving the star changes the effect of the rotation.
This leads to a typical use of stacking matrices when you want to rotate something in-place. First you move it to the origin, then you rotate it, then you move it back. What you have here is pretty much the same concept.
I find that using two hands to visualize matrices is useful. Keep one hand to represent the origin, and the second (usually the right, if you're in a right-handed coordinate system like OpenGL), represents the object. I splay my fingers like the XYZ axes to I can visualize the rotation locally as well as around the origin. Starting like this, the sequence of rotations around the origin, and linear moves, should be easier to picture.
The second question you asked pertains to how the camera matrix behaves in a typical OpenGL setup. First, understand the concept of screen-space coordinates (similarly, device-coordinates). This is the space that is actually displayed. X and Y are the vectors of your screen, and Z is depth. The space is usually in the range -1 to 1. Moving an object down Z effectively moves the object away.
The Camera (or Perspective Matrix) is typically responsible for converting 'World' space into this screen space. This matrix defines the 'viewer', but in the end it is just another matrix. The matrix is always applied 'last', so if you are reading the transforms upward as I described before, the camera is usually at the very top, just as you are seeing. In this case you could think of that last transform (translate by zoom) as a very simple camera matrix, that moves the camera back by zoom units.
Good luck. :)
The glTranslatef in the middle is affected by the rotation : it moves the star along axis x' to distance dist, and axis x' is at that time rotated by ( tilt + angle ) compared to the original x axis.
In opengl you have object coordinates which are multiplied by a (a stack of) projection matrix. So you are moving the objects. If you want to "move a camera" you have to mutiply by the inverse matrix of the camera position and axis :
ProjectedCoords = CameraMatrix^-1 . ObjectMatrix . ObjectCoord
I also found this very confusing but I just played around with some of the NeHe code to get a better understanding of glTranslatef() and glRotatef().
My current understanding is that glRotatef() actually rotates the coordinate system, such that glRotatef(90.0f, 0.0f, 0.0f, 1.0f) will cause the x-axis to be where the y-axis was previously. After this rotation, glTranslatef(1.0f, 0.0f, 0.0f) will move an object upwards on the screen.
Thus, glTranslatef() moves objects in accordance with the current rotation of the coordinate system. Therefore, the order of glTranslatef and glRotatef are important in tutorial 9.
In technical terms my description might not be perfect, but it works for me.

Implementing a Fixed Coordinate system in OpenGL

The simple version of my project is rotating a sphere based on user input.
However my rotations get weird after multiple rotations in certain situations due to openGL's local coordinate system.
What I really need is to rotate based on a world/fixed coordinate system. Such that when I rotate the sphere once its rotation axes are not rotated as well.
This issue is explained here http://www.opengl.org/resources/faq/technical/transformations.htm at 9.070.
I understand all that. However setting up what they suggest is a bit beyond my skill level. Does anyone have any experience with this? I tried using some code I found here
http:// chaosengineer.com/?cat=19
but had no luck.
Here is the basic code I am using.
glPushMatrix();
//draw in middle of the screen
glTranslatef(ofGetWidth()/2,ofGetHeight()/2,0);
glRotatef(xRot,1,0,0);
glRotatef(zRot,0,0,1);
glTranslatef(-ofGetWidth()/2,-ofGetHeight()/2,0);
ofSetColor(255, 255, 255, 255);
squirrelModel.draw();
glPopMatrix();
YOu need to store object orientation as a matrix. Or as a set of vectors (local X, local Y, local Z).
To rotate object a bit, multiply orientation matrix by rotation matrix each time. Just make sure that all vectors of orientation matrix remain perpendicular to each other.
In this case, when rendering, instead of glRotate/glTranslate you'll need to use glMultMatrix.
Or use quaternions.
I understand it is not a complete answer, but (although I have written libraries and code for doing this), explaining this will take a lot of time.
Try downloading NVidia OpenGL sdk and see how they handled object rotation in their demos.
Also search for 3d orientation with google. Some papers should explain how to store object orientation using matrices, and so on.
And check euclidianspace.com

OpenGl And Flickering

When objects from a CallList intersect the near plane I get a flicker..., what can I do?
Im using OpenGL and SDL.
Yes it is double buffered.
It sounds like you're getting z-fighting.
"Z-fighting is a phenomenon in 3D rendering that occurs when two or more primitives have similar values in the z-buffer, and is particularly prevalent with coplanar polygons. The effect causes pseudo-random pixels to be rendered with the color of one polygon or another in a non-deterministic manner, varying as the scene is animated, causing one polygon to "win" the z test, then another, and so on."
(From wikipedia)
You can get more information about the problem in the OpenGL FAQ.
glPolygonOffset might help, but you can also get yourself into trouble with it. Tom Forsyth has a good explanation in his FAQ Note: It talks about ZBIAS, but that's just the DirectX equivilent.
The problem was that my rotation function had some floating point errors which screwed up my model_view matrix.
None of you could have guessed it, sorry for the waste of your time.
Although I don't think that moving the near plane should be even considered a solution to any kind of problem usually something else is wrong, because openGL does support polygon intersection with the near plane.
Try to put the near clipping plane a little bit further :
for example with gluPerspective -> third parameter zNear
http://www.opengl.org/documentation/specs/man_pages/hardcopy/GL/html/glu/perspective.html
Ah, you meant the near plane. :)
Well...another thing when drawing polygons in the same plane is to use glPolygonOffset
From the description
glPolygonOffset is useful for rendering hidden-line images,
for applying decals to surfaces, and for rendering solids
with highlighted edges.