-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathblock_manager.cpp
More file actions
95 lines (80 loc) · 3.38 KB
/
Copy pathblock_manager.cpp
File metadata and controls
95 lines (80 loc) · 3.38 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
#include <fstream>
#include <iostream>
#include <glad/glad.h>
#include <GLFW/glfw3.h>
#include <nlohmann/json.hpp>
#include "block_manager.hpp"
#include "stb_image.h"
using json = nlohmann::json;
void BlockManager::loadBlocks(const char* configPath) {
std::ifstream f(configPath);
if (!f.is_open()) {
std::cerr << "Failed to open block config: " << configPath << std::endl;
return;
}
json data = json::parse(f);
std::vector<std::string> texturePaths;
std::map<std::string, int> pathToIndex;
// Process JSON
for (auto& block : data) {
int id = block["id"];
BlockFaceTextures faces;
// Lambda to avoid duplicates
auto registerTex = [&](std::string path) -> int {
if (pathToIndex.find(path) == pathToIndex.end()) {
pathToIndex[path] = texturePaths.size();
texturePaths.push_back(path);
}
return pathToIndex[path];
};
if (block.contains("texture") && block["texture"].is_string()) {
int idx = registerTex(block["texture"]);
faces = { idx, idx, idx };
}
else if (block.contains("textures")) {
faces.topLayer = registerTex(block["textures"]["top"]);
faces.bottomLayer = registerTex(block["textures"]["bottom"]);
faces.sideLayer = registerTex(block["textures"]["side"]);
}
blockData[id] = faces;
}
// Generate OpenGL Texture Array
if (texturePaths.empty()) return;
glGenTextures(1, &textureArrayID);
glBindTexture(GL_TEXTURE_2D_ARRAY, textureArrayID);
// Load the first image to determine width/height for the whole array
int width, height, nrChannels;
std::string firstPath = "textures/" + texturePaths[0];
unsigned char* temp = stbi_load(firstPath.c_str(), &width, &height, &nrChannels, 0);
if (!temp) {
std::cerr << "Failed to load base texture: " << firstPath << std::endl;
return;
}
stbi_image_free(temp);
// Allocate the storage on GPU (width x height x NumberOfImages)
glTexImage3D(GL_TEXTURE_2D_ARRAY, 0, GL_RGBA, width, height, texturePaths.size(), 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL);
// Upload images to layers
for (int i = 0; i < texturePaths.size(); i++) {
std::string fullPath = "textures/" + texturePaths[i];
unsigned char* data = stbi_load(fullPath.c_str(), &width, &height, &nrChannels, 0);
if (data) {
GLenum format = (nrChannels == 4) ? GL_RGBA : GL_RGB;
glTexSubImage3D(GL_TEXTURE_2D_ARRAY, 0, 0, 0, i, width, height, 1, format, GL_UNSIGNED_BYTE, data);
stbi_image_free(data);
}
else {
std::cerr << "Failed to load texture layer: " << fullPath << std::endl;
}
}
glGenerateMipmap(GL_TEXTURE_2D_ARRAY);
// Texture Parameters
glTexParameteri(GL_TEXTURE_2D_ARRAY, GL_TEXTURE_MIN_FILTER, GL_NEAREST_MIPMAP_LINEAR);
glTexParameteri(GL_TEXTURE_2D_ARRAY, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D_ARRAY, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D_ARRAY, GL_TEXTURE_WRAP_T, GL_REPEAT);
if (glfwExtensionSupported("GL_EXT_texture_filter_anisotropic")) {
float aniso = 0.0f;
glGetFloatv(GL_MAX_TEXTURE_MAX_ANISOTROPY, &aniso);
glTexParameterf(GL_TEXTURE_2D_ARRAY, GL_TEXTURE_MAX_ANISOTROPY, aniso);
}
}