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


lol, code. by godecho
1
 
    istringstream iss((char*)(&bytes[0]));
    int           type;
    unsigned int  primitiveCount = 0;
    unsigned int  indexCount     = 0;
    unsigned int  vertCount      = 0;
    IndexBuffer*  ib             = NULL;
    VertexBuffer* vb             = NULL;
    float         radius;

    iss >> type >> primitiveCount >> indexCount >> vertCount >> radius;
    
    if(!iss.bad()          &&
       primitiveCount  > 0 &&
       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;
        }
        
        // ********************
        // Read in index buffer
        // ********************
        ib = renderer->createIndexBuffer(indexCount * sizeof(short), INDEX_FORMAT16);  // Create index buffer
        short* indexData = reinterpret_cast<short*>(&(ib->getBytes()[0]));             // Get the internal pointer

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

        ib->update();   // update the index buffer with the API

        // *************************
        // Read in the vertex buffer
        // *************************
      
        vb = renderer->createVertexBuffer(sizeof(Vertex) * vertCount, sizeof(Vertex)); // Create vertex buffer
        Vertex* vertexData = reinterpret_cast<Vertex*>(&(vb->getBytes()[0]));          // Get the internal pointer
        
        // Reads in all of the vertecies from the iss
        for(unsigned int x = 0; x < vertCount; ++x)
        {
            iss >> vertexData[x];
        }

        vb->update();   // Unlock the buffer for reading

        if(!iss.bad())
        {
        // all AMF models have the same vertex format
            static VertexFormatElement format[] = { VertexFormatElement(POSITION,  VTX_FLOAT, 3),
                                                    VertexFormatElement(NORMAL,    VTX_FLOAT, 3),
                                                    VertexFormatElement(TEXCOORD0, VTX_FLOAT, 2),
                                                    VertexFormatElement::ENDELEMENT
                                                  };

            return new Model(ib,
                             vb,
                             format,
                             primitiveCount,
                             indexCount,
                             vertCount,
                             radius,
                             static_cast<Model::PrimitiveType>(type));
        }
        else
        {
            delete ib;
            delete vb;
        }
    }
    
    return NULL;