Leviathan  0.8.0.0
Leviathan game engine
Leviathan::GeometryHelpers Class Reference

Helpers for generating procedural geometry. More...

#include <GeometryHelpers.h>

Public Member Functions

 GeometryHelpers ()=delete
 

Static Public Member Functions

static DLLEXPORT bs::HMesh CreateScreenSpaceQuad (float x, float y, float width, float height, bool autoflipUV=true)
 Creates a screen space plane with UV coordinates. More...
 
static DLLEXPORT bs::HMesh CreateXZPlane (float width, float height)
 Creates a plane with specific size and UV coordinates with 0, 0 on top left. More...
 

Detailed Description

Helpers for generating procedural geometry.

Definition at line 11 of file GeometryHelpers.h.

Constructor & Destructor Documentation

◆ GeometryHelpers()

Leviathan::GeometryHelpers::GeometryHelpers ( )
delete

Member Function Documentation

◆ CreateScreenSpaceQuad()

DLLEXPORT bs::HMesh GeometryHelpers::CreateScreenSpaceQuad ( float  x,
float  y,
float  width,
float  height,
bool  autoflipUV = true 
)
static

Creates a screen space plane with UV coordinates.

The coordinates are in screen space -1 - 1 where -1 is the left and top of the screen so a full screen quad would be CreateScreenSpaceQuad(-1, -1, 2, 2)

Parameters
meshnameThe name of the created mesh, must be unique
Todo:
Parameter to specify if shadow geometry is needed

Definition at line 11 of file GeometryHelpers.cpp.

13 {
14  static bool flippedY = Engine::Get()->GetGraphics()->IsVerticalUVFlipped();
15 
16  bs::MESH_DESC meshDesc;
17  meshDesc.numVertices = 4;
18  meshDesc.numIndices = 6;
19  meshDesc.indexType = bs::IT_16BIT;
20  meshDesc.usage = bs::MU_STATIC;
21  meshDesc.subMeshes.push_back(bs::SubMesh(0, 6, bs::DOT_TRIANGLE_LIST));
22 
23  bs::SPtr<bs::VertexDataDesc> vertexDesc = bs::VertexDataDesc::create();
24  vertexDesc->addVertElem(bs::VET_FLOAT2, bs::VES_POSITION);
25  vertexDesc->addVertElem(bs::VET_FLOAT2, bs::VES_TEXCOORD);
26  const auto stride = 4;
27  meshDesc.vertexDesc = vertexDesc;
28 
29  bs::SPtr<bs::MeshData> meshData = bs::MeshData::create(4, 6, vertexDesc, bs::IT_16BIT);
30 
31  // Generate vertex data
32  float* vertices = reinterpret_cast<float*>(meshData->getStreamData(0));
33  size_t index = 0;
34 
35  float uvBottom = 1.f;
36  float uvTop = 0.f;
37 
38  if(flippedY && autoflipUV) {
39  uvBottom = 0.f;
40  uvTop = 1.f;
41  }
42 
43  {
44  // First vertex
45  index = 0;
46  vertices[index * stride + 0] = x;
47  vertices[index * stride + 1] = y;
48  vertices[index * stride + 2] = 0;
49  vertices[index * stride + 3] = uvTop;
50  }
51 
52  {
53  // Second
54  index = 1;
55  vertices[index * stride + 0] = x + width;
56  vertices[index * stride + 1] = y;
57  vertices[index * stride + 2] = 1;
58  vertices[index * stride + 3] = uvTop;
59  }
60 
61  {
62  // Third
63  index = 2;
64  vertices[index * stride + 0] = x + width;
65  vertices[index * stride + 1] = y + height;
66  vertices[index * stride + 2] = 1;
67  vertices[index * stride + 3] = uvBottom;
68  }
69 
70  {
71  // Fourth
72  index = 3;
73  vertices[index * stride + 0] = x;
74  vertices[index * stride + 1] = y + height;
75  vertices[index * stride + 2] = 0;
76  vertices[index * stride + 3] = uvBottom;
77  }
78 
79  // 1 to 1 index buffer mapping
80  constexpr uint16_t indicesData[] = {3, 0, 1, 1, 2, 3};
81 
82  std::memcpy(meshData->getIndices16(), indicesData, sizeof(indicesData));
83 
84  return bs::Mesh::create(meshData, meshDesc);
85 
86  // // Set the bounds to get frustum culling and LOD to work correctly.
87  // // To infinite to always render
88  // mesh->_setBounds(Ogre::Aabb::BOX_INFINITE /*, false*/);
89 }
DLLEXPORT bool IsVerticalUVFlipped() const
Definition: Graphics.cpp:568
Graphics * GetGraphics()
Definition: Engine.h:185
unsigned short uint16_t
Definition: core.h:39
static DLLEXPORT Engine * Get()
Definition: Engine.cpp:85

◆ CreateXZPlane()

DLLEXPORT bs::HMesh GeometryHelpers::CreateXZPlane ( float  width,
float  height 
)
static

Creates a plane with specific size and UV coordinates with 0, 0 on top left.

This plane is flat on the Y axis. The actual width of the mesh will be double of width to make it easier to calculate

Todo:

Merge the shared code with CreateScreenSpaceQuad

This needs to be verified that this generates good geometry (currently crashes when recorded with RenderDoc). This might be outdated note after bsf

Definition at line 91 of file GeometryHelpers.cpp.

92 {
93  static bool flippedY = Engine::Get()->GetGraphics()->IsVerticalUVFlipped();
94 
95  const auto x = -width / 2;
96  const auto z = -height / 2;
97 
98  bs::MESH_DESC meshDesc;
99  meshDesc.numVertices = 4;
100  meshDesc.numIndices = 6;
101  meshDesc.indexType = bs::IT_16BIT;
102  meshDesc.usage = bs::MU_STATIC;
103  meshDesc.subMeshes.push_back(bs::SubMesh(0, 6, bs::DOT_TRIANGLE_LIST));
104 
105  bs::SPtr<bs::VertexDataDesc> vertexDesc = bs::VertexDataDesc::create();
106  vertexDesc->addVertElem(bs::VET_FLOAT3, bs::VES_POSITION);
107  vertexDesc->addVertElem(bs::VET_FLOAT2, bs::VES_TEXCOORD);
108  const auto stride = 5;
109  meshDesc.vertexDesc = vertexDesc;
110 
111  bs::SPtr<bs::MeshData> meshData = bs::MeshData::create(4, 6, vertexDesc, bs::IT_16BIT);
112 
113  // Generate vertex data
114  float* vertices = reinterpret_cast<float*>(meshData->getStreamData(0));
115  size_t index = 0;
116 
117  {
118  // First vertex
119  index = 0;
120  vertices[index * stride + 0] = x;
121  vertices[index * stride + 1] = 0;
122  vertices[index * stride + 2] = z;
123  vertices[index * stride + 3] = 0;
124  vertices[index * stride + 4] = 0;
125  }
126 
127  {
128  // Second
129  index = 1;
130  vertices[index * stride + 0] = x + width;
131  vertices[index * stride + 1] = 0;
132  vertices[index * stride + 2] = z;
133  vertices[index * stride + 3] = 1;
134  vertices[index * stride + 4] = 0;
135  }
136 
137  {
138  // Third
139  index = 2;
140  vertices[index * stride + 0] = x + width;
141  vertices[index * stride + 1] = 1;
142  vertices[index * stride + 2] = z + height;
143  vertices[index * stride + 3] = 1;
144  vertices[index * stride + 4] = 1;
145  }
146 
147  {
148  // Fourth
149  index = 3;
150  vertices[index * stride + 0] = x;
151  vertices[index * stride + 1] = 0;
152  vertices[index * stride + 2] = z + height;
153  vertices[index * stride + 3] = 0;
154  vertices[index * stride + 4] = 1;
155  }
156 
157  // 1 to 1 index buffer mapping
158  constexpr uint16_t indicesData[] = {3, 0, 1, 1, 2, 3};
159 
160  std::memcpy(meshData->getIndices16(), indicesData, sizeof(indicesData));
161 
162  return bs::Mesh::create(meshData, meshDesc);
163 
164  // // Set the bounds to get frustum culling and LOD to work correctly.
165  // // To infinite to always render
166  // mesh->_setBounds(Ogre::Aabb::BOX_INFINITE /*, false*/);
167 }
DLLEXPORT bool IsVerticalUVFlipped() const
Definition: Graphics.cpp:568
Graphics * GetGraphics()
Definition: Engine.h:185
unsigned short uint16_t
Definition: core.h:39
static DLLEXPORT Engine * Get()
Definition: Engine.cpp:85

The documentation for this class was generated from the following files: