Esempio n. 1
0
/// ---------------------------------------------------------------------------
/// Loads the scene file specified by the filename
/// ---------------------------------------------------------------------------
void prScene::Load(const char *pFilename)
{
    // Parse the document
    if (pFilename && *pFilename) 
    {
        TiXmlDocument* doc = new TiXmlDocument(pFilename);
        if (doc)
        {
            bool loaded = doc->LoadFile();      
            if (loaded)
            {
                ParseFile(doc);
            }
            else
            {
                PRWARN("Failed to Load scene '%s'\n", pFilename);
                return;
            }

            delete doc;
        }
    }
}
Esempio n. 2
0
// Loads the mesh.
//
bool prMesh_MD2::Load(const char *filename)
{
    // Loaded?
    if (!mLoaded)
    {
        // Open the file
        std::ifstream ifs(filename, std::ios::binary);

        if (ifs.fail())
        {
            PRWARN("Failed to open model file %s", filename);
            return false;
        }


        // Is the file too small?
        ifs.seekg(0, std::ios::end);
        int size = (int)ifs.tellg();
        ifs.seekg(0, std::ios::beg);

        if (size <= sizeof(prMD2Header))
        {
            PRWARN("File '%s' is not an MD2 file. It is too small", filename);
            ifs.close();
            return false;
        }


        // Load the header.
        prMD2Header header;

        if (ifs.read(reinterpret_cast<char*>(&header), sizeof(header)))
        {
            if (header.ident != MD2_MAGIC || header.version != MD2_VERSION)
            {
                PRWARN("File '%s' is not an MD2 file.", filename);
                ifs.close();
                return false;
            }


            // Display file details
            #ifdef MD2_DEBUG
            prTrace(prLogLevel::LogError, "-----------------------------------------\n");
            prTrace(prLogLevel::LogError, "File       :     %s\n", filename            );
            prTrace(prLogLevel::LogError, "-----------------------------------------\n");
            prTrace(prLogLevel::LogError, "Ident      :     %x\n", header.ident        );
            prTrace(prLogLevel::LogError, "Version    :     %i\n", header.version      );
            prTrace(prLogLevel::LogError, "Frames     :     %i\n", header.num_frames   );
            prTrace(prLogLevel::LogError, "commands   :     %i\n", header.num_glcmds   );
            prTrace(prLogLevel::LogError, "Triangles  :     %i\n", header.num_tris     );
            prTrace(prLogLevel::LogError, "Tex coords :     %i\n", header.num_st       );
            prTrace(prLogLevel::LogError, "Vertices   :     %i\n", header.num_vertices );
            prTrace(prLogLevel::LogError, "Textures   :     %i\n", header.num_skins    );
            prTrace(prLogLevel::LogError, "Tex width  :     %i\n", header.skinWidth    );
            prTrace(prLogLevel::LogError, "Tex height :     %i\n", header.skinHeight   );
            prTrace(prLogLevel::LogError, "Frame size :     %i\n", header.frameSize    );
            prTrace(prLogLevel::LogError, "-----------------------------------------\n");
            #endif


            // Memory allocation for model data
            m_texCoords = new prMD2TexCoord [header.num_st];
            m_triangles = new prMD2Triangle [header.num_tris];
            m_frames    = new prMD2Frame    [header.num_frames];
            m_glcmds    = new int           [header.num_glcmds];


            // Read skin names
            if (header.num_skins > 0)
            {
                m_skins = new prMD2Skin [header.num_skins];

                ifs.seekg(header.offset_skins, std::ios::beg);
                ifs.read(reinterpret_cast<char*>(m_skins), sizeof (prMD2Skin) * header.num_skins);

                #ifdef MD2_DEBUG
                for (int i=0; i<header.num_skins; i++)
                {
                    prTrace(prLogLevel::LogError, "Skin: %s\n", m_skins[i].name);

                    char buffer[FILE_MAX_FILENAME_SIZE];

                    prStringCopySafe(buffer, m_skins[i].name, sizeof(buffer));
                    prTrace(prLogLevel::LogError, "Skin 1: %s\n", buffer);

                    prStringToLower(buffer);
                    prTrace(prLogLevel::LogError, "Skin 2: %s\n", buffer);

                    prStringReplaceChar(buffer, '\\', '/');
                    prTrace(prLogLevel::LogError, "Skin 3: %s\n", buffer);

                    s32 index = prStringFindLastIndex(buffer, '/');
                    if (index > 0)
                    {
                        prStringCopySafe(buffer, &buffer[index + 1], sizeof(buffer));
                    }

                    //prStringReplaceChar(buffer, '\\', '/');
                    prTrace(prLogLevel::LogError, "Skin 4: %s\n", buffer);

                    index = prStringFindLastIndex(buffer, '.');
                    if (index > 0)
                    {
                        buffer[index] = 0;
                    //    prStringCopySafe(buffer, m_skins[i].name, sizeof(buffer));
                    }
                    prTrace(prLogLevel::LogError, "Skin 5: %s\n", buffer);
                    //else
                    //{
                    //}

                    prStringAddString(".pvr", buffer);
                    prTrace(prLogLevel::LogError, "Skin 6: %s\n", buffer);

                    char fullpath[FILE_MAX_FILENAME_SIZE];
                    sprintf(fullpath, "data/textures/%s", buffer);

                    prTrace(prLogLevel::LogError, "Texture: '%s'\n", fullpath);
                    
                    // Load the texture
                    prResourceManager *pRM = static_cast<prResourceManager *>(prCoreGetComponent(PRSYSTEM_RESOURCEMANAGER));
                    if (pRM)
                    {
                        mpTexture = pRM->Load<prTexture>(fullpath);

                        //pRM->Load(fullpath);//  >Unload((*it).texture);
                    }            

                }
                #endif
            }
            else
            {
                prTrace(prLogLevel::LogError, "Model %s has no associated textures\n", filename);
            }


            // Read texture coords.
            ifs.seekg(header.offset_st, std::ios::beg);
            ifs.read(reinterpret_cast<char*>(m_texCoords), sizeof(prMD2TexCoord) * header.num_st);

            // Read triangle data
            ifs.seekg(header.offset_tris, std::ios::beg);
            ifs.read(reinterpret_cast<char*>(m_triangles), sizeof(prMD2Triangle) * header.num_tris);

            // Read frames
            ifs.seekg(header.offset_frames, std::ios::beg);
            
            for (int i=0; i<header.num_frames; i++)
            {
                // Memory allocation for the vertices of this frame
                m_frames[i].verts = new prMD2Vertex[header.num_vertices];

                // Read frame data
                ifs.read(reinterpret_cast<char*>(&m_frames[i].scale),    sizeof(prMD2Vec3));
                ifs.read(reinterpret_cast<char*>(&m_frames[i].translate),sizeof(prMD2Vec3));
                ifs.read(reinterpret_cast<char*>(&m_frames[i].name),     sizeof(char) * 16);
                ifs.read(reinterpret_cast<char*>( m_frames[i].verts),    sizeof(prMD2Vertex) * header.num_vertices);

                #ifdef MD2_DEBUG
                prTrace(prLogLevel::LogError, "Name: %s\n", &m_frames[i].name);
                #endif
            }

            // Read OpenGL commands
            ifs.seekg(header.offset_glcmds, std::ios::beg);
            ifs.read(reinterpret_cast<char*>(m_glcmds), sizeof(int) * header.num_glcmds);


            // Store some of the header data.
            m_numTriangles  = header.num_tris;
            m_skinWidth     = header.skinWidth;
            m_skinHeight    = header.skinHeight;
            m_numFrames     = header.num_frames;


            // Done.
            mLoaded = true;
            ifs.close();


            // Create the animation data
            if (m_numFrames > 0)
            {
                mpAnimation = new prAnimation_MD2(m_frames, m_numFrames);
            }
        }
        else
        {
            PRWARN("Failed to load mesh '%s'.", filename);
        }
    }

    return true;
}