From bc89bd0592941dc29be1542a2a413ef91dd8632a Mon Sep 17 00:00:00 2001 From: Daniel Evans Date: Mon, 1 Jul 2013 22:16:47 +0100 Subject: [PATCH] UV data fixed --- framework/LoaderDFF.cpp | 37 ++++++++++-- framework/TextureLoader.cpp | 115 +++++++++++++++++++++++++----------- viewer/main.cpp | 2 +- 3 files changed, 115 insertions(+), 39 deletions(-) diff --git a/framework/LoaderDFF.cpp b/framework/LoaderDFF.cpp index 15550924..dcf26c6f 100644 --- a/framework/LoaderDFF.cpp +++ b/framework/LoaderDFF.cpp @@ -41,7 +41,8 @@ std::unique_ptr LoaderDFF::loadFromMemory(char *data) } /** TEX COORDS **/ - if (geometry.flags & RW::BSGeometry::TexCoords1 || geometry.flags & RW::BSGeometry::TexCoords2) { + if ((geometry.flags & RW::BSGeometry::TexCoords1) == RW::BSGeometry::TexCoords1 || + (geometry.flags & RW::BSGeometry::TexCoords2) == RW::BSGeometry::TexCoords1) { for (size_t v = 0; v < geometry.numverts; ++v) { geometryStruct.texcoords.push_back(readStructure(data, dataI)); } @@ -110,14 +111,40 @@ std::unique_ptr LoaderDFF::loadFromMemory(char *data) glGenBuffers(1, &geometryStruct.VBO); glGenBuffers(1, &geometryStruct.EBO); + size_t buffsize = (geometryStruct.vertices.size() * sizeof(float) * 3) + + (geometryStruct.texcoords.size() * sizeof(float) * 2) + + (geometryStruct.normals.size() * sizeof(float) * 3); + // Vertices glBindBuffer(GL_ARRAY_BUFFER, geometryStruct.VBO); - glBufferData( + glBufferData(GL_ARRAY_BUFFER, buffsize, NULL, GL_STATIC_DRAW); + + glBufferSubData( GL_ARRAY_BUFFER, - geometryStruct.vertices.size() * 3 * sizeof(float), - &geometryStruct.vertices[0], - GL_STATIC_DRAW + 0, + (geometryStruct.vertices.size() * sizeof(float) * 3), + &geometryStruct.vertices[0] ); + + if(geometryStruct.texcoords.size() > 0) + { + glBufferSubData( + GL_ARRAY_BUFFER, + (geometryStruct.vertices.size() * sizeof(float) * 3), + (geometryStruct.texcoords.size() * sizeof(float) * 2), + &geometryStruct.texcoords[0] + ); + } + + if(geometryStruct.normals.size() > 0 ) + { + glBufferSubData( + GL_ARRAY_BUFFER, + (geometryStruct.vertices.size() * sizeof(float) * 3) + (geometryStruct.texcoords.size() * sizeof(float) * 2), + geometryStruct.normals.size() * 3 * sizeof(float), + &geometryStruct.normals[0] + ); + } // Elements uint16_t indicies[geometryStruct.triangles.size() * 3]; diff --git a/framework/TextureLoader.cpp b/framework/TextureLoader.cpp index 35790ba1..bd4aa8b7 100644 --- a/framework/TextureLoader.cpp +++ b/framework/TextureLoader.cpp @@ -33,45 +33,94 @@ bool TextureLoader::loadFromMemory(char *data) continue; auto texNative = rootSection.readStructure(); - if ( ! (texNative.rasterformat & RW::BSTextureNative::FORMAT_EXT_PAL8)) + + if(texNative.platform != 8) + { + std::cerr << "Unsupported texture platform " << std::dec << texNative.platform << std::endl; continue; - - auto palette = rootSection.readSubStructure(sizeof(RW::BSTextureNative)); - auto coldata = rootSection.raw() + sizeof(RW::BSTextureNative) + sizeof(RW::BSPaletteData); - - uint8_t fullColor[texNative.width * texNative.height * 4]; - - for (size_t y = 0; y < texNative.height; y++) { - for (size_t x = 0; x < texNative.width; x++) { - size_t texI = ((y * texNative.width) + x) * 4; - size_t palI = 4 * static_cast( - coldata[(y * texNative.width) + x] - ); - - fullColor[texI+0] = palette.palette[palI+2]; - fullColor[texI+1] = palette.palette[palI+1]; - fullColor[texI+2] = palette.palette[palI+0]; - fullColor[texI+3] = palette.palette[palI+3]; - } } + + GLuint texture = 0; + + if((texNative.rasterformat & RW::BSTextureNative::FORMAT_EXT_PAL8) == RW::BSTextureNative::FORMAT_EXT_PAL8) + { + auto palette = rootSection.readSubStructure(sizeof(RW::BSTextureNative)); + auto coldata = rootSection.raw() + sizeof(RW::BSTextureNative) + sizeof(RW::BSPaletteData); - GLuint texture; - glGenTextures(1, &texture); - glBindTexture(GL_TEXTURE_2D, texture); - // todo: not completely ignore everything the TXD says. - glTexImage2D( - GL_TEXTURE_2D, 0, GL_RGBA, - texNative.width, texNative.height, 0, - GL_BGRA, GL_UNSIGNED_BYTE, fullColor - ); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR_MIPMAP_LINEAR); + uint8_t fullColor[texNative.width * texNative.height * 4]; - glGenerateMipmap(GL_TEXTURE_2D); + for (size_t y = 0; y < texNative.height; y++) { + for (size_t x = 0; x < texNative.width; x++) { + size_t texI = ((y * texNative.width) + x) * 4; + size_t palI = 4 * static_cast( + coldata[(y * texNative.width) + x] + ); - std::string name = std::string(texNative.diffuseName); + fullColor[texI+0] = palette.palette[palI+2]; + fullColor[texI+1] = palette.palette[palI+1]; + fullColor[texI+2] = palette.palette[palI+0]; + fullColor[texI+3] = palette.palette[palI+3]; + } + } + + glGenTextures(1, &texture); + glBindTexture(GL_TEXTURE_2D, texture); + glTexImage2D( + GL_TEXTURE_2D, 0, GL_RGBA, + texNative.width, texNative.height, 0, + GL_BGRA, GL_UNSIGNED_BYTE, fullColor + ); + } + else if( + texNative.rasterformat == RW::BSTextureNative::FORMAT_1555 || + texNative.rasterformat == RW::BSTextureNative::FORMAT_8888 || + texNative.rasterformat == RW::BSTextureNative::FORMAT_888 + ) + { + auto coldata = rootSection.raw() + sizeof(RW::BSTextureNative) + sizeof(uint32_t); + + GLenum type, format; + switch(texNative.rasterformat) + { + case RW::BSTextureNative::FORMAT_1555: + format = GL_RGBA; + type = GL_UNSIGNED_SHORT_1_5_5_5_REV; + break; + case RW::BSTextureNative::FORMAT_8888: + format = GL_BGRA; + type = GL_UNSIGNED_BYTE; + break; + case RW::BSTextureNative::FORMAT_888: + format = GL_BGR; + type = GL_UNSIGNED_BYTE; + break; + } + + glGenTextures(1, &texture); + glBindTexture(GL_TEXTURE_2D, texture); + glTexImage2D( + GL_TEXTURE_2D, 0, GL_RGBA, + texNative.width, texNative.height, 0, + format, type, coldata + ); + } + else + { + std::cerr << "Unsuported raster format " << std::hex << texNative.rasterformat << std::endl; + } + + if(texture != 0) + { + // todo: not completely ignore everything the TXD says. + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR_MIPMAP_LINEAR); - textures[name] = texture; + glGenerateMipmap(GL_TEXTURE_2D); + + std::string name = std::string(texNative.diffuseName); + + textures[name] = texture; + } // std::cout << "Loaded texture '" << name << "'" << std::endl; } diff --git a/viewer/main.cpp b/viewer/main.cpp index bc99a732..527269ef 100644 --- a/viewer/main.cpp +++ b/viewer/main.cpp @@ -217,7 +217,7 @@ void render() glBindBuffer(GL_ARRAY_BUFFER, model->geometries[g].VBO); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, model->geometries[g].EBO); glVertexAttribPointer(posAttrib, 3, GL_FLOAT, GL_FALSE, 0, 0); - glVertexAttribPointer(texAttrib, 2, GL_FLOAT, GL_FALSE, 0, 0); + glVertexAttribPointer(texAttrib, 2, GL_FLOAT, GL_FALSE, 0, (void*)(model->geometries[g].vertices.size() * sizeof(float) * 3)); glEnableVertexAttribArray(posAttrib); glEnableVertexAttribArray(texAttrib);