From 06d489e0fe2f04cf765b920541d899c329dd0ca9 Mon Sep 17 00:00:00 2001 From: Your Name Date: Mon, 17 Dec 2018 14:22:23 +0100 Subject: [PATCH] prepared for obj stuff --- CMakeLists.txt | 1 + assets/shaders/vertex_shader.glsl | 4 +-- src/AssetManager.hpp | 1 - src/Camera.cpp | 33 ++++++++++++++++++++++ src/Camera.hpp | 46 +++++++++++++++++++++++++++++++ src/System.cpp | 42 ++++++++++++++++++++++++++-- src/System.hpp | 12 ++++---- 7 files changed, 128 insertions(+), 11 deletions(-) create mode 100644 src/Camera.cpp create mode 100644 src/Camera.hpp diff --git a/CMakeLists.txt b/CMakeLists.txt index b9858d6..2c137d1 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -43,6 +43,7 @@ include_directories( set(SRCS "src/AssetManager.cpp" + "src/Camera.cpp" "src/Logger.cpp" "src/Main.cpp" "src/Model.cpp" diff --git a/assets/shaders/vertex_shader.glsl b/assets/shaders/vertex_shader.glsl index 744420a..02d5904 100644 --- a/assets/shaders/vertex_shader.glsl +++ b/assets/shaders/vertex_shader.glsl @@ -1,9 +1,9 @@ #version 330 core layout(location = 0) in vec3 vert_pos; uniform vec3 col; -/*uniform mat4 mvp;*/ +uniform mat4 mvp; out vec3 frag_col; void main() { - gl_Position = /*mvp * */vec4(vert_pos, 1); + gl_Position = mvp * vec4(vert_pos, 1); frag_col = col; } diff --git a/src/AssetManager.hpp b/src/AssetManager.hpp index 1c80fde..f63e8da 100644 --- a/src/AssetManager.hpp +++ b/src/AssetManager.hpp @@ -25,7 +25,6 @@ class AssetManager { public: - AssetManager(); /*void loadOBJ(const std::string &path, const std::string &name);*/ diff --git a/src/Camera.cpp b/src/Camera.cpp new file mode 100644 index 0000000..daf471d --- /dev/null +++ b/src/Camera.cpp @@ -0,0 +1,33 @@ +/* + * Copyright (C) 2018 Ortega Froysa, Nicolás All rights reserved. + * Author: Ortega Froysa, Nicolás + * + * This software is provided 'as-is', without any express or implied + * warranty. In no event will the authors be held liable for any damages + * arising from the use of this software. + * + * Permission is granted to anyone to use this software for any purpose, + * including commercial applications, and to alter it and redistribute it + * freely, subject to the following restrictions: + * + * 1. The origin of this software must not be misrepresented; you must not + * claim that you wrote the original software. If you use this software + * in a product, an acknowledgment in the product documentation would be + * appreciated but is not required. + * + * 2. Altered source versions must be plainly marked as such, and must not be + * misrepresented as being the original software. + * + * 3. This notice may not be removed or altered from any source + * distribution. + */ + +#include "Camera.hpp" + +#include +#include + +Camera::Camera() : pos(0.0f, 0.0f, 0.0f), + yaw(45.0f), angle(45.0f), dist(5.0f) { } + + diff --git a/src/Camera.hpp b/src/Camera.hpp new file mode 100644 index 0000000..5cbebc2 --- /dev/null +++ b/src/Camera.hpp @@ -0,0 +1,46 @@ +/* + * Copyright (C) 2018 Ortega Froysa, Nicolás All rights reserved. + * Author: Ortega Froysa, Nicolás + * + * This software is provided 'as-is', without any express or implied + * warranty. In no event will the authors be held liable for any damages + * arising from the use of this software. + * + * Permission is granted to anyone to use this software for any purpose, + * including commercial applications, and to alter it and redistribute it + * freely, subject to the following restrictions: + * + * 1. The origin of this software must not be misrepresented; you must not + * claim that you wrote the original software. If you use this software + * in a product, an acknowledgment in the product documentation would be + * appreciated but is not required. + * + * 2. Altered source versions must be plainly marked as such, and must not be + * misrepresented as being the original software. + * + * 3. This notice may not be removed or altered from any source + * distribution. + */ + +#pragma once + +#include +#include +#include + + +class Camera { +public: + Camera(); + inline glm::vec3 get_pos() { + return pos; + } + inline void setPosition(glm::vec3 _pos) { + this->pos = _pos; + } +private: + glm::vec3 pos; + float yaw; + float angle; + float dist; // distance from origin +}; diff --git a/src/System.cpp b/src/System.cpp index cbd5855..8c457ce 100644 --- a/src/System.cpp +++ b/src/System.cpp @@ -65,9 +65,24 @@ System::System() : running(true) { glClearColor(0.0f, 0.0f, 0.0f, 1.0f); - shader = std::make_unique( + /*shader = std::make_unique( "../assets/shaders/vertex_shader.glsl", - "../assets/shaders/fragment_shader.glsl"); + "../assets/shaders/fragment_shader.glsl");*/ + shaders.insert({"default", Shader( + "../assets/shaders/vertex_shader.glsl", + "../assets/shaders/fragment_shader.glsl")}); + matrix_id = glGetUniformLocation( + shaders.at("default").getId(), + "mvp"); + asset_mngr.loadModel( + "../assets/models/board.dae", + "board"); + asset_mngr.loadModel( + "../assets/models/cube.dae", + "cube"); + asset_mngr.loadModel( + "../assets/models/sphere.dae", + "sphere"); } System::~System() { @@ -79,12 +94,33 @@ System::~System() { void System::run() { unsigned int lastTime = SDL_GetTicks(); + cam.setPosition(glm::vec3(4,4,4)); while(running) { syncInputs(); glClear(GL_COLOR_BUFFER_BIT bitor GL_DEPTH_BUFFER_BIT); - shader->use(); + shaders.at("default").use(); + glm::mat4 mvp; + { + glm::mat4 proj = glm::perspective(glm::radians(45.0f), + static_cast(WINDOW_WIDTH) / static_cast(WINDOW_HEIGHT), + 0.1f, 100.0f); + + glm::mat4 view = glm::lookAt( + cam.get_pos(), // camera position + glm::vec3(0,0,0), // where the camera is looking + glm::vec3(0,1,0) // which way is vertically up + ); + glm::mat4 mod = glm::mat4(1.0f); // identity matrix, object is at origin + mvp = proj * view * mod; + + } + glUniformMatrix4fv(matrix_id, 1, GL_FALSE, &mvp[0][0]); + + + asset_mngr.getModel("board")->setColor(1,0,0); + asset_mngr.getModel("board")->draw(shaders.at("default")); SDL_GL_SwapWindow(window); diff --git a/src/System.hpp b/src/System.hpp index b8cf672..3750caf 100644 --- a/src/System.hpp +++ b/src/System.hpp @@ -20,12 +20,13 @@ #include "Logger.hpp" #include "Shader.hpp" - +#include "AssetManager.hpp" #include #include +#include #include #include - +#include "Camera.hpp" class System { public: System(); @@ -36,10 +37,11 @@ public: private: void syncInputs(); - + AssetManager asset_mngr; SDL_Window *window; SDL_GLContext glcontext; - std::unique_ptr shader; - + std::map shaders; + Camera cam; bool running; + GLuint matrix_id; };