Starting normal maps.
This commit is contained in:
parent
7ca9302834
commit
e13e94ecce
146 changed files with 16471 additions and 0 deletions
161
43-normal-maps/src/main.cpp
Normal file
161
43-normal-maps/src/main.cpp
Normal file
|
|
@ -0,0 +1,161 @@
|
|||
#define _USE_MATH_DEFINES
|
||||
#include <stb_image.h>
|
||||
#include "scene.hpp"
|
||||
#include "utils.hpp"
|
||||
#include <print>
|
||||
#include <format>
|
||||
#include "text.hpp"
|
||||
|
||||
void framebuffer_size_callback(GLFWwindow *window, int width, int height);
|
||||
|
||||
std::string text_content{""};
|
||||
|
||||
void init_glfw() {
|
||||
glfwInit();
|
||||
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
|
||||
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
|
||||
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
|
||||
|
||||
#ifdef __APPLE__
|
||||
glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
|
||||
#endif
|
||||
}
|
||||
|
||||
void framebuffer_size_callback(GLFWwindow *, int width, int height) {
|
||||
glViewport(0, 0, width, height);
|
||||
}
|
||||
|
||||
void mouse_callback(GLFWwindow *window, double xpos, double ypos) {
|
||||
Scene* scene = static_cast<Scene*>(glfwGetWindowUserPointer(window));
|
||||
scene->camera.mouse_move(xpos, ypos);
|
||||
}
|
||||
|
||||
void scroll_callback(GLFWwindow *window, double xoffset, double yoffset) {
|
||||
Scene* scene = static_cast<Scene*>(glfwGetWindowUserPointer(window));
|
||||
scene->camera.mouse_scroll(xoffset, yoffset);
|
||||
}
|
||||
|
||||
GLFWwindow* create_window() {
|
||||
GLFWwindow* window = glfwCreateWindow(SCR_WIDTH, SCR_HEIGHT, "LearnOpenGL", NULL, NULL);
|
||||
dbc::check(window != NULL, "failed to open window");
|
||||
|
||||
glfwMakeContextCurrent(window);
|
||||
|
||||
glfwSetFramebufferSizeCallback(window, framebuffer_size_callback);
|
||||
|
||||
auto good = gladLoadGLLoader((GLADloadproc)glfwGetProcAddress);
|
||||
dbc::check(good, "failed to load GLAD");
|
||||
|
||||
glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_DISABLED);
|
||||
glfwSetCursorPosCallback(window, mouse_callback);
|
||||
glfwSetScrollCallback(window, scroll_callback);
|
||||
|
||||
return window;
|
||||
}
|
||||
|
||||
void process_input(GLFWwindow *window, std::shared_ptr<Scene> scene) {
|
||||
if(glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_PRESS) {
|
||||
glfwSetWindowShouldClose(window, true);
|
||||
}
|
||||
|
||||
if(glfwGetKey(window, GLFW_KEY_W) == GLFW_PRESS) {
|
||||
scene->camera.forward();
|
||||
}
|
||||
|
||||
if(glfwGetKey(window, GLFW_KEY_S) == GLFW_PRESS) {
|
||||
scene->camera.back();
|
||||
}
|
||||
|
||||
if(glfwGetKey(window, GLFW_KEY_A) == GLFW_PRESS) {
|
||||
scene->camera.left();
|
||||
}
|
||||
|
||||
if(glfwGetKey(window, GLFW_KEY_D) == GLFW_PRESS) {
|
||||
scene->camera.right();
|
||||
}
|
||||
|
||||
if(glfwGetKey(window, GLFW_KEY_R) == GLFW_PRESS) {
|
||||
scene->reflect_on = !scene->reflect_on;
|
||||
}
|
||||
|
||||
if(glfwGetKey(window, GLFW_KEY_J) == GLFW_PRESS) {
|
||||
scene->debug_on = !scene->debug_on;
|
||||
}
|
||||
|
||||
if(glfwGetKey(window, GLFW_KEY_H) == GLFW_PRESS) {
|
||||
scene->spawn("human");
|
||||
glm::vec3 pos = scene->camera.position;
|
||||
text_content = std::format("Spawn human at {},{},{}", pos.x, pos.y, pos.z);
|
||||
}
|
||||
|
||||
if(glfwGetKey(window, GLFW_KEY_L) == GLFW_PRESS) {
|
||||
scene->light.camera.on = !scene->light.camera.on;
|
||||
scene->shader.apply_lighting(scene->light);
|
||||
scene->shadows_rendered = false;
|
||||
}
|
||||
|
||||
if(glfwGetKey(window, GLFW_KEY_O) == GLFW_PRESS) {
|
||||
// BUG: this assumes the old shader with different light so broken
|
||||
scene->light.directional.adjust(-0.01f);
|
||||
scene->shader.apply_lighting(scene->light);
|
||||
scene->shadows_rendered = false;
|
||||
}
|
||||
|
||||
if(glfwGetKey(window, GLFW_KEY_P) == GLFW_PRESS) {
|
||||
// BUG: this assumes the old shader with different light so broken
|
||||
scene->light.directional.adjust(0.01f);
|
||||
scene->shader.apply_lighting(scene->light);
|
||||
scene->shadows_rendered = false;
|
||||
}
|
||||
}
|
||||
|
||||
std::shared_ptr<Scene> setup() {
|
||||
glEnable(GL_DEPTH_TEST);
|
||||
glEnable(GL_BLEND);
|
||||
glDisable(GL_CULL_FACE);
|
||||
glEnable(GL_MULTISAMPLE);
|
||||
|
||||
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
|
||||
|
||||
glDepthFunc(GL_LESS);
|
||||
|
||||
components::Scene config = utils::load_scene_config("config.json");
|
||||
|
||||
return std::make_shared<Scene>(config);
|
||||
}
|
||||
|
||||
void update(GLFWwindow* window, std::shared_ptr<Scene> scene) {
|
||||
glfwPollEvents();
|
||||
process_input(window, scene);
|
||||
scene->update();
|
||||
}
|
||||
|
||||
void render(GLFWwindow* window, std::shared_ptr<Scene> scene, Text &text) {
|
||||
scene->render();
|
||||
text.render(text_content, 25.0f, 25.0f, 1.0f, glm::vec3(1.0, 0.0f, 0.0f));
|
||||
glfwSwapBuffers(window);
|
||||
}
|
||||
|
||||
void quit(GLFWwindow* window, std::shared_ptr<Scene> scene) {
|
||||
scene->cleanup();
|
||||
glfwTerminate();
|
||||
}
|
||||
|
||||
int main() {
|
||||
init_glfw();
|
||||
|
||||
auto window = create_window();
|
||||
std::shared_ptr<Scene> scene = setup();
|
||||
Text text;
|
||||
|
||||
glfwSetWindowUserPointer(window, scene.get());
|
||||
|
||||
while(!glfwWindowShouldClose(window)) {
|
||||
update(window, scene);
|
||||
render(window, scene, text);
|
||||
}
|
||||
|
||||
quit(window, scene);
|
||||
|
||||
return 0;
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue