-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
179 lines (140 loc) · 4.96 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
#include <iostream>
#include <glad/glad.h>
#include <GLFW/glfw3.h>
#include <stb/stb_image.h>
#include <glm/glm.hpp>
#include <glm/gtc/matrix_transform.hpp>
#include <glm/gtc/type_ptr.hpp>
#include "Texture.h"
#include "shaderClass.h"
#include "VAO.h"
#include "VBO.h"
#include "EBO.h"
#include "camera.h"
#include "blocks.h"
#include "worldGen.h"
#define WIDTH 1920
#define HEIGHT 1080
GLfloat lightVertices[] =
{ // COORDINATES //
-0.1f, -0.1f, 0.1f,
-0.1f, -0.1f, -0.1f,
0.1f, -0.1f, -0.1f,
0.1f, -0.1f, 0.1f,
-0.1f, 0.1f, 0.1f,
-0.1f, 0.1f, -0.1f,
0.1f, 0.1f, -0.1f,
0.1f, 0.1f, 0.1f
};
GLuint lightIndices[] =
{
0, 1, 2,
0, 2, 3,
0, 4, 7,
0, 7, 3,
3, 7, 6,
3, 6, 2,
2, 6, 5,
2, 5, 1,
1, 5, 4,
1, 4, 0,
4, 5, 6,
4, 6, 7
};
int main()
{
// Initialize GLFW
glfwInit();
// Tell GLFW what version of OpenGL we are using
// In this case we are using OpenGL 3.3
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
// Tell GLFW we are using the CORE profile
// So that means we only have the modern functions
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
// Create a GLFWwindow object of 1920 by 1080 pixels
GLFWwindow* window = glfwCreateWindow(WIDTH, HEIGHT, "MineMinecraft", NULL, NULL);
// Error check if the window fails to create
if (window == NULL)
{
std::cout << "Failed to create GLFW window" << std::endl;
glfwTerminate();
return -1;
}
// Introduce the window into the current context
glfwMakeContextCurrent(window);
//Load GLAD so it configures OpenGL
gladLoadGL();
// Specify the viewport of OpenGL in the Window
// In this case the viewport goes from x = 0, y = 0, to x = 1920, y = 1080
glViewport(0, 0, WIDTH, HEIGHT);
// Generates Shader object using shaders default.vert and default.frag
Shader shaderProgram("default.vert", "default.frag");
Chunk* block = new Chunk(&shaderProgram);
generate_world(block);
// Shader for light cube
Shader lightShader("light.vert", "light.frag");
// Generates Vertex Array Object and binds it
VAO lightVAO;
lightVAO.Bind();
// Generates Vertex Buffer Object and links it to vertices
VBO lightVBO(lightVertices, sizeof(lightVertices));
// Generates Element Buffer Object and links it to indices
EBO lightEBO(lightIndices, sizeof(lightIndices));
// Links VBO attributes such as coordinates and colors to VAO
lightVAO.LinkAttrib(lightVBO, 0, 3, GL_FLOAT, 3 * sizeof(float), (void*)0);
// Unbind all to prevent accidentally modifying them
lightVAO.Unbind();
lightVBO.Unbind();
lightEBO.Unbind();
glm::vec4 lightColor = glm::vec4(1.0f, 1.0f, 1.0f, 1.0f);
glm::vec3 lightPos = glm::vec3(0.9f, 0.9f, 0.9f);
glm::mat4 lightModel = glm::mat4(1.0f);
lightModel = glm::translate(lightModel, lightPos);
lightShader.Activate();
glUniformMatrix4fv(glGetUniformLocation(lightShader.ID, "model"), 1, GL_FALSE, glm::value_ptr(lightModel));
glUniform4f(glGetUniformLocation(lightShader.ID, "lightColor"), lightColor.x, lightColor.y, lightColor.z, lightColor.w);
glm::vec3 blockPos = glm::vec3(0.f, 0.f, 0.f);
glm::mat4 blockModel = glm::mat4(1.0f);
blockModel = glm::translate(blockModel, blockPos);
shaderProgram.Activate();
glUniformMatrix4fv(glGetUniformLocation(shaderProgram.ID, "model"), 1, GL_FALSE, glm::value_ptr(blockModel));
glUniform4f(glGetUniformLocation(shaderProgram.ID, "lightColor"), lightColor.x, lightColor.y, lightColor.z, lightColor.w);
glUniform3f(glGetUniformLocation(shaderProgram.ID, "lightPos"), lightPos.x, lightPos.y, lightPos.z);
glEnable(GL_DEPTH_TEST);
Camera camera(WIDTH, HEIGHT, glm::vec3(0.f, 0.f, 0.f));
// Main while loop
while (!glfwWindowShouldClose(window))
{
// Specify the color of the background
glClearColor(0.07f, 0.13f, 0.17f, 1.0f);
// Clean the back buffer and assign the new color to it
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
camera.Inputs(window, block);
camera.updateMatrix(45.f, .03f, 100.f);
shaderProgram.Activate();
// Exports the camera Position to the Fragment Shader for specular lighting
glUniform3f(glGetUniformLocation(shaderProgram.ID, "camPos"), camera.Position.x, camera.Position.y, camera.Position.z);
// Export the camMatrix to the Vertex Shader
camera.Matrix(shaderProgram, "camMatrix");
block->print_chunk();
// Tells OpenGL which Shader Program we want to use
lightShader.Activate();
// Export the camMatrix to the Vertex Shader of the light cube
camera.Matrix(lightShader, "camMatrix");
// Bind the VAO so OpenGL knows to use it
lightVAO.Bind();
// Draw primitives, number of indices, datatype of indices, index of indices
glDrawElements(GL_TRIANGLES, sizeof(lightIndices) / sizeof(int), GL_UNSIGNED_INT, 0);
// Swap the back buffer with the front buffer
glfwSwapBuffers(window);
// Take care of all GLFW events
glfwPollEvents();
}
shaderProgram.Delete();
// Delete window before ending the program
glfwDestroyWindow(window);
// Terminate GLFW before ending the program
glfwTerminate();
return 0;
}