1 /*========================================================================= 2 3 Program: Visualization Toolkit 4 5 Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen 6 All rights reserved. 7 See Copyright.txt or http://www.kitware.com/Copyright.htm for details. 8 9 This software is distributed WITHOUT ANY WARRANTY; without even 10 the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR 11 PURPOSE. See the above copyright notice for more information. 12 13 =========================================================================*/ 14 15 #ifndef vtkGLVBOHelpher_h 16 #define vtkGLVBOHelpher_h 17 18 #include "vtkRenderingOpenGL2Module.h" // for export macro 19 20 #include "vtkglBufferObject.h" 21 #include "vtkglVertexArrayObject.h" 22 23 #include "vtk_glew.h" // used for struct ivars 24 #include <vector> // used for struct ivars 25 #include "vtkTimeStamp.h" // used for struct ivars 26 27 class vtkCellArray; 28 class vtkPoints; 29 class vtkDataArray; 30 class vtkPolyData; 31 class vtkOpenGLShaderCache; 32 class vtkWindow; 33 34 namespace vtkgl 35 { 36 37 // Process the string, and return a version with replacements. 38 std::string VTKRENDERINGOPENGL2_EXPORT replace(std::string source, 39 const std::string &search, 40 const std::string replace, bool all = true); 41 42 // perform in place string substitutions, indicate if a substitution was done 43 bool VTKRENDERINGOPENGL2_EXPORT substitute(std::string &source, 44 const std::string &search, 45 const std::string replace, bool all = true); 46 47 // used to create an IBO for triangle primatives 48 size_t CreateTriangleIndexBuffer(vtkCellArray *cells, 49 BufferObject &indexBuffer, 50 vtkPoints *points, std::vector<unsigned int> &cellPointMap); 51 52 // used to create an IBO for triangle primatives 53 void AppendTriangleIndexBuffer( 54 std::vector<unsigned int> &indexArray, 55 vtkCellArray *cells, 56 vtkPoints *points, 57 std::vector<unsigned int> &cellPointMap, 58 vtkIdType vertexOffset); 59 60 // create a IBO for wireframe polys/tris 61 size_t CreateTriangleLineIndexBuffer(vtkCellArray *cells, 62 BufferObject &indexBuffer); 63 64 // create a IBO for wireframe polys/tris 65 void AppendTriangleLineIndexBuffer( 66 std::vector<unsigned int> &indexArray, 67 vtkCellArray *cells, 68 vtkIdType vertexOffset); 69 70 // used to create an IBO for primatives as points 71 size_t CreatePointIndexBuffer(vtkCellArray *cells, BufferObject &indexBuffer); 72 73 // used to create an IBO for primatives as points 74 void AppendPointIndexBuffer( 75 std::vector<unsigned int> &indexArray, 76 vtkCellArray *cells, 77 vtkIdType vertexOffset); 78 79 // used to create an IBO for line strips and triangle strips 80 size_t CreateMultiIndexBuffer(vtkCellArray *cells, BufferObject &indexBuffer, 81 std::vector<GLintptr> &memoryOffsetArray, 82 std::vector<unsigned int> &elementCountArray, 83 bool wireframeTriStrips); 84 85 // special index buffer for polys wireframe with edge visibilityflags 86 size_t CreateEdgeFlagIndexBuffer(vtkCellArray *cells, BufferObject &indexBuffer, 87 vtkDataArray *edgeflags); 88 89 // Store the shaders, program, and ibo in a common struct. 90 class VTKRENDERINGOPENGL2_EXPORT CellBO 91 { 92 public: 93 vtkShaderProgram *Program; 94 BufferObject ibo; 95 VertexArrayObject vao; 96 vtkTimeStamp ShaderSourceTime; 97 98 size_t indexCount; 99 // These are client side objects for multi draw where IBOs are not used. 100 std::vector<GLintptr> offsetArray; 101 std::vector<unsigned int> elementsArray; 102 103 vtkTimeStamp attributeUpdateTime; 104 CellBO()105 CellBO() {this->Program = NULL; }; 106 void ReleaseGraphicsResources(vtkWindow *win); 107 }; 108 109 // Sizes/offsets are all in bytes as OpenGL API expects them. 110 struct VBOLayout 111 { 112 size_t VertexCount; // Number of vertices in the VBO 113 int Stride; // The size of a complete vertex + attributes 114 int VertexOffset; // Offset of the vertex 115 int NormalOffset; // Offset of the normal 116 int TCoordOffset; // Offset of the texture coordinates 117 int TCoordComponents; // Number of texture dimensions 118 int ColorOffset; // Offset of the color 119 int ColorComponents; // Number of color components 120 std::vector<float> PackedVBO; // the data 121 }; 122 123 // Take the points, and pack them into the VBO object supplied. This currently 124 // takes whatever the input type might be and packs them into a VBO using 125 // floats for the vertices and normals, and unsigned char for the colors (if 126 // the array is non-null). 127 VBOLayout CreateVBO(vtkPoints *points, unsigned int numPoints, 128 vtkDataArray *normals, 129 vtkDataArray *tcoords, 130 unsigned char *colors, int colorComponents, 131 BufferObject &vertexBuffer, 132 unsigned int *cellPointMap, unsigned int *pointCellMap, 133 bool cellScalars, bool cellNormals); 134 void AppendVBO(VBOLayout &layout, vtkPoints *points, unsigned int numPoints, 135 vtkDataArray *normals, 136 vtkDataArray *tcoords, 137 unsigned char *colors, int colorComponents, 138 unsigned int *cellPointMap, unsigned int *pointCellMap, 139 bool cellScalars, bool cellNormals); 140 141 142 // used to create an IBO for stripped primatives such as lines and strips 143 void CreateCellSupportArrays(vtkPolyData *poly, vtkCellArray *[4], 144 std::vector<unsigned int> &cellPointMap, 145 std::vector<unsigned int> &pointCellMap); 146 147 } // End namespace 148 149 #endif // vtkGLVBOHelpher_h 150 151 // VTK-HeaderTest-Exclude: vtkglVBOHelper.h 152