//---------------------------------------------------------------------------- void ParticleSystems::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); int vstride = vformat->GetStride(); const int numParticles = 32; VertexBuffer* vbuffer = new0 VertexBuffer(4*numParticles, vstride); Float4* positionSizes = new1<Float4>(numParticles); for (int i = 0; i < numParticles; ++i) { positionSizes[i][0] = Mathf::SymmetricRandom(); positionSizes[i][1] = Mathf::SymmetricRandom(); positionSizes[i][2] = Mathf::SymmetricRandom(); positionSizes[i][3] = 0.25f*Mathf::UnitRandom(); } Particles* particles = new0 Particles(vformat, vbuffer, sizeof(int), positionSizes, 1.0f); particles->AttachController(new0 BloodCellController()); mScene->AttachChild(particles); // Create an image with transparency. const int xsize = 32, ysize = 32; Texture2D* texture = new0 Texture2D(Texture::TF_A8R8G8B8, xsize, ysize, 1); unsigned char* data = (unsigned char*)texture->GetData(0); float factor = 1.0f/(xsize*xsize + ysize*ysize); for (int y = 0, i = 0; y < ysize; ++y) { for (int x = 0; x < xsize; ++x) { // The image is red. data[i++] = 0; data[i++] = 0; data[i++] = 255; // Semitransparent within a disk, dropping off to zero outside the // disk. int dx = 2*x - xsize; int dy = 2*y - ysize; float value = factor*(dx*dx + dy*dy); if (value < 0.125f) { value = Mathf::Cos(4.0f*Mathf::PI*value); } else { value = 0.0f; } data[i++] = (unsigned char)(255.0f*value); } } Texture2DEffect* effect = new0 Texture2DEffect(Shader::SF_LINEAR); effect->GetAlphaState(0, 0)->BlendEnabled = true; effect->GetDepthState(0, 0)->Enabled = false; particles->SetEffectInstance(effect->CreateInstance(texture)); }
//---------------------------------------------------------------------------- void BspNodes::CreateScene () { // Create the scene graph. // // 1. The rectangles represent the BSP planes of the BSP tree. They // share a VertexColor3Effect. You can see a plane from either side // (backface culling disabled). The planes do not interfere with view // of the solid objects (wirestate enabled). // // 2. The sphere, tetrahedron, and cube share a TextureEffect. These // objects are convex. The backfacing triangles are discarded // (backface culling enabled). The front facing triangles are drawn // correctly by convexity, so depthbuffer reads are disabled and // depthbuffer writes are enabled. The BSP-based sorting of objects // guarantees that front faces of convex objects in the foreground // are drawn after the front faces of convex objects in the background, // which allows us to set the depthbuffer state as we have. That is, // BSPNode sorts from back to front. // // 3. The torus has backface culling enabled and depth buffering enabled. // This is necessary, because the torus is not convex. // // 4. Generally, if all objects are opaque, then you want to draw from // front to back with depth buffering fully enabled. You need to // reverse-order the elements of the visible set before drawing. If // any of the objects are semitransparent, then drawing back to front // is the correct order to handle transparency. However, you do not // get the benefit of early z-rejection for opaque objects. A better // BSP sorter needs to be built to produce a visible set with opaque // objects listed first (front-to-back order) and semitransparent // objects listed last (back-to-front order). // // scene // ground // bsp0 // bsp1 // bsp3 // torus // rectangle3 // sphere // rectangle1 // tetrahedron // rectangle0 // bsp2 // cube // rectangle2 // octahedron mScene = new0 Node(); // Create the ground. It covers a square with vertices (1,1,0), (1,-1,0), // (-1,1,0), and (-1,-1,0). Multiply the texture coordinates by a factor // to enhance the wrap-around. VertexFormat* vformat = VertexFormat::Create(2, VertexFormat::AU_POSITION, VertexFormat::AT_FLOAT3, 0, VertexFormat::AU_TEXCOORD, VertexFormat::AT_FLOAT2, 0); StandardMesh sm(vformat); VertexBufferAccessor vba; TriMesh* ground = sm.Rectangle(2, 2, 16.0f, 16.0f); vba.ApplyTo(ground); for (int i = 0; i < vba.GetNumVertices(); ++i) { Float2& tcoord = vba.TCoord<Float2>(0, i); tcoord[0] *= 128.0f; tcoord[1] *= 128.0f; } std::string path = Environment::GetPathR("Horizontal.wmtf"); Texture2D* texture = Texture2D::LoadWMTF(path); ground->SetEffectInstance(Texture2DEffect::CreateUniqueInstance(texture, Shader::SF_LINEAR_LINEAR, Shader::SC_REPEAT, Shader::SC_REPEAT)); mScene->AttachChild(ground); // Partition the region above the ground into 5 convex pieces. Each plane // is perpendicular to the ground (not required generally). VertexColor3Effect* vceffect = new0 VertexColor3Effect(); vceffect->GetCullState(0, 0)->Enabled = false; vceffect->GetWireState(0, 0)->Enabled = true; Vector2f v0(-1.0f, 1.0f); Vector2f v1(1.0f, -1.0f); Vector2f v2(-0.25f, 0.25f); Vector2f v3(-1.0f, -1.0f); Vector2f v4(0.0f, 0.0f); Vector2f v5(1.0f, 0.5f); Vector2f v6(-0.75f, -7.0f/12.0f); Vector2f v7(-0.75f, 0.75f); Vector2f v8(1.0f, 1.0f); BspNode* bsp0 = CreateNode(v0, v1, vceffect, Float3(1.0f, 0.0f, 0.0f)); BspNode* bsp1 = CreateNode(v2, v3, vceffect, Float3(0.0f, 0.5f, 0.0f)); BspNode* bsp2 = CreateNode(v4, v5, vceffect, Float3(0.0f, 0.0f, 1.0f)); BspNode* bsp3 = CreateNode(v6, v7, vceffect, Float3(0.0f, 0.0f, 0.0f)); bsp0->AttachPositiveChild(bsp1); bsp0->AttachNegativeChild(bsp2); bsp1->AttachPositiveChild(bsp3); // Attach an object in each convex region. float height = 0.1f; Vector2f center; TriMesh* mesh; // The texture effect for the convex objects. Texture2DEffect* cvxeffect = new0 Texture2DEffect(Shader::SF_LINEAR_LINEAR); cvxeffect->GetDepthState(0, 0)->Enabled = false; cvxeffect->GetDepthState(0, 0)->Writable = true; // The texture effect for the torus. Texture2DEffect* toreffect = new0 Texture2DEffect(Shader::SF_LINEAR_LINEAR); // The texture image shared by the objects. path = Environment::GetPathR("Flower.wmtf"); texture = Texture2D::LoadWMTF(path); // Region 0: Create a torus mesh. mesh = sm.Torus(16, 16, 1.0f, 0.25f); mesh->SetEffectInstance(toreffect->CreateInstance(texture)); mesh->LocalTransform.SetUniformScale(0.1f); center = (v2 + v6 + v7)/3.0f; mesh->LocalTransform.SetTranslate(APoint(center[0], center[1], height)); bsp3->AttachPositiveChild(mesh); // Region 1: Create a sphere mesh. mesh = sm.Sphere(32, 16, 1.0f); mesh->SetEffectInstance(cvxeffect->CreateInstance(texture)); mesh->LocalTransform.SetUniformScale(0.1f); center = (v0 + v3 + v6 + v7)/4.0f; mesh->LocalTransform.SetTranslate(APoint(center[0], center[1], height)); bsp3->AttachNegativeChild(mesh); // Region 2: Create a tetrahedron. mesh = sm.Tetrahedron(); mesh->SetEffectInstance(cvxeffect->CreateInstance(texture)); mesh->LocalTransform.SetUniformScale(0.1f); center = (v1 + v2 + v3)/3.0f; mesh->LocalTransform.SetTranslate(APoint(center[0], center[1], height)); bsp1->AttachNegativeChild(mesh); // Region 3: Create a hexahedron (cube). mesh = sm.Hexahedron(); mesh->SetEffectInstance(cvxeffect->CreateInstance(texture)); mesh->LocalTransform.SetUniformScale(0.1f); center = (v1 + v4 + v5)/3.0f; mesh->LocalTransform.SetTranslate(APoint(center[0], center[1], height)); bsp2->AttachPositiveChild(mesh); // Region 4: Create an octahedron. mesh = sm.Octahedron(); mesh->SetEffectInstance(cvxeffect->CreateInstance(texture)); mesh->LocalTransform.SetUniformScale(0.1f); center = (v0 + v4 + v5 + v8)/4.0f; mesh->LocalTransform.SetTranslate(APoint(center[0], center[1], height)); bsp2->AttachNegativeChild(mesh); mScene->AttachChild(bsp0); }