Purpose of the Sample:
Demonstrate how to create and render a vertex buffer object.
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 |
#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(); unsigned int m_VBO; unsigned int m_VAO; 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 |
#include "CGame.h" void CGame::Initialize() { SetupGPUProgram(); SetupVAO(); } void CGame::Shutdown() { } void CGame::OnResize(int width, int height) { glViewport(0, 0, width, height); } void CGame::Render() { glClearColor(0.2f, 0.2f, 0.2f, 1.0f); glClear(GL_COLOR_BUFFER_BIT); m_pShader->use(); glBindVertexArray(m_VBO); glDrawArrays(GL_TRIANGLES, 0, 3); } void CGame::SetupVAO() { float vertices[] = { -0.5f, -0.5f, 0.0f, 0.5f, -0.5f, 0.0f, 0.0f, 0.5f, 0.0f }; glGenBuffers(1, &m_VBO); glGenVertexArrays(1, &m_VAO); glBindVertexArray(m_VAO); glBindBuffer(GL_ARRAY_BUFFER, m_VBO); glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW); glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(float), (void*)0); glEnableVertexAttribArray(0); } void CGame::SetupGPUProgram() { m_pShader = new Shader("vertex.glsl", "fragment.glsl"); } |
vertex.glsl
1 2 3 4 5 6 7 |
#version 330 core layout (location = 0) in vec3 aPos; void main() { gl_Position = vec4(aPos.x, aPos.y, aPos.z, 1.0); } |
fragment.glsl
1 2 3 4 5 6 7 |
#version 330 core out vec4 FragColor; void main() { FragColor = vec4(1.0f, 0.0f, 0.0f, 1.0f); } |