COMPUTER GRAPHICS(CS675)ASSIGNMENT - 3 : FINAL REPORT
Design a stage and texture map it with a wooden texture.And light up the stage with two directional lights & one movable spotlight.
(1). Modelling & Texture maping the stage:- Let (w,h) be the width & height of stage.We divide the area of stage in 100*100 similar rectangles, and then divide each rectangle into two triangles.Then we map complete stage with corresponding texture by maping each and every triangle with corresponding part in the texture image as :-
1.1) Steps in texture mapping :-
Create a texture object and specify a texture for that object :- Create a texture object(say teexturexName) and bind it with a particular texture.
unsigned char* tex_image= read_PPM(filename, &width, &height); //read the texture image
glGenTextures(1, &texName); //create a texture object to store texture.
glBindTexture(GL_TEXTURE_2D, texName); //bind the type of texture with texture object
gluBuild2DMipmaps( GL_TEXTURE_2D, 1, width,height, GL_RGB, GL_UNSIGNED_BYTE, tex_image ); //texture data is copied to texture objectEnable Texture Mapping :-
glEnable(GL_TEXTURE_2D);
Draw the scene :- To map the stage we have to map each vertex of constitute triangle by the corresponding texture point in texture image.This can be done as:
glBegin(GL_TRIANGLES);
glTexCoord2f(i/100.0, j/100.0);
glVertex3f(point[i][j].px, point[i][j].py, point[i][j].pz );
glTexCoord2f(i/100.0, (j+1)/100.0);
glVertex3f(point[i][j+1].px, point[i][j+1].py, point[i][j+1].pz );
glTexCoord2f((i+1)/100.0, (j)/100.0);
glVertex3f(point[i+1][j].px, point[i+1][j].py, point[i+1][j].pz );
glTexCoord2f((i+1)/100.0, (j)/100.0);
glVertex3f(point[i+1][j].px, point[i+1][j].py, point[i+1][j].pz );
glTexCoord2f(i/100.0, (j+1)/100.0);
glVertex3f(point[i][j+1].px, point[i][j+1].py, point[i][j+1].pz );
glTexCoord2f((i+1)/100.0, (j+1)/100.0);
glVertex3f(point[i+1][j+1].px, point[i+1][j+1].py, point[i+1][j+1].pz );
glEnd();Now,repeat this procedure for each constitute rectangle of stage grid.
(2). Lighting :-
2.1) Lighting the stage :-
Create, Position, and Enable Light Source -
// Create Light
glLightfv(GL_LIGHT0, GL_AMBIENT, greenish);
glLightfv(GL_LIGHT0, GL_POSITION, position1);
glEnable(GL_LIGHT0);Similarly we can create more light sources.
//Enable lighting
glEnable(GL_LIGHTING);
2.2) Spotlight :-We create a spot light with specified position & spot direction as:
float spot_direction[] = { -1 + result * ligh_rot, -1.0, -1 + ligh_rot * result1 }; //for defining the direction of spotlight
glLightf(GL_LIGHT1,GL_SPOT_CUTOFF,15.0);
glLightfv(GL_LIGHT1, GL_SPOT_DIRECTION, spot_direction);
glLightf(GL_LIGHT1,GL_SPOT_EXPONENT,2.0);
glLightfv(GL_LIGHT1, GL_POSITION, position);
glEnable(GL_LIGHT1);
(3)OUTPUT:-
PART - 2 :
Design a unicycle and render it on stage made in part-1 .
(1). Modelling The Unicycle:- For making a unicycle we use Hierarchical Modelling.We use a matrix stack for model the unicycle.
1. ``Push'' the new transformation onto the matrix stack
2. ``Pop'' it on return.
Output:
PART - 3:
Animate the hierarchical model of the Unicycle created in the previous part by simple keyframing.
Implementation :
(1). Store the KeyFrame : Keyframing is an animation technique. A key frame in animation is a drawing that defines the starting and ending points of any smooth transition over a time period. They are called "frames". We can generate inbetween frames by linear interpolation between starting frame and end frame. In our assignment there are seven degree of freedoms of our unicyle model which we have to store as a single keyframe as:
std::ofstream out("frame.txt", std::ios::out ); //open a file to store keyframes
out<<x_pos<<" "<<y_pos<<" "<<z_pos<<" "<<x_rot<<" "<<y_rot<<" "<<z_rot<<" "<<paddle<<std::endl; //write defined DOF's of unicycle to file
These seven parameters can be stored to the file by pressing the key 'k' or 'K' in our assignment.
(2). Playback the keyframes :
2.1) In 1st step we have to read the keyframes in an array from the file, in which kerframes were stored in last step.
2.2) Choose 45 as no of frames we have to generated between two successive keyframe.Now we can calculate the difference between different DOF's of two successive keyframe(say D).
2.3) Start with the initial keyframe and generate next frame by linear interpolation.To generate next frame by linear interpolation we have to add (D/45) in previous frame's value of corresponding DOF.
OUTPUT :