GLProgramming.com

home :: about :: development guides :: irc :: forums :: search :: paste :: links :: contribute :: code dump

-> Click here to learn how to get live help <-


New Paste :: Recent Pastes:: No Line Numbers


ho ho ho ! by godecho
1
 
using std::max;
using std::vector;



struct Vertex
{
    Vec3f position;
    Vec3f normal;
    Vec2f texcoord;            
};

istream& operator >> (istream& is, Vertex& v)
{
    return is >> v.position.x >>
                 v.position.y >>
                 v.position.z >>
                 v.normal.x   >>
                 v.normal.y   >>
                 v.normal.z   >>
                 v.texcoord.x >> // u
                 v.texcoord.y;   // v
}

bool load(const ByteStream& bytes)
{

    istringstream iss((char*)(&bytes[0]));
    int           type;
    unsigned int  primitiveCount = 0;
    unsigned int  vertCount      = 0;
    Spheref       bounding;

    iss >> type >> primitiveCount >> bounding.center.x >> bounding.center.y >> bounding.center.z >> bounding.radius;
    
    if(!iss.bad()          &&
       primitiveCount  > 0 &&
       bounding.radius > 0)
    {
        // Determine the vert count based on the primitive type, and primitive count
        switch(type)
        {
            case Model::TriangleList:  vertCount = primitiveCount * 3; break;
            case Model::TriangleStrip: vertCount = primitiveCount + 1; break;
            case Model::TriangleFan:   vertCount = primitiveCount + 2; break;
            
            default: return false;
        }

        if(vertCount >= 65536) // If the vert count is >= 2^16 (max size of a unsigned short), that'll screw stuff up
        {
            return false;
        }
        
        vector<short> indexData(vertCount);

        // Reads in all of the indicies from the iss
        for(unsigned int x = 0; x < vertCount; ++x)
        {
            iss >> indexData[x];
        }

        vector<Vertex> verts(vertCount);
        
        // Reads in all of the vertecies from the iss
        for(unsigned int x = 0; x < vertCount; ++x)
        {
            iss >> verts[x];
        }

        #undef far
        #undef near

        float right = verts[0].x;
        float left  = verts[0].x;
        float high  = verts[0].y;
        float low   = verts[0].y;
        float far   = verts[0].z;
        float near  = verts[0].z;

        // find the extreme points
        for(unsigned int i = 1; i < vertCount; ++i)
        {
            if     (verts[i].x > right.x) right = verts[i].x;
            else if(verts[i].x < left.x)  left  = verts[i].x;

            if     (verts[i].y > high.y)  high  = verts[i].y;
            else if(verts[i].y < low.y)   low   = verts[i].y;

            if     (verts[i].z > far.z)   far   = verts[i].z;
            else if(verts[i].z < close.z) close = verts[i].z;
        }

        Vec3f midpoint((right-left)/2.0f,
                       (high -low) /2.0f,
                       (far  -near)/2.0f);

        // normalize the points to a 0,0,0 center
        for(unsigned int i = 0; i < vertCount; ++i)
        {
            verts[i] -= midpoint;
        }

        float radius = max(max((right-left), (high-low)), (far-near));
    }
}