GteIndexBuffer.h
Go to the documentation of this file.
1 // David Eberly, Geometric Tools, Redmond WA 98052
2 // Copyright (c) 1998-2017
3 // Distributed under the Boost Software License, Version 1.0.
4 // http://www.boost.org/LICENSE_1_0.txt
5 // http://www.geometrictools.com/License/Boost/LICENSE_1_0.txt
6 // File Version: 3.0.0 (2016/06/19)
7 
8 #pragma once
9 
10 #include <LowLevel/GteLogger.h>
11 #include <Graphics/GteBuffer.h>
13 #include <cstdint>
14 
15 // Test for mismatches of primitive type in {Set,Get}Point, {Set,Get}Segment,
16 // and {Set,Get}Triangle.
17 //#define GTE_VERIFY_PRIMITIVE_TYPE
18 
19 namespace gte
20 {
21 
23 {
24 public:
25  // Construction. The first constructor leads to a DrawIndexed call in the
26  // graphics engine.
27  IndexBuffer(IPType type, uint32_t numPrimitives, size_t indexSize, bool createStorage = true);
28 
29  // Use this constructor when you want the indexing to be implied by the
30  // ordering of vertices in the vertex buffer. This leads to a Draw call
31  // in the graphics engine. You must ensure that numPrimitives and
32  // numVertices (in the VertexBuffer) are consistent. The usage flag is
33  // not applicable, because there is no system-memory resource data.
34  IndexBuffer(IPType type, uint32_t numPrimitives);
35 
36  // Member access.
37  inline IPType GetPrimitiveType() const;
38  inline uint32_t GetNumPrimitives() const;
39  inline bool IsIndexed() const;
40 
41  // Specify how many primitives are to be drawn (if you do not want them
42  // all drawn). The default value is mNumPrimitives. The function
43  // SetNumActivePrimitives ensures that the input satisfies
44  // numActive <= mNumPrimitives.
45  void SetNumActivePrimitives(uint32_t numActive);
46  inline uint32_t GetNumActivePrimitives() const;
47  uint32_t GetNumActiveIndices() const;
48 
49  // Specify the index of the first primitive to be drawn. The default
50  // value is zero. If you plan to modify both the number of active
51  // primitives and the first primitive to be drawn, set the number of
52  // of active primitives first. SetFirstPrimitive ensures that
53  // first < mNumPrimitives and first + numActive <= mNumPrimitives.
54  void SetFirstPrimitive(uint32_t first);
55  inline uint32_t GetFirstPrimitive() const;
56  uint32_t GetFirstIndex() const;
57 
58  // Support for set/get of primitive indices. The functions return 'true'
59  // iff the index i is within range for the primitive. The caller is
60  // responsible for using the correct functions for the primitive type.
61  // If you want to trap mismatches, enable GTE_VERIFY_PRIMITIVE_TYPE.
62  // These functions have per-primitive overhead, namely, various range
63  // checks and typecasting, so consider these a convenience. For optimum
64  // speed, you can use
65  // std::shared_ptr<IndexBuffer> ibuffer = std::make_shared<IndexBuffer>(...);
66  // type* indices = (type*)ibuffer->GetRawData();
67  // <set or get indices[i]>;
68  // where 'type' is 'int' or 'short' depending on how you constructed the
69  // index buffer.
70  bool SetPoint(uint32_t i, uint32_t v);
71  bool GetPoint(uint32_t i, uint32_t& v) const;
72  bool SetSegment(uint32_t i, uint32_t v0, uint32_t v1);
73  bool GetSegment(uint32_t i, uint32_t& v0, uint32_t& v1) const;
74  bool SetTriangle(uint32_t i, uint32_t v0, uint32_t v1, uint32_t v2);
75  bool GetTriangle(uint32_t i, uint32_t& v0, uint32_t& v1, uint32_t& v2) const;
76 
77  // No member functions exist currently to set the IP_*_ADJ index values.
78  // The layout of the indices is shown in
79  // https://msdn.microsoft.com/en-us/library/windows/desktop/bb205124(v=vs.85).aspx
80 
81 protected:
83  uint32_t mNumPrimitives;
85  uint32_t mFirstPrimitive;
86 
87  inline bool ValidPrimitiveType(IPType type) const;
88 
89  typedef uint32_t(*ICFunction)(uint32_t);
90  static ICFunction msIndexCounter[IP_NUM_TYPES];
91 
92  static uint32_t GetPolypointIndexCount(uint32_t numPrimitives);
93  static uint32_t GetPolysegmentDisjointIndexCount(uint32_t numPrimitives);
94  static uint32_t GetPolysegmentContiguousIndexCount(uint32_t numPrimitives);
95  static uint32_t GetTrimeshIndexCount(uint32_t numPrimitives);
96  static uint32_t GetTristripIndexCount(uint32_t numPrimitives);
97  static uint32_t GetPolysegmentDisjointAdjIndexCount(uint32_t numPrimitives);
98  static uint32_t GetPolysegmentContiguousAdjIndexCount(uint32_t numPrimitives);
99  static uint32_t GetTrimeshAdjIndexCount(uint32_t numPrimitives);
100  static uint32_t GetTristripAdjIndexCount(uint32_t numPrimitives);
101 };
102 
103 
104 inline IPType IndexBuffer::GetPrimitiveType() const
105 {
106  return mPrimitiveType;
107 }
108 
109 inline uint32_t IndexBuffer::GetNumPrimitives() const
110 {
111  return mNumPrimitives;
112 }
113 
114 inline bool IndexBuffer::IsIndexed() const
115 {
116  return mData != nullptr;
117 }
118 
119 inline uint32_t IndexBuffer::GetNumActivePrimitives() const
120 {
121  return mNumActivePrimitives;
122 }
123 
124 inline uint32_t IndexBuffer::GetFirstPrimitive() const
125 {
126  return mFirstPrimitive;
127 }
128 
129 inline bool IndexBuffer::ValidPrimitiveType(IPType type) const
130 {
131  if ((mPrimitiveType & type) != 0)
132  {
133  return true;
134  }
135  else
136  {
137 #if defined(GTE_VERIFY_PRIMITIVE_TYPE)
138  LogError("Invalid primitive type used in Set<Primitive> call.");
139 #endif
140  return false;
141  }
142 }
143 
144 }
uint32_t GetNumPrimitives() const
GLfloat GLfloat v1
Definition: glcorearb.h:812
uint32_t GetNumActivePrimitives() const
uint32_t mNumPrimitives
#define LogError(message)
Definition: GteLogger.h:92
uint32_t GetFirstPrimitive() const
GLint first
Definition: glcorearb.h:400
uint32_t mFirstPrimitive
bool ValidPrimitiveType(IPType type) const
IPType GetPrimitiveType() const
GLfloat v0
Definition: glcorearb.h:811
bool IsIndexed() const
const GLdouble * v
Definition: glcorearb.h:832
GLfloat GLfloat GLfloat v2
Definition: glcorearb.h:813
uint32_t mNumActivePrimitives
#define GTE_IMPEXP
Definition: GTEngineDEF.h:63
GLint GLint GLsizei GLint GLenum GLenum type
Definition: glcorearb.h:103


geometric_tools_engine
Author(s): Yijiang Huang
autogenerated on Thu Jul 18 2019 04:00:00