12 glGenVertexArrays(1, &va);
13 glGenBuffers(ATTRIB_COUNT, handle);
17 glDeleteBuffers(ATTRIB_COUNT, handle);
18 glDeleteVertexArrays(1, &va);
21 Model::Model(Model &&other)
23 , count(other.count) {
25 for (int i = 0; i < ATTRIB_COUNT; ++i) {
26 handle[i] = other.handle[i];
31 Model &Model::operator =(Model &&other) {
32 std::swap(va, other.va);
33 for (int i = 0; i < ATTRIB_COUNT; ++i) {
34 std::swap(handle[i], other.handle[i]);
40 void Model::Update(const Buffer &buf) {
41 glBindVertexArray(va);
42 glBindBuffer(GL_ARRAY_BUFFER, handle[ATTRIB_VERTEX]);
43 glBufferData(GL_ARRAY_BUFFER, buf.vertices.size() * sizeof(glm::vec3), buf.vertices.data(), GL_STATIC_DRAW);
44 glEnableVertexAttribArray(ATTRIB_VERTEX);
45 glVertexAttribPointer(
46 ATTRIB_VERTEX, // location (for shader)
49 GL_FALSE, // normalized
55 if (buf.colors.size() < buf.vertices.size()) {
56 std::cerr << "Model: not enough colors!" << std::endl;
59 glBindBuffer(GL_ARRAY_BUFFER, handle[ATTRIB_COLOR]);
60 glBufferData(GL_ARRAY_BUFFER, buf.colors.size() * sizeof(glm::vec3), buf.colors.data(), GL_STATIC_DRAW);
61 glEnableVertexAttribArray(ATTRIB_COLOR);
62 glVertexAttribPointer(
63 ATTRIB_COLOR, // location (for shader)
66 GL_FALSE, // normalized
72 if (buf.normals.size() < buf.vertices.size()) {
73 std::cerr << "Model: not enough normals!" << std::endl;
76 glBindBuffer(GL_ARRAY_BUFFER, handle[ATTRIB_NORMAL]);
77 glBufferData(GL_ARRAY_BUFFER, buf.normals.size() * sizeof(glm::vec3), buf.normals.data(), GL_STATIC_DRAW);
78 glEnableVertexAttribArray(ATTRIB_NORMAL);
79 glVertexAttribPointer(
80 ATTRIB_NORMAL, // location (for shader)
83 GL_FALSE, // normalized
88 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, handle[ATTRIB_INDEX]);
89 glBufferData(GL_ELEMENT_ARRAY_BUFFER, buf.indices.size() * sizeof(Index), buf.indices.data(), GL_STATIC_DRAW);
90 count = buf.indices.size();
94 void Model::Draw() const {
95 glBindVertexArray(va);
99 GL_UNSIGNED_INT, // type
105 BlockModel::BlockModel()
109 glGenVertexArrays(1, &va);
110 glGenBuffers(ATTRIB_COUNT, handle);
113 BlockModel::~BlockModel() {
114 glDeleteBuffers(ATTRIB_COUNT, handle);
115 glDeleteVertexArrays(1, &va);
118 BlockModel::BlockModel(BlockModel &&other)
120 , count(other.count) {
122 for (int i = 0; i < ATTRIB_COUNT; ++i) {
123 handle[i] = other.handle[i];
128 BlockModel &BlockModel::operator =(BlockModel &&other) {
129 std::swap(va, other.va);
130 for (int i = 0; i < ATTRIB_COUNT; ++i) {
131 std::swap(handle[i], other.handle[i]);
137 void BlockModel::Update(const Buffer &buf) {
138 glBindVertexArray(va);
139 glBindBuffer(GL_ARRAY_BUFFER, handle[ATTRIB_VERTEX]);
140 glBufferData(GL_ARRAY_BUFFER, buf.vertices.size() * sizeof(glm::vec3), buf.vertices.data(), GL_STATIC_DRAW);
141 glEnableVertexAttribArray(ATTRIB_VERTEX);
142 glVertexAttribPointer(
143 ATTRIB_VERTEX, // location (for shader)
146 GL_FALSE, // normalized
152 if (buf.colors.size() < buf.vertices.size()) {
153 std::cerr << "BlockModel: not enough colors!" << std::endl;
156 glBindBuffer(GL_ARRAY_BUFFER, handle[ATTRIB_COLOR]);
157 glBufferData(GL_ARRAY_BUFFER, buf.colors.size() * sizeof(glm::vec3), buf.colors.data(), GL_STATIC_DRAW);
158 glEnableVertexAttribArray(ATTRIB_COLOR);
159 glVertexAttribPointer(
160 ATTRIB_COLOR, // location (for shader)
163 GL_FALSE, // normalized
169 if (buf.lights.size() < buf.vertices.size()) {
170 std::cerr << "BlockModel: not enough lights!" << std::endl;
173 glBindBuffer(GL_ARRAY_BUFFER, handle[ATTRIB_LIGHT]);
174 glBufferData(GL_ARRAY_BUFFER, buf.lights.size() * sizeof(float), buf.lights.data(), GL_STATIC_DRAW);
175 glEnableVertexAttribArray(ATTRIB_LIGHT);
176 glVertexAttribPointer(
177 ATTRIB_LIGHT, // location (for shader)
180 GL_FALSE, // normalized
185 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, handle[ATTRIB_INDEX]);
186 glBufferData(GL_ELEMENT_ARRAY_BUFFER, buf.indices.size() * sizeof(Index), buf.indices.data(), GL_STATIC_DRAW);
187 count = buf.indices.size();
191 void BlockModel::Draw() const {
192 glBindVertexArray(va);
196 GL_UNSIGNED_INT, // type
201 OutlineModel::OutlineModel()
208 glGenVertexArrays(1, &va);
209 glGenBuffers(ATTRIB_COUNT, handle);
212 OutlineModel::~OutlineModel() {
213 glDeleteBuffers(ATTRIB_COUNT, handle);
214 glDeleteVertexArrays(1, &va);
218 void OutlineModel::Clear() {
225 void OutlineModel::Reserve(int v, int i) {
232 void OutlineModel::Update() {
233 glBindBuffer(GL_ARRAY_BUFFER, handle[ATTRIB_VERTEX]);
234 glBufferData(GL_ARRAY_BUFFER, vertices.size() * sizeof(glm::vec3), vertices.data(), GL_STATIC_DRAW);
235 glEnableVertexAttribArray(ATTRIB_VERTEX);
236 glVertexAttribPointer(
237 ATTRIB_VERTEX, // location (for shader)
240 GL_FALSE, // normalized
246 if (colors.size() < vertices.size()) {
247 std::cerr << "OutlineModel: not enough colors!" << std::endl;
248 colors.resize(vertices.size(), { 1, 0, 1 });
251 glBindBuffer(GL_ARRAY_BUFFER, handle[ATTRIB_COLOR]);
252 glBufferData(GL_ARRAY_BUFFER, colors.size() * sizeof(glm::vec3), colors.data(), GL_STATIC_DRAW);
253 glEnableVertexAttribArray(ATTRIB_COLOR);
254 glVertexAttribPointer(
255 ATTRIB_COLOR, // location (for shader)
258 GL_FALSE, // normalized
263 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, handle[ATTRIB_INDEX]);
264 glBufferData(GL_ELEMENT_ARRAY_BUFFER, indices.size() * sizeof(Index), indices.data(), GL_STATIC_DRAW);
270 void OutlineModel::Draw() {
271 glBindVertexArray(va);
277 glEnable(GL_LINE_SMOOTH);
282 indices.size(), // count
283 GL_UNSIGNED_SHORT, // type