Guest User

VoxTestApplication.cpp

a guest
Sep 10th, 2013
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.79 KB | None | 0 0
  1. /*
  2. -----------------------------------------------------------------------------
  3. Filename:    VoxTestApplication.cpp
  4. -----------------------------------------------------------------------------
  5.  
  6.  
  7. This source file is generated by the
  8.    ___                   _              __    __ _                  _
  9.   /___\__ _ _ __ ___    /_\  _ __  _ __/ / /\ \ (_)______ _ _ __ __| |
  10.  //  // _` | '__/ _ \  //_\\| '_ \| '_ \ \/  \/ / |_  / _` | '__/ _` |
  11. / \_// (_| | | |  __/ /  _  \ |_) | |_) \  /\  /| |/ / (_| | | | (_| |
  12. \___/ \__, |_|  \___| \_/ \_/ .__/| .__/ \/  \/ |_/___\__,_|_|  \__,_|
  13.       |___/                 |_|   |_|
  14.       Ogre 1.8.x Application Wizard for Code::Blocks (May 2012)
  15.       https://bitbucket.org/jacmoe/ogreappwizards
  16.       -----------------------------------------------------------------------------
  17.       */
  18. #include "VoxTestApplication.h"
  19.  
  20. //-------------------------------------------------------------------------------------
  21. VoxTestApplication::VoxTestApplication(void)
  22. {
  23. }
  24. //-------------------------------------------------------------------------------------
  25. VoxTestApplication::~VoxTestApplication(void)
  26. {
  27. }
  28.  
  29. void createSphereInVolume(SimpleVolume<uint8_t>& volData, float fRadius)
  30. {
  31.         //This vector hold the position of the center of the volume
  32.         Vector3DFloat v3dVolCenter(volData.getWidth() / 2, volData.getHeight() / 2, volData.getDepth() / 2);
  33.  
  34.         //This three-level for loop iterates over every voxel in the volume
  35.         for (int z = 0; z < volData.getDepth(); z++)
  36.         {
  37.                 for (int y = 0; y < volData.getHeight(); y++)
  38.                 {
  39.                         for (int x = 0; x < volData.getWidth(); x++)
  40.                         {
  41.                                 //Store our current position as a vector...
  42.                                 Vector3DFloat v3dCurrentPos(x,y,z);
  43.                                 //And compute how far the current position is from the center of the volume
  44.                                 float fDistToCenter = (v3dCurrentPos - v3dVolCenter).length();
  45.  
  46.                                 uint8_t uVoxelValue = 0;
  47.  
  48.                                 //If the current voxel is less than 'radius' units from the center then we make it solid.
  49.                                 if(fDistToCenter <= fRadius)
  50.                                 {
  51.                                         //Our new voxel value
  52.                                         uVoxelValue = 255;
  53.                                 }
  54.  
  55.                                 //Wrte the voxel value into the volume
  56.                                 volData.setVoxelAt(x, y, z, uVoxelValue);
  57.                         }
  58.                 }
  59.         }
  60. }
  61.  
  62. //-------------------------------------------------------------------------------------
  63. void VoxTestApplication::createScene(void)
  64. {
  65.     SimpleVolume<uint8_t> volData(PolyVox::Region(Vector3DInt32(0, 0, 0), Vector3DInt32(63, 63, 63)));
  66.     createSphereInVolume(volData, 30);
  67.  
  68.     SurfaceMesh<PositionMaterialNormal> mesh;
  69.  
  70.     //MarchingCubesSurfaceExtractor< SimpleVolume<uint8_t> > surfaceExtractor(&volData, volData.getEnclosingRegion(), &mesh);
  71.     CubicSurfaceExtractorWithNormals< SimpleVolume<uint8_t> > surfaceExtractor(&volData, volData.getEnclosingRegion(), &mesh);
  72.  
  73.     surfaceExtractor.execute();
  74.     cerr << mesh.getNoOfIndices() << ' ' << mesh.getNoOfVertices() << endl;
  75.  
  76.     // Set the scene's ambient light
  77.     mSceneMgr->setAmbientLight(Ogre::ColourValue(0.5f, 0.5f, 0.5f));
  78.  
  79.     //Create a ManualObject
  80.     Ogre::ManualObject* manual = mSceneMgr->createManualObject("manual");
  81.  
  82.     //Begin a section of the ManualObject (we're only using one section)
  83.     manual->begin("BaseWhiteNoLighting", Ogre::RenderOperation::OT_TRIANGLE_LIST);
  84.  
  85.     //Iterate through all the vertices in the mesh produced by the surface extractor
  86.     //and add them to the ManualObject section.
  87.     for(vector<PositionMaterialNormal>::const_iterator it = mesh.getVertices().begin(); it != mesh.getVertices().end(); ++it)
  88.     {
  89.         const Vector3DFloat& vertexPos = it->getPosition();
  90.         const Vector3DFloat& vertexNorm = it->getNormal();
  91.         manual->position(vertexPos.getX(),vertexPos.getY(),vertexPos.getZ());
  92.         manual->normal(vertexNorm.getX(),vertexNorm.getY(),vertexNorm.getZ());
  93.         manual->colour(abs(vertexNorm.getX()),abs(vertexNorm.getY()),abs(vertexNorm.getZ()));
  94.     }
  95.  
  96.     //Now we iterate through all the indices from the mesh and also add them to the ManualObject section
  97.     for(vector<uint32_t>::const_iterator it = mesh.getIndices().begin(); it != mesh.getIndices().end(); ++it)
  98.     {
  99.         manual->index(*it);
  100.     }
  101.  
  102.     //End the section then add the ManualObject to the scenegraph
  103.     manual->end();
  104.     mSceneMgr->getRootSceneNode()->createChildSceneNode()->attachObject(manual);
  105.  
  106.     // Create a Light and set its position
  107.     Ogre::Light* light = mSceneMgr->createLight("MainLight");
  108.     light->setPosition(20.0f, 80.0f, 50.0f);
  109. }
  110.  
  111.  
  112.  
  113. #if OGRE_PLATFORM == OGRE_PLATFORM_WIN32
  114. #define WIN32_LEAN_AND_MEAN
  115. #include "windows.h"
  116. #endif
  117.  
  118. #ifdef __cplusplus
  119. extern "C" {
  120. #endif
  121.  
  122. #if OGRE_PLATFORM == OGRE_PLATFORM_WIN32
  123.     INT WINAPI WinMain( HINSTANCE hInst, HINSTANCE, LPSTR strCmdLine, INT )
  124. #else
  125.     int main(int argc, char *argv[])
  126. #endif
  127.     {
  128.         // Create application object
  129.         VoxTestApplication app;
  130.  
  131.         try {
  132.             app.go();
  133.         } catch( Ogre::Exception& e ) {
  134. #if OGRE_PLATFORM == OGRE_PLATFORM_WIN32
  135.             MessageBox( NULL, e.getFullDescription().c_str(), "An exception has occured!", MB_OK | MB_ICONERROR | MB_TASKMODAL);
  136. #else
  137.             std::cerr << "An exception has occured: " <<
  138.                 e.getFullDescription().c_str() << std::endl;
  139. #endif
  140.         }
  141.  
  142.         return 0;
  143.     }
  144.  
  145. #ifdef __cplusplus
  146. }
  147. #endif
Advertisement
Add Comment
Please, Sign In to add comment