-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcommon.cpp
More file actions
154 lines (140 loc) · 4.78 KB
/
Copy pathcommon.cpp
File metadata and controls
154 lines (140 loc) · 4.78 KB
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
#include "common.hpp"
#include "camera.hpp"
#include <glad/glad.h>
#include <GLFW/glfw3.h>
// #define SPDLOG_ACTIVE_LEVEL SPDLOG_LEVEL_DEBUG
#include <spdlog/spdlog.h>
#define STB_IMAGE_IMPLEMENTATION
#include <stb_image.h>
Camera* gcam = nullptr;
void GLFWErrorCB(int ec, const char* emsg) {
SPDLOG_ERROR("GLFW Error Callback({}): {}", ec, emsg);
}
void GLFWFrameResizeCB(GLFWwindow* window, int w, int h) {
glViewport(0, 0, w, h);
}
float lxpos, lypos;
bool bIsFirstCursor = true;
void GLFWCursorPosCB(GLFWwindow* window, double xpos, double ypos) {
if (!gcam) {
SPDLOG_CRITICAL("Camera not set");
return;
}
if (bIsFirstCursor) {
bIsFirstCursor = false;
} else {
gcam->ProcessMouseMovement(xpos-lxpos, lypos-ypos);
// gcam->ProcessMouseMovement(xpos-lxpos, ypos-lypos);
}
lxpos = xpos;
lypos = ypos;
}
void GLFWScrollCB(GLFWwindow* window, double xoffset, double yoffset) {
if (!gcam) {
SPDLOG_CRITICAL("Camera not set");
return;
}
gcam->ProcessMouseScroll(yoffset);
}
void ProcessInputs(GLFWwindow* window, float delta) {
if (!gcam) {
SPDLOG_CRITICAL("Camera not set");
return;
}
if (glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_PRESS) {
glfwSetWindowShouldClose(window, GL_TRUE);
}
if (glfwGetKey(window, GLFW_KEY_W) == GLFW_PRESS) {
gcam->ProcessKeyboard(ECameraMovement::FORWARD, delta);
}
if (glfwGetKey(window, GLFW_KEY_S) == GLFW_PRESS) {
gcam->ProcessKeyboard(ECameraMovement::BACKWARD, delta);
}
if (glfwGetKey(window, GLFW_KEY_A) == GLFW_PRESS) {
gcam->ProcessKeyboard(ECameraMovement::LEFT, delta);
}
if (glfwGetKey(window, GLFW_KEY_D) == GLFW_PRESS) {
gcam->ProcessKeyboard(ECameraMovement::RIGHT, delta);
}
}
void SetCamera(Camera* pcam) {
gcam = pcam;
}
unsigned int LoadTexture(char const* path) {
unsigned int texture;
glGenTextures(1, &texture);
// stbi_set_flip_vertically_on_load(true);
int w, h, c;
unsigned char* data = stbi_load(path, &w, &h, &c, 0);
if (data) {
GLenum fmt = GL_RGB;
GLint param = GL_REPEAT;
switch (c) {
case 1:
fmt = GL_RED;
break;
case 3:
fmt = GL_RGB;
break;
case 4:
fmt = GL_RGBA;
param = GL_CLAMP_TO_EDGE;
break;
default:
SPDLOG_WARN("Unhandled image format(channel={})", c);
break;
}
SPDLOG_DEBUG("Load texture({}), format: 0x{:x}, param: 0x{:x}", path, fmt, param);
glBindTexture(GL_TEXTURE_2D, texture);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, param);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, param);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
glTexImage2D(GL_TEXTURE_2D, 0, fmt, w, h, 0, fmt, GL_UNSIGNED_BYTE, data);
glGenerateMipmap(GL_TEXTURE_2D);
} else {
SPDLOG_WARN("Load Texture({}) Failed", path);
}
glBindTexture(GL_TEXTURE_2D, 0);
return texture;
}
unsigned int LoadCubeTexture(char const* pathes[]) {
unsigned int texcube;
glGenTextures(1, &texcube);
glBindTexture(GL_TEXTURE_CUBE_MAP, texcube);
// stbi_set_flip_vertically_on_load(true);
int w, h, c;
unsigned char* data;
for (int i = 0; i < 6; ++i) {
data = stbi_load(pathes[i], &w, &h, &c, 0);
if (data) {
GLenum fmt = GL_RGB;
switch (c) {
case 1:
fmt = GL_RED;
break;
case 3:
fmt = GL_RGB;
break;
case 4:
fmt = GL_RGBA;
break;
default:
SPDLOG_WARN("Unhandled image format(channel={})", c);
break;
}
glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_X+i, 0, fmt, w, h, 0, fmt, GL_UNSIGNED_BYTE, data);
} else {
SPDLOG_WARN("Load Cube Texture({}) Failed", pathes[i]);
}
stbi_image_free(data);
}
glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_R, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glGenerateMipmap(GL_TEXTURE_CUBE_MAP);
glBindTexture(GL_TEXTURE_CUBE_MAP, 0);
return texcube;
}