Thursday, June 30, 2011

OpenGL Lighting

Here I will show you how to use opengl light within your opengl application.
You enable the depth, the lighting, and which light you are using.

To perform basic lighting, you will need to
* Enable lighting support by OpenGL : When lighting is enabled, it automatically generates colors for your models
* Enable a light source :
* Provide information about the surface you will be lighting
a. Surface normals
b. Material properties

1.Enable depth Support
Firstly clear the depth buffer glClearDepth(1);
Secondly enable depth testing glEnable(GL_DEPTH_TEST);

2.Enable OpenGL lighting support
glEnable(GL_LIGHTING);

3.Enable the light ranging from GL_LIGHT0 -> GL_LIGHT4
glEnable(GL_LIGHT0); or glEnable(GL_LIGHT1)/glEnable(GL_LIGHT2) etc

Rest is pretty clear if you go through the code listed below

#include < GL/glut.h >
GLfloat angle = 0.0;

void drawPlane()
{
// Draw a red x-axis, a green y-axis, and a blue z-axis. Each of the
// axes are ten units long.
glBegin(GL_LINES);
glColor3f(1, 0, 0); glVertex3f(-10, 0, 0); glVertex3f(10, 0, 0);
glColor3f(0, 1, 0); glVertex3f(0, -10, 0); glVertex3f(0, 10, 0);
glColor3f(1, 1, 1); glVertex3f(0, 0, -10); glVertex3f(0, 0, 10);
glEnd();
}

void drawCube (void)
{
//Color will not work if this is not enabled
glEnable(GL_COLOR_MATERIAL);
glRotatef(angle, 1.0, 0.0, 0.0);
glRotatef(angle, 0.0, 1.0, 0.0);
glRotatef(angle, 0.0, 0.0, 1.0);
glColor3f(1.0, 0.0, 0.0);

glutSolidCube(2);
}

void init (void)
{
glEnable(GL_DEPTH_TEST);
glEnable (GL_LIGHTING);
glEnable (GL_LIGHT0);
}

void display (void)
{
glClearColor (0.0,0.0,0.0,1.0);
glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
gluLookAt (0.0, 0.0, 5.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);
drawPlane();
drawCube();
glutSwapBuffers();
angle ++;
}

void reshape (int w, int h)
{
glViewport (0, 0, (GLsizei)w, (GLsizei)h);
glMatrixMode (GL_PROJECTION);
glLoadIdentity ();
gluPerspective (60, (GLfloat)w / (GLfloat)h, 1.0, 100.0);
glMatrixMode (GL_MODELVIEW);
}

int main (int argc, char **argv) {
glutInit (&argc, argv);
glutInitDisplayMode (GLUT_DOUBLE | GLUT_RGBA | GLUT_DEPTH);
glutInitWindowSize (500, 500);
glutInitWindowPosition (100, 100);
glutCreateWindow ("OpenGL Lighting");
init ();
glutDisplayFunc (display);
glutIdleFunc (display);
glutReshapeFunc (reshape);
glutMainLoop ();
return 0;
}

OpenGL Translation/Rotation over C++ Example

#include < GL/glut.h >
float yRotationAngle = 0.0f;
float yLocation = 0.0f;

void display (void)
{
glClearColor(0.2f, 0.7f, 0.0f, 1.0f);

glClear(GL_COLOR_BUFFER_BIT);
glLoadIdentity();

// Push everything 10 units back into the scene
glTranslatef(0.0f, 0.0f, -10.0f);

// Translate our object along the y axis
glTranslatef(0.0f, yLocation, 0.0f);


// Rotate our object around the y axis
glRotatef(yRotationAngle, 0.0f, 1.0f, 0.0f);

// Render the cube
glutWireCube(2.0f);

// Flush buffer to window
glFlush();


yRotationAngle += 0.01f;

//Start opposite rotation if the angle >360 or < -360 if (yRotationAngle > 360.0f)
yRotationAngle -= 360.0f;
}

void reshape (int width, int height)
{
glViewport(0, 0, (GLsizei)width, (GLsizei)height);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(60, (GLfloat)width / (GLfloat)height, 1.0, 100.0);
glMatrixMode(GL_MODELVIEW);
}


int main (int argc, char **argv)
{
glutInit(&argc, argv);
glutInitDisplayMode (GLUT_SINGLE);
glutInitWindowSize (500, 500);
glutInitWindowPosition (100, 100);
glutCreateWindow ("OpenGL Rotation and Translation");
glutDisplayFunc(display);
glutIdleFunc(display);
glutReshapeFunc(reshape);
glutMainLoop();
}

Tuesday, June 28, 2011

Model Transformation in OpenGL

If you are new to transformations and OpenGl concepts then I would like you to run though my previous artile listed here
The following are the OpenGL functions for Model transformations:
glRotate()
glScale()
glTranslate()

It will be helpful if you understand Matrices/Vectors and basic Trignometry along the 4 Quadrants around a circle.
For our use here we will use an Identity Matrix.

In mathematical terms if we were to represent this following transformation then
glTranslatef( 4.0, 2.0, 0.0 );
glRotatef( 90.0, 0.0, 0.0, 1.0 );
glVertex3f( 1.0, 2.0, 3.0 );

would be represented as(1st Quad rule applied)
|1 0 0 4.0| |Cos90 -Sin90 0 0| |1.0|      |x|
|0 1 0 2.0| |Sin90 Cos90 0 0| |2.0|   = |y|
|0 0 0 0.0| |0 0 1 0| |3.0|      |z|
|0 0 0 0 | |0 0 0 1| |1.0|      |y| Transformed vertex



The most important thing to remember about Model transformation is that it effects the co-ordinate system.As a result all models appear to be transformed because they are drawn relative to the new co-ordinates.
Also the current co-ordinate system is represented by the current matrix.

The final position or layout of the object will depend on the order the functions are called and there by the co-ordinate system is re-aligned.

Example 1: Let us see how a cube/rectangle will render itself if we were to follow the transformation given in the order below.

1. glPushMatrix() : this will create a new matrix of co-ordinates as the original axes layout and any further changes/transformation will happen to the new Matrix on the stack while the original Matrix is saved.




2. glTranslatef( 4.0, 2.0, 0.0 )
: This moves the co-ordinate system origin to the new location





3. glScalef( 1.0, 0.5, 1.0 ) : The co-ordinate system for y-axis is scaled down by half




4. glRotatef( 45.0, 0.0, 0.0, 1.0 ) : Rotates the co-ordinate system 45 degrees around the z-axis



5. Any custom function to draw a rectangle will result in the model being drawn wrt to the new matrix co-ordinate system.



6. glPopMatrix() : After drawing we want to reset our co-ordinate system to the original layout



If you visualize(as shown above), any model drawn is transformed according to the co-ordinate system layout changes.