画三角形drawTriangle

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

画三角形drawTriangle

Post by liqi98136 »

drawTriangle.rar
(2.64 KiB) Downloaded 4793 times

Code: Select all

import "ecere"
#define SIDES 3 

class Triangle : Object
{
public:
   bool Create(DisplaySystem displaySystem)
   {
      bool result = false;
      if(this)
      {
         InitializeMesh(displaySystem);

         if(mesh)
         {
            if(mesh.Allocate({ vertices = true, texCoords1 = true }, SIDES, displaySystem))
            {
               Vector3Df vertices[SIDES] =
               {
                   //{1,0,0},
                   //{-1,0,0}, 
                   //{0,-1,0}
                  { (float)size.x/2,     0,              0 },
                  {  -(float)size.x/2,   0,              0 },
                  {  0,                -(float)size.y/2, 0 }
               };

               uint16 indices[SIDES] = { 0, 2, 1};

               CopyBytes(mesh.vertices, vertices, sizeof(vertices));
               {
                  PrimitiveGroup group;
                  String name = "triangle"; 
                  Material 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, SIDES);
                  
                  if(group)
                  {
                     CopyBytes(group.indices, indices, sizeof(indices));
                     mesh.UnlockPrimitiveGroup(group);
                     group.material = material;
                  }
               }
               mesh.ComputeNormals();
               result = true;
               mesh.Unlock(0);
            }
            SetMinMaxRadius(true);
         }
      }
      return result;
   }

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

private:
   Triangle()
   {
      size = { 1,1,0 };
   }

   Vector3Df size;
} 
liqi98136
Posts: 53
Joined: Sun Jan 17, 2010 11:37 pm

Re: 画圆drawCircle

Post by liqi98136 »

画圆,变椭圆时,size={a,b,0}改变a,b的值
drawCircle

Code: Select all

import "ecere"

#define SIDE 50   //内切多边形的边数
class Model : Object     // 画圆
{
public:
   bool Create(DisplaySystem displaySystem)
   {
      bool result = false;
      if(this)
      {
         InitializeMesh(displaySystem);

         if(mesh)
         {
            if(mesh.Allocate({ vertices = true, texCoords1 = true }, SIDE*3, displaySystem))
            {
               Vector3Df vertices[SIDE*3] ;   // 圆上坐标点
               uint16 indices[SIDE][3];     //圆上坐标点的索引
               int c;
               {
                  int x;
                  int index=0;
                  for (x=0;x<SIDE;x++)
                     
                     {
                       Angle theta=x*2*Pi/SIDE;
                       Angle omega=(x+1)*2*Pi/SIDE;
                       vertices[index++]={0,-(float)size.y/2,(float)size.z/2};
                       vertices[index++]={(float)(size.x*sin(theta)/2),-(float)(size.y*cos(theta)/2),(float)size.z/2};
                       vertices[index++]={(float)(size.x*sin(omega)/2),-(float)(size.y*cos(omega)/2),(float)size.z/2};
 
                     }
               };

               
               {
                  int i,j;
                  int x=SIDE*3-1;
                  for (i=0;i<SIDE;i++)
                     {
                     for(j=0;j<3;j++)
                        indices[i][j]=(uint16)(x-j);
                     x=x-3;
                     }
               };

               

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



               {
                  PrimitiveGroup group;
                  Material material;
                  String name {};
                  sprintf(name, "Triangle Face"); 
                  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*SIDE);
                  
                  if(group)
                  {
                     CopyBytes(group.indices, indices, sizeof(indices));
                     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,0 };
   }

   Vector3Df size;
}
Post Reply