MVE - Multi-View Environment mve-devel
Loading...
Searching...
No Matches
vertex_array.cc
Go to the documentation of this file.
1/*
2 * Copyright (C) 2015, Simon Fuhrmann
3 * TU Darmstadt - Graphics, Capture and Massively Parallel Computing
4 * All rights reserved.
5 *
6 * This software may be modified and distributed under the terms
7 * of the BSD 3-Clause license. See the LICENSE.txt file for details.
8 */
9
10#include <algorithm>
11#include <stdexcept>
12
13#include "ogl/vertex_array.h"
14
16
17struct VBORemovePred
18{
19 std::string name;
20 VBORemovePred (std::string const& name) : name(name) {}
21 bool operator() (VertexArray::BoundVBO const& o)
22 { return o.second == name; }
23};
24
25void
26VertexArray::remove_vbo (std::string const& name)
27{
28 VBOList::iterator new_end = std::remove_if(this->vbo_list.begin(),
29 this->vbo_list.end(), VBORemovePred(name));
30 this->vbo_list.erase(new_end, this->vbo_list.end());
31}
32
33/* ---------------------------------------------------------------- */
34
35void
36VertexArray::assign_attrib (BoundVBO const& bound_vbo)
37{
38 VertexBuffer::Ptr vbo = bound_vbo.first;
39 std::string const& name = bound_vbo.second;
40
41 GLint location = this->shader->get_attrib_location(name.c_str());
42 if (location < 0)
43 return;
44
45 vbo->bind();
46 glVertexAttribPointer(location, vbo->get_values_per_vertex(),
47 vbo->get_data_type(), GL_TRUE, 0, nullptr);
49 glEnableVertexAttribArray(location);
51}
52
53/* ---------------------------------------------------------------- */
54
55void
56VertexArray::draw (void)
57{
58 if (this->vert_vbo == nullptr)
59 throw std::runtime_error("No vertex VBO set!");
60
61 if (this->shader == nullptr)
62 throw std::runtime_error("No shader program set!");
63
64 /* Make current vertex array active. */
65 glBindVertexArray(this->vao_id);
67
68 /* Bind the shader program. */
69 this->shader->bind();
70
71 /* Assign vertex positions attribute. */
72 this->assign_attrib(BoundVBO(this->vert_vbo, OGL_ATTRIB_POSITION));
73
74 /* Assign generic vertex attributes. */
75 for (std::size_t i = 0; i < this->vbo_list.size(); ++i)
76 this->assign_attrib(this->vbo_list[i]);
77
78 /* Draw triangles if indices are given, draw points otherwise. */
79 if (this->index_vbo != nullptr)
80 {
81 this->index_vbo->bind();
82 glDrawElements(this->primitive, this->index_vbo->get_element_amount(),
83 GL_UNSIGNED_INT, nullptr);
85 }
86 else
87 {
88 glDrawArrays(this->primitive, 0, this->vert_vbo->get_element_amount());
90 }
91
92 this->shader->unbind();
93 glBindVertexArray(0);
95}
96
std::pair< VertexBuffer::Ptr, std::string > BoundVBO
std::shared_ptr< VertexBuffer > Ptr
void check_gl_error()
#define OGL_NAMESPACE_END
Definition defines.h:14
#define OGL_NAMESPACE_BEGIN
Definition defines.h:13
#define OGL_ATTRIB_POSITION