Purpose of the Sample:
Demonstrates how to bind and sample from multiple textures. The scene is rendered using MSAA and resolved prior to rendering to the backbuffer. This sample uses assimp to load models.
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 |
#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; Model* m_pManModel; GLuint m_ColorTextureId; GLuint m_IntermediateFBOId; GLuint m_TekkaTexture; GLuint m_RampTexture; Mesh* m_pFullscreenQuad; 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 |
#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); 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); glClearColor(0.2f, 0.2f, 0.2f, 1.0f); glClearDepth(1.0f); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // Draw the Filled Man 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; // This must be done to assign the texture samplers to a texture unit. m_pShaderPass0->setInt("mainTexture", 0); m_pShaderPass0->setInt("rampTexture", 1); glActiveTexture(GL_TEXTURE0); glBindTexture(GL_TEXTURE_2D, m_TekkaTexture); glActiveTexture(GL_TEXTURE1); glBindTexture(GL_TEXTURE_2D, m_RampTexture); 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_pManModel->meshes[0].Draw(*m_pShaderPass0); // Bind the backbuffer glBindFramebuffer(GL_FRAMEBUFFER, 0); // Resolve the MSAA 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("tekka.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"); } |
vertex.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; } |
fragment.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); } |