encapsulate in classes
This commit is contained in:
parent
b315325f61
commit
6b1a8ede56
8 changed files with 137 additions and 91 deletions
|
|
@ -1,4 +1,3 @@
|
|||
|
||||
project('test', 'cpp')
|
||||
|
||||
deps = []
|
||||
|
|
|
|||
93
src/main.cpp
93
src/main.cpp
|
|
@ -1,41 +1,8 @@
|
|||
#include <GL/glew.h>
|
||||
#define GLFW_INCLUDE_NONE
|
||||
#include <GLFW/glfw3.h>
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <stddef.h>
|
||||
#include <stdio.h>
|
||||
#include <fmt/core.h>
|
||||
#include <linmath.h>
|
||||
|
||||
typedef struct Vertex {
|
||||
vec2 pos;
|
||||
vec3 col;
|
||||
} Vertex;
|
||||
|
||||
static const Vertex vertices[3] = {{{-0.6f, -0.4f}, {1.f, 0.f, 0.f}},
|
||||
{{0.6f, -0.4f}, {0.f, 1.f, 0.f}},
|
||||
{{0.f, 0.6f}, {0.f, 0.f, 1.f}}};
|
||||
|
||||
static const char *vertex_shader_text =
|
||||
"#version 330\n"
|
||||
"uniform mat4 MVP;\n"
|
||||
"in vec3 vCol;\n"
|
||||
"in vec2 vPos;\n"
|
||||
"out vec3 color;\n"
|
||||
"void main()\n"
|
||||
"{\n"
|
||||
" gl_Position = MVP * vec4(vPos, 0.0, 1.0);\n"
|
||||
" color = vCol;\n"
|
||||
"}\n";
|
||||
|
||||
static const char *fragment_shader_text = "#version 330\n"
|
||||
"in vec3 color;\n"
|
||||
"out vec4 fragment;\n"
|
||||
"void main()\n"
|
||||
"{\n"
|
||||
" fragment = vec4(color, 1.0);\n"
|
||||
"}\n";
|
||||
#include "program.hpp"
|
||||
|
||||
void error_callback(int error_code, const char *description) {
|
||||
fmt::print("[ERROR - {}] {}\n", error_code, description);
|
||||
|
|
@ -49,7 +16,7 @@ void key_callback(GLFWwindow *window, int key, int scancode, int action,
|
|||
fmt::print("Key pressed {}\n", key_name);
|
||||
}
|
||||
|
||||
int main(int argc, char *argv[]) {
|
||||
int main() {
|
||||
|
||||
glfwSetErrorCallback(error_callback);
|
||||
|
||||
|
|
@ -86,60 +53,10 @@ int main(int argc, char *argv[]) {
|
|||
return -1;
|
||||
}
|
||||
|
||||
GLuint vertex_buffer;
|
||||
glGenBuffers(1, &vertex_buffer);
|
||||
glBindBuffer(GL_ARRAY_BUFFER, vertex_buffer);
|
||||
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
|
||||
ShaderSystem shaderSystem = ShaderSystem();
|
||||
Program program = Program(window, shaderSystem);
|
||||
|
||||
const GLuint vertex_shader = glCreateShader(GL_VERTEX_SHADER);
|
||||
glShaderSource(vertex_shader, 1, &vertex_shader_text, NULL);
|
||||
glCompileShader(vertex_shader);
|
||||
|
||||
const GLuint fragment_shader = glCreateShader(GL_FRAGMENT_SHADER);
|
||||
glShaderSource(fragment_shader, 1, &fragment_shader_text, NULL);
|
||||
glCompileShader(fragment_shader);
|
||||
|
||||
const GLuint program = glCreateProgram();
|
||||
glAttachShader(program, vertex_shader);
|
||||
glAttachShader(program, fragment_shader);
|
||||
glLinkProgram(program);
|
||||
|
||||
const GLint mvp_location = glGetUniformLocation(program, "MVP");
|
||||
const GLint vpos_location = glGetAttribLocation(program, "vPos");
|
||||
const GLint vcol_location = glGetAttribLocation(program, "vCol");
|
||||
|
||||
GLuint vertex_array;
|
||||
glGenVertexArrays(1, &vertex_array);
|
||||
glBindVertexArray(vertex_array);
|
||||
glEnableVertexAttribArray(vpos_location);
|
||||
glVertexAttribPointer(vpos_location, 2, GL_FLOAT, GL_FALSE, sizeof(Vertex),
|
||||
(void *)offsetof(Vertex, pos));
|
||||
glEnableVertexAttribArray(vcol_location);
|
||||
glVertexAttribPointer(vcol_location, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex),
|
||||
(void *)offsetof(Vertex, col));
|
||||
|
||||
while (!glfwWindowShouldClose(window)) {
|
||||
int width, height;
|
||||
glfwGetFramebufferSize(window, &width, &height);
|
||||
const float ratio = width / (float)height;
|
||||
|
||||
glViewport(0, 0, width, height);
|
||||
glClear(GL_COLOR_BUFFER_BIT);
|
||||
|
||||
mat4x4 m, p, mvp;
|
||||
mat4x4_identity(m);
|
||||
mat4x4_rotate_Z(m, m, (float)glfwGetTime());
|
||||
mat4x4_ortho(p, -ratio, ratio, -1.f, 1.f, 1.f, -1.f);
|
||||
mat4x4_mul(mvp, p, m);
|
||||
|
||||
glUseProgram(program);
|
||||
glUniformMatrix4fv(mvp_location, 1, GL_FALSE, (const GLfloat *)&mvp);
|
||||
glBindVertexArray(vertex_array);
|
||||
glDrawArrays(GL_TRIANGLES, 0, 3);
|
||||
|
||||
glfwSwapBuffers(window);
|
||||
glfwPollEvents();
|
||||
}
|
||||
program.Loop();
|
||||
|
||||
glfwDestroyWindow(window);
|
||||
|
||||
|
|
|
|||
|
|
@ -1 +1,2 @@
|
|||
sources = files('main.cpp')
|
||||
sources = files('main.cpp', 'program.cpp', 'program.hpp')
|
||||
subdir('systems')
|
||||
|
|
|
|||
18
src/program.cpp
Normal file
18
src/program.cpp
Normal file
|
|
@ -0,0 +1,18 @@
|
|||
#include "program.hpp"
|
||||
#include <GLFW/glfw3.h>
|
||||
|
||||
Program::Program(GLFWwindow *window, class ShaderSystem shaderSystem) {
|
||||
this->window = window;
|
||||
this->shaderSystem = shaderSystem;
|
||||
}
|
||||
|
||||
int Program::Loop() {
|
||||
while (!glfwWindowShouldClose(window)) {
|
||||
int width, height;
|
||||
glfwGetFramebufferSize(window, &width, &height);
|
||||
shaderSystem.Draw(width, height, (float)glfwGetTime());
|
||||
glfwSwapBuffers(window);
|
||||
glfwPollEvents();
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
12
src/program.hpp
Normal file
12
src/program.hpp
Normal file
|
|
@ -0,0 +1,12 @@
|
|||
#define GLFW_INCLUDE_NONE
|
||||
#include <GLFW/glfw3.h>
|
||||
#include "systems/shader-system.hpp"
|
||||
|
||||
class Program {
|
||||
GLFWwindow *window;
|
||||
ShaderSystem shaderSystem;
|
||||
|
||||
public:
|
||||
Program(GLFWwindow *window, class ShaderSystem shaderSystem);
|
||||
int Loop();
|
||||
};
|
||||
1
src/systems/meson.build
Normal file
1
src/systems/meson.build
Normal file
|
|
@ -0,0 +1 @@
|
|||
sources += files('shader-system.cpp', 'shader-system.hpp')
|
||||
81
src/systems/shader-system.cpp
Normal file
81
src/systems/shader-system.cpp
Normal file
|
|
@ -0,0 +1,81 @@
|
|||
#include "shader-system.hpp"
|
||||
|
||||
typedef struct Vertex {
|
||||
vec2 pos;
|
||||
vec3 col;
|
||||
} Vertex;
|
||||
|
||||
static const Vertex vertices[3] = {{{-0.6f, -0.4f}, {1.f, 0.f, 0.f}},
|
||||
{{0.6f, -0.4f}, {0.f, 1.f, 0.f}},
|
||||
{{0.f, 0.6f}, {0.f, 0.f, 1.f}}};
|
||||
|
||||
static const char *vertex_shader_text =
|
||||
"#version 330\n"
|
||||
"uniform mat4 MVP;\n"
|
||||
"in vec3 vCol;\n"
|
||||
"in vec2 vPos;\n"
|
||||
"out vec3 color;\n"
|
||||
"void main()\n"
|
||||
"{\n"
|
||||
" gl_Position = MVP * vec4(vPos, 0.0, 1.0);\n"
|
||||
" color = vCol;\n"
|
||||
"}\n";
|
||||
|
||||
static const char *fragment_shader_text = "#version 330\n"
|
||||
"in vec3 color;\n"
|
||||
"out vec4 fragment;\n"
|
||||
"void main()\n"
|
||||
"{\n"
|
||||
" fragment = vec4(color, 1.0);\n"
|
||||
"}\n";
|
||||
|
||||
ShaderSystem::ShaderSystem() {
|
||||
glGenBuffers(1, &vertex_buffer);
|
||||
glBindBuffer(GL_ARRAY_BUFFER, vertex_buffer);
|
||||
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
|
||||
|
||||
vertex_shader = glCreateShader(GL_VERTEX_SHADER);
|
||||
glShaderSource(vertex_shader, 1, &vertex_shader_text, NULL);
|
||||
glCompileShader(vertex_shader);
|
||||
|
||||
fragment_shader = glCreateShader(GL_FRAGMENT_SHADER);
|
||||
glShaderSource(fragment_shader, 1, &fragment_shader_text, NULL);
|
||||
glCompileShader(fragment_shader);
|
||||
|
||||
program = glCreateProgram();
|
||||
glAttachShader(program, vertex_shader);
|
||||
glAttachShader(program, fragment_shader);
|
||||
glLinkProgram(program);
|
||||
|
||||
mvp_location = glGetUniformLocation(program, "MVP");
|
||||
vpos_location = glGetAttribLocation(program, "vPos");
|
||||
vcol_location = glGetAttribLocation(program, "vCol");
|
||||
|
||||
glGenVertexArrays(1, &vertex_array);
|
||||
glBindVertexArray(vertex_array);
|
||||
glEnableVertexAttribArray(vpos_location);
|
||||
glVertexAttribPointer(vpos_location, 2, GL_FLOAT, GL_FALSE, sizeof(Vertex),
|
||||
(void *)offsetof(Vertex, pos));
|
||||
glEnableVertexAttribArray(vcol_location);
|
||||
glVertexAttribPointer(vcol_location, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex),
|
||||
(void *)offsetof(Vertex, col));
|
||||
}
|
||||
|
||||
int ShaderSystem::Draw(int width, int height, float time) {
|
||||
const float ratio = width / (float)height;
|
||||
|
||||
glViewport(0, 0, width, height);
|
||||
glClear(GL_COLOR_BUFFER_BIT);
|
||||
|
||||
mat4x4 m, p, mvp;
|
||||
mat4x4_identity(m);
|
||||
mat4x4_rotate_Z(m, m, time);
|
||||
mat4x4_ortho(p, -ratio, ratio, -1.f, 1.f, 1.f, -1.f);
|
||||
mat4x4_mul(mvp, p, m);
|
||||
|
||||
glUseProgram(program);
|
||||
glUniformMatrix4fv(mvp_location, 1, GL_FALSE, (const GLfloat *)&mvp);
|
||||
glBindVertexArray(vertex_array);
|
||||
glDrawArrays(GL_TRIANGLES, 0, 3);
|
||||
return 0;
|
||||
}
|
||||
17
src/systems/shader-system.hpp
Normal file
17
src/systems/shader-system.hpp
Normal file
|
|
@ -0,0 +1,17 @@
|
|||
#include <GL/glew.h>
|
||||
#include <linmath.h>
|
||||
|
||||
class ShaderSystem {
|
||||
GLuint vertex_buffer;
|
||||
GLuint vertex_shader;
|
||||
GLuint fragment_shader;
|
||||
GLuint program;
|
||||
GLint mvp_location;
|
||||
GLint vpos_location;
|
||||
GLint vcol_location;
|
||||
GLuint vertex_array;
|
||||
|
||||
public:
|
||||
ShaderSystem();
|
||||
int Draw(int width, int height, float time);
|
||||
};
|
||||
Loading…
Add table
Reference in a new issue