-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.cpp
125 lines (100 loc) · 4.07 KB
/
main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
#include <iostream>
#include <windows.h>
#include <glad/glad.h>
#include <GLFW/glfw3.h>
#include <glm/glm.hpp>
#include <glm/gtc/matrix_transform.hpp>
#include <plog/Log.h>
#include <plog/Initializers/RollingFileInitializer.h>
#include "shader.h"
#include "mesh.h"
#include "camera.h"
int width = 1600, height = 900;
float lastTime;
Camera *camera = nullptr;
void framebuffer_size_callback(GLFWwindow *window, int width, int height);
void cursor_position_callback(GLFWwindow *window, double x, double y);
void scroll_callback(GLFWwindow* window, double x, double y);
void handle_keyboard(GLFWwindow* window, float deltaTime);
int main() {
plog::init(plog::warning);
glfwInit();
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
glfwWindowHint(GLFW_SAMPLES, 4);
// create window
GLFWwindow *window = glfwCreateWindow(width, height, "Mesh Viewer", nullptr, nullptr);
if (!window) {
std::cerr << "Failed to create GLFW window" << std::endl;
glfwTerminate();
return -1;
}
glfwMakeContextCurrent(window);
// init glad
if (!gladLoadGLLoader((GLADloadproc) glfwGetProcAddress)) {
std::cerr << "Failed to initialize GLAD" << std::endl;
return -1;
}
glfwSetFramebufferSizeCallback(window, framebuffer_size_callback);
glfwSetCursorPosCallback(window, cursor_position_callback);
glfwSetScrollCallback(window, scroll_callback);
ShaderProgram program("../src/shader/common.vert", "../src/shader/phong.frag");
Model lumine("../resource/lumine/Lumine.obj", &program);
camera = new ModelRotationCamera({0.0f, 10.0f, 0.0f}, 20.0f);
lastTime = glfwGetTime();
glm::vec3 lightPos(0.0f, 10.0f, 5.0f),
lightColor(3.0f, 3.0f, 1.0f),
lightAmbient(1.0f, 1.0f, 1.0f);
glViewport(0, 0, width, height);
while (!glfwWindowShouldClose(window)) {
glClearColor(0.3f, 0.3f, 0.3f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
float nowTime = glfwGetTime();
float deltaTime = nowTime - lastTime;
lastTime = nowTime;
handle_keyboard(window, deltaTime);
glm::mat4 model = glm::identity<glm::mat4>();
glm::mat4 view = camera->getViewMatrix();
glm::mat4 projection = glm::perspective(glm::radians(camera->getFOV()),
(float) width / height, 0.1f, 100.0f);
program.setMVPMatrices(model, view, projection);
program.setVec3("eyePos", camera->getPosition());
program.setVec3("light.position", lightPos);
program.setVec3("light.ambient", lightAmbient);
program.setVec3("light.diffuse", lightColor);
program.setVec3("light.specular", lightColor);
lumine.draw();
glfwSwapBuffers(window);
glfwPollEvents();
}
delete camera;
glfwTerminate();
return 0;
}
void framebuffer_size_callback(GLFWwindow *window, int width, int height) {
::width = width;
::height = height;
glViewport(0, 0, width, height);
}
void cursor_position_callback(GLFWwindow *window, double x, double y) {
bool pressed = glfwGetMouseButton(window, GLFW_MOUSE_BUTTON_LEFT) == GLFW_PRESS;
if (camera != nullptr) camera->handleMouseInput(x, y, pressed);
}
void scroll_callback(GLFWwindow* window, double x, double y) {
if (camera != nullptr) camera->handleScrollInput(y);
}
void handle_keyboard(GLFWwindow* window, float deltaTime) {
if (glfwGetKey(window, GLFW_KEY_W) == GLFW_PRESS) {
if (camera != nullptr) camera->handleKeyboardInput(GLFW_KEY_W, deltaTime);
}
if (glfwGetKey(window, GLFW_KEY_A) == GLFW_PRESS) {
if (camera != nullptr) camera->handleKeyboardInput(GLFW_KEY_A, deltaTime);
}
if (glfwGetKey(window, GLFW_KEY_S) == GLFW_PRESS) {
if (camera != nullptr) camera->handleKeyboardInput(GLFW_KEY_S, deltaTime);
}
if (glfwGetKey(window, GLFW_KEY_D) == GLFW_PRESS) {
if (camera != nullptr) camera->handleKeyboardInput(GLFW_KEY_D, deltaTime);
}
}