Skip to content

Commit

Permalink
Changed the cloud mesh generation to not use dynamically allocated bu…
Browse files Browse the repository at this point in the history
…ffers

Added back the naming for the different meshes
  • Loading branch information
hhyyrylainen committed Feb 9, 2019
1 parent f955236 commit 6c3e4cf
Showing 1 changed file with 21 additions and 67 deletions.
88 changes: 21 additions & 67 deletions src/microbe_stage/compound_cloud_system.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -325,10 +325,12 @@ void
if(!Ogre::Root::getSingletonPtr())
return;

// Create the plane mesh
m_planeMesh =
Ogre::MeshManager::getSingleton().createManual("cloudPlaneMesh",
Ogre::ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME);
const auto meshName =
"CompoundCloudSystem_Plane_" + std::to_string(++CloudMeshNumberCounter);

// Create a background plane on which the fluid clouds will be drawn.
m_planeMesh = Ogre::MeshManager::getSingleton().createManual(
meshName, Ogre::ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME);

Ogre::SubMesh* planeSubMesh = m_planeMesh->createSubMesh();

Expand All @@ -341,83 +343,35 @@ void
myVertexElements.push_back(
Ogre::VertexElement2(Ogre::VET_FLOAT2, Ogre::VES_TEXTURE_COORDINATES));

// TODO: *make this static (will probably need a buffer alternative for
// generating the vertex data that will be then referenced here instead of
// nullptr)

// Simple square plane with 4 vertices & 2 primitive triangles.
const int vertexBufferSize = 4;
const int indexBufferSize = 6;
Ogre::VertexBufferPacked* myVertexBuffer =
myVaoManager->createVertexBuffer(myVertexElements, vertexBufferSize,
Ogre::BT_DYNAMIC_DEFAULT, nullptr, false);
CloudPlaneVertex meshVertices[] = {
{Ogre::Vector3(-CLOUD_WIDTH, 0, -CLOUD_HEIGHT), Ogre::Vector2(0, 0)},
{Ogre::Vector3(-CLOUD_WIDTH, 0, CLOUD_HEIGHT), Ogre::Vector2(0, 1)},
{Ogre::Vector3(CLOUD_WIDTH, 0, CLOUD_HEIGHT), Ogre::Vector2(1, 1)},
{Ogre::Vector3(CLOUD_WIDTH, 0, -CLOUD_HEIGHT), Ogre::Vector2(1, 0)}};

Ogre::VertexBufferPacked* myVertexBuffer = myVaoManager->createVertexBuffer(
myVertexElements, sizeof(meshVertices) / sizeof(CloudPlaneVertex),
Ogre::BT_IMMUTABLE, meshVertices, false);

Ogre::VertexBufferPackedVec myVertexBuffers;
myVertexBuffers.push_back(myVertexBuffer);

Ogre::uint16* myIndices = reinterpret_cast<Ogre::uint16*>(OGRE_MALLOC_SIMD(
sizeof(Ogre::uint16) * indexBufferSize, Ogre::MEMCATEGORY_GEOMETRY));
myIndices[0] = static_cast<Ogre::uint16>(2);
myIndices[1] = static_cast<Ogre::uint16>(0);
myIndices[2] = static_cast<Ogre::uint16>(1);
myIndices[3] = static_cast<Ogre::uint16>(0);
myIndices[4] = static_cast<Ogre::uint16>(2);
myIndices[5] = static_cast<Ogre::uint16>(3);

Ogre::IndexBufferPacked* myIndexBuffer = nullptr;

try {
myIndexBuffer =
myVaoManager->createIndexBuffer(Ogre::IndexBufferPacked::IT_16BIT,
indexBufferSize, Ogre::BT_IMMUTABLE,
// Could this be false like the vertex buffer to not keep a
// shadow buffer
myIndices, true);
} catch(const Ogre::Exception& e) {

// Avoid memory leak
OGRE_FREE_SIMD(myIndices, Ogre::MEMCATEGORY_GEOMETRY);
myIndexBuffer = nullptr;
throw e;
}
uint16_t myIndices[] = {2, 0, 1, 0, 2, 3};

Ogre::IndexBufferPacked* myIndexBuffer = myVaoManager->createIndexBuffer(
Ogre::IndexBufferPacked::IT_16BIT, sizeof(myIndices) / sizeof(uint16_t),
Ogre::BT_IMMUTABLE, myIndices, false);

Ogre::VertexArrayObject* myVao = myVaoManager->createVertexArrayObject(
myVertexBuffers, myIndexBuffer, Ogre::OT_TRIANGLE_LIST);

planeSubMesh->mVao[Ogre::VpNormal].push_back(myVao);

// This might be needed because we use a v2 mesh
// Use the same geometry for shadow casting.
// If m_item->setCastShadows(false); is set then this isn't needed
planeSubMesh->mVao[Ogre::VpShadow].push_back(myVao);

// Set the bounds to get frustum culling and LOD to work correctly.
m_planeMesh->_setBounds(Ogre::Aabb(Ogre::Vector3::ZERO,
Ogre::Vector3(CLOUD_WIDTH, CLOUD_HEIGHT, CLOUD_Y_COORDINATE))
/*, false*/);
m_planeMesh->_setBoundingSphereRadius(
sqrt(CLOUD_WIDTH * CLOUD_WIDTH + CLOUD_HEIGHT * CLOUD_HEIGHT));

// Update mesh data //
// Map the buffer for writing //
// DO NOT READ FROM THE MAPPED BUFFER
CloudPlaneVertex* RESTRICT_ALIAS meshVertices =
reinterpret_cast<CloudPlaneVertex * RESTRICT_ALIAS>(
myVertexBuffer->map(0, myVertexBuffer->getNumElements()));

// Creates a 3D prism from the 2D vertices.
meshVertices[0] = {
Ogre::Vector3(-CLOUD_WIDTH, 0, -CLOUD_HEIGHT), Ogre::Vector2(0, 0)};
meshVertices[1] = {
Ogre::Vector3(-CLOUD_WIDTH, 0, CLOUD_HEIGHT), Ogre::Vector2(0, 1)};
meshVertices[2] = {
Ogre::Vector3(CLOUD_WIDTH, 0, CLOUD_HEIGHT), Ogre::Vector2(1, 1)};
meshVertices[3] = {
Ogre::Vector3(CLOUD_WIDTH, 0, -CLOUD_HEIGHT), Ogre::Vector2(1, 0)};

// Upload finished data to the gpu (unmap all needs to be used to
// suppress warnings about destroying mapped buffers)
myVertexBuffer->unmap(Ogre::UO_UNMAP_ALL);
Ogre::Vector3(CLOUD_WIDTH, CLOUD_Y_COORDINATE, CLOUD_HEIGHT)));

// Need to edit the render queue (for when the item is created)
world.GetScene()->getRenderQueue()->setRenderQueueMode(
Expand Down

0 comments on commit 6c3e4cf

Please sign in to comment.