Purpose of the Sample:
Demonstrates how to make a toon shader using a stencil buffer for the silhouette.
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); private: void SetupGPUProgram(); void SetupModel(); Shader* m_pShaderPass0; Shader* m_pShaderPass1; Shader* m_pShaderPass2; Model* m_pManModel; Model* m_pQuad; GLuint m_ColorTextureId; GLuint m_IntermediateFBOId; GLuint m_TekkaTexture; GLuint m_RampTexture; 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 189 190 191 192 193 194 195 196 197 198 199 200 201 |
#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); glGenRenderbuffers(1, &depthStencilId); glBindRenderbuffer(GL_RENDERBUFFER, depthStencilId); glRenderbufferStorageMultisample(GL_RENDERBUFFER, 4, GL_DEPTH24_STENCIL8, screenWidth, screenHeight); glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_STENCIL_ATTACHMENT, GL_RENDERBUFFER, depthStencilId); 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. glStencilFunc(GL_ALWAYS, 3, 0xFF); glStencilOp(GL_REPLACE, GL_REPLACE, GL_REPLACE); glm::mat4 model = glm::rotate(glm::mat4(1.0f), time * glm::radians(60.0f), glm::vec3(0.0f, 1.0f, 0.0f)); 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; glActiveTexture(GL_TEXTURE0); glBindTexture(GL_TEXTURE_2D, m_TekkaTexture); glActiveTexture(GL_TEXTURE1); glBindTexture(GL_TEXTURE_2D, m_RampTexture); m_pShaderPass0->use(); // This must be done to assign the texture samplers to a texture unit. m_pShaderPass0->setInt("mainTexture", 0); m_pShaderPass0->setInt("rampTexture", 1); 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_pManModel->meshes[0].Draw(*m_pShaderPass0); // Draw Silhouette glActiveTexture(GL_TEXTURE0); glBindTexture(GL_TEXTURE_2D, 0); glActiveTexture(GL_TEXTURE1); glBindTexture(GL_TEXTURE_2D, 0); glCullFace(GL_FRONT); // When we drew tekka in the first pass at the normal size and with his texture, // we filled the stencil buffer with the value 3 anywhere he was drawn. The rest of // the stencil buffer was cleared to 0. // We don't want the Silhouette to be drawn over tekka. We only want the Silhouette to be // drawn around tekka. Remember, everything at this point in the stencil buffer is 0 // except for where tekka was drawn which is 3. Therefore to make the outline, // we will AND 3 with 1 and if it comes back as 1, we will not keep the contents of the buffer. glStencilFunc(GL_NOTEQUAL, 1, 0x01); float silhouetteThickness = 0.02f; glm::vec3 silhouetteColor = glm::vec3(1, 0, 0); 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->setFloat("time", time); m_pShaderPass1->setFloat("silhouetteThickness", silhouetteThickness); m_pShaderPass1->setVector3("silhouetteColor", glm::value_ptr(silhouetteColor)); m_pManModel->meshes[0].Draw(*m_pShaderPass1); glDisable(GL_STENCIL_TEST); // Draw a Floor quad glCullFace(GL_BACK); silhouetteColor = glm::vec3(0.4f, 0.4f, 0.4f); 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)); mvp = projection * view * model; m_pShaderPass2->use(); m_pShaderPass2->setMatrix4x4("modelMatrix", glm::value_ptr(model)); m_pShaderPass2->setMatrix4x4("viewMatrix", glm::value_ptr(view)); m_pShaderPass2->setMatrix4x4("projectionMatrix", glm::value_ptr(projection)); m_pShaderPass2->setMatrix4x4("mvpMatrix", glm::value_ptr(mvp)); m_pShaderPass2->setVector3("eyePos", glm::value_ptr(eyePos)); m_pShaderPass2->setFloat("time", time); m_pShaderPass2->setFloat("silhouetteThickness", silhouetteThickness); m_pShaderPass2->setVector3("silhouetteColor", glm::value_ptr(silhouetteColor)); m_pQuad->meshes[0].Draw(*m_pShaderPass2); // Blit intermediate FBO to backbuffer FBO glDisable(GL_STENCIL_TEST); glBindFramebuffer(GL_FRAMEBUFFER, 0); 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("tekka.obj"); m_pQuad = new Model(); m_pQuad->Load("quad.obj"); int width, height, nrChannels; unsigned char *data = stbi_load("tekka.png", &width, &height, &nrChannels, 0); glGenTextures(1, &m_TekkaTexture); glBindTexture(GL_TEXTURE_2D, m_TekkaTexture); glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, data); glGenerateMipmap(GL_TEXTURE_2D); unsigned char *data2 = stbi_load("ramp3.png", &width, &height, &nrChannels, 0); glGenTextures(1, &m_RampTexture); glBindTexture(GL_TEXTURE_2D, m_RampTexture); glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, data2); glGenerateMipmap(GL_TEXTURE_2D); } void CGame::SetupGPUProgram() { m_pShaderPass0 = new Shader("vertexPass0.glsl", "fragmentPass0.glsl"); m_pShaderPass1 = new Shader("vertexPass1.glsl", "fragmentPass1.glsl"); m_pShaderPass2 = new Shader("vertexPass1.glsl", "fragmentPass2.glsl"); } |
vertexPass0.glsl
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
#version 330 core layout (location = 0) in vec3 position; layout (location = 1) in vec3 normal; layout (location = 2) in vec2 uv; uniform mat4 modelMatrix; uniform mat4 viewMatrix; uniform mat4 projectionMatrix; uniform mat4 mvpMatrix; uniform vec3 eyePos; uniform float time; out vec3 fsNormal; out vec2 fsUV; void main() { vec4 pos = mvpMatrix * vec4(position.xyz, 1); fsNormal = vec3(modelMatrix * viewMatrix * vec4(normal, 0)).xyz; fsUV = uv; 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 |
#version 330 core out vec4 FragColor; in vec3 fsNormal; in vec2 fsUV; uniform vec3 eyePos; uniform sampler2D mainTexture; uniform sampler2D rampTexture; void main() { vec3 lightDir = normalize(vec3(0, 0, 1)); vec3 normal = normalize(fsNormal); float nDotl = clamp(dot(normal, lightDir), 0, 1); float ramp = texture(rampTexture, vec2(nDotl, 0.5)).r; vec3 albedo = texture(mainTexture, fsUV).rgb; vec3 diffuse = albedo * pow(ramp, 1.3); FragColor = vec4(diffuse, 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 |
#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 float time; uniform float silhouetteThickness; uniform vec3 silhouetteColor; out vec3 fsSilhouetteColor; out float fsTime; void main() { vec4 pos = mvpMatrix * vec4(position.xyz + (normal * silhouetteThickness), 1); fsSilhouetteColor = silhouetteColor; fsTime = time; gl_Position = pos; } |
fragmentPass1.glsl
1 2 3 4 5 6 7 8 9 10 11 12 13 |
#version 330 core out vec4 FragColor; in vec3 fsSilhouetteColor; in float fsTime; void main() { float t = sin(fsTime * 5.0) * 0.5 + 0.5; vec3 baseColor = vec3(0.5, 0.1, 0.0); vec3 color = mix(baseColor, fsSilhouetteColor, t); FragColor = vec4(color, 1.0f); } |
fragmentPass2.glsl
1 2 3 4 5 6 7 8 9 10 |
#version 330 core out vec4 FragColor; in vec3 fsSilhouetteColor; in float fsTime; void main() { FragColor = vec4(fsSilhouetteColor, 1.0f); } |