Purpose of the Sample:
Unity’s legacy renderer supported multiple light sources in the forward renderer by issuing a draw call for a single object for each light that affected the object. The first draw call will be the base draw call and all other draw calls are additively blended on top of the base draw call.
CGame.h
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
#ifndef __CGAME__ #define __CGAME__ #include <glad/glad.h> #include "shader_s.h" class Model; class Mesh; class CGame { public: CGame() {}; void Initialize(unsigned int screenWidth, unsigned int screenHeight); void Shutdown(); void Render(); void OnResize(int screenWidth, int screenHeight); void ReloadShaders(); private: void SetupGPUProgram(); void SetupModel(); Shader* m_pShaderPass0; Shader* m_pShaderPass1; Model* m_pManModel; Model* m_pQuad; GLuint m_ColorTextureId; GLuint m_DepthStencilTextureId; GLuint m_IntermediateFBOId; unsigned int m_Width; unsigned int m_Height; }; #endif |
CGame.cpp
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 |
#include "CGame.h" #define STB_IMAGE_IMPLEMENTATION #include "stb_image/stb_image.h" #include "Model.h" #include "Mesh.h" #include "glm/mat4x4.hpp" #include "glm/vec3.hpp" #include "glm/glm.hpp" #include "glm/gtc/matrix_transform.hpp" #include "glm/gtc/type_ptr.hpp" #include void CGame::Initialize(unsigned int screenWidth, unsigned int screenHeight) { m_Width = screenWidth; m_Height = screenHeight; SetupGPUProgram(); SetupModel(); glEnable(GL_DEPTH_TEST); glDepthFunc(GL_LEQUAL); glEnable(GL_MULTISAMPLE); glEnable(GL_STENCIL_TEST); GLuint depthStencilId; glGenFramebuffers(1, &m_IntermediateFBOId); glBindFramebuffer(GL_FRAMEBUFFER, m_IntermediateFBOId); glGenTextures(1, &m_ColorTextureId); glBindTexture(GL_TEXTURE_2D_MULTISAMPLE, m_ColorTextureId); glTexImage2DMultisample(GL_TEXTURE_2D_MULTISAMPLE, 4, GL_RGB8, m_Width, m_Height, GL_TRUE); glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D_MULTISAMPLE, m_ColorTextureId, 0); glGenTextures(1, &m_DepthStencilTextureId); glBindTexture(GL_TEXTURE_2D_MULTISAMPLE, m_DepthStencilTextureId); glTexImage2DMultisample(GL_TEXTURE_2D_MULTISAMPLE, 4, GL_DEPTH24_STENCIL8, screenWidth, screenHeight, GL_TRUE); glFramebufferTexture2D(GL_FRAMEBUFFER, GL_DEPTH_STENCIL_ATTACHMENT, GL_TEXTURE_2D_MULTISAMPLE, m_DepthStencilTextureId, 0); if (glCheckFramebufferStatus(GL_FRAMEBUFFER) == GL_FRAMEBUFFER_COMPLETE) { // Great! } glBindFramebuffer(GL_FRAMEBUFFER, 0); } void CGame::Shutdown() { } void CGame::OnResize(int width, int height) { m_Width = width; m_Height = height; glViewport(0, 0, m_Width, m_Height); glScissor(0, 0, m_Width, m_Height); } void CGame::Render() { static auto startTime = std::chrono::high_resolution_clock::now(); auto currentTime = std::chrono::high_resolution_clock::now(); float time = std::chrono::duration<float, std::chrono::seconds::period>(currentTime - startTime).count(); glEnable(GL_MULTISAMPLE); glBindFramebuffer(GL_FRAMEBUFFER, m_IntermediateFBOId); glEnable(GL_STENCIL_TEST); glClearColor(0.2f, 0.2f, 0.2f, 1.0f); glClearDepth(1.0f); glClearStencil(0); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT); // Draw the Filled Man glEnable(GL_CULL_FACE); glFrontFace(GL_CCW); glCullFace(GL_BACK); // Fill the stencil buffer with 1 anywhere our tekka model is drawn. float scaleFactor = 0.4f; glm::mat4 model = glm::scale(glm::mat4(1.0f), glm::vec3(scaleFactor, scaleFactor, scaleFactor)); model = glm::rotate(model, glm::radians(45.0f * time), glm::vec3(0, 1, 0)); glm::mat4 view; glm::vec3 eyePos = glm::vec3(0.0f, -1.0f, -2.5f); view = glm::translate(glm::mat4(1.0f), eyePos); glm::mat4 projection; projection = glm::perspective(glm::radians(60.0f), m_Width / static_cast(m_Height), 0.03f, 100.0f); glm::mat4 mvp = projection * view * model; glm::vec3 diffuseColor = glm::vec3(1,0,0); m_pShaderPass0->use(); m_pShaderPass0->setMatrix4x4("modelMatrix", glm::value_ptr(model)); m_pShaderPass0->setMatrix4x4("viewMatrix", glm::value_ptr(view)); m_pShaderPass0->setMatrix4x4("projectionMatrix", glm::value_ptr(projection)); m_pShaderPass0->setMatrix4x4("mvpMatrix", glm::value_ptr(mvp)); m_pShaderPass0->setVector3("eyePos", glm::value_ptr(eyePos)); m_pShaderPass0->setVector3("diffuseColor", glm::value_ptr(diffuseColor)); m_pShaderPass0->setFloat("time", time); m_pManModel->meshes[0].Draw(*m_pShaderPass0); // Additive Point Light Pass glm::vec3 pointLightColor = glm::vec3(0, 1, 0); glm::vec3 pointLightPositionInViewSpace = glm::vec3(2, 0, 5); glEnable(GL_BLEND); glBlendEquation(GL_FUNC_ADD); glBlendFunc(GL_ONE, GL_ONE); m_pShaderPass1->use(); m_pShaderPass1->setMatrix4x4("modelMatrix", glm::value_ptr(model)); m_pShaderPass1->setMatrix4x4("viewMatrix", glm::value_ptr(view)); m_pShaderPass1->setMatrix4x4("projectionMatrix", glm::value_ptr(projection)); m_pShaderPass1->setMatrix4x4("mvpMatrix", glm::value_ptr(mvp)); m_pShaderPass1->setVector3("eyePos", glm::value_ptr(eyePos)); m_pShaderPass1->setVector3("pointLightPosition", glm::value_ptr(pointLightPositionInViewSpace)); m_pShaderPass1->setVector3("pointLightColor", glm::value_ptr(pointLightColor)); m_pShaderPass1->setFloat("time", time); m_pManModel->meshes[0].Draw(*m_pShaderPass1); glDisable(GL_BLEND); // Additive Point Light Pass pointLightColor = glm::vec3(0, 0, 1); pointLightPositionInViewSpace = glm::vec3(0, 2, 2); glEnable(GL_BLEND); glBlendEquation(GL_FUNC_ADD); glBlendFunc(GL_ONE, GL_ONE); m_pShaderPass1->use(); m_pShaderPass1->setMatrix4x4("modelMatrix", glm::value_ptr(model)); m_pShaderPass1->setMatrix4x4("viewMatrix", glm::value_ptr(view)); m_pShaderPass1->setMatrix4x4("projectionMatrix", glm::value_ptr(projection)); m_pShaderPass1->setMatrix4x4("mvpMatrix", glm::value_ptr(mvp)); m_pShaderPass1->setVector3("eyePos", glm::value_ptr(eyePos)); m_pShaderPass1->setVector3("pointLightPosition", glm::value_ptr(pointLightPositionInViewSpace)); m_pShaderPass1->setVector3("pointLightColor", glm::value_ptr(pointLightColor)); m_pShaderPass1->setFloat("time", time); m_pManModel->meshes[0].Draw(*m_pShaderPass1); glDisable(GL_BLEND); // Draw a Floor quad diffuseColor = glm::vec3(1, 1, 1); model = glm::translate(glm::mat4(1.0f), glm::vec3(0.0f, -0.2f, 0.0f)); model = glm::scale(model, glm::vec3(5.0f, 5.0f, 5.0f)); model = glm::rotate(model, glm::radians(45.0f * time), glm::vec3(0, 1, 0)); mvp = projection * view * model; m_pShaderPass0->use(); m_pShaderPass0->setMatrix4x4("modelMatrix", glm::value_ptr(model)); m_pShaderPass0->setMatrix4x4("viewMatrix", glm::value_ptr(view)); m_pShaderPass0->setMatrix4x4("projectionMatrix", glm::value_ptr(projection)); m_pShaderPass0->setMatrix4x4("mvpMatrix", glm::value_ptr(mvp)); m_pShaderPass0->setVector3("eyePos", glm::value_ptr(eyePos)); m_pShaderPass0->setFloat("time", time); m_pShaderPass0->setVector3("diffuseColor", glm::value_ptr(diffuseColor)); m_pQuad->meshes[0].Draw(*m_pShaderPass0); // Blit intermediate FBO to backbuffer FBO glBindFramebuffer(GL_READ_FRAMEBUFFER, m_IntermediateFBOId); glBindFramebuffer(GL_DRAW_FRAMEBUFFER, 0); glBlitFramebuffer(0, 0, m_Width, m_Height, 0, 0, m_Width, m_Height, GL_COLOR_BUFFER_BIT, GL_NEAREST); } void CGame::SetupModel() { m_pManModel = new Model(); m_pManModel->Load("man.obj"); m_pQuad = new Model(); m_pQuad->Load("quad.obj"); } void CGame::SetupGPUProgram() { m_pShaderPass0 = new Shader("vertexPass0.glsl", "fragmentPass0.glsl"); m_pShaderPass1 = new Shader("vertexPass1.glsl", "fragmentPass1.glsl"); } void CGame::ReloadShaders() { OutputDebugStringA("Reloading Shaders!\n"); SetupGPUProgram(); } |
vertexPass0.glsl
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
#version 330 core layout (location = 0) in vec3 position; layout (location = 1) in vec3 normal; uniform mat4 modelMatrix; uniform mat4 viewMatrix; uniform mat4 projectionMatrix; uniform mat4 mvpMatrix; uniform vec3 eyePos; uniform float time; uniform vec3 diffuseColor; uniform vec3 specularColor; out vec3 fsNormal; out vec3 fsFragPos; void main() { vec4 pos = mvpMatrix * vec4(position.xyz, 1); fsNormal = vec3(modelMatrix * vec4(normal, 0)).xyz; fsFragPos = vec3(modelMatrix * vec4(position.xyz, 0)).xyz; gl_Position = pos; } |
fragmentPass0.glsl
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
#version 330 core out vec4 FragColor; in vec3 fsNormal; in vec3 fsFragPos; uniform vec3 eyePos; uniform vec3 diffuseColor; uniform vec3 specularColor; vec3 calculateDirectionalLighting(vec3 lightDir, vec3 normal, vec3 viewDir) { float di = clamp(dot(normal, lightDir), 0, 1); vec3 half = normalize( lightDir + viewDir ); float si = clamp(dot(normal, half), 0, 1); float ki = 0.01; vec3 ambient = ki * vec3(1); vec3 diffuse = di * diffuseColor; vec3 specular = pow(si, 8.0) * vec3(1); vec3 finalColor = ambient + diffuse + specular; return finalColor; } void main() { vec3 viewDir = normalize(eyePos - fsFragPos); vec3 lightDir = vec3(0,1,1); vec3 normal = normalize(fsNormal); vec3 finalColor = calculateDirectionalLighting(lightDir, normal, viewDir); FragColor = vec4(finalColor, 1.0f); } |
vertexPass1.glsl
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
#version 330 core layout (location = 0) in vec3 position; layout (location = 1) in vec3 normal; uniform mat4 modelMatrix; uniform mat4 viewMatrix; uniform mat4 projectionMatrix; uniform mat4 mvpMatrix; uniform vec3 eyePos; uniform float time; uniform vec3 pointLightPosition; uniform vec3 pointLightColor; out vec3 fsNormal; out vec3 fsFragPos; void main() { vec4 pos = mvpMatrix * vec4(position.xyz, 1); fsNormal = vec3(modelMatrix * vec4(normal, 0)).xyz; fsFragPos = vec3(modelMatrix * vec4(position.xyz, 0)).xyz; gl_Position = pos; } |
fragmentPass1.glsl
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
#version 330 core out vec4 FragColor; in vec3 fsNormal; in vec3 fsFragPos; uniform vec3 eyePos; uniform vec3 pointLightPosition; uniform vec3 pointLightColor; vec3 calculatePointLighting(vec3 pointLightPosition, vec3 objPosition, vec3 normal, vec3 viewDir) { float distFromObjectToLight = length(pointLightPosition - objPosition); vec3 lightDir = normalize(pointLightPosition - objPosition); float di = clamp(dot(normal, lightDir), 0, 1); vec3 half = normalize( lightDir + viewDir ); float si = clamp(dot(normal, half), 0, 1); vec3 diffuse = di * pointLightColor; vec3 specular = pow(si, 8.0) * vec3(1); // Point Light Formula //http://ogldev.atspace.co.uk/www/tutorial20/tutorial20.html float exp = 0.01; float falloff = distFromObjectToLight + (exp * distFromObjectToLight * distFromObjectToLight); vec3 finalColor = (diffuse + specular) / falloff; return finalColor; } void main() { vec3 viewDir = normalize(eyePos - fsFragPos); vec3 lightDir = vec3(0,1,1); vec3 normal = normalize(fsNormal); vec3 finalColor = calculatePointLighting(pointLightPosition, fsFragPos, normal, viewDir); FragColor = vec4(finalColor, 1.0f); } |