THe trouble is, I dont think I am understanding how the Geometry data is handled by the engine. My init routine just produces shards of polygons. Am I even going about this the right way?
thanks for the help.
Code: Select all
#include "ExampleApplication.h"
/*********************************************************************/
class WaterMesh
{
public:
SceneNode* pNode;
Entity* patchEntity;
PatchSurface ps;
protected:
GeometryData patchCtlPoints;
struct PatchVertex {
Real x, y, z;
Real nx, ny, nz;
Real u, v;
};
public:
WaterMesh(int numVertices)
{
patchCtlPoints.pVertices = 0;
init(numVertices);
}
~WaterMesh()
{
if (patchCtlPoints.pVertices)
delete [] patchCtlPoints.pVertices;
}
/* numVertices must be a square number which is >=9
and a multiple of 3 (since polygons come in multiples of 3)
*/
void init( int numVertices )
{
// Create patch
patchCtlPoints.hasColours = false;
patchCtlPoints.hasNormals = true;
patchCtlPoints.numTexCoords = 1;
patchCtlPoints.numTexCoordDimensions[0] = 2;
// Packed data
patchCtlPoints.vertexStride = sizeof(Real) * 5;
patchCtlPoints.normalStride = sizeof(Real) * 5;
patchCtlPoints.texCoordStride[0] = sizeof(Real) * 6;
patchCtlPoints.pVertices = (Real*)( new PatchVertex[numVertices] );
patchCtlPoints.numVertices = numVertices;
patchCtlPoints.pNormals = patchCtlPoints.pVertices + 3;
patchCtlPoints.pTexCoords[0] = patchCtlPoints.pVertices + 6;
/*** Space out x/z coordinates of Vertex control points ***/
PatchVertex *pVert = (PatchVertex*)patchCtlPoints.pVertices;
int side = (sqrt(numVertices)-1)/2;
for (int z = -side; z < side; z++)
{
for (int x = -side; x < side; x++)
{
pVert->x = x*100; pVert->y = 0.0; pVert->z = z*100;
pVert++;
}
}
/*** Set up the face data ***/
pVert = (PatchVertex*)patchCtlPoints.pVertices;
for (int faces = 0; faces < (numVertices/3); faces++)
{
pVert->nx = -0.5; pVert->ny = 0.5; pVert->nz = 0.0;
pVert->u = 0.0; pVert->v = 0.0;
pVert++;
pVert->nx = 0.0; pVert->ny = 0.5; pVert->nz = 0.0;
pVert->u = 0.5; pVert->v = 0.0;
pVert++;
pVert->nx = 0.5; pVert->ny = 0.5; pVert->nz = 0.0;
pVert->u = 1.0; pVert->v = 0.0;
pVert++;
}
ps.defineSurface("Bezier1", patchCtlPoints, 3, PatchSurface::PST_BEZIER, 0, PatchSurface::VS_BOTH);
ps.build();
}
};
/*********************************************************************/
static WaterMesh* mWaterMesh;
// Listener class for frame updates
class myFrameListener : public ExampleFrameListener
{
public:
myFrameListener(RenderWindow* win, Camera* cam)
: ExampleFrameListener(win, cam) { }
bool frameStarted(const FrameEvent& evt)
{
// Call superclass
return ExampleFrameListener::frameStarted(evt);
}
};
/*********************************************************************/
class myApplication : public ExampleApplication
{
public:
myApplication() {}
~myApplication() { delete mWaterMesh; }
protected:
// Just override the mandatory create scene method
void createScene(void)
{
// Set ambient light
mSceneMgr->setAmbientLight(ColourValue(1, 1, 1));
// Create a light
Light* l = mSceneMgr->createLight("MainLight");
l->setPosition(20,80,50);
/* // and an entity (ogre cube)
Entity *ent = mSceneMgr->createEntity("cube", "cube.mesh");
ent->setMaterialName("Examples/OgreLogo");
// Create a new node, attach the cube entity
mCubeNode = static_cast<SceneNode*>(mSceneMgr->getRootSceneNode()->createChild());
mCubeNode->attachObject(ent);
*/
// Create a new Watermesh with 81 controll points
mWaterMesh= new WaterMesh( 81 );
// Create entity based on patch
mWaterMesh->patchEntity = mSceneMgr->createEntity("Entity1", "Bezier1");
Material *pMat = (Material*)MaterialManager::getSingleton().create( "TextMat" );
pMat->addTextureLayer( "Water02.jpg" );
mWaterMesh->patchEntity->setMaterialName("TextMat");
// Attach the entity to the root of the scene
mWaterMesh->pNode = static_cast<SceneNode*>(mSceneMgr->getRootSceneNode()->createChild());
mWaterMesh->pNode->attachObject(mWaterMesh->patchEntity);
mCamera->setPosition(500,500, 1500);
mCamera->lookAt(0,200,-300);
}
void createFrameListener(void)
{
// This is where we instantiate our own frame listener
mFrameListener= new myFrameListener(mWindow, mCamera);
mRoot->addFrameListener(mFrameListener);
}
};