From 14b50a5bbf39cd54e02f44b08dd3d95e97c6c91e Mon Sep 17 00:00:00 2001 From: sigonasr2 Date: Mon, 30 Oct 2023 00:28:06 -0500 Subject: [PATCH] Implement Phong Light Shading + Optimizations --- Lighting/fragShader.glsl | 36 ++++++++++++- Lighting/main.cpp | 108 ++++++++++++++++++++++++++++++++------- Lighting/utils.h | 30 +++++++++++ Lighting/vertShader.glsl | 24 +++++++++ LoadingModels/main.cpp | 14 ++--- 5 files changed, 186 insertions(+), 26 deletions(-) diff --git a/Lighting/fragShader.glsl b/Lighting/fragShader.glsl index 87df112..a3e2dfc 100644 --- a/Lighting/fragShader.glsl +++ b/Lighting/fragShader.glsl @@ -1,8 +1,42 @@ #version 430 layout (binding=0) uniform sampler2D samp; in vec2 uv; +in vec3 varyingNormal; +in vec3 varyingLightDir; +in vec3 varyingVertPos; +in vec3 varyingHalfVector; out vec4 color; + +struct PositionalLight +{ vec4 ambient; + vec4 diffuse; + vec4 specular; + vec3 position; +}; +struct Material +{ vec4 ambient; + vec4 diffuse; + vec4 specular; + float shininess; +}; +uniform vec4 globalAmbient; +uniform PositionalLight light; +uniform Material material; + void main(void) { - color=texture(samp,uv); + vec3 Normal=normalize(varyingNormal); + vec3 Light=normalize(varyingLightDir); + + vec3 ViewVec=normalize(-varyingVertPos); + vec3 H=normalize(varyingHalfVector); + + float cosTheta=dot(Light,Normal); + float cosPhi=dot(Normal,H); + + vec3 ambient=((globalAmbient * material.ambient) + (light.ambient * material.ambient)).xyz; + vec3 diffuse=light.diffuse.xyz * material.diffuse.xyz * max(cosTheta, 0.0); + vec3 specular=material.specular.xyz * light.specular.xyz * pow(max(cosPhi, 0.0f), material.shininess*3.0); + + color=vec4(ambient+diffuse+specular,material.diffuse.w); } \ No newline at end of file diff --git a/Lighting/main.cpp b/Lighting/main.cpp index 011b5e1..38ec787 100644 --- a/Lighting/main.cpp +++ b/Lighting/main.cpp @@ -20,16 +20,36 @@ GLuint vao[numVAOs]; GLuint vbo[numVBOs]; glm::vec3 camera; -GLuint mvLoc,projLoc; +GLuint globalAmbLoc,ambLoc,diffLoc,specLoc,lPosLoc,mAmbLoc,mDiffLoc,mSpecLoc,mShinLoc,mvLoc,projLoc,nLoc; int width, height; float aspect; -glm::mat4 mMat, pMat, vMat, mvMat; +glm::mat4 mMat, pMat, vMat, mvMat,invTrMat; +glm::vec3 currentLightPos,lightPosV; +std::arraylightPos; +glm::vec3 initialLightLoc={0.0f,2.0f,4.0f}; + +float globalAmbient[4] = { 0.7f, 0.7f, 0.7f, 1.0f }; +float lightAmbient[4] = { 0.0f, 0.0f, 0.0f, 1.0f }; +float lightDiffuse[4] = { 1.0f, 1.0f, 1.0f, 1.0f }; +float lightSpecular[4] = { 1.0f, 1.0f, 1.0f, 1.0f }; + +float* matAmb = utils::silverAmbient(); +float* matDif = utils::silverDiffuse(); +float* matSpe = utils::silverSpecular(); +float matShi = utils::silverShininess(); + +float* matAmb2 = utils::jadeAmbient(); +float* matDif2 = utils::jadeDiffuse(); +float* matSpe2 = utils::jadeSpecular(); +float matShi2 = utils::jadeShininess(); + + std::stacktransforms; GLuint pyrTex; Sphere sphere(48); -Torus torus(1.9,0.4,48); +Torus torus(1.0,0.5,48); ImportedModel shuttle("shuttle.obj"); void setupVertices(void) { @@ -117,13 +137,13 @@ void setupVertices(void) { nvalues.push_back(normals[i].z); } - glBindBuffer(GL_ARRAY_BUFFER, vbo[7]); + glBindBuffer(GL_ARRAY_BUFFER, vbo[8]); glBufferData(GL_ARRAY_BUFFER, pvalues.size()*sizeof(float), pvalues.data(), GL_STATIC_DRAW); - glBindBuffer(GL_ARRAY_BUFFER, vbo[8]); + glBindBuffer(GL_ARRAY_BUFFER, vbo[9]); glBufferData(GL_ARRAY_BUFFER, tvalues.size()*sizeof(float), tvalues.data(),GL_STATIC_DRAW); - glBindBuffer(GL_ARRAY_BUFFER, vbo[9]); + glBindBuffer(GL_ARRAY_BUFFER, vbo[10]); glBufferData(GL_ARRAY_BUFFER, nvalues.size()*sizeof(float), nvalues.data(), GL_STATIC_DRAW); } @@ -139,7 +159,7 @@ void setupTextures(){ void init(GLFWwindow* window) { renderingProgram=utils::createShaderProgram("vertShader.glsl","fragShader.glsl"); - camera={0.0f,5.0f,-10.0f}; + camera={0.0f,0.0f,-10.0f}; setupVertices(); setupTextures(); } @@ -147,6 +167,7 @@ void init(GLFWwindow* window) { void DrawSphere(){ glUniformMatrix4fv(mvLoc,1,GL_FALSE,glm::value_ptr(mvMat)); glUniformMatrix4fv(projLoc,1,GL_FALSE,glm::value_ptr(pMat)); + glUniformMatrix4fv(nLoc, 1, GL_FALSE, glm::value_ptr(invTrMat)); glBindBuffer(GL_ARRAY_BUFFER,vbo[0]); glVertexAttribPointer(0,3,GL_FLOAT,GL_FALSE,0,0); glBindBuffer(GL_ARRAY_BUFFER,vbo[1]); @@ -161,6 +182,7 @@ void DrawSphere(){ void DrawTorus(){ glUniformMatrix4fv(mvLoc,1,GL_FALSE,glm::value_ptr(mvMat)); glUniformMatrix4fv(projLoc,1,GL_FALSE,glm::value_ptr(pMat)); + glUniformMatrix4fv(nLoc, 1, GL_FALSE, glm::value_ptr(invTrMat)); glBindBuffer(GL_ARRAY_BUFFER,vbo[4]); glVertexAttribPointer(0,3,GL_FLOAT,GL_FALSE,0,0); glBindBuffer(GL_ARRAY_BUFFER,vbo[5]); @@ -175,16 +197,48 @@ void DrawTorus(){ void DrawShuttle(){ glUniformMatrix4fv(mvLoc,1,GL_FALSE,glm::value_ptr(mvMat)); glUniformMatrix4fv(projLoc,1,GL_FALSE,glm::value_ptr(pMat)); - glBindBuffer(GL_ARRAY_BUFFER,vbo[7]); - glVertexAttribPointer(0,3,GL_FLOAT,GL_FALSE,0,0); + glUniformMatrix4fv(nLoc, 1, GL_FALSE, glm::value_ptr(invTrMat)); glBindBuffer(GL_ARRAY_BUFFER,vbo[8]); - glVertexAttribPointer(1,2,GL_FLOAT,GL_FALSE,0,0); + glVertexAttribPointer(0,3,GL_FLOAT,GL_FALSE,0,0); glBindBuffer(GL_ARRAY_BUFFER,vbo[9]); + glVertexAttribPointer(1,2,GL_FLOAT,GL_FALSE,0,0); + glBindBuffer(GL_ARRAY_BUFFER,vbo[10]); glVertexAttribPointer(2,3,GL_FLOAT,GL_FALSE,0,0); glDrawArrays(GL_TRIANGLES,0,shuttle.getNumVertices()); } +void installLights(glm::mat4 vMat){ + currentLightPos = glm::vec3(initialLightLoc.x, initialLightLoc.y, initialLightLoc.z); + // convert light's position to view space, and save it in a float array + lightPosV = glm::vec3(vMat * glm::vec4(currentLightPos, 1.0)); + lightPos[0] = lightPosV.x; + lightPos[1] = lightPosV.y; + lightPos[2] = lightPosV.z; + + // get the locations of the light and material fields in the shader + globalAmbLoc = glGetUniformLocation(renderingProgram, "globalAmbient"); + ambLoc = glGetUniformLocation(renderingProgram, "light.ambient"); + diffLoc = glGetUniformLocation(renderingProgram, "light.diffuse"); + specLoc = glGetUniformLocation(renderingProgram, "light.specular"); + lPosLoc = glGetUniformLocation(renderingProgram, "light.position"); + mAmbLoc = glGetUniformLocation(renderingProgram, "material.ambient"); + mDiffLoc = glGetUniformLocation(renderingProgram, "material.diffuse"); + mSpecLoc = glGetUniformLocation(renderingProgram, "material.specular"); + mShinLoc = glGetUniformLocation(renderingProgram, "material.shininess"); + + // set the uniform light and material values in the shader + glProgramUniform4fv(renderingProgram, globalAmbLoc, 1, globalAmbient); + glProgramUniform4fv(renderingProgram, ambLoc, 1, lightAmbient); + glProgramUniform4fv(renderingProgram, diffLoc, 1, lightDiffuse); + glProgramUniform4fv(renderingProgram, specLoc, 1, lightSpecular); + glProgramUniform3fv(renderingProgram, lPosLoc, 1, lightPos.data()); + glProgramUniform4fv(renderingProgram, mAmbLoc, 1, matAmb); + glProgramUniform4fv(renderingProgram, mDiffLoc, 1, matDif); + glProgramUniform4fv(renderingProgram, mSpecLoc, 1, matSpe); + glProgramUniform1f(renderingProgram, mShinLoc, matShi); +} + double lastTime=0; void display(GLFWwindow* window, double currentTime) { @@ -198,35 +252,53 @@ void display(GLFWwindow* window, double currentTime) { glUseProgram(renderingProgram); glEnable(GL_DEPTH_TEST); glDepthFunc(GL_LEQUAL); + glEnable(GL_CULL_FACE); + glFrontFace(GL_CCW); + glEnable(GL_BLEND); + glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); glEnableVertexAttribArray(0); glEnableVertexAttribArray(1); glEnableVertexAttribArray(2); mvLoc=glGetUniformLocation(renderingProgram,"mv_matrix"); projLoc=glGetUniformLocation(renderingProgram,"proj_matrix"); + nLoc = glGetUniformLocation(renderingProgram, "norm_matrix"); glfwGetFramebufferSize(window,&width,&height); aspect=(float)width/(float)height; pMat=glm::perspective(utils::degToRad(60),aspect,0.1f,1000.f); vMat=glm::translate(glm::mat4(1.0f),glm::vec3{camera.x,camera.y,camera.z}); - vMat=glm::lookAt(glm::vec3{camera.x,camera.y,camera.z},{0,-3,0},{0,1,0}); + + installLights(vMat); + + //vMat=glm::lookAt(glm::vec3{camera.x,camera.y,camera.z},{0,-3,0},{0,1,0}); - mMat=glm::translate(glm::mat4(1.0f),{0.f,0.f,0.f}); - mMat=glm::rotate(mMat,float(currentTime/4),{0.f,1.f,-0.2f}); - mMat=glm::scale(mMat,{8,8,8}); + mMat=glm::translate(glm::mat4(1.0f),{0.f,-2.f,0.f}); + mMat=glm::rotate(mMat,float(currentTime/4),{0.f,1.f,0.2f}); + mMat=glm::scale(mMat,{7,7,7}); mvMat=vMat*mMat; + invTrMat=glm::transpose(glm::inverse(mvMat)); + //DrawSphere(); DrawShuttle(); + - mMat=glm::translate(glm::mat4(1.0f),{0.f,-3.f,0.f}); - mMat=glm::rotate(mMat,float(currentTime/4),{0.f,1.f,0.4f}); - mMat=glm::scale(mMat,{3,0.1,3}); + glProgramUniform4fv(renderingProgram, mAmbLoc, 1, matAmb2); + glProgramUniform4fv(renderingProgram, mDiffLoc, 1, matDif2); + glProgramUniform4fv(renderingProgram, mSpecLoc, 1, matSpe2); + glProgramUniform1f(renderingProgram, mShinLoc, matShi2); + + mMat=glm::translate(glm::mat4(1.0f),{4.f,2.f,0.f}); + mMat=glm::rotate(mMat,float(currentTime/4),{1.f,0.f,0.f}); + mMat=glm::scale(mMat,{3,3,3}); mvMat=vMat*mMat; + + invTrMat=glm::transpose(glm::inverse(mvMat)); - //DrawTorus(); + DrawTorus(); } int main(void) { diff --git a/Lighting/utils.h b/Lighting/utils.h index e4c13b2..7418df6 100644 --- a/Lighting/utils.h +++ b/Lighting/utils.h @@ -91,4 +91,34 @@ public: } return textureID; } + + // GOLD material - ambient, diffuse, specular, and shininess + inline static float* goldAmbient() { static float a[4] = { 0.2473f, 0.1995f, 0.0745f, 1 }; return (float*)a; } + inline static float* goldDiffuse() { static float a[4] = { 0.7516f, 0.6065f, 0.2265f, 1 }; return (float*)a; } + inline static float* goldSpecular() { static float a[4] = { 0.6283f, 0.5558f, 0.3661f, 1 }; return (float*)a; } + inline static float goldShininess() { return 51.2f; } + + // SILVER material - ambient, diffuse, specular, and shininess + inline static float* silverAmbient() { static float a[4] = { 0.1923f, 0.1923f, 0.1923f, 1 }; return (float*)a; } + inline static float* silverDiffuse() { static float a[4] = { 0.5075f, 0.5075f, 0.5075f, 1 }; return (float*)a; } + inline static float* silverSpecular() { static float a[4] = { 0.5083f, 0.5083f, 0.5083f, 1 }; return (float*)a; } + inline static float silverShininess() { return 51.2f; } + + // BRONZE material - ambient, diffuse, specular, and shininess + inline static float* bronzeAmbient() { static float a[4] = { 0.2125f, 0.1275f, 0.0540f, 1 }; return (float*)a; } + inline static float* bronzeDiffuse() { static float a[4] = { 0.7140f, 0.4284f, 0.1814f, 1 }; return (float*)a; } + inline static float* bronzeSpecular() { static float a[4] = { 0.3935f, 0.2719f, 0.1667f, 1 }; return (float*)a; } + inline static float bronzeShininess() { return 25.6f; } + + // JADE material - ambient, diffuse, specular, and shininess + inline static float* jadeAmbient() { static float a[4] = { 0.135f, 0.2225f, 0.1575f, 0.95f }; return (float*)a; } + inline static float* jadeDiffuse() { static float a[4] = { 0.540f, 0.89f, 0.63f, 0.95f }; return (float*)a; } + inline static float* jadeSpecular() { static float a[4] = { 0.3162f, 0.3162f, 0.3162f, 0.95f }; return (float*)a; } + inline static float jadeShininess() { return 12.8f; } + + // PEARL material - ambient, diffuse, specular, and shininess + inline static float* pearlAmbient() { static float a[4] = { 0.25f, 0.2073f, 0.2073f, 0.922f }; return (float*)a; } + inline static float* pearlDiffuse() { static float a[4] = { 1.f, 0.829f, 0.829f, 0.922f }; return (float*)a; } + inline static float* pearlSpecular() { static float a[4] = { 0.2966f, 0.2966f, 0.2966f, 0.922f }; return (float*)a; } + inline static float pearlShininess() { return 11.264f; } }; \ No newline at end of file diff --git a/Lighting/vertShader.glsl b/Lighting/vertShader.glsl index 95c56bd..ccbf751 100644 --- a/Lighting/vertShader.glsl +++ b/Lighting/vertShader.glsl @@ -4,15 +4,39 @@ layout (location=1) in vec2 texCoords; layout (location=2) in vec3 normals; uniform mat4 mv_matrix; uniform mat4 proj_matrix; +uniform mat4 norm_matrix; out vec2 uv; +out vec3 varyingNormal; // eye-space vertex normal +out vec3 varyingLightDir; // vector pointing to the light +out vec3 varyingVertPos; // vertex position in eye space +out vec3 varyingHalfVector; mat4 buildTranslate(float x, float y, float z); mat4 buildRotateX(float rad); mat4 buildRotateY(float rad); mat4 buildRotateZ(float rad); +struct PositionalLight +{ vec4 ambient; + vec4 diffuse; + vec4 specular; + vec3 position; +}; +struct Material +{ vec4 ambient; + vec4 diffuse; + vec4 specular; + float shininess; +}; +uniform PositionalLight light; +uniform Material material; + void main(void) { + varyingVertPos=(mv_matrix * vec4(position,1.0)).xyz; + varyingLightDir = light.position - varyingVertPos; + varyingNormal=(norm_matrix * vec4(normals,1.0)).xyz; + varyingHalfVector=(varyingLightDir-varyingVertPos).xyz; gl_Position=proj_matrix*mv_matrix*vec4(position,1.0); uv=texCoords; } diff --git a/LoadingModels/main.cpp b/LoadingModels/main.cpp index 011b5e1..7efcd7d 100644 --- a/LoadingModels/main.cpp +++ b/LoadingModels/main.cpp @@ -117,13 +117,13 @@ void setupVertices(void) { nvalues.push_back(normals[i].z); } - glBindBuffer(GL_ARRAY_BUFFER, vbo[7]); + glBindBuffer(GL_ARRAY_BUFFER, vbo[8]); glBufferData(GL_ARRAY_BUFFER, pvalues.size()*sizeof(float), pvalues.data(), GL_STATIC_DRAW); - glBindBuffer(GL_ARRAY_BUFFER, vbo[8]); + glBindBuffer(GL_ARRAY_BUFFER, vbo[9]); glBufferData(GL_ARRAY_BUFFER, tvalues.size()*sizeof(float), tvalues.data(),GL_STATIC_DRAW); - glBindBuffer(GL_ARRAY_BUFFER, vbo[9]); + glBindBuffer(GL_ARRAY_BUFFER, vbo[10]); glBufferData(GL_ARRAY_BUFFER, nvalues.size()*sizeof(float), nvalues.data(), GL_STATIC_DRAW); } @@ -175,11 +175,11 @@ void DrawTorus(){ void DrawShuttle(){ glUniformMatrix4fv(mvLoc,1,GL_FALSE,glm::value_ptr(mvMat)); glUniformMatrix4fv(projLoc,1,GL_FALSE,glm::value_ptr(pMat)); - glBindBuffer(GL_ARRAY_BUFFER,vbo[7]); - glVertexAttribPointer(0,3,GL_FLOAT,GL_FALSE,0,0); glBindBuffer(GL_ARRAY_BUFFER,vbo[8]); - glVertexAttribPointer(1,2,GL_FLOAT,GL_FALSE,0,0); + glVertexAttribPointer(0,3,GL_FLOAT,GL_FALSE,0,0); glBindBuffer(GL_ARRAY_BUFFER,vbo[9]); + glVertexAttribPointer(1,2,GL_FLOAT,GL_FALSE,0,0); + glBindBuffer(GL_ARRAY_BUFFER,vbo[10]); glVertexAttribPointer(2,3,GL_FLOAT,GL_FALSE,0,0); glDrawArrays(GL_TRIANGLES,0,shuttle.getNumVertices()); @@ -226,7 +226,7 @@ void display(GLFWwindow* window, double currentTime) { mMat=glm::scale(mMat,{3,0.1,3}); mvMat=vMat*mMat; - //DrawTorus(); + DrawTorus(); } int main(void) {