From 521656dc1ed8b7cef5a320b8af491183e65ba0aa Mon Sep 17 00:00:00 2001 From: Angelo Papenhoff Date: Wed, 24 Dec 2014 11:38:25 +0100 Subject: [PATCH] Added some OpenGL specific code. --- Makefile | 2 +- src/clump.cpp | 6 ++++ src/ogl.cpp | 81 ++++++++++++++++++++++++++++++++++++++++--------- src/plugins.cpp | 8 ++--- src/rwogl.h | 23 +++++++++----- 5 files changed, 93 insertions(+), 27 deletions(-) diff --git a/Makefile b/Makefile index 714543e..9b57df9 100644 --- a/Makefile +++ b/Makefile @@ -4,7 +4,7 @@ SRC := $(patsubst %.cpp,$(SRCDIR)/%.cpp, rwbase.cpp clump.cpp\ geometry.cpp plugins.cpp ps2.cpp\ ogl.cpp) OBJ := $(patsubst $(SRCDIR)/%.cpp,$(BUILDDIR)/%.o,$(SRC)) -OBJ := $(patsubst $(SRCDIR)/%.cpp,$(BUILDDIR)/%.d,$(SRC)) +DEP := $(patsubst $(SRCDIR)/%.cpp,$(BUILDDIR)/%.d,$(SRC)) CFLAGS=-Wall -Wextra -g #-Wno-parentheses #-Wconversion librw.a: $(OBJ) diff --git a/src/clump.cpp b/src/clump.cpp index 99bacfd..4475db6 100644 --- a/src/clump.cpp +++ b/src/clump.cpp @@ -19,6 +19,12 @@ Frame::Frame(void) this->child = NULL; this->next = NULL; this->root = NULL; + for(int i = 0; i < 16; i++) + this->matrix[i] = 0.0f; + this->matrix[0] = 1.0f; + this->matrix[5] = 1.0f; + this->matrix[10] = 1.0f; + this->matrix[15] = 1.0f; constructPlugins(); } diff --git a/src/ogl.cpp b/src/ogl.cpp index ff015b6..df7f140 100644 --- a/src/ogl.cpp +++ b/src/ogl.cpp @@ -11,17 +11,20 @@ #include "rwobjects.h" #include "rwogl.h" +#include + using namespace std; namespace Rw { +namespace Gl { void* -DestroyNativeDataOGL(void *object, int32, int32) +DestroyNativeData(void *object, int32, int32) { Geometry *geometry = (Geometry*)object; assert(geometry->instData->platform == PLATFORM_OGL); - OGLInstanceDataHeader *header = - (OGLInstanceDataHeader*)geometry->instData; + InstanceDataHeader *header = + (InstanceDataHeader*)geometry->instData; delete[] header->attribs; delete[] header->data; delete header; @@ -29,16 +32,18 @@ DestroyNativeDataOGL(void *object, int32, int32) } void -ReadNativeDataOGL(istream &stream, int32, void *object, int32, int32) +ReadNativeData(istream &stream, int32, void *object, int32, int32) { Geometry *geometry = (Geometry*)object; - OGLInstanceDataHeader *header = new OGLInstanceDataHeader; + InstanceDataHeader *header = new InstanceDataHeader; geometry->instData = header; header->platform = PLATFORM_OGL; + header->vbo = 0; + header->ibo = 0; header->numAttribs = readUInt32(stream); - header->attribs = new OGLAttrib[header->numAttribs]; + header->attribs = new AttribDesc[header->numAttribs]; stream.read((char*)header->attribs, - header->numAttribs*sizeof(OGLAttrib)); + header->numAttribs*sizeof(AttribDesc)); // Any better way to find out the size? (header length can be wrong) header->dataSize = header->attribs[0].stride*geometry->numVertices; header->data = new uint8[header->dataSize]; @@ -46,26 +51,72 @@ ReadNativeDataOGL(istream &stream, int32, void *object, int32, int32) } void -WriteNativeDataOGL(ostream &stream, int32 len, void *object, int32, int32) +WriteNativeData(ostream &stream, int32 len, void *object, int32, int32) { Geometry *geometry = (Geometry*)object; assert(geometry->instData->platform == PLATFORM_OGL); - OGLInstanceDataHeader *header = - (OGLInstanceDataHeader*)geometry->instData; + InstanceDataHeader *header = + (InstanceDataHeader*)geometry->instData; writeUInt32(header->numAttribs, stream); stream.write((char*)header->attribs, - header->numAttribs*sizeof(OGLAttrib)); + header->numAttribs*sizeof(AttribDesc)); stream.write((char*)header->data, header->dataSize); } int32 -GetSizeNativeDataOGL(void *object, int32, int32) +GetSizeNativeData(void *object, int32, int32) { Geometry *geometry = (Geometry*)object; assert(geometry->instData->platform == PLATFORM_OGL); - OGLInstanceDataHeader *header = - (OGLInstanceDataHeader*)geometry->instData; - return 4 + header->numAttribs*sizeof(OGLAttrib) + header->dataSize; + InstanceDataHeader *header = + (InstanceDataHeader*)geometry->instData; + return 4 + header->numAttribs*sizeof(AttribDesc) + header->dataSize; +} + + +void +UploadGeo(Geometry *geo) +{ + using namespace Rw; + InstanceDataHeader *inst = (InstanceDataHeader*)geo->instData; + MeshHeader *meshHeader = geo->meshHeader; + + glGenBuffers(1, &inst->vbo); + glBindBuffer(GL_ARRAY_BUFFER, inst->vbo); + glBufferData(GL_ARRAY_BUFFER, inst->dataSize, + inst->data, GL_STATIC_DRAW); + + glGenBuffers(1, &inst->ibo); + glBindBuffer(GL_ARRAY_BUFFER, inst->ibo); + glBufferData(GL_ARRAY_BUFFER, meshHeader->totalIndices*2, + 0, GL_STATIC_DRAW); + GLintptr offset = 0; + for(uint32 i = 0; i < meshHeader->numMeshes; i++){ + Mesh *mesh = &meshHeader->mesh[i]; + glBufferSubData(GL_ARRAY_BUFFER, offset, mesh->numIndices*2, + mesh->indices); + offset += mesh->numIndices*2; + } + glBindBuffer(GL_ARRAY_BUFFER, 0); +} + +void +SetAttribPointers(InstanceDataHeader *inst) +{ + using namespace Rw; + static GLenum attribType[] = { + GL_FLOAT, + GL_BYTE, GL_UNSIGNED_BYTE, + GL_SHORT, GL_UNSIGNED_SHORT + }; + for(int32 i = 0; i < inst->numAttribs; i++){ + AttribDesc *a = &inst->attribs[i]; + glEnableVertexAttribArray(a->index); + glVertexAttribPointer(a->index, a->size, attribType[a->type], + a->normalized, a->stride, + (void*)(uint64)a->offset); + } } } +} diff --git a/src/plugins.cpp b/src/plugins.cpp index 5385f5d..b635931 100644 --- a/src/plugins.cpp +++ b/src/plugins.cpp @@ -199,7 +199,7 @@ destroyNativeData(void *object, int32 offset, int32 size) if(geometry->instData->platform == PLATFORM_PS2) return DestroyNativeDataPS2(object, offset, size); if(geometry->instData->platform == PLATFORM_OGL) - return DestroyNativeDataOGL(object, offset, size); + return Gl::DestroyNativeData(object, offset, size); return object; } @@ -224,7 +224,7 @@ readNativeData(istream &stream, int32 len, void *object, int32 o, int32 s) stream.seekg(len, ios::cur); }else{ stream.seekg(-12, ios::cur); - ReadNativeDataOGL(stream, len, object, o, s); + Gl::ReadNativeData(stream, len, object, o, s); } } @@ -237,7 +237,7 @@ writeNativeData(ostream &stream, int32 len, void *object, int32 o, int32 s) if(geometry->instData->platform == PLATFORM_PS2) WriteNativeDataPS2(stream, len, object, o, s); else if(geometry->instData->platform == PLATFORM_OGL) - WriteNativeDataOGL(stream, len, object, o, s); + Gl::WriteNativeData(stream, len, object, o, s); } static int32 @@ -251,7 +251,7 @@ getSizeNativeData(void *object, int32 offset, int32 size) else if(geometry->instData->platform == PLATFORM_XBOX) return -1; else if(geometry->instData->platform == PLATFORM_OGL) - return GetSizeNativeDataOGL(object, offset, size); + return Gl::GetSizeNativeData(object, offset, size); return -1; } diff --git a/src/rwogl.h b/src/rwogl.h index 60c8e3c..af95adc 100644 --- a/src/rwogl.h +++ b/src/rwogl.h @@ -1,6 +1,7 @@ namespace Rw { +namespace Gl { -struct OGLAttrib +struct AttribDesc { // arguments to glVertexAttribPointer (should use OpenGL types here) // Vertex = 0, TexCoord, Normal, Color, Weight, Bone Index @@ -13,17 +14,25 @@ struct OGLAttrib uint32 offset; }; -struct OGLInstanceDataHeader : InstanceDataHeader +struct InstanceDataHeader : Rw::InstanceDataHeader { int32 numAttribs; - OGLAttrib *attribs; + AttribDesc *attribs; uint32 dataSize; uint8 *data; + + // needed for rendering + uint32 vbo; + uint32 ibo; }; -void *DestroyNativeDataOGL(void *object, int32, int32); -void ReadNativeDataOGL(std::istream &stream, int32 len, void *object, int32, int32); -void WriteNativeDataOGL(std::ostream &stream, int32 len, void *object, int32, int32); -int32 GetSizeNativeDataOGL(void *object, int32, int32); +void *DestroyNativeData(void *object, int32, int32); +void ReadNativeData(std::istream &stream, int32 len, void *object, int32, int32); +void WriteNativeData(std::ostream &stream, int32 len, void *object, int32, int32); +int32 GetSizeNativeData(void *object, int32, int32); + +void UploadGeo(Geometry *geo); +void SetAttribPointers(InstanceDataHeader *inst); } +}