Repeat Exercise 7.1, but applied to Program 7.2. This should only require substituting the shaders for Phong

Question:

Repeat Exercise 7.1, but applied to Program 7.2. This should only require substituting the shaders for Phong shading into your solution to Exercise 7.1. The improvement from Gouraud to Phong shading should be even more apparent here, when the light is being moved around.

Exercise 7.1

Modify Program 7.1 so that the light can be positioned by moving the mouse. After doing this, move the mouse around and note the movement of the specular highlight and the appearance of the Gouraud shading artifacts. You may find it convenient to render a point (or small object) at the location of the light source.

Program 7.1
public class Code extends JFrame implements GLEventListener { private GLCanvas myCanvas; // declarations forprivate Vector3f currentLightPos = new Vector3f(); private float[] lightPos = new float[3]; // current light// This function is unchanged from the previous chapter. // The following portion is repeated for clarity,// set up lights currentLightPos.set(initialLightLoc); install Lights();// build the inverse-transpose of the MV matrix, for transforming normal vectors mMat.invert(inv TrMat);gl.glEnable(GL_CULL_FACE); gl.glFrontFace(GL_CCW); gl.glEnable(GL_DEPTH_TEST); gl.glDepth Func(GL_LEQUAL);// get the locations of the light and material fields in the shader globalAmbLoc = gl.glGetUniform Location// set the uniform light and material values in the shader gl.glProgramUniform4fv(renderingVertex Shader #version 430 layout (location=0) in vec3 vertPos; layout (location=1) in vec3 vertNormal; outuniform vec4 globalAmbient; uniform PositionalLight light; uniform Material material; uniform mat4 m_matrix;// ambient, diffuse, and specular contributions vec3 ambient = ((globalAmbient * material.ambient) +} 0.0); vec3 diffuse = light.diffuse.xyz* material.diffuse.xyz*max(dot(N,L), vec3 specular =Fragment Shader #version 430 in vec4 varying Color; out vec4 fragColor; // uniforms match those in the vertexuniform vec4 globalAmbient; uniform PositionalLight light; uniform Material material; uniform mat4 m_matrix;

Program 7.2Vertex Shader #version 430 layout (location=0) in vec3 vertPos; layout (location=1) in vec3 vertNormal; outFragment Shader #version 430 in vec3 varying Normal; in vec3 varyingLightDir; in vec3 varyingVertPos; outvoid main(void) { // normalize the light, normal, and view vectors: vec3 L = normalize(varying LightDir);

Fantastic news! We've Found the answer you've been seeking!

Step by Step Answer:

Related Book For  book-img-for-question

Computer Graphics Programming In OpenGL With JAVA

ISBN: 9781683922193

2nd Edition

Authors: V. Scott Gordon PhD, John L. Clevenger PhD

Question Posted: