diff --git a/aufgabe/proposal.pdf b/aufgabe/proposal.pdf index 25b548f..f8c478a 100644 Binary files a/aufgabe/proposal.pdf and b/aufgabe/proposal.pdf differ diff --git a/media/pawn.3ds b/media/pawn.3ds index d036c8e..78f3e90 100644 Binary files a/media/pawn.3ds and b/media/pawn.3ds differ diff --git a/media/pawn.blend b/media/pawn.blend index 434c4f9..79d882b 100644 Binary files a/media/pawn.blend and b/media/pawn.blend differ diff --git a/src/field3d.cpp b/src/field3d.cpp index d447ab1..1da54bd 100644 --- a/src/field3d.cpp +++ b/src/field3d.cpp @@ -77,6 +77,9 @@ namespace toruschess { void Field3D::paintGL() { glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); +// m_lib->paint_pawn(); +// return; + glBindTexture(GL_TEXTURE_2D, m_textureID); glEnable(GL_TEXTURE_2D); { diff --git a/src/media.qrc b/src/media.qrc index 1021410..0c94b0b 100644 --- a/src/media.qrc +++ b/src/media.qrc @@ -12,5 +12,6 @@ ../media/Chess_rdt45.svg ../media/Chess_qdt45.svg ../media/Chess_kdt45.svg + ../media/pawn.3ds diff --git a/src/piecelibrary.cpp b/src/piecelibrary.cpp index 35d98fd..65e0f1c 100644 --- a/src/piecelibrary.cpp +++ b/src/piecelibrary.cpp @@ -20,15 +20,125 @@ #include "piecelibrary.h" #include +#include + +#include +#include + +#include namespace toruschess { +typedef Lib3dsBool (*Lib3dsIoErrorFunc)(void *self); +typedef long (*Lib3dsIoSeekFunc)(void *self, long offset, Lib3dsIoSeek origin); +typedef long (*Lib3dsIoTellFunc)(void *self); +typedef size_t (*Lib3dsIoReadFunc)(void *self, void *buffer, size_t size); +typedef size_t (*Lib3dsIoWriteFunc)(void *self, const void *buffer, size_t size); + + Lib3dsBool io3ds_error(void *self); + long io3ds_seek(void *self, long offset, Lib3dsIoSeek origin); + long io3ds_tell(void *self); + size_t io3ds_read(void *self, void *buffer, size_t size); + size_t io3ds_write(void *self, const void *buffer, size_t size); + + class IO3ds { + public: + IO3ds(const QByteArray &data) : m_data(data), m_offset(0) { + m_io = lib3ds_io_new(this, io3ds_error, io3ds_seek, io3ds_tell, io3ds_read, io3ds_write); + } + ~IO3ds() { + lib3ds_io_free(m_io); + } + + long seek(long offset, Lib3dsIoSeek origin) { + switch (origin) { + case LIB3DS_SEEK_SET: + m_offset = offset; + break; + case LIB3DS_SEEK_CUR: + m_offset += offset; + break; + case LIB3DS_SEEK_END: + m_offset = m_data.length() + offset; + break; + } + return 0; + } + + long tell() { return m_offset; } + size_t read(void *buffer, size_t size) { + qDebug("read (size %i): data-len = %i, offset = %i", (int) size, m_data.length(), (int) m_offset); + if (m_offset >= m_data.length()) return 0; + size_t r = qMin(size, (size_t) m_data.length() - m_offset); + memcpy(buffer, m_data.constData() + m_offset, r); + m_offset += r; + qDebug("read: done with %i", (int) r); + return r; + } + size_t write(const void *, size_t ) { + return 0; + } + Lib3dsBool error() { + qDebug("3ds error"); + return true; + } + + Lib3dsIo* io() { return m_io; } + + private: + Lib3dsIo *m_io; + QByteArray m_data; + long m_offset; + }; + + Lib3dsBool io3ds_error(void *self) { + IO3ds *s = (IO3ds*) self; + return s->error(); + } + long io3ds_seek(void *self, long offset, Lib3dsIoSeek origin) { + IO3ds *s = (IO3ds*) self; + return s->seek(offset, origin); + } + long io3ds_tell(void *self) { + IO3ds *s = (IO3ds*) self; + return s->tell(); + } + size_t io3ds_read(void *self, void *buffer, size_t size) { + IO3ds *s = (IO3ds*) self; + return s->read(buffer, size); + } + size_t io3ds_write(void *self, const void *buffer, size_t size) { + IO3ds *s = (IO3ds*) self; + return s->write(buffer, size); + } + + + Lib3dsFile *read_3ds(const QByteArray &data) { + return lib3ds_file_load("media/pawn.3ds"); + IO3ds io(data); + Lib3dsFile *f = lib3ds_file_new(); + if (!lib3ds_file_read(f, io.io())) { + qDebug("data size: %i", (int) data.size()); + lib3ds_file_free(f); + return NULL; + } + return f; + } + + QByteArray loadResource(const QString &filename) { + QResource res(filename); + qDebug("res size: %i", (int) res.size()); + return QByteArray((const char*) res.data(), (int) res.size()); + } + PieceLibrary::PieceLibrary(QObject *parent) : QObject(parent), m_pieces(0), m_buffers(0) { + m_pawn = read_3ds(loadResource(":/media/pawn.3ds")); setSize(45, 45); } PieceLibrary::~PieceLibrary() { + lib3ds_file_free(m_pawn); delete [] m_pieces; delete [] m_buffers; } @@ -68,5 +178,15 @@ namespace toruschess { if (place <= 0 || place > 12) return; pt.drawImage(rect, *m_buffers[place-1]); } - + + + void PieceLibrary::paint_pawn() { + glEnableClientState(GL_VERTEX_ARRAY); + glVertexPointer(3,GL_FLOAT,0,m_pawn->meshes->pointL); + for (unsigned int i = 0; i < m_pawn->meshes->faces; i++) { + glDrawElements(GL_TRIANGLES, 3, GL_UNSIGNED_SHORT, m_pawn->meshes->faceL[i].points); + } + glDisableClientState(GL_VERTEX_ARRAY); + } + } diff --git a/src/piecelibrary.h b/src/piecelibrary.h index c709201..b78e535 100644 --- a/src/piecelibrary.h +++ b/src/piecelibrary.h @@ -22,6 +22,8 @@ #include +#include + /** @author Stefan Bühler */ @@ -40,10 +42,14 @@ namespace toruschess { void paint(QPainter &pt, int place) const; void paint(QPainter &pt, int place, const QRect &rect) const; + void paint_pawn(); + private: QSvgRenderer **m_pieces; QImage **m_buffers; int m_width, m_height; + + Lib3dsFile *m_pawn; }; } diff --git a/src/src.pro b/src/src.pro index 9c9b466..be8315d 100644 --- a/src/src.pro +++ b/src/src.pro @@ -23,3 +23,5 @@ toruschess.h \ field3d.h CONFIG -= release +LIBS += -l3ds +