Graphics Systems (LEEC)

2.      Shading and Textures

2.1.    Shading and Smooth-shading

The aim of this group of exercises it to apply the concepts of Flat Shading, Gouraud Smooth Shading and Texture mapping of planar surfaces and curved surfaces approximated by polygonal meshes.

Fig. 1 – OpenGL scene

 

The 3D scene of fig.1 is composed by 3 axes, light source, 3 “walls”, one prism and a cylinder whose axis are coincident with z-axis and finally, a pyramid without base.

For the following questions only the prism, the cylinder and the xy wall are required.

In the file G2_Shad_Text it is supplied a colection of files that built parto of the 3D scene. Compile and run the code. It will show the axes, light source and xy plane.

Note: It is recommended previous reading of the manual OpenGL Programming Guide, mainly the section Specifying a Shading Model (pag. 109).

1.     Write the function myCylinder(), whose header is given in the supplied program, and use it to define the prim. Its parameters can be identified on the code by the names cyl1_radius, cyl1_height, cyl1_slices and cyl1_smooth. At this stage ignore the parameter smooth.

2.     Add to your function the smooth parameter (Boolean) :

a)     Modify the function in order to draw prisms (polygon mesh) and cylinders (rounded surface by the effect of Gouraud method for smooth shading).

b)     Use this last function to draw the outside cylinder as shown in fig. 1.

c)     Try different values for slices.

d)     Compare the results with the ones achieved with gluCylinder() function.

3.     What is the effect of the smooth shading on each of the 3 objects (2 cylinders and What is the effect of the smooth shading on each of the 3 objects (2 cylinders and xy wall).

4.     Change, in the inicializacao() function, the instruction glShadeModel (GL_SMOOTH) to glShadeModel(GL_FLAT).

a)     What changes do you find in the same objects ?

b)     Depending on the OpenGL implementation, the rectangles may appear like a group of two triangles. Make the relation between Flat and smooth shading with those rectangles/triangles.

2.2.    Texture Mapping

The zip file given before includes a file RGBpixmap.cpp where it is defined a class with the following two methods: readBMPFile() and  setTexture(). These methods are invoked in the function inicializacao() to define the four textures to be used in the following exercise.

readBMPFile(): the file containing the image is read and stored in the array "pixel";

setTexture(): the content of the array pixel is passed to the OpenGL to store the image on its own structure; here it initializes other parameters related with texture mapping.

Starting with the 3D scene resulted form the last exercise (2. Shading e Texturas Shading e Smooth-shading), map the four textures supplied (clamp.bmp, feup.bmp, mandril, tile.bmp) as shown in Fig. 1.

Remove from comments the following instructions on the inicializacao()function:

      pixmap.readBMPFile("<file name>");

      pixmap.setTexture(<n>);

As well as the following instructions on the display() function:

glEnable(GL_TEXTURE_2D);

....

glDisable(GL_TEXTURE_2D);

Replace the set of instructions that define the xy wall by the following set of instructios:

      // draws the xy wall

      glBindTexture(GL_TEXTURE_2D, 1);    // sets texture 1 (feup)

      glBegin(GL_POLYGON);

            glNormal3d(0.0,0.0,1.0);

            glTexCoord2f(0.0,0.0); glVertex3d( 0.0,  0.0,  0.0);

            glTexCoord2f(1.0,0.0); glVertex3d(dimx,  0.0,  0.0);

            glTexCoord2f(1.0,1.0); glVertex3d(dimx, dimy,  0.0);

            glTexCoord2f(0.0,1.0); glVertex3d( 0.0, dimy,  0.0);

      glEnd();

Compile and execute the program. You can see that the texture identified as 1 (feup.bmp) is mapped to the xy wall

1.     You can see that, each polygon vertex is related to one texture vertex. Analyze want happens if the vertices order is kept and the texture vertices are rotated by one position, i.e.:

            glTexCoord2f(0.0,1.0); glVertex3d( 0.0,  0.0,  0.0);

            glTexCoord2f(0.0,0.0); glVertex3d(dimx,  0.0,  0.0);

            glTexCoord2f(1.0,0.0); glVertex3d(dimx, dimy,  0.0);

            glTexCoord2f(1.0,1.0); glVertex3d( 0.0, dimy,  0.0);

2.     Verify that the texture aspect  ratio (length/width) does not match the polygon aspect ratio.

3.     Implement a correction of the aspect ratio by changing the texture coordinates (s,t), so that the texture is clipped correctly.

4.     Draw the wall xz and assign texture 2 to it (tile.bmp), using a 5*5 tiling effect. See the syntax of glTexParameter*()function.

a)     Verify that the aspect ratio of the texture in not correct.

b)     Correct the former problem by repenting the texture in the right proportion.

5.     Draw the floor and map texture 3 (clamp.bmp) in clamping mode; start with:

            (s, t) = (-0.2, -0.2) for vertex x=10.0, y=0.0, z=10.0

6.     Draw the four faces of the pyramid.

a)     The pyramid axe is coincident with y axe. The 5 vertices correspond to points on the 3 coordinate system axes with coordinate 1. Therefore each of the four normals can be defined as

 (±1.0, +1.0, ±1.0)

            Note that it needs to be normalized.

b)     Map texture 4 (mandril.bmp) over the pyramid. The texture centre should be assigned to the higher vertex of the pyramid.

7.     Map the texture feup.bmp over the cylinder.

AAS/JGB/JVV