forked from 15-466/15-466-f22-base4
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Mesh.cpp
150 lines (129 loc) · 4.89 KB
/
Mesh.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
#include "Mesh.hpp"
#include "read_write_chunk.hpp"
#include <glm/glm.hpp>
#include <stdexcept>
#include <fstream>
#include <iostream>
#include <vector>
#include <string>
#include <set>
#include <cstddef>
MeshBuffer::MeshBuffer(std::string const &filename) {
glGenBuffers(1, &buffer);
std::ifstream file(filename, std::ios::binary);
GLuint total = 0;
struct Vertex {
glm::vec3 Position;
glm::vec3 Normal;
glm::u8vec4 Color;
glm::vec2 TexCoord;
};
static_assert(sizeof(Vertex) == 3*4+3*4+4*1+2*4, "Vertex is packed.");
std::vector< Vertex > data;
//read + upload data chunk:
if (filename.size() >= 5 && filename.substr(filename.size()-5) == ".pnct") {
read_chunk(file, "pnct", &data);
//upload data:
glBindBuffer(GL_ARRAY_BUFFER, buffer);
glBufferData(GL_ARRAY_BUFFER, data.size() * sizeof(Vertex), data.data(), GL_STATIC_DRAW);
glBindBuffer(GL_ARRAY_BUFFER, 0);
total = GLuint(data.size()); //store total for later checks on index
//store attrib locations:
Position = Attrib(3, GL_FLOAT, GL_FALSE, sizeof(Vertex), offsetof(Vertex, Position));
Normal = Attrib(3, GL_FLOAT, GL_FALSE, sizeof(Vertex), offsetof(Vertex, Normal));
Color = Attrib(4, GL_UNSIGNED_BYTE, GL_TRUE, sizeof(Vertex), offsetof(Vertex, Color));
TexCoord = Attrib(2, GL_FLOAT, GL_FALSE, sizeof(Vertex), offsetof(Vertex, TexCoord));
} else {
throw std::runtime_error("Unknown file type '" + filename + "'");
}
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;
for (uint32_t v = entry.vertex_begin; v < entry.vertex_end; ++v) {
mesh.min = glm::min(mesh.min, data[v].Position);
mesh.max = glm::max(mesh.max, data[v].Position);
}
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;
}
/* //DEBUG:
std::cout << "File '" << filename << "' contained meshes";
for (auto const &m : meshes) {
if (&m.second == &meshes.rbegin()->second && meshes.size() > 1) std::cout << " and";
std::cout << " '" << m.first << "'";
if (&m.second != &meshes.rbegin()->second) std::cout << ",";
}
std::cout << std::endl;
*/
}
const 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;
}
GLuint MeshBuffer::make_vao_for_program(GLuint program) const {
//create a new vertex array object:
GLuint vao = 0;
glGenVertexArrays(1, &vao);
glBindVertexArray(vao);
//Try to bind all attributes in this buffer:
std::set< GLuint > bound;
glBindBuffer(GL_ARRAY_BUFFER, buffer);
auto bind_attribute = [&](char const *name, MeshBuffer::Attrib const &attrib) {
if (attrib.size == 0) return; //don't bind empty attribs
GLint location = glGetAttribLocation(program, name);
if (location == -1) return; //can't bind missing attribs
glVertexAttribPointer(location, attrib.size, attrib.type, attrib.normalized, attrib.stride, (GLbyte *)0 + attrib.offset);
glEnableVertexAttribArray(location);
bound.insert(location);
};
bind_attribute("Position", Position);
bind_attribute("Normal", Normal);
bind_attribute("Color", Color);
bind_attribute("TexCoord", TexCoord);
glBindBuffer(GL_ARRAY_BUFFER, 0);
glBindVertexArray(0);
//Check that all active attributes were bound:
GLint active = 0;
glGetProgramiv(program, GL_ACTIVE_ATTRIBUTES, &active);
assert(active >= 0 && "Doesn't makes sense to have negative active attributes.");
for (GLuint i = 0; i < GLuint(active); ++i) {
GLchar name[100];
GLint size = 0;
GLenum type = 0;
glGetActiveAttrib(program, i, 100, NULL, &size, &type, name);
name[99] = '\0';
GLint location = glGetAttribLocation(program, name);
if (!bound.count(GLuint(location))) {
throw std::runtime_error("ERROR: active attribute '" + std::string(name) + "' in program is not bound.");
}
}
return vao;
}