Monday, May 30, 2011

OpenGl-1

OpenGl is a huge area and very easily you can give it up considering that there are so many concepts to pickup.Beginners find it complex to understand the basics, which once understood, can make using this excellent API real fun and produce stunning graphic results.I feel it is always a good idea to keep theory to as little as possible and quickly start getting vizual results for beginners to feel that they are creating something and not get bogged down with theoritical concepts.I will provide the Iphone sources from the next post onwards but first the the dirty part ie.some theory :)

VIEWPORT
Is the the part where you can draw or render the output.
We can change the viewport using the function glViewport(glInt x, glInt y,Glsizei x, Glsizei y)
Here x,y are the co-ordinates measured from lower left of the screen 0,0
The viewport is dependant on the world co-ordinates set by glOrtho which are mapped into device co-ordinates.
Later the device co-ordinates are mapped to the viewport as pixellated co-ordinates using glViewport();

GlOrtho()
Since we brought in the GlOrtho() let me dig into it a little before progressing further. To understand
this function let us look at the syntax for this
GLvoid glOrtho( GLdouble left, GLdouble right,
GLdouble bottom, GLdouble top,
GLdouble nearClip, GLdouble farClip );

If we were think of the 6 parameters above each as a single point then joining this could represent a cube(which it really does).It represents a box in virtual space like the one below and this becomes our viewing volume.


Any object outside the viewing volume will be clipped and not visible.

The viewing volume is flattened to represent a 2D screen.To explain this let us create a viewing volume of 2 for all points
glOrtho( -2.0, 2.0, -2.0, 2.0, -2.0, 2.0 );



Vertex
It is a point in space represented by x,y,z,w co-ordinates.It should be easy to understand x,y,z represents
a 3D point with +z into the screen and –z away from the screen.
W is the scaling factor ranging 0.0 to 1.0

Syntax: GLvoid glVertex2f( GLfloat x, GLfloat y ) or
glVertex3f( x, y, z )


There are different functions for defining a vertex using double, float and int type parameters and the only difference is the changing glVertex2d() or glVertex2i().

Additionally we can also create an array for the x,y,z separately and pass that as a vector parameter like the example below

static GLfloat leftVertex[] = { 1.0, 1.0, 1.0 } and then pass it to
glVertex3fv(leftVertex).



Geometric Primitives

All drawing done on screen using OpenGl will be from the shape primitives like Lines,Points,Line strips,Line loops,Triangles,quads etc.These are called the drawing modes and the stored as enum types in the OpenGl specification.The concept is simple we declare the co-ordinates within the primitive drawing mode and OpenGl will do the job of rendering the display.To reduce the theory and improve clarity clarity let us use an example to draw 2 points :

static float v[] = { 0.3, 0.7 };
glPointSize( 2.5 ); //specifies the point size or thickness
glBegin( GL_POINTS ); //What type of drawing to render using the vertices specified
glVertex2fv( v );
glVertex2f( 0.6, 0.2 );
glEnd(); //end of GL_point declarations




Or
static float v[] = { 0.3, 0.7 };
glColor3f( 1.0, 0.0, 0.0 ); //enabling color
glLineWidth( 2.5 );
glBegin( GL_LINES );
glVertex2fv( v ); //defining a vertex using array syntax
glVertex2f( 0.6, 0.2 ); //defining a vertex using x,y syntax
glVertex2f( 0.3, 0.2 );
glVertex2f( 0.6, 0.7 );
glEnd();



Transformations
I will delve deeper into transformation once you get comfortable with the basic idea of transformation here but to start with let us consider 2 major players involved when we are using a camera to photograph an object and try understand the same concept in the 3D space while using OpenGl.

Assume you want to take a photo of a person(ie.model in OpenGl).I have a camera and I start clicking.Next if we want to take the photo at a different angle then what are the options available to us??

1.Keeping the Person/Model stationary and moving the camera
i.I can change my camera position keeping the model stationary and probably go to the left,right,upwards or downwards and view the object differently even though the Person/Model has not moved.

ii.I can zoom in and zoom out the camera lens and have a change in the perspective view of the Model
This is PROJECTION TRANSFORMATION

2.Keeping the camera stationary and moving the Person/Model position
i.Moving the initial position on the person with respect to the stationary camera will change the way the camera views the person.

ii.Rotate/turn the Person with respect to his/her current position will change the view
iii.Moving the person closer or away from the camera will cause the same effect and the camera zoom in our zoomout.
This is MODEL TRANSFORMATION


This should give you a rough idea of how OpenGl works.I will add one more blog on the concepts which are essential to get started and then we will do all our work on the Iphone(source included) so that we do not get lost only learning the OpenGl API which anyway is better explained on the OpenGl website.

No comments:

Post a Comment