everything
This commit is contained in:
parent
79fa59c3aa
commit
8998bc3936
19 changed files with 855 additions and 7504 deletions
268
main.cpp
268
main.cpp
|
|
@ -3,15 +3,13 @@
|
|||
#include <iostream>
|
||||
|
||||
#include <glad/glad.h>
|
||||
#include <GLFW/glfw3.h>
|
||||
#include <glfw/include/GLFW/glfw3.h>
|
||||
#include <glm/glm.hpp>
|
||||
#include <glm/gtc/matrix_transform.hpp>
|
||||
#include <glm/gtc/type_ptr.hpp>
|
||||
#include "stb/stb_easy_font.h"
|
||||
|
||||
#include <engine/mesh.h>
|
||||
#include <engine/shader.h>
|
||||
#include <engine/texture.h>
|
||||
#include <engine/model.h>
|
||||
#include <engine/camera.h>
|
||||
|
||||
void window_resize_callback(GLFWwindow* window, int width, int height);
|
||||
|
|
@ -19,11 +17,12 @@ void mouse_callback(GLFWwindow* window, double xpos, double ypos);
|
|||
void scroll_callback(GLFWwindow* window, double xoffset, double yoffset);
|
||||
void processInput(GLFWwindow *window);
|
||||
|
||||
const int SCREEN_WIDTH = 1920;
|
||||
const int SCREEN_HEIGHT = 1080;
|
||||
int SCREEN_WIDTH = 1920;
|
||||
int SCREEN_HEIGHT = 1080;
|
||||
|
||||
static bool drawWireframe = false;
|
||||
static bool wireframeHeld = false;
|
||||
static bool flashlight = true;
|
||||
|
||||
Camera camera(glm::vec3(0.0f, 0.0f, 3.0f));
|
||||
float lastX = SCREEN_WIDTH / 2.0f;
|
||||
|
|
@ -65,6 +64,8 @@ int main(int argc, char* argv[])
|
|||
glfwSetCursorPosCallback(window, mouse_callback);
|
||||
glfwSetScrollCallback(window, scroll_callback);
|
||||
|
||||
glEnable(GL_DEPTH_TEST);
|
||||
|
||||
// Hide & Lock Cursor
|
||||
glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_DISABLED);
|
||||
glfwSetInputMode(window, GLFW_RAW_MOUSE_MOTION, GLFW_TRUE);
|
||||
|
|
@ -73,145 +74,167 @@ int main(int argc, char* argv[])
|
|||
Testing
|
||||
*/
|
||||
|
||||
// Load Shader
|
||||
std::vector<Vertex> vertices = {
|
||||
// Front face (-z) – normal: (0, 0, -1)
|
||||
{ -0.5f, -0.5f, -0.5f, 0.0f, 0.0f, -1.0f, 0.0f, 0.0f },
|
||||
{ 0.5f, -0.5f, -0.5f, 0.0f, 0.0f, -1.0f, 1.0f, 0.0f },
|
||||
{ 0.5f, 0.5f, -0.5f, 0.0f, 0.0f, -1.0f, 1.0f, 1.0f },
|
||||
{ 0.5f, 0.5f, -0.5f, 0.0f, 0.0f, -1.0f, 1.0f, 1.0f },
|
||||
{ -0.5f, 0.5f, -0.5f, 0.0f, 0.0f, -1.0f, 0.0f, 1.0f },
|
||||
{ -0.5f, -0.5f, -0.5f, 0.0f, 0.0f, -1.0f, 0.0f, 0.0f },
|
||||
|
||||
// Back face (+z) – normal: (0, 0, +1)
|
||||
{ -0.5f, -0.5f, 0.5f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f },
|
||||
{ 0.5f, -0.5f, 0.5f, 0.0f, 0.0f, 1.0f, 1.0f, 0.0f },
|
||||
{ 0.5f, 0.5f, 0.5f, 0.0f, 0.0f, 1.0f, 1.0f, 1.0f },
|
||||
{ 0.5f, 0.5f, 0.5f, 0.0f, 0.0f, 1.0f, 1.0f, 1.0f },
|
||||
{ -0.5f, 0.5f, 0.5f, 0.0f, 0.0f, 1.0f, 0.0f, 1.0f },
|
||||
{ -0.5f, -0.5f, 0.5f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f },
|
||||
|
||||
// Left face (-x) – normal: (-1, 0, 0)
|
||||
{ -0.5f, 0.5f, 0.5f, -1.0f, 0.0f, 0.0f, 1.0f, 0.0f },
|
||||
{ -0.5f, 0.5f, -0.5f, -1.0f, 0.0f, 0.0f, 1.0f, 1.0f },
|
||||
{ -0.5f, -0.5f, -0.5f, -1.0f, 0.0f, 0.0f, 0.0f, 1.0f },
|
||||
{ -0.5f, -0.5f, -0.5f, -1.0f, 0.0f, 0.0f, 0.0f, 1.0f },
|
||||
{ -0.5f, -0.5f, 0.5f, -1.0f, 0.0f, 0.0f, 0.0f, 0.0f },
|
||||
{ -0.5f, 0.5f, 0.5f, -1.0f, 0.0f, 0.0f, 1.0f, 0.0f },
|
||||
|
||||
// Right face (+x) – normal: (+1, 0, 0)
|
||||
{ 0.5f, 0.5f, 0.5f, 1.0f, 0.0f, 0.0f, 1.0f, 0.0f },
|
||||
{ 0.5f, 0.5f, -0.5f, 1.0f, 0.0f, 0.0f, 1.0f, 1.0f },
|
||||
{ 0.5f, -0.5f, -0.5f, 1.0f, 0.0f, 0.0f, 0.0f, 1.0f },
|
||||
{ 0.5f, -0.5f, -0.5f, 1.0f, 0.0f, 0.0f, 0.0f, 1.0f },
|
||||
{ 0.5f, -0.5f, 0.5f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f },
|
||||
{ 0.5f, 0.5f, 0.5f, 1.0f, 0.0f, 0.0f, 1.0f, 0.0f },
|
||||
|
||||
// Bottom face (-y) – normal: (0, -1, 0)
|
||||
{ -0.5f, -0.5f, -0.5f, 0.0f, -1.0f, 0.0f, 0.0f, 1.0f },
|
||||
{ 0.5f, -0.5f, -0.5f, 0.0f, -1.0f, 0.0f, 1.0f, 1.0f },
|
||||
{ 0.5f, -0.5f, 0.5f, 0.0f, -1.0f, 0.0f, 1.0f, 0.0f },
|
||||
{ 0.5f, -0.5f, 0.5f, 0.0f, -1.0f, 0.0f, 1.0f, 0.0f },
|
||||
{ -0.5f, -0.5f, 0.5f, 0.0f, -1.0f, 0.0f, 0.0f, 0.0f },
|
||||
{ -0.5f, -0.5f, -0.5f, 0.0f, -1.0f, 0.0f, 0.0f, 1.0f },
|
||||
|
||||
// Top face (+y) – normal: (0, +1, 0)
|
||||
{ -0.5f, 0.5f, -0.5f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f },
|
||||
{ 0.5f, 0.5f, -0.5f, 0.0f, 1.0f, 0.0f, 1.0f, 1.0f },
|
||||
{ 0.5f, 0.5f, 0.5f, 0.0f, 1.0f, 0.0f, 1.0f, 0.0f },
|
||||
{ 0.5f, 0.5f, 0.5f, 0.0f, 1.0f, 0.0f, 1.0f, 0.0f },
|
||||
{ -0.5f, 0.5f, 0.5f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f },
|
||||
{ -0.5f, 0.5f, -0.5f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f }
|
||||
};
|
||||
std::vector<unsigned int> indices = {
|
||||
0, 1, 2, 2, 4, 0,
|
||||
6, 7, 8, 8, 10, 6,
|
||||
12, 13, 14, 14, 16, 12,
|
||||
18, 19, 20, 20, 22, 18,
|
||||
24, 25, 26, 26, 28, 24,
|
||||
30, 31, 32, 32, 34, 30
|
||||
};
|
||||
|
||||
Mesh testMesh(vertices, indices);
|
||||
|
||||
Texture texture1("assets/wall.jpg");
|
||||
Texture texture2("assets/awesomeface.png", true);
|
||||
|
||||
Shader shaderTest("assets/shaders/basicVertex.vert", "assets/shaders/basicFragment.frag");
|
||||
shaderTest.Use(); // don't forget to activate/use the shader before setting uniforms!
|
||||
shaderTest.setInt("ourTexture", 0);
|
||||
shaderTest.setInt("decal", 1);
|
||||
|
||||
Shader lightingShader("assets/shaders/basicVertex.vert", "assets/shaders/lightFragment.frag");
|
||||
Mesh lightMesh(vertices, indices);
|
||||
Model ourModel("assets/models/sponza/Sponza.gltf");
|
||||
|
||||
shaderTest.Use();
|
||||
shaderTest.setInt("ourTexture", 0);
|
||||
shaderTest.setInt("material.diffuse", 0);
|
||||
shaderTest.setInt("material.specular", 1);
|
||||
|
||||
// Set decals - TODO: Shift this to mesh.h with helpers
|
||||
shaderTest.setDecal(0, 2, 1, glm::vec4(0.5, 0.5, 0, 0));
|
||||
shaderTest.setDecal(1, 2, 1, glm::vec4(0.5, 0.5, -0.75, -0.75));
|
||||
shaderTest.setInt("numberOfDecals", 2);
|
||||
|
||||
shaderTest.setInt("numberOfPointLights", 1);
|
||||
shaderTest.setInt("numberOfSpotLights", 1);
|
||||
|
||||
glm::vec3 pointLightPositions[] = {
|
||||
glm::vec3( 0.7f, 0.2f, 2.0f),
|
||||
glm::vec3( 2.3f, -3.3f, -4.0f),
|
||||
glm::vec3(-4.0f, 2.0f, -12.0f),
|
||||
glm::vec3( 0.0f, 0.0f, -3.0f)
|
||||
};
|
||||
|
||||
// Render Loop
|
||||
float lastUpdate = 0;
|
||||
glm::vec3 lightPos = glm::vec3(0.0f, 0.0f, 0.0f);
|
||||
glm::vec3 orbitCenter = glm::vec3(0.0f, 0.0f, 0.0f);
|
||||
while (!glfwWindowShouldClose(window))
|
||||
{
|
||||
// per-frame time logic
|
||||
// --------------------
|
||||
float currentFrame = static_cast<float>(glfwGetTime());
|
||||
deltaTime = currentFrame - lastFrame;
|
||||
lastFrame = currentFrame;
|
||||
|
||||
// crappy fps printout
|
||||
if (currentFrame - lastUpdate >= 1.0) {
|
||||
std::cout << 1.0f / deltaTime << std::endl;
|
||||
// std::cout << 1.0f / deltaTime << std::endl;
|
||||
lastUpdate += 1.0f;
|
||||
}
|
||||
|
||||
// Input processing
|
||||
processInput(window);
|
||||
|
||||
// Rendering
|
||||
glClearColor(0.2f, 0.3f, 0.3f, 1.0f);
|
||||
glClearColor(0, 0, 0, 1);
|
||||
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
|
||||
glEnable(GL_DEPTH_TEST);
|
||||
|
||||
glActiveTexture(GL_TEXTURE0);
|
||||
glBindTexture(GL_TEXTURE_2D, texture1.ID);
|
||||
glActiveTexture(GL_TEXTURE1);
|
||||
glBindTexture(GL_TEXTURE_2D, texture2.ID);
|
||||
|
||||
shaderTest.Use();
|
||||
shaderTest.setVec3("lightColor", 1.0f, 1.0f, 1.0f);
|
||||
shaderTest.setVec3("lightPos", orbitCenter);
|
||||
shaderTest.setVec3("viewPos", camera.position);
|
||||
|
||||
// pass projection matrix to shader (note that in this case it could change every frame)
|
||||
glm::mat4 projection = glm::perspective(glm::radians(camera.zoom), (float)SCREEN_WIDTH / (float)SCREEN_HEIGHT, 0.1f, 100.0f);
|
||||
shaderTest.setMat4("projection", projection);
|
||||
camera.Update(shaderTest, SCREEN_WIDTH, SCREEN_HEIGHT);
|
||||
|
||||
// camera/view transformation
|
||||
glm::mat4 view = camera.GetViewMatrix();
|
||||
shaderTest.setMat4("view", view);
|
||||
shaderTest.setFloat("material.shininess", 32.0f);
|
||||
|
||||
shaderTest.setVec3("dirLight.direction", -0.2f, -1.0f, -0.3f);
|
||||
shaderTest.setVec3("dirLight.ambient", 0.05f, 0.05f, 0.05f);
|
||||
shaderTest.setVec3("dirLight.diffuse", 0.8f, 0.8f, 0.8f);
|
||||
shaderTest.setVec3("dirLight.specular", 0.8f, 0.8f, 0.8f);
|
||||
|
||||
// -----------------------------------------------------------------------------
|
||||
// Ground Floor - Right Corridor (Warm Light)
|
||||
// Placed halfway down the right hallway behind the arches
|
||||
// -----------------------------------------------------------------------------
|
||||
shaderTest.setVec3("pointLights[0].position", -1.88f, 8.45f, -0.98f);
|
||||
shaderTest.setVec3("pointLights[0].ambient", 0.05f, 0.05f, 0.05f);
|
||||
shaderTest.setVec3("pointLights[0].diffuse", 1.0f, 0.7f, 0.4f);
|
||||
shaderTest.setVec3("pointLights[0].specular", 1.0f, 1.0f, 1.0f);
|
||||
shaderTest.setFloat("pointLights[0].constant", 1.0f);
|
||||
shaderTest.setFloat("pointLights[0].linear", 0.09f);
|
||||
shaderTest.setFloat("pointLights[0].quadratic", 0.032f);
|
||||
|
||||
// -----------------------------------------------------------------------------
|
||||
// Ground Floor - Right Corridor, opposite end (Warm Light)
|
||||
// -----------------------------------------------------------------------------
|
||||
shaderTest.setVec3("pointLights[1].position", -15.0f, 2.5f, 7.5f);
|
||||
shaderTest.setVec3("pointLights[1].ambient", 0.05f, 0.05f, 0.05f);
|
||||
shaderTest.setVec3("pointLights[1].diffuse", 1.0f, 0.7f, 0.4f);
|
||||
shaderTest.setVec3("pointLights[1].specular", 1.0f, 1.0f, 1.0f);
|
||||
shaderTest.setFloat("pointLights[1].constant", 1.0f);
|
||||
shaderTest.setFloat("pointLights[1].linear", 0.09f);
|
||||
shaderTest.setFloat("pointLights[1].quadratic", 0.032f);
|
||||
|
||||
// -----------------------------------------------------------------------------
|
||||
// Ground Floor - Left Corridor (Warm Light)
|
||||
// Placed halfway down the left hallway behind the arches
|
||||
// -----------------------------------------------------------------------------
|
||||
shaderTest.setVec3("pointLights[2].position", 15.0f, 2.5f, -7.5f);
|
||||
shaderTest.setVec3("pointLights[2].ambient", 0.05f, 0.05f, 0.05f);
|
||||
shaderTest.setVec3("pointLights[2].diffuse", 1.0f, 0.7f, 0.4f);
|
||||
shaderTest.setVec3("pointLights[2].specular", 1.0f, 1.0f, 1.0f);
|
||||
shaderTest.setFloat("pointLights[2].constant", 1.0f);
|
||||
shaderTest.setFloat("pointLights[2].linear", 0.09f);
|
||||
shaderTest.setFloat("pointLights[2].quadratic", 0.032f);
|
||||
|
||||
// -----------------------------------------------------------------------------
|
||||
// Ground Floor - Left Corridor, opposite end (Warm Light)
|
||||
// -----------------------------------------------------------------------------
|
||||
shaderTest.setVec3("pointLights[3].position", -15.0f, 2.5f, -7.5f);
|
||||
shaderTest.setVec3("pointLights[3].ambient", 0.05f, 0.05f, 0.05f);
|
||||
shaderTest.setVec3("pointLights[3].diffuse", 1.0f, 0.7f, 0.4f);
|
||||
shaderTest.setVec3("pointLights[3].specular", 1.0f, 1.0f, 1.0f);
|
||||
shaderTest.setFloat("pointLights[3].constant", 1.0f);
|
||||
shaderTest.setFloat("pointLights[3].linear", 0.09f);
|
||||
shaderTest.setFloat("pointLights[3].quadratic", 0.032f);
|
||||
|
||||
// -----------------------------------------------------------------------------
|
||||
// Upper Balcony - East Atrium Edge (Cool Light)
|
||||
// Hovering above the balcony floor, shining into the main atrium
|
||||
// -----------------------------------------------------------------------------
|
||||
shaderTest.setVec3("pointLights[4].position", 22.0f, 9.0f, 0.0f);
|
||||
shaderTest.setVec3("pointLights[4].ambient", 0.05f, 0.05f, 0.05f);
|
||||
shaderTest.setVec3("pointLights[4].diffuse", 0.7f, 0.8f, 1.0f);
|
||||
shaderTest.setVec3("pointLights[4].specular", 1.0f, 1.0f, 1.0f);
|
||||
shaderTest.setFloat("pointLights[4].constant", 1.0f);
|
||||
shaderTest.setFloat("pointLights[4].linear", 0.09f);
|
||||
shaderTest.setFloat("pointLights[4].quadratic", 0.032f);
|
||||
|
||||
// -----------------------------------------------------------------------------
|
||||
// Upper Balcony - West Atrium Edge (Cool Light)
|
||||
// Hovering above the balcony floor, shining into the main atrium
|
||||
// -----------------------------------------------------------------------------
|
||||
shaderTest.setVec3("pointLights[5].position", -22.0f, 9.0f, 0.0f);
|
||||
shaderTest.setVec3("pointLights[5].ambient", 0.05f, 0.05f, 0.05f);
|
||||
shaderTest.setVec3("pointLights[5].diffuse", 0.7f, 0.8f, 1.0f);
|
||||
shaderTest.setVec3("pointLights[5].specular", 1.0f, 1.0f, 1.0f);
|
||||
shaderTest.setFloat("pointLights[5].constant", 1.0f);
|
||||
shaderTest.setFloat("pointLights[5].linear", 0.09f);
|
||||
shaderTest.setFloat("pointLights[5].quadratic", 0.032f);
|
||||
|
||||
// spotLight1
|
||||
shaderTest.setBool("spotLights[0].disabled", flashlight);
|
||||
shaderTest.setVec3("spotLights[0].position", camera.position);
|
||||
shaderTest.setVec3("spotLights[0].direction", camera.front);
|
||||
shaderTest.setVec3("spotLights[0].ambient", 0.0f, 0.0f, 0.0f);
|
||||
shaderTest.setVec3("spotLights[0].diffuse", 1.0f, 1.0f, 1.0f);
|
||||
shaderTest.setVec3("spotLights[0].specular", 1.0f, 1.0f, 1.0f);
|
||||
shaderTest.setFloat("spotLights[0].constant", 1.0f);
|
||||
shaderTest.setFloat("spotLights[0].linear", 0.09f);
|
||||
shaderTest.setFloat("spotLights[0].quadratic", 0.032f);
|
||||
shaderTest.setFloat("spotLights[0].cutOff", glm::cos(glm::radians(12.5f)));
|
||||
shaderTest.setFloat("spotLights[0].outerCutOff", glm::cos(glm::radians(15.0f)));
|
||||
// spotLight2
|
||||
shaderTest.setVec3("spotLights[1].position", -1.5441f, -0.294683f, 0.211498f);
|
||||
shaderTest.setVec3("spotLights[1].direction", 1, 0, 0);
|
||||
shaderTest.setVec3("spotLights[1].ambient", 0.0f, 0.0f, 0.0f);
|
||||
shaderTest.setVec3("spotLights[1].diffuse", 1.0f, 1.0f, 1.0f);
|
||||
shaderTest.setVec3("spotLights[1].specular", 1.0f, 1.0f, 1.0f);
|
||||
shaderTest.setFloat("spotLights[1].constant", 1.0f);
|
||||
shaderTest.setFloat("spotLights[1].linear", 0.09f);
|
||||
shaderTest.setFloat("spotLights[1].quadratic", 0.032f);
|
||||
shaderTest.setFloat("spotLights[1].cutOff", glm::cos(glm::radians(12.5f)));
|
||||
shaderTest.setFloat("spotLights[1].outerCutOff", glm::cos(glm::radians(15.0f)));
|
||||
|
||||
// render the loaded model
|
||||
glm::mat4 model = glm::mat4(1.0f);
|
||||
model = glm::translate(model, glm::vec3(0.0f, 0.0f, 0.0f));
|
||||
model = glm::scale(model, glm::vec3(0.02f, 0.02f, 0.02f));
|
||||
shaderTest.setMat4("model", model);
|
||||
ourModel.Draw(shaderTest);
|
||||
|
||||
testMesh.Draw();
|
||||
|
||||
|
||||
lightingShader.Use();
|
||||
lightingShader.setMat4("projection", projection);
|
||||
lightingShader.setMat4("view", view);
|
||||
model = glm::mat4(1.0f);
|
||||
|
||||
glm::vec3 offset = glm::vec3(
|
||||
2.0f * cos(glfwGetTime() * 1.0f),
|
||||
0.0f,
|
||||
sin(glfwGetTime() * 1.0f) * 2.0f
|
||||
);
|
||||
orbitCenter = lightPos + offset;
|
||||
model = glm::translate(model, orbitCenter);
|
||||
|
||||
model = glm::scale(model, glm::vec3(0.2f)); // a smaller cube
|
||||
lightingShader.setMat4("model", model);
|
||||
|
||||
lightMesh.Draw();
|
||||
std::cout << "start" << std::endl;
|
||||
std::cout << camera.position.x << std::endl;
|
||||
std::cout << camera.position.y << std::endl;
|
||||
std::cout << camera.position.z << std::endl;
|
||||
|
||||
// Call events & swap buffers
|
||||
glfwSwapBuffers(window);
|
||||
|
|
@ -227,6 +250,8 @@ int main(int argc, char* argv[])
|
|||
void window_resize_callback(GLFWwindow* window, int width, int height)
|
||||
{
|
||||
glViewport(0, 0, width, height);
|
||||
SCREEN_WIDTH = width;
|
||||
SCREEN_HEIGHT = height;
|
||||
}
|
||||
|
||||
void processInput(GLFWwindow* window)
|
||||
|
|
@ -252,6 +277,15 @@ void processInput(GLFWwindow* window)
|
|||
if (glfwGetKey(window, GLFW_KEY_F1) == GLFW_RELEASE)
|
||||
wireframeHeld = false;
|
||||
|
||||
// Flashlight toggle
|
||||
if (glfwGetKey(window, GLFW_KEY_F) == GLFW_PRESS && !wireframeHeld)
|
||||
{
|
||||
wireframeHeld = true;
|
||||
flashlight = !flashlight;
|
||||
}
|
||||
if (glfwGetKey(window, GLFW_KEY_F) == GLFW_RELEASE)
|
||||
wireframeHeld = false;
|
||||
|
||||
// Camera
|
||||
if (glfwGetKey(window, GLFW_KEY_W) == GLFW_PRESS)
|
||||
camera.processKeyboard(FORWARD, deltaTime);
|
||||
|
|
@ -261,6 +295,8 @@ void processInput(GLFWwindow* window)
|
|||
camera.processKeyboard(LEFT, deltaTime);
|
||||
if (glfwGetKey(window, GLFW_KEY_D) == GLFW_PRESS)
|
||||
camera.processKeyboard(RIGHT, deltaTime);
|
||||
if (glfwGetKey(window, GLFW_KEY_LEFT_SHIFT) == GLFW_PRESS)
|
||||
camera.processKeyboard(SPRINT, deltaTime);
|
||||
}
|
||||
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue