-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathMeshBuffer.cpp
More file actions
153 lines (118 loc) · 4.18 KB
/
Copy pathMeshBuffer.cpp
File metadata and controls
153 lines (118 loc) · 4.18 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
#include "MeshBuffer.hpp"
#include "read_chunk.hpp"
#include "GLBuffer.hpp"
#include <glm/glm.hpp>
#include <stdexcept>
#include <fstream>
#include <iostream>
#include <vector>
#include <string>
namespace kit {
MeshBuffer::MeshBuffer(std::string const &filename) {
std::ifstream file(filename, std::ios::binary);
auto endswith = [&filename](std::string ext) {
return filename.size() >= ext.size() && filename.substr(filename.size()-ext.size()) == ext;
};
GLuint total = 0;
//read + upload data chunk:
if (endswith(".p") || endswith(".pl")) {
GLAttribBuffer< glm::vec3 > buffer_;
std::vector< decltype(buffer_)::Vertex > data;
read_chunk(file, "p...", &data);
//upload data:
buffer_.set(data, GL_STATIC_DRAW);
total = GLuint(data.size()); //store total for later checks on index
//store attrib locations:
Position = buffer_[0];
this->buffer = std::move(buffer_);
} else if (endswith(".pn")) {
GLAttribBuffer< glm::vec3, glm::vec3 > buffer_;
std::vector< decltype(buffer_)::Vertex > data;
read_chunk(file, "pn..", &data);
//upload data:
buffer_.set(data, GL_STATIC_DRAW);
total = GLuint(data.size()); //store total for later checks on index
//store attrib locations:
Position = buffer_[0];
Normal = buffer_[1];
this->buffer = std::move(buffer_);
} else if (endswith(".pc")) {
GLAttribBuffer< glm::vec3, glm::u8vec4 > buffer_;
std::vector< decltype(buffer_)::Vertex > data;
read_chunk(file, "pc..", &data);
//upload data:
buffer_.set(data, GL_STATIC_DRAW);
total = GLuint(data.size()); //store total for later checks on index
//store attrib locations:
Position = buffer_[0];
Color = buffer_[1];
this->buffer = std::move(buffer_);
} else if (endswith(".pnc")) {
GLAttribBuffer< glm::vec3, glm::vec3, glm::u8vec4 > buffer_;
std::vector< decltype(buffer_)::Vertex > data;
read_chunk(file, "pnc.", &data);
//upload data:
buffer_.set(data, GL_STATIC_DRAW);
total = GLuint(data.size()); //store total for later checks on index
//store attrib locations:
Position = buffer_[0];
Normal = buffer_[1];
Color = buffer_[2];
this->buffer = std::move(buffer_);
} else if (endswith(".pnct")) {
GLAttribBuffer< glm::vec3, glm::vec3, glm::u8vec4, glm::vec2 > buffer_;
std::vector< decltype(buffer_)::Vertex > data;
read_chunk(file, "pnct", &data);
//upload data:
buffer_.set(data, GL_STATIC_DRAW);
total = GLuint(data.size()); //store total for later checks on index
//store attrib locations:
Position = buffer_[0];
Normal = buffer_[1];
Color = buffer_[2];
TexCoord = buffer_[3];
this->buffer = std::move(buffer_);
} else {
throw std::runtime_error("Unknown file type '" + filename + "'");
}
assert(this->buffer.buffer);
std::vector< char > strings;
read_chunk(file, "str0", &strings);
{ //read index chunk, add to meshes:
struct IndexEntry {
uint32_t name_begin, name_end;
uint32_t vertex_begin, vertex_end;
};
static_assert(sizeof(IndexEntry) == 16, "Index entry should be packed");
std::vector< IndexEntry > index;
read_chunk(file, "idx0", &index);
for (auto const &entry : index) {
if (!(entry.name_begin <= entry.name_end && entry.name_end <= strings.size())) {
throw std::runtime_error("index entry has out-of-range name begin/end");
}
if (!(entry.vertex_begin <= entry.vertex_end && entry.vertex_end <= total)) {
throw std::runtime_error("index entry has out-of-range vertex start/count");
}
std::string name(&strings[0] + entry.name_begin, &strings[0] + entry.name_end);
Mesh mesh;
mesh.type = GL_TRIANGLES;
mesh.start = entry.vertex_begin;
mesh.count = entry.vertex_end - entry.vertex_begin;
bool inserted = meshes.insert(std::make_pair(name, mesh)).second;
if (!inserted) {
std::cerr << "WARNING: mesh name '" + name + "' in filename '" + filename + "' collides with existing mesh." << std::endl;
}
}
}
if (file.peek() != EOF) {
std::cerr << "WARNING: trailing data in mesh file '" + filename + "'" << std::endl;
}
}
const MeshBuffer::Mesh &MeshBuffer::lookup(std::string const &name) const {
auto f = meshes.find(name);
if (f == meshes.end()) {
throw std::runtime_error("Looking up mesh '" + name + "' that doesn't exist.");
}
return f->second;
}
}