IntMu.Lab5 0. Download all the files available from http://www.dee.isep.ipp.pt/~jml/intmu/lab5: wget http://www.dee.isep.ipp.pt/~jml/intmu/lab5/makefile make getall Analyze the program windmill.c. Compile it and run the resulting application (windmill). make./windmill An empty window is shown. 0.1. Base The Display() function is automaticly called whenever the window requires a redraw. Inside this function, add an horizontal square (in the xy plan) with sides of 6 units. glcolor3f(0., 0.7, 0.); glbegin(gl_quads); glvertex3f(-3., -3., 0.); glvertex3f(-3., 3., 0.); glvertex3f( 3., 3., 0.); glvertex3f( 3., -3., 0.); 1. Windmill The windmill() function is allways called from the Display() function. Here, a graphical representation of a traditional windmill will be produced. 1.1. Cone Inside the windmill() function, apply the tool glutsolidcone to create a cone based on the xy plan, with red color, a base radius of 1 unit and height of 1 unit. glcolor3f(0.9, 0., 0.); glutsolidcone(1., 1., 32, 1); Change the cone s position by applying a vertical translation of 3 units. gltranslatef(0., 0., 3.); Also apply a scale to increase its base radius in 20%. glscalef(1.2, 1.2, 1.);
1.2. Cylinder Add a vertical cylinder to reproduce the windmill body. glcolor3f(0.9, 0.9, 0.); glpushmatrix(); glscalef(1., 1., 3.); glutsolidcylinder(1., 1., 32, 1); glpopmatrix(); 1.3. Arm Add a triangular polygon to reproduce one of the windmill arms. glcolor3f(1.,1.,1.); glpushmatrix(); gltranslatef(0., -1.25, 2.4); glnormal3f(0., -1., 0.); glbegin(gl_triangles); glvertex3f( 0., 0., 0.); glvertex3f( -1.8, -0.1, 1.3); glvertex3f( -1.8, 0., 0.); glpopmatrix(); 1.4. Arms Reuse the last arm specification to create the other arms, applying successive rotations for( i=0 ; i<4 ; i++ ) glbegin(gl_triangles); glvertex3f( 0., 0., 0.); glvertex3f( 1.8, 0., 1.5); glvertex3f( 1.8, 0., 0.); glrotatef(90., 0., 1., 0.); 2. Animation Apply a rotation transformation around the y axis with constant rotation speed to the arms group. glrotatef(0.05*timenow, 0., 1., 0.); Verify that the arms position is changed each time the window is updated.
In order to force a constant window update, define a new callback function: void Redisplay(void) glutpostredisplay(); Register this new function as the callback tio be used in the idle state. glutidlefunc(redisplay); 3. Illumination All objects should be reproduced using the flat shaded color specified width the glcolor3f() function. In order to achieve a more realistic viewing, the illumination calculus can be implemented through the graphics pipeline. Enable the illumination calculus. glenable (GL_LIGHTING); Verify that all the objects lost their colors. 3.1 Light source Create a point light source (GL_LIGHT0) in the Init() function: float light_ambient[] = 0.2, 0.2, 0.2, 1.0 ; float light_diffuse[] = 0.35, 0.35, 0.35, 1.0 ; float light_specular[] = 1.0, 1.0, 1.0, 1.0 ; float light_position[] = 30.0, -10.0, 30.0, 1.0 ; gllightfv (GL_LIGHT0, GL_AMBIENT, light_ambient); gllightfv (GL_LIGHT0, GL_DIFFUSE, light_diffuse); gllightfv (GL_LIGHT0, GL_SPECULAR, light_specular); gllightfv (GL_LIGHT0, GL_POSITION, light_position); Turn the light source on. glenable (GL_LIGHT0); Verify that the illumination calculus is now working. 3.2 Materiais Prepare a function to define the optical properties of the used materials. void SetMaterial(float r, float g, float b) float mat_dif[] = r, g, b, 1.0 ;
float mat_amb[] = r, g, b, 1.0 ; float mat_spe[] = 0.2, 0.2, 0.2, 1.0 ; float mat_shi = 10.; glmaterialfv(gl_front, GL_AMBIENT, mat_amb); glmaterialfv(gl_front, GL_DIFFUSE, mat_dif); glmaterialfv(gl_front, GL_SPECULAR, mat_spe); glmaterialf( GL_FRONT, GL_SHININESS, mat_shi); Apply this function to define the properties of all used materials, replacing al the references to the function glcolor3f(): 4. Textures SetMaterial(0.9, 0., 0.); Use the SOIL (Simple OpenGL Image Loader) library to load an image that will be mapped over the base polygon. #include <SOIL/SOIL.h> unsigned char* image = SOIL_load_image("grass.jpg", &width, &height, 0, SOIL_LOAD_RGB); glbindtexture(gl_texture_2d, 0); gltexparameteri(gl_texture_2d, GL_TEXTURE_MAG_FILTER, GL_NEAREST); gltexparameteri(gl_texture_2d, GL_TEXTURE_MIN_FILTER, GL_NEAREST); glteximage2d(gl_texture_2d, 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, image); SOIL_free_image_data(image); Inside the Display() function, enable the use of this texture to colorize the base polygon. glenable(gl_texture_2d); gltexenvf(gl_texture_env, GL_TEXTURE_ENV_MODE, GL_DECAL); glbindtexture(gl_texture_2d, 0); For each vertex, specify the texture coordinates, pointing to a corner of the image. glbegin(gl_quads); gltexcoord2f(0.0, 0.0); glvertex3f(-6., -6., 0.); gltexcoord2f(0.0, 1.0); glvertex3f(-6., 6., 0.); gltexcoord2f(1.0, 1.0); glvertex3f( 6., 6., 0.); gltexcoord2f(1.0, 0.0); glvertex3f( 6., -6., 0.); After the redraw of the base polygon, turn off the texture mapping, so that the other objects are not affected. gldisable(gl_texture_2d);
5. Submission To submit your work, copy the files windmill.c and windmill to the directory intmu/lab5/ under your WWW area in the ave server. make submit Verify that both files became available from the URL: www.dee.isep.ipp.pt/~student_nr/intmu/lab5/ 6. Bibliographic info The OpenGL Programming Guide: The Official Guide to Learning OpenGL, Versions 3.0 and 3.1 (7th Edition), Dave Shreiner, ISBN 978-0-321-55262-4 OpenGLBook.com, http://openglbook.com/ 7. Requirements SOIL (Sample OpenGL Image Loader) Source: http://www.lonesock.net/soil.html SOIL installing: yum install SOIL SOIL-devel or: make instsoil 8. History V1, 2000, jml V2, 2004, jml V3, 2013, jml V3.1, 2014, jml V4.1, 2016, jml V4.1e, 2017, jml