From 5c613a10364f30bd6add25f7950433f0c482c3ca Mon Sep 17 00:00:00 2001 From: Matthew Kosarek Date: Sun, 31 Oct 2021 13:54:11 -0400 Subject: (mkosarek) Working undamped spring motion --- shared_cpp/OrthographicRenderer.cpp | 87 ------------------------------------- shared_cpp/OrthographicRenderer.h | 43 ------------------ shared_cpp/Renderer2d.cpp | 87 +++++++++++++++++++++++++++++++++++++ shared_cpp/Renderer2d.h | 43 ++++++++++++++++++ shared_cpp/WebglHelper.h | 4 +- shared_cpp/mathlib.cpp | 11 +++++ shared_cpp/mathlib.h | 4 ++ 7 files changed, 147 insertions(+), 132 deletions(-) delete mode 100644 shared_cpp/OrthographicRenderer.cpp delete mode 100644 shared_cpp/OrthographicRenderer.h create mode 100644 shared_cpp/Renderer2d.cpp create mode 100644 shared_cpp/Renderer2d.h (limited to 'shared_cpp') diff --git a/shared_cpp/OrthographicRenderer.cpp b/shared_cpp/OrthographicRenderer.cpp deleted file mode 100644 index 21c13a1..0000000 --- a/shared_cpp/OrthographicRenderer.cpp +++ /dev/null @@ -1,87 +0,0 @@ -#include "OrthographicRenderer.h" -#include "WebglContext.h" -#include "mathlib.h" - -const char* orthographicVertex = -"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* orthographicFragment = -"varying lowp vec4 VertexColor; \n" -"void main() { \n" -" gl_FragColor = VertexColor; \n" -"}"; - -void OrthographicRenderer::load(WebglContext* context) { - printf("Compiling orthographic shader...\n"); - shader = loadShader(orthographicVertex, orthographicFragment); - - 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 OrthographicRenderer::render() { - glEnable(GL_DEPTH_TEST); - glDepthFunc(GL_LEQUAL); - glDepthMask(GL_TRUE); - glClearColor(0.2f, 0.3f, 0.3f, 1.0f); - glClearDepth(1.0f); - glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT); - useShader(shader); - setShaderMat4(uniforms.projection, projection); -} - -void OrthographicRenderer::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) { - 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), &inVertices[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, GLenum drawType) { - setShaderMat4(renderer->uniforms.model, model); - - glBindVertexArray(vao); - glDrawArrays(drawType, 0, numVertices); - glBindVertexArray(0); -} - -void OrthographicShape::unload() { - glDeleteVertexArrays(1, &vao); - glDeleteBuffers(1, &vbo); -} \ No newline at end of file diff --git a/shared_cpp/OrthographicRenderer.h b/shared_cpp/OrthographicRenderer.h deleted file mode 100644 index cef5305..0000000 --- a/shared_cpp/OrthographicRenderer.h +++ /dev/null @@ -1,43 +0,0 @@ -#pragma once - -#include "WebglContext.h" -#include "types.h" -#include "Shader.h" -#include "mathlib.h" - -struct WebglContext; - -struct OrthographicRenderer { - Mat4x4 projection; - uint32 shader; - - struct { - int32 position; - int32 color; - } attributes; - - struct { - int32 projection; - int32 model; - } uniforms; - - void load(WebglContext* context); - void render(); - void unload(); -}; - -struct OrthographicVertex { - Vector2 position; - Vector4 color; -}; - -struct OrthographicShape { - 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 unload(); -}; \ No newline at end of file diff --git a/shared_cpp/Renderer2d.cpp b/shared_cpp/Renderer2d.cpp new file mode 100644 index 0000000..f91ed08 --- /dev/null +++ b/shared_cpp/Renderer2d.cpp @@ -0,0 +1,87 @@ +#include "Renderer2d.h" +#include "WebglContext.h" +#include "mathlib.h" + +const char* orthographicVertex = +"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* orthographicFragment = +"varying lowp vec4 VertexColor; \n" +"void main() { \n" +" gl_FragColor = VertexColor; \n" +"}"; + +void Renderer2d::load(WebglContext* context) { + printf("Compiling orthographic shader...\n"); + shader = loadShader(orthographicVertex, orthographicFragment); + + 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.2f, 0.3f, 0.3f, 1.0f); + 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.2f, 0.3f, 0.3f, 1.0f); + glClear(GL_COLOR_BUFFER_BIT); + glDeleteProgram(shader); +} + + +void Mesh2d::load(Vertex2d* inVertices, uint32 inNumVertices, Renderer2d* renderer, GLenum loadType) { + numVertices = inNumVertices; + useShader(renderer->shader); + + glGenVertexArrays(1, &vao); + glBindVertexArray(vao); + + glGenBuffers(1, &vbo); + glBindBuffer(GL_ARRAY_BUFFER, vbo); + 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); + + glEnableVertexAttribArray(renderer->attributes.color); + glVertexAttribPointer(renderer->attributes.color, 4, GL_FLOAT, GL_FALSE, sizeof(Vertex2d), (GLvoid *)offsetof(Vertex2d, color)); + + glBindBuffer(GL_ARRAY_BUFFER, 0); + glBindVertexArray(0); +} + +void Mesh2d::render(Renderer2d* renderer, GLenum drawType) { + setShaderMat4(renderer->uniforms.model, model); + + glBindVertexArray(vao); + glDrawArrays(drawType, 0, numVertices); + glBindVertexArray(0); +} + +void Mesh2d::unload() { + glDeleteVertexArrays(1, &vao); + glDeleteBuffers(1, &vbo); +} diff --git a/shared_cpp/Renderer2d.h b/shared_cpp/Renderer2d.h new file mode 100644 index 0000000..afde20e --- /dev/null +++ b/shared_cpp/Renderer2d.h @@ -0,0 +1,43 @@ +#pragma once + +#include "WebglContext.h" +#include "types.h" +#include "Shader.h" +#include "mathlib.h" + +struct WebglContext; + +struct Renderer2d { + Mat4x4 projection; + uint32 shader; + + struct { + int32 position; + int32 color; + } attributes; + + struct { + int32 projection; + int32 model; + } uniforms; + + void load(WebglContext* context); + void render(); + void unload(); +}; + +struct Vertex2d { + Vector2 position; + Vector4 color; +}; + +struct Mesh2d { + uint32 vao; + uint32 vbo; + uint32 numVertices = 0; + Mat4x4 model; + + void load(Vertex2d* vertices, uint32 numVertices, Renderer2d* renderer, GLenum loadType = GL_STATIC_DRAW); + void render(Renderer2d* renderer, GLenum drawType = GL_TRIANGLES); + void unload(); +}; 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(rand()) / static_cast(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, -- cgit v1.2.1