parent
43bfba0d71
commit
2c7acb5efa
@ -0,0 +1,8 @@ |
||||
#version 460 core |
||||
out vec4 FragColor; |
||||
uniform vec4 color; |
||||
|
||||
void main() |
||||
{ |
||||
FragColor = vec4(1.0, 0.0, 0.0, 1.0); |
||||
} |
||||
@ -0,0 +1,9 @@ |
||||
#version 460 core |
||||
layout (location = 0) in vec2 aPos; // the position variable has attribute position 0 |
||||
|
||||
uniform mat4 projection; |
||||
|
||||
void main() |
||||
{ |
||||
gl_Position = projection * vec4(aPos, 0.0, 1.0); // see how we directly give a vec3 to vec4's constructor |
||||
} |
||||
@ -0,0 +1,43 @@ |
||||
#include "rect.hpp" |
||||
#include <glm/glm.hpp> |
||||
#include <glm/gtc/matrix_transform.hpp> |
||||
#include <glm/gtc/type_ptr.hpp> |
||||
#include <glad/gl.h> |
||||
#include <GLFW/glfw3.h> |
||||
#include "config.hpp" |
||||
|
||||
void Rect::init() { |
||||
// glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
|
||||
|
||||
shader.use(); |
||||
|
||||
glm::mat4 projection = glm::ortho(0.0f, static_cast<float>(WINDOW_WIDTH), 0.0f, static_cast<float>(WINDOW_HEIGHT)); |
||||
shader.setMat4("projection", projection); |
||||
shader.setVec4("color", color); |
||||
|
||||
glGenVertexArrays(1, &rect_vao); |
||||
glGenBuffers(1, &rect_vbo); |
||||
|
||||
glBindVertexArray(rect_vao); |
||||
glBindBuffer(GL_ARRAY_BUFFER, rect_vbo); |
||||
glBufferData(GL_ARRAY_BUFFER, sizeof(float) * 6 * 2, NULL, GL_DYNAMIC_DRAW); |
||||
|
||||
// position attrib location=0
|
||||
glEnableVertexAttribArray(0); |
||||
glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 2 * sizeof(float), (void*)0); |
||||
glBindVertexArray(0); |
||||
} |
||||
|
||||
void Rect::render() { |
||||
shader.use(); |
||||
glBindVertexArray(rect_vao); |
||||
|
||||
glBindBuffer(GL_ARRAY_BUFFER, rect_vbo); |
||||
glBufferSubData(GL_ARRAY_BUFFER, 0, sizeof(vertices), vertices); |
||||
|
||||
glBindBuffer(GL_ARRAY_BUFFER, 0); |
||||
// render quad
|
||||
glDrawArrays(GL_TRIANGLES, 0, 6); |
||||
|
||||
glBindVertexArray(0); |
||||
} |
||||
@ -0,0 +1,28 @@ |
||||
#pragma once |
||||
#include "shader.hpp" |
||||
#include "config.hpp" |
||||
|
||||
struct Rect { |
||||
float x = 0.0f; |
||||
float y = 0.0f; |
||||
float w = 0.0f; |
||||
float h = 0.0f; |
||||
glm::vec4 color{1.0f, 1.0f, 1.0f, 1.0f}; |
||||
|
||||
float vertices[6][2] = { |
||||
{ x, WINDOW_HEIGHT - y + h}, |
||||
{ x, WINDOW_HEIGHT - y}, |
||||
{ x + w, WINDOW_HEIGHT - y}, |
||||
{ x, WINDOW_HEIGHT - y + h}, |
||||
{ x + w, WINDOW_HEIGHT - y}, |
||||
{ x + w, WINDOW_HEIGHT - y + h} |
||||
}; |
||||
|
||||
unsigned int rect_vao = UINT_MAX; |
||||
unsigned int rect_vbo = UINT_MAX; |
||||
|
||||
Shader shader{{"shaders/rect.vert.glsl", "shaders/rect.frag.glsl", ""}}; |
||||
|
||||
void init(); |
||||
void render(); |
||||
}; |
||||
Loading…
Reference in new issue