This toturial was written for me by Imran Khan (imranahmedkhan82@hotmail.com,
iak1982@yahoo.com). |
||||||
Use of Camera: In this section we will learn 'How to use Camera in Opengl' . Camera usage is the most important feature of OpenGL. As it is impossible to move in a 3D-World physically. We therefore use Camera in order to move in 3D-world. First of all we will define a Vector Structure.
First we define 2 constructors, one with no arguments ( which is also called Default Constructor) and one with 3 arguments. As we are in 3D , we will use 3 coordinates (x,y,z). we assign the value of the arguments (new_x,new_y,new_z) to its local variable(x,y,z). Next we Overload ( + , - , * , / ) Operators.So that we can easily add,subtract,multiply,divide Vectors . We will use this structure in order to move the camera. Now declare 3 variable of " tVector " structure.
First variable use for Setting position of the Camera.
We can easily understand this by the following figure. Now I think, it will be more clear that why we use Vector structure. Now we will define a method which will set the position,view and up vector of the Camera.
It takes 9 arguments : (x,y,z) for each ( position,view,up ). We use this method so that we can set the initial postion of the Camera.First we set initliaze the mPos then mView and then mUp.We will call this method in the InitGL method. So that our Camera postion will be set at the bigenning of the program. Now the InitGL method will be look like :
As we set the position of the Camera. We just assign values to some variables (vector ). Now we will call a predefined function 'gluLookAt' and passed the variables, which we have already set, to this method. We will call the this method in 'DrawGLScene' method. Before each shape we draw, we will first call 'gluLookAt '. Before drawing Pyramid....
Before Drawing Cube...
"gluLookAt " , places the camera at the position of the first 3 parameters, places the center of the scene at the position of the next 3 parameters, and the last 3 parameters describe the vector that is "up".We done nothing , we just passed the variable , which we already set. You can play with the initial vlaues of Position,View and Up Vectors, to see wat changes it will produce. |
metadata block |
|
see also: |
|
Correspondence about this page |
This site may have errors. Don't use for critical systems.
Copyright (c) 1998-2023 Martin John Baker - All rights reserved - privacy policy.