-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
290 lines (216 loc) · 8.01 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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
#include <iostream>
#include <vector>
#include <gl/glew.h>
#include <GLFW/glfw3.h>
#include "program/shader.h"
#include "imglib/stb_image.h"
#include "camera/camera.h"
#include "coneobject/cone_approx.h"
#include "pointlight/pointlight.h"
#include "skybox/skybox.h"
#include <glm/glm.hpp>
#include <glm/gtc/matrix_transform.hpp>
#include "skyboxes.h"
#define LIGHT_COLOR 1.0f
#define LIGHT_INTENSITY 10.0f
#define AMBIENT_PART 0.1f
#define DIFFUSE_PART 0.45f
#define SPECULAR_PART 0.45f
#define LIGHT_ATTENUATION_CONSTANT 1.0f
#define LIGHT_ATTENUATION_LINEAR 0.3f
#define LIGHT_ATTENUATION_QUADRATIC 0.24f
#define CONE_HEIGHT 4.0f
#define CONE_RADIUS 2.0f
#define CONE_COLOR 0.0f, 0.5f, 1.0f
#define CONE_SPECULAR_COLOR 1.0f
#define CONE_SHININESS 64.0f
#define SKYBOX_INDEX 3
// View globals
Camera camera(glm::vec3(0.0f));
float cameraSpeed = camera.MovementSpeed;
float wndWidth = 2560.0f;
float wndHeight = 1440.0f;
bool firstMouse = true;
float lastX = wndWidth / 2;
float lastY = wndHeight / 2;
// Misc globals
float deltaTime = 0.0f;
float lastFrame = 0.0f;
bool wireframeKeyPressed = false;
bool wireframeToggle = false;
float coneOpacity = 1.0f;
// Light globals
glm::vec3 lightPos = glm::vec3(0.0f);
glm::vec3 lightCol = glm::vec3(LIGHT_COLOR);
float lightYaw = 0.0f;
float lightPitch = 30.0f;
float lightDistance = 4.0f;
float lightSpeed = 1.25f;
// UI + callbacks
void framebuffer_size_callback(GLFWwindow* window, int width, int height)
{
wndWidth = width;
wndHeight = height;
glViewport(0, 0, wndWidth, wndHeight);
}
void keyboard(GLFWwindow* wnd)
{
if (glfwGetKey(wnd, GLFW_KEY_ESCAPE) == GLFW_PRESS)
glfwSetWindowShouldClose(wnd, GLFW_TRUE);
if (glfwGetKey(wnd, GLFW_KEY_LEFT_SHIFT) == GLFW_PRESS)
camera.MovementSpeed = cameraSpeed * 5;
else
camera.MovementSpeed = cameraSpeed;
if (glfwGetKey(wnd, GLFW_KEY_1) != GLFW_PRESS) wireframeKeyPressed = false;
if (glfwGetKey(wnd, GLFW_KEY_1) == GLFW_PRESS && !wireframeKeyPressed)
{
wireframeKeyPressed = true;
wireframeToggle = !wireframeToggle;
}
if (glfwGetKey(wnd, GLFW_KEY_UP) == GLFW_PRESS)
lightPitch = fmin(lightPitch + lightSpeed, 89.0f);
if (glfwGetKey(wnd, GLFW_KEY_DOWN) == GLFW_PRESS)
lightPitch = fmax(lightPitch - lightSpeed, -89.0f);
if (glfwGetKey(wnd, GLFW_KEY_LEFT) == GLFW_PRESS)
lightYaw += lightSpeed;
if (glfwGetKey(wnd, GLFW_KEY_RIGHT) == GLFW_PRESS)
lightYaw -= lightSpeed;
if (glfwGetKey(wnd, GLFW_KEY_W) == GLFW_PRESS)
coneOpacity = fmin(coneOpacity + 0.01f, 1.0f);
if (glfwGetKey(wnd, GLFW_KEY_S) == GLFW_PRESS)
coneOpacity = fmax(coneOpacity - 0.01f, 0.0f);
}
void mouse_callback(GLFWwindow* wnd, double xpos, double ypos)
{
if (firstMouse)
{
lastX = xpos;
lastY = ypos;
firstMouse = false;
}
float xoffset = xpos - lastX;
float yoffset = lastY - ypos;
lastX = xpos;
lastY = ypos;
camera.ProcessMouseMovement(xoffset, yoffset, GL_TRUE);
}
void mouse_scroll_callback(GLFWwindow* wnd, double xoffset, double yoffset)
{
lightDistance = glm::clamp(lightDistance + lightSpeed * (float)yoffset, 4.0f, 15.0f);
}
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(wndWidth, wndHeight, "OpenGL", nullptr, nullptr);
if (window == nullptr)
{
std::cerr << "Window could not be opened." << std::endl;
glfwTerminate();
return window;
}
glfwMakeContextCurrent(window);
glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_DISABLED);
glfwSetFramebufferSizeCallback(window, framebuffer_size_callback);
glfwSetCursorPosCallback(window, mouse_callback);
glfwSetScrollCallback(window, mouse_scroll_callback);
glewInit();
glViewport(0, 0, wndWidth, wndHeight);
glfwMaximizeWindow(window);
return window;
}
int main()
{
GLFWwindow* window = init();
if (window == nullptr) return -1;
Shader cone_shader("../shaders/shader.vert", "../shaders/shader.frag");
Shader light_shader("../shaders/lightshader.vert", "../shaders/lighshader.frag");
Shader skybox_shader("../shaders/skybox.vert", "../shaders/skybox.frag");
float aspect = wndHeight / wndWidth;
float width = 20.0f;
glm::mat4 projection = glm::ortho(-0.5f * width, 0.5f * width, -0.5f * aspect * width, 0.5f * aspect * width, -100.0f, 100.0f);
// Skybox definition
skybox skybox(skybox_textures[SKYBOX_INDEX]);
skybox_shader.use();
skybox_shader.setMat4("projection", glm::perspective(glm::radians(45.0f), wndWidth / wndHeight, 0.1f, 100.0f));
skybox_shader.setInt("skybox", 0);
//---------------------
// Cone definition
cone_approx<>::generate_vertices();
cone_approx<> cone;
glm::mat4 cone_model = glm::mat4(1.0f);
cone_model = glm::scale(cone_model, glm::vec3(CONE_RADIUS, CONE_HEIGHT, CONE_RADIUS));
glm::mat4 cone_normal = cone_model;
cone_normal = glm::inverse(cone_normal);
cone_normal = glm::transpose(cone_normal);
cone_shader.use();
cone_shader.setMat4("model", cone_model);
cone_shader.setMat4("normalmodel", cone_normal);
cone_shader.setMat4("projection", projection);
cone_shader.setFloat("pointlight.constant", LIGHT_ATTENUATION_CONSTANT);
cone_shader.setFloat("pointlight.linear", LIGHT_ATTENUATION_LINEAR);
cone_shader.setFloat("pointlight.quadratic", LIGHT_ATTENUATION_QUADRATIC);
cone_shader.setVec3("pointlight.ambient", AMBIENT_PART * LIGHT_INTENSITY * lightCol);
cone_shader.setVec3("pointlight.diffuse", DIFFUSE_PART * LIGHT_INTENSITY * lightCol);
cone_shader.setVec3("pointlight.specular", SPECULAR_PART * LIGHT_INTENSITY * lightCol);
cone_shader.setVec3("material.diffuse", glm::vec3(CONE_COLOR));
cone_shader.setVec3("material.specular", glm::vec3(CONE_SPECULAR_COLOR));
cone_shader.setFloat("material.shininess", CONE_SHININESS);
//---------------------
// Light definition
pointlight light(15.0f);
glm::mat4 light_model(1.0f);
light_shader.use();
light_shader.setMat4("projection", projection);
light_shader.setVec3("lightColor", lightCol);
//------------------
glm::mat4 view = camera.view();
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glFrontFace(GL_CW);
glEnable(GL_CULL_FACE);
glEnable(GL_DEPTH_TEST);
while(!glfwWindowShouldClose(window))
{
auto currentFrame = (float)glfwGetTime();
deltaTime = currentFrame - lastFrame;
lastFrame = currentFrame;
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
keyboard(window);
view = camera.view();
// Light render
float cp = cosf(glm::radians(lightPitch));
float sp = sinf(glm::radians(lightPitch));
float cy = cosf(glm::radians(lightYaw));
float sy = sinf(glm::radians(lightYaw));
lightPos = lightDistance * glm::vec3(-sy * cp, sp, cy * cp);
light_model = glm::translate(glm::mat4(1.0f), lightPos);
light_shader.use();
light_shader.setMat4("model", light_model);
light_shader.setMat4("view", view);
light.draw();
//-------------
// Skybox render
glm::mat4 skyview = glm::mat4(glm::mat3(view));
skybox_shader.use();
skybox_shader.setMat4("view", skyview);
skybox.draw();
//--------------
// Cone render
cone_shader.use();
cone_shader.setMat4("view", view);
cone_shader.setVec3("pointlight.position", lightPos);
cone_shader.setFloat("material.opacity", coneOpacity);
cone_shader.setVec3("toView", -camera.Front);
if (wireframeToggle) glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
cone.draw();
glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
//-------------
glfwSwapBuffers(window);
glfwPollEvents();
}
glfwTerminate();
return 0;
}