Show my square

This commit is contained in:
Dane Johnson 2021-01-13 13:17:31 -06:00
parent 41b99aa442
commit d5788228f6
6 changed files with 40 additions and 10 deletions

View File

@ -1,10 +1,12 @@
#include "Ball.h" #include "Ball.h"
Ball::Ball() { Ball::Ball() {
vertices.push_back(Vertex(0.0f, 0.0f, 0.0f)); vertices.push_back(Vertex(1.0f, 1.0f, -1.0f));
vertices.push_back(Vertex(1.0f, 0.0f, 0.0f)); vertices.push_back(Vertex(1.0f, -1.0f, -1.0f));
vertices.push_back(Vertex(0.5f, 1.0f, 0.0f)); vertices.push_back(Vertex(-1.0f, 1.0f, -1.0f));
vertices.push_back(Vertex(-1.0f, -1.0f, -1.0f));
indices.push_back(Index(0, 1, 2)); indices.push_back(Index(0, 1, 2));
indices.push_back(Index(1, 2, 3));
} }

View File

@ -65,9 +65,22 @@ Shader::Shader(const char* vertexPath, const char* fragmentPath) {
glDeleteShader(vertex); glDeleteShader(vertex);
glDeleteShader(fragment); glDeleteShader(fragment);
} }
void Shader::Use() { void Shader::Use() {
glUseProgram(id); glUseProgram(id);
} }
void Shader::UpdateView(Matrix view) {
glUniformMatrix4fv(glGetUniformLocation(id, "VIEW"), 1, GL_FALSE, glm::value_ptr(view));
}
void Shader::UpdateModel(Matrix model) {
glUniformMatrix4fv(glGetUniformLocation(id, "MODEL"), 1, GL_FALSE, glm::value_ptr(model));
}
void Shader::UpdateProjection(Matrix projection) {
glUniformMatrix4fv(glGetUniformLocation(id, "PROJECTION"), 1, GL_FALSE, glm::value_ptr(projection));
}

View File

@ -6,6 +6,8 @@
#include <sstream> #include <sstream>
#include <iostream> #include <iostream>
#include <glm/gtc/type_ptr.hpp>
#include "types.h" #include "types.h"
class Shader { class Shader {
@ -14,6 +16,9 @@ public:
Shader(const char *vertexPath, const char *fragmentPath); Shader(const char *vertexPath, const char *fragmentPath);
void Use(); void Use();
void UpdateView(Matrix view);
void UpdateModel(Matrix model);
void UpdateProjection(Matrix projection);
}; };
#endif /* SHADER_H */ #endif /* SHADER_H */

View File

@ -3,6 +3,8 @@
#include <GL/glew.h> #include <GL/glew.h>
#include <GLFW/glfw3.h> #include <GLFW/glfw3.h>
#include <glm/gtc/matrix_transform.hpp>
#include "types.h" #include "types.h"
#include "Shader.h" #include "Shader.h"
@ -42,10 +44,18 @@ int main() {
Ball ball; Ball ball;
ball.SetupMesh(); ball.SetupMesh();
Matrix projection = glm::perspective(glm::radians(45.0f), 800.0f/600.0f, 0.1f, 100.0f);
shader.UpdateProjection(projection);
Matrix view(1.0f);
shader.UpdateView(view);
while(!glfwWindowShouldClose(window)) { while(!glfwWindowShouldClose(window)) {
glClearColor(0.5f, 0.5f, 0.5f, 1.0f); glClearColor(0.5f, 0.5f, 0.5f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT); glClear(GL_COLOR_BUFFER_BIT);
Matrix model(1.0f);
model = glm::translate(model, ball.transform.position);
shader.UpdateModel(model);
ball.Draw(); ball.Draw();
glfwSwapBuffers(window); glfwSwapBuffers(window);

View File

@ -9,6 +9,7 @@
typedef GLFWwindow Window; typedef GLFWwindow Window;
typedef glm::vec3 Vector3; typedef glm::vec3 Vector3;
typedef glm::mat4 Matrix;
typedef GLfloat cfloat; typedef GLfloat cfloat;
typedef GLuint Id; typedef GLuint Id;

View File

@ -2,11 +2,10 @@
layout (location = 0) in vec3 pos; layout (location = 0) in vec3 pos;
// uniform mat4 MODEL; uniform mat4 MODEL;
// uniform mat4 VIEW; uniform mat4 VIEW;
// uniform mat4 PROJECTION; uniform mat4 PROJECTION;
void main() { void main() {
//gl_Position = MODEL * VIEW * PROJECTION * vec4(pos, 1.0); gl_Position = PROJECTION * VIEW * MODEL * vec4(pos, 1.0);
gl_Position = vec4(pos, 1.0);
} }