三角锥Cone

来自中国的朋友,欢迎您在本版面使用中文讨论问题。请注意,如果您想得到不懂中文的人的帮助,请同时提供英文译文。
Help and discussions in Chinese.
Post Reply
liqi98136
Posts: 53
Joined: Sun Jan 17, 2010 11:37 pm

三角锥Cone

Post by liqi98136 »

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;
}
Post Reply