void ZoaDebugFunctions::addQuadToMesh( ci::TriMesh& mesh, const ci::Vec3f& P0, const ci::Vec3f& P1, const ci::Vec3f& P2, const ci::Vec3f& P3, const ci::ColorA& color )
{
	ci::Vec3f e0 = P2 - P0;
	ci::Vec3f e1 = P2 - P1;
	ci::Vec3f n = e0.cross(e1).normalized();

	mesh.appendVertex( P0 );
	mesh.appendColorRGBA( color );
	mesh.appendNormal( n );

	mesh.appendVertex( P1 );
	mesh.appendColorRGBA( color );
	mesh.appendNormal( n );

	mesh.appendVertex( P2 );
	mesh.appendColorRGBA( color );
	mesh.appendNormal( n );

	mesh.appendVertex( P3 );
	mesh.appendColorRGBA( color );
	mesh.appendNormal( n );

	int vert0 = mesh.getNumVertices() - 4;
	int vert1 = mesh.getNumVertices() - 1;
	int vert2 = mesh.getNumVertices() - 2;
	int vert3 = mesh.getNumVertices() - 3;

	mesh.appendTriangle( vert0, vert3, vert1 );
	mesh.appendTriangle( vert3, vert2, vert1 );
}
static void genPlaneResult(unsigned int width, unsigned int height, ci::TriMesh& mesh)
{
    mesh.clear();

    float demiW = static_cast<float>(width / 2);
    float demiH = static_cast<float>(height / 2);

    for (float j = -demiH; j < demiH; ++j)
    {
        for (float i = -demiW; i < demiW; ++i)
        {
            mesh.appendVertex(ci::Vec3f(i, j, 0.0f));
            
            mesh.appendTexCoord(ci::Vec2f(
                (i + demiW) / width,
                (j + demiH) / height
                ));
        }
    }

    //For create face algorithme
    size_t w = width - 1;
    size_t h = height - 1;

    for (size_t j = 0; j < h; ++j)
    {
        for (size_t i = 0; i < w; ++i)
        {
            mesh.appendTriangle(width * j + i, width * (j + 1) + i, width * j + i + 1);
            mesh.appendTriangle(width * j + i + 1, width * (j + 1) + i, width * (j + 1) + i + 1);
        }
    }
}
Exemple #3
0
void World::generateMesh(ci::TriMesh & mesh)
{
   using namespace ci;
   
   Vec3f offset = Vec3f(m_size * -0.5f, m_size * -0.5f, m_size * -0.5f);
   
   for (size_t z = 0; z < m_size; ++z)
      for (size_t y = 0; y < m_size; ++y)
         for (size_t x = 0; x < m_size; ++x)
         {
            if (cell(x, y, z) == AIR)
            {
               for (size_t i = 0; i < 6; ++i)
               {
                  int index = getNeigbour(x, y, z, i);
                  
                  if (index != -1)
                  {
                     if (cell(index) == DIRT)
                     {
                        Vec3f normal = getNeigbourOffset(i);
                        Vec3f up = std::abs(normal.dot(Vec3f::yAxis())) > 0.9f ? Vec3f::zAxis() : Vec3f::yAxis();                   
                        Vec3f side = normal.cross(up) * 0.5f;
                        up = up * 0.5f;
                        
                        Vec3f pos = offset + Vec3f(x, y, z) + (normal * 0.5f);
                        
                        size_t indexStart = mesh.getNumVertices();
                        
                        
                        // Layout of the quad
                        //
                        // 0 --------------- 3
                        // |        ^ up     |
                        // |        |        |
                        // |  side  |        |
                        // |  <-----o pos    |
                        // |                 |
                        // |                 |
                        // |                 |
                        // 1 --------------- 2
                        
                        // Create the vertex data
                        mesh.appendVertex(pos + side + up);
                        mesh.appendVertex(pos + side - up);
                        mesh.appendVertex(pos - side - up);
                        mesh.appendVertex(pos - side + up);
                        mesh.appendNormal(-normal);
                        mesh.appendNormal(-normal);
                        mesh.appendNormal(-normal);
                        mesh.appendNormal(-normal);
                        
                        // Create triangle data
                        mesh.appendTriangle(indexStart + 0, indexStart + 1, indexStart + 2);
                        mesh.appendTriangle(indexStart + 0, indexStart + 2, indexStart + 3);
                     }
                  }
               }
            }
         }
}