FrustumMesh

Post those lines of code you feel like sharing or find what you require for your project here; or simply use them as tutorials.
Post Reply
smso
Posts: 246
Joined: Fri Jun 04, 2010 3:28 pm
Location: Hong Kong

FrustumMesh

Post by smso »

Texture coordinates are not set up properly in the original CGeometryCreator::createConeMesh() function. So texture cannot be projected correctly onto the cone mesh. This similar function includes the texture info.

Code: Select all

 
scene::IMesh* createFrustumMesh
(
        f32 topRadius,
        f32 bottomRadius,
        f32 height,
        u32 tesselation = 128
)
{
        scene::SMeshBuffer* buffer = new scene::SMeshBuffer();
        video::S3DVertex v;
        f32 radius;
        f32 angle;
        f32 x, y, z;
        f32 s, t;
 
        for (u32 m = 0; m <= tesselation; ++m) // height step:
        {
                y = height * f32(m) / f32(tesselation);
                radius = ((height - y) * (bottomRadius - topRadius) / height) + topRadius;
                //radius = 15.0f;
                t = 1.0f - f32(m) / f32(tesselation);
                for (u32 n = 0; n <= tesselation; ++n) // subdivide circle:
                {
                        angle = core::PI * 2.0f * f32(n) / f32(tesselation);
                        x = radius * cosf(angle);
                        z = radius * sinf(angle);
                        s = f32(n) / f32(tesselation);
 
                        v.Pos.X = x;
                        v.Pos.Y = y;
                        v.Pos.Z = z;
 
                        v.Normal = v.Pos;
                        v.Normal.normalize();
                        v.Color = 0xFFFFFFFF;
 
                        //v.TCoords.X = s;
                        //v.TCoords.Y = t;
                        v.TCoords = core::vector2df(s, t);
 
                        buffer->Vertices.push_back(v);
                        //printf("(s,t)=%f,%f\n", s, t);
                }
        }
 
        u32 index00, index10, index11, index01;
        for (u32 m = 0; m < tesselation; ++m)
        {
                for (u32 n = 0; n < tesselation; ++n)
                {
                        index00 = m * (tesselation + 1) + n;
                        index10 = (m + 1) * (tesselation + 1) + n;
                        index11 = (m + 1) * (tesselation + 1) + n + 1;
                        index01 = m * (tesselation + 1) + n + 1;
 
                        buffer->Indices.push_back(index00);
                        buffer->Indices.push_back(index10);
                        buffer->Indices.push_back(index11);
 
                        buffer->Indices.push_back(index00);
                        buffer->Indices.push_back(index11);
                        buffer->Indices.push_back(index01);
                }
        }
        
        buffer->recalculateBoundingBox();
        scene::SMesh* mesh = new scene::SMesh();
        mesh->addMeshBuffer(buffer);
        buffer->drop();
 
        mesh->setHardwareMappingHint(scene::EHM_STATIC);
        mesh->recalculateBoundingBox();
        return mesh;
}
 
Post Reply