#### Archived

This topic is now archived and is closed to further replies.

# moving the camera in OGL

This topic is 5179 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

hi, i have a triangle and i want to make my camera fly around it in all directions, like people do in demos (the movement of the camera).How do i do it? i''m not asking step-by-step code by explanation, although some example code wouldn''t hurt much

##### Share on other sites
Just move the triangle around to "simulate" a camera...

If you want to move the camera right, then move the triangle left :D It will look like the camera is moving to the right.

This is really how it''s done, although there are some other tricks or "hacks", maybe using gluLookAt.

##### Share on other sites
tnx, so i should move the objects rather then ''camera'' ok,
what about gluLookAt() ? i''ve heard it makes life easier

##### Share on other sites
Ok, the first step would be to use gluLookAt before you render any of your frame but I would advise you set about creating a camera yourself. As Anonymous Poster briefly mentioned the trick is to move the world around the camera rather than moving te actual camera. This is due to the fact that you are never able to render the scene from anywhere other than the (0, 0, 0) point in the world, looking straight down the z axis.

Ok so let''s just imagine that you are floating in space and there is an asteroid just in front of you. Ok next you are going to slide yourself to the right. What effect does this have on where the asteroid is in your view? Well, the asteroid is now to your left. In theory moving the asteroid to the left would actually have the same effect as moving yourself to the right. Got that. Well that is how cameras work in a simple sense.

The whole world is moved to make it look like the camera has changed position. Does it seem innefficient and arduous? Yes. Is it? Not really.

So how would I manage a simple camera. First of all I''d create a CCamera class and make sure it stores three float variables - mLocationX, mLocationY and mLocationZ. Pressing forwards backwards left and right will make these values increase and decrease in exactly the manner you would like if a camera were placed at these coordinates. So, pressing left will make x decrease and so on and so forth.

Thats easy enough, so how do we make it relate to of scene. Well, immediately after we clear the buffer, and before we do any drawing, we should call...

// translate to the exact opposite coordinates of our camera locationglTranslatef(-mLocationX, -mLocationY, -mLocationZ);

Ok we can now treat this location that we have translated to as our worlds (0, 0, 0). So to draw your triangle you would probably do something like...

// remember our locationglPushMatrix();// move to triangles location in worldglTranslatef(triX, triY, triZ);// draw triangle code goes here/* I can''t be bothered right now */// put us back where we wereglPopMatrix();

And there you have it. Let''s just run throught the process. Lets say you wanted to move the camera left, you press the left key and the mLocationX value changes to -10.0f. Before any of the drawing happens that lone glTranslate command will move us +10.0f on the X axis away from the (0, 0, 0) where the scene is always viewed from. So, when you draw your triangle at what is supposed to be a location just in front of us it will actually be just in front of us and to the right (+10.0f on x axis) making it look like the camera has moved to the left. Magic.

Anyway, that is a massive post, but to summmarise, we move the entire world in the opposite direction to our desired camera location to simulate camera movement.

Hope that helps.

##### Share on other sites
tnx ghosted, that helped me, really, but i was having in mind moving without the user intervention, no keys getting pressed, just simple flying accross the screen, ok i wont bother you anymore with this moving, i''ll get it sooner or later BUT here comes the next problem since the clearing color is black (or any other color) and when i ''move'' it''s not showing like i move but like an object (my triangle) is just spinning around its own axis, now i''d like to make the background look like it''s a world not 2d black screen.Don''t know how should i do that.I should maybe draw some lines bellow so it would look like a floor.I don''t know.Can you help me with this one please?

##### Share on other sites
I''m not too sure what sort of end effect your trying to achieve but drawing a little grid on the floor is easy enough. I''d do it using GL_LINES and simply loop through a process of placing vertices. The following code does it simply and hopefully you can use it create your own desired effect...

	// move to where we will draw the grid	glTranslatef (0.0f, -2.0f, -10.0f);	// set color to green	glColor3f(0.0f, 1.0f, 0.0f);	/*DRAW GRID OF LINES*/	glBegin(GL_LINES);		// draw lines going along z axis		for (int x = -5; x <= 5; x++)		{			glVertex3f((float)x, 0.0f, 5.0f);			glVertex3f((float)x, 0.0f, -5.0f);		}		// draw lines going across x axis		for (int z = -5; z <= 5; z++)		{			glVertex3f(5.0f, 0.0f, (float)z);			glVertex3f(-5.0f, 0.0f, (float)z);		}	glEnd();

If you chuck that in just before, or after, you draw the triangle it should appear just below you. Obviously you may need to change the intial translation values that place it in the scene for it to be where you want it.

Best of luck

##### Share on other sites
thank you so much ghosted.That grid really helps, now i have a feeling like its a 3d world

first i didnt see the grid, but then i added glFrustum() command and i now i can see the grid but no triangle OR i can see the triangle but no grid, don''t know why i cant see both of them at a time but i''ll fix it (i hope)

thanks again!

1. 1
2. 2
frob
14
3. 3
4. 4
5. 5
Rutin
12

• 12
• 10
• 57
• 14
• 15
• ### Forum Statistics

• Total Topics
632113
• Total Posts
3004184

×

## Important Information

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!