Click here to Skip to main content
15,895,836 members
Articles / Multimedia / OpenGL

Modified version of lib3ds reader of .3ds format

Rate me:
Please Sign up or sign in to vote.
4.78/5 (15 votes)
4 Oct 2012CPOL3 min read 42.9K   2.9K   23  
In this article I present version of lib3ds reader for C++ language
#include "Lib3ds_io.h"

typedef union
{
    quint32 dword_value;
    float float_value;
} Lib3dsDwordFloat;

Lib3ds_io::Lib3ds_io()
{
}

qint64 Lib3ds_io::lib3ds_io_seek(qint64 offset, Lib3dsIoSeek origin)
{
    return fileio_seek_func(offset, origin);
}

qint64 Lib3ds_io::lib3ds_io_tell()
{
    return fileio_tell_func();
}

qint64 Lib3ds_io::lib3ds_io_read(void *buffer, qint64 size)
{
    return fileio_read_func(buffer, size);
}

qint64 Lib3ds_io::lib3ds_io_write(const void *buffer, qint64 size)
{
    return fileio_write_func(buffer, size);
}

/*!
 * Read a byte from a file stream.
 */
quint8 Lib3ds_io::lib3ds_io_read_byte()
{
    quint8 b;

    lib3ds_io_read(&b, 1);

    return(b);
}

/**
 * Read a word from a file stream in little endian format.
 */
quint16 Lib3ds_io::lib3ds_io_read_word()
{
    quint8 b[2];
    quint16 w;

    lib3ds_io_read(b, 2);
    w = ((quint16)b[1] << 8) |
        ((quint16)b[0]);
    return(w);
}

/*!
 * Read a dword from file a stream in little endian format.
 */
quint32 Lib3ds_io::lib3ds_io_read_dword()
{
    quint8 b[4];
    quint32 d;

    lib3ds_io_read(b, 4);
    d = ((quint32)b[3] << 24) |
        ((quint32)b[2] << 16) |
        ((quint32)b[1] << 8) |
        ((quint32)b[0]);
    return(d);
}

/*!
 * Read a signed byte from a file stream.
 */
qint8 Lib3ds_io::lib3ds_io_read_intb()
{
    qint8 b;

    lib3ds_io_read(&b, 1);
    return(b);
}

/*!
 * Read a signed word from a file stream in little endian format.
 */
qint16 Lib3ds_io::lib3ds_io_read_intw()
{
    quint8 b[2];
    quint16 w;

    lib3ds_io_read(b, 2);
    w = ((quint16)b[1] << 8) |
        ((quint16)b[0]);
    return((qint16)w);
}

/*!
 * Read a signed dword a from file stream in little endian format.
 */
qint32 Lib3ds_io::lib3ds_io_read_intd()
{
    quint8 b[4];
    quint32 d;

    lib3ds_io_read(b, 4);
    d = ((quint32)b[3] << 24) |
        ((quint32)b[2] << 16) |
        ((quint32)b[1] << 8) |
        ((quint32)b[0]);
    return((qint32)d);
}

/*!
 * Read a float from a file stream in little endian format.
 */
float Lib3ds_io::lib3ds_io_read_float()
{
    quint8 b[4];
    Lib3dsDwordFloat d;

    lib3ds_io_read(b, 4);
    d.dword_value = ((quint32)b[3] << 24) |
                    ((quint32)b[2] << 16) |
                    ((quint32)b[1] << 8) |
                    ((quint32)b[0]);
    return d.float_value;
}

/*!
 * Read a vector from a file stream in little endian format.
 *
 * \param io IO input handle.
 * \param v  The vector to store the data.
 */
void Lib3ds_io::lib3ds_io_read_vector(float v[3])
{
    v[0] = lib3ds_io_read_float();
    v[1] = lib3ds_io_read_float();
    v[2] = lib3ds_io_read_float();
}

void Lib3ds_io::lib3ds_io_read_rgb(float rgb[3])
{
    rgb[0] = lib3ds_io_read_float();
    rgb[1] = lib3ds_io_read_float();
    rgb[2] = lib3ds_io_read_float();
}

/*!
 * Read a zero-terminated string from a file stream.
 *
 * \param io      IO input handle.
 * \param s       The buffer to store the read string.
 * \param buflen  Buffer length.
 *
 * \return        True on success, False otherwise.
 */
bool Lib3ds_io::lib3ds_io_read_string(QString &s, int buflen)
{
    char c;
    int k = 0;

    s.clear();

    for (;;)
    {
        if (lib3ds_io_read(&c, 1) != 1)
        {
            return false;
        }

        if (!c)
        {
            break;
        }

        s += c;

        ++k;

        if (k >= buflen)
        {
            return false;
        }
    }

    return true;
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer
Australia Australia
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions