From dc70ea9ff7b4573f7af716071e88a93fdcd69ea5 Mon Sep 17 00:00:00 2001 From: Gunnar Birke Date: Sat, 30 May 2020 18:12:39 +0200 Subject: [PATCH] WINTERMUTE: Render mesh without indices usage --- engines/wintermute/base/gfx/opengl/mesh.cpp | 57 ++++++++++++++++++++- engines/wintermute/base/gfx/opengl/mesh.h | 1 + 2 files changed, 56 insertions(+), 2 deletions(-) diff --git a/engines/wintermute/base/gfx/opengl/mesh.cpp b/engines/wintermute/base/gfx/opengl/mesh.cpp index cc8ec31648d..01d462bc716 100644 --- a/engines/wintermute/base/gfx/opengl/mesh.cpp +++ b/engines/wintermute/base/gfx/opengl/mesh.cpp @@ -22,6 +22,7 @@ #include "mesh.h" #include "loader3ds.h" +#include "common/file.h" Wintermute::Mesh::Mesh() : _vertexData(nullptr), _vertexCount(0), @@ -136,10 +137,62 @@ void Wintermute::Mesh::render() { glBindBuffer(GL_ARRAY_BUFFER, _vertexBuffer); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, _indexBuffer); - glInterleavedArrays(GL_C4UB_V3F, 0, 0); + uint32 *vertices = reinterpret_cast(glMapBuffer(GL_ARRAY_BUFFER, GL_READ_ONLY)); + uint16 *indices = reinterpret_cast(glMapBuffer(GL_ELEMENT_ARRAY_BUFFER, GL_READ_ONLY)); - glDrawElements(GL_TRIANGLES, _indexCount, GL_UNSIGNED_SHORT, 0); + uint32 *tempVertices = new uint32[4 * _indexCount]; + int pos = 0; + + for (uint16 *index = indices; index < indices + _indexCount; ++index) { + *reinterpret_cast(tempVertices + 4 * pos) = *reinterpret_cast(vertices + 4 * *index); + *reinterpret_cast(tempVertices + 4 * pos + 1) = *reinterpret_cast(vertices + 4 * *index + 1); + *reinterpret_cast(tempVertices + 4 * pos + 2) = *reinterpret_cast(vertices + 4 * *index + 2); + *reinterpret_cast(tempVertices + 4 * pos + 3) = *reinterpret_cast(vertices + 4 * *index + 3); + ++pos; + } + + glUnmapBuffer(GL_ELEMENT_ARRAY_BUFFER); + glUnmapBuffer(GL_ARRAY_BUFFER); + glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0); + glBindBuffer(GL_ARRAY_BUFFER, 0); + +// glBindBuffer(GL_ARRAY_BUFFER, _vertexBuffer); +// glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, _indexBuffer); + glInterleavedArrays(GL_C4UB_V3F, 0, tempVertices); +// glInterleavedArrays(GL_C4UB_V3F, 0, 0); + + glDrawArrays(GL_TRIANGLES, 0, _indexCount); +// glDrawElements(GL_TRIANGLES, _indexCount, GL_UNSIGNED_SHORT, 0); + +// glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0); +// glBindBuffer(GL_ARRAY_BUFFER, 0); + + delete[] tempVertices; +} + +void Wintermute::Mesh::dumpVertexCoordinates(const char *filename) { + Common::DumpFile dump; + dump.open(filename); + + glBindBuffer(GL_ARRAY_BUFFER, _vertexBuffer); + glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, _indexBuffer); + uint32 *vertices = reinterpret_cast(glMapBuffer(GL_ARRAY_BUFFER, GL_READ_ONLY)); + uint16 *indices = reinterpret_cast(glMapBuffer(GL_ELEMENT_ARRAY_BUFFER, GL_READ_ONLY)); + + for (uint16 *index = indices; index < indices + _indexCount; ++index) { + float x = *reinterpret_cast(vertices + 4 * *index + 1); + float y = *reinterpret_cast(vertices + 4 * *index + 2); + float z = *reinterpret_cast(vertices + 4 * *index + 3); + + dump.writeString(Common::String::format("%u ", *index)); + dump.writeString(Common::String::format("%g ", x)); + dump.writeString(Common::String::format("%g ", y)); + dump.writeString(Common::String::format("%g\n", z)); + } + + glUnmapBuffer(GL_ELEMENT_ARRAY_BUFFER); + glUnmapBuffer(GL_ARRAY_BUFFER); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0); glBindBuffer(GL_ARRAY_BUFFER, 0); } diff --git a/engines/wintermute/base/gfx/opengl/mesh.h b/engines/wintermute/base/gfx/opengl/mesh.h index c18c0bf9ad0..8d078d9a1af 100644 --- a/engines/wintermute/base/gfx/opengl/mesh.h +++ b/engines/wintermute/base/gfx/opengl/mesh.h @@ -36,6 +36,7 @@ public: void fillVertexBuffer(uint32 color); bool loadFrom3DS(byte **buffer); void render(); + void dumpVertexCoordinates(const char *filename); private: byte *_vertexData;