From 2a12a1f11900fb84da22dd70b9f11d500dfb9270 Mon Sep 17 00:00:00 2001 From: Dane Johnson Date: Fri, 15 Jan 2021 16:15:47 -0600 Subject: [PATCH] Added new shader types --- core/Input.cpp | 11 +++++++++-- core/Input.h | 1 + core/Shader.cpp | 4 ++++ core/Shader.h | 2 ++ core/Shaders/FlatShader.cpp | 7 +++++++ core/Shaders/FlatShader.h | 7 +++++++ core/couch.cpp | 15 ++++++++------- core/types.h | 2 ++ shaders/flat.frag | 5 ++--- 9 files changed, 42 insertions(+), 12 deletions(-) create mode 100644 core/Shaders/FlatShader.cpp create mode 100644 core/Shaders/FlatShader.h diff --git a/core/Input.cpp b/core/Input.cpp index 780bf2f..b1de7d8 100644 --- a/core/Input.cpp +++ b/core/Input.cpp @@ -26,8 +26,15 @@ void Input::HandleKeys(Window *window, int keys, int code, int action, int mods) } void Input::HandleMousePosition(Window *window, double xpos, double ypos) { - double relx = xpos - instance->lastx; - double rely = ypos - instance->lasty; + double relx, rely; + if (instance->firstMousePositionUpdate) { + relx = 0.0; + rely = 0.0; + instance->firstMousePositionUpdate = false; + } else { + relx = xpos - instance->lastx; + rely = ypos - instance->lasty; + } for (MousePositionHandler mousePositionHandler : instance->mousePositionHandlers) { mousePositionHandler(window, xpos, ypos, relx, rely); } diff --git a/core/Input.h b/core/Input.h index 96696d5..9b8da02 100644 --- a/core/Input.h +++ b/core/Input.h @@ -17,6 +17,7 @@ public: std::vector keyHandlers; std::vector mousePositionHandlers; private: + bool firstMousePositionUpdate = true; double lastx, lasty; Input(); static void HandleKeys(Window *window, int key, int code, int action, int mods); diff --git a/core/Shader.cpp b/core/Shader.cpp index e146806..a3d33c5 100644 --- a/core/Shader.cpp +++ b/core/Shader.cpp @@ -84,3 +84,7 @@ void Shader::UpdateModel(Matrix model) { void Shader::UpdateProjection(Matrix projection) { glUniformMatrix4fv(glGetUniformLocation(id, "PROJECTION"), 1, GL_FALSE, glm::value_ptr(projection)); } + +Name Shader::GetName() const { + return "Unnamed Shader"; +} diff --git a/core/Shader.h b/core/Shader.h index 32e5d93..0b8c55e 100644 --- a/core/Shader.h +++ b/core/Shader.h @@ -19,6 +19,8 @@ public: void UpdateView(Matrix view); void UpdateModel(Matrix model); void UpdateProjection(Matrix projection); + + virtual Name GetName() const; }; #endif /* SHADER_H */ diff --git a/core/Shaders/FlatShader.cpp b/core/Shaders/FlatShader.cpp new file mode 100644 index 0000000..1ab44d4 --- /dev/null +++ b/core/Shaders/FlatShader.cpp @@ -0,0 +1,7 @@ +#include "FlatShader.h" + +FlatShader::FlatShader() : Shader("shaders/flat.vert", "shaders/flat.frag") {} + +void FlatShader::UpdateColor(Vector3 color) { + glUniform3f(glGetUniformLocation(id, "color"), color.r, color.g, color.b); +} diff --git a/core/Shaders/FlatShader.h b/core/Shaders/FlatShader.h new file mode 100644 index 0000000..165b58f --- /dev/null +++ b/core/Shaders/FlatShader.h @@ -0,0 +1,7 @@ +#include "Shader.h" + +class FlatShader : public Shader { +public: + FlatShader(); + void UpdateColor(Vector3 color); +}; diff --git a/core/couch.cpp b/core/couch.cpp index 3e6a803..728b07c 100644 --- a/core/couch.cpp +++ b/core/couch.cpp @@ -8,7 +8,7 @@ #include "types.h" -#include "Shader.h" +#include "Shaders/FlatShader.h" #include "Ball.h" #include "Camera.h" #include "Input.h" @@ -22,7 +22,7 @@ const int height = 600; Node *root; -void render(Node *curr, Shader shader, Matrix model) { +void render(Node *curr, Shader *shader, Matrix model) { if (curr->IsDrawable()) { if (curr->IsTransformable()) { Spatial *spatial = dynamic_cast(curr); @@ -30,7 +30,7 @@ void render(Node *curr, Shader shader, Matrix model) { model = glm::rotate(model, spatial->transform.rotation.y, Vector3(0.0f, 1.0f, 0.0f)); model = glm::rotate(model, spatial->transform.rotation.z, Vector3(0.0f, 0.0f, 1.0f)); model = glm::translate(model, spatial->transform.position); - shader.UpdateModel(model); + shader->UpdateModel(model); } Drawable *drawable = dynamic_cast(curr); drawable->Draw(); @@ -72,11 +72,12 @@ int main() { Camera defaultCamera; - Shader shader("shaders/flat.vert", "shaders/flat.frag"); - shader.Use(); + FlatShader *shader = new FlatShader(); + shader->Use(); + shader->UpdateColor(Vector3(0.0f, 1.0f, 0.0f)); Matrix projection = glm::perspective(glm::radians(45.0f), 800.0f/600.0f, 0.1f, 100.0f); - shader.UpdateProjection(projection); + shader->UpdateProjection(projection); // TODO Allow multiple scripting languages Lua *lua = new Lua(); @@ -97,7 +98,7 @@ int main() { view = glm::rotate(view, -camera->transform.rotation.y, Vector3(0.0f, 1.0f, 0.0f)); view = glm::rotate(view, -camera->transform.rotation.z, Vector3(0.0f, 0.0f, 1.0f)); view = glm::translate(view, -camera->transform.position); - shader.UpdateView(view); + shader->UpdateView(view); // Render the scene tree render(root, shader, Matrix(1.0f)); diff --git a/core/types.h b/core/types.h index 7f57050..e4586d8 100644 --- a/core/types.h +++ b/core/types.h @@ -2,6 +2,7 @@ #define TYPES_H #include +#include #include #include @@ -10,6 +11,7 @@ typedef GLFWwindow Window; typedef glm::vec3 Vector3; typedef glm::mat4 Matrix; +typedef std::string Name; typedef GLfloat cfloat; typedef GLuint Id; diff --git a/shaders/flat.frag b/shaders/flat.frag index 9b6c80c..64b3003 100644 --- a/shaders/flat.frag +++ b/shaders/flat.frag @@ -1,10 +1,9 @@ #version 330 core -// uniform vec3 color; +uniform vec3 color; out vec4 FragColor; void main() { - //FragColor = vec4(color, 1.0); - FragColor = vec4(1.0, 0.0, 0.0, 1.0); + FragColor = vec4(color, 1.0); }