CSCE 441 - Computer Graphics Programming Assignment 4 Deadline: Nov. 6th (11:59 pm) 1 Goal The goal of this assignment is to write shading codes in GLSL. 2 Starter Code The starter code can be...

I have uploaded the assignment


CSCE 441 - Computer Graphics Programming Assignment 4 Deadline: Nov. 6th (11:59 pm) 1 Goal The goal of this assignment is to write shading codes in GLSL. 2 Starter Code The starter code can be downloaded from here. 3 Task 1 Download the code and run it. You should be able to see a red bunny as shown in Fig. 1. Make sure you write your name in the appropriate place in the code, so it shows up at the top of the window. Here is a brief explanation of the starter code: • There are three folders in the package. “obj” contains the “bunny.obj” file that has the geometry information (vertex position, normal, etc.) for a bunny object. “shaders” contains the vertex and fragment shader programs. You’ll be mainly modifying these files to implement different shading methods. Finally, “src” contains the source files. Program is a class for loading, compiling, and linking the shader programs as well as sending data to them. Moreover, “tiny obj loader.h” is a simple header file for loading the obj files. You will use the Program class and “tiny obj loader.h” as is, but have to modify the main file. In summary, you’ll be modifying the following functions: – “main.cpp”: You need to modify this function to pass appropriate data to the program shaders and set up the materials and lighting. – “shader.vert”: This is the vertex shader and you’ll be filling it out to implement a specific shader. – “shader.frag”: This is the fragment shader and you’ll be filling it out to implement a specific shader. • Now let’s take a look at “main.cpp” – There are several global variables defined at the top of this file. Specifically, program is responsible for processing the shader programs. posBuff, norBuff, and texBuff store the vertex position, normal, and texture coordinates, respectively. materials and lights are structures for storing the material parameters and lighting information. – The structure of main function in “main.cpp” is similar to the one in all the previous assign- ments. – The Init function ∗ The first few functions are called to initializes the window and events. ∗ LoadModel function reads the obj file and saves the position, normal, and texture coordi- nates of each vertex of the geometry in the posBuff, norBuff, and texBuff vectors. We do not use the texBuff data in this assignment. ∗ The last four lines are responsible for setting up the shader programs. ∗ program.SetShadersFileName sets the address of the vertex and fragment shader files so we can load and compile them next. 1 https://drive.google.com/open?id=18pjAR64_jryrC3o4OQvicK56qcVrXJ9m Figure 1: Running the skeleton code should produce a red bunny as shown here. ∗ program.Init loads, compiles, and links, the shader files, so they are ready to be exe- cuted. ∗ program.SendAttributeData sends the vertex attributes to the GPU. Attributes are defined at each vertex and will be directly passed as the input to the vertex shader program. program.SendAttributeData(posBuff, ‘‘vPositionModel’’) sends the position of each vertex which is stored in posBuff to the vertex program under the name of vPositionModel. If you look at the vertex program (“shader.vert”), you see a vari- able called vPositionModel which contains the position of each vertex. Here, we send the position and normal of the vertices to the vertex program. – The Display function ∗ The first few lines of this function set the projection, view, and model matrices. These are the matrices used to project the vertices from the object space (the space that the positions in posBuff are given in) to the normalize device coordinate. ∗ program.Bind activates the shader programs so they can be used for drawing. Note that you can have an array of program variables each set up with a different vertex and shader files, e.g., program[0] set up with “shader0.vert” and “shader0.frag” and program[1] set up with “shader1.vert” and “shader1.frag”. This set up can be done in the Init func- tion. Then in the Display function, you can just bind the particular shader program that you would like to be used for drawing, e.g., program[1].Bind(). In fact in this as- signment you have to write three different shaders and be able to cycle through them using a key. ∗ program.SendUniformData sends uniform data to the shader programs (both vertex and fragment). These are the variables that are the same for all the vertices. Here, we send the model, view, and projection matrices to the vertex program, so we can use them to transform the vertices to normalize device coordinate in the vertex program. Variables model, view, and projection in “shader.vert” are basically these 4×4 matrices. ∗ glDrawArrays basically performs the graphics pipeline including the shader programs to display the triangles on the screen. ∗ Finally, program.Unbind deactivates the shader. • Now let’s look at the “shader.vert” and “shader.frag” 2 – “shader.vert” ∗ At the top, we define the two attributes vPositionModel and vNormalModel con- taining the position and normal of each vertex, respetively. These two variables are of type vec3 meaning that they have three elements. The data for these two variables are privided through calling program.SendAttributeData in the Init function of “main.cpp”. ∗ Next we define three uniform 4×4 matrices (mat4) to serve as the model, view, and pro- jection matrices. Uniform variables are constant for all the vertices (do not change from one vertex to another). These matrices are passed to the vertex shader through calling program.SendUniformData in the Display function of “main.cpp”. ∗ The next few lines of codes define a structure for holding the information about the light sources. ∗ The line uniform lightStruct lights[NUM LIGHTS] creates an array of the previously defined light structure, called lights. Note that since the light sources are the same for all the vertices, they should be defined as uniform variables. Moreover, please note that currently no value is passed to these variables. You need to define these light sources in the “main.cpp” by indicating the position and color of each light source and then pass them to the vertex program by calling program.SendUniformData with appropriate arguments in the Display function. ∗ Next, we have four uniform variables (three vec3 and one float) which contain infor- mation about the materials. These are the parameters required to calculate the color of each pixel or vertex based on the Phong shading model. Again the value for these variables need to be passed by calling program.SendUniformData with appropriate arguments in the Display function. ∗ In the next line, we define a varying variable of type vec3, called color. Similar to attributes, varying variables are defined per vertex. These are the variables that are defined in the vertex program and are passed in the fragment program. This is in contrast to attributes which are passed from CPU to the vertex program. ∗ The next few lines are the main function of the vertex program. We first multiply the model, view, and projection matrices to transform the vertices stored in vPositionModel to normalized device coordinate. The output of this process is stored in gl Position which is a pre-defined output of the vertex shader. Note that, we add 1 to the end of each vertex to take them to homogeneous coordinate. We then define the color of each vertex to be red (vec3(1.0f, 0.0f, 0.0f)). – “shader.frag” ∗ We first define the varying variable color. This is the variable that is passed from the vertex program to the fragment program. ∗ In the main function, we set gl FragColor which is a pre-defined output of the frag- ment shader to be equal to color. Note that, gl FragColor has four elements corre- sponding to red, gree, blue, and alpha. Alpha defines the transparency of the color. Alpha equal to 1 means the object is opaque which is why we add 1 to the end of the color variable before passing it as gl FragColor. 4 Task 2 In this part, you will be implementing Phong shading model using Gouraud approach. Phong shading model uses the following equation to calculate the color of each point: 3 I = ka + k∑ i=1 Ci [kdmax(0, Li ·N) + ksmax(0, Ri · E)s] . (1) Note that this equation is slightly different from the one in the slides. In the slides, the ambient term is defined as kaA , but here we assume the intensity of the ambient illumination A is equal to (1, 1, 1). Use the following material and light sources to implement the shading. • Material 1 – ka = (0.2, 0.2, 0.2) – kd = (0.8, 0.7, 0.7) – ks = (1.0, 1.0, 1.0) – s = 10.0 • Light – light 1 ∗ position = (0.0, 0.0, 3.0) in world coordinate ∗ color = C1 = (0.5, 0.5, 0.5) – light 2 ∗ position = (0.0, 3.0, 0.0) in world coordinate ∗ color = C2 = (0.2, 0.2, 0.2) You need to first set the material and light information in the structure arrays provided at the top of “main.cpp”. Note that materials is an array of size 3, but for this task you only set the first element, i.e., materials[0]. You need to set the other two materials and be able to cycle through them in the next task. Once you set material and light sources properly, you need to pass them to the vertex shader by calling program.SendUniformData with appropriate arguments in the Display function. Note that, the position and color of the light sources are defined with a structure in the shader program. You won’t be able to pass a structure from CPU to GPU, so you should just pass the individual properties for each light source. For example, you can pass the position of the first light source as: program.SendUniformData(lights[0].position, ‘‘lights[0].position’’) Here, the first argument is the name of the variable on CPU, and the second argument is the name of the variable on GPU inside quotations. The position and color of light sources along with the material properties (ka, kd, ks, s) can then be used to implement Eq. 1 in the vertex program. I in this equation is basically the color of the vertex. So you need to set color (the varying variable in the vertex program) to I . This color is then interpolated in the graphics hardware to obtain the color of each pixel (fragment). The varying variable color in the fragment shader is the interpolated color and thus setting gl FragColor to
Nov 05, 2021
SOLUTION.PDF

Get Answer To This Question

Submit New Assignment

Copy and Paste Your Assignment Here