Repository navigation
Expand file tree
/
Copy pathGame.cpp
More file actions
221 lines (176 loc) · 6.3 KB
/
Copy pathGame.cpp
File metadata and controls
221 lines (176 loc) · 6.3 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
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
#include"Game.h"
Game::Game(unsigned int _winWidth, unsigned int _winHeight, unsigned int _samples)
{
winWidth = _winWidth;
winHeight = _winHeight;
samples = _samples;
}
void Game::run()
{
srand((unsigned int)time(NULL)); // random number seed
/* --------------------- window stuff --------------------- */
glfwInit();
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
glfwWindowHint(GLFW_SAMPLES, samples);
GLFWwindow* window = glfwCreateWindow(winWidth, winHeight, "3D Game", NULL, NULL);
glfwSetWindowPos(window, (1920 / 2) - (winWidth / 2), 30);
glfwMakeContextCurrent(window);
gladLoadGL();
glViewport(0, 0, winWidth, winWidth);
/* --------------------- stuff for blocks --------------------- */
Terrain world;
Shader shaderProgram("default.vert", "default.frag");
VAO VAO1;
VAO1.Bind();
VBO VBO1(world.vertices.data(), sizeof(float) * world.vertices.size());
//EBO EBO1(cubeIndices, sizeof(cubeIndices));
VAO1.LinkAttrib(VBO1, 0, 3, GL_FLOAT, 9 * sizeof(float), (void*)0);
VAO1.LinkAttrib(VBO1, 1, 3, GL_FLOAT, 9 * sizeof(float), (void*)(3 * sizeof(float)));
VAO1.LinkAttrib(VBO1, 2, 2, GL_FLOAT, 9 * sizeof(float), (void*)(6 * sizeof(float)));
VAO1.LinkAttrib(VBO1, 3, 1, GL_FLOAT, 9 * sizeof(float), (void*)(8 * sizeof(float)));
VAO1.Unbind();
VBO1.Unbind();
//EBO1.Unbind();
Texture texture("dirt_block/snow.png", GL_TEXTURE_2D, GL_TEXTURE0, GL_RGB, GL_UNSIGNED_BYTE);
Texture texture2("dirt_block/top.png", GL_TEXTURE_2D, GL_TEXTURE1, GL_RGB, GL_UNSIGNED_BYTE);
int textures[] = { 0, 1 };
shaderProgram.Activate();
shaderProgram.setList("tex_array", 2, textures);
/* --------------------- other stuff --------------------- */
glEnable(GL_DEPTH_TEST);
glEnable(GL_MULTISAMPLE);
Camera camera(winWidth, winHeight, glm::vec3(0.0f, 0.0f, 2.0f), 1.25f, 100.0f);
/* --------------------- skybox stuff --------------------- */
Shader skyboxShader("skybox.vert", "skybox.frag");
VAO skyboxVAO;
skyboxVAO.Bind();
VBO skyboxVBO(skyboxVertices, sizeof(skyboxVertices));
EBO skyboxEBO(skyboxIndices, sizeof(skyboxIndices));
skyboxVAO.LinkAttrib(skyboxVBO, 0, 3, GL_FLOAT, 3 * sizeof(float), (void*)0);
skyboxVAO.Unbind();
skyboxVBO.Unbind();
skyboxEBO.Unbind();
std::string facesCubemap[6] =
{
"skybox/right.png",
"skybox/left.png",
"skybox/top.png",
"skybox/bottom.png",
"skybox/front.png",
"skybox/back.png"
};
unsigned int cubemapTexture;
glGenTextures(1, &cubemapTexture);
glBindTexture(GL_TEXTURE_CUBE_MAP, cubemapTexture);
glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
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);
for (unsigned int i = 0; i < 6; i++)
{
int width, height, nrChannels;
unsigned char* data = stbi_load(facesCubemap[i].c_str(), &width, &height, &nrChannels, 0);
stbi_set_flip_vertically_on_load(false);
glTexImage2D(
GL_TEXTURE_CUBE_MAP_POSITIVE_X + i,
0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, data);
stbi_image_free(data);
}
skyboxShader.Activate();
skyboxShader.setInt("skybox", 0);
loop(window, shaderProgram, skyboxShader,
VAO1, skyboxVAO, camera, world,
texture, texture2, cubemapTexture);
}
void Game::loop(
GLFWwindow* window,
Shader shaderProgram, Shader skyboxShader,
VAO VAO1, VAO skyboxVAO,
Camera camera, Terrain world,
Texture texture, Texture texture2,
unsigned int cubemapTexture)
{
double previousTime = glfwGetTime();
int fps = 0;
while (!glfwWindowShouldClose(window))
{
processInput(window);
// --- counting frames
double currentTime = glfwGetTime();
fps++;
if (currentTime - previousTime >= 1.0)
{
std::stringstream ss;
ss << "3D Game" << " [" << fps << " FPS]";
glfwSetWindowTitle(window, ss.str().c_str());
fps = 0;
previousTime = currentTime;
}
// ----- draw sequence ----- //
glClearColor(0.07f, 0.13f, 0.17f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
// player camera movement
shaderProgram.Activate();
camera.Move(window);
camera.Matrix(fov, 0.1f, render_distance, shaderProgram, "camMatrix");
// cubes
texture.Bind();
texture2.Bind();
VAO1.Bind();
glm::mat4 model = glm::mat4(1.0f);
model = glm::translate(model, glm::vec3(world.offset[0], world.offset[1], world.offset[2]));
shaderProgram.setMat4("model", model);
glDrawArrays(GL_TRIANGLES, 0, (GLsizei)(sizeof(float) * world.vertices.size()));
drawSkybox(skyboxShader, camera, cubemapTexture, skyboxVAO);
// update screen
glfwSwapBuffers(window);
glfwPollEvents();
}
}
void Game::destroy(VAO VAO1, VBO VBO1, VAO skyboxVAO, VBO skyboxVBO, EBO skyboxEBO,
Texture texture, Texture texture2,
Shader shaderProgram, Shader skyboxShader,
GLFWwindow* window)
{
VAO1.Delete();
VBO1.Delete();
//EBO1.Delete();
texture.Delete();
texture2.Delete();
shaderProgram.Delete();
skyboxVAO.Delete();
skyboxVBO.Delete();
skyboxEBO.Delete();
skyboxShader.Delete();
glfwDestroyWindow(window);
glfwTerminate();
}
void Game::processInput(GLFWwindow* _window)
{
if (glfwGetKey(_window, GLFW_KEY_UP) == GLFW_PRESS) {
fov += 5.0f;
std::cout << fov << std::endl;
}
if (glfwGetKey(_window, GLFW_KEY_DOWN) == GLFW_PRESS) {
fov -= 5.0f;
std::cout << fov << std::endl;
}
}
void Game::drawSkybox(Shader _skyboxShader, Camera _camera, unsigned int _cubemapTexture, VAO _skyboxVAO) {
glDepthFunc(GL_LEQUAL);
_skyboxShader.Activate();
glm::mat4 view = glm::mat4(1.0f);
glm::mat4 projection = glm::mat4(1.0f);
view = glm::mat4(glm::mat3(glm::lookAt(_camera.Position, _camera.Position + _camera.Orientation, _camera.Up)));
projection = glm::perspective(glm::radians(45.0f), (float)(winWidth / winHeight), 0.1f, 100.0f);
glUniformMatrix4fv(glGetUniformLocation(_skyboxShader.ID, "view"), 1, GL_FALSE, glm::value_ptr(view));
glUniformMatrix4fv(glGetUniformLocation(_skyboxShader.ID, "projection"), 1, GL_FALSE, glm::value_ptr(projection));
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_CUBE_MAP, _cubemapTexture);
_skyboxVAO.Bind();
glDrawElements(GL_TRIANGLES, 36, GL_UNSIGNED_INT, 0);
glDepthFunc(GL_LESS);
}