Get callbacks to work with Scene class

This commit is contained in:
Jack Christensen 2024-08-18 22:26:33 -04:00
parent dcc610b245
commit c8e19a6d12
4 changed files with 30 additions and 13 deletions

View File

@ -1,10 +1,15 @@
#pragma once #pragma once
#include <GLFW/glfw3.h> #include <GLFW/glfw3.h>
#include <iostream>
#include "scene.h"
namespace Callbacks namespace Callbacks
{ {
void Register(GLFWwindow* window); extern Scene* scene_;
void Register(GLFWwindow* window, Scene* scene);
void Keyboard(GLFWwindow* window, int key, int scancode, int action, int mods); void Keyboard(GLFWwindow* window, int key, int scancode, int action, int mods);
void MouseCursor(GLFWwindow* window, double x, double y); void MouseCursor(GLFWwindow* window, double x, double y);

View File

@ -1,10 +1,16 @@
#include "callbacks.h"
#include "platform_utils.h"
#include "scene.h"
#include <glm/glm.hpp> #include <glm/glm.hpp>
void Callbacks::Register(GLFWwindow* window) #include "callbacks.h"
//#include "platform_utils.h"
namespace Callbacks {
Scene* scene_ = nullptr;
};
void Callbacks::Register(GLFWwindow* window, Scene* scene)
{ {
scene_ = scene;
glfwSetKeyCallback(window, Keyboard); glfwSetKeyCallback(window, Keyboard);
glfwSetCursorPosCallback(window, MouseCursor); glfwSetCursorPosCallback(window, MouseCursor);
glfwSetMouseButtonCallback(window, MouseButton); glfwSetMouseButtonCallback(window, MouseButton);
@ -22,9 +28,10 @@ void Callbacks::Keyboard(GLFWwindow* window, int key, int scancode, int action,
{ {
case 'r': case 'r':
case 'R': case 'R':
//Scene::ReloadShader(); if (scene_) {
scene_->ReloadShader();
}
break; break;
case GLFW_KEY_ESCAPE: case GLFW_KEY_ESCAPE:
glfwSetWindowShouldClose(window, GLFW_TRUE); glfwSetWindowShouldClose(window, GLFW_TRUE);
break; break;

View File

@ -128,6 +128,7 @@ int main() {
if (!InitializeGlew()) return -1; if (!InitializeGlew()) return -1;
Scene scene(kInitWindowWidth, kInitWindowHeight); Scene scene(kInitWindowWidth, kInitWindowHeight);
Callbacks::Register(window, &scene);
scene.Init(); scene.Init();
InitializeImGui(window); InitializeImGui(window);

View File

@ -128,7 +128,7 @@ void Scene::GenerateGrid(int divisions, std::vector<Vertex>& verts, std::vector<
// Currently creates a test triangle and initializes its buffers // Currently creates a test triangle and initializes its buffers
void Scene::InitBuffers() { void Scene::InitBuffers() {
int divisions = 1 + (tessellationFactor / 64); // Number of divisions along one axis of the grid int divisions = 16; // Number of divisions along one axis of the grid
GenerateGrid(divisions, vertices, indices); GenerateGrid(divisions, vertices, indices);
// Create and bind VAO, VBO, and EBO, and pass the data to OpenGL // Create and bind VAO, VBO, and EBO, and pass the data to OpenGL
@ -189,20 +189,24 @@ void Scene::InitQuadBuffers() {
// Allows for runtime shader updates // Allows for runtime shader updates
void Scene::ReloadShader() { void Scene::ReloadShader() {
GLuint new_shader = InitShader(kVertexShaderPath.c_str(), kTessellationCtrlPath.c_str(), kTessellationEvalPath.c_str(), kFragmentShaderPath.c_str()); GLuint geo_shader = InitShader(kVertexShaderPath.c_str(), kTessellationCtrlPath.c_str(), kTessellationEvalPath.c_str(), kFragmentShaderPath.c_str());
if (new_shader == -1) { GLuint lht_shader = InitShader(kQuadVertexPath.c_str(), kQuadFragmentPath.c_str());
if (geo_shader == -1 || lht_shader == -1) {
DEBUG_BREAK(); DEBUG_BREAK();
glClearColor(1.0f, 0.0f, 1.0f, 0.0f); glClearColor(1.0f, 0.0f, 1.0f, 0.0f);
} else { } else {
glClearColor(0.35f, 0.35f, 0.35f, 0.0f); glClearColor(0.35f, 0.35f, 0.35f, 0.0f);
if (shader_program_ != -1) { if (shader_program_ != -1 && quad_shader_program_ != -1) {
glDeleteProgram(shader_program_); glDeleteProgram(shader_program_);
glDeleteProgram(quad_shader_program_);
} }
shader_program_ = new_shader; shader_program_ = geo_shader;
quad_shader_program_ = lht_shader;
} }
} }
void Scene::Display(GLFWwindow* window) { void Scene::Display(GLFWwindow* window) {
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
fbo_.Bind(); fbo_.Bind();
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
@ -210,7 +214,7 @@ void Scene::Display(GLFWwindow* window) {
//glPolygonMode(GL_FRONT_AND_BACK, GL_LINE); //glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
view_matrix_ = glm::lookAt(glm::vec3(0.0f, 1.4f, -1.4f), glm::vec3(0.0f, 0.0f, 0.0f), glm::vec3(0.0f, 1.0f, 0.0f)); view_matrix_ = glm::lookAt(glm::vec3(0.0f, 1.4f, 1.4f), 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 Uniforms::SceneData.PV = projection_matrix_ * view_matrix_; // Projection-View matrix
Uniforms::BufferSceneData(); Uniforms::BufferSceneData();