Ecere SDK/eC Forums
http://www.ecere.com/forums/
Print view

三角锥Cone
http://www.ecere.com/forums/viewtopic.php?f=30&t=42
Page 1 of 1
Author:  liqi98136 [ Wed Mar 10, 2010 8:59 am ]
Post subject:  三角锥Cone

Code: Select all

import "ecere"

class Model : Object     //三角锥
{
public:
   bool Create(DisplaySystem displaySystem)
   {
      bool result = false;
      if(this)
      {
         InitializeMesh(displaySystem);

         if(mesh)
         {
            if(mesh.Allocate({ vertices = true, texCoords1 = true }, 12, displaySystem))
            {
               Vector3Df vertices[12] =
               {
                    //正面
                   {0,               -(float)size.y/2,0},
                   {-(float)size.x/2,(float)size.y/2,(float)size.z/2},
                   {(float)size.x/2,(float)size.y/2,(float)size.z/2},     
                    //右侧面
                   { 0,-(float)size.y/2,0},
                   {(float)size.x/2,(float)size.y/2,(float)size.z/2},
                   {-(float)size.x/2,(float)size.y/2,-(float)size.z/2},    
                   //左侧面 
                   {0,-(float)size.y/2,0},
                   {-(float)size.x/2,(float)size.y/2,-(float)size.z/2},
                   {-(float)size.x/2,(float)size.y/2,(float)size.z/2},
                   //底面
                   {-(float)size.x/2,(float)size.y/2,(float)size.z/2},
                   {(float)size.x/2,(float)size.y/2,(float)size.z/2},
                   {-(float)size.x/2,(float)size.y/2,-(float)size.z/2},
               };
               Pointf texCoords[12] =
               {
                  { 0, 0 }, { 0, 1 }, { 1, 0 },
                  { 0, 0 }, { 1, 0 }, { 1, 1 },
                  { 0, 0 }, { 1, 1 }, { 0, 1 }, 
                  { 0, 1 }, { 1, 0 }, { 1, 1 }
               };
               uint16 indices[4][3] = { 
                              {0,1,2},
                              {3,4,5},
                              {6,7,8},
                              {9,10,11}
               };

               int c;

               CopyBytes(mesh.vertices, vertices, sizeof(vertices));
               CopyBytes(mesh.texCoords, texCoords, sizeof(texCoords));

               for(c = 0; c<4; c++)
               {
                  PrimitiveGroup group;
                  Material material;
                  String name {};
                  sprintf(name, "Triangle Face %d", c+1); 
                  material = displaySystem.AddNamedMaterial(name);
                  
                  if(material)
                  {
                     material.flags = { noFog = true, doubleSided = true, translucent = true };
                     material.opacity = 0.5f;

                     material.diffuse.r = material.diffuse.g = material.diffuse.b = 1;
                     material.ambient = material.diffuse;

                  }
                  group = mesh.AddPrimitiveGroup(triStrip, 3);
                  
                  if(group)
                  {
                     CopyBytes(group.indices, indices[c], sizeof(indices[c]));
                     mesh.UnlockPrimitiveGroup(group);
                     group.material = material;
                  }
               }
               mesh.ComputeNormals();
               result = true;
               mesh.Unlock(0);
            }
            SetMinMaxRadius(true);
         }
      }
      return result;
   }

   property Vector3Df size { set { size = value; } };

private:
   Model()
   {
      size = { 1,1,1 };
   }

   Vector3Df size;
}
All times are UTC-05:00 Page 1 of 1