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);
96 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, handle[ATTRIB_INDEX]);
100 GL_UNSIGNED_INT, // type
106 BlockModel::BlockModel()
110 glGenVertexArrays(1, &va);
111 glGenBuffers(ATTRIB_COUNT, handle);
114 BlockModel::~BlockModel() {
115 glDeleteBuffers(ATTRIB_COUNT, handle);
116 glDeleteVertexArrays(1, &va);
119 BlockModel::BlockModel(BlockModel &&other)
121 , count(other.count) {
123 for (int i = 0; i < ATTRIB_COUNT; ++i) {
124 handle[i] = other.handle[i];
129 BlockModel &BlockModel::operator =(BlockModel &&other) {
130 std::swap(va, other.va);
131 for (int i = 0; i < ATTRIB_COUNT; ++i) {
132 std::swap(handle[i], other.handle[i]);
138 void BlockModel::Update(const Buffer &buf) {
139 glBindVertexArray(va);
140 glBindBuffer(GL_ARRAY_BUFFER, handle[ATTRIB_VERTEX]);
141 glBufferData(GL_ARRAY_BUFFER, buf.vertices.size() * sizeof(glm::vec3), buf.vertices.data(), GL_STATIC_DRAW);
142 glEnableVertexAttribArray(ATTRIB_VERTEX);
143 glVertexAttribPointer(
144 ATTRIB_VERTEX, // location (for shader)
147 GL_FALSE, // normalized
153 if (buf.colors.size() < buf.vertices.size()) {
154 std::cerr << "BlockModel: not enough colors!" << std::endl;
157 glBindBuffer(GL_ARRAY_BUFFER, handle[ATTRIB_COLOR]);
158 glBufferData(GL_ARRAY_BUFFER, buf.colors.size() * sizeof(glm::vec3), buf.colors.data(), GL_STATIC_DRAW);
159 glEnableVertexAttribArray(ATTRIB_COLOR);
160 glVertexAttribPointer(
161 ATTRIB_COLOR, // location (for shader)
164 GL_FALSE, // normalized
170 if (buf.normals.size() < buf.vertices.size()) {
171 std::cerr << "BlockModel: not enough normals!" << std::endl;
174 glBindBuffer(GL_ARRAY_BUFFER, handle[ATTRIB_NORMAL]);
175 glBufferData(GL_ARRAY_BUFFER, buf.normals.size() * sizeof(glm::vec3), buf.normals.data(), GL_STATIC_DRAW);
176 glEnableVertexAttribArray(ATTRIB_NORMAL);
177 glVertexAttribPointer(
178 ATTRIB_NORMAL, // location (for shader)
181 GL_FALSE, // normalized
187 if (buf.lights.size() < buf.vertices.size()) {
188 std::cerr << "BlockModel: not enough lights!" << std::endl;
191 glBindBuffer(GL_ARRAY_BUFFER, handle[ATTRIB_LIGHT]);
192 glBufferData(GL_ARRAY_BUFFER, buf.lights.size() * sizeof(float), buf.lights.data(), GL_STATIC_DRAW);
193 glEnableVertexAttribArray(ATTRIB_LIGHT);
194 glVertexAttribPointer(
195 ATTRIB_LIGHT, // location (for shader)
198 GL_FALSE, // normalized
203 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, handle[ATTRIB_INDEX]);
204 glBufferData(GL_ELEMENT_ARRAY_BUFFER, buf.indices.size() * sizeof(Index), buf.indices.data(), GL_STATIC_DRAW);
205 count = buf.indices.size();
209 void BlockModel::Draw() const {
210 glBindVertexArray(va);
211 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, handle[ATTRIB_INDEX]);
215 GL_UNSIGNED_INT, // type
220 OutlineModel::OutlineModel()
227 glGenVertexArrays(1, &va);
228 glGenBuffers(ATTRIB_COUNT, handle);
231 OutlineModel::~OutlineModel() {
232 glDeleteBuffers(ATTRIB_COUNT, handle);
233 glDeleteVertexArrays(1, &va);
237 void OutlineModel::Clear() {
244 void OutlineModel::Reserve(int v, int i) {
251 void OutlineModel::Update() {
252 glBindBuffer(GL_ARRAY_BUFFER, handle[ATTRIB_VERTEX]);
253 glBufferData(GL_ARRAY_BUFFER, vertices.size() * sizeof(glm::vec3), vertices.data(), GL_STATIC_DRAW);
254 glEnableVertexAttribArray(ATTRIB_VERTEX);
255 glVertexAttribPointer(
256 ATTRIB_VERTEX, // location (for shader)
259 GL_FALSE, // normalized
265 if (colors.size() < vertices.size()) {
266 std::cerr << "OutlineModel: not enough colors!" << std::endl;
267 colors.resize(vertices.size(), { 1, 0, 1 });
270 glBindBuffer(GL_ARRAY_BUFFER, handle[ATTRIB_COLOR]);
271 glBufferData(GL_ARRAY_BUFFER, colors.size() * sizeof(glm::vec3), colors.data(), GL_STATIC_DRAW);
272 glEnableVertexAttribArray(ATTRIB_COLOR);
273 glVertexAttribPointer(
274 ATTRIB_COLOR, // location (for shader)
277 GL_FALSE, // normalized
282 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, handle[ATTRIB_INDEX]);
283 glBufferData(GL_ELEMENT_ARRAY_BUFFER, indices.size() * sizeof(Index), indices.data(), GL_STATIC_DRAW);
289 void OutlineModel::Draw() {
290 glBindVertexArray(va);
296 glEnable(GL_LINE_SMOOTH);
299 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, handle[ATTRIB_INDEX]);
302 indices.size(), // count
303 GL_UNSIGNED_SHORT, // type