diff options
| author | Matthew Kosarek <mattkae@protonmail.com> | 2021-03-27 20:54:27 -0400 |
|---|---|---|
| committer | Matthew Kosarek <mattkae@protonmail.com> | 2021-03-27 20:54:27 -0400 |
| commit | cc05fdc7396532b329f30decde5583853da92a44 (patch) | |
| tree | fc05dd91ef0245acfea1dcf539d4c968a081b5c5 /frontend/shared_cpp/OrthographicRenderer.cpp | |
| parent | 58488f8eabcc61089e0ae4297f38f10cc28d78c7 (diff) | |
Drawing an ellipse
Diffstat (limited to 'frontend/shared_cpp/OrthographicRenderer.cpp')
| -rw-r--r-- | frontend/shared_cpp/OrthographicRenderer.cpp | 47 |
1 files changed, 46 insertions, 1 deletions
diff --git a/frontend/shared_cpp/OrthographicRenderer.cpp b/frontend/shared_cpp/OrthographicRenderer.cpp index b6bd5b6..1d62c04 100644 --- a/frontend/shared_cpp/OrthographicRenderer.cpp +++ b/frontend/shared_cpp/OrthographicRenderer.cpp @@ -1,4 +1,6 @@ #include "OrthographicRenderer.h" +#include "WebglContext.h" +#include "mathlib.h" const char* orthographicVertex = "attribute vec2 position; \n" @@ -18,7 +20,7 @@ const char* orthographicFragment = " gl_FragColor = VertexColor; \n" "}"; -void OrthographicRenderer::load() { +void OrthographicRenderer::load(WebglContext* context) { printf("Compiling orthographic shader...\n"); shader = loadShader(orthographicVertex, orthographicFragment); @@ -27,6 +29,7 @@ void OrthographicRenderer::load() { 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"); } @@ -47,3 +50,45 @@ void OrthographicRenderer::unload() { glClear(GL_COLOR_BUFFER_BIT); glDeleteProgram(shader); } + + +void OrthographicShape::load(OrthographicVertex* inVertices, uint32 inNumVertices, OrthographicRenderer* renderer) { + vertices = inVertices; + numVertices = inNumVertices; + useShader(renderer->shader); + + glGenVertexArrays(1, &vao); + glBindVertexArray(vao); + + glGenBuffers(1, &vbo); + glBindBuffer(GL_ARRAY_BUFFER, vbo); + glBufferData(GL_ARRAY_BUFFER, inNumVertices * sizeof(OrthographicVertex), &vertices[0], GL_STATIC_DRAW); + + glEnableVertexAttribArray(renderer->attributes.position); + glVertexAttribPointer(renderer->attributes.position, 2, GL_FLOAT, GL_FALSE, sizeof(OrthographicVertex), (GLvoid *)0); + + glEnableVertexAttribArray(renderer->attributes.color); + glVertexAttribPointer(renderer->attributes.color, 4, GL_FLOAT, GL_FALSE, sizeof(OrthographicVertex), (GLvoid *)offsetof(OrthographicVertex, color)); + + glBindBuffer(GL_ARRAY_BUFFER, 0); + glBindVertexArray(0); +} + +void OrthographicShape::render(OrthographicRenderer* renderer) { + setShaderMat4(renderer->uniforms.model, model); + + glBindVertexArray(vao); + glDrawArrays(GL_TRIANGLES, 0, numVertices); + glBindVertexArray(0); +} + +void OrthographicShape::unload() { + glDeleteVertexArrays(1, &vao); + glDeleteBuffers(1, &vbo); + + if (vertices != NULL) { + delete vertices; + } + + vertices = NULL; +}
\ No newline at end of file |
