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 OutlineModel::OutlineModel()
113 glGenVertexArrays(1, &va);
114 glGenBuffers(ATTRIB_COUNT, handle);
117 OutlineModel::~OutlineModel() {
118 glDeleteBuffers(ATTRIB_COUNT, handle);
119 glDeleteVertexArrays(1, &va);
123 void OutlineModel::Clear() {
130 void OutlineModel::Reserve(int v, int i) {
137 void OutlineModel::Update() {
138 glBindBuffer(GL_ARRAY_BUFFER, handle[ATTRIB_VERTEX]);
139 glBufferData(GL_ARRAY_BUFFER, vertices.size() * sizeof(glm::vec3), vertices.data(), GL_STATIC_DRAW);
140 glEnableVertexAttribArray(ATTRIB_VERTEX);
141 glVertexAttribPointer(
142 ATTRIB_VERTEX, // location (for shader)
145 GL_FALSE, // normalized
151 if (colors.size() < vertices.size()) {
152 std::cerr << "OutlineModel: not enough colors!" << std::endl;
153 colors.resize(vertices.size(), { 1, 0, 1 });
156 glBindBuffer(GL_ARRAY_BUFFER, handle[ATTRIB_COLOR]);
157 glBufferData(GL_ARRAY_BUFFER, colors.size() * sizeof(glm::vec3), colors.data(), GL_STATIC_DRAW);
158 glEnableVertexAttribArray(ATTRIB_COLOR);
159 glVertexAttribPointer(
160 ATTRIB_COLOR, // location (for shader)
163 GL_FALSE, // normalized
168 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, handle[ATTRIB_INDEX]);
169 glBufferData(GL_ELEMENT_ARRAY_BUFFER, indices.size() * sizeof(Index), indices.data(), GL_STATIC_DRAW);
175 void OutlineModel::Draw() {
176 glBindVertexArray(va);
182 glEnable(GL_LINE_SMOOTH);
185 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, handle[ATTRIB_INDEX]);
188 indices.size(), // count
189 GL_UNSIGNED_SHORT, // type