Init
This commit is contained in:
200
physx/samples/pxtoolkit/src/PxTkBmpLoader.cpp
Normal file
200
physx/samples/pxtoolkit/src/PxTkBmpLoader.cpp
Normal file
@ -0,0 +1,200 @@
|
||||
//
|
||||
// Redistribution and use in source and binary forms, with or without
|
||||
// modification, are permitted provided that the following conditions
|
||||
// are met:
|
||||
// * Redistributions of source code must retain the above copyright
|
||||
// notice, this list of conditions and the following disclaimer.
|
||||
// * Redistributions in binary form must reproduce the above copyright
|
||||
// notice, this list of conditions and the following disclaimer in the
|
||||
// documentation and/or other materials provided with the distribution.
|
||||
// * Neither the name of NVIDIA CORPORATION nor the names of its
|
||||
// contributors may be used to endorse or promote products derived
|
||||
// from this software without specific prior written permission.
|
||||
//
|
||||
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ''AS IS'' AND ANY
|
||||
// EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
||||
// PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
|
||||
// CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
|
||||
// EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
|
||||
// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
|
||||
// PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
|
||||
// OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
||||
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
//
|
||||
// Copyright (c) 2008-2021 NVIDIA Corporation. All rights reserved.
|
||||
// Copyright (c) 2004-2008 AGEIA Technologies, Inc. All rights reserved.
|
||||
// Copyright (c) 2001-2004 NovodeX AG. All rights reserved.
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <assert.h>
|
||||
#include "PxTkBmpLoader.h"
|
||||
#include "PxTkFile.h"
|
||||
|
||||
|
||||
using namespace PxToolkit;
|
||||
|
||||
#define MAKETWOCC(a,b) ( (static_cast<char>(a)) | ((static_cast<char>(b))<< 8) )
|
||||
|
||||
static bool isBigEndian() { int i = 1; return *(reinterpret_cast<char*>(&i))==0; }
|
||||
|
||||
static unsigned short endianSwap(unsigned short nValue)
|
||||
{
|
||||
return (((nValue>> 8)) | (nValue << 8));
|
||||
|
||||
}
|
||||
|
||||
static unsigned int endianSwap(unsigned int i)
|
||||
{
|
||||
unsigned char b1, b2, b3, b4;
|
||||
|
||||
b1 = i & 255;
|
||||
b2 = ( i >> 8 ) & 255;
|
||||
b3 = ( i>>16 ) & 255;
|
||||
b4 = ( i>>24 ) & 255;
|
||||
|
||||
return (static_cast<unsigned int>(b1) << 24) + (static_cast<unsigned int>(b2) << 16) + (static_cast<unsigned int>(b3) << 8) + b4;
|
||||
}
|
||||
|
||||
// -------------------------------------------------------------------
|
||||
|
||||
#pragma pack(1)
|
||||
|
||||
struct BMPHEADER {
|
||||
unsigned short Type;
|
||||
unsigned int Size;
|
||||
unsigned short Reserved1;
|
||||
unsigned short Reserved2;
|
||||
unsigned int OffBits;
|
||||
};
|
||||
|
||||
// Only Win3.0 BMPINFO (see later for OS/2)
|
||||
struct BMPINFO {
|
||||
unsigned int Size;
|
||||
unsigned int Width;
|
||||
unsigned int Height;
|
||||
unsigned short Planes;
|
||||
unsigned short BitCount;
|
||||
unsigned int Compression;
|
||||
unsigned int SizeImage;
|
||||
unsigned int XPelsPerMeter;
|
||||
unsigned int YPelsPerMeter;
|
||||
unsigned int ClrUsed;
|
||||
unsigned int ClrImportant;
|
||||
};
|
||||
|
||||
#pragma pack()
|
||||
|
||||
// Compression Type
|
||||
#define BI_RGB 0L
|
||||
#define BI_RLE8 1L
|
||||
#define BI_RLE4 2L
|
||||
|
||||
// -------------------------------------------------------------------
|
||||
BmpLoader::BmpLoader() :
|
||||
mWidth (0),
|
||||
mHeight (0),
|
||||
mRGB (NULL),
|
||||
mHasAlpha (false)
|
||||
{
|
||||
}
|
||||
|
||||
// -------------------------------------------------------------------
|
||||
BmpLoader::~BmpLoader()
|
||||
{
|
||||
if (mRGB) free(mRGB);
|
||||
}
|
||||
|
||||
// -------------------------------------------------------------------
|
||||
bool BmpLoader::loadBmp(PxFileHandle f)
|
||||
{
|
||||
if (!f) return false;
|
||||
|
||||
if (mRGB) {
|
||||
free(mRGB);
|
||||
mRGB = NULL;
|
||||
}
|
||||
mWidth = 0;
|
||||
mHeight = 0;
|
||||
|
||||
size_t num;
|
||||
BMPHEADER header;
|
||||
num = fread(&header, 1, sizeof(BMPHEADER), f);
|
||||
if(isBigEndian()) header.Type = endianSwap(header.Type);
|
||||
if (num != sizeof(BMPHEADER)) { fclose(f); return false; }
|
||||
if (header.Type != MAKETWOCC('B','M')) { fclose(f); return false; }
|
||||
|
||||
BMPINFO info;
|
||||
num = fread(&info, 1, sizeof(BMPINFO), f);
|
||||
if (num != sizeof(BMPINFO)) { fclose(f); return false; }
|
||||
if(isBigEndian()) info.Size = endianSwap(info.Size);
|
||||
if(isBigEndian()) info.BitCount = endianSwap(info.BitCount);
|
||||
if(isBigEndian()) info.Compression = endianSwap(info.Compression);
|
||||
if(isBigEndian()) info.Width = endianSwap(info.Width);
|
||||
if(isBigEndian()) info.Height = endianSwap(info.Height);
|
||||
|
||||
if (info.Size != sizeof(BMPINFO)) { fclose(f); return false; }
|
||||
if (info.BitCount != 24 && info.BitCount != 32) { fclose(f); return false; }
|
||||
if (info.Compression != BI_RGB) { fclose(f); return false; }
|
||||
|
||||
mWidth = info.Width;
|
||||
mHeight = info.Height;
|
||||
|
||||
int bytesPerPixel = 0;
|
||||
if(info.BitCount == 24)
|
||||
{
|
||||
mHasAlpha = false;
|
||||
bytesPerPixel = 3;
|
||||
}
|
||||
else if(info.BitCount == 32)
|
||||
{
|
||||
mHasAlpha = true;
|
||||
bytesPerPixel = 4;
|
||||
}
|
||||
else assert(0);
|
||||
|
||||
mRGB = static_cast<unsigned char*>(malloc(mWidth * mHeight * bytesPerPixel));
|
||||
|
||||
int lineLen = (((info.Width * (info.BitCount>>3)) + 3)>>2)<<2;
|
||||
unsigned char* line = static_cast<unsigned char*>(malloc(lineLen));
|
||||
|
||||
for(int i = info.Height-1; i >= 0; i--)
|
||||
{
|
||||
num = fread(line, 1, static_cast<size_t>(lineLen), f);
|
||||
if (num != static_cast<size_t>(lineLen))
|
||||
{
|
||||
fclose(f);
|
||||
free(line);
|
||||
return false;
|
||||
}
|
||||
unsigned char* src = line;
|
||||
unsigned char* dest = mRGB + i*info.Width*bytesPerPixel;
|
||||
for(unsigned int j = 0; j < info.Width; j++)
|
||||
{
|
||||
unsigned char b = *src++;
|
||||
unsigned char g = *src++;
|
||||
unsigned char r = *src++;
|
||||
unsigned char a = mHasAlpha ? *src++ : 0;
|
||||
*dest++ = r;
|
||||
*dest++ = g;
|
||||
*dest++ = b;
|
||||
if(mHasAlpha)
|
||||
*dest++ = a;
|
||||
}
|
||||
}
|
||||
|
||||
free(line);
|
||||
return true;
|
||||
}
|
||||
|
||||
// -------------------------------------------------------------------
|
||||
bool BmpLoader::loadBmp(const char *filename)
|
||||
{
|
||||
PxFileHandle f = NULL;
|
||||
PxToolkit::fopen_s(&f, filename, "rb");
|
||||
bool ret = loadBmp(f);
|
||||
if(f) fclose(f);
|
||||
return ret;
|
||||
}
|
||||
69
physx/samples/pxtoolkit/src/PxTkFPS.cpp
Normal file
69
physx/samples/pxtoolkit/src/PxTkFPS.cpp
Normal file
@ -0,0 +1,69 @@
|
||||
//
|
||||
// Redistribution and use in source and binary forms, with or without
|
||||
// modification, are permitted provided that the following conditions
|
||||
// are met:
|
||||
// * Redistributions of source code must retain the above copyright
|
||||
// notice, this list of conditions and the following disclaimer.
|
||||
// * Redistributions in binary form must reproduce the above copyright
|
||||
// notice, this list of conditions and the following disclaimer in the
|
||||
// documentation and/or other materials provided with the distribution.
|
||||
// * Neither the name of NVIDIA CORPORATION nor the names of its
|
||||
// contributors may be used to endorse or promote products derived
|
||||
// from this software without specific prior written permission.
|
||||
//
|
||||
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ''AS IS'' AND ANY
|
||||
// EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
||||
// PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
|
||||
// CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
|
||||
// EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
|
||||
// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
|
||||
// PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
|
||||
// OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
||||
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
//
|
||||
// Copyright (c) 2008-2021 NVIDIA Corporation. All rights reserved.
|
||||
// Copyright (c) 2004-2008 AGEIA Technologies, Inc. All rights reserved.
|
||||
// Copyright (c) 2001-2004 NovodeX AG. All rights reserved.
|
||||
|
||||
#include "PxTkFPS.h"
|
||||
|
||||
using namespace PxToolkit;
|
||||
|
||||
FPS::FPS()
|
||||
{
|
||||
mFrames = 0;
|
||||
mFrames2 = 0;
|
||||
mFPS = 0.0f;
|
||||
mInstantFPS = 0.0f;
|
||||
}
|
||||
|
||||
FPS::~FPS()
|
||||
{
|
||||
}
|
||||
|
||||
void FPS::update()
|
||||
{
|
||||
// Keep track of the time lapse and frame count
|
||||
mFrames++;
|
||||
mFrames2++;
|
||||
|
||||
// Instant frame rate
|
||||
PxReal Delta = PxReal(mTimer2.peekElapsedSeconds());
|
||||
if(Delta > 0.01f)
|
||||
{
|
||||
mInstantFPS = PxReal(mFrames2) / Delta;
|
||||
mTimer2.getElapsedSeconds();
|
||||
mFrames2 = 0;
|
||||
}
|
||||
|
||||
// Update the frame rate once per second
|
||||
Delta = PxReal(mTimer.peekElapsedSeconds());
|
||||
if(Delta > 1.0f)
|
||||
{
|
||||
mFPS = PxReal(mFrames) / Delta;
|
||||
mTimer.getElapsedSeconds();
|
||||
mFrames = 0;
|
||||
}
|
||||
}
|
||||
89
physx/samples/pxtoolkit/src/PxTkMatrixUtils.cpp
Normal file
89
physx/samples/pxtoolkit/src/PxTkMatrixUtils.cpp
Normal file
@ -0,0 +1,89 @@
|
||||
//
|
||||
// Redistribution and use in source and binary forms, with or without
|
||||
// modification, are permitted provided that the following conditions
|
||||
// are met:
|
||||
// * Redistributions of source code must retain the above copyright
|
||||
// notice, this list of conditions and the following disclaimer.
|
||||
// * Redistributions in binary form must reproduce the above copyright
|
||||
// notice, this list of conditions and the following disclaimer in the
|
||||
// documentation and/or other materials provided with the distribution.
|
||||
// * Neither the name of NVIDIA CORPORATION nor the names of its
|
||||
// contributors may be used to endorse or promote products derived
|
||||
// from this software without specific prior written permission.
|
||||
//
|
||||
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ''AS IS'' AND ANY
|
||||
// EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
||||
// PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
|
||||
// CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
|
||||
// EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
|
||||
// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
|
||||
// PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
|
||||
// OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
||||
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
//
|
||||
// Copyright (c) 2008-2021 NVIDIA Corporation. All rights reserved.
|
||||
// Copyright (c) 2004-2008 AGEIA Technologies, Inc. All rights reserved.
|
||||
// Copyright (c) 2001-2004 NovodeX AG. All rights reserved.
|
||||
|
||||
#include "PxTkMatrixUtils.h"
|
||||
|
||||
using namespace PxToolkit;
|
||||
|
||||
void PxToolkit::setRotX(PxMat33& m, PxReal angle)
|
||||
{
|
||||
m = PxMat33(PxIdentity);
|
||||
|
||||
const PxReal cos = cosf(angle);
|
||||
const PxReal sin = sinf(angle);
|
||||
|
||||
m[1][1] = m[2][2] = cos;
|
||||
m[1][2] = sin;
|
||||
m[2][1] = -sin;
|
||||
}
|
||||
|
||||
void PxToolkit::setRotY(PxMat33& m, PxReal angle)
|
||||
{
|
||||
m = PxMat33(PxIdentity);
|
||||
|
||||
const PxReal cos = cosf(angle);
|
||||
const PxReal sin = sinf(angle);
|
||||
|
||||
m[0][0] = m[2][2] = cos;
|
||||
m[0][2] = -sin;
|
||||
m[2][0] = sin;
|
||||
}
|
||||
|
||||
void PxToolkit::setRotZ(PxMat33& m, PxReal angle)
|
||||
{
|
||||
m = PxMat33(PxIdentity);
|
||||
|
||||
const PxReal cos = cosf(angle);
|
||||
const PxReal sin = sinf(angle);
|
||||
|
||||
m[0][0] = m[1][1] = cos;
|
||||
m[0][1] = sin;
|
||||
m[1][0] = -sin;
|
||||
}
|
||||
|
||||
PxQuat PxToolkit::getRotXQuat(float angle)
|
||||
{
|
||||
PxMat33 m;
|
||||
setRotX(m, angle);
|
||||
return PxQuat(m);
|
||||
}
|
||||
|
||||
PxQuat PxToolkit::getRotYQuat(float angle)
|
||||
{
|
||||
PxMat33 m;
|
||||
setRotY(m, angle);
|
||||
return PxQuat(m);
|
||||
}
|
||||
|
||||
PxQuat PxToolkit::getRotZQuat(float angle)
|
||||
{
|
||||
PxMat33 m;
|
||||
setRotZ(m, angle);
|
||||
return PxQuat(m);
|
||||
}
|
||||
141
physx/samples/pxtoolkit/src/PxTkRandom.cpp
Normal file
141
physx/samples/pxtoolkit/src/PxTkRandom.cpp
Normal file
@ -0,0 +1,141 @@
|
||||
//
|
||||
// Redistribution and use in source and binary forms, with or without
|
||||
// modification, are permitted provided that the following conditions
|
||||
// are met:
|
||||
// * Redistributions of source code must retain the above copyright
|
||||
// notice, this list of conditions and the following disclaimer.
|
||||
// * Redistributions in binary form must reproduce the above copyright
|
||||
// notice, this list of conditions and the following disclaimer in the
|
||||
// documentation and/or other materials provided with the distribution.
|
||||
// * Neither the name of NVIDIA CORPORATION nor the names of its
|
||||
// contributors may be used to endorse or promote products derived
|
||||
// from this software without specific prior written permission.
|
||||
//
|
||||
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ''AS IS'' AND ANY
|
||||
// EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
||||
// PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
|
||||
// CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
|
||||
// EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
|
||||
// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
|
||||
// PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
|
||||
// OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
||||
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
//
|
||||
// Copyright (c) 2008-2021 NVIDIA Corporation. All rights reserved.
|
||||
// Copyright (c) 2004-2008 AGEIA Technologies, Inc. All rights reserved.
|
||||
// Copyright (c) 2001-2004 NovodeX AG. All rights reserved.
|
||||
|
||||
#include "PxTkRandom.h"
|
||||
#include "foundation/PxQuat.h"
|
||||
|
||||
using namespace physx;
|
||||
using namespace PxToolkit;
|
||||
|
||||
static RandomR250 gRandomR250(0x95d6739b);
|
||||
|
||||
PxVec3 BasicRandom::unitRandomPt()
|
||||
{
|
||||
PxVec3 v;
|
||||
do
|
||||
{
|
||||
v.x = randomFloat();
|
||||
v.y = randomFloat();
|
||||
v.z = randomFloat();
|
||||
}
|
||||
while(v.normalize()<1e-6f);
|
||||
return v;
|
||||
}
|
||||
|
||||
PxQuat BasicRandom::unitRandomQuat()
|
||||
{
|
||||
PxQuat v;
|
||||
do
|
||||
{
|
||||
v.x = randomFloat();
|
||||
v.y = randomFloat();
|
||||
v.z = randomFloat();
|
||||
v.w = randomFloat();
|
||||
}
|
||||
while(v.normalize()<1e-6f);
|
||||
|
||||
return v;
|
||||
}
|
||||
|
||||
|
||||
void BasicRandom::unitRandomPt(PxVec3& v)
|
||||
{
|
||||
v = unitRandomPt();
|
||||
}
|
||||
void BasicRandom::unitRandomQuat(PxQuat& v)
|
||||
{
|
||||
v = unitRandomQuat();
|
||||
}
|
||||
|
||||
|
||||
void PxToolkit::SetSeed(PxU32 seed)
|
||||
{
|
||||
gRandomR250.setSeed(seed);
|
||||
}
|
||||
|
||||
PxU32 PxToolkit::Rand()
|
||||
{
|
||||
return gRandomR250.randI() & TEST_MAX_RAND;
|
||||
}
|
||||
|
||||
|
||||
RandomR250::RandomR250(PxI32 s)
|
||||
{
|
||||
setSeed(s);
|
||||
}
|
||||
|
||||
void RandomR250::setSeed(PxI32 s)
|
||||
{
|
||||
BasicRandom lcg(s);
|
||||
mIndex = 0;
|
||||
|
||||
PxI32 j;
|
||||
for (j = 0; j < 250; j++) // fill r250 buffer with bit values
|
||||
mBuffer[j] = lcg.randomize();
|
||||
|
||||
for (j = 0; j < 250; j++) // set some MSBs to 1
|
||||
if ( lcg.randomize() > 0x40000000L )
|
||||
mBuffer[j] |= 0x80000000L;
|
||||
|
||||
PxU32 msb = 0x80000000; // turn on diagonal bit
|
||||
PxU32 mask = 0xffffffff; // turn off the leftmost bits
|
||||
|
||||
for (j = 0; j < 32; j++)
|
||||
{
|
||||
const PxI32 k = 7 * j + 3; // select a word to operate on
|
||||
mBuffer[k] &= mask; // turn off bits left of the diagonal
|
||||
mBuffer[k] |= msb; // turn on the diagonal bit
|
||||
mask >>= 1;
|
||||
msb >>= 1;
|
||||
}
|
||||
}
|
||||
|
||||
PxU32 RandomR250::randI()
|
||||
{
|
||||
PxI32 j;
|
||||
|
||||
// wrap pointer around
|
||||
if ( mIndex >= 147 ) j = mIndex - 147;
|
||||
else j = mIndex + 103;
|
||||
|
||||
const PxU32 new_rand = mBuffer[ mIndex ] ^ mBuffer[ j ];
|
||||
mBuffer[ mIndex ] = new_rand;
|
||||
|
||||
// increment pointer for next time
|
||||
if ( mIndex >= 249 ) mIndex = 0;
|
||||
else mIndex++;
|
||||
|
||||
return new_rand >> 1;
|
||||
}
|
||||
|
||||
PxReal RandomR250::randUnit()
|
||||
{
|
||||
PxU32 mask = (1<<23)-1;
|
||||
return PxF32(randI()&(mask))/PxF32(mask);
|
||||
}
|
||||
149
physx/samples/pxtoolkit/src/PxTkStream.cpp
Normal file
149
physx/samples/pxtoolkit/src/PxTkStream.cpp
Normal file
@ -0,0 +1,149 @@
|
||||
//
|
||||
// Redistribution and use in source and binary forms, with or without
|
||||
// modification, are permitted provided that the following conditions
|
||||
// are met:
|
||||
// * Redistributions of source code must retain the above copyright
|
||||
// notice, this list of conditions and the following disclaimer.
|
||||
// * Redistributions in binary form must reproduce the above copyright
|
||||
// notice, this list of conditions and the following disclaimer in the
|
||||
// documentation and/or other materials provided with the distribution.
|
||||
// * Neither the name of NVIDIA CORPORATION nor the names of its
|
||||
// contributors may be used to endorse or promote products derived
|
||||
// from this software without specific prior written permission.
|
||||
//
|
||||
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ''AS IS'' AND ANY
|
||||
// EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
||||
// PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
|
||||
// CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
|
||||
// EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
|
||||
// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
|
||||
// PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
|
||||
// OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
||||
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
//
|
||||
// Copyright (c) 2008-2021 NVIDIA Corporation. All rights reserved.
|
||||
// Copyright (c) 2004-2008 AGEIA Technologies, Inc. All rights reserved.
|
||||
// Copyright (c) 2001-2004 NovodeX AG. All rights reserved.
|
||||
|
||||
#include "PxTkStream.h"
|
||||
#include "foundation/PxAssert.h"
|
||||
#include "PxTkFile.h"
|
||||
#include "PxTkNamespaceMangle.h"
|
||||
#include "PsIntrinsics.h"
|
||||
#include "foundation/PxMath.h"
|
||||
#include "PxPhysics.h"
|
||||
#include "geometry/PxConvexMesh.h"
|
||||
#include "cooking/PxCooking.h"
|
||||
#include "foundation/PxBounds3.h"
|
||||
#include "extensions/PxDefaultStreams.h"
|
||||
|
||||
using namespace PxToolkit;
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
PxTriangleMesh* PxToolkit::createTriangleMesh32(PxPhysics& physics, PxCooking& cooking, const PxVec3* verts, PxU32 vertCount, const PxU32* indices32, PxU32 triCount, bool insert)
|
||||
{
|
||||
PxTriangleMeshDesc meshDesc;
|
||||
meshDesc.points.count = vertCount;
|
||||
meshDesc.points.stride = sizeof(PxVec3);
|
||||
meshDesc.points.data = verts;
|
||||
|
||||
meshDesc.triangles.count = triCount;
|
||||
meshDesc.triangles.stride = 3*sizeof(PxU32);
|
||||
meshDesc.triangles.data = indices32;
|
||||
|
||||
if(!insert)
|
||||
{
|
||||
PxDefaultMemoryOutputStream writeBuffer;
|
||||
bool status = cooking.cookTriangleMesh(meshDesc, writeBuffer);
|
||||
if(!status)
|
||||
return NULL;
|
||||
|
||||
PxDefaultMemoryInputData readBuffer(writeBuffer.getData(), writeBuffer.getSize());
|
||||
return physics.createTriangleMesh(readBuffer);
|
||||
}
|
||||
else
|
||||
{
|
||||
return cooking.createTriangleMesh(meshDesc,physics.getPhysicsInsertionCallback());
|
||||
}
|
||||
}
|
||||
|
||||
PxTriangleMesh* PxToolkit::createTriangleMesh32(PxPhysics& physics, PxCooking& cooking, PxTriangleMeshDesc* meshDesc, bool insert)
|
||||
{
|
||||
if(!insert)
|
||||
{
|
||||
PxDefaultMemoryOutputStream writeBuffer;
|
||||
bool status = cooking.cookTriangleMesh(*meshDesc, writeBuffer);
|
||||
if(!status)
|
||||
return NULL;
|
||||
|
||||
PxDefaultMemoryInputData readBuffer(writeBuffer.getData(), writeBuffer.getSize());
|
||||
return physics.createTriangleMesh(readBuffer);
|
||||
}
|
||||
else
|
||||
{
|
||||
return cooking.createTriangleMesh(*meshDesc,physics.getPhysicsInsertionCallback());
|
||||
}
|
||||
}
|
||||
|
||||
PxConvexMesh* PxToolkit::createConvexMesh(PxPhysics& physics, PxCooking& cooking, const PxVec3* verts, PxU32 vertCount, PxConvexFlags flags)
|
||||
{
|
||||
PxConvexMeshDesc convexDesc;
|
||||
convexDesc.points.count = vertCount;
|
||||
convexDesc.points.stride = sizeof(PxVec3);
|
||||
convexDesc.points.data = verts;
|
||||
convexDesc.flags = flags;
|
||||
return cooking.createConvexMesh(convexDesc, physics.getPhysicsInsertionCallback());
|
||||
}
|
||||
|
||||
PxConvexMesh* PxToolkit::createConvexMeshSafe(PxPhysics& physics, PxCooking& cooking, const PxVec3* verts, PxU32 vertCount, PxConvexFlags flags, PxU32 vLimit)
|
||||
{
|
||||
PxConvexMeshDesc convexDesc;
|
||||
convexDesc.points.count = vertCount;
|
||||
convexDesc.points.stride = sizeof(PxVec3);
|
||||
convexDesc.points.data = verts;
|
||||
convexDesc.flags = flags;
|
||||
convexDesc.vertexLimit = vLimit;
|
||||
|
||||
PxDefaultMemoryOutputStream buf;
|
||||
bool retVal = cooking.cookConvexMesh(convexDesc, buf);
|
||||
if(!retVal)
|
||||
{
|
||||
// create AABB
|
||||
PxBounds3 aabb;
|
||||
aabb.setEmpty();
|
||||
for (PxU32 i = 0; i < vertCount; i++)
|
||||
{
|
||||
aabb.include(verts[i]);
|
||||
}
|
||||
|
||||
PxVec3 aabbVerts[8];
|
||||
aabbVerts[0] = PxVec3(aabb.minimum.x,aabb.minimum.y,aabb.minimum.z);
|
||||
aabbVerts[1] = PxVec3(aabb.maximum.x,aabb.minimum.y,aabb.minimum.z);
|
||||
aabbVerts[2] = PxVec3(aabb.maximum.x,aabb.maximum.y,aabb.minimum.z);
|
||||
aabbVerts[3] = PxVec3(aabb.minimum.x,aabb.maximum.y,aabb.minimum.z);
|
||||
|
||||
aabbVerts[4] = PxVec3(aabb.minimum.x,aabb.minimum.y,aabb.maximum.z);
|
||||
aabbVerts[5] = PxVec3(aabb.maximum.x,aabb.minimum.y,aabb.maximum.z);
|
||||
aabbVerts[6] = PxVec3(aabb.maximum.x,aabb.maximum.y,aabb.maximum.z);
|
||||
aabbVerts[7] = PxVec3(aabb.minimum.x,aabb.maximum.y,aabb.maximum.z);
|
||||
|
||||
convexDesc.points.count = 8;
|
||||
convexDesc.points.stride = sizeof(PxVec3);
|
||||
convexDesc.points.data = &aabbVerts[0];
|
||||
convexDesc.flags = flags;
|
||||
|
||||
retVal = cooking.cookConvexMesh(convexDesc, buf);
|
||||
}
|
||||
|
||||
if(!retVal)
|
||||
{
|
||||
return NULL;
|
||||
}
|
||||
|
||||
PxDefaultMemoryInputData input(buf.getData(), buf.getSize());
|
||||
return physics.createConvexMesh(input);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user