diff options
Diffstat (limited to 'shared_cpp')
-rw-r--r-- | shared_cpp/Renderer2d.cpp (renamed from shared_cpp/OrthographicRenderer.cpp) | 22 | ||||
-rw-r--r-- | shared_cpp/Renderer2d.h (renamed from shared_cpp/OrthographicRenderer.h) | 12 | ||||
-rw-r--r-- | shared_cpp/WebglHelper.h | 4 | ||||
-rw-r--r-- | shared_cpp/mathlib.cpp | 11 | ||||
-rw-r--r-- | shared_cpp/mathlib.h | 4 |
5 files changed, 34 insertions, 19 deletions
diff --git a/shared_cpp/OrthographicRenderer.cpp b/shared_cpp/Renderer2d.cpp index 21c13a1..f91ed08 100644 --- a/shared_cpp/OrthographicRenderer.cpp +++ b/shared_cpp/Renderer2d.cpp @@ -1,4 +1,4 @@ -#include "OrthographicRenderer.h" +#include "Renderer2d.h" #include "WebglContext.h" #include "mathlib.h" @@ -20,7 +20,7 @@ const char* orthographicFragment = " gl_FragColor = VertexColor; \n" "}"; -void OrthographicRenderer::load(WebglContext* context) { +void Renderer2d::load(WebglContext* context) { printf("Compiling orthographic shader...\n"); shader = loadShader(orthographicVertex, orthographicFragment); @@ -34,7 +34,7 @@ void OrthographicRenderer::load(WebglContext* context) { printf("Orthographic shader compiled.\n"); } -void OrthographicRenderer::render() { +void Renderer2d::render() { glEnable(GL_DEPTH_TEST); glDepthFunc(GL_LEQUAL); glDepthMask(GL_TRUE); @@ -45,14 +45,14 @@ void OrthographicRenderer::render() { setShaderMat4(uniforms.projection, projection); } -void OrthographicRenderer::unload() { +void Renderer2d::unload() { glClearColor(0.2f, 0.3f, 0.3f, 1.0f); glClear(GL_COLOR_BUFFER_BIT); glDeleteProgram(shader); } -void OrthographicShape::load(OrthographicVertex* inVertices, uint32 inNumVertices, OrthographicRenderer* renderer) { +void Mesh2d::load(Vertex2d* inVertices, uint32 inNumVertices, Renderer2d* renderer, GLenum loadType) { numVertices = inNumVertices; useShader(renderer->shader); @@ -61,19 +61,19 @@ void OrthographicShape::load(OrthographicVertex* inVertices, uint32 inNumVertice glGenBuffers(1, &vbo); glBindBuffer(GL_ARRAY_BUFFER, vbo); - glBufferData(GL_ARRAY_BUFFER, inNumVertices * sizeof(OrthographicVertex), &inVertices[0], GL_STATIC_DRAW); + glBufferData(GL_ARRAY_BUFFER, inNumVertices * sizeof(Vertex2d), &inVertices[0], loadType); glEnableVertexAttribArray(renderer->attributes.position); - glVertexAttribPointer(renderer->attributes.position, 2, GL_FLOAT, GL_FALSE, sizeof(OrthographicVertex), (GLvoid *)0); + glVertexAttribPointer(renderer->attributes.position, 2, GL_FLOAT, GL_FALSE, sizeof(Vertex2d), (GLvoid *)0); glEnableVertexAttribArray(renderer->attributes.color); - glVertexAttribPointer(renderer->attributes.color, 4, GL_FLOAT, GL_FALSE, sizeof(OrthographicVertex), (GLvoid *)offsetof(OrthographicVertex, color)); + glVertexAttribPointer(renderer->attributes.color, 4, GL_FLOAT, GL_FALSE, sizeof(Vertex2d), (GLvoid *)offsetof(Vertex2d, color)); glBindBuffer(GL_ARRAY_BUFFER, 0); glBindVertexArray(0); } -void OrthographicShape::render(OrthographicRenderer* renderer, GLenum drawType) { +void Mesh2d::render(Renderer2d* renderer, GLenum drawType) { setShaderMat4(renderer->uniforms.model, model); glBindVertexArray(vao); @@ -81,7 +81,7 @@ void OrthographicShape::render(OrthographicRenderer* renderer, GLenum drawType) glBindVertexArray(0); } -void OrthographicShape::unload() { +void Mesh2d::unload() { glDeleteVertexArrays(1, &vao); glDeleteBuffers(1, &vbo); -}
\ No newline at end of file +} diff --git a/shared_cpp/OrthographicRenderer.h b/shared_cpp/Renderer2d.h index cef5305..afde20e 100644 --- a/shared_cpp/OrthographicRenderer.h +++ b/shared_cpp/Renderer2d.h @@ -7,7 +7,7 @@ struct WebglContext; -struct OrthographicRenderer { +struct Renderer2d { Mat4x4 projection; uint32 shader; @@ -26,18 +26,18 @@ struct OrthographicRenderer { void unload(); }; -struct OrthographicVertex { +struct Vertex2d { Vector2 position; Vector4 color; }; -struct OrthographicShape { +struct Mesh2d { uint32 vao; uint32 vbo; uint32 numVertices = 0; Mat4x4 model; - void load(OrthographicVertex* vertices, uint32 numVertices, OrthographicRenderer* renderer); - void render(OrthographicRenderer* renderer, GLenum drawType = GL_TRIANGLES); + void load(Vertex2d* vertices, uint32 numVertices, Renderer2d* renderer, GLenum loadType = GL_STATIC_DRAW); + void render(Renderer2d* renderer, GLenum drawType = GL_TRIANGLES); void unload(); -};
\ No newline at end of file +}; diff --git a/shared_cpp/WebglHelper.h b/shared_cpp/WebglHelper.h index dbdb23f..55dfe97 100644 --- a/shared_cpp/WebglHelper.h +++ b/shared_cpp/WebglHelper.h @@ -1,6 +1,6 @@ #pragma once #include "mathlib.h" -struct OrthographicRenderer; +struct Renderer2d; -void render2DArrow(Vector2 start, Vector2 end, OrthographicRenderer* renderer); +void render2DArrow(Vector2 start, Vector2 end, Renderer2d* renderer); diff --git a/shared_cpp/mathlib.cpp b/shared_cpp/mathlib.cpp index 3402f0e..5996ba3 100644 --- a/shared_cpp/mathlib.cpp +++ b/shared_cpp/mathlib.cpp @@ -12,6 +12,13 @@ // *************************************** // Vector2 +Vector2::Vector2() { } + +Vector2::Vector2(float inX, float inY) { + x = inX; + y = inY; +} + Vector2 getRandomNormalVector2() { Vector2 retval = { static_cast<float>(rand()) / static_cast<float>(RAND_MAX), @@ -291,6 +298,10 @@ Vector4 Vector4::cross(const Vector4& other) { }; } +Vector4 lerp(Vector4 start, Vector4 end, float t) { + return (end - start) * t + start; +} + Vector4 Vector4::operator+(const Vector4& v2) { return add(v2); } diff --git a/shared_cpp/mathlib.h b/shared_cpp/mathlib.h index 5f9abff..0a39195 100644 --- a/shared_cpp/mathlib.h +++ b/shared_cpp/mathlib.h @@ -36,6 +36,8 @@ struct Vector2 { float x = 0; float y = 0; + Vector2(); + Vector2(float inX, float inY); Vector2 operator+(Vector2 other); Vector2& operator+=(Vector2 other); Vector2 operator-(Vector2 other); @@ -113,6 +115,8 @@ struct Vector4 { void printDebug(const char* name); }; +Vector4 lerp(Vector4 start, Vector4 end, float t); + struct Mat4x4 { float m[16] = { 1, 0, 0, 0, |