From f395a5d0319ab334fb6af4fe25fda4de16db94e3 Mon Sep 17 00:00:00 2001 From: = <=> Date: Tue, 16 Sep 2014 14:22:09 -0700 Subject: [PATCH] split Wavefront .obj to graphics conversion in a reusable file (Wavefront2GLInstanceGraphicsShape.*) --- Demos3/AllBullet2Demos/CMakeLists.txt | 1 + Demos3/AllBullet2Demos/premake4.lua | 1 + Demos3/ImportObjDemo/ImportObjSetup.cpp | 100 +--------------- .../Wavefront2GLInstanceGraphicsShape.cpp | 109 ++++++++++++++++++ .../Wavefront2GLInstanceGraphicsShape.h | 9 ++ 5 files changed, 123 insertions(+), 97 deletions(-) create mode 100644 Demos3/ImportObjDemo/Wavefront2GLInstanceGraphicsShape.cpp create mode 100644 Demos3/ImportObjDemo/Wavefront2GLInstanceGraphicsShape.h diff --git a/Demos3/AllBullet2Demos/CMakeLists.txt b/Demos3/AllBullet2Demos/CMakeLists.txt index e955147d2..692cdd728 100644 --- a/Demos3/AllBullet2Demos/CMakeLists.txt +++ b/Demos3/AllBullet2Demos/CMakeLists.txt @@ -42,6 +42,7 @@ SET(App_AllBullet2Demos_SRCS ../ImportURDFDemo/ImportURDFSetup.cpp ../ImportURDFDemo/ImportURDFSetup.h ../ImportObjDemo/ImportObjSetup.cpp + ../ImportObjDemo/Wavefront2GLInstanceGraphicsShape.cpp ../ImportSTLDemo/ImportSTLSetup.cpp ../Wavefront/tiny_obj_loader.cpp ../Wavefront/tiny_obj_loader.h diff --git a/Demos3/AllBullet2Demos/premake4.lua b/Demos3/AllBullet2Demos/premake4.lua index f9003441f..7d06e0e41 100644 --- a/Demos3/AllBullet2Demos/premake4.lua +++ b/Demos3/AllBullet2Demos/premake4.lua @@ -60,6 +60,7 @@ "../bullet2/ConstraintDemo/ConstraintPhysicsSetup.h", "../ImportURDFDemo/ImportURDFSetup.cpp", "../ImportObjDemo/ImportObjSetup.cpp", + "../ImportObjDemo/Wavefront2GLInstanceGraphicsShape.cpp", "../ImportSTLDemo/ImportSTLSetup.cpp", "../Wavefront/tiny_obj_loader.cpp", "../Wavefront/tiny_obj_loader.h", diff --git a/Demos3/ImportObjDemo/ImportObjSetup.cpp b/Demos3/ImportObjDemo/ImportObjSetup.cpp index fb65a7b08..bc300a6fe 100644 --- a/Demos3/ImportObjDemo/ImportObjSetup.cpp +++ b/Demos3/ImportObjDemo/ImportObjSetup.cpp @@ -5,6 +5,7 @@ #include "OpenGLWindow/GLInstanceGraphicsShape.h" #include "btBulletDynamicsCommon.h" #include "OpenGLWindow/SimpleOpenGL3App.h" +#include "Wavefront2GLInstanceGraphicsShape.h" ImportObjDemo::ImportObjDemo(SimpleOpenGL3App* app) :m_app(app) @@ -19,101 +20,6 @@ ImportObjDemo::~ImportObjDemo() -static GLInstanceGraphicsShape* gCreateGraphicsShapeFromWavefrontObj(std::vector& shapes) -{ - - b3AlignedObjectArray* vertices = new b3AlignedObjectArray; - { - // int numVertices = obj->vertexCount; - // int numIndices = 0; - b3AlignedObjectArray* indicesPtr = new b3AlignedObjectArray; - - for (int s=0;s<(int)shapes.size();s++) - { - tinyobj::shape_t& shape = shapes[s]; - int faceCount = shape.mesh.indices.size(); - - - for (int f=0;fsize(); - - - - - GLInstanceVertex vtx0; - vtx0.xyzw[0] = shape.mesh.positions[shape.mesh.indices[f]*3+0]; - vtx0.xyzw[1] = shape.mesh.positions[shape.mesh.indices[f]*3+1]; - vtx0.xyzw[2] = shape.mesh.positions[shape.mesh.indices[f]*3+2]; - vtx0.xyzw[3] = 0.f; - - vtx0.uv[0] = 0.5f;//shape.mesh.positions[shape.mesh.indices[f]*3+2];? - vtx0.uv[1] = 0.5f; - - GLInstanceVertex vtx1; - vtx1.xyzw[0] = shape.mesh.positions[shape.mesh.indices[f+1]*3+0]; - vtx1.xyzw[1] = shape.mesh.positions[shape.mesh.indices[f+1]*3+1]; - vtx1.xyzw[2] = shape.mesh.positions[shape.mesh.indices[f+1]*3+2]; - vtx1.xyzw[3]= 0.f; - vtx1.uv[0] = 0.5f;//obj->textureList[face->vertex_index[1]]->e[0]; - vtx1.uv[1] = 0.5f;//obj->textureList[face->vertex_index[1]]->e[1]; - - GLInstanceVertex vtx2; - vtx2.xyzw[0] = shape.mesh.positions[shape.mesh.indices[f+2]*3+0]; - vtx2.xyzw[1] = shape.mesh.positions[shape.mesh.indices[f+2]*3+1]; - vtx2.xyzw[2] = shape.mesh.positions[shape.mesh.indices[f+2]*3+2]; - vtx2.xyzw[3] = 0.f; - vtx2.uv[0] = 0.5f; - vtx2.uv[1] = 0.5f; - - - btVector3 v0(vtx0.xyzw[0],vtx0.xyzw[1],vtx0.xyzw[2]); - btVector3 v1(vtx1.xyzw[0],vtx1.xyzw[1],vtx1.xyzw[2]); - btVector3 v2(vtx2.xyzw[0],vtx2.xyzw[1],vtx2.xyzw[2]); - - normal = (v1-v0).cross(v2-v0); - btScalar len2 = normal.length2(); - //skip degenerate triangles - if (len2 > SIMD_EPSILON) - { - normal.normalize(); - vtx0.normal[0] = normal[0]; - vtx0.normal[1] = normal[1]; - vtx0.normal[2] = normal[2]; - vtx1.normal[0] = normal[0]; - vtx1.normal[1] = normal[1]; - vtx1.normal[2] = normal[2]; - vtx2.normal[0] = normal[0]; - vtx2.normal[1] = normal[1]; - vtx2.normal[2] = normal[2]; - vertices->push_back(vtx0); - vertices->push_back(vtx1); - vertices->push_back(vtx2); - indicesPtr->push_back(vtxBaseIndex); - indicesPtr->push_back(vtxBaseIndex+1); - indicesPtr->push_back(vtxBaseIndex+2); - } - - } - } - } - - - GLInstanceGraphicsShape* gfxShape = new GLInstanceGraphicsShape; - gfxShape->m_vertices = vertices; - gfxShape->m_numvertices = vertices->size(); - gfxShape->m_indices = indicesPtr; - gfxShape->m_numIndices = indicesPtr->size(); - for (int i=0;i<4;i++) - gfxShape->m_scaling[i] = 1;//bake the scaling into the vertices - return gfxShape; - } -} @@ -150,7 +56,7 @@ void ImportObjDemo::initPhysics(GraphicsPhysicsBridge& gfxBridge) return; btVector3 shift(0,0,0); - btVector3 scaling(1,1,1); + btVector3 scaling(10,10,10); // int index=10; { @@ -158,7 +64,7 @@ void ImportObjDemo::initPhysics(GraphicsPhysicsBridge& gfxBridge) std::vector shapes; std::string err = tinyobj::LoadObj(shapes, relativeFileName, prefix[prefixIndex]); - GLInstanceGraphicsShape* gfxShape = gCreateGraphicsShapeFromWavefrontObj(shapes); + GLInstanceGraphicsShape* gfxShape = btgCreateGraphicsShapeFromWavefrontObj(shapes); btTransform trans; trans.setIdentity(); diff --git a/Demos3/ImportObjDemo/Wavefront2GLInstanceGraphicsShape.cpp b/Demos3/ImportObjDemo/Wavefront2GLInstanceGraphicsShape.cpp new file mode 100644 index 000000000..f7d6d7d19 --- /dev/null +++ b/Demos3/ImportObjDemo/Wavefront2GLInstanceGraphicsShape.cpp @@ -0,0 +1,109 @@ +#include "Wavefront2GLInstanceGraphicsShape.h" + +#include "OpenGLWindow/GLInstancingRenderer.h" +#include "OpenGLWindow/GLInstanceGraphicsShape.h" +#include "btBulletDynamicsCommon.h" +#include "OpenGLWindow/SimpleOpenGL3App.h" +#include "Wavefront2GLInstanceGraphicsShape.h" +#include "OpenGLWindow/GLInstancingRenderer.h" +#include "OpenGLWindow/GLInstanceGraphicsShape.h" + +GLInstanceGraphicsShape* btgCreateGraphicsShapeFromWavefrontObj(std::vector& shapes) +{ + + b3AlignedObjectArray* vertices = new b3AlignedObjectArray; + { + // int numVertices = obj->vertexCount; + // int numIndices = 0; + b3AlignedObjectArray* indicesPtr = new b3AlignedObjectArray; + + for (int s=0;s<(int)shapes.size();s++) + { + tinyobj::shape_t& shape = shapes[s]; + int faceCount = shape.mesh.indices.size(); + + + for (int f=0;fsize(); + + + + GLInstanceVertex vtx0; + vtx0.xyzw[0] = shape.mesh.positions[shape.mesh.indices[f]*3+0]; + vtx0.xyzw[1] = shape.mesh.positions[shape.mesh.indices[f]*3+1]; + vtx0.xyzw[2] = shape.mesh.positions[shape.mesh.indices[f]*3+2]; + vtx0.xyzw[3] = 0.f; + + vtx0.uv[0] = 0.5f;//shape.mesh.positions[shape.mesh.indices[f]*3+2];? + vtx0.uv[1] = 0.5f; + + GLInstanceVertex vtx1; + vtx1.xyzw[0] = shape.mesh.positions[shape.mesh.indices[f+1]*3+0]; + vtx1.xyzw[1] = shape.mesh.positions[shape.mesh.indices[f+1]*3+1]; + vtx1.xyzw[2] = shape.mesh.positions[shape.mesh.indices[f+1]*3+2]; + vtx1.xyzw[3]= 0.f; + vtx1.uv[0] = 0.5f;//obj->textureList[face->vertex_index[1]]->e[0]; + vtx1.uv[1] = 0.5f;//obj->textureList[face->vertex_index[1]]->e[1]; + + GLInstanceVertex vtx2; + vtx2.xyzw[0] = shape.mesh.positions[shape.mesh.indices[f+2]*3+0]; + vtx2.xyzw[1] = shape.mesh.positions[shape.mesh.indices[f+2]*3+1]; + vtx2.xyzw[2] = shape.mesh.positions[shape.mesh.indices[f+2]*3+2]; + vtx2.xyzw[3] = 0.f; + vtx2.uv[0] = 0.5f; + vtx2.uv[1] = 0.5f; + + + btVector3 v0(vtx0.xyzw[0],vtx0.xyzw[1],vtx0.xyzw[2]); + btVector3 v1(vtx1.xyzw[0],vtx1.xyzw[1],vtx1.xyzw[2]); + btVector3 v2(vtx2.xyzw[0],vtx2.xyzw[1],vtx2.xyzw[2]); + + normal = (v1-v0).cross(v2-v0); + btScalar len2 = normal.length2(); + //skip degenerate triangles + if (len2 > SIMD_EPSILON) + { + normal.normalize(); + } else + { + normal.setValue(0,0,0); + } + vtx0.normal[0] = normal[0]; + vtx0.normal[1] = normal[1]; + vtx0.normal[2] = normal[2]; + vtx1.normal[0] = normal[0]; + vtx1.normal[1] = normal[1]; + vtx1.normal[2] = normal[2]; + vtx2.normal[0] = normal[0]; + vtx2.normal[1] = normal[1]; + vtx2.normal[2] = normal[2]; + vertices->push_back(vtx0); + vertices->push_back(vtx1); + vertices->push_back(vtx2); + indicesPtr->push_back(vtxBaseIndex); + indicesPtr->push_back(vtxBaseIndex+1); + indicesPtr->push_back(vtxBaseIndex+2); + + + } + } + } + + + GLInstanceGraphicsShape* gfxShape = new GLInstanceGraphicsShape; + gfxShape->m_vertices = vertices; + gfxShape->m_numvertices = vertices->size(); + gfxShape->m_indices = indicesPtr; + gfxShape->m_numIndices = indicesPtr->size(); + for (int i=0;i<4;i++) + gfxShape->m_scaling[i] = 1;//bake the scaling into the vertices + return gfxShape; + } +} + diff --git a/Demos3/ImportObjDemo/Wavefront2GLInstanceGraphicsShape.h b/Demos3/ImportObjDemo/Wavefront2GLInstanceGraphicsShape.h new file mode 100644 index 000000000..09f0bf683 --- /dev/null +++ b/Demos3/ImportObjDemo/Wavefront2GLInstanceGraphicsShape.h @@ -0,0 +1,9 @@ +#ifndef WAVEFRONT2GRAPHICS_H +#define WAVEFRONT2GRAPHICS_H + +#include"../Wavefront/tiny_obj_loader.h" +#include + +struct GLInstanceGraphicsShape* btgCreateGraphicsShapeFromWavefrontObj(std::vector& shapes); + +#endif //WAVEFRONT2GRAPHICS_H