-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathBufferManager.cpp
More file actions
140 lines (122 loc) · 5.77 KB
/
Copy pathBufferManager.cpp
File metadata and controls
140 lines (122 loc) · 5.77 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
#include "BufferManager.hpp"
#include "resources/Models/Light.cpp"
#define STB_IMAGE_IMPLEMENTATION // Have to include this or STB will throw error
#include <stb/stb_image.h> // Used for textures / image processing
#include <glm/glm.hpp>
#include <glm/gtc/matrix_transform.hpp>
#include <glm/gtc/type_ptr.hpp>
// Initialize all of our buffers
BufferManager::BufferManager() : cubeShaderProgram("shaders/cube.vert", "shaders/cube.frag") // Member-Initializer List
{
LightVAO.Bind();
// Generates Vertex Buffer Object and links it to vertices
LightVBO.InitVBO(lightVertices, sizeof(lightVertices));
// Generates Element Buffer Object and links it to indices
LightEBO.InitEBO(lightIndices, sizeof(lightIndices));
// Links VBO attributes such as coordinates and colors to VAO
LightVAO.LinkAttrib(LightVBO, 0, 3, GL_FLOAT, 3 * sizeof(GLfloat), (void *)0);
// Unbind all to prevent accidentally modifying them
LightVAO.Unbind();
LightVBO.Unbind();
LightEBO.Unbind();
// Activate all of our textures
ActivateTextures();
// Generate our chunks if they have not been generated yet
chunkManager.GenerateChunks();
// Set our blocksize in our vertex shader
glUniform1f(glGetUniformLocation(cubeShaderProgram.GetID(), "blockSize"), World::blockSize);
}
BufferManager::~BufferManager()
{
LightVAO.Delete();
LightVBO.Delete();
LightEBO.Delete();
}
void BufferManager::RunLoop(GLFWwindow *window, glm::vec3 cameraPosition, glm::vec3 cameraOrientation)
{
// Set all of the uniforms for our lighting shader
glm::vec4 lightColor = glm::vec4(1.0f, 1.0f, 1.0f, 1.0f); // Color of the light
// glm::vec3 lightPosition = glm::vec3(sin(glfwGetTime()) * World::chunkSize, World::heightLimit, sin(glfwGetTime()) * World::chunkSize); // Position of the light cube
glm::vec3 lightPosition = glm::vec3(World::chunkSize, World::heightLimit, World::chunkSize); // Position of the light cube
glm::mat4 lightModel = glm::mat4(1.0f);
lightModel = glm::translate(lightModel, lightPosition);
// Tells OpenGL which Shader Program we want to use
lightShaderProgram.Activate();
// Export our various uniform values to our lighting shaders
glUniformMatrix4fv(glGetUniformLocation(lightShaderProgram.GetID(), "model"), 1, GL_FALSE, glm::value_ptr(lightModel));
glUniform4f(glGetUniformLocation(lightShaderProgram.GetID(), "lightColor"), lightColor.x, lightColor.y, lightColor.z, lightColor.w);
// Bind the VAO so OpenGL knows to use it
LightVAO.Bind();
// Draw primitive, number of indices, datatype of indices, index of indices
// Draw our light cube
glDrawElements(GL_TRIANGLES, sizeof(lightIndices) / sizeof(GLint), GL_UNSIGNED_INT, 0);
// Tell OpenGL which Shader Program we want to use
cubeShaderProgram.Activate();
// Assign uniforms for our cube shader
glUniform4f(glGetUniformLocation(cubeShaderProgram.GetID(), "lightColor"), lightColor.x, lightColor.y, lightColor.z, lightColor.w);
glUniform3f(glGetUniformLocation(cubeShaderProgram.GetID(), "lightPosition"), lightPosition.x, lightPosition.y, lightPosition.z);
glUniform3f(glGetUniformLocation(cubeShaderProgram.GetID(), "cameraPosition"), cameraPosition.x, cameraPosition.y, cameraPosition.z);
// Render all of our chunks
chunkManager.RenderChunks(cameraPosition, cameraOrientation, cubeShaderProgram.GetID());
}
void BufferManager::ActivateTextures()
{
// Tell OpenGL which Shader Program we want to use
cubeShaderProgram.Activate();
// Load all of our textures from a json file
std::ifstream ifs("resources/blocks.json");
json jf;
if (ifs.is_open())
{
jf = json::parse(ifs);
}
else
{
std::cout << "The texture file could not be opened." << std::endl;
}
std::string texture_path;
std::string group;
bool texture_transparent;
bool texture_isFoliage;
GLuint index = 0;
GLuint count = 0;
// Add air tile
blocks["Air"]["index"] = -1;
blocks["Air"]["transparent"] = true;
blocks["Air"]["isFoliage"] = false;
blocks["Air"]["group"] = "Air";
blocks["-1"]["transparent"] = true;
blocks["-1"]["isFoliage"] = false;
blocks["-1"]["group"] = "Air";
// Loop through all of our textures. Double for loop
// makes sure we get all the indices in order
for (auto it = jf.begin(); it != jf.end(); it++)
{
for (auto el : jf.items())
{
index = el.value()["id"];
if (index == count)
{
// Get texture path and group from json
texture_path = el.value()["texture"];
group = el.value()["group"];
texture_transparent = (bool)el.value()["transparent"];
texture_isFoliage = (bool)el.value()["isFoliage"];
// Add the texture to our texture array
textureArray.AddTextureToArray(texture_path.c_str());
// Make an index for this texture
blocks[el.key()]["index"] = index;
blocks[el.key()]["group"] = group;
blocks[el.key()]["transparent"] = texture_transparent;
blocks[el.key()]["isFoliage"] = texture_isFoliage;
blocks["" + std::to_string(index) + ""]["transparent"] = texture_transparent;
blocks["" + std::to_string(index) + ""]["isFoliage"] = texture_isFoliage;
blocks["" + std::to_string(index) + ""]["group"] = group;
blocks["" + std::to_string(index) + ""]["texture"] = texture_path;
count++;
}
}
}
// Activate our 2D texture array
textureArray.ActivateShaderArray(cubeShaderProgram.GetID());
}