

Purpose of the Sample:
Demonstrates how to texture map a quad. This sample is based on the learnopengl.com tutorial.
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 | #ifndef __CGAME__ #define __CGAME__ #include <glad/glad.h> #include "shader_s.h" class CGame { public:     CGame() {};     void Initialize();     void Shutdown();     void Render();     void OnResize(int width, int height); private:     void SetupVAO();     void SetupGPUProgram();     void SetupTexture();     unsigned int m_VAO;     unsigned int m_Texture;     Shader* m_pShader; }; #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 | #include "CGame.h" #define STB_IMAGE_IMPLEMENTATION #include "stb_image/stb_image.h" void CGame::Initialize() {     SetupGPUProgram();     SetupTexture();     SetupVAO(); } void CGame::Shutdown() { } void CGame::OnResize(int width, int height) {     glViewport(0, 0, width, height); } void CGame::Render() {     glClearColor(0.2f, 0.3f, 0.3f, 1.0f);     glClear(GL_COLOR_BUFFER_BIT);     glBindTexture(GL_TEXTURE_2D, m_Texture);     m_pShader->use();     glBindVertexArray(m_VAO);     glDrawArrays(GL_TRIANGLES, 0, 6); } void CGame::SetupVAO() {     float vertices[] = {         1.0f, -1.0f, 0.0f,      1.0f, 0.0f, 0.0f,       1.0f, 1.0f,         -1.0f,  1.0f, 0.0f,     0.0f, 1.0f, 0.0f,       0.0f, 0.0f,         -1.0f, -1.0f, 0.0f,     0.0f, 0.0f, 1.0f,       0.0f, 1.0f,         1.0f, -1.0f, 0.0f,      1.0f, 0.0f, 0.0f,       1.0f, 1.0f,         1.0f,  1.0f, 0.0f,      0.0f, 0.0f, 1.0f,       1.0f, 0.0f,         -1.0f, 1.0f, 0.0f,      0.0f, 1.0f, 0.0f,       0.0f, 0.0f,     };     unsigned int vbo;     glGenBuffers(1, &vbo);     glGenVertexArrays(1, &m_VAO);     // ..:: Initialization code (done once (unless your object frequently changes)) :: ..     // 1. bind Vertex Array Object     glBindVertexArray(m_VAO);     // 2. copy our vertices array in a buffer for OpenGL to use     glBindBuffer(GL_ARRAY_BUFFER, vbo);     glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);     // 3. then set our vertex attributes pointers     // Position Attribute.     glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 8 * sizeof(float), (void*)0);     glEnableVertexAttribArray(0);     // Color Attribute.     glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 8 * sizeof(float), (void*)(3 * sizeof(float)));     glEnableVertexAttribArray(1);     // UV Attribute.     glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, 8 * sizeof(float), (void*)(6 * sizeof(float)));     glEnableVertexAttribArray(2); } void CGame::SetupGPUProgram() {     m_pShader = new Shader("vertex.glsl", "fragment.glsl"); } void CGame::SetupTexture() {     int width, height, nrChannels;     unsigned char *data = stbi_load("awesomeface.jpg", &width, &height, &nrChannels, 0);     glGenTextures(1, &m_Texture);     glBindTexture(GL_TEXTURE_2D, m_Texture);     glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, data);     glGenerateMipmap(GL_TEXTURE_2D); } | 
vertex.glsl
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 | #version 330 core layout (location = 0) in vec3 aPos; layout(location = 1) in vec3 aColor; layout(location = 2) in vec2 aUV; out vec3 vertexColor; out vec2 vertexUV; void main() {     vertexColor = aColor;     vertexUV = aUV;     gl_Position = vec4(aPos.x, aPos.y, aPos.z, 1.0); } | 
fragment.glsl
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | #version 330 core out vec4 FragColor; in vec3 vertexColor; in vec2 vertexUV; uniform sampler2D myTexture; void main() {     vec2 ndc = vertexUV * 2.0 - 1.0;     //vec3 color = vertexColor * length(ndc);     vec3 color = texture(myTexture, vertexUV).rgb;     FragColor = vec4(color.rgb, 1.0f); } | 
Download Sample App

 
	