I am rotating the camera around itself (when the camera is located at (0,0,0)) using the following:
glRotatef(x_camera_angle, 1.0, 0.0, 0.0);
glRotatef(y_camera_angle, 0.0, 1.0, 0.0);
I wish to move the camera in the direction its looking. For example, I want to move the camera 5 units right and 3 units forward. How can this be done?
I know that there's a lot of information out there, but I have yet to find a satisfying and simple answer.
Any help would be highly appreciated.
The best way is probably to translate before the rotate:
glTranslatef(5, 0, 3);
glRotatef(x_camera_angle, 1.0, 0.0, 0.0);
glRotatef(y_camera_angle, 0.0, 1.0, 0.0);
Related
I want to ask a question about transformation.
glPushMatrix();
glTranslatef(0.0, -10, 0.0);
glScalef(5000.0, 10.0, 5000.0);
glPushMatrix();
glColor3f(0,0,0);
glutSolidCube(1);
glColor3f(0.0, 0.0, 0.0);
glutWireCube(1);
glPopMatrix();
glPopMatrix();
The above code is the one I wrote to create a cube. In this case, I have understood the centre of the cube will be on y = -5. Did I understand correctly?
One of the great things about computer graphics is that you can always run the program and see what happens. And then swap the order of the translate and scale calls and run it again. You'll figure it out.
In this case, you may have difficulties because there's no reference to compare the cube to. I often use a little drawAxes() function, and recommend it to students, that just draws three lines from 0,0,0 to each of X=1, Y=1, Z=1. (Helps to draw the X line in red, Y in green, Z in blue.) Draw the axes as the very first thing you do so you've got a reference point, draw them again anytime you're not sure where a series of transformations will end up.
I'm doing this OpenGL project for my Computer Graphics class, where I display an object and I rotate it and stuff, the thing is that at the beginning of the project we used glOrtho() and it looked really great.
But now the teacher said that we have to use glFrustum() for perspective and if I use that function, the object is drawn like this and I really don't know why does this happens:
This is my code from the init() function where everything changes:
void init (void)
{
/* select clearing (background) color */
glClearColor (0.0, 0.0, 0.0, 0.0);
glEnable(GL_CULL_FACE);
glCullFace(GL_BACK);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(-100.0, 100.0, -60.0, 160.0, -100.0, 100.0);
//glFrustum(-100, 100 ,-100 ,100 ,1 , 40);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glRotatef(90,0,1,0);
}
I'd appreciate your help.
EDIT: If I use glFrustum(-100, 100, -100, 100, 20, 200) it looks like this, like I'm getting closer but what about the left, right, top and bottom parameters? Are they okay with that values?
It's hard to be certain without more information. Perhaps the model could give some insight. But I suspect it may have to do with your clipping planes (nearVal and farVal as described here) arguments passed to glFrustum (1, 40). Perhaps try setting them to a broader range like your glOrtho call: 1, 150 (Note: neither nearVal or farVal can be negative when passed to glFrustum).
This all depends on the scale of the model and how it is positioned relative to the camera. If part of the model falls outside of the clipping planes, then it will be, well... clipped.
I have created a helicopter that moves around a track, now where I'm having trouble is that we're supposed to have 2 light sources in the game, one that replicates the sun ( which i've done it wasnt too hard) and the second is a spotlight located on the actual helicopter itself. This is essentially what i have so far:
GLfloat specular2[] = { 1.0, 1.0, 1.0, 1.0 };
GLfloat position2[] = { vx, vy, vz, 1.0 };
glLightfv(GL_LIGHT2, GL_DIFFUSE, ambientLight);
glLightfv(GL_LIGHT2, GL_SPECULAR, specular2);
glLightfv(GL_LIGHT2, GL_POSITION, position2);
glLightf(GL_LIGHT2, GL_SPOT_CUTOFF, 60.0f);
glLightf(GL_LIGHT2, GL_SPOT_EXPONENT, 100.0f);
glEnable(GL_LIGHT2);
But, it doesn't do anything that I notice. I've done a great deal of research on lights and i just cant seem to figure it out. By the way ( vx,vy,vz) is the current position i want the light to be in infront of the helicopter.
The value you use for GL_SPOT_EXPONENT is very large:
glLightf(GL_LIGHT2, GL_SPOT_EXPONENT, 100.0f);
This controls how quickly the intensity drops off from the center of the cone. For example, with this value of 100.0, the intensity would be down to about 20% at angle of just 10 degrees off the cone center. You may want to try a much smaller value to see if it gives you a better result.
While the vales are not shown, this call looks suspicious just from the naming. It's using a variable named ambientLight to set the diffuse component of the light source:
glLightfv(GL_LIGHT2, GL_DIFFUSE, ambientLight);
Also, you do not specify a direction for the spot light. The default is (0.0, 0.0, -1.0). Unless there is anything in the negative z-direction from the helicopter, the spot light will not illuminate anything. You will need to specify GL_SPOT_DIRECTION if the default is not what you want.
It's also important to be aware that the current modelview transformation is applied when a light source position and direction are specified. So if your helicopter is transformed, it's easiest to specify the position/direction of the spotlight while the transformation is already set up, and before making the draw calls.
I have an object which is at (0.0, 0.0, -39.0) and never moves, the rest of the scene moves relative to this object; I want the camera to look at this object and at the same direction. When I do this:
gluLookAt(0.0, 120.0, 10.0, 0.0, 0.0, -39.0, 0.0, 0.0, 1.0);
drawobject();
glTranslatef(0.0, y, 0.0);
glRotatef(angle, 0.0, 0.0, 1.0);
drawScene();
instead of scene rotating around object (actually around world z-axis), it rotates around its center. If I change the order of translation and rotation then it rotates around object correctly, but it translates in the direction of scene y-direction (I want it translate towards world y-direction). I think I know why this happens, but I don't know how to fix it. I guess I have made myself confused XD.
I should add that I use arrow keys to get input from user to change "y" and "angle". So, basically if this object is the player, I want the camera look at its back at all times, and player can move around using the keyboard. However, instead of moving the player, I decided to move the scene. I am not sure if this is the standard approach though.
I have this data and i need to come up with the transformation matrix:
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(-1.0, 1.0, -1.0, 1.0, -1.0, 1.0);
Suppose that there is a unit cube centered at the origin and we want to view it by looking directly at one of its corners (i.e., at to the three axes). What 4X4 transformation matrix would be required?
I understand that it is translation +Scaling, but i am not sure how to put it in matrices form.
Look at the OpenGL glOrtho man page:
glOrtho
It tells you how the matrix is computed.
Here is a copy of the man page better formatted for web use:
glOrtho