-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathResourceLoading.cpp
More file actions
143 lines (120 loc) · 3.75 KB
/
Copy pathResourceLoading.cpp
File metadata and controls
143 lines (120 loc) · 3.75 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
#include "ResourceLoading.h"
#include "tiny_obj_loader.h"
#include "webgpu/webgpu.hpp"
#include <filesystem>
#include <fstream>
#include <sstream>
#include <string>
namespace fs = std::filesystem;
bool loadGeometry(const fs::path& path, std::vector<float>& pointData, std::vector<uint16_t>& indexData, int dimensions) {
std::ifstream file(path);
if (!file.is_open()) {
return false;
}
pointData.clear();
indexData.clear();
enum class Section {
None,
Points,
Indices,
};
Section currentSection = Section::None;
float value;
uint16_t index;
std::string line;
while (!file.eof()) {
getline(file, line);
// overcome the `CRLF` problem
if (!line.empty() && line.back() == '\r') {
line.pop_back();
}
if (line == "[points]") {
currentSection = Section::Points;
}
else if (line == "[indices]") {
currentSection = Section::Indices;
}
else if (line[0] == '#' || line.empty()) {
// Do nothing, this is a comment
}
else if (currentSection == Section::Points) {
std::istringstream iss(line);
// Get x, y, r, g, b
for (int i = 0; i < dimensions + 3; ++i) {
iss >> value;
pointData.push_back(value);
}
}
else if (currentSection == Section::Indices) {
std::istringstream iss(line);
// Get corners #0 #1 and #2
for (int i = 0; i < 3; ++i) {
iss >> index;
indexData.push_back(index);
}
}
}
return true;
}
bool loadGeometryFromObj(const fs::path& path, std::vector<VertexAttributes>& vertexData) {
tinyobj::attrib_t attrib;
std::vector<tinyobj::shape_t> shapes;
std::vector<tinyobj::material_t> materials;
std::string warn;
std::string err;
bool ret = tinyobj::LoadObj(&attrib, &shapes, &materials, &warn, &err, path.string().c_str());
if (!warn.empty()) {
std::cout << warn << std::endl;
}
if (!err.empty()) {
std::cerr << err << std::endl;
}
if (!ret) {
return false;
}
// Fill in vertexData here
vertexData.clear();
for (const auto& shape : shapes) {
size_t offset = vertexData.size();
vertexData.resize(offset + shape.mesh.indices.size());
for (int i = 0; i < vertexData.size(); ++i) {
const tinyobj::index_t& idx = shape.mesh.indices[i];
vertexData[offset + i].position = {
attrib.vertices[3 * idx.vertex_index + 0],
-attrib.vertices[3 * idx.vertex_index + 2],
attrib.vertices[3 * idx.vertex_index + 1]
};
vertexData[offset + i].normal = {
attrib.normals[3 * idx.normal_index + 0],
-attrib.normals[3 * idx.normal_index + 2],
attrib.normals[3 * idx.normal_index + 1]
};
vertexData[offset + i].color = {
attrib.colors[3 * idx.vertex_index + 0],
attrib.colors[3 * idx.vertex_index + 1],
attrib.colors[3 * idx.vertex_index + 2]
};
}
}
return true;
}
wgpu::ShaderModule loadShaderModule(const fs::path& path, wgpu::Device device) {
std::ifstream file(path);
if (!file.is_open()) {
return nullptr;
}
file.seekg(0, std::ios::end);
size_t size = file.tellg();
std::string shaderSource(size, ' ');
file.seekg(0);
file.read(shaderSource.data(), size);
wgpu::ShaderModuleWGSLDescriptor shaderCodeDesc{};
shaderCodeDesc.chain.next = nullptr;
shaderCodeDesc.chain.sType = wgpu::SType::ShaderModuleWGSLDescriptor;
shaderCodeDesc.code = shaderSource.c_str();
wgpu::ShaderModuleDescriptor shaderDesc{};
shaderDesc.hintCount = 0;
shaderDesc.hints = nullptr;
shaderDesc.nextInChain = &shaderCodeDesc.chain;
return device.createShaderModule(shaderDesc);
}