summaryrefslogtreecommitdiff
path: root/shared_cpp/Renderer2d.cpp
diff options
context:
space:
mode:
authormattkae <mattkae@protonmail.com>2022-01-22 13:57:55 -0500
committermattkae <mattkae@protonmail.com>2022-01-22 13:57:55 -0500
commit3f3d1ed1cba61015aa47a6ad9812ae0d30316cc5 (patch)
treeba6f5d8aa5114de81f331828ef877a6f4ddebfda /shared_cpp/Renderer2d.cpp
parent19defa9be56588803bbae0f38e2f271a91b9d690 (diff)
(mkosarek) Added dots to the spring bodied rectangle
Diffstat (limited to 'shared_cpp/Renderer2d.cpp')
-rw-r--r--shared_cpp/Renderer2d.cpp24
1 files changed, 18 insertions, 6 deletions
diff --git a/shared_cpp/Renderer2d.cpp b/shared_cpp/Renderer2d.cpp
index adef4de..c050172 100644
--- a/shared_cpp/Renderer2d.cpp
+++ b/shared_cpp/Renderer2d.cpp
@@ -3,15 +3,16 @@
#include "mathlib.h"
const char* orthographicVertex =
-"attribute vec2 position; \n"
+"attribute vec4 position; \n"
"attribute vec4 color; \n"
"uniform mat4 projection; \n"
"uniform mat4 model; \n"
"varying lowp vec4 VertexColor; \n"
"void main() { \n"
-" vec4 fragmentPosition = projection * model * vec4(position, 1, 1); \n"
+" vec4 fragmentPosition = projection * model * position; \n"
" gl_Position = fragmentPosition; \n"
" VertexColor = color; \n"
+" gl_PointSize = 2.0; \n"
"}";
const char* orthographicFragment =
@@ -55,6 +56,7 @@ void Renderer2d::unload() {
void Mesh2d::load(Vertex2d* inVertices, uint32 inNumVertices, Renderer2d* renderer, GLenum loadType) {
mode = MeshMode::Vertex;
numVertices = inNumVertices;
+ isDynamic = loadType == GL_DYNAMIC_DRAW;
useShader(renderer->shader);
glGenVertexArrays(1, &vao);
@@ -65,7 +67,7 @@ void Mesh2d::load(Vertex2d* inVertices, uint32 inNumVertices, Renderer2d* render
glBufferData(GL_ARRAY_BUFFER, inNumVertices * sizeof(Vertex2d), &inVertices[0], loadType);
glEnableVertexAttribArray(renderer->attributes.position);
- glVertexAttribPointer(renderer->attributes.position, 2, GL_FLOAT, GL_FALSE, sizeof(Vertex2d), (GLvoid *)0);
+ glVertexAttribPointer(renderer->attributes.position, 4, GL_FLOAT, GL_FALSE, sizeof(Vertex2d), (GLvoid *)0);
glEnableVertexAttribArray(renderer->attributes.color);
glVertexAttribPointer(renderer->attributes.color, 4, GL_FLOAT, GL_FALSE, sizeof(Vertex2d), (GLvoid *)offsetof(Vertex2d, color));
@@ -78,6 +80,7 @@ void Mesh2d::load(Vertex2d* inVertices, uint32 inNumVertices, GLuint* inIndices,
mode = MeshMode::Index;
numVertices = inNumVertices;
numIndices = inNumIndices;
+ isDynamic = loadType == GL_DYNAMIC_DRAW;
useShader(renderer->shader);
glGenVertexArrays(1, &vao);
@@ -88,7 +91,7 @@ void Mesh2d::load(Vertex2d* inVertices, uint32 inNumVertices, GLuint* inIndices,
glBufferData(GL_ARRAY_BUFFER, inNumVertices * sizeof(Vertex2d), &inVertices[0], loadType);
glEnableVertexAttribArray(renderer->attributes.position);
- glVertexAttribPointer(renderer->attributes.position, 2, GL_FLOAT, GL_FALSE, sizeof(Vertex2d), (GLvoid *)0);
+ glVertexAttribPointer(renderer->attributes.position, 4, GL_FLOAT, GL_FALSE, sizeof(Vertex2d), (GLvoid *)0);
glEnableVertexAttribArray(renderer->attributes.color);
glVertexAttribPointer(renderer->attributes.color, 4, GL_FLOAT, GL_FALSE, sizeof(Vertex2d), (GLvoid *)offsetof(Vertex2d, color));
@@ -116,8 +119,7 @@ void Mesh2d::render(Renderer2d* renderer, GLenum drawType) {
}
glBindVertexArray(0);
-}
-
+}
void Mesh2d::unload() {
glDeleteVertexArrays(1, &vao);
@@ -128,3 +130,13 @@ void Mesh2d::unload() {
glDeleteBuffers(1, &ebo);
}
}
+
+void Mesh2d::updateVertices(Vertex2d* vertices) {
+ if (!isDynamic) {
+ printf("Cannot update vertices on a non-dynamic mesh.\n");
+ return;
+ }
+
+ glBindBuffer(GL_ARRAY_BUFFER, vbo);
+ glBufferSubData(GL_ARRAY_BUFFER, 0, numVertices * sizeof(Vertex2d), &vertices[0]);
+}