Skip to content

Recitation 06 OpenGL Shader Programming

jijup edited this page Oct 31, 2023 · 1 revision

Objectives

In this lab, you will learn how to render an object and texture it using OpenGL and shaders. Specific objectives include rendering a triangle and a quad, texture the quad and do a simple animation using scaling and rotation. This exercise will make you familiar with the data layout specification for the buffers (VBO), interpolation of the vertex colors, shader based 2D transformations, uniforms, and texturing.

Starter Code

The skeleton code for this week's recitation is uploaded to 'GraphicsLab/shaders'. The 'shaders' directory has three subdirectories as follows:

  • Helpers: Consists of helper codes. Please read and understand the 'ShaderHelper.h' file. The file consists of various functions for loading and compiling vertex and fragment shaders.
  • Triangle: Consists of 'Triangle.h' and the associated shaders to render the triangle. The file 'Triangle.h' has some @TODOs for you. You should fill in the init() and draw() functions of Triangle.h. The vertex shader, i.e., 'shaders/Triangle/vshader.glsl' has some @TODOs for you.
  • Quad: Consists of Quad.h' and the associated shaders to render and texture a quad (GL_TRIANGLE_STRIP). The vertex shader, i.e., 'shaders/Quad/vshader.glsl' has some @TODOs for you.

The 'main.cpp' contains the GLFW commands for window creation, OpenGL configuration, etc. Please read and understand those commands. More details can be found here. You will be using those commands for most of your OpenGL programs in this course. There are two tasks for you to complete in this recitation. For task 1, all the statements related to the 'Triangle' class in 'main.cpp' should be uncommented. Similarly, for task 2, all the statements related to Quad class in 'main.cpp' should be uncommented.

alt text

Vertex Color Interpolation (Task 1):

The first part of this recitation is to get you familiarized with the basic syntax and functionality of GLSL (OpenGL Shader Language). The code given under 'shaders/Triangle' draws a red triangle as shown in Figure 1(a). If carefully read the program, you can see that it does the following things.

  • Compile the shaders (init() function)
  • Set up the vertex array object (VAO) (init() function)
  • Allocate GPU memory for the vertex buffer (vertex buffer object (VBO)) (init() function)
  • Pass the vertex positions to OpenGL
  • Creates Vertex Attribute to store Vertex Positions(init() function)
  • Draw the triangle (glDrawArrays) (draw() function)

You have to modify the code so that the program draws the triangle with vertex colors interpoltation as shown in Figure 1(b). Specific @TODOs are explained below.

  • TODO 1: Currently the array vertices contains the geometry of the triangle defined using the triangle's vertex cooridnates. You have to pack color information to this array. You can follow the order, V1 coords, V1 color, V2 coords, V2 color,... To specify the color of a vertex, use the RGB color format with values between 0 and 1.

  • TODO 2: Specify the vertex color attribute pointers. Use glGetAttribLocation() to get the location id for the color attribute and enable the vertex attribute array (glEnableVertexAttribArray()).

  • TODO 3: Declare the appropriate shader variable for the color in vshader.glsl. Use appropriate qualifier for the color variable.

  • TODO 4: In the current vertex shader, vertexColor is hard coded to red. Modify the vertexColor (in vshader.glsl) so that it takes the color of each triangle vertex.

Please refer to the slides for the code and the explanation.

Quad Texturing (Task 2):

The second task for you is to learn and understand the commands for texturing objects (a quad in this example). The given code will render a quad as shown in Figure 2. You have to modify the code to create a simple animation as shown in Figure 3. alt text alt text

Specific TODOs follow.

  • TODO1: Allocate memory for storing texture coordinates.
  • TODO2: Create Vertex Attribute pointer to specify vertex texture coordinates.
  • TODO3: Define Scaling and Rotations for the quad in 'vshader.glsl'. Please note that GLS uses column major order to store values in matrix.
  • TODO4: Use the transformation functions to move the quad geometry. For instance you may use the following.
gl_Position = S(.05*time)*R(2*3.14*time)*vec4(vpoint, 1.0); 

to model the flipping airplane animation as shown in Figure 3.

References

  1. https://docs.gl/
  2. https://registry.khronos.org/OpenGL-Refpages/gl4/
  3. GLFW 2.7 Manual
  4. OpenGL Programming Guide - 8th edition
  5. Marschner et al. "Fundamentals of Computer Graphics", CRC Press.