//---------------------------------------------------------------------------- void Cloth::CreateCloth () { // create quadratic spline using particles as control points m_pkSpline = new BSplineRectanglef(m_pkModule->GetRows(), m_pkModule->GetCols(),m_pkModule->Positions2D(),2,2,false,false, true,true); // generate a rectangle surface int iUSamples = 16; int iVSamples = 32; bool bWantNormals = false; bool bWantColors = false; bool bDoubleSided = true; Vector2f kTextureMin(0.0f,0.0f), kTextureMax(1.0f,1.0f); m_spkCloth = new RectangleSurface(m_pkSpline,iUSamples,iVSamples, bWantNormals,bWantColors,bDoubleSided,&kTextureMin,&kTextureMax); // attach a texture for the surface Texture* pkTexture = new Texture; pkTexture->SetImage(Image::Load("purplecloth.mif")); pkTexture->Filter() = Texture::FM_LINEAR; pkTexture->Mipmap() = Texture::MM_LINEAR_LINEAR; pkTexture->Apply() = Texture::AM_REPLACE; pkTexture->Wrap() = Texture::WM_WRAP_S_WRAP_T; TextureState* pkTS = new TextureState; pkTS->Set(0,pkTexture); m_spkCloth->SetRenderState(pkTS); m_spkTrnNode->AttachChild(m_spkCloth); }
//---------------------------------------------------------------------------- bool EnvironmentMaps::Setup () { // load model m_spkScene = new Node(1); m_spkTrnNode = new Node(1); m_spkScene->AttachChild(m_spkTrnNode); Stream kStream; bool bLoaded = kStream.Load("Face.mgc"); if ( !bLoaded ) return false; m_spkModel = (Node*) kStream.GetObjectAt(0); m_spkTrnNode->AttachChild(m_spkModel); // attach environment map Image* pkImage = Image::Load("SphereMap.mif"); if ( !pkImage ) return false; Texture* pkTexture = new Texture; pkTexture->SetImage(pkImage); pkTexture->Filter() = Texture::FM_LINEAR; pkTexture->Mipmap() = Texture::MM_NONE; pkTexture->Apply() = Texture::AM_DECAL; pkTexture->Envmap() = Texture::EM_SPHERE; TextureState* pkTS = new TextureState; pkTS->Set(0,pkTexture); m_spkModel->SetRenderState(pkTS); return true; }
//---------------------------------------------------------------------------- TriMesh* BallHill::CreateGround () { TriMesh* pkGround = NULL; CreateRectangleMesh(pkGround,Vector3f::ZERO,Vector3f::UNIT_X, Vector3f::UNIT_Y,Vector3f::UNIT_Z,32.0f,32.0f,false,false,true); m_spkGround = pkGround; // change the texture repeat int iVQuantity = m_spkGround->GetVertexQuantity(); Vector2f* akUV = m_spkGround->Textures(); for (int i = 0; i < iVQuantity; i++) akUV[i] *= 8.0f; Texture* pkTexture = new Texture; pkTexture->SetImage(Image::Load("grass.mif")); pkTexture->Filter() = Texture::FM_LINEAR; pkTexture->Mipmap() = Texture::MM_LINEAR_LINEAR; pkTexture->Apply() = Texture::AM_REPLACE; pkTexture->Wrap() = Texture::WM_WRAP_S_WRAP_T; TextureState* pkTS = new TextureState; pkTS->Set(0,pkTexture); m_spkGround->SetRenderState(pkTS); return pkGround; }
//---------------------------------------------------------------------------- void GelatinBlob::CreateSphere () { TriMesh* pkMesh = NULL; bool bWantNormals = false; bool bWantColors = false; bool bWantUVs = true; bool bOutsideView = true; CreateIcosahedronMesh(pkMesh,bWantNormals,bWantColors,bWantUVs, bOutsideView); m_spkSphere = pkMesh; // texture for the water objects Texture* pkTexture = new Texture; pkTexture->SetImage(Image::Load("WaterA.mif")); pkTexture->Filter() = Texture::FM_LINEAR; pkTexture->Mipmap() = Texture::MM_LINEAR_LINEAR; pkTexture->Wrap() = Texture::WM_WRAP_S_WRAP_T; TextureState* pkTS = new TextureState; pkTS->Set(0,pkTexture); m_spkSphere->SetRenderState(pkTS); // the texture has an alpha channel of 1/2 AlphaState* pkAS = new AlphaState; pkAS->BlendEnabled() = true; m_spkSphere->SetRenderState(pkAS); }
//---------------------------------------------------------------------------- void GelatinCube::CreateBox () { // Create a quadratic spline using the interior particles as control // points. int iSlices = m_pkModule->GetSlices()-2; int iRows = m_pkModule->GetRows()-2; int iCols = m_pkModule->GetCols()-2; m_pkSpline = new BSplineVolumef(iCols,iRows,iSlices,2,2,2); for (int iSlice = 0; iSlice < iSlices; iSlice++) { for (int iRow = 0; iRow < iRows; iRow++) { for (int iCol = 0; iCol < iCols; iCol++) { m_pkSpline->ControlPoint(iCol,iRow,iSlice) = m_pkModule->Position(iSlice+1,iRow+1,iCol+1); } } } // generate a rectangle surface int iUSamples = 8; int iVSamples = 8; int iWSamples = 8; bool bWantNormals = false; bool bWantColors = false; bool bWantTextures = true; m_spkBox = new BoxSurface(m_pkSpline,iUSamples,iVSamples,iWSamples, bWantNormals,bWantColors,bWantTextures); // texture for the water objects Texture* pkTexture = new Texture; pkTexture->SetImage(Image::Load("WaterA.mif")); pkTexture->Filter() = Texture::FM_LINEAR; pkTexture->Mipmap() = Texture::MM_LINEAR_LINEAR; pkTexture->Wrap() = Texture::WM_WRAP_S_WRAP_T; TextureState* pkTS = new TextureState; pkTS->Set(0,pkTexture); m_spkBox->SetRenderState(pkTS); // the texture has an alpha channel of 1/2 AlphaState* pkAS = new AlphaState; pkAS->BlendEnabled() = true; m_spkBox->SetRenderState(pkAS); m_spkBox->EnableSorting(); m_spkTrnNode->AttachChild(m_spkBox); }
//---------------------------------------------------------------------------- void WaterDropFormation::CreateWall () { // generate the wall TriMesh* pkMesh = NULL; CreateRectangleMesh(pkMesh,Vector3f(-8.0f,0.0f,8.0f),Vector3f::UNIT_Y, Vector3f::UNIT_Z,Vector3f::UNIT_X,16.0f,8.0f,false,false,true); m_spkWall = pkMesh; // add a texture to the wall Texture* pkTexture = new Texture; pkTexture->SetImage(Image::Load("stone.mif")); pkTexture->Filter() = Texture::FM_LINEAR; pkTexture->Mipmap() = Texture::MM_LINEAR_LINEAR; pkTexture->Wrap() = Texture::WM_WRAP_S_WRAP_T; TextureState* pkTS = new TextureState; pkTS->Set(0,pkTexture); m_spkWall->SetRenderState(pkTS); m_spkTrnNode->AttachChild(m_spkWall); }
//---------------------------------------------------------------------------- void WaterDropFormation::CreatePlane () { // generate the plane TriMesh* pkMesh = NULL; CreateRectangleMesh(pkMesh,Vector3f::ZERO,Vector3f::UNIT_X, Vector3f::UNIT_Y,Vector3f::UNIT_Z,8.0f,16.0f,false,false,true); m_spkPlane = pkMesh; // add a texture to the plane Texture* pkTexture = new Texture; pkTexture->SetImage(Image::Load("gravel.mif")); pkTexture->Filter() = Texture::FM_LINEAR; pkTexture->Mipmap() = Texture::MM_LINEAR_LINEAR; pkTexture->Wrap() = Texture::WM_WRAP_S_WRAP_T; TextureState* pkTS = new TextureState; pkTS->Set(0,pkTexture); m_spkPlane->SetRenderState(pkTS); m_spkTrnNode->AttachChild(m_spkPlane); }
//---------------------------------------------------------------------------- void WaterDropFormation::CreateWaterRoot () { m_spkWaterRoot = new Node(2); m_spkTrnNode->AttachChild(m_spkWaterRoot); m_spkWaterRoot->Translate() = 0.1f*Vector3f::UNIT_Z; m_spkWaterRoot->Scale() = 8.0f; // texture for the water objects Texture* pkTexture = new Texture; pkTexture->SetImage(Image::Load("WaterA.mif")); pkTexture->Filter() = Texture::FM_LINEAR; pkTexture->Mipmap() = Texture::MM_LINEAR_LINEAR; pkTexture->Wrap() = Texture::WM_WRAP_S_WRAP_T; TextureState* pkTS = new TextureState; pkTS->Set(0,pkTexture); m_spkWaterRoot->SetRenderState(pkTS); // the texture has an alpha channel of 1/2 AlphaState* pkAS = new AlphaState; pkAS->BlendEnabled() = true; m_spkWaterRoot->SetRenderState(pkAS); }
//---------------------------------------------------------------------------- TriMesh* BallHill::CreateBall () { TriMesh* pkBall = NULL; m_fRadius = (float)m_kModule.Radius; CreateSphereMesh(pkBall,16,16,Vector3f::ZERO,m_fRadius,Vector3f::UNIT_X, Vector3f::UNIT_Y,Vector3f::UNIT_Z,false,false,true,true); m_spkBall = pkBall; m_spkBall->Translate().Z() = (float)m_kModule.A3 + m_fRadius; Texture* pkTexture = new Texture; pkTexture->SetImage(Image::Load("BallTexture.mif")); pkTexture->Filter() = Texture::FM_LINEAR; pkTexture->Mipmap() = Texture::MM_LINEAR_LINEAR; pkTexture->Apply() = Texture::AM_REPLACE; pkTexture->Wrap() = Texture::WM_WRAP_S_WRAP_T; TextureState* pkTS = new TextureState; pkTS->Set(0,pkTexture); m_spkBall->SetRenderState(pkTS); UpdateBall(); return m_spkBall; }
//---------------------------------------------------------------------------- void Rope::CreateRope () { // create quadratic spline using particles as control points int iNumCtrlPoints = m_pkModule->GetNumParticles(); Vector3f* akCtrlPoint = m_pkModule->Positions(); int iDegree = 2; m_pkSpline = new BSplineCurve3f(iNumCtrlPoints,akCtrlPoint,iDegree, false,true); // generate a tube surface whose medial axis is the spline bool bClosed = false; Vector3f kUpVector = Vector3f::UNIT_Z; int iMedialSamples = 64; int iSliceSamples = 8; bool bWantNormals = false; bool bWantColors = false; bool bSampleByArcLength = false; bool bInsideView = false; Vector2f kTextureMin(0.0f,0.0f), kTextureMax(1.0f,1.0f); m_spkRope = new TubeSurface(m_pkSpline,Radial,bClosed,kUpVector, iMedialSamples,iSliceSamples,bWantNormals,bWantColors, bSampleByArcLength,bInsideView,&kTextureMin,&kTextureMax); // attach a texture for the rope Texture* pkTexture = new Texture; pkTexture->SetImage(Image::Load("rope.mif")); pkTexture->Filter() = Texture::FM_LINEAR; pkTexture->Mipmap() = Texture::MM_LINEAR_LINEAR; pkTexture->Apply() = Texture::AM_REPLACE; pkTexture->Wrap() = Texture::WM_WRAP_S_WRAP_T; TextureState* pkTS = new TextureState; pkTS->Set(0,pkTexture); m_spkRope->SetRenderState(pkTS); m_spkTrnNode->AttachChild(m_spkRope); }
//---------------------------------------------------------------------------- TriMesh* BallHill::CreateHill () { TriMesh* pkHill = NULL; CreateDiskMesh(pkHill,32,32,Vector3f::ZERO,2.0f,Vector3f::UNIT_X, Vector3f::UNIT_Y,Vector3f::UNIT_Z,false,false,true); m_spkHill = pkHill; // change the texture repeat int iVQuantity = m_spkHill->GetVertexQuantity(); Vector2f* akUV = m_spkHill->Textures(); int i; for (i = 0; i < iVQuantity; i++) akUV[i] *= 8.0f; Texture* pkTexture = new Texture; pkTexture->SetImage(Image::Load("gravel.mif")); pkTexture->Filter() = Texture::FM_LINEAR; pkTexture->Mipmap() = Texture::MM_LINEAR_LINEAR; pkTexture->Apply() = Texture::AM_REPLACE; pkTexture->Wrap() = Texture::WM_WRAP_S_WRAP_T; TextureState* pkTS = new TextureState; pkTS->Set(0,pkTexture); m_spkHill->SetRenderState(pkTS); // adjust disk vertices to form elliptical paraboloid for the hill Vector3f* akVertex = m_spkHill->Vertices(); for (i = 0; i < iVQuantity; i++) { akVertex[i].Z() = m_kModule.GetHeight(akVertex[i].X(), akVertex[i].Y()); } m_spkHill->UpdateModelBound(); m_spkHill->UpdateModelNormals(); return m_spkHill; }
//---------------------------------------------------------------------------- bool RipplingOcean::Setup () { m_bStopped = false; m_fStopTime = GetTimeInSeconds(); m_spkScene = new Node(1); m_spkTrnNode = new Node(1); m_spkScene->AttachChild(m_spkTrnNode); // Root node for a scene graph that contains a bump-mapped triangle mesh // square. m_spkModel = new Node(1); // create the triangle mesh surface TriMesh* pkMesh = NULL; CreateRectangleMesh(pkMesh, Vector3f::ZERO, Vector3f::UNIT_X, Vector3f::UNIT_Y, -Vector3f::UNIT_Z, 1400.0f, 1200.0f, 50, 50, true, true, true); m_spkTriMesh = pkMesh; m_spkTriMesh->SetVertexShader(m_spkVertShader); m_spkTriMesh->SetPixelShader(m_spkPixShader); SetupShaders(); Image* pkNormal = Image::Load("plasma.mif"); if ( !pkNormal ) return false; HeightToNormalMap( pkNormal ); Texture* pkNormalTex = new Texture; pkNormalTex->SetImage(pkNormal); pkNormalTex->Mipmap() = Texture::MM_LINEAR_LINEAR; pkNormalTex->Filter() = Texture::FM_LINEAR; pkNormalTex->Apply() = Texture::AM_DECAL; pkNormalTex->Wrap() = Texture::WM_WRAP_S_WRAP_T; TextureState* pkTS = new TextureState; pkTS->Set(0,pkNormalTex); Image* pkWater = Image::Load("watergradient.mif"); if (!pkWater) return false; Texture* pkWaterTex = new Texture; pkWaterTex->SetImage(pkWater); pkWaterTex->Apply() = Texture::AM_DECAL; pkWaterTex->Wrap() = Texture::WM_CLAMP_S_CLAMP_T; pkTS->Set(1,pkWaterTex); Image* pkSkySphere = Image::Load("sky.mif"); if (!pkSkySphere) return false; Texture* pkSkySphereTex = new Texture; pkSkySphereTex->SetImage(pkSkySphere); pkTS->Set(2,pkSkySphereTex); m_spkTriMesh->SetRenderState(pkTS); m_spkModel->AttachChild(m_spkTriMesh); m_spkTrnNode->AttachChild(m_spkModel); // I'll admit that this is kind of a hack, but it puts the sun // a smidge higher in the sky. It makes it look nicest to start. =) Matrix3f kIncr; kIncr.FromAxisAngle(Vector3f::UNIT_X, -0.08f); m_spkTrnNode->Rotate() = kIncr; return true; }
//---------------------------------------------------------------------------- void WrigglingSnake::CreateSnakeBody () { // create the B-spline curve for the snake body Vector3f* akCtrl = new Vector3f[m_iNumCtrl]; int i; for (i = 0; i < m_iNumCtrl; i++) { // control points for a snake float fRatio = ((float)i)/(float)(m_iNumCtrl-1); float fX = -1.0f + 2.0f*fRatio; float fXMod = 10.0f*fX - 4.0f; akCtrl[i].X() = fX; akCtrl[i].Y() = ms_fRadius*(1.5f + Mathf::ATan(fXMod)/Mathf::PI); akCtrl[i].Z() = 0.0f; // sinusoidal motion for snake m_afAmplitude[i] = 0.1f+fRatio*Mathf::Exp(-fRatio); m_afPhase[i] = 1.5f*fRatio*Mathf::TWO_PI; } // the control points are copied by the curve objects m_pkCenter = new BSplineCurve3f(m_iNumCtrl,akCtrl,m_iDegree,false,true); delete[] akCtrl; // generate a tube surface bool bClosed = false; Vector3f kUpVector = Vector3f::UNIT_Y; int iMedialSamples = 128; int iSliceSamples = 32; bool bWantNormals = true; bool bWantColors = false; bool bSampleByArcLength = false; bool bInsideView = false; Vector2f kTextureMin(0.0f,0.0f), kTextureMax(1.0f,16.0f); m_spkSnakeBody = new TubeSurface(m_pkCenter,Radial,bClosed,kUpVector, iMedialSamples,iSliceSamples,bWantNormals,bWantColors, bSampleByArcLength,bInsideView,&kTextureMin,&kTextureMax); // attach a texture for the snake body Texture* pkTexture = new Texture; pkTexture->SetImage(Image::Load("snake.mif")); pkTexture->Filter() = Texture::FM_LINEAR; pkTexture->Mipmap() = Texture::MM_LINEAR_LINEAR; pkTexture->Apply() = Texture::AM_REPLACE; pkTexture->Wrap() = Texture::WM_WRAP_S_WRAP_T; TextureState* pkTS = new TextureState; pkTS->Set(0,pkTexture); //m_spkSnakeBody->SetRenderState(pkTS); // Set up a light map to add to the current color. pkTexture = new Texture; pkTexture->SetImage(Image::Load("LightMap.mif")); pkTexture->Filter() = Texture::FM_LINEAR; pkTexture->Mipmap() = Texture::MM_LINEAR_LINEAR; pkTexture->Apply() = Texture::AM_ADD; pkTexture->Envmap() = Texture::EM_SPHERE; pkTS->Set(1,pkTexture); m_spkSnakeBody->SetRenderState(pkTS); m_spkSnakeRoot->AttachChild(m_spkSnakeBody); }