From c1b31acece3ab275133fe1570c02c5618954472f Mon Sep 17 00:00:00 2001 From: Nicolay Korslund Date: Sun, 10 Jan 2010 18:32:21 +0100 Subject: [PATCH] Added first test, doesn't work :( --- nifogre/tests/.gitignore | 2 + nifogre/tests/Makefile | 3 + nifogre/tests/ogre_manualresource_test.cpp | 100 ++++++++++++-- nifogre/tests/ogre_nif_test.cpp | 145 +++++++++++++++++++++ nifogre/tests/plugins.cfg | 12 ++ 5 files changed, 251 insertions(+), 11 deletions(-) create mode 100644 nifogre/tests/.gitignore create mode 100644 nifogre/tests/ogre_nif_test.cpp create mode 100644 nifogre/tests/plugins.cfg diff --git a/nifogre/tests/.gitignore b/nifogre/tests/.gitignore new file mode 100644 index 000000000..1c98244a9 --- /dev/null +++ b/nifogre/tests/.gitignore @@ -0,0 +1,2 @@ +ogre.cfg +*_test diff --git a/nifogre/tests/Makefile b/nifogre/tests/Makefile index 977262bca..e97a2f53f 100644 --- a/nifogre/tests/Makefile +++ b/nifogre/tests/Makefile @@ -8,6 +8,9 @@ L_OGRE=$(shell pkg-config --libs OGRE) ogre_manualresource_test: ogre_manualresource_test.cpp $(GCC) $^ -o $@ $(I_OGRE) $(L_OGRE) +ogre_nif_test: ogre_nif_test.cpp ../../nif/nif_file.cpp ../../bsa/bsa_file.cpp ../../tools/stringops.cpp + $(GCC) $^ -o $@ $(I_OGRE) $(L_OGRE) + clean: rm *_test diff --git a/nifogre/tests/ogre_manualresource_test.cpp b/nifogre/tests/ogre_manualresource_test.cpp index 721c56112..9d812a8f1 100644 --- a/nifogre/tests/ogre_manualresource_test.cpp +++ b/nifogre/tests/ogre_manualresource_test.cpp @@ -5,6 +5,7 @@ using namespace std; using namespace Ogre; +// Why doesn't it work? Bad code, BAD! struct MyMeshLoader : ManualResourceLoader { void loadResource(Resource *resource) @@ -16,11 +17,73 @@ struct MyMeshLoader : ManualResourceLoader cout << "Manually loading mesh " << name << endl; // Create the mesh here + int numVerts = 4; + int numFaces = 2; + const float vertices[] = + { -1,-1,0, 1,-1,0, + 1,1,0, -1,1,0 }; + + const short faces[] = + { 0,1,2, 0,3,2 }; + + mesh->sharedVertexData = new VertexData(); + mesh->sharedVertexData->vertexCount = numVerts; + + VertexDeclaration* decl = mesh->sharedVertexData->vertexDeclaration; + + decl->addElement(0, 0, VET_FLOAT3, VES_POSITION); + + HardwareVertexBufferSharedPtr vbuf = + HardwareBufferManager::getSingleton().createVertexBuffer( + VertexElement::getTypeSize(VET_FLOAT3), + numVerts, HardwareBuffer::HBU_STATIC_WRITE_ONLY); + + // Upload the vertex data to the card + vbuf->writeData(0, vbuf->getSizeInBytes(), vertices, true); + + // Set vertex buffer binding so buffer 0 is bound to our vertex buffer + VertexBufferBinding* bind = mesh->sharedVertexData->vertexBufferBinding; + bind->setBinding(0, vbuf); + + /// Allocate index buffer of the requested number of faces + HardwareIndexBufferSharedPtr ibuf = HardwareBufferManager::getSingleton(). + createIndexBuffer(HardwareIndexBuffer::IT_16BIT, + numFaces, + HardwareBuffer::HBU_STATIC_WRITE_ONLY); + + /// Upload the index data to the card + ibuf->writeData(0, ibuf->getSizeInBytes(), faces, true); + + SubMesh* sub = mesh->createSubMesh("tris"); + sub->useSharedVertices = true; + + /// Set parameters of the submesh + sub->indexData->indexBuffer = ibuf; + sub->indexData->indexCount = numFaces; + sub->indexData->indexStart = 0; + + sub->setMaterialName(MaterialManager::getSingleton().getDefaultSettings()->getName()); + + mesh->_setBounds(AxisAlignedBox(-1.1,-1.1,-1.1,1.1,1.1,1.1)); + mesh->_setBoundingSphereRadius(2); } }; MyMeshLoader mml; +RenderWindow *window; + +// Lets you quit by closing the window +struct QuitListener : FrameListener +{ + bool frameStarted(const FrameEvent& evt) + { + if(window->isClosed()) + return false; + return true; + } +} qlistener; + int main() { // When the test is done, consider disabling the rendering part @@ -29,40 +92,55 @@ int main() // and still do some meaningful testing, even if you can't inpsect // the result visually. - /* // Disable Ogre logging new LogManager; Log *log = LogManager::getSingleton().createLog(""); log->setDebugOutputEnabled(false); - */ // Set up Root. - Root *root = new Root("plugin.cfg","ogre.cfg",""); + Root *root = new Root("plugins.cfg","ogre.cfg",""); if(!root->restoreConfig()) if(!root->showConfigDialog()) return 1; // Create a window - RenderWindow *window = root->initialise(true, "Test"); + window = root->initialise(true, "Test"); // We might need input managment too // More initialization - SceneManager *mgr = mRoot->createSceneManager(ST_GENERIC); + SceneManager *mgr = root->createSceneManager(ST_GENERIC); Camera *cam = mgr->createCamera("cam"); - ViewPort *vp = window->addViewport(cam); + Viewport *vp = window->addViewport(cam); cam->setAspectRatio(Real(vp->getActualWidth()) / Real(vp->getActualHeight())); cam->setFOVy(Degree(55)); + cam->setPosition(0,0,0); + cam->lookAt(0,0,10); + cam->setNearClipDistance(1); + + root->addFrameListener(&qlistener); // Background color - vp->setBackgroundColour(ColourValue(0,0,0)); + vp->setBackgroundColour(ColourValue(0.5,0.5,0.5)); + + mgr->setAmbientLight(ColourValue(1,1,1)); + + // Create a couple of manual meshes + MeshManager::getSingleton().createManual("mesh1.mm", "General", &mml); + MeshManager::getSingleton().createManual("mesh2.mm", "General", &mml); + + // Display the meshes + SceneNode *node = mgr->getRootSceneNode()->createChildSceneNode("node"); + Entity *ent1 = mgr->createEntity("Mesh1", "mesh1.mm"); + node->attachObject(ent1); - // Declare a couple of manual meshes - ResourceGroupManager::getSingleton().declareResource("mesh1.mm", "Mesh", "General", &mml); - ResourceGroupManager::getSingleton().declareResource("mesh2.mm", "Mesh", "General", &mml); + node->setPosition(0,0,30); - // Display the meshes here + // Render loop + root->startRendering(); + // Cleanup + delete root; return 0; } diff --git a/nifogre/tests/ogre_nif_test.cpp b/nifogre/tests/ogre_nif_test.cpp new file mode 100644 index 000000000..675714d4d --- /dev/null +++ b/nifogre/tests/ogre_nif_test.cpp @@ -0,0 +1,145 @@ +#include +#include +#include + +using namespace std; +using namespace Ogre; + +struct MyMeshLoader : ManualResourceLoader +{ + void loadResource(Resource *resource) + { + Mesh *mesh = dynamic_cast(resource); + assert(mesh); + + const String& name = mesh->getName(); + cout << "Manually loading mesh " << name << endl; + + // Create the mesh here + int numVerts = 4; + int numFaces = 2; + const float vertices[] = + { -1,-1,0, 1,-1,0, + 1,1,0, -1,1,0 }; + + const short faces[] = + { 0,1,2, 0,3,2 }; + + mesh->sharedVertexData = new VertexData(); + mesh->sharedVertexData->vertexCount = numVerts; + + VertexDeclaration* decl = mesh->sharedVertexData->vertexDeclaration; + + decl->addElement(0, 0, VET_FLOAT3, VES_POSITION); + + HardwareVertexBufferSharedPtr vbuf = + HardwareBufferManager::getSingleton().createVertexBuffer( + VertexElement::getTypeSize(VET_FLOAT3), + numVerts, HardwareBuffer::HBU_STATIC_WRITE_ONLY); + + // Upload the vertex data to the card + vbuf->writeData(0, vbuf->getSizeInBytes(), vertices, true); + + // Set vertex buffer binding so buffer 0 is bound to our vertex buffer + VertexBufferBinding* bind = mesh->sharedVertexData->vertexBufferBinding; + bind->setBinding(0, vbuf); + + /// Allocate index buffer of the requested number of faces + HardwareIndexBufferSharedPtr ibuf = HardwareBufferManager::getSingleton(). + createIndexBuffer(HardwareIndexBuffer::IT_16BIT, + numFaces, + HardwareBuffer::HBU_STATIC_WRITE_ONLY); + + /// Upload the index data to the card + ibuf->writeData(0, ibuf->getSizeInBytes(), faces, true); + + SubMesh* sub = mesh->createSubMesh("tris"); + sub->useSharedVertices = true; + + /// Set parameters of the submesh + sub->indexData->indexBuffer = ibuf; + sub->indexData->indexCount = numFaces; + sub->indexData->indexStart = 0; + + sub->setMaterialName(MaterialManager::getSingleton().getDefaultSettings()->getName()); + + mesh->_setBounds(AxisAlignedBox(-1.1,-1.1,-1.1,1.1,1.1,1.1)); + mesh->_setBoundingSphereRadius(2); + } +}; + +MyMeshLoader mml; + +RenderWindow *window; + +// Lets you quit by closing the window +struct QuitListener : FrameListener +{ + bool frameStarted(const FrameEvent& evt) + { + if(window->isClosed()) + return false; + return true; + } +} qlistener; + +int main() +{ + // When the test is done, consider disabling the rendering part + // unless a command line parameter is given (and write a note about + // this to console.) This allows you to run the test from scripts + // and still do some meaningful testing, even if you can't inpsect + // the result visually. + + // Disable Ogre logging + new LogManager; + Log *log = LogManager::getSingleton().createLog(""); + log->setDebugOutputEnabled(false); + + // Set up Root. + Root *root = new Root("plugins.cfg","ogre.cfg",""); + + if(!root->restoreConfig()) + if(!root->showConfigDialog()) + return 1; + + // Create a window + window = root->initialise(true, "Test"); + + // We might need input managment too + + // More initialization + SceneManager *mgr = root->createSceneManager(ST_GENERIC); + Camera *cam = mgr->createCamera("cam"); + Viewport *vp = window->addViewport(cam); + cam->setAspectRatio(Real(vp->getActualWidth()) / Real(vp->getActualHeight())); + cam->setFOVy(Degree(55)); + cam->setPosition(0,0,0); + cam->lookAt(0,0,10); + cam->setNearClipDistance(1); + + root->addFrameListener(&qlistener); + + // Background color + vp->setBackgroundColour(ColourValue(0.5,0.5,0.5)); + + mgr->setAmbientLight(ColourValue(1,1,1)); + + // Create a couple of manual meshes + MeshManager::getSingleton().createManual("mesh1.mm", "General", &mml); + MeshManager::getSingleton().createManual("mesh2.mm", "General", &mml); + + // Display the meshes + SceneNode *node = mgr->getRootSceneNode()->createChildSceneNode("node"); + Entity *ent1 = mgr->createEntity("Mesh1", "mesh1.mm"); + node->attachObject(ent1); + + node->setPosition(0,0,30); + + // Render loop + root->startRendering(); + + // Cleanup + delete root; + return 0; +} diff --git a/nifogre/tests/plugins.cfg b/nifogre/tests/plugins.cfg new file mode 100644 index 000000000..9133aec32 --- /dev/null +++ b/nifogre/tests/plugins.cfg @@ -0,0 +1,12 @@ +# Defines plugins to load + +# Define plugin folder +PluginFolder=/usr/local/lib/OGRE + +# Define plugins +Plugin=RenderSystem_GL +Plugin=Plugin_ParticleFX +Plugin=Plugin_OctreeSceneManager + + +