diff options
author | mattkae <mattkae@protonmail.com> | 2021-09-19 16:32:15 -0400 |
---|---|---|
committer | mattkae <mattkae@protonmail.com> | 2021-09-19 16:32:15 -0400 |
commit | de850676e79da39aec6f1ce0400cfa94cecbd744 (patch) | |
tree | 48927e3cd3a518ddee1874df40d188cbc5112393 /themes/Renderer2d.cpp |
First commit
Diffstat (limited to 'themes/Renderer2d.cpp')
-rw-r--r-- | themes/Renderer2d.cpp | 87 |
1 files changed, 87 insertions, 0 deletions
diff --git a/themes/Renderer2d.cpp b/themes/Renderer2d.cpp new file mode 100644 index 0000000..c51d4a9 --- /dev/null +++ b/themes/Renderer2d.cpp @@ -0,0 +1,87 @@ +#include "Renderer2d.h" +#include "WebglContext.h" +#include "mathlib.h" + +const char* renderer2dVertexShader = +"attribute vec2 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" +" gl_Position = fragmentPosition; \n" +" VertexColor = color; \n" +"}"; + +const char* renderer2dFragmentShader = +"varying lowp vec4 VertexColor; \n" +"void main() { \n" +" gl_FragColor = VertexColor; \n" +"}"; + +void Renderer2d::load(WebglContext* context) { + printf("Compiling orthographic shader...\n"); + shader = loadShader(renderer2dVertexShader, renderer2dFragmentShader); + + useShader(shader); + attributes.position = getShaderAttribute(shader, "position"); + attributes.color = getShaderAttribute(shader, "color"); + uniforms.projection = getShaderUniform(shader, "projection"); + uniforms.model = getShaderUniform(shader, "model"); + projection = Mat4x4().getOrthographicMatrix(0, context->width, 0, context->height); + + printf("Orthographic shader compiled.\n"); +} + +void Renderer2d::render() { + glEnable(GL_DEPTH_TEST); + glDepthFunc(GL_LEQUAL); + glDepthMask(GL_TRUE); + glClearColor(0.f, 0.f, 0.f, 1.f); + glClearDepth(1.0f); + glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT); + useShader(shader); + setShaderMat4(uniforms.projection, projection); +} + +void Renderer2d::unload() { + glClearColor(0.f, 0.f, 0.f, 0.f); + glClear(GL_COLOR_BUFFER_BIT); + glDeleteProgram(shader); +} + + +void Renderer2dShape::load(Renderer2dVertex* inVertices, uint32 inNumVertices, Renderer2d* renderer) { + numVertices = inNumVertices; + useShader(renderer->shader); + + glGenVertexArrays(1, &vao); + glBindVertexArray(vao); + + glGenBuffers(1, &vbo); + glBindBuffer(GL_ARRAY_BUFFER, vbo); + glBufferData(GL_ARRAY_BUFFER, inNumVertices * sizeof(Renderer2dVertex), &inVertices[0], GL_STATIC_DRAW); + + glEnableVertexAttribArray(renderer->attributes.position); + glVertexAttribPointer(renderer->attributes.position, 2, GL_FLOAT, GL_FALSE, sizeof(Renderer2dVertex), (GLvoid *)0); + + glEnableVertexAttribArray(renderer->attributes.color); + glVertexAttribPointer(renderer->attributes.color, 4, GL_FLOAT, GL_FALSE, sizeof(Renderer2dVertex), (GLvoid *)offsetof(Renderer2dVertex, color)); + + glBindBuffer(GL_ARRAY_BUFFER, 0); + glBindVertexArray(0); +} + +void Renderer2dShape::render(Renderer2d* renderer, GLenum drawType) { + setShaderMat4(renderer->uniforms.model, model); + + glBindVertexArray(vao); + glDrawArrays(drawType, 0, numVertices); + glBindVertexArray(0); +} + +void Renderer2dShape::unload() { + glDeleteVertexArrays(1, &vao); + glDeleteBuffers(1, &vbo); +} |