//---------------------------------------------------------------------------- void ExtremalQuery::CreateScene () { mScene = new0 Node(); mWireState = new0 WireState(); mRenderer->SetOverrideWireState(mWireState); mCullState = new0 CullState(); mCullState->Enabled = false; mRenderer->SetOverrideCullState(mCullState); const int numVertices = 32; CreateConvexPolyhedron(numVertices); mScene->AttachChild(CreateVisualConvexPolyhedron()); // Use small spheres to show the extreme points in the camera's right // direction. VertexFormat* vformat = VertexFormat::Create(2, VertexFormat::AU_POSITION, VertexFormat::AT_FLOAT3, 0, VertexFormat::AU_COLOR, VertexFormat::AT_FLOAT3, 0); StandardMesh sm(vformat); VertexColor3Effect* effect = new0 VertexColor3Effect(); // maximum sphere mMaxSphere = sm.Sphere(8, 8, 0.05f); mMaxSphere->SetEffectInstance(effect->CreateInstance()); mScene->AttachChild(mMaxSphere); // minimum sphere mMinSphere = sm.Sphere(8, 8, 0.05f); mMinSphere->SetEffectInstance(effect->CreateInstance()); mScene->AttachChild(mMinSphere); UpdateExtremePoints(); }
//---------------------------------------------------------------------------- void BlendedAnimations::CreateScene () { mWireState = new0 WireState(); mRenderer->SetOverrideWireState(mWireState); mScene = new0 Node(); mScene->AttachChild(mManager.GetRoot()); // Create a floor to walk on. VertexFormat* vformat = VertexFormat::Create(3, VertexFormat::AU_POSITION, VertexFormat::AT_FLOAT3, 0, VertexFormat::AU_NORMAL, VertexFormat::AT_FLOAT3, 0, VertexFormat::AU_TEXCOORD, VertexFormat::AT_FLOAT2, 0); mFloor = StandardMesh(vformat).Rectangle(2, 2, 1024.0f, 2048.0f); VertexBufferAccessor vba(mFloor); for (int i = 0; i < vba.GetNumVertices(); ++i) { Float2& tcoord = vba.TCoord<Float2>(0, i); tcoord[0] *= 64.0f; tcoord[1] *= 256.0f; } std::string textureName = Environment::GetPathR("Grating.wmtf"); Texture2D* texture = Texture2D::LoadWMTF(textureName); texture->GenerateMipmaps(); mFloor->SetEffectInstance(Texture2DEffect::CreateUniqueInstance(texture, Shader::SF_LINEAR_LINEAR, Shader::SC_REPEAT, Shader::SC_REPEAT)); mScene->AttachChild(mFloor); ComputeVisibleSet(mScene); }
//---------------------------------------------------------------------------- void FreeFormDeformation::CreateScene () { mScene = new0 Node(); mTrnNode = new0 Node(); mScene->AttachChild(mTrnNode); mWireState = new0 WireState(); mRenderer->SetOverrideWireState(mWireState); // Load the small brain data set. It has positions (x,y,z) and texture // coordinates (u,v). std::string path = Environment::GetPathR("SmallBrainPT2.wmvf"); Visual::PrimitiveType type; VertexFormat* vformat; VertexBuffer* vbuffer; IndexBuffer* ibuffer; Visual::LoadWMVF(path, type, vformat, vbuffer, ibuffer); path = Environment::GetPathR("Quartz.wmtf"); Texture2D* texture = Texture2D::LoadWMTF(path); mMesh = new0 TriMesh(vformat, vbuffer, ibuffer); mMesh->SetEffectInstance(Texture2DEffect::CreateUniqueInstance(texture, Shader::SF_LINEAR, Shader::SC_CLAMP_EDGE, Shader::SC_CLAMP_EDGE)); mTrnNode->AttachChild(mMesh); CreateBSplineVolume(); CreatePolylines(); CreateControlBoxes(); }
//---------------------------------------------------------------------------- void FoucaultPendulum::CreateScene () { mScene = new0 Node(); mScene->AttachChild(CreateFloor()); mScene->AttachChild(CreatePath()); mScene->AttachChild(CreatePendulum()); mWireState = new0 WireState(); mRenderer->SetOverrideWireState(mWireState); }
//---------------------------------------------------------------------------- void SimplePendulumFriction::CreateScene () { mScene = new0 Node(); mWireState = new0 WireState(); mRenderer->SetOverrideWireState(mWireState); mScene->AttachChild(CreateFloor()); mScene->AttachChild(CreatePendulum()); }
//---------------------------------------------------------------------------- void PointInPolyhedron::CreateScene () { mScene = new0 Node(); mWireState = new0 WireState(); mRenderer->SetOverrideWireState(mWireState); // Create a semitransparent sphere mesh. VertexFormat* vformatMesh = VertexFormat::Create(1, VertexFormat::AU_POSITION, VertexFormat::AT_FLOAT3, 0); TriMesh* mesh = StandardMesh(vformatMesh).Sphere(16, 16, 1.0f); Material* material = new0 Material(); material->Diffuse = Float4(1.0f, 0.0f, 0.0f, 0.25f); VisualEffectInstance* instance = MaterialEffect::CreateUniqueInstance( material); instance->GetEffect()->GetAlphaState(0, 0)->BlendEnabled = true; mesh->SetEffectInstance(instance); // Create the data structures for the polyhedron that represents the // sphere mesh. CreateQuery(mesh); // Create a set of random points. Points inside the polyhedron are // colored white. Points outside the polyhedron are colored blue. VertexFormat* vformat = VertexFormat::Create(2, VertexFormat::AU_POSITION, VertexFormat::AT_FLOAT3, 0, VertexFormat::AU_COLOR, VertexFormat::AT_FLOAT3, 0); int vstride = vformat->GetStride(); VertexBuffer* vbuffer = new0 VertexBuffer(1024, vstride); VertexBufferAccessor vba(vformat, vbuffer); Float3 white(1.0f, 1.0f, 1.0f); Float3 blue(0.0f, 0.0f, 1.0f); for (int i = 0; i < vba.GetNumVertices(); ++i) { Vector3f random(Mathf::SymmetricRandom(), Mathf::SymmetricRandom(), Mathf::SymmetricRandom()); vba.Position<Vector3f>(i) = random; if (mQuery->Contains(random)) { vba.Color<Float3>(0, i) = white; } else { vba.Color<Float3>(0, i) = blue; } } DeleteQuery(); mPoints = new0 Polypoint(vformat, vbuffer); mPoints->SetEffectInstance(VertexColor3Effect::CreateUniqueInstance()); mScene->AttachChild(mPoints); mScene->AttachChild(mesh); }
//---------------------------------------------------------------------------- void WrigglingSnake::CreateScene () { mScene = new0 Node(); mTrnNode = new0 Node(); mScene->AttachChild(mTrnNode); mWireState = new0 WireState(); mRenderer->SetOverrideWireState(mWireState); CreateSnake(); }
//---------------------------------------------------------------------------- void PlanarShadows::CreateScene () { mScene = new0 Node(); mWireState = new0 WireState(); mRenderer->SetOverrideWireState(mWireState); LoadBiped(); CreatePlanes(); CreatePlanarShadow(); }
//---------------------------------------------------------------------------- void PlanarReflections::CreateScene () { mScene = new0 Node(); mWireState = new0 WireState(); mRenderer->SetOverrideWireState(mWireState); LoadBiped(); CreatePlanes(); CreatePlanarReflection(); mScene->LocalTransform.SetRotate(HMatrix(AVector::UNIT_Z, 0.75f)); }
//---------------------------------------------------------------------------- void GelatinCube::CreateScene () { mScene = new0 Node(); mTrnNode = new0 Node(); mScene->AttachChild(mTrnNode); mWireState = new0 WireState(); mRenderer->SetOverrideWireState(mWireState); CreateSprings(); CreateBox(); }
//---------------------------------------------------------------------------- void RoughPlaneSolidBox::CreateScene () { mScene = new0 Node(); mWireState = new0 WireState(); mRenderer->SetOverrideWireState(mWireState); mScene->AttachChild(CreateGround()); mScene->AttachChild(CreateRamp()); mScene->AttachChild(CreateBox()); mScene->LocalTransform.SetRotate(HMatrix(AVector::UNIT_Z, 0.661917f)); }
//---------------------------------------------------------------------------- void PolyhedronDistance::CreateScene () { // ** layout of scene graph ** // scene // tetra[4] // plane // line[2] // Create the objects. mScene = new0 Node(); mWireState = new0 WireState(); mRenderer->SetOverrideWireState(mWireState); TriMesh* plane = CreatePlane(); int i; for (i = 0; i < 2; ++i) { // Build the display tetrahedra. float size = 0.3f + 0.2f*(i + 1); if (i == 0) { mEdgeLength = size; } mTetras[i] = CreateTetra(size, false); mSegments[i] = CreateSegment(); // Build the point tetrahedra. mSmall = 0.02f; mTetras[i + 2] = CreateTetra(mSmall, true); } // Tetrahedra faces. mFaces = new1<Tuple<3,int> >(4); mFaces[0][0] = 1; mFaces[0][1] = 2; mFaces[0][2] = 0; mFaces[1][0] = 0; mFaces[1][1] = 3; mFaces[1][2] = 2; mFaces[2][0] = 0; mFaces[2][1] = 1; mFaces[2][2] = 3; mFaces[3][0] = 1; mFaces[3][1] = 2; mFaces[3][2] = 3; // Transform the tetrahedra. mTetras[0]->LocalTransform.SetRotate(HMatrix(AVector::UNIT_Z, 1.1f)); mTetras[0]->LocalTransform.SetTranslate(APoint(-0.25f, 0.1f, 0.3f)); mTetras[1]->LocalTransform.SetRotate(HMatrix(AVector::UNIT_Z, 0.3f)); mTetras[1]->LocalTransform.SetTranslate(APoint(0.25f, 0.4f, 0.5f)); // Set parent-child links. mScene->AttachChild(plane); for (i = 0; i < 2; ++i) { mScene->AttachChild(mTetras[i]); mScene->AttachChild(mSegments[i]); mScene->AttachChild(mTetras[i + 2]); } }
//---------------------------------------------------------------------------- void WaterDropFormation::CreateScene () { mScene = new0 Node(); mTrnNode = new0 Node(); mScene->AttachChild(mTrnNode); mWireState = new0 WireState(); mRenderer->SetOverrideWireState(mWireState); CreatePlane(); CreateWall(); CreateWaterRoot(); Configuration0(); }
//---------------------------------------------------------------------------- bool FxCompiler::CreateEffect (const Program& vProgram, const Program& pProgram) { InputArray vInputs, pInputs; OutputArray vOutputs, pOutputs; ConstantArray vConstants, pConstants; SamplerArray vSamplers, pSamplers; if (!Process(vProgram, vInputs, vOutputs, vConstants, vSamplers)) { return false; } if (!Process(pProgram, pInputs, pOutputs, pConstants, pSamplers)) { return false; } mVShader = (VertexShader*)CreateShader(true, vProgram, vInputs, vOutputs, vConstants, vSamplers); mPShader = (PixelShader*)CreateShader(false, pProgram, pInputs, pOutputs, pConstants, pSamplers); VisualPass* pass = new0 VisualPass(); pass->SetVertexShader(mVShader); pass->SetPixelShader(mPShader); // TODO. Once Cg FX files are parsed, the global state from each pass // should be set here. For now, the application is responsible for // setting the global state after the *.wmfx file is loaded. pass->SetAlphaState(new0 AlphaState()); pass->SetCullState(new0 CullState()); pass->SetDepthState(new0 DepthState()); pass->SetOffsetState(new0 OffsetState()); pass->SetStencilState(new0 StencilState()); pass->SetWireState(new0 WireState()); // TODO. Once Cg FX files are parsed, we might have multiple techniques // or multiple passes per technique. VisualTechnique* technique = new0 VisualTechnique(); technique->InsertPass(pass); mEffect = new0 VisualEffect(); mEffect->InsertTechnique(technique); return true; }
//---------------------------------------------------------------------------- void NonuniformScale::CreateScene () { mScene = new0 Node(); mWireState = new0 WireState(); mRenderer->SetOverrideWireState(mWireState); VertexFormat* vformat = VertexFormat::Create(2, VertexFormat::AU_POSITION, VertexFormat::AT_FLOAT3, 0, VertexFormat::AU_TEXCOORD, VertexFormat::AT_FLOAT2, 0); mMesh = StandardMesh(vformat).Dodecahedron(); std::string path = Environment::GetPathR("Flower.wmtf"); Texture2D* texture = Texture2D::LoadWMTF(path); mMesh->SetEffectInstance(Texture2DEffect::CreateUniqueInstance(texture, Shader::SF_LINEAR, Shader::SC_CLAMP_EDGE, Shader::SC_CLAMP_EDGE)); mScene->AttachChild(mMesh); }
//---------------------------------------------------------------------------- void IntersectTriangleCylinder::CreateScene () { mScene = new0 Node(); mCullState = new0 CullState(); mCullState->Enabled = false; mRenderer->SetOverrideCullState(mCullState); mWireState = new0 WireState(); mRenderer->SetOverrideWireState(mWireState); VertexFormat* vformat = VertexFormat::Create(2, VertexFormat::AU_POSITION, VertexFormat::AT_FLOAT3, 0, VertexFormat::AU_COLOR, VertexFormat::AT_FLOAT3, 0); int vstride = vformat->GetStride(); VertexBuffer* vbuffer = new0 VertexBuffer(3, vstride); VertexBufferAccessor vba(vformat, vbuffer); vba.Position<Vector3f>(0) = (const Vector3f&)mTriangleMVertex0; vba.Color<Float3>(0, 0) = Float3(0.0f, 0.0f, 1.0f); vba.Position<Vector3f>(1) = (const Vector3f&)mTriangleMVertex1; vba.Color<Float3>(0, 1) = Float3(0.0f, 0.0f, 1.0f); vba.Position<Vector3f>(2) = (const Vector3f&)mTriangleMVertex2; vba.Color<Float3>(0, 2) = Float3(0.0f, 0.0f, 1.0f); IndexBuffer* ibuffer = new0 IndexBuffer(3, sizeof(int)); int* indices = (int*)ibuffer->GetData(); indices[0] = 0; indices[1] = 1; indices[2] = 2; mTMesh = new0 TriMesh(vformat, vbuffer, ibuffer); mTMesh->SetEffectInstance( VertexColor3Effect::CreateUniqueInstance()); mTMesh->LocalTransform.SetTranslate(APoint(0.0f, 1.125f, 0.0f)); mCMesh = StandardMesh(vformat).Cylinder(8, 16, mCylinderRadius, mCylinderHeight, false); vba.ApplyTo(mCMesh); for (int i = 0; i < vba.GetNumVertices(); ++i) { vba.Color<Float3>(0, i) = Float3(1.0f, 0.0f, 0.0f); } mCMesh->SetEffectInstance( VertexColor3Effect::CreateUniqueInstance()); mScene->AttachChild(mTMesh); mScene->AttachChild(mCMesh); }
//---------------------------------------------------------------------------- void BouncingBall::CreateScene () { mScene = new0 Node(); mWireState = new0 WireState(); mRenderer->SetOverrideWireState(mWireState); CreateBall(); CreateFloor(); CreateWall(); mScene->AttachChild(mFloor); mScene->AttachChild(mWall); // The floor reflects an image of the ball. mPREffect = new0 PlanarReflectionEffect(1); mPREffect->SetPlane(0, mFloor); mPREffect->SetReflectance(0, 0.2f); }
//---------------------------------------------------------------------------- void ConvexHull3D::CreateScene () { mScene = new0 Node(); mTrnNode = new0 Node(); mScene->AttachChild(mTrnNode); mWireState = new0 WireState(); mRenderer->SetOverrideWireState(mWireState); mCullState = new0 CullState(); mCullState->Enabled = false; mRenderer->SetOverrideCullState(mCullState); VertexFormat* vformat = VertexFormat::Create(2, VertexFormat::AU_POSITION, VertexFormat::AT_FLOAT3, 0, VertexFormat::AU_COLOR, VertexFormat::AT_FLOAT3, 0); TriMesh* sphere = StandardMesh(vformat).Sphere(8, 8, 0.01f); sphere->SetEffectInstance(VertexColor3Effect::CreateUniqueInstance()); mTrnNode->SetChild(1, sphere); // The current file is "Data/data01.txt". LoadData(); }
//---------------------------------------------------------------------------- void HelixTubeSurface::CreateScene () { mScene = new0 Node(); mWireState = new0 WireState(); mRenderer->SetOverrideWireState(mWireState); VertexFormat* vformat = VertexFormat::Create(2, VertexFormat::AU_POSITION, VertexFormat::AT_FLOAT3, 0, VertexFormat::AU_TEXCOORD, VertexFormat::AT_FLOAT2, 0); mCurve = CreateCurve(); Float2 tcoordMin(0.0f, 0.0f), tcoordMax(1.0f, 32.0f); TubeSurface* tube = new TubeSurface(mCurve, Radial, false, Vector3f::UNIT_Z, 256, 32, false, true, &tcoordMin, &tcoordMax, vformat); std::string path = Environment::GetPathR("Grating.wmtf"); Texture2D* texture = Texture2D::LoadWMTF(path); tube->SetEffectInstance(Texture2DEffect::CreateUniqueInstance(texture, Shader::SF_LINEAR, Shader::SC_REPEAT, Shader::SC_REPEAT)); mScene->AttachChild(tube); }
//---------------------------------------------------------------------------- void ClodMeshes::CreateScene () { mScene = new0 Node(); mTrnNode = new0 Node(); mScene->AttachChild(mTrnNode); mWireState = new0 WireState(); mRenderer->SetOverrideWireState(mWireState); // Load the face model. #ifdef WM5_LITTLE_ENDIAN std::string path = Environment::GetPathR("FacePN.wmof"); #else std::string path = Environment::GetPathR("FacePN.be.wmof"); #endif InStream inStream; inStream.Load(path); TriMeshPtr mesh = StaticCast<TriMesh>(inStream.GetObjectAt(0)); VertexBufferAccessor vba0(mesh); // Remove the normals and add texture coordinates. VertexFormat* vformat = VertexFormat::Create(2, VertexFormat::AU_POSITION, VertexFormat::AT_FLOAT3, 0, VertexFormat::AU_TEXCOORD, VertexFormat::AT_FLOAT2, 0); int vstride = vformat->GetStride(); VertexBuffer* vbuffer = new0 VertexBuffer(vba0.GetNumVertices(), vstride); VertexBufferAccessor vba1(vformat, vbuffer); float xmin = Mathf::MAX_REAL, xmax = -Mathf::MAX_REAL; float ymin = Mathf::MAX_REAL, ymax = -Mathf::MAX_REAL; int i; for (i = 0; i < vba0.GetNumVertices(); ++i) { Float3 position = vba0.Position<Float3>(i); vba1.Position<Float3>(i) = position; float x = position[0]; float y = position[2]; vba1.TCoord<Float2>(0, i) = Float2(x, y); if (x < xmin) { xmin = x; } if (x > xmax) { xmax = x; } if (y < ymin) { ymin = y; } if (y > ymax) { ymax = y; } } float xmult = 1.0f/(xmax - xmin); float ymult = 1.0f/(ymax - ymin); for (i = 0; i < vba1.GetNumVertices(); ++i) { Float2 tcoord = vba1.TCoord<Float2>(0, i); vba1.TCoord<Float2>(0,i) = Float2( (tcoord[0] - xmin)*xmult, (tcoord[1] - ymin)*ymult); } mesh->SetVertexFormat(vformat); mesh->SetVertexBuffer(vbuffer); // Create a texture for the face. Use the generated texture coordinates. Texture2DEffect* effect = new0 Texture2DEffect(Shader::SF_LINEAR); path = Environment::GetPathR("Magician.wmtf"); Texture2D* texture = Texture2D::LoadWMTF(path); #ifdef USE_CLOD_MESH // Create the collapse records to be shared by two CLOD meshes. int numRecords = 0; CollapseRecord* records = 0; CreateClodMesh ccm(mesh, numRecords, records); CollapseRecordArray* recordArray = new0 CollapseRecordArray(numRecords, records); mClod[0] = new0 ClodMesh(mesh, recordArray); mClod[0]->LocalTransform = mesh->LocalTransform; mClod[0]->LocalTransform.SetTranslate(mesh->LocalTransform.GetTranslate() - 150.0f*AVector::UNIT_X); mClod[0]->SetEffectInstance(effect->CreateInstance(texture)); mTrnNode->AttachChild(mClod[0]); mClod[1] = new0 ClodMesh(mesh, recordArray); mClod[1]->LocalTransform = mesh->LocalTransform; mClod[1]->LocalTransform.SetTranslate(mesh->LocalTransform.GetTranslate() + 150.0f*AVector::UNIT_X - 100.0f*AVector::UNIT_Y); mClod[1]->SetEffectInstance(effect->CreateInstance(texture)); mTrnNode->AttachChild(mClod[1]); mActive = mClod[0]; #else IndexBuffer* ibuffer = mesh->GetIndexBuffer(); TriMesh* face = new0 TriMesh(vformat, vbuffer,ibuffer); face->LocalTransform = mesh->LocalTransform; face->LocalTransform.SetTranslate(mesh->LocalTransform.GetTranslate() - 150.0f*AVector::UNIT_X); face->SetEffectInstance(effect->CreateInstance(texture)); mTrnNode->AttachChild(face); face = new0 TriMesh(vformat, vbuffer, ibuffer); face->LocalTransform = mesh->LocalTransform; face->LocalTransform.SetTranslate(mesh->LocalTransform.GetTranslate() + 150.0f*AVector::UNIT_X); face->SetEffectInstance(effect->CreateInstance(texture)); mTrnNode->AttachChild(face); #endif }
//---------------------------------------------------------------------------- void CubeMaps::CreateScene () { // Create the root of the scene. mScene = new0 Node(); mWireState = new0 WireState(); mRenderer->SetOverrideWireState(mWireState); // Create the walls of the cube room. Each of the six texture images is // RGBA 64-by-64. Node* room = new0 Node(); mScene->AttachChild(room); // Index buffer shared by the room walls. IndexBuffer* ibuffer = new0 IndexBuffer(6, sizeof(int)); int* indices = (int*)ibuffer->GetData(); indices[0] = 0; indices[1] = 1; indices[2] = 3; indices[3] = 0; indices[4] = 3; indices[5] = 2; // The vertex format shared by the room walls. VertexFormat* vformat = VertexFormat::Create(2, VertexFormat::AU_POSITION, VertexFormat::AT_FLOAT3, 0, VertexFormat::AU_TEXCOORD, VertexFormat::AT_FLOAT2, 0); int vstride = vformat->GetStride(); VertexBufferAccessor vba; // The texture effect shared by the room walls. Texture2DEffect* effect = new0 Texture2DEffect(Shader::SF_LINEAR); VertexBuffer* vbuffer; TriMesh* wall; std::string textureName; // +x wall vbuffer = new0 VertexBuffer(4, vstride); vba.ApplyTo(vformat, vbuffer); vba.Position<Float3>(0) = Float3(+1.0f, -1.0f, -1.0f); vba.Position<Float3>(1) = Float3(+1.0f, -1.0f, +1.0f); vba.Position<Float3>(2) = Float3(+1.0f, +1.0f, -1.0f); vba.Position<Float3>(3) = Float3(+1.0f, +1.0f, +1.0f); vba.TCoord<Float2>(0, 0) = Float2(0.0f, 0.0f); vba.TCoord<Float2>(0, 1) = Float2(1.0f, 0.0f); vba.TCoord<Float2>(0, 2) = Float2(0.0f, 1.0f); vba.TCoord<Float2>(0, 3) = Float2(1.0f, 1.0f); wall = new0 TriMesh(vformat, vbuffer, ibuffer); room->AttachChild(wall); textureName = Environment::GetPathR("XpFace.wmtf"); Texture2D* xpTexture = Texture2D::LoadWMTF(textureName); wall->SetEffectInstance(effect->CreateInstance(xpTexture)); // -x wall vbuffer = new0 VertexBuffer(4, vstride); vba.ApplyTo(vformat, vbuffer); vba.Position<Float3>(0) = Float3(-1.0f, -1.0f, +1.0f); vba.Position<Float3>(1) = Float3(-1.0f, -1.0f, -1.0f); vba.Position<Float3>(2) = Float3(-1.0f, +1.0f, +1.0f); vba.Position<Float3>(3) = Float3(-1.0f, +1.0f, -1.0f); vba.TCoord<Float2>(0, 0) = Float2(0.0f, 0.0f); vba.TCoord<Float2>(0, 1) = Float2(1.0f, 0.0f); vba.TCoord<Float2>(0, 2) = Float2(0.0f, 1.0f); vba.TCoord<Float2>(0, 3) = Float2(1.0f, 1.0f); wall = new0 TriMesh(vformat, vbuffer, ibuffer); room->AttachChild(wall); textureName = Environment::GetPathR("XmFace.wmtf"); Texture2D* xmTexture = Texture2D::LoadWMTF(textureName); wall->SetEffectInstance(effect->CreateInstance(xmTexture)); // +y wall vbuffer = new0 VertexBuffer(4, vstride); vba.ApplyTo(vformat, vbuffer); vba.Position<Float3>(0) = Float3(+1.0f, +1.0f, +1.0f); vba.Position<Float3>(1) = Float3(-1.0f, +1.0f, +1.0f); vba.Position<Float3>(2) = Float3(+1.0f, +1.0f, -1.0f); vba.Position<Float3>(3) = Float3(-1.0f, +1.0f, -1.0f); vba.TCoord<Float2>(0, 0) = Float2(0.0f, 0.0f); vba.TCoord<Float2>(0, 1) = Float2(1.0f, 0.0f); vba.TCoord<Float2>(0, 2) = Float2(0.0f, 1.0f); vba.TCoord<Float2>(0, 3) = Float2(1.0f, 1.0f); wall = new0 TriMesh(vformat, vbuffer, ibuffer); room->AttachChild(wall); textureName = Environment::GetPathR("YpFace.wmtf"); Texture2D* ypTexture = Texture2D::LoadWMTF(textureName); wall->SetEffectInstance(effect->CreateInstance(ypTexture)); // -y wall vbuffer = new0 VertexBuffer(4, vstride); vba.ApplyTo(vformat, vbuffer); vba.Position<Float3>(0) = Float3(+1.0f, -1.0f, -1.0f); vba.Position<Float3>(1) = Float3(-1.0f, -1.0f, -1.0f); vba.Position<Float3>(2) = Float3(+1.0f, -1.0f, +1.0f); vba.Position<Float3>(3) = Float3(-1.0f, -1.0f, +1.0f); vba.TCoord<Float2>(0, 0) = Float2(0.0f, 0.0f); vba.TCoord<Float2>(0, 1) = Float2(1.0f, 0.0f); vba.TCoord<Float2>(0, 2) = Float2(0.0f, 1.0f); vba.TCoord<Float2>(0, 3) = Float2(1.0f, 1.0f); wall = new0 TriMesh(vformat, vbuffer, ibuffer); room->AttachChild(wall); textureName = Environment::GetPathR("YmFace.wmtf"); Texture2D* ymTexture = Texture2D::LoadWMTF(textureName); wall->SetEffectInstance(effect->CreateInstance(ymTexture)); // +z wall vbuffer = new0 VertexBuffer(4, vstride); vba.ApplyTo(vformat, vbuffer); vba.Position<Float3>(0) = Float3(+1.0f, -1.0f, +1.0f); vba.Position<Float3>(1) = Float3(-1.0f, -1.0f, +1.0f); vba.Position<Float3>(2) = Float3(+1.0f, +1.0f, +1.0f); vba.Position<Float3>(3) = Float3(-1.0f, +1.0f, +1.0f); vba.TCoord<Float2>(0, 0) = Float2(0.0f, 0.0f); vba.TCoord<Float2>(0, 1) = Float2(1.0f, 0.0f); vba.TCoord<Float2>(0, 2) = Float2(0.0f, 1.0f); vba.TCoord<Float2>(0, 3) = Float2(1.0f, 1.0f); wall = new0 TriMesh(vformat, vbuffer, ibuffer); room->AttachChild(wall); textureName = Environment::GetPathR("ZpFace.wmtf"); Texture2D* zpTexture = Texture2D::LoadWMTF(textureName); wall->SetEffectInstance(effect->CreateInstance(zpTexture)); // -z wall vbuffer = new0 VertexBuffer(4, vstride); vba.ApplyTo(vformat, vbuffer); vba.Position<Float3>(0) = Float3(-1.0f, -1.0f, -1.0f); vba.Position<Float3>(1) = Float3(+1.0f, -1.0f, -1.0f); vba.Position<Float3>(2) = Float3(-1.0f, +1.0f, -1.0f); vba.Position<Float3>(3) = Float3(+1.0f, +1.0f, -1.0f); vba.TCoord<Float2>(0, 0) = Float2(0.0f, 0.0f); vba.TCoord<Float2>(0, 1) = Float2(1.0f, 0.0f); vba.TCoord<Float2>(0, 2) = Float2(0.0f, 1.0f); vba.TCoord<Float2>(0, 3) = Float2(1.0f, 1.0f); wall = new0 TriMesh(vformat, vbuffer, ibuffer); room->AttachChild(wall); textureName = Environment::GetPathR("ZmFace.wmtf"); Texture2D* zmTexture = Texture2D::LoadWMTF(textureName); wall->SetEffectInstance(effect->CreateInstance(zmTexture)); // A sphere to reflect the environment via a cube map. The colors will // be used to modulate the cube map texture. vformat = VertexFormat::Create(3, VertexFormat::AU_POSITION, VertexFormat::AT_FLOAT3, 0, VertexFormat::AU_NORMAL, VertexFormat::AT_FLOAT3, 0, VertexFormat::AU_COLOR, VertexFormat::AT_FLOAT3, 0); vstride = vformat->GetStride(); mSphere = StandardMesh(vformat).Sphere(64, 64, 0.125f); room->AttachChild(mSphere); // Generate random vertex colors for the sphere. The StandardMesh class // produces a sphere with duplicated vertices along a longitude line. // This allows texture coordinates to be assigned in a manner that treats // the sphere as if it were a rectangle mesh. For vertex colors, we want // the duplicated vertices to have the same color, so a hash table is used // to look up vertex colors for the duplicates. vba.ApplyTo(mSphere); std::map<Float3,Float3> dataMap; for (int i = 0; i < vba.GetNumVertices(); ++i) { Float3& position = vba.Position<Float3>(i); Float3& color = vba.Color<Float3>(0, i); std::map<Float3,Float3>::iterator iter = dataMap.find(position); if (iter != dataMap.end()) { color = iter->second; } else { color[0] = 0.0f; color[1] = Mathf::IntervalRandom(0.5f, 0.75f); color[2] = Mathf::IntervalRandom(0.75f, 1.0f); dataMap.insert(std::make_pair(position, color)); } } // Create the cube map and attach it to the sphere. std::string effectFile = Environment::GetPathR("CubeMap.wmfx"); CubeMapEffect* cubeMapEffect = new0 CubeMapEffect(effectFile); ShaderFloat* reflectivity = new0 ShaderFloat(1); (*reflectivity)[0] = 0.5f; std::string cubeName = Environment::GetPathR("CubeMap.wmtf"); TextureCube* cubeTexture = TextureCube::LoadWMTF(cubeName); cubeTexture->GenerateMipmaps(); mCubeMapInstance = cubeMapEffect->CreateInstance(cubeTexture, reflectivity, false); mSphere->SetEffectInstance(mCubeMapInstance); // Allow culling to be disabled on the sphere so when you move inside // the sphere, you can see the previously hidden facets and verify that // the cube image for those facets is correctly oriented. mSphereCullState = cubeMapEffect->GetCullState(0, 0); }
//---------------------------------------------------------------------------- void CollisionsBoundTree::CreateScene () { // The root of the scene will have two cylinders as children. mScene = new0 Node(); mWireState = new0 WireState(); mRenderer->SetOverrideWireState(mWireState); mCullState = new0 CullState(); mCullState->Enabled = false; mRenderer->SetOverrideCullState(mCullState); // Create a texture image to be used by both cylinders. Texture2D* texture = new0 Texture2D(Texture::TF_A8R8G8B8, 2, 2, 1); unsigned int* data = (unsigned int*)texture->GetData(0); data[0] = Color::MakeR8G8B8(0, 0, 255); // blue data[1] = Color::MakeR8G8B8(0, 255, 255); // cyan data[2] = Color::MakeR8G8B8(255, 0, 0); // red data[3] = Color::MakeR8G8B8(255, 255, 0); // yellow Texture2DEffect* effect = new0 Texture2DEffect(Shader::SF_LINEAR); // Create two cylinders, one short and thick, one tall and thin. VertexFormat* vformat = VertexFormat::Create(2, VertexFormat::AU_POSITION, VertexFormat::AT_FLOAT3, 0, VertexFormat::AU_TEXCOORD, VertexFormat::AT_FLOAT2, 0); StandardMesh sm(vformat); VertexBufferAccessor vba; int i; mCylinder0 = sm.Cylinder(8, 16, 1.0f, 2.0f, false); vba.ApplyTo(mCylinder0); for (i = 0; i < vba.GetNumVertices(); ++i) { vba.TCoord<Float2>(0, i) = mBlueUV; } mCylinder0->SetEffectInstance(effect->CreateInstance(texture)); mScene->AttachChild(mCylinder0); mCylinder1 = sm.Cylinder(16,8,0.25,4.0,false); vba.ApplyTo(mCylinder1); for (i = 0; i < vba.GetNumVertices(); ++i) { vba.TCoord<Float2>(0, i) = mRedUV; } mCylinder1->SetEffectInstance(effect->CreateInstance(texture)); mScene->AttachChild(mCylinder1); mScene->Update(); // Set up the collision system. Record0 handles the collision response. // Record1 is not given a callback so that 'double processing' of the // events does not occur. CTree* tree0 = new0 CTree(mCylinder0, 1, false); CRecord* record0 = new0 CRecord(tree0, 0, Response, this); CTree* tree1 = new0 CTree(mCylinder1, 1, false); CRecord* record1 = new0 CRecord(tree1, 0, 0, 0); mGroup = new0 CGroup(); mGroup->Add(record0); mGroup->Add(record1); ResetColors(); mGroup->TestIntersection(); }
//---------------------------------------------------------------------------- void BSplineSurfaceFitter::CreateScene () { mScene = new0 Node(); mWireState = new0 WireState(); mRenderer->SetOverrideWireState(mWireState); mCullState = new0 CullState(); mCullState->Enabled = false; mRenderer->SetOverrideCullState(mCullState); // Begin with a flat 64x64 height field. const int numSamples = 64; const float extent = 8.0f; VertexFormat* vformat = VertexFormat::Create(2, VertexFormat::AU_POSITION, VertexFormat::AT_FLOAT3, 0, VertexFormat::AU_TEXCOORD, VertexFormat::AT_FLOAT2, 0); mHeightField = StandardMesh(vformat).Rectangle(numSamples, numSamples, extent, extent); mScene->AttachChild(mHeightField); // Set the heights based on a precomputed height field. Also create a // texture image to go with the height field. std::string path = Environment::GetPathR("HeightField.wmtf"); Texture2D* texture = Texture2D::LoadWMTF(path); VisualEffectInstance* instance = Texture2DEffect::CreateUniqueInstance( texture, Shader::SF_LINEAR, Shader::SC_CLAMP_EDGE, Shader::SC_CLAMP_EDGE); mHeightField->SetEffectInstance(instance); unsigned char* data = (unsigned char*)texture->GetData(0); VertexBufferAccessor vba(mHeightField); Vector3f** samplePoints = new2<Vector3f>(numSamples, numSamples); int i; for (i = 0; i < vba.GetNumVertices(); ++i) { unsigned char value = *data; float height = 3.0f*((float)value)/255.0f + 0.05f*Mathf::SymmetricRandom(); *data++ = (unsigned char)Mathf::IntervalRandom(32.0f, 64.0f); *data++ = 3*(128 - value/2)/4; *data++ = 0; data++; vba.Position<Vector3f>(i).Z() = height; samplePoints[i % numSamples][i / numSamples] = vba.Position<Vector3f>(i); } // Compute a B-Spline surface with NxN control points, where N < 64. // This surface will be sampled to 64x64 and displayed together with the // original height field for comparison. const int numCtrlPoints = 32; const int degree = 3; BSplineSurfaceFitf fitter(degree, numCtrlPoints, numSamples, degree, numCtrlPoints, numSamples, samplePoints); delete2(samplePoints); vformat = VertexFormat::Create(2, VertexFormat::AU_POSITION, VertexFormat::AT_FLOAT3, 0, VertexFormat::AU_COLOR, VertexFormat::AT_FLOAT4, 0); mFittedField = StandardMesh(vformat).Rectangle(numSamples, numSamples, extent, extent); mScene->AttachChild(mFittedField); vba.ApplyTo(mFittedField); Float4 translucent(1.0f, 1.0f, 1.0f, 0.5f); for (i = 0; i < vba.GetNumVertices(); ++i) { float u = 0.5f*(vba.Position<Vector3f>(i).X()/extent + 1.0f); float v = 0.5f*(vba.Position<Vector3f>(i).Y()/extent + 1.0f); vba.Position<Vector3f>(i) = fitter.GetPosition(u, v); vba.Color<Float4>(0,i) = translucent; } instance = VertexColor4Effect::CreateUniqueInstance(); mFittedField->SetEffectInstance(instance); instance->GetEffect()->GetAlphaState(0, 0)->BlendEnabled = true; }
//---------------------------------------------------------------------------- void PolygonOffsets::CreateScene () { mScene = new0 Node(); mWireState = new0 WireState(); mRenderer->SetOverrideWireState(mWireState); VertexFormat* vformat = VertexFormat::Create(2, VertexFormat::AU_POSITION, VertexFormat::AT_FLOAT3, 0, VertexFormat::AU_COLOR, VertexFormat::AT_FLOAT3, 0); int vstride = vformat->GetStride(); // Vertices to be shared by rectangles 1 and 3. VertexBuffer* vbuffer0 = new0 VertexBuffer(4, vstride); VertexBufferAccessor vba(vformat, vbuffer0); vba.Position<Float3>(0) = Float3(-1.0f, 0.0f, -1.0f); vba.Position<Float3>(1) = Float3(-1.0f, 0.0f, +1.0f); vba.Position<Float3>(2) = Float3(+1.0f, 0.0f, +1.0f); vba.Position<Float3>(3) = Float3(+1.0f, 0.0f, -1.0f); vba.Color<Float3>(0, 0) = Float3(1.0f, 0.0f, 0.0f); vba.Color<Float3>(0, 1) = Float3(1.0f, 0.0f, 0.0f); vba.Color<Float3>(0, 2) = Float3(1.0f, 0.0f, 0.0f); vba.Color<Float3>(0, 3) = Float3(1.0f, 0.0f, 0.0f); // Vertices to be shared by rectangles 2 and 4. VertexBuffer* vbuffer1 = new0 VertexBuffer(4, vstride); vba.ApplyTo(vformat, vbuffer1); vba.Position<Float3>(0) = Float3(-1.0f, 0.0f, -1.0f); vba.Position<Float3>(1) = Float3(-1.0f, 0.0f, +1.0f); vba.Position<Float3>(2) = Float3(+1.0f, 0.0f, +1.0f); vba.Position<Float3>(3) = Float3(+1.0f, 0.0f, -1.0f); vba.Color<Float3>(0, 0) = Float3(0.0f, 1.0f, 0.0f); vba.Color<Float3>(0, 1) = Float3(0.0f, 1.0f, 0.0f); vba.Color<Float3>(0, 2) = Float3(0.0f, 1.0f, 0.0f); vba.Color<Float3>(0, 3) = Float3(0.0f, 1.0f, 0.0f); // Indices to be shared by all rectangles. IndexBuffer* ibuffer = new0 IndexBuffer(6, sizeof(int)); int* indices = (int*)ibuffer->GetData(); indices[0] = 0; indices[1] = 1; indices[2] = 3; indices[3] = 3; indices[4] = 1; indices[5] = 2; // Effect to be shared by the first three rectangles. VertexColor3Effect* effect = new0 VertexColor3Effect(); // rectangle 1 TriMesh* mesh = new0 TriMesh(vformat, vbuffer0, ibuffer); mesh->SetEffectInstance(effect->CreateInstance()); mesh->LocalTransform.SetTranslate(APoint(+2.0f, -4.0f, 0.0f)); mScene->AttachChild(mesh); // rectangle 2 mesh = new0 TriMesh(vformat, vbuffer1, ibuffer); mesh->SetEffectInstance(effect->CreateInstance()); mesh->LocalTransform.SetTranslate(APoint(+2.0f, -4.0f, 0.0f)); mesh->LocalTransform.SetUniformScale(0.5f); mScene->AttachChild(mesh); // rectangle 3 mesh = new0 TriMesh(vformat, vbuffer0, ibuffer); mesh->SetEffectInstance(effect->CreateInstance()); mesh->LocalTransform.SetTranslate(APoint(-2.0f, -4.0f, 0.0f)); mScene->AttachChild(mesh); // rectangle 4 mesh = new0 TriMesh(vformat, vbuffer1, ibuffer); mesh->LocalTransform.SetTranslate(APoint(-2.0f, -4.0f, 0.0f)); mesh->LocalTransform.SetUniformScale(0.5f); mScene->AttachChild(mesh); // Set up the polygon offset for rectangle 4. effect = new0 VertexColor3Effect(); OffsetState* ostate = effect->GetOffsetState(0, 0); ostate->FillEnabled = true; ostate->Scale = -1.0f; ostate->Bias = -2.0f; mesh->SetEffectInstance(effect->CreateInstance()); }
//---------------------------------------------------------------------------- void GeodesicHeightField::CreateScene () { mScene = new0 Node(); mWireState = new0 WireState(); mRenderer->SetOverrideWireState(mWireState); mCullState = new0 CullState(); mCullState->Enabled = false; mRenderer->SetOverrideCullState(mCullState); // Create the ground. It covers a square with vertices (1,1,0), (1,-1,0), // (-1,1,0), and (-1,-1,0). VertexFormat* vformat = VertexFormat::Create(3, VertexFormat::AU_POSITION, VertexFormat::AT_FLOAT3, 0, VertexFormat::AU_NORMAL, VertexFormat::AT_FLOAT3, 0, VertexFormat::AU_TEXCOORD, VertexFormat::AT_FLOAT2, 0); const int xSize = 64; const int ySize = 64; const float xExtent = 1.0f; const float yExtent = 1.0f; mMesh = StandardMesh(vformat).Rectangle(xSize, ySize, xExtent, yExtent); // Create a B-Spline height field. The heights of the control point are // defined in an input file. The input file is structured as // // numUCtrlPoints numVCtrlPoints UDegree VDegree // z[0][0] z[0][1] ... z[0][numV-1] // z[1][0] z[1][1] ... z[1][numV_1] // : // z[numU-1][0] z[numU-1][1] ... z[numU-1][numV-1] std::string path = Environment::GetPathR("ControlPoints.txt"); std::ifstream inFile(path.c_str()); int numUCtrlPoints, numVCtrlPoints, uDegree, vDegree; double height; inFile >> numUCtrlPoints; inFile >> numVCtrlPoints; inFile >> uDegree; inFile >> vDegree; Vector3d** ctrlPoints = new2<Vector3d>(numUCtrlPoints, numVCtrlPoints); int i; for (i = 0; i < numUCtrlPoints; ++i) { double u = (double)(xExtent*(-1.0f + 2.0f*i/(numUCtrlPoints-1))); for (int j = 0; j < numVCtrlPoints; ++j) { double v = (double)(yExtent*(-1.0f + 2.0f*j/(numVCtrlPoints-1))); inFile >> height; ctrlPoints[i][j] = Vector3d(u, v, height); } } inFile.close(); mSurface = new0 BSplineRectangled(numUCtrlPoints, numVCtrlPoints, ctrlPoints, uDegree, vDegree, false, false, true, true); delete2(ctrlPoints); VertexBufferAccessor vba(mMesh); for (i = 0; i < vba.GetNumVertices(); ++i) { Vector3f& position = vba.Position<Vector3f>(i); double u = (double)((position.X() + xExtent)/(2.0f*xExtent)); double v = (double)((position.Y() + yExtent)/(2.0f*yExtent)); position.Z() = (float)mSurface->P(u,v).Z(); } mMesh->UpdateModelSpace(Visual::GU_NORMALS); // Attach an effect that uses lights, material, and texture. mMesh->SetEffectInstance(CreateEffectInstance()); mScene->AttachChild(mMesh); // Create the geodesic calculator. mGeodesic = new0 BSplineGeodesicd(*mSurface); mGeodesic->Subdivisions = 6; mGeodesic->Refinements = 1; mGeodesic->SearchRadius = 0.1; mGeodesic->RefineCallback = &GeodesicHeightField::RefineCallback; mPQuantity = (1 << mGeodesic->Subdivisions) + 1; }
//---------------------------------------------------------------------------- void MaterialTextures::CreateScene () { mScene = new0 Node(); mTrnNode = new0 Node(); mScene->AttachChild(mTrnNode); mWireState = new0 WireState(); mRenderer->SetOverrideWireState(mWireState); // Create a square object. VertexFormat* vformat = VertexFormat::Create(2, VertexFormat::AU_POSITION, VertexFormat::AT_FLOAT3, 0, VertexFormat::AU_TEXCOORD, VertexFormat::AT_FLOAT2, 0); int vstride = vformat->GetStride(); VertexBuffer* vbuffer = new0 VertexBuffer(4, vstride); VertexBufferAccessor vba(vformat, vbuffer); vba.Position<Float3>(0) = Float3(-1.0f, -1.0f, 0.0f); vba.Position<Float3>(1) = Float3(-1.0f, 1.0f, 0.0f); vba.Position<Float3>(2) = Float3( 1.0f, 1.0f, 0.0f); vba.Position<Float3>(3) = Float3( 1.0f, -1.0f, 0.0f); vba.TCoord<Float2>(0, 0) = Float2(0.0f, 0.0f); vba.TCoord<Float2>(0, 1) = Float2(1.0f, 0.0f); vba.TCoord<Float2>(0, 2) = Float2(1.0f, 1.0f); vba.TCoord<Float2>(0, 3) = Float2(0.0f, 1.0f); IndexBuffer* ibuffer = new0 IndexBuffer(6, sizeof(int)); int* indices = (int*)ibuffer->GetData(); indices[0] = 0; indices[1] = 1; indices[2] = 3; indices[3] = 3; indices[4] = 1; indices[5] = 2; // Create a square with a door texture. TriMesh* door = new0 TriMesh(vformat, vbuffer, ibuffer); std::string path = Environment::GetPathR("Door.wmtf"); Texture2D* texture = Texture2D::LoadWMTF(path); door->SetEffectInstance(Texture2DEffect::CreateUniqueInstance(texture, Shader::SF_LINEAR, Shader::SC_CLAMP_EDGE, Shader::SC_CLAMP_EDGE)); mTrnNode->AttachChild(door); // Material-texture effect shared by two objects. The material is // semitransparent, so alpha blending must be enabled. MaterialTextureEffect* effect = new0 MaterialTextureEffect(Shader::SF_LINEAR); effect->GetAlphaState(0, 0)->BlendEnabled = true; // Create a square with a material-texture effect. The texture is combined // with the material to produce a semitransparenteffect on the sand. You // should be able to see the door through it. TriMesh* sand = new0 TriMesh(vformat, vbuffer, ibuffer); sand->LocalTransform.SetTranslate(APoint(0.25f, 0.25f, -0.25f)); mTrnNode->AttachChild(sand); mMaterial = new0 Material(); mMaterial->Diffuse = Float4(1.0f, 0.0f, 0.0f, 0.5f); path = Environment::GetPathR("Sand.wmtf"); texture = Texture2D::LoadWMTF(path); VisualEffectInstance* instance = effect->CreateInstance(mMaterial, texture); sand->SetEffectInstance(instance); // The material alpha is adjustable during run time, so we must enable // the corresponding shader constant to automatically update. instance->GetVertexConstant(0, "MaterialDiffuse")->EnableUpdater(); // Create another square with a material-texture effect. TriMesh* water = new0 TriMesh(vformat, vbuffer, ibuffer); water->LocalTransform.SetTranslate(APoint(0.5f, 0.5f, -0.5f)); mTrnNode->AttachChild(water); Material* material = new0 Material(); material->Diffuse = Float4(0.0f, 0.0f, 1.0f, 0.5f); path = Environment::GetPathR("Water.wmtf"); texture = Texture2D::LoadWMTF(path); water->SetEffectInstance(effect->CreateInstance(material, texture)); }
//---------------------------------------------------------------------------- VisualEffectInstance* GeodesicHeightField::CreateEffectInstance () { // Create the vertex shader. VertexShader* vshader = new0 VertexShader("Wm5.DLight2MatTex", 3, 3, 16, 0, false); vshader->SetInput(0, "modelPosition", Shader::VT_FLOAT3, Shader::VS_POSITION); vshader->SetInput(1, "modelNormal", Shader::VT_FLOAT3, Shader::VS_NORMAL); vshader->SetInput(2, "modelTCoord", Shader::VT_FLOAT2, Shader::VS_TEXCOORD0); vshader->SetOutput(0, "clipPosition", Shader::VT_FLOAT4, Shader::VS_POSITION); vshader->SetOutput(1, "vertexColor", Shader::VT_FLOAT4, Shader::VS_COLOR0); vshader->SetOutput(2, "vertexTCoord", Shader::VT_FLOAT2, Shader::VS_TEXCOORD0); vshader->SetConstant( 0, "PVWMatrix", 4); vshader->SetConstant( 1, "CameraModelPosition", 1); vshader->SetConstant( 2, "MaterialEmissive", 1); vshader->SetConstant( 3, "MaterialAmbient", 1); vshader->SetConstant( 4, "MaterialDiffuse", 1); vshader->SetConstant( 5, "MaterialSpecular", 1); vshader->SetConstant( 6, "Light0ModelDirection", 1); vshader->SetConstant( 7, "Light0Ambient", 1); vshader->SetConstant( 8, "Light0Diffuse", 1); vshader->SetConstant( 9, "Light0Specular", 1); vshader->SetConstant(10, "Light0Attenuation", 1); vshader->SetConstant(11, "Light1ModelDirection", 1); vshader->SetConstant(12, "Light1Ambient", 1); vshader->SetConstant(13, "Light1Diffuse", 1); vshader->SetConstant(14, "Light1Specular", 1); vshader->SetConstant(15, "Light1Attenuation", 1); vshader->SetBaseRegisters(msVRegisters); vshader->SetPrograms(msVPrograms); // Create the pixel shader. PixelShader* pshader = new0 PixelShader("Wm5.DLight2MatTex", 2, 1, 0, 1, false); pshader->SetInput(0, "vertexColor", Shader::VT_FLOAT4, Shader::VS_COLOR0); pshader->SetInput(1, "vertexTCoord", Shader::VT_FLOAT2, Shader::VS_TEXCOORD0); pshader->SetOutput(0, "pixelColor", Shader::VT_FLOAT4, Shader::VS_COLOR0); pshader->SetSampler(0, "BaseSampler", Shader::ST_2D); pshader->SetFilter(0, Shader::SF_LINEAR /*_LINEAR */); pshader->SetCoordinate(0, 0, Shader::SC_CLAMP_EDGE); pshader->SetCoordinate(0, 1, Shader::SC_CLAMP_EDGE); pshader->SetTextureUnits(msPTextureUnits); pshader->SetPrograms(msPPrograms); VisualPass* pass = new0 VisualPass(); pass->SetVertexShader(vshader); pass->SetPixelShader(pshader); pass->SetAlphaState(new0 AlphaState()); pass->SetCullState(new0 CullState()); pass->SetDepthState(new0 DepthState()); pass->SetOffsetState(new0 OffsetState()); pass->SetStencilState(new0 StencilState()); pass->SetWireState(new0 WireState()); // Create the effect. VisualTechnique* technique = new0 VisualTechnique(); technique->InsertPass(pass); VisualEffect* effect = new0 VisualEffect(); effect->InsertTechnique(technique); // Create the material for the effect. Float4 black(0.0f, 0.0f, 0.0f, 1.0f); Float4 white(1.0f, 1.0f, 1.0f, 1.0f); Material* material = new0 Material(); material->Emissive = black; material->Ambient = Float4(0.24725f, 0.2245f, 0.0645f, 1.0f); material->Diffuse = Float4(0.34615f, 0.3143f, 0.0903f, 1.0f); material->Specular = Float4(0.797357f, 0.723991f, 0.208006f, 83.2f); // Create the lights for the effect. Light* light0 = new0 Light(Light::LT_DIRECTIONAL); light0->SetDirection(AVector(0.0f, 0.0f, -1.0f)); light0->Ambient = white; light0->Diffuse = white; light0->Specular = black; Light* light1 = new0 Light(Light::LT_DIRECTIONAL); light1->SetDirection(AVector(0.0f, 0.0f, 1.0f)); light1->Ambient = white; light1->Diffuse = white; light1->Specular = black; // Create a texture for the effect. mTexture = new0 Texture2D(Texture::TF_A8R8G8B8, 512, 512, 0); unsigned char* data = (unsigned char*)mTexture->GetData(0); memset(data, 0xFF, mTexture->GetNumLevelBytes(0)); // Create an instance of the effect. VisualEffectInstance* instance = new0 VisualEffectInstance(effect, 0); instance->SetVertexConstant(0, 0, new0 PVWMatrixConstant()); instance->SetVertexConstant(0, 1, new0 CameraModelPositionConstant()); instance->SetVertexConstant(0, 2, new0 MaterialEmissiveConstant(material)); instance->SetVertexConstant(0, 3, new0 MaterialAmbientConstant(material)); instance->SetVertexConstant(0, 4, new0 MaterialDiffuseConstant(material)); instance->SetVertexConstant(0, 5, new0 MaterialSpecularConstant(material)); instance->SetVertexConstant(0, 6, new0 LightModelDVectorConstant(light0)); instance->SetVertexConstant(0, 7, new0 LightAmbientConstant(light0)); instance->SetVertexConstant(0, 8, new0 LightDiffuseConstant(light0)); instance->SetVertexConstant(0, 9, new0 LightSpecularConstant(light0)); instance->SetVertexConstant(0, 10, new0 LightAttenuationConstant(light0)); instance->SetVertexConstant(0, 11, new0 LightModelDVectorConstant(light1)); instance->SetVertexConstant(0, 12, new0 LightAmbientConstant(light1)); instance->SetVertexConstant(0, 13, new0 LightDiffuseConstant(light1)); instance->SetVertexConstant(0, 14, new0 LightSpecularConstant(light1)); instance->SetVertexConstant(0, 15, new0 LightAttenuationConstant(light1)); instance->SetPixelTexture(0, 0, mTexture); return instance; }
//---------------------------------------------------------------------------- void IntersectingBoxes::CreateScene () { // Create some axis-aligned boxes for intersection testing. const int imax = 16; int i; for (i = 0; i < imax; ++i) { float xMin = 0.5f*mSize*Mathf::SymmetricRandom(); float xMax = xMin + Mathf::IntervalRandom(8.0f, 32.0f); float yMin = 0.5f*mSize*Mathf::SymmetricRandom(); float yMax = yMin + Mathf::IntervalRandom(8.0f, 32.0f); float zMin = 0.5f*mSize*Mathf::SymmetricRandom(); float zMax = zMin + Mathf::IntervalRandom(8.0f, 32.0f); mBoxes.push_back( AxisAlignedBox3f(xMin, xMax, yMin, yMax, zMin, zMax)); } mManager = new0 BoxManagerf(mBoxes); // Scene graph for the visual representation of the boxes. mScene = new0 Node(); mWireState = new0 WireState(); mRenderer->SetOverrideWireState(mWireState); // Effects for boxes, blue for nonintersecting and red for intersecting. Float4 black(0.0f, 0.0f, 0.0f, 1.0f); Float4 white(1.0f, 1.0f, 1.0f, 1.0f); Material* blueMaterial = new0 Material(); blueMaterial->Emissive = black; blueMaterial->Ambient = Float4(0.25f, 0.25f, 0.25f, 1.0f); blueMaterial->Diffuse = Float4(0.0f, 0.0f, 1.0f, 1.0f); blueMaterial->Specular = black; Material* redMaterial = new0 Material(); redMaterial->Emissive = black; redMaterial->Ambient = Float4(0.25f, 0.25f, 0.25f, 1.0f); redMaterial->Diffuse = Float4(1.0f, 0.0f, 0.0f, 1.0f); redMaterial->Specular = black; // A light for the effects. Light* light = new0 Light(Light::LT_DIRECTIONAL); light->Ambient = white; light->Diffuse = white; light->Specular = black; light->SetDirection(AVector::UNIT_Z); LightDirPerVerEffect* effect = new0 LightDirPerVerEffect(); mNoIntersectEffect = effect->CreateInstance(light, blueMaterial); mIntersectEffect = effect->CreateInstance(light, redMaterial); // Create visual representations of the boxes. VertexFormat* vformat = VertexFormat::Create(2, VertexFormat::AU_POSITION, VertexFormat::AT_FLOAT3, 0, VertexFormat::AU_NORMAL, VertexFormat::AT_FLOAT3, 0); for (i = 0; i < imax; ++i) { APoint center( 0.5f*(mBoxes[i].Min[0] + mBoxes[i].Max[0]), 0.5f*(mBoxes[i].Min[1] + mBoxes[i].Max[1]), 0.5f*(mBoxes[i].Min[2] + mBoxes[i].Max[2])); Transform transform; transform.SetTranslate(center); float xExtent = 0.5f*(mBoxes[i].Max[0] - mBoxes[i].Min[0]); float yExtent = 0.5f*(mBoxes[i].Max[1] - mBoxes[i].Min[1]); float zExtent = 0.5f*(mBoxes[i].Max[2] - mBoxes[i].Min[2]); StandardMesh sm(vformat, true, false, &transform); TriMesh* mesh = sm.Box(xExtent, yExtent, zExtent); mesh->SetEffectInstance(mNoIntersectEffect); mScene->AttachChild(mesh); } }
//---------------------------------------------------------------------------- void BouncingSpheres::CreateScene () { CreateBalls(); CreateFloor(); CreateBackWall(); CreateSideWall1(); CreateSideWall2(); // ** layout of scene graph ** // scene // room // backwall // floor // sidewall1 // sidewall2 // balls mScene = new0 Node(); mWireState = new0 WireState(); mRenderer->SetOverrideWireState(mWireState); Node* room = new0 Node(); room->AttachChild(mFloor); room->AttachChild(mSideWall1); room->AttachChild(mSideWall2); room->AttachChild(mBackWall); mScene->AttachChild(room); Node* ballRoot = new0 Node(); int i; for (i = 0; i < NUM_BALLS; ++i) { ballRoot->AttachChild(mBallNodes[i]); } mScene->AttachChild(ballRoot); // The balls are constrained to bounce around in a rectangular solid // region. The six defining planes are defined to be immovable rigid // bodies. The boundaries are parallel to coordinate axes and pass // through the points indicated by the value other than +-100. That is, // the back wall is at x = 1, the left wall is at y = 2, the floor is at // z = 1, the right wall is at y = 15, the ceiling is at z = 17, and the // front wall is at x = 9. The ceiling and front wall are invisible // objects (not rendered), but you will see balls bouncing against it // and reflecting away from it towards the back wall. mBoundaryLocations[0] = Vector3f(1.0f, -100.0f, -100.0f); mBoundaryNormals[0] = Vector3f(1.0f, 0.0f, 0.0f); mBoundaryLocations[1] = Vector3f(-100.0f, 2.0f, -100.0f); mBoundaryNormals[1] = Vector3f(0.0f, 1.0f, 0.0f); mBoundaryLocations[2] = Vector3f(-100.0f, -100.0f, 1.0f); mBoundaryNormals[2] = Vector3f(0.0f, 0.0f, 1.0f); mBoundaryLocations[3] = Vector3f(100.0f, 15.0f, 100.0f); mBoundaryNormals[3] = Vector3f(0.0f, -1.0f, 0.0f); mBoundaryLocations[4] = Vector3f(100.0f, 100.0f, 17.0f); mBoundaryNormals[4] = Vector3f(0.0f, 0.0f, -1.0f); mBoundaryLocations[5] = Vector3f(8.0f, 100.0f, 100.0f); mBoundaryNormals[5] = Vector3f(-1.0f, 0.0f, 0.0f); for (i = 0; i < 6; ++i) { mBoundaries[i].SetMass(0.0f); mBoundaries[i].SetPosition(mBoundaryLocations[i]); } }
//---------------------------------------------------------------------------- bool NonlocalBlowup::OnInitialize () { if (!WindowApplication3::OnInitialize()) { return false; } #ifdef RUN_CONSOLE RunConsole(); return false; #endif mScene = new0 Node(); mScene->LocalTransform.SetRotate(HMatrix( 0.80475128f, 0.59107417f, -0.054833174f, 0.0f, -0.17529237f, 0.32487807f, 0.92936903f, 0.0f, 0.56714010f, -0.73829913f, 0.36505684f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f)); mWireState = new0 WireState(); mRenderer->SetOverrideWireState(mWireState); VertexFormat* vformat = VertexFormat::Create(2, VertexFormat::AU_POSITION, VertexFormat::AT_FLOAT3, 0, VertexFormat::AU_TEXCOORD, VertexFormat::AT_FLOAT2, 0); TriMesh* mesh = StandardMesh(vformat).Rectangle(256, 256, 16.0f, 16.0f); mScene->AttachChild(mesh); std::string gridName = Environment::GetPathR("Grid.wmtf"); Texture2D* gridTexture = Texture2D::LoadWMTF(gridName); gridTexture->GenerateMipmaps(); Texture1D* colorTexture = new0 Texture1D(Texture::TF_A8R8G8B8, 8, 1); unsigned char* color = (unsigned char*)colorTexture->GetData(0); color[ 0] = 128; color[ 1] = 128; color[ 2] = 128; color[ 3] = 255; color[ 4] = 255; color[ 5] = 0; color[ 6] = 128; color[ 7] = 255; color[ 8] = 255; color[ 9] = 0; color[10] = 0; color[11] = 255; color[12] = 0; color[13] = 255; color[14] = 0; color[15] = 255; color[16] = 0; color[17] = 255; color[18] = 255; color[19] = 255; color[20] = 0; color[21] = 128; color[22] = 255; color[23] = 255; color[24] = 0; color[25] = 0; color[26] = 255; color[27] = 255; color[28] = 255; color[29] = 255; color[30] = 255; color[31] = 255; float dt = 0.01f, dx = 1.0f, dy = 1.0f; // Uncomment only one of these at a time. NonconvexDomain0p50(dt, dx, dy); //SquareSymmetric0p01(dt, dx, dy); //SquareSymmetric0p50(dt, dx, dy); //SquareSymmetric0p99(dt, dx, dy); //SquareGaussX0p50(dt, dx, dy); //SquareGaussXY0p50(dt, dx, dy); //SquareGaussFour0p50(dt, dx, dy); DisplacementEffect* effect = new0 DisplacementEffect(); mesh->SetEffectInstance(effect->CreateInstance(mHeightTexture, gridTexture, colorTexture, mDomainTexture)); // Set up the camera so that it looks at the graph from slightly above // the xy-plane and at a skewed angle/direction of view. mCamera->SetFrustum(60.0f, GetAspectRatio(), 1.0f, 10000.0f); APoint camPosition(0.0f, 3.46f, 42.97f); AVector camDVector(0.0f, 0.0f, -1.0f); AVector camUVector(0.0f, 1.0f, 0.0f); AVector camRVector = camDVector.Cross(camUVector); mCamera->SetFrame(camPosition, camDVector, camUVector, camRVector); // Initial update of objects. mScene->Update(); // Initial culling of scene. mCuller.SetCamera(mCamera); mCuller.ComputeVisibleSet(mScene); InitializeCameraMotion(0.01f, 0.01f); InitializeObjectMotion(mScene); return true; }