vtkOpenGLRenderUtilities.cxx 3.15 KB
Newer Older
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 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91
/*=========================================================================

  Program:   Visualization Toolkit
  Module:    vtkOpenGLRenderUtilities.cxx

  Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
  All rights reserved.
  See Copyright.txt or http://www.kitware.com/Copyright.htm for details.

     This software is distributed WITHOUT ANY WARRANTY; without even
     the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
     PURPOSE.  See the above copyright notice for more information.

=========================================================================*/
#include "vtk_glew.h"
#include "vtkOpenGLRenderUtilities.h"

#include "vtkNew.h"
#include "vtkOpenGLBufferObject.h"
#include "vtkOpenGLVertexArrayObject.h"

// ----------------------------------------------------------------------------
vtkOpenGLRenderUtilities::vtkOpenGLRenderUtilities()
{
}

// ----------------------------------------------------------------------------
vtkOpenGLRenderUtilities::~vtkOpenGLRenderUtilities()
{
}

void vtkOpenGLRenderUtilities::PrintSelf(ostream& os, vtkIndent indent)
{
  this->Superclass::PrintSelf(os,indent);
}

// ---------------------------------------------------------------------------
// a program must be bound
// a VAO must be bound
void vtkOpenGLRenderUtilities::RenderQuad(
  float *verts,
  float *tcoords,
  vtkShaderProgram *program, vtkOpenGLVertexArrayObject *vao)
{
  GLuint iboData[] = {0, 1, 2, 0, 2, 3};
  vtkOpenGLRenderUtilities::RenderTriangles(verts, 4,
    iboData, 6,
    tcoords,
    program, vao);
}

// ---------------------------------------------------------------------------
// a program must be bound
// a VAO must be bound
void vtkOpenGLRenderUtilities::RenderTriangles(
  float *verts, unsigned int numVerts,
  GLuint *iboData, unsigned int numIndices,
  float *tcoords,
  vtkShaderProgram *program, vtkOpenGLVertexArrayObject *vao)
{
  if (!program || !vao || !verts)
    {
    vtkGenericWarningMacro(<< "Error must have verts, program and vao");
    }

  vtkNew<vtkOpenGLBufferObject> vbo;
  vbo->Upload(verts, numVerts*3, vtkOpenGLBufferObject::ArrayBuffer);
  vao->Bind();
  if (!vao->AddAttributeArray(program, vbo.Get(), "vertexMC", 0,
      sizeof(float)*3, VTK_FLOAT, 3, false))
    {
    vtkGenericWarningMacro(<< "Error setting 'vertexMC' in shader VAO.");
    }

  vtkNew<vtkOpenGLBufferObject> tvbo;
  if (tcoords)
    {
    tvbo->Upload(tcoords, numVerts*2, vtkOpenGLBufferObject::ArrayBuffer);
    if (!vao->AddAttributeArray(program, tvbo.Get(), "tcoordMC", 0,
        sizeof(float)*2, VTK_FLOAT, 2, false))
      {
      vtkGenericWarningMacro(<< "Error setting 'tcoordMC' in shader VAO.");
      }
    }

  vtkNew<vtkOpenGLBufferObject> ibo;
  vao->Bind();
  ibo->Upload(iboData, numIndices, vtkOpenGLBufferObject::ElementArrayBuffer);
  glDrawElements(GL_TRIANGLES, numIndices, GL_UNSIGNED_INT,
    reinterpret_cast<const GLvoid *>(NULL));
  ibo->Release();
92
  ibo->ReleaseGraphicsResources();
93 94 95 96
  vao->RemoveAttributeArray("vertexMC");
  vao->RemoveAttributeArray("tcoordMC");
  vao->Release();
  vbo->Release();
97
  vbo->ReleaseGraphicsResources();
98 99 100
  if (tcoords)
    {
    tvbo->Release();
101
    tvbo->ReleaseGraphicsResources();
102 103
    }
}