161 lines
4.6 KiB
C++
161 lines
4.6 KiB
C++
#include "camera.hpp"
|
|
#include "glad/glad.h"
|
|
#include <GLFW/glfw3.h>
|
|
#include <cassert>
|
|
#include <glm/glm.hpp>
|
|
#include <mesh.hpp>
|
|
#include <shader.hpp>
|
|
#include <texture.hpp>
|
|
|
|
#define UNUSED(x) (void)(x)
|
|
|
|
glm::vec3 processInput(GLFWwindow *window);
|
|
void framebuffer_size_callback(GLFWwindow *window, int width, int height);
|
|
void mouse_callback(GLFWwindow *window, double xpos, double ypos);
|
|
|
|
float deltaTime = 0;
|
|
|
|
GLFWwindow *init() {
|
|
glfwInit();
|
|
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
|
|
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
|
|
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
|
|
|
|
GLFWwindow *window = glfwCreateWindow(800, 600, "LearnOpenGL", NULL, NULL);
|
|
assert(window && "Window Failed");
|
|
glfwMakeContextCurrent(window);
|
|
|
|
int is_ok = 0;
|
|
is_ok = gladLoadGLLoader((GLADloadproc)glfwGetProcAddress);
|
|
assert(is_ok && "Failed to init glad");
|
|
glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_DISABLED);
|
|
|
|
glViewport(0, 0, 800, 600);
|
|
glfwSetFramebufferSizeCallback(window, framebuffer_size_callback);
|
|
glfwSetCursorPosCallback(window, mouse_callback);
|
|
return window;
|
|
}
|
|
|
|
void showFPS(GLFWwindow *window, double delta) {
|
|
char title[22];
|
|
title[21] = '\0';
|
|
snprintf(title, 20, "OpenGl FPS: [%3.2f]", (1 / delta));
|
|
glfwSetWindowTitle(window, title);
|
|
}
|
|
|
|
int main(void) {
|
|
glm::vec3 lightPos = glm::vec3(1, 2.0f, 1);
|
|
glm::vec3 lightColor = glm::vec3(1);
|
|
|
|
GLFWwindow *window = init();
|
|
Camera *cam = new Camera(glm::vec3(0), 0, 0, 45, 800.0 / 600);
|
|
glfwSetWindowUserPointer(window, cam);
|
|
|
|
Shader *cubeShader = new Shader("./shaders/lighting/Vert.glsl",
|
|
"./shaders/lighting/Frag.glsl");
|
|
Shader *lightShader = new Shader("./shaders/lighting/Vert.glsl",
|
|
"./shaders/lighting/LightFrag.glsl");
|
|
|
|
Mesh *cube = new Mesh(cubeShader, "./cube.obj");
|
|
Mesh *light = new Mesh(lightShader, "./cube.obj");
|
|
cube->setOwnedShader(true);
|
|
light->setOwnedShader(true);
|
|
|
|
light->setPosition(lightPos);
|
|
light->setScale(glm::vec3(.3));
|
|
|
|
glEnable(GL_DEPTH_TEST);
|
|
float lastFrame = 0;
|
|
while (!glfwWindowShouldClose(window)) {
|
|
float currentFrame = (float)glfwGetTime();
|
|
deltaTime = currentFrame - lastFrame;
|
|
lastFrame = currentFrame;
|
|
showFPS(window, deltaTime);
|
|
|
|
glClearColor(0.2f, 0.3f, 0.3f, 1.0f);
|
|
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
|
|
|
|
Shader *s = cube->getShader();
|
|
s->use();
|
|
s->setVec3("lightColor", lightColor);
|
|
s->setVec3("ourColor", glm::vec3(0.24725, 0.1995, 0.0745));
|
|
s->setVec3("lightPos", lightPos);
|
|
s->setVec3("viewPos", cam->getPos());
|
|
|
|
s->setVec3("light.ambient", glm::vec3(0.2));
|
|
s->setVec3("light.diffuse", glm::vec3(0.5));
|
|
s->setVec3("light.specular", glm::vec3(1));
|
|
s->setVec3("light.position", lightPos);
|
|
|
|
s->setVec3("material.ambient", glm::vec3(1.0, 0.5, 0.31));
|
|
s->setVec3("material.diffuse", glm::vec3(1.0, 0.5, 0.31));
|
|
s->setVec3("material.specular", glm::vec3(0.5));
|
|
s->setFloat("material.shininess", 32);
|
|
cube->draw(cam);
|
|
|
|
s = light->getShader();
|
|
s->use();
|
|
s->setVec3("ourColor", lightColor);
|
|
light->draw(cam);
|
|
|
|
glfwSwapBuffers(window);
|
|
glfwPollEvents();
|
|
glm::vec3 mov = processInput(window);
|
|
mov *= deltaTime;
|
|
cam->addFPos(mov);
|
|
}
|
|
|
|
delete cam;
|
|
delete cube;
|
|
delete light;
|
|
glfwTerminate();
|
|
return 0;
|
|
}
|
|
|
|
glm::vec3 processInput(GLFWwindow *window) {
|
|
glm::vec3 mov = glm::vec3(0);
|
|
if (glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_PRESS)
|
|
glfwSetWindowShouldClose(window, true);
|
|
|
|
if (glfwGetKey(window, GLFW_KEY_W) == GLFW_PRESS)
|
|
mov += glm::vec3(1, 0, 0);
|
|
if (glfwGetKey(window, GLFW_KEY_S) == GLFW_PRESS)
|
|
mov -= glm::vec3(1, 0, 0);
|
|
if (glfwGetKey(window, GLFW_KEY_D) == GLFW_PRESS)
|
|
mov += glm::vec3(0, 0, 1);
|
|
if (glfwGetKey(window, GLFW_KEY_A) == GLFW_PRESS)
|
|
mov -= glm::vec3(0, 0, 1);
|
|
if (glfwGetKey(window, GLFW_KEY_Q) == GLFW_PRESS)
|
|
mov += glm::vec3(0, 1, 0);
|
|
if (glfwGetKey(window, GLFW_KEY_E) == GLFW_PRESS)
|
|
mov -= glm::vec3(0, 1, 0);
|
|
|
|
mov *= 5;
|
|
return mov;
|
|
}
|
|
|
|
void framebuffer_size_callback(GLFWwindow *window, int width, int height) {
|
|
UNUSED(window);
|
|
glViewport(0, 0, width, height);
|
|
Camera *cam = (Camera *)glfwGetWindowUserPointer(window);
|
|
if (cam)
|
|
cam->aspect = (float)width / height;
|
|
}
|
|
|
|
void mouse_callback(GLFWwindow *window, double xpos, double ypos) {
|
|
static float xprev = 0.0, yprev = 0.0;
|
|
|
|
float xchange = xpos - xprev;
|
|
float ychange = yprev - ypos;
|
|
xprev = xpos;
|
|
yprev = ypos;
|
|
|
|
float sensitivity = 0.1f;
|
|
xchange *= sensitivity;
|
|
ychange *= sensitivity;
|
|
|
|
Camera *cam = (Camera *)glfwGetWindowUserPointer(window);
|
|
if (cam)
|
|
cam->addRotate(xchange, ychange);
|
|
}
|