2024-08-10 19:12:21 -04:00
|
|
|
#include <GL/glew.h>
|
|
|
|
#include <GLFW/glfw3.h>
|
|
|
|
|
2024-08-12 21:40:28 -04:00
|
|
|
#define GLM_ENABLE_EXPERIMENTAL
|
|
|
|
#include <glm/glm.hpp>
|
|
|
|
#include <glm/gtc/matrix_transform.hpp>
|
|
|
|
#include <glm/gtx/transform.hpp>
|
|
|
|
#include <glm/gtc/type_ptr.hpp>
|
|
|
|
|
2024-08-11 21:20:17 -04:00
|
|
|
#include <imgui.h>
|
|
|
|
#include <imgui_impl_glfw.h>
|
|
|
|
#include <imgui_impl_opengl3.h>
|
|
|
|
|
2024-08-10 19:12:21 -04:00
|
|
|
#include <iostream>
|
|
|
|
#include <fstream>
|
|
|
|
#include <sstream>
|
|
|
|
#include <string>
|
|
|
|
|
2024-08-12 17:20:14 -04:00
|
|
|
#include "scene.h"
|
2024-08-12 21:40:28 -04:00
|
|
|
#include "Uniforms.h"
|
|
|
|
#include "InitShader.h" //Functions for loading shaders from text files
|
|
|
|
#include "DebugCallback.h"
|
2024-08-12 21:55:08 -04:00
|
|
|
#include "PlatformUtils.h"
|
2024-08-12 21:40:28 -04:00
|
|
|
|
2024-08-13 16:25:56 -04:00
|
|
|
namespace {
|
2024-08-12 21:40:28 -04:00
|
|
|
|
2024-08-13 16:25:56 -04:00
|
|
|
const std::string kVertexShaderPath = "shaders/vertex.glsl";
|
|
|
|
const std::string kFragmentShaderPath = "shaders/fragment.glsl";
|
2024-08-12 21:40:28 -04:00
|
|
|
|
2024-08-13 16:25:56 -04:00
|
|
|
} // namespace
|
2024-08-12 21:40:28 -04:00
|
|
|
|
2024-08-13 16:25:56 -04:00
|
|
|
Scene::Scene() : shader_program_(-1), vao_(-1), angle_(0.0f), scale_(1.0f), aspect_(1280.0f / 720.0f), near_z_(0.1f), far_z_(100.0f), fov_(glm::pi<float>() / 4.0f) {};
|
2024-08-12 21:40:28 -04:00
|
|
|
|
2024-08-13 16:25:56 -04:00
|
|
|
Scene::~Scene() {
|
|
|
|
glDeleteProgram(shader_program_);
|
|
|
|
glDeleteVertexArrays(1, &vao_);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Scene::Init() {
|
|
|
|
glewInit();
|
|
|
|
|
|
|
|
glEnable(GL_DEPTH_TEST);
|
|
|
|
glEnable(GL_CULL_FACE);
|
|
|
|
|
|
|
|
//InitTri();
|
|
|
|
InitBuffers();
|
|
|
|
ReloadShader();
|
|
|
|
|
|
|
|
UpdateCamera();
|
|
|
|
Uniforms::Init();
|
|
|
|
}
|
|
|
|
|
|
|
|
void Scene::InitBuffers() {
|
|
|
|
GLuint vbo;
|
|
|
|
float vertices[] = {
|
|
|
|
-0.5f, -0.5f, 0.0f,
|
|
|
|
0.5f, -0.5f, 0.0f,
|
|
|
|
0.0f, 0.5f, 0.0f
|
|
|
|
};
|
|
|
|
|
|
|
|
glGenBuffers(1, &vbo);
|
|
|
|
glBindBuffer(GL_ARRAY_BUFFER, vbo);
|
|
|
|
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
|
|
|
|
|
|
|
|
glGenVertexArrays(1, &vao_);
|
|
|
|
glBindVertexArray(vao_);
|
|
|
|
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(float), (void*)0);
|
|
|
|
glEnableVertexAttribArray(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Scene::ReloadShader() {
|
|
|
|
GLuint new_shader = InitShader(kVertexShaderPath.c_str(), kFragmentShaderPath.c_str());
|
|
|
|
if (new_shader == -1) {
|
|
|
|
DEBUG_BREAK();
|
|
|
|
glClearColor(1.0f, 0.0f, 1.0f, 0.0f);
|
|
|
|
} else {
|
|
|
|
glClearColor(0.35f, 0.35f, 0.35f, 0.0f);
|
|
|
|
if (shader_program_ != -1) {
|
|
|
|
glDeleteProgram(shader_program_);
|
|
|
|
}
|
|
|
|
shader_program_ = new_shader;
|
2024-08-12 21:40:28 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Scene::Display(GLFWwindow* window) {
|
2024-08-13 16:25:56 -04:00
|
|
|
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
|
2024-08-12 21:40:28 -04:00
|
|
|
|
2024-08-13 16:25:56 -04:00
|
|
|
view_matrix_ = glm::lookAt(glm::vec3(Uniforms::SceneData.eye_w), glm::vec3(0.0f, 0.0f, 0.0f), glm::vec3(0.0f, 1.0f, 0.0f));
|
|
|
|
Uniforms::SceneData.PV = projection_matrix_ * view_matrix_; // Projection-View matrix
|
2024-08-12 21:40:28 -04:00
|
|
|
Uniforms::BufferSceneData();
|
|
|
|
|
2024-08-13 16:25:56 -04:00
|
|
|
glm::mat4 model_matrix = glm::rotate(angle_, glm::vec3(0.0f, 1.0f, 0.0f)) * glm::scale(glm::vec3(scale_));
|
|
|
|
glUniformMatrix4fv(Uniforms::UniformLocs::M, 1, false, glm::value_ptr(model_matrix));
|
2024-08-12 21:40:28 -04:00
|
|
|
|
2024-08-13 16:25:56 -04:00
|
|
|
glUseProgram(shader_program_);
|
|
|
|
glBindVertexArray(vao_);
|
|
|
|
glDrawArrays(GL_TRIANGLES, 0, 3);
|
2024-08-12 21:40:28 -04:00
|
|
|
|
2024-08-13 16:25:56 -04:00
|
|
|
DrawGui(window);
|
2024-08-12 21:40:28 -04:00
|
|
|
glfwSwapBuffers(window);
|
|
|
|
}
|
2024-08-10 19:12:21 -04:00
|
|
|
|
2024-08-13 16:25:56 -04:00
|
|
|
void Scene::DrawGui(GLFWwindow* window) {
|
2024-08-11 21:20:17 -04:00
|
|
|
// Begin ImGui frame
|
|
|
|
ImGui_ImplOpenGL3_NewFrame();
|
|
|
|
ImGui_ImplGlfw_NewFrame();
|
|
|
|
|
|
|
|
// Enable docking
|
|
|
|
ImGuiIO& io = ImGui::GetIO();
|
|
|
|
io.ConfigFlags |= ImGuiConfigFlags_DockingEnable;
|
|
|
|
|
|
|
|
ImGui::NewFrame();
|
|
|
|
|
|
|
|
static ImGuiDockNodeFlags dockspace_flags = ImGuiDockNodeFlags_PassthruCentralNode;
|
|
|
|
|
|
|
|
// Add menu bar
|
|
|
|
ImGuiWindowFlags window_flags = ImGuiWindowFlags_MenuBar | ImGuiWindowFlags_NoDocking;
|
|
|
|
|
|
|
|
// Style windows
|
|
|
|
const ImGuiViewport* viewport = ImGui::GetMainViewport();
|
|
|
|
ImGui::SetNextWindowPos(viewport->WorkPos);
|
|
|
|
ImGui::SetNextWindowSize(viewport->WorkSize);
|
|
|
|
ImGui::SetNextWindowViewport(viewport->ID);
|
|
|
|
ImGui::PushStyleVar(ImGuiStyleVar_WindowRounding, 0.0f);
|
|
|
|
ImGui::PushStyleVar(ImGuiStyleVar_WindowBorderSize, 0.0f);
|
|
|
|
window_flags |= ImGuiWindowFlags_NoTitleBar | ImGuiWindowFlags_NoCollapse | ImGuiWindowFlags_NoResize | ImGuiWindowFlags_NoMove;
|
|
|
|
window_flags |= ImGuiWindowFlags_NoBringToFrontOnFocus | ImGuiWindowFlags_NoNavFocus;
|
|
|
|
window_flags |= ImGuiWindowFlags_NoBackground;
|
|
|
|
|
|
|
|
ImGui::PushStyleVar(ImGuiStyleVar_WindowPadding, ImVec2(0.0f, 0.0f));
|
|
|
|
ImGui::Begin("TerraVisor", nullptr, window_flags);
|
|
|
|
ImGui::PopStyleVar(3);
|
|
|
|
|
|
|
|
// Submit the DockSpace
|
2024-08-13 16:25:56 -04:00
|
|
|
if (io.ConfigFlags & ImGuiConfigFlags_DockingEnable) {
|
2024-08-11 21:20:17 -04:00
|
|
|
ImGuiID dockspace_id = ImGui::GetID("MyDockSpace");
|
|
|
|
ImGui::DockSpace(dockspace_id, ImVec2(0.0f, 0.0f), dockspace_flags);
|
|
|
|
}
|
|
|
|
|
2024-08-13 16:25:56 -04:00
|
|
|
if (ImGui::BeginMenuBar()) {
|
|
|
|
if (ImGui::BeginMenu("Options")) {
|
2024-08-11 21:20:17 -04:00
|
|
|
ImGui::Text("Nothing Here Yet, Check Back Later!");
|
|
|
|
|
|
|
|
ImGui::EndMenu();
|
|
|
|
}
|
|
|
|
|
|
|
|
ImGui::EndMenuBar();
|
|
|
|
}
|
|
|
|
|
|
|
|
ImGui::End();
|
|
|
|
|
|
|
|
//Draw Gui
|
|
|
|
ImGui::Begin("Terrain Controls");
|
2024-08-13 16:25:56 -04:00
|
|
|
if (ImGui::Button("Quit")) {
|
2024-08-11 21:20:17 -04:00
|
|
|
glfwSetWindowShouldClose(window, GLFW_TRUE);
|
|
|
|
}
|
|
|
|
|
|
|
|
ImGui::Text("Application average %.3f ms/frame (%.1f FPS)", 1000.0f / ImGui::GetIO().Framerate, ImGui::GetIO().Framerate);
|
|
|
|
ImGui::End();
|
|
|
|
|
|
|
|
ImGui::Render();
|
|
|
|
ImGui_ImplOpenGL3_RenderDrawData(ImGui::GetDrawData());
|
2024-08-10 19:12:21 -04:00
|
|
|
}
|
|
|
|
|
2024-08-12 21:40:28 -04:00
|
|
|
void Scene::Idle() {
|
2024-08-13 16:25:56 -04:00
|
|
|
|
2024-08-10 19:12:21 -04:00
|
|
|
}
|
|
|
|
|
2024-08-13 16:25:56 -04:00
|
|
|
void Scene::UpdateCamera() {
|
|
|
|
projection_matrix_ = glm::perspective(fov_, aspect_, near_z_, far_z_);
|
2024-08-10 19:12:21 -04:00
|
|
|
}
|
|
|
|
|
2024-08-13 16:25:56 -04:00
|
|
|
// namespace scene {
|
2024-08-11 14:52:38 -04:00
|
|
|
|
2024-08-13 16:25:56 -04:00
|
|
|
// const int kInitWindowWidth = 1280;
|
|
|
|
// const int kInitWindowHeight = 720;
|
2024-08-12 21:40:28 -04:00
|
|
|
|
2024-08-13 16:25:56 -04:00
|
|
|
// static const std::string kVertexShader("shaders/vertex.glsl");
|
|
|
|
// static const std::string kFragmentShader("shaders/fragment.glsl");
|
|
|
|
|
|
|
|
// GLuint shader_program = -1;
|
|
|
|
|
|
|
|
// // Vertex data for a basic triangle. Testing use only
|
|
|
|
// float vertices[] = {
|
|
|
|
// -0.5f, -0.5f, 0.0f,
|
|
|
|
// 0.5f, -0.5f, 0.0f,
|
|
|
|
// 0.0f, 0.5f, 0.0f
|
|
|
|
// };
|
|
|
|
|
|
|
|
// unsigned int VAO = -1;
|
|
|
|
|
|
|
|
// float angle = 0.0f;
|
|
|
|
// float scale = 1.0f;
|
|
|
|
|
|
|
|
// namespace camera {
|
|
|
|
// glm::mat4 V, P;
|
|
|
|
|
|
|
|
// float aspect = static_cast<float>(kInitWindowWidth) / static_cast<float>(kInitWindowHeight);
|
|
|
|
// float near_z = 0.1f;
|
|
|
|
// float far_z = 100.0f;
|
|
|
|
// float fov = glm::pi<float>() / 4.0f;
|
|
|
|
|
|
|
|
// void UpdateP() {
|
|
|
|
// P = glm::perspective(fov, aspect, near_z, far_z);
|
|
|
|
// }
|
|
|
|
// } // namespace camera
|
|
|
|
|
|
|
|
// // Temp function to test shader pipline with basic triangle
|
|
|
|
// void InitTri() {
|
|
|
|
// unsigned int vbo;
|
|
|
|
// glGenBuffers(1, &vbo);
|
|
|
|
// glBindBuffer(GL_ARRAY_BUFFER, vbo);
|
|
|
|
// glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
|
|
|
|
|
|
|
|
// glGenVertexArrays(1, &VAO);
|
|
|
|
// glBindVertexArray(VAO);
|
|
|
|
// glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(float), (void*)0);
|
|
|
|
// glEnableVertexAttribArray(0);
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
|
|
// void Display(GLFWwindow* window) {
|
|
|
|
// // Clear the color and depth buffers
|
|
|
|
// glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
|
|
|
|
|
|
|
|
// camera::V = glm::lookAt(glm::vec3(Uniforms::SceneData.eye_w), glm::vec3(0.0f, 0.0f, 0.0f), glm::vec3(0.0f, 1.0f, 0.0f));
|
|
|
|
// Uniforms::SceneData.PV = camera::P * camera::V; // Projection-View matrix
|
|
|
|
// Uniforms::BufferSceneData();
|
|
|
|
|
|
|
|
// // Model matrix
|
|
|
|
// glm::mat4 M = glm::rotate(angle, glm::vec3(0.0f, 1.0f, 0.0f)) * glm::scale(glm::vec3(scale));
|
|
|
|
// glUniformMatrix4fv(Uniforms::UniformLocs::M, 1, false, glm::value_ptr(M));
|
|
|
|
|
|
|
|
// glUseProgram(shader_program);
|
|
|
|
|
|
|
|
// glBindVertexArray(VAO);
|
|
|
|
// glDrawArrays(GL_TRIANGLES, 0, 3);
|
|
|
|
|
|
|
|
// DrawGUI(window);
|
|
|
|
// glfwSwapBuffers(window);
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// void DrawGUI(GLFWwindow* window) {
|
|
|
|
// // Begin ImGui frame
|
|
|
|
// ImGui_ImplOpenGL3_NewFrame();
|
|
|
|
// ImGui_ImplGlfw_NewFrame();
|
|
|
|
|
|
|
|
// // Enable docking
|
|
|
|
// ImGuiIO& io = ImGui::GetIO();
|
|
|
|
// io.ConfigFlags |= ImGuiConfigFlags_DockingEnable;
|
|
|
|
|
|
|
|
// ImGui::NewFrame();
|
|
|
|
|
|
|
|
// static ImGuiDockNodeFlags dockspace_flags = ImGuiDockNodeFlags_PassthruCentralNode;
|
|
|
|
|
|
|
|
// // Add menu bar
|
|
|
|
// ImGuiWindowFlags window_flags = ImGuiWindowFlags_MenuBar | ImGuiWindowFlags_NoDocking;
|
|
|
|
|
|
|
|
// // Style windows
|
|
|
|
// const ImGuiViewport* viewport = ImGui::GetMainViewport();
|
|
|
|
// ImGui::SetNextWindowPos(viewport->WorkPos);
|
|
|
|
// ImGui::SetNextWindowSize(viewport->WorkSize);
|
|
|
|
// ImGui::SetNextWindowViewport(viewport->ID);
|
|
|
|
// ImGui::PushStyleVar(ImGuiStyleVar_WindowRounding, 0.0f);
|
|
|
|
// ImGui::PushStyleVar(ImGuiStyleVar_WindowBorderSize, 0.0f);
|
|
|
|
// window_flags |= ImGuiWindowFlags_NoTitleBar | ImGuiWindowFlags_NoCollapse | ImGuiWindowFlags_NoResize | ImGuiWindowFlags_NoMove;
|
|
|
|
// window_flags |= ImGuiWindowFlags_NoBringToFrontOnFocus | ImGuiWindowFlags_NoNavFocus;
|
|
|
|
// window_flags |= ImGuiWindowFlags_NoBackground;
|
|
|
|
|
|
|
|
// ImGui::PushStyleVar(ImGuiStyleVar_WindowPadding, ImVec2(0.0f, 0.0f));
|
|
|
|
// ImGui::Begin("TerraVisor", nullptr, window_flags);
|
|
|
|
// ImGui::PopStyleVar(3);
|
|
|
|
|
|
|
|
// // Submit the DockSpace
|
|
|
|
// if (io.ConfigFlags & ImGuiConfigFlags_DockingEnable) {
|
|
|
|
// ImGuiID dockspace_id = ImGui::GetID("MyDockSpace");
|
|
|
|
// ImGui::DockSpace(dockspace_id, ImVec2(0.0f, 0.0f), dockspace_flags);
|
|
|
|
// }
|
|
|
|
|
|
|
|
// if (ImGui::BeginMenuBar()) {
|
|
|
|
// if (ImGui::BeginMenu("Options")) {
|
|
|
|
// ImGui::Text("Nothing Here Yet, Check Back Later!");
|
|
|
|
|
|
|
|
// ImGui::EndMenu();
|
|
|
|
// }
|
|
|
|
|
|
|
|
// ImGui::EndMenuBar();
|
|
|
|
// }
|
|
|
|
|
|
|
|
// ImGui::End();
|
|
|
|
|
|
|
|
// //Draw Gui
|
|
|
|
// ImGui::Begin("Terrain Controls");
|
|
|
|
// if (ImGui::Button("Quit")) {
|
|
|
|
// glfwSetWindowShouldClose(window, GLFW_TRUE);
|
|
|
|
// }
|
|
|
|
|
|
|
|
// ImGui::Text("Application average %.3f ms/frame (%.1f FPS)", 1000.0f / ImGui::GetIO().Framerate, ImGui::GetIO().Framerate);
|
|
|
|
// ImGui::End();
|
|
|
|
|
|
|
|
// ImGui::Render();
|
|
|
|
// ImGui_ImplOpenGL3_RenderDrawData(ImGui::GetDrawData());
|
|
|
|
// }
|
|
|
|
|
|
|
|
// void Idle() {
|
|
|
|
// // Idle function to update the scene when no rendering is performed.
|
|
|
|
// }
|
|
|
|
|
|
|
|
// void ReloadShader()
|
|
|
|
// {
|
|
|
|
// GLuint new_shader = InitShader(kVertexShader.c_str(), kFragmentShader.c_str());
|
|
|
|
|
|
|
|
// if (new_shader == -1) { // loading failed
|
|
|
|
// DEBUG_BREAK(); //alert user by breaking and showing debugger
|
|
|
|
// glClearColor(1.0f, 0.0f, 1.0f, 0.0f); //change clear color if shader can't be compiled
|
|
|
|
// }
|
|
|
|
// else {
|
|
|
|
// glClearColor(0.35f, 0.35f, 0.35f, 0.0f);
|
|
|
|
|
|
|
|
// if (shader_program != -1) {
|
|
|
|
// glDeleteProgram(shader_program);
|
|
|
|
// }
|
|
|
|
// shader_program = new_shader;
|
|
|
|
// }
|
|
|
|
// }
|
|
|
|
|
|
|
|
// void Init() {
|
|
|
|
// glewInit();
|
|
|
|
|
|
|
|
// glEnable(GL_DEPTH_TEST);
|
|
|
|
// glEnable(GL_CULL_FACE);
|
|
|
|
|
|
|
|
// InitTri();
|
|
|
|
|
|
|
|
// ReloadShader();
|
|
|
|
|
|
|
|
// camera::UpdateP();
|
|
|
|
// Uniforms::Init();
|
|
|
|
// }
|
|
|
|
|
|
|
|
// } // namespace scene
|