Added new shader types

This commit is contained in:
Dane Johnson 2021-01-15 16:15:47 -06:00
parent e99b623fec
commit 2a12a1f119
9 changed files with 42 additions and 12 deletions

View File

@ -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) { void Input::HandleMousePosition(Window *window, double xpos, double ypos) {
double relx = xpos - instance->lastx; double relx, rely;
double rely = ypos - instance->lasty; 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) { for (MousePositionHandler mousePositionHandler : instance->mousePositionHandlers) {
mousePositionHandler(window, xpos, ypos, relx, rely); mousePositionHandler(window, xpos, ypos, relx, rely);
} }

View File

@ -17,6 +17,7 @@ public:
std::vector<KeyHandler> keyHandlers; std::vector<KeyHandler> keyHandlers;
std::vector<MousePositionHandler> mousePositionHandlers; std::vector<MousePositionHandler> mousePositionHandlers;
private: private:
bool firstMousePositionUpdate = true;
double lastx, lasty; double lastx, lasty;
Input(); Input();
static void HandleKeys(Window *window, int key, int code, int action, int mods); static void HandleKeys(Window *window, int key, int code, int action, int mods);

View File

@ -84,3 +84,7 @@ void Shader::UpdateModel(Matrix model) {
void Shader::UpdateProjection(Matrix projection) { void Shader::UpdateProjection(Matrix projection) {
glUniformMatrix4fv(glGetUniformLocation(id, "PROJECTION"), 1, GL_FALSE, glm::value_ptr(projection)); glUniformMatrix4fv(glGetUniformLocation(id, "PROJECTION"), 1, GL_FALSE, glm::value_ptr(projection));
} }
Name Shader::GetName() const {
return "Unnamed Shader";
}

View File

@ -19,6 +19,8 @@ public:
void UpdateView(Matrix view); void UpdateView(Matrix view);
void UpdateModel(Matrix model); void UpdateModel(Matrix model);
void UpdateProjection(Matrix projection); void UpdateProjection(Matrix projection);
virtual Name GetName() const;
}; };
#endif /* SHADER_H */ #endif /* SHADER_H */

View File

@ -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);
}

View File

@ -0,0 +1,7 @@
#include "Shader.h"
class FlatShader : public Shader {
public:
FlatShader();
void UpdateColor(Vector3 color);
};

View File

@ -8,7 +8,7 @@
#include "types.h" #include "types.h"
#include "Shader.h" #include "Shaders/FlatShader.h"
#include "Ball.h" #include "Ball.h"
#include "Camera.h" #include "Camera.h"
#include "Input.h" #include "Input.h"
@ -22,7 +22,7 @@ const int height = 600;
Node *root; Node *root;
void render(Node *curr, Shader shader, Matrix model) { void render(Node *curr, Shader *shader, Matrix model) {
if (curr->IsDrawable()) { if (curr->IsDrawable()) {
if (curr->IsTransformable()) { if (curr->IsTransformable()) {
Spatial *spatial = dynamic_cast<Spatial*>(curr); Spatial *spatial = dynamic_cast<Spatial*>(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.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::rotate(model, spatial->transform.rotation.z, Vector3(0.0f, 0.0f, 1.0f));
model = glm::translate(model, spatial->transform.position); model = glm::translate(model, spatial->transform.position);
shader.UpdateModel(model); shader->UpdateModel(model);
} }
Drawable *drawable = dynamic_cast<Drawable*>(curr); Drawable *drawable = dynamic_cast<Drawable*>(curr);
drawable->Draw(); drawable->Draw();
@ -72,11 +72,12 @@ int main() {
Camera defaultCamera; Camera defaultCamera;
Shader shader("shaders/flat.vert", "shaders/flat.frag"); FlatShader *shader = new FlatShader();
shader.Use(); 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); 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 // TODO Allow multiple scripting languages
Lua *lua = new Lua(); 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.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::rotate(view, -camera->transform.rotation.z, Vector3(0.0f, 0.0f, 1.0f));
view = glm::translate(view, -camera->transform.position); view = glm::translate(view, -camera->transform.position);
shader.UpdateView(view); shader->UpdateView(view);
// Render the scene tree // Render the scene tree
render(root, shader, Matrix(1.0f)); render(root, shader, Matrix(1.0f));

View File

@ -2,6 +2,7 @@
#define TYPES_H #define TYPES_H
#include <vector> #include <vector>
#include <string>
#include <GL/glew.h> #include <GL/glew.h>
#include <GLFW/glfw3.h> #include <GLFW/glfw3.h>
@ -10,6 +11,7 @@
typedef GLFWwindow Window; typedef GLFWwindow Window;
typedef glm::vec3 Vector3; typedef glm::vec3 Vector3;
typedef glm::mat4 Matrix; typedef glm::mat4 Matrix;
typedef std::string Name;
typedef GLfloat cfloat; typedef GLfloat cfloat;
typedef GLuint Id; typedef GLuint Id;

View File

@ -1,10 +1,9 @@
#version 330 core #version 330 core
// uniform vec3 color; uniform vec3 color;
out vec4 FragColor; out vec4 FragColor;
void main() { void main() {
//FragColor = vec4(color, 1.0); FragColor = vec4(color, 1.0);
FragColor = vec4(1.0, 0.0, 0.0, 1.0);
} }