Cube position in OpenGL - opengl

I'm trying to determine the position of a cube after the execution of the following code:
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glTranslatef(3.0, 2.0, 1.0);
glPushMatrix();
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glTranslatef(1.0, 2.0, 3.0);
glPushMatrix();
glTranslatef(1.0, 1.0, 1.0);
glMatrixMode(GL_MODELVIEW);
glPopMatrix();
glTranslatef(2.0, 2.0, 2.0);
glutWireCube(1.0);
I don't know if i should consider the GL_MODELVIEW matrix or the GL_PROJECTION
matrix. After the first glPushMatrix() the glMatrixMode is changed from GL_MODELVIEW to GL_PROJECTION, so should I ignore the glTranslatef(3.0, 2.0, 1.0), so the final position is of the cube is: (3.0, 4.0, 5.0)?

Lets take just the MODELVIEW matrix stack:
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glTranslatef(3.0, 2.0, 1.0);
glPushMatrix();
glPopMatrix(); // nullifies the above push
glTranslatef(2.0, 2.0, 2.0);
The stack starts with one entry; you set it to identity, then multiply it with a translation matrix. PushMatrix creates a copy of this matrix in a new entry atop the older one, on the stack and then you undo this by PopMatrix. So you continue to have the first matrix, which is then multiplied by another translation matrix. Effectively your model view transform, which transforms the cube from its model space into the world space, is
| 1 0 0 5 |
| 0 1 0 4 |
| 0 0 1 3 |
| 0 0 0 1 |
A cube which is centered at (5, 4, 3) in the world space.
Then you go on to set the projection matrix stack with the following
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glTranslatef(1.0, 2.0, 3.0);
glPushMatrix();
glTranslatef(1.0, 1.0, 1.0);
Based on the same logic as above, you've an effective projection matrix of
| 1 0 0 2 |
| 0 1 0 3 |
| 0 0 1 4 |
| 0 0 0 1 |
This isn't a valid projection transformation matrix; neither orthogonal nor perspective. You can create a valid projection matrix using the helper functions glFrustum (for a perspective camera) or glOrtho (for orthographic camera). The GLU functions gluPerspective and gluOrtho2D are slightly more intuitive than their gl counterparts. See the OpenGL FAQ on Using Viewing and Camera Transforms for details.

Related

OpenGL is not displaying anything when looking along the y axis

I am currently trying to display a red grid viewed from the top using SFML and OpenGL.
The grid plan is normal to the y axis (y axis is the vector looking to the top).
This is called before my rendering loop:
glEnable(GL_DEPTH_TEST);
gluPerspective(90.0f, (GLfloat)640/(GLfloat)480, 1.0f, 1000.0f);
Inside my rendering loop:
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt(0.0, -3.0, 0.0,
0.0, -0.8, 0.0,
0.0, 1.0, 0.0);
for (float x = -10.0f, x < 10.0f, x += 1.0f) {
glColor3f(1.0f, 0.0f, 0.0f);
glBegin(GL_LINES);
glVertex3f(x, -0.5f, -10.0f);
glVertex3f(x, -0.5f, 10.0f);
glVertex3f(-10.0f, -0.5f, x);
glVertex3f(10.0f, -0.5f, x);
glEnd();
}
window.display(); // swap buffers in SFML
I stripped the SFML part.
When I replace the gluLookAt(); part by this one (slight tilt of the camera):
gluLookAt(0.0, -3.0, 0.0,
0.0, -0.8, 0.01,
0.0, 1.0, 0.0);
or this one:
gluLookAt(0.0, -3.0, 0.0,
0.0, -0.8, 0.0,
0.0, 0.0, 1.0);
Everything is displayed fine.
I concluded that when I look exactly in the -y direction (90°), nothing is drawn. But why? Did I miss something?
My OpenGL supported version is 2.1 Mesa 10.2.4
Many thanks in advance!
The problem is, that you up-axis also points towards the y-axis. It is not necessary that the up-vector points exactly upwards, but it has to be different from your viewing-direction. For more details have a look at this answer
Edit: Since requested, here is the mathematical explaination:
gluLookAt generates a 3d coordinate frame, that consists of three perpendicular vectors.
Assuming the input to gluLookAt is a camera position (C), a eye-position (E), and a up-vector (U), than the matrix is constructed using the following equations:
F = C - E
f = F / |F|
U' = U / |U|
this results in two vectors that span a plane. We already know, that f (the view-vector) will be the -z axis of our final coordinate frame (opengl looks always along the negative z-axis). The x-axis can be calculated by finding the normal vector of the plane spanned by f and u and can thus be calculated via
S = f x U' (x is the cross product)
s = S / |S|
Note that the normalization would not be required if u and f are already perpendicular, but since gluLookAt does not require the real up vector this is not necessarily given. Out of the same reason we cannot use u directly as y-axis. What we are looking for is a y-vector that is perpendicular to both, f and s, which is given as:
u = s x f
The final matrix is than constructed by
| s 0 |
M = | u 0 |
| -f 0 |
| 0 0 0 1 |
Now consider your case where f = U':
When calculating S we get
S = f x U' = U' x U' = [0, 0, 0]
Since this gives a null-vector, u will also be a null-vector. In total this results in a matrix where the first two rows are all zero, and thus each vector multiplied with this matrix will end up with x and y beeing 0.
Hope this helps you. The formulas for gluLookAt are taken from here

problems with gluLookAt

I am trying to test gluLookAt using this code. But I can see only a black screen. What is wrong with this code ? Is there any basic concept about glulookAt (or opengl camera) that I need to understand.
glViewport(0,0,640,480);
glEnable(GL_DEPTH_TEST);
glClearColor(0,0,0,1);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluLookAt(0,0,5,0,0,0,0,0,1);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glBegin(GL_POLYGON);
glColor3f(1, 0, 0);
glVertex2d(0.25, 0.25);
glVertex2d(-0.25, 0.25);
glVertex2d(-0.25, -0.25);
glVertex2d(0.25, -0.25);
glEnd();
One issue is the up vector of gluLookAt is in the same direction as the look direction.
All you need to do is set +Y up and it should work...
gluLookAt(0, 0, 0.5, //position is +0.5 along Z (NOTE: 0.5, not 5. see below),
0, 0, 0, //looking at a 0.5x0.5 X/Y quad at the origin
0, 1, 0 //rotated such that +Y is up
);
The other issue is that gluLookAt shouldn't be applied to the projection matrix. It'll work for now but will break lighting later. Move it to the modelview matrix:
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt(... as before ...);
Assuming the projection matrix hasn't been set, changing the from position of gluLookAt back to your 5 will make the quad disappear. This is because the default projection gives a viewing volume of an orthographic -1 to 1 cube. With the "camera" now too far away it won't see anything. This is where you'll want to investigate changing the projection matrix. Maybe increase the size of the orthographic projection with glOrtho(), or look into the more complex but natural gluPerspective().

OpenGL the text moving with the camera and also the texture was also behaving the same way expert advise needed

i am Making a pool game using openGL and finding this problem vary irritating.
while i am trying to print one text on the screen and move my camera the text is also leaving its original position in the window and moving with the camera.
here is the code that i have in draw().
void display() {
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // Clear Screen and Depth Buffer
glLoadIdentity();
// GLfloat position1[] = { 00.0, 100.0, 00.0, 1.0 };
//cam position update
gluLookAt( world.camera->cameraFrom.x,world.camera->cameraFrom.y,world.camera->cameraFrom.z, world.camera->cameraTo.x,0,world.camera->cameraTo.z, 0,1,0); // Define a viewing transformation
// Pop the current matrix stack
//**************************************************************
drawTable(world.table);
world.update();
glPushMatrix();
sprintf(str, "Player 1 Score: 1, Player 2 Score: 10");
glRasterPos2f(10, 10);
glutBitmapString(font,(unsigned char*)str);
glPopMatrix();
glutSwapBuffers();
}
glRasterPos() transforms the given position by the modelview and projection matrices. You'll have to reset those to something that positions your text correctly:
void display()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glMatrixMode( GL_PROJECTION );
glLoadIdentity();
// set projection matrix here
glMatrixMode( GL_MODELVIEW );
glLoadIdentity();
//cam position update
gluLookAt
(
world.camera->cameraFrom.x, world.camera->cameraFrom.y, world.camera->cameraFrom.z,
world.camera->cameraTo.x, 0, world.camera->cameraTo.z,
0,1,0
);
drawTable(world.table);
world.update();
glMatrixMode( GL_PROJECTION );
glLoadIdentity();
// set appropriate projection matrix here
glMatrixMode( GL_MODELVIEW );
glLoadIdentity();
sprintf(str, "Player 1 Score: 1, Player 2 Score: 10");
glRasterPos2f(10, 10);
glutBitmapString(font,(unsigned char*)str);
glutSwapBuffers();
}
Or use glWindowPos2f() instead, which bypasses both matrices and sets the raster position directly.

Moving some points in z-axis in OpenGL doesnt work

I have a simple rectangle i have drawn on screen in opengl.
My target is to draw this rectangle in 3D so the left side of it is deeper (z-axis) than the right side.
Look at this pic so you can see what i mean:
http://www.battleteam.net/tech/fis/docs/images/metroid_hud1.png
This is the code i use to draw a rectangle which uses different colors and i moved both points on left side up a bit.
glColor4f( 1.0, 1.0, 1.0, 1.0 );
glVertex3f( 0, -20, z_left );
glColor4f( 0.0, 1.0, 1.0, 1.0 );
glVertex3f( SQUARE_WIDTH, 0, 0 );
glColor4f( 1.0, 0.0, 1.0, 1.0 );
glVertex3f( SQUARE_WIDTH, SQUARE_HEIGHT, 0 );
glColor4f( 1.0, 1.0, 0.0, 1.0 );
glVertex3f( 0, SQUARE_HEIGHT-20, z_left );
I use the z_left variable to dynamically change the z-value for the both points on the left side to move these points on the z-axis. But what happens is that the rectangle gets cut off from the left side. This happens when the z_left value reaches the zFar or zNear Variable defined via the "glOrtho" function call.
My glOrtho looks like this:
glOrtho( 0, SCREEN_WIDTH, SCREEN_HEIGHT, 0, -100, 100 );
So if the z_left gets higher than 100 or less than -100 then that strange cutting off begins. I dont know why. I expected to get the left side of the rectangle to be moved on z-axis, means moving it deeper (away from eye) or closer.
Can somebody tell me whats wrong here? The rest of the code is pretty simple and standard.
A simple rectangle in a 3D environment being changed a bit to have a "3d panel" like rectangle.
My OpenGL init looks like this.
glClearColor( 0, 0, 0, 0 );
glMatrixMode( GL_PROJECTION );
glLoadIdentity(); glOrtho( 0, SCREEN_WIDTH, SCREEN_HEIGHT, 0, -100, 100 );
glMatrixMode( GL_MODELVIEW );
glLoadIdentity();
Thanks for any help.
Ortographic projections have that property, you can move them in the Z axis, but the object looks the same. Switch to a perspective projection, on which objects get smaller with distance to the camera.
About the culling, you're drawing outside the viewing cube (when Z < -100 or Z > 100). The projection will cull away anything outside it's view.

OpenGL and GLUT uncomprehension

i'm completely don't understanding opengl+glut works....
PLEASE explain why he does that? =(
I have simple code
void display()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glPushMatrix();
ChessboardSurrogate.Draw(6,8,50, 0,0); // draw chessboard(6x8) in (0,0,0) edge per cell 50
glPopMatrix();
glPushMatrix();
//glutSolidCube(100); //!!!!!
glPopMatrix();
glLoadIdentity();
gluLookAt( 0.0, 0.0, -testSet::znear*2,
0.0, 0.0, 0.0,
0.0, 1.0, 0.0);
glMultMatrixf(_data->m); // my transformation matrix
glutSwapBuffers();
}
And i get the expected result. screenshot #1
Then I uncomment glutSolidCube(100). In any case, I even do push/pop current matrix, and later override it by identity matrix.... i think that i would see the same result image with cude... BUT! i see THIS screenshot #2 What the..... &*^##$% Why?
If i add code
glRotatef(angleX,1.0,0.0,0.0);
glRotatef(angleY,0.0,1.0,0.0);
before glutSwapBuffers, than I'll see that the chessboard on the spot .....
screenshot #3
This is probably only half the answer, but why on earth are you setting your matrices AFTER drawing ? What do you expect it to do ?
so :
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
gluLookAt( 0.0, 0.0, -testSet::znear*2,
0.0, 0.0, 0.0,
0.0, 1.0, 0.0);
glMultMatrixf(_data->m); // my transformation matrix
glPushMatrix();
ChessboardSurrogate.Draw(6,8,50, 0,0); // draw chessboard(6x8) in (0,0,0) edge per cell 50
glPopMatrix();
glPushMatrix();
//glutSolidCube(100); //!!!!!
glPopMatrix();
glutSwapBuffers();
moreover, always make sure you're setting the right matrix :
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt()...
Lastly, unless your Draw() method changes the modelview matrix, your Push/PopMatrix is useless and should be avoided (for performance and portability reasons since it's deprecated)