DrawMesh.cc 6.16 KB
Newer Older
Jan Möbius's avatar
Jan Möbius committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
/*===========================================================================*\
 *                                                                           *
 *                              OpenFlipper                                  *
 *           Copyright (c) 2001-2015, RWTH-Aachen University                 *
 *           Department of Computer Graphics and Multimedia                  *
 *                          All rights reserved.                             *
 *                            www.openflipper.org                            *
 *                                                                           *
 *---------------------------------------------------------------------------*
 * This file is part of OpenFlipper.                                         *
 *---------------------------------------------------------------------------*
 *                                                                           *
 * Redistribution and use in source and binary forms, with or without        *
 * modification, are permitted provided that the following conditions        *
 * are met:                                                                  *
 *                                                                           *
 * 1. Redistributions of source code must retain the above copyright notice, *
 *    this list of conditions and the following disclaimer.                  *
 *                                                                           *
 * 2. Redistributions in binary form must reproduce the above copyright      *
 *    notice, this list of conditions and the following disclaimer in the    *
 *    documentation and/or other materials provided with the distribution.   *
 *                                                                           *
 * 3. Neither the name of the copyright holder nor the names of its          *
 *    contributors may be used to endorse or promote products derived from   *
 *    this software without specific prior written permission.               *
 *                                                                           *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS       *
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED *
 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A           *
 * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER *
 * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,  *
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,       *
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR        *
 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF    *
 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING      *
 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS        *
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.              *
 *                                                                           *
\*===========================================================================*/

42

Jan Möbius's avatar
Jan Möbius committed
43 44 45 46 47 48 49 50 51 52 53

#include <ACG/GL/acg_glew.hh>
#include "DrawMesh.hh"

namespace ACG {

DrawMeshBase::DrawMeshBase() :
        vbo_(0), ibo_(0),
        numTris_(0), numVerts_(0),
        meshComp_(0),
        lineIBO_(0),
54
        heVBO_(0),
Jan Möbius's avatar
Jan Möbius committed
55 56 57 58 59 60 61 62 63 64
        indexType_(0),
        pickVertexIBO_(0) {

    vertexDecl_ = new VertexDeclaration;
    vertexDeclEdgeCol_ = new VertexDeclaration;
    vertexDeclHalfedgeCol_ = new VertexDeclaration;
    vertexDeclHalfedgePos_ = new VertexDeclaration;
}

DrawMeshBase::~DrawMeshBase() {
65 66 67 68
    if (vbo_) glDeleteBuffers(1, &vbo_);
    if (ibo_) glDeleteBuffers(1, &ibo_);
    if (lineIBO_) glDeleteBuffers(1, &lineIBO_);
    if (heVBO_) glDeleteBuffers(1, &heVBO_);
Jan Möbius's avatar
Jan Möbius committed
69 70 71 72 73 74

    delete vertexDecl_;
    delete vertexDeclEdgeCol_;
    delete vertexDeclHalfedgeCol_;
    delete vertexDeclHalfedgePos_;

75
    if (pickVertexIBO_) glDeleteBuffers(1, &pickVertexIBO_);
Jan Möbius's avatar
Jan Möbius committed
76 77 78 79 80 81 82 83 84 85
}

void DrawMeshBase::deleteIbo() {
    if (ibo_)
        glDeleteBuffers(1, &ibo_);
    ibo_ = 0;
}

void DrawMeshBase::bindVbo() {
    if (!vbo_)
86
      glGenBuffers(1, &vbo_);
Jan Möbius's avatar
Jan Möbius committed
87 88 89 90 91 92

    ACG::GLState::bindBufferARB(GL_ARRAY_BUFFER_ARB, vbo_);
}

void DrawMeshBase::bindIbo() {
    if (!ibo_)
93
      glGenBuffers(1, &ibo_);
Jan Möbius's avatar
Jan Möbius committed
94 95 96 97 98 99

    ACG::GLState::bindBufferARB(GL_ELEMENT_ARRAY_BUFFER_ARB, ibo_);
}

void DrawMeshBase::bindLineIbo() {
    if (!lineIBO_)
100
      glGenBuffers(1, &lineIBO_);
Jan Möbius's avatar
Jan Möbius committed
101 102 103 104

    ACG::GLState::bindBufferARB(GL_ELEMENT_ARRAY_BUFFER, lineIBO_);
}

105 106 107
void DrawMeshBase::bindHEVbo() {
  glGetIntegerv(GL_ARRAY_BUFFER_BINDING,&prevVBO_);
    if (!heVBO_)
108
      glGenBuffers(1, &heVBO_);
109 110 111 112 113 114 115 116

    ACG::GLState::bindBufferARB(GL_ARRAY_BUFFER_ARB, heVBO_);
}

void DrawMeshBase::unbindHEVbo() {
    ACG::GLState::bindBufferARB(GL_ARRAY_BUFFER_ARB, prevVBO_);
}

Jan Möbius's avatar
Jan Möbius committed
117 118
void DrawMeshBase::bindPickVertexIbo() {
    if (!pickVertexIBO_)
119
        glGenBuffers(1, &pickVertexIBO_);
Jan Möbius's avatar
Jan Möbius committed
120

121
    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER_ARB, pickVertexIBO_);
Jan Möbius's avatar
Jan Möbius committed
122 123 124
}

void DrawMeshBase::createIndexBuffer() {
125
        glBufferData(GL_ELEMENT_ARRAY_BUFFER_ARB,
Jan Möbius's avatar
Jan Möbius committed
126 127 128 129 130 131 132
            numTris_ * 3 * sizeof(unsigned int),
            meshComp_->getIndexBuffer(), GL_STATIC_DRAW_ARB);
}

void DrawMeshBase::fillLineBuffer(size_t n_edges, void *data) {
    // 2 or 4 byte indices:
    if (indexType_ == GL_UNSIGNED_SHORT)
133
      glBufferData(GL_ELEMENT_ARRAY_BUFFER_ARB,
Jan Möbius's avatar
Jan Möbius committed
134 135 136
                   n_edges * 2 * sizeof(unsigned short),
                   data, GL_STATIC_DRAW_ARB);
    else
137
      glBufferData(GL_ELEMENT_ARRAY_BUFFER_ARB,
Jan Möbius's avatar
Jan Möbius committed
138 139 140 141
                   n_edges * 2 * sizeof(unsigned int),
                   data, GL_STATIC_DRAW_ARB);
}

142 143 144 145 146 147 148
void DrawMeshBase::fillHEVBO(size_t numberOfElements_, size_t sizeOfElements_, void* data_)
{
  bindHEVbo();
  glBufferData(GL_ARRAY_BUFFER,numberOfElements_ * sizeOfElements_, data_, GL_STATIC_DRAW);
  unbindHEVbo();
}

Jan Möbius's avatar
Jan Möbius committed
149 150
void DrawMeshBase::fillVertexBuffer() {
    if (!vertices_.empty())
151
      glBufferData(GL_ARRAY_BUFFER_ARB, numVerts_ * vertexDecl_->getVertexStride(), &vertices_[0], GL_STATIC_DRAW_ARB);
Jan Möbius's avatar
Jan Möbius committed
152 153 154
}

void DrawMeshBase::fillInvVertexMap(size_t n_vertices, void *data) {
155
    glBufferData(GL_ELEMENT_ARRAY_BUFFER_ARB, sizeof(int) * n_vertices, data, GL_STATIC_DRAW);
Jan Möbius's avatar
Jan Möbius committed
156 157 158
}

} /* namespace ACG */