You are on page 1of 6

glEnable(GL_LIGHTING); // Enable lighting glEnable(GL_LIGHTING); // disable lighting glEnable(GL_COLOR_MATERIAL); // enable per vertex/per face color glShadeModel(GL_SMOOTH); // enable smooth

shading glShadeModel(GL_FLAT); // enable Flat shading glEnable(GL_LIGHTn) // (e.g : GL_LIGHT0) Enable up to 8 light sources Configure your light sources: glLightfv(GL_LIGHTn, GL_AMBIENT, color4f ); glLightfv(GL_LIGHTn, GL_DIFFUSE, color4f ); glLightfv(GL_LIGHTn, GL_SPECULAR, color4f ); // color4f is array of floats with rgba, eg: {1,0,0,1} Directional or positional light glLightfv(GL_LIGHTn, GL_POSITION, vector4f ); vector4f is x,y,z + w component Spotlight glLightfv(GL_LIGHTn, GL_POSITION, vector4f ); glLightfv(GL_LIGHTn, GL_SPOT_DIRECTION, vector3f ); glLightf(GL_LIGHTn, GL_SPOT_CUTOFF, angle); // angle is 0 to 180 glLightf(GL_LIGHTn, GL_SPOT_EXPONENT, exp); // exponent is 0 to 128 setup the current material glMaterialfv(faces, channel, color4f); Channel GL_AMBIENTGL_DIFFUSEGL_AMBIENT_AND_DIFFUSEGL_SPECULARGL_EMISSIONGL_SHININESS GL_COLOR_INDEXES Faces GL_FRONT,GL_BACK, GL_FRONT_AND_BACK. color4f is a float array that specifies red, green blue and alpha values

glGenTextures(1,&Tex);
glBindTexture(GL_TEXTURE_2D, tex); glBindTexture bind a named texture to a texturing target

glMatrixMode(GL_TEXTURE);
The glTexEnvf function sets a texture environment parameter. glTexEnvf(target, pname, param);

target A texture environment. Must be GL_TEXTURE_ENV. pname

The symbolic name of a single-valued texture environment parameter. Must be GL_TEXTURE_ENV_MODE. param A single symbolic constant, one of GL_MODULATE, GL_DECAL, GL_BLEND, or GL_REPLACE. glTexCoord set the current texture coordinates void glTexCoord2f( GLfloat s, GLfloat t);glTexParameterf, glTexParameteri, glTexParameterfv, glTexParameteriv: set texture parameters.
void glTexParameterf(GLenum target,GLenum pname, GLfloat param)

glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER,
GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR); glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR); glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_WRAP_S,GL_CLAMP); glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_WRAP_T,GL_CLAMP); glBegin(GL_QUADS); glTexCoord2f(1.0f,1.0f); glVertex3f(-1.0f, 1.0f, 1.0f); glTexCoord2f(1.0f,0.0f); glVertex3f( 1.0f, 1.0f, 1.0f); glTexCoord2f(0.0f,0.0f); glVertex3f( 1.0f, 1.0f,-1.0f); glTexCoord2f(0.0f,1.0f); glVertex3f(-1.0f, 1.0f,-1.0f); glEnd();

Basic Graphics primitives glBegin(int mode); and glEnd( void );

The parameter mode of the function glBegin can be one of the following:
GL_POINTS GL_LINES GL_LINE_STRIP GL_LINE_LOOP GL_TRIANGLES GL_TRIANGLE_STRIP GL_TRIANGLE_FAN GL_QUADS

GL_QUAD_STRIP GL_POLYGON

Attributes glLineWidth() glEnable() glDisable() glLineStipple() glPointSize() glColor3f() glClearColor() glClear() Viewing the Co-ordinates (setting up the camera position)glMatrixMode() glLoadIdentity()gluOrtho2D()glOrtho()gluLookAt();glViewport()gluPerspective() Modeling Transformation glScaled()glTranslate()glRotated()glPushMatrix()glPopMatrix()
Opening a window
glutInit(&argc, argv); glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB); glutInitWindowSize(400, 400); glutInitWindowPosition(200, 200); glutCreateWindow(""); Callback Functions (Event Driven Programming functionS) glutDisplayFunc(functionname); glutReshapeFunc(functionname); glutKeyboardFunc(functionname); glutMouseFunc(functionname); glutMotionFunc(functionname); glutMainLoop();

Introduction to shading models The reflection of light from a surface depends on many factors geometric factors Viewers eye position characteristics of the surface. A shading model dictates how light is scattered or reflected from a surface. Achromatic light has brightness and no color, it is a shade of gray so it is described by a single value its intensity. A shading model uses two types of light sources

1) point light sources 2) ambient light. Incident light interacts with the surface in three different ways: Some is absorbed by the surface and is converted to heat. Some is reflected from the surface Some is transmitted into the interior of the object If all incident light is absorbed the object appears black and is known as a black body. If all of the incident light is transmitted the object is visible only through the effects of reflection. Some amount of the reflected light travels in the right direction to reach the eye causing the object to be seen. The amount of light that reaches the eye depends on the orientation of the surface, light and the observer. There are two different types of reflection of incident light

Diffuse scattering occurs when some of the incident light slightly penetrates the surface and is re-radiated uniformly in all directions. Specular reflections are more mirrorlike and highly directional. Incident light is directly reflected from its outer surface. This makes the surface looks shinny. In the simplest model this makes the material look like plastic reflected light has the same color as the incident light. GEOMETRIC INGREDIENTS FOR FINDING REFLECTED LIGHT The normal vector , m , to the surface at P. 2.The vector v from P to the viewers eye.

3.The vector s from P to the light source.


SPECULAR REFLECTION Real objects do not scatter light uniformly in all directions So a specular component is added to the shading model. Specular reflection causes highlights which can add reality to a picture when objects are shinny. The behavior of specular light can be explained with Phong model. PHONG MODEL It is easy to apply and the highlights generated by the phong model given an plasticlike appearance , so the phong model is good when the object is made of shinny plastic or glass. The Phong model is less successful with objects that have a shinny metallic surface. Light from a source impinges on a surface and is reflected in different directions

The amount of light reflected is greatest in the direction of perfect mirror reflection , r, where the angle of incidence equals the angle of reflection. This is the direction in which all light would travel if the surface were a perfect mirror.

How to combine Light Contributions The total amount of light I that reaches the eye from point P: I = ambient + diffuse + specular I= Ia a + Id d lambert + Isp s phongf I depends on various source intensities and reflection coefficients and the relative positions of the point P, the eye and the point light source.

Shading and the Graphics Pipeline

glBegin(GL_POLYGON);for( int i=0 ;i< 3; i++){ glNormal3f(m[i].x, m[i].y, m[i].z); glEnd(); Clipping a polygon against the view volume glVertex3f(v[i].x, v[i].y, v[i].z); }

To Use Light Sources in OpenGL GLfloat myLightPosition[]={3.0 , 6.0,5.0,1.0 }; glLightfv(GL_LIGHT0, GL-POSITION, myLightPosition); glEnable(GL_LIGHTING); glEnable(GL_LIGHT0); //enable lighting in general //enable source GL_LIGHT0 Create a Light Source we can define upto eight sources, which are referred through names GL_LIGHT0, GL_LIGHT1 Each source has properties and must be enabled. Each property has a default value. For example, to create a source located at (3,6,5) in the world coordinates

You might also like