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 Mesh::pointer CreateScreenSpaceQuad (float x, float y, float width, float height, bool autoflipUV=true)
 Creates a screen space plane with UV coordinates. More...
 
static DLLEXPORT Mesh::pointer 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 Mesh::pointer 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 12 of file GeometryHelpers.cpp.

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

◆ CreateXZPlane()

DLLEXPORT Mesh::pointer 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 92 of file GeometryHelpers.cpp.

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

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