GteVisual.cpp
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 #include <GTEnginePCH.h>
9 #include <Graphics/GteVisual.h>
10 using namespace gte;
11 
13 {
14 }
15 
17  std::shared_ptr<VertexBuffer> const& vbuffer,
18  std::shared_ptr<IndexBuffer> const& ibuffer,
19  std::shared_ptr<VisualEffect> const& effect)
20  :
21  mVBuffer(vbuffer),
22  mIBuffer(ibuffer),
23  mEffect(effect)
24 {
25 }
26 
28 {
29  if (!mVBuffer)
30  {
31  LogError("Buffer not attached.");
32  return false;
33  }
34 
35  std::set<DFType> required;
36  required.insert(DF_R32G32B32_FLOAT);
37  required.insert(DF_R32G32B32A32_FLOAT);
38  char const* positions = mVBuffer->GetChannel(VA_POSITION, 0, required);
39  if (positions)
40  {
41  int const numElements = mVBuffer->GetNumElements();
42  int const vertexSize = (int)mVBuffer->GetElementSize();
43  modelBound.ComputeFromData(numElements, vertexSize, positions);
44  return true;
45  }
46 
47  return false;
48 }
49 
51 {
52  if (!mVBuffer || !mIBuffer)
53  {
54  LogError("Buffers not attached.");
55  return false;
56  }
57 
58  // Get vertex positions.
59  std::set<DFType> required;
60  required.insert(DF_R32G32B32_FLOAT);
61  required.insert(DF_R32G32B32A32_FLOAT);
62  char const* positions = mVBuffer->GetChannel(VA_POSITION, 0, required);
63  if (!positions)
64  {
65  return false;
66  }
67 
68  // Get vertex normals.
69  char* normals = mVBuffer->GetChannel(VA_NORMAL, 0, required);
70  if (!normals)
71  {
72  return false;
73  }
74 
75  // Get triangle primitives.
76  IPType primitiveType = mIBuffer->GetPrimitiveType();
77  if ((primitiveType & IP_HAS_TRIANGLES) == 0)
78  {
79  // Normal vectors are not defined for point or segment primitives.
80  return false;
81  }
82 
83  unsigned int const numVertices = mVBuffer->GetNumElements();
84  unsigned int const stride = (int)mVBuffer->GetElementSize();
85  unsigned int i;
86  for (i = 0; i < numVertices; ++i)
87  {
88  Vector3<float>& normal = *(Vector3<float>*)(normals + i*stride);
89  normal = { 0.0f, 0.0f, 0.0f };
90  }
91 
92  unsigned int const numTriangles = mIBuffer->GetNumPrimitives();
93  bool isIndexed = mIBuffer->IsIndexed();
94  for (i = 0; i < numTriangles; ++i)
95  {
96  // Get the vertex indices for the triangle.
97  unsigned int v0, v1, v2;
98  if (isIndexed)
99  {
100  mIBuffer->GetTriangle(i, v0, v1, v2);
101  }
102  else if (primitiveType == IP_TRIMESH)
103  {
104  v0 = 3 * i;
105  v1 = v0 + 1;
106  v2 = v0 + 2;
107  }
108  else // primitiveType == IP_TRISTRIP
109  {
110  int offset = (i & 1);
111  v0 = i + offset;
112  v1 = i + 1 + offset;
113  v2 = i + 2 - offset;
114  }
115 
116  // Get the vertex positions.
117  Vector3<float> pos0 = *(Vector3<float>*)(positions + v0*stride);
118  Vector3<float> pos1 = *(Vector3<float>*)(positions + v1*stride);
119  Vector3<float> pos2 = *(Vector3<float>*)(positions + v2*stride);
120 
121  // Compute the triangle normal. The length of this normal is used
122  // in the weighted sum of normals.
123  Vector3<float> edge1 = pos1 - pos0;
124  Vector3<float> edge2 = pos2 - pos0;
125  Vector3<float> normal = Cross(edge1, edge2);
126 
127  // Add the triangle normal to the vertices' normal sums.
128  Vector3<float>& nor0 = *(Vector3<float>*)(normals + v0*stride);
129  Vector3<float>& nor1 = *(Vector3<float>*)(normals + v1*stride);
130  Vector3<float>& nor2 = *(Vector3<float>*)(normals + v2*stride);
131  nor0 += normal;
132  nor1 += normal;
133  nor2 += normal;
134  }
135 
136  // The vertex normals must be unit-length vectors.
137  for (i = 0; i < numVertices; ++i)
138  {
139  Vector3<float>& normal = *(Vector3<float>*)(normals + i*stride);
140  if (normal != Vector3<float>::Zero())
141  {
142  Normalize(normal);
143  }
144  }
145 
146  return true;
147 }
VA_NORMAL
virtual ~Visual()
Definition: GteVisual.cpp:12
IP_TRIMESH
std::shared_ptr< VertexBuffer > mVBuffer
Definition: GteVisual.h:54
GLfloat GLfloat v1
Definition: glcorearb.h:812
BoundingSphere modelBound
Definition: GteVisual.h:44
bool UpdateModelBound()
Definition: GteVisual.cpp:27
VA_POSITION
typedef int(WINAPI *PFNWGLRELEASEPBUFFERDCARBPROC)(HPBUFFERARB hPbuffer
#define LogError(message)
Definition: GteLogger.h:92
Visual(std::shared_ptr< VertexBuffer > const &vbuffer=std::shared_ptr< VertexBuffer >(), std::shared_ptr< IndexBuffer > const &ibuffer=std::shared_ptr< IndexBuffer >(), std::shared_ptr< VisualEffect > const &effect=std::shared_ptr< VisualEffect >())
Definition: GteVisual.cpp:16
DF_R32G32B32A32_FLOAT
Definition: GteDataFormat.h:20
Real Normalize(GVector< Real > &v, bool robust=false)
Definition: GteGVector.h:454
GLfloat v0
Definition: glcorearb.h:811
DualQuaternion< Real > Cross(DualQuaternion< Real > const &d0, DualQuaternion< Real > const &d1)
GLfloat GLfloat GLfloat v2
Definition: glcorearb.h:813
IP_HAS_TRIANGLES
bool UpdateModelNormals()
Definition: GteVisual.cpp:50
void ComputeFromData(int numVertices, int vertexSize, char const *data)
GLintptr offset
Definition: glcorearb.h:660
GLint GLenum GLboolean GLsizei stride
Definition: glcorearb.h:867
std::shared_ptr< IndexBuffer > mIBuffer
Definition: GteVisual.h:55
DF_R32G32B32_FLOAT
Definition: GteDataFormat.h:20
std::shared_ptr< VisualEffect > mEffect
Definition: GteVisual.h:56


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