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.lights.size() < buf.vertices.size()) {
171 std::cerr << "BlockModel: not enough lights!" << std::endl;
174 glBindBuffer(GL_ARRAY_BUFFER, handle[ATTRIB_LIGHT]);
175 glBufferData(GL_ARRAY_BUFFER, buf.lights.size() * sizeof(float), buf.lights.data(), GL_STATIC_DRAW);
176 glEnableVertexAttribArray(ATTRIB_LIGHT);
177 glVertexAttribPointer(
178 ATTRIB_LIGHT, // location (for shader)
181 GL_FALSE, // normalized
186 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, handle[ATTRIB_INDEX]);
187 glBufferData(GL_ELEMENT_ARRAY_BUFFER, buf.indices.size() * sizeof(Index), buf.indices.data(), GL_STATIC_DRAW);
188 count = buf.indices.size();
192 void BlockModel::Draw() const {
193 glBindVertexArray(va);
194 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, handle[ATTRIB_INDEX]);
198 GL_UNSIGNED_INT, // type
203 OutlineModel::OutlineModel()
210 glGenVertexArrays(1, &va);
211 glGenBuffers(ATTRIB_COUNT, handle);
214 OutlineModel::~OutlineModel() {
215 glDeleteBuffers(ATTRIB_COUNT, handle);
216 glDeleteVertexArrays(1, &va);
220 void OutlineModel::Clear() {
227 void OutlineModel::Reserve(int v, int i) {
234 void OutlineModel::Update() {
235 glBindBuffer(GL_ARRAY_BUFFER, handle[ATTRIB_VERTEX]);
236 glBufferData(GL_ARRAY_BUFFER, vertices.size() * sizeof(glm::vec3), vertices.data(), GL_STATIC_DRAW);
237 glEnableVertexAttribArray(ATTRIB_VERTEX);
238 glVertexAttribPointer(
239 ATTRIB_VERTEX, // location (for shader)
242 GL_FALSE, // normalized
248 if (colors.size() < vertices.size()) {
249 std::cerr << "OutlineModel: not enough colors!" << std::endl;
250 colors.resize(vertices.size(), { 1, 0, 1 });
253 glBindBuffer(GL_ARRAY_BUFFER, handle[ATTRIB_COLOR]);
254 glBufferData(GL_ARRAY_BUFFER, colors.size() * sizeof(glm::vec3), colors.data(), GL_STATIC_DRAW);
255 glEnableVertexAttribArray(ATTRIB_COLOR);
256 glVertexAttribPointer(
257 ATTRIB_COLOR, // location (for shader)
260 GL_FALSE, // normalized
265 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, handle[ATTRIB_INDEX]);
266 glBufferData(GL_ELEMENT_ARRAY_BUFFER, indices.size() * sizeof(Index), indices.data(), GL_STATIC_DRAW);
272 void OutlineModel::Draw() {
273 glBindVertexArray(va);
279 glEnable(GL_LINE_SMOOTH);
282 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, handle[ATTRIB_INDEX]);
285 indices.size(), // count
286 GL_UNSIGNED_SHORT, // type