How to Set Camera View using gluLookAt() function? - opengl

I have used glOrtho(500, 600, 600, 700, -100, 100) projection with this i want to use camera view settings with gluLookAt() method what should be the parameters for gluLookAt function on this projection..

glOrtho builds a matrix that forms the "lens" of your virtual camera. gluLookAt moves that virtual camera.
http://msdn.microsoft.com/en-us/library/windows/desktop/dd368663%28v=vs.85%29.aspx
eyeX/Y/Z are where the camera is.
centerX/Y/Z are the spot at which the camera is looking.
upX/Y/Z is which way up the camera is.
An example use might be:
gluLookAt
(
0.0f, 2.0f, -16.0f,
0.0f, 0.5f, 0.0f,
0.0f, 1.0f, 0.0f
);
This will put the camera 16 units backwards, raise it slightly, point slightly above 0, 0, 0, with the top of the screen pointing along Y+.
You could change the first value to move the camera.
Change the second to change which part of the scene it's pointed at.
Change the third to roll/bank the camera.
The important question, however, is what do you want to do with it?

Related

Rotate the vertexes of a cube using a rotation matrix

I'm trying to rotate a cube's vertexes with a rotation matrix but whenever I run the program the cube just disappears.
I'm using a rotation matrix that was given to us in a lecture that rotates the cube's x coordinates.
double moveCubeX = 0;
float xRotationMatrix[9] = {1, 0, 0,
0, cos(moveCubeX), sin(moveCubeX),
0, -sin(moveCubeX), cos(moveCubeX)
};
I'm adding to the moveCubeX variable with the 't' key on my keyboard
case 't':
moveCubeX += 5;
break;
And to do the matrix multiplication I'm using
glMultMatrixf();
However when I add this into my code when running it the cube has just disappeared. This is where I add in the glMultMatrixf() function.
void display(void)
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
gluLookAt(pan, 0, -g_fViewDistance,
pan, 0, -1,
0, 1, 0);
glRotatef(rotate_x, 1.0f, 0.0f, 0.0f); //Rotate the camera
glRotatef(rotate_y, 0.0f, 1.0f, 0.0f); //Rotate the camera
glMultMatrixf(xRotationMatrix);
I'm struggling to see where it is I have gone wrong.
OpenGL uses matrices of size 4x4. Therefore, your rotation matrix needs to be expanded to 4 rows and 4 columns, for a total of 16 elements:
float xRotationMatrix[16] = {1.0f, 0.0f, 0.0f, 0.0f,
0.0f, cos(moveCubeX), sin(moveCubeX), 0.0f,
0.0f, -sin(moveCubeX), cos(moveCubeX), 0.0f,
0.0f, 0.0f, 0.0f, 1.0f};
You will also need to be careful about the units for your angles. Since you add 5 to your angle every time the user presses a key, it looks like you're thinking in degrees. The standard cos() and sin() functions in C/C++ libraries expect the angle to be in radians.
In addition, it looks like your matrix is defined at a global level. If you do this, the elements will only be evaluated once at program startup. You will either have to make the matrix definition local to the display(), so that the matrix is re-evaluated each time you draw, or update the matrix every time the angle changes.
For the second option, you can update only the matrix elements that depend on the angle every time the angle changes. In the function that modifies moveCubeX, add:
xRotationMatrix[5] = cos(moveCubeX);
xRotationMatrix[6] = sin(moveCubeX);
xRotationMatrix[9] = -sin(moveCubeX);
xRotationMatrix[10] = cos(moveCubeX);

How to use glOrtho2D with moving positions?

I'm trying to move the camera based on a player (simple square)'s position (x, y). The scale is relatively small and the character is 0.5f by 0.5f.
How can I focus the camera on the player's x, and y coordinates using glOrtho2D?
I am really confused by how you use left, right, down, and up. It makes absolutely no sense, it apparently defines the screen ratio as well as the position in which it draws?
Any help is EAGERLY appreciated.
I switched from the 3d version (gluLookAt) which was the following:
gluLookAt(jake.px, 0.0f, jake.pz + 20, jake.px, 7.0f, jake.pz, 0.0f, 1.0f, 0.0f );
and on Resize
gluPerspective(45.0f, ratio, 0.1f, 100.0f);

Push/Pop matrix and individual object rotation around its own axis in OpenGL

This has been asked many, many times before and I've read loads of posts and forums on the internet about it, but I just can't get one object to rotate around it's own axis.
I have several objects drawn like this:
gl.glMatrixMode(GL2.GL_MODELVIEW);
gl.glLoadIdentity();
.....
gl.glPushMatrix();
gl.glRotatef(angle, 0.0f, 1.0f, 0.0f);
gl.glTranslatef(0.0f, 0.0f, 0.0f);
texture.bind(gl);
gl.glTexCoordPointer(2, GL2.GL_FLOAT, 0, textureRLt);
gl.glNormalPointer(GL2.GL_FLOAT, 0, normalRLt);
gl.glVertexPointer(3, GL2.GL_FLOAT, 0, vertexRLt);
gl.glDrawElements(GL2.GL_TRIANGLES, countI, GL2.GL_UNSIGNED_SHORT, indexRLt);
gl.glPopMatrix();
And this is drawn correctly, with all textures and normals applied..
I know that OpenGL executes commands in reverse, so that's why glRotatef is first. Also I know that all rotations are around the origin, so I need to translate the object to that origin (not that I think I have to, because "the pen" is already at the origin because I save the matrix before drawing every object and pop it afterwards). Is it something with glDrawElements? Something doesn't seem right.
Any help will be greatly appreciated. :)
Edit: I can see how the objects rotate around the main x axis, but I want them to rotate around their local x-axis.
"OpenGL executes commands reversely", means it multiplies the transformation matrix from right rather than left. What does this mean?
Imagine transformation A and B:
y = Ax
transforms x by A and yields y
This is equivalent to:
// sudo code:
glA()
glDraw(x)
Now, usually, in programming you think you get the transformations in order that you write them. So, you think that
glA()
glB()
glDraw(x)
would give you
y = BAx
but that is wrong. You actually get:
y = ABx
This means that, first B is applied to x and then A to the result.
Put in english, take a look at this example:
glScalef(...) // third, scale the whole thing
glTranslatef(...) // second, translate it somewhere
glRotatef(...) // first, rotate the object (or course,
// around its own axes, because the object is at origin)
glDrawElements(...) // draw everything at origin
So, what you need to do is to write:
// When everything is drawn, move them to destination:
gl.glTranslatef(destination[0], destination[1], destination[2]);
// For every object, rotate it the way it should, then restore transformation matrix
// object: RLt
gl.glPushMatrix();
gl.glRotatef(angle, 0.0f, 1.0f, 0.0f);
texture.bind(gl);
gl.glTexCoordPointer(2, GL2.GL_FLOAT, 0, textureRLt);
gl.glNormalPointer(GL2.GL_FLOAT, 0, normalRLt);
gl.glVertexPointer(3, GL2.GL_FLOAT, 0, vertexRLt);
gl.glDrawElements(GL2.GL_TRIANGLES, countI, GL2.GL_UNSIGNED_SHORT, indexRLt);
gl.glPopMatrix();
// object: RLt2
gl.glPushMatrix();
gl.glRotatef(angle2, 0.0f, 1.0f, 0.0f);
texture.bind(gl);
gl.glTexCoordPointer(2, GL2.GL_FLOAT, 0, textureRLt2);
gl.glNormalPointer(GL2.GL_FLOAT, 0, normalRLt2);
gl.glVertexPointer(3, GL2.GL_FLOAT, 0, vertexRLt2);
gl.glDrawElements(GL2.GL_TRIANGLES, countI2, GL2.GL_UNSIGNED_SHORT, indexRLt2);
gl.glPopMatrix();
I am not sure, whether you are updating 'angle' variable periodically. If you are not, then there won't be any rotation. The pseudo code is below. You can use the gluttimerfunc for periodic update of the variable.
for (every opengl loop)
angle+=5.0f
Satish

3D object in front of 2D Sprite (background) , how?

I'm new to Direct3D and I was on a project taking pictures from a webcam and draw some 3D objects in front of it.
I was able to render webcam images as background using Orthogonal Projection.
//init matrix
D3DXMatrixOrthoLH(&Ortho, frameWidth, frameHeight, 0.0f, 100.0f);
//some code
D3DXVECTOR3 position = D3DXVECTOR3(0.0f, 0.0f, 100.0f);
g_pSprite->Begin(D3DXSPRITE_OBJECTSPACE);
g_pSprite->Draw(g_pTexture,NULL,&center,&position,0xFFFFFFFF);
g_pSprite->End();
Then I tried to insert a simple triangle in front of it. The Matrices are setup as follow
D3DXMATRIXA16 matWorld;
D3DXMatrixTranslation( &matWorld, 0.0f,0.0f,5.0f );
g_pd3dDevice->SetTransform( D3DTS_WORLD, &matWorld );
D3DXMATRIXA16 matProj;
D3DXMatrixPerspectiveFovLH( &matProj, D3DX_PI / 4, 1.0f, 1.0f, 100.0f );
g_pd3dDevice->SetTransform( D3DTS_PROJECTION, &matProj );
5.0 should be < 100.0 and the triangle is supposed to be appear in front of the images. However it does not appear unless set the z position to 0. At position 0, i can see the triangle but background is blank.
Do you guys have any suggestions?
I would not draw the webcam image in the object space (D3DXSPRITE_OBJECTSPACE) if you intend to use your image solely for background purpose; something like
D3DXVECTOR3 backPos (0.f, 0.f, 0.f);
pBackgroundSprite->Begin(D3DXSPRITE_ALPHABLEND);
pBackgroundSprite->Draw (pBackgroundTexture,
0,
0,
&backPos,
0xFFFFFFFF);
pBackgroundSprite->End();
should hopefully do what you're looking for.
As a quick fix you could disable depth testing as follows;
g_pd3dDevice->SetRenderState(D3DRS_ZENABLE, D3DZB_FALSE);
This way the z-index of the primitives being drawn should reflect the order in which they are drawn.
Also, try using the PIX debugging tool (this is bundled with the DirectX SDK). This is always my first port of call for drawing discrepancies as it allows you to debug each Draw call separately with access to the depth buffer and transformed vertices.

Using glRotate and glTranslate with collision detection

Say I use glRotate to translate the current view based on some arbitrary user input (i.e, if key left is pressed then rtri+=2.5f)
glRotatef(rtri,0.0f,1.0f,0.0f);
Then I draw the triangle in the rotated position:
glBegin(GL_TRIANGLES); // Drawing Using Triangles
glVertex3f( 0.0f, 1.0f, 0.0f); // Top
glVertex3f(-1.0f,-1.0f, 0.0f); // Bottom Left
glVertex3f( 1.0f,-1.0f, 0.0f); // Bottom Right
glEnd(); // Finished Drawing The Triangle
How do I get the resulting translated vertexes for use in collision detection? Or will I have to manually apply the transform myself and thus doubling up the work?
The reason I ask is that I wouldn't mind implementing display lists.
The objects you use for collision detection are usually not the objects you use for display. They are usually simpler and faster.
So yes, the way to do it is to maintain the transformation you're using manually but you wouldn't be doubling up the work because the objects are different.
Your game loop should look like (with c++ syntax) :
void Scene::Draw()
{
this->setClearColor(0.0f, 0.0f, 0.0f);
for(std::vector<GameObject*>::iterator it = this->begin(); it != this->end(); ++it)
{
this->updateColliders(it);
glPushMatrix();
glRotatef(it->rotation.angle, it->rotation.x, it->rotation.y, it->rotation.z);
glTranslatef(it->position.x, it->position.y, it->position.z);
glScalef(it->scale.x, it->scale.y, it->scale.z);
it->Draw();
glPopMatrix();
}
this->runNextFrame(this->Draw, Scene::MAX_FPS);
}
So, for instance, if i use a basic box collider with a cube the draw method will :
Fill the screen with a black color (rgb : (0,0,0))
For each object
Compute the collisions with position and size informations
Save the actual ModelView matrix state
Transform the ModelView matrix (rotate, translate, scale)
Draw the cube
Restore the ModelView matrix state
Check the FPS and run the next frame at the right time
** The class Scene inherits from the vector class
I hope it will help ! :)