prepare for GPU cloth/softbody
This commit is contained in:
@@ -38,9 +38,9 @@ public:
|
||||
:useOpenCL(true),
|
||||
preferredOpenCLPlatformIndex(-1),
|
||||
preferredOpenCLDeviceIndex(-1),
|
||||
arraySizeX(20),
|
||||
arraySizeX(25),
|
||||
arraySizeY(20),
|
||||
arraySizeZ(20),
|
||||
arraySizeZ(25),
|
||||
m_useConcaveMesh(false),
|
||||
gapX(14.3),
|
||||
gapY(14.0),
|
||||
|
||||
@@ -31,6 +31,8 @@
|
||||
#include "rigidbody/GpuCompoundScene.h"
|
||||
#include "rigidbody/GpuSphereScene.h"
|
||||
#include "rigidbody/Bullet2FileDemo.h"
|
||||
#include "softbody/GpuSoftBodyDemo.h"
|
||||
|
||||
//#include "BroadphaseBenchmark.h"
|
||||
|
||||
int g_OpenGLWidth=1024;
|
||||
@@ -67,8 +69,6 @@ GpuDemo::CreateFunc* allDemos[]=
|
||||
{
|
||||
// ConcaveCompound2Scene::MyCreateFunc,
|
||||
|
||||
|
||||
Bullet2FileDemo::MyCreateFunc,
|
||||
GpuBoxPlaneScene::MyCreateFunc,
|
||||
GpuConvexPlaneScene::MyCreateFunc,
|
||||
|
||||
@@ -91,7 +91,10 @@ GpuDemo::CreateFunc* allDemos[]=
|
||||
|
||||
GpuSphereScene::MyCreateFunc,
|
||||
|
||||
GpuSoftClothDemo::MyCreateFunc,
|
||||
|
||||
Bullet2FileDemo::MyCreateFunc,
|
||||
|
||||
PairBench::MyCreateFunc,
|
||||
|
||||
|
||||
|
||||
@@ -61,7 +61,8 @@ function createProject(vendor)
|
||||
"../../btgui/OpenGLTrueTypeFont/opengl_fontstashcallbacks.cpp",
|
||||
"../../btgui/OpenGLTrueTypeFont/opengl_fontstashcallbacks.h",
|
||||
"../../btgui/FontFiles/OpenSans.cpp",
|
||||
|
||||
"../../btgui/stb_image/stb_image.cpp",
|
||||
"../../btgui/stb_image/stb_image.h",
|
||||
}
|
||||
|
||||
if os.is("Windows") then
|
||||
|
||||
410
Demos3/GpuDemos/softbody/GpuSoftBodyDemo.cpp
Normal file
410
Demos3/GpuDemos/softbody/GpuSoftBodyDemo.cpp
Normal file
@@ -0,0 +1,410 @@
|
||||
#include "GpuSoftBodyDemo.h"
|
||||
#include "Bullet3Common/b3Quickprof.h"
|
||||
#include "OpenGLWindow/ShapeData.h"
|
||||
#include "OpenGLWindow/GLInstancingRenderer.h"
|
||||
#include "Bullet3Common/b3Quaternion.h"
|
||||
#include "OpenGLWindow/b3gWindowInterface.h"
|
||||
#include "Bullet3OpenCL/BroadphaseCollision/b3GpuSapBroadphase.h"
|
||||
#include "../GpuDemoInternalData.h"
|
||||
#include "Bullet3OpenCL/Initialize/b3OpenCLUtils.h"
|
||||
#include "OpenGLWindow/OpenGLInclude.h"
|
||||
#include "OpenGLWindow/GLInstanceRendererInternalData.h"
|
||||
#include "Bullet3OpenCL/ParallelPrimitives/b3LauncherCL.h"
|
||||
#include "Bullet3OpenCL/RigidBody/b3GpuRigidBodyPipeline.h"
|
||||
#include "Bullet3OpenCL/RigidBody/b3GpuNarrowPhase.h"
|
||||
#include "Bullet3OpenCL/RigidBody/b3Config.h"
|
||||
#include "GpuSoftBodyDemoInternalData.h"
|
||||
#include "Bullet3Collision/BroadPhaseCollision/b3DynamicBvhBroadphase.h"
|
||||
#include "stb_image/stb_image.h"
|
||||
|
||||
static b3KeyboardCallback oldCallback = 0;
|
||||
extern bool gReset;
|
||||
|
||||
#define MSTRINGIFY(A) #A
|
||||
|
||||
static const char* s_rigidBodyKernelString = MSTRINGIFY(
|
||||
|
||||
typedef struct
|
||||
{
|
||||
float4 m_pos;
|
||||
float4 m_quat;
|
||||
float4 m_linVel;
|
||||
float4 m_angVel;
|
||||
unsigned int m_collidableIdx;
|
||||
float m_invMass;
|
||||
float m_restituitionCoeff;
|
||||
float m_frictionCoeff;
|
||||
} Body;
|
||||
|
||||
__kernel void
|
||||
copyTransformsToVBOKernel( __global Body* gBodies, __global float4* posOrnColor, const int numNodes)
|
||||
{
|
||||
int nodeID = get_global_id(0);
|
||||
if( nodeID < numNodes )
|
||||
{
|
||||
posOrnColor[nodeID] = (float4) (gBodies[nodeID].m_pos.xyz,1.0);
|
||||
posOrnColor[nodeID + numNodes] = gBodies[nodeID].m_quat;
|
||||
}
|
||||
}
|
||||
);
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
GpuSoftBodyDemo::GpuSoftBodyDemo()
|
||||
:m_instancingRenderer(0),
|
||||
m_window(0)
|
||||
{
|
||||
m_data = new GpuSoftBodyDemoInternalData;
|
||||
}
|
||||
GpuSoftBodyDemo::~GpuSoftBodyDemo()
|
||||
{
|
||||
|
||||
delete m_data;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
static void PairKeyboardCallback(int key, int state)
|
||||
{
|
||||
if (key=='R' && state)
|
||||
{
|
||||
gReset = true;
|
||||
}
|
||||
|
||||
//b3DefaultKeyboardCallback(key,state);
|
||||
oldCallback(key,state);
|
||||
}
|
||||
|
||||
void GpuSoftBodyDemo::setupScene(const ConstructionInfo& ci)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
void GpuSoftBodyDemo::initPhysics(const ConstructionInfo& ci)
|
||||
{
|
||||
|
||||
if (ci.m_window)
|
||||
{
|
||||
m_window = ci.m_window;
|
||||
oldCallback = ci.m_window->getKeyboardCallback();
|
||||
ci.m_window->setKeyboardCallback(PairKeyboardCallback);
|
||||
|
||||
}
|
||||
|
||||
m_instancingRenderer = ci.m_instancingRenderer;
|
||||
|
||||
initCL(ci.preferredOpenCLDeviceIndex,ci.preferredOpenCLPlatformIndex);
|
||||
|
||||
if (m_clData->m_clContext)
|
||||
{
|
||||
int errNum=0;
|
||||
|
||||
cl_program rbProg=0;
|
||||
m_data->m_copyTransformsToVBOKernel = b3OpenCLUtils::compileCLKernelFromString(m_clData->m_clContext,m_clData->m_clDevice,s_rigidBodyKernelString,"copyTransformsToVBOKernel",&errNum,rbProg);
|
||||
|
||||
b3Config config;
|
||||
b3GpuNarrowPhase* np = new b3GpuNarrowPhase(m_clData->m_clContext,m_clData->m_clDevice,m_clData->m_clQueue,config);
|
||||
b3GpuSapBroadphase* bp = new b3GpuSapBroadphase(m_clData->m_clContext,m_clData->m_clDevice,m_clData->m_clQueue);
|
||||
m_data->m_np = np;
|
||||
m_data->m_bp = bp;
|
||||
b3DynamicBvhBroadphase* broadphaseDbvt = new b3DynamicBvhBroadphase(config.m_maxConvexBodies);
|
||||
|
||||
m_data->m_rigidBodyPipeline = new b3GpuRigidBodyPipeline(m_clData->m_clContext,m_clData->m_clDevice,m_clData->m_clQueue, np, bp,broadphaseDbvt);
|
||||
|
||||
|
||||
setupScene(ci);
|
||||
|
||||
m_data->m_rigidBodyPipeline->writeAllInstancesToGpu();
|
||||
np->writeAllBodiesToGpu();
|
||||
bp->writeAabbsToGpu();
|
||||
|
||||
}
|
||||
|
||||
|
||||
m_instancingRenderer->writeTransforms();
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
void GpuSoftBodyDemo::exitPhysics()
|
||||
{
|
||||
delete m_data->m_instancePosOrnColor;
|
||||
delete m_data->m_rigidBodyPipeline;
|
||||
|
||||
m_window->setKeyboardCallback(oldCallback);
|
||||
|
||||
delete m_data->m_np;
|
||||
m_data->m_np = 0;
|
||||
delete m_data->m_bp;
|
||||
m_data->m_bp = 0;
|
||||
|
||||
exitCL();
|
||||
}
|
||||
|
||||
|
||||
|
||||
struct GraphicsVertex
|
||||
{
|
||||
float pos[4];
|
||||
float normal[3];
|
||||
float texcoord[2];
|
||||
};
|
||||
|
||||
void GpuSoftClothDemo::renderScene()
|
||||
{
|
||||
if (m_data->m_clothShapeIndex>=0 && m_data->m_clothVertices)
|
||||
{
|
||||
GraphicsVertex* cpu_buffer = (GraphicsVertex*)m_data->m_clothVertices;
|
||||
int width = 256;
|
||||
int height=256;
|
||||
static float shift = 0.f;
|
||||
shift+=0.01;
|
||||
if (shift>B3_2_PI)
|
||||
shift-=B3_2_PI;
|
||||
|
||||
int numVertices = 0;
|
||||
// Initial test data for rendering
|
||||
for(int y = 0; y < height; y++)
|
||||
{
|
||||
for(int x = 0; x < width; x++)
|
||||
{
|
||||
|
||||
double coord = b3Sin(x/5.0+shift)*0.01;
|
||||
//coord = sin(y/);
|
||||
|
||||
cpu_buffer[y*width+x].pos[0] = (x/((float)(width-1)))*1;
|
||||
cpu_buffer[y*width+x].pos[1] = coord;
|
||||
cpu_buffer[y*width+x].pos[2] = (y/((float)(height-1)))*1;
|
||||
cpu_buffer[y*width+x].pos[3] = 0.f;
|
||||
|
||||
cpu_buffer[y*width+x].normal[0] = 1;
|
||||
cpu_buffer[y*width+x].normal[1] = 0;
|
||||
cpu_buffer[y*width+x].normal[2] = 0;
|
||||
cpu_buffer[y*width+x].texcoord[0] = 1*x/((float)(width-1));
|
||||
cpu_buffer[y*width+x].texcoord[1] = (1.f-4*y/((float)(height-1)));
|
||||
numVertices++;
|
||||
}
|
||||
}
|
||||
|
||||
m_instancingRenderer->updateShape(m_data->m_clothShapeIndex,m_data->m_clothVertices);
|
||||
}
|
||||
m_instancingRenderer->RenderScene();
|
||||
|
||||
}
|
||||
void GpuSoftBodyDemo::renderScene()
|
||||
{
|
||||
m_instancingRenderer->RenderScene();
|
||||
}
|
||||
|
||||
void GpuSoftBodyDemo::clientMoveAndDisplay()
|
||||
{
|
||||
bool animate=true;
|
||||
int numObjects= m_data->m_rigidBodyPipeline->getNumBodies();
|
||||
//m_instancingRenderer->getInternalData()->m_totalNumInstances;
|
||||
b3Vector4* positions = 0;
|
||||
if (animate && numObjects)
|
||||
{
|
||||
B3_PROFILE("gl2cl");
|
||||
|
||||
if (!m_data->m_instancePosOrnColor)
|
||||
{
|
||||
GLuint vbo = m_instancingRenderer->getInternalData()->m_vbo;
|
||||
int arraySizeInBytes = numObjects * (3)*sizeof(b3Vector4);
|
||||
glBindBuffer(GL_ARRAY_BUFFER, vbo);
|
||||
cl_bool blocking= CL_TRUE;
|
||||
positions= (b3Vector4*)glMapBufferRange( GL_ARRAY_BUFFER,m_instancingRenderer->getMaxShapeCapacity(),arraySizeInBytes, GL_MAP_READ_BIT );//GL_READ_WRITE);//GL_WRITE_ONLY
|
||||
GLint err = glGetError();
|
||||
assert(err==GL_NO_ERROR);
|
||||
m_data->m_instancePosOrnColor = new b3OpenCLArray<b3Vector4>(m_clData->m_clContext,m_clData->m_clQueue);
|
||||
m_data->m_instancePosOrnColor->resize(3*numObjects);
|
||||
m_data->m_instancePosOrnColor->copyFromHostPointer(positions,3*numObjects,0);
|
||||
glUnmapBuffer( GL_ARRAY_BUFFER);
|
||||
err = glGetError();
|
||||
assert(err==GL_NO_ERROR);
|
||||
}
|
||||
}
|
||||
|
||||
{
|
||||
B3_PROFILE("stepSimulation");
|
||||
m_data->m_rigidBodyPipeline->stepSimulation(1./60.f);
|
||||
}
|
||||
|
||||
if (numObjects)
|
||||
{
|
||||
B3_PROFILE("cl2gl_convert");
|
||||
int ciErrNum = 0;
|
||||
cl_mem bodies = m_data->m_rigidBodyPipeline->getBodyBuffer();
|
||||
b3LauncherCL launch(m_clData->m_clQueue,m_data->m_copyTransformsToVBOKernel);
|
||||
launch.setBuffer(bodies);
|
||||
launch.setBuffer(m_data->m_instancePosOrnColor->getBufferCL());
|
||||
launch.setConst(numObjects);
|
||||
launch.launch1D(numObjects);
|
||||
oclCHECKERROR(ciErrNum, CL_SUCCESS);
|
||||
}
|
||||
|
||||
if (animate && numObjects)
|
||||
{
|
||||
B3_PROFILE("cl2gl_upload");
|
||||
GLint err = glGetError();
|
||||
assert(err==GL_NO_ERROR);
|
||||
GLuint vbo = m_instancingRenderer->getInternalData()->m_vbo;
|
||||
int arraySizeInBytes = numObjects * (3)*sizeof(b3Vector4);
|
||||
glBindBuffer(GL_ARRAY_BUFFER, vbo);
|
||||
cl_bool blocking= CL_TRUE;
|
||||
positions= (b3Vector4*)glMapBufferRange( GL_ARRAY_BUFFER,m_instancingRenderer->getMaxShapeCapacity(),arraySizeInBytes, GL_MAP_WRITE_BIT );//GL_READ_WRITE);//GL_WRITE_ONLY
|
||||
err = glGetError();
|
||||
assert(err==GL_NO_ERROR);
|
||||
m_data->m_instancePosOrnColor->copyToHostPointer(positions,3*numObjects,0);
|
||||
glUnmapBuffer( GL_ARRAY_BUFFER);
|
||||
err = glGetError();
|
||||
assert(err==GL_NO_ERROR);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
GpuSoftClothDemo::GpuSoftClothDemo()
|
||||
{
|
||||
}
|
||||
GpuSoftClothDemo::~GpuSoftClothDemo()
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
unsigned char* GpuSoftClothDemo::loadImage(const char* fileName, int& width, int& height, int& n)
|
||||
{
|
||||
unsigned char *data = stbi_load(fileName, &width, &height, &n, 0);
|
||||
if (data)
|
||||
{
|
||||
GLubyte* image=new GLubyte[512*256*4];
|
||||
for(int y=0;y<256;++y)
|
||||
{
|
||||
const int t=y>>4;
|
||||
GLubyte* pi=image+y*512*3;
|
||||
for(int x=0;x<512;++x)
|
||||
{
|
||||
const int s=x>>5;
|
||||
const GLubyte b=180;
|
||||
GLubyte c=b+((s+t&1)&1)*(255-b);
|
||||
pi[0]=pi[1]=pi[2]=c;pi+=3;
|
||||
}
|
||||
}
|
||||
|
||||
{
|
||||
|
||||
for (int i=0;i<width;i++)
|
||||
{
|
||||
for (int j=0;j<height;j++)
|
||||
{
|
||||
int offsetx = (512-width)/2;
|
||||
int offsety = (256-height)/2;
|
||||
|
||||
GLubyte* pi=image+((j+offsety)*512+i+offsetx)*3;
|
||||
const GLubyte* src=data+(j*width+i)*4;
|
||||
pi[0] = src[0];
|
||||
pi[1] = src[1];
|
||||
pi[2] = src[2];
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
width = 512;
|
||||
height = 256;
|
||||
return image;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
void GpuSoftClothDemo::setupScene(const ConstructionInfo& ci)
|
||||
{
|
||||
int width = 256;
|
||||
int height = 256;
|
||||
|
||||
GraphicsVertex* cpu_buffer = new GraphicsVertex[width*height];
|
||||
memset(cpu_buffer, 0, width*height*sizeof(GraphicsVertex));
|
||||
|
||||
|
||||
int numVertices = 0;
|
||||
// Initial test data for rendering
|
||||
for(int y = 0; y < height; y++)
|
||||
{
|
||||
for(int x = 0; x < width; x++)
|
||||
{
|
||||
double coord = b3Sin(x/5.0)*0.01;
|
||||
//coord = sin(y/);
|
||||
|
||||
cpu_buffer[y*width+x].pos[0] = (x/((float)(width-1)))*1;
|
||||
cpu_buffer[y*width+x].pos[1] = coord;
|
||||
cpu_buffer[y*width+x].pos[2] = (y/((float)(height-1)))*1;
|
||||
cpu_buffer[y*width+x].pos[3] = 0.f;
|
||||
|
||||
cpu_buffer[y*width+x].normal[0] = 1;
|
||||
cpu_buffer[y*width+x].normal[1] = 0;
|
||||
cpu_buffer[y*width+x].normal[2] = 0;
|
||||
cpu_buffer[y*width+x].texcoord[0] = 1*x/((float)(width-1));
|
||||
cpu_buffer[y*width+x].texcoord[1] = (1.f-4*y/((float)(height-1)));
|
||||
numVertices++;
|
||||
}
|
||||
}
|
||||
|
||||
int numIndices = 0;
|
||||
|
||||
// Generate and fill index array for rendering
|
||||
int* indices = new int[width*3*2+2 + height*width*3*2];
|
||||
|
||||
for(int y = 0; y < height-1; y++)
|
||||
{
|
||||
for(int x = 0; x < width-1; x++)
|
||||
{
|
||||
// *3 indices/triangle, *2 triangles/quad
|
||||
int baseIndex = (x + y*(width-1))*3*2;
|
||||
indices[baseIndex] = x + y*width;
|
||||
indices[baseIndex+1] = x+1 + y*width;
|
||||
indices[baseIndex+2] = x+width + y*width;
|
||||
|
||||
|
||||
indices[baseIndex+3] = x + 1 + y*width;
|
||||
indices[baseIndex+4] = x+(width+1) + y*width;
|
||||
indices[baseIndex+5] = x+width + y*width;
|
||||
numIndices++;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
int num_barrel_vertices = sizeof(barrel_vertices)/(9*sizeof(float));
|
||||
int num_barrel_indices = sizeof(barrel_indices)/sizeof(int);
|
||||
|
||||
int textureIndex = -1;
|
||||
{
|
||||
int width,height,n;
|
||||
FILE* f = fopen("test.tst","wb");
|
||||
fclose(f);
|
||||
const char* filename = "../../data/bullet_logo.png";
|
||||
const unsigned char* image = loadImage(filename,width,height,n);
|
||||
textureIndex = ci.m_instancingRenderer->registerTexture(image,width,height);
|
||||
}
|
||||
// int shapeIndex = ci.m_instancingRenderer->registerShape(barrel_vertices,num_barrel_vertices,barrel_indices,num_barrel_indices);
|
||||
|
||||
m_data->m_clothVertices = &cpu_buffer[0].pos[0];
|
||||
|
||||
int shapeIndex = ci.m_instancingRenderer->registerShape(&cpu_buffer[0].pos[0],numVertices,indices,numIndices,B3_GL_TRIANGLES,textureIndex);
|
||||
m_data->m_clothShapeIndex = shapeIndex;
|
||||
|
||||
float pos[4] = {0,0,0,0};
|
||||
float orn[4] = {0,0,0,1};
|
||||
float color[4] = {1,1,1,1};
|
||||
float scaling[4] = {10,10,10,1};
|
||||
|
||||
ci.m_instancingRenderer->registerGraphicsInstance(shapeIndex,pos,orn,color,scaling);
|
||||
ci.m_instancingRenderer->setCameraDistance(4);
|
||||
ci.m_instancingRenderer->setCameraTargetPosition(pos);
|
||||
|
||||
}
|
||||
71
Demos3/GpuDemos/softbody/GpuSoftBodyDemo.h
Normal file
71
Demos3/GpuDemos/softbody/GpuSoftBodyDemo.h
Normal file
@@ -0,0 +1,71 @@
|
||||
#ifndef GPU_SOFT_BODY_DEMO_H
|
||||
#define GPU_SOFT_BODY_DEMO_H
|
||||
|
||||
#include "../GpuDemo.h"
|
||||
|
||||
class GpuSoftBodyDemo : public GpuDemo
|
||||
{
|
||||
protected:
|
||||
class GLInstancingRenderer* m_instancingRenderer;
|
||||
class b3gWindowInterface* m_window;
|
||||
|
||||
struct GpuSoftBodyDemoInternalData* m_data;
|
||||
|
||||
public:
|
||||
|
||||
GpuSoftBodyDemo();
|
||||
virtual ~GpuSoftBodyDemo();
|
||||
|
||||
virtual void initPhysics(const ConstructionInfo& ci);
|
||||
|
||||
virtual void setupScene(const ConstructionInfo& ci);
|
||||
|
||||
virtual void destroyScene(){};
|
||||
|
||||
virtual void exitPhysics();
|
||||
|
||||
virtual const char* getName()
|
||||
{
|
||||
return "GPUSOFT";
|
||||
}
|
||||
static GpuDemo* MyCreateFunc()
|
||||
{
|
||||
GpuDemo* demo = new GpuSoftBodyDemo;
|
||||
return demo;
|
||||
}
|
||||
|
||||
virtual void renderScene();
|
||||
|
||||
virtual void clientMoveAndDisplay();
|
||||
|
||||
|
||||
};
|
||||
|
||||
class GpuSoftClothDemo : public GpuSoftBodyDemo
|
||||
{
|
||||
|
||||
public:
|
||||
GpuSoftClothDemo();
|
||||
virtual ~GpuSoftClothDemo();
|
||||
|
||||
unsigned char* loadImage(const char* fileName, int& width, int& height, int& n);
|
||||
|
||||
virtual void setupScene(const ConstructionInfo& ci);
|
||||
|
||||
virtual void renderScene();
|
||||
|
||||
virtual const char* getName()
|
||||
{
|
||||
return "GpuSoftCloth";
|
||||
}
|
||||
|
||||
static GpuDemo* MyCreateFunc()
|
||||
{
|
||||
GpuDemo* demo = new GpuSoftClothDemo;
|
||||
return demo;
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
#endif //GPU_SOFT_BODY_DEMO_H
|
||||
|
||||
34
Demos3/GpuDemos/softbody/GpuSoftBodyDemoInternalData.h
Normal file
34
Demos3/GpuDemos/softbody/GpuSoftBodyDemoInternalData.h
Normal file
@@ -0,0 +1,34 @@
|
||||
#ifndef GPU_SOFTBODY_INTERNAL_DATA_H
|
||||
#define GPU_SOFTBODY_INTERNAL_DATA_H
|
||||
|
||||
#include "Bullet3OpenCL/Initialize/b3OpenCLUtils.h"
|
||||
#include "Bullet3OpenCL/ParallelPrimitives/b3OpenCLArray.h"
|
||||
#include "Bullet3Common/b3Vector3.h"
|
||||
|
||||
struct GpuSoftBodyDemoInternalData
|
||||
{
|
||||
|
||||
cl_kernel m_copyTransformsToVBOKernel;
|
||||
|
||||
b3OpenCLArray<b3Vector4>* m_instancePosOrnColor;
|
||||
|
||||
class b3GpuRigidBodyPipeline* m_rigidBodyPipeline;
|
||||
|
||||
class b3GpuNarrowPhase* m_np;
|
||||
class b3GpuSapBroadphase* m_bp;
|
||||
int m_clothShapeIndex;
|
||||
float* m_clothVertices;
|
||||
|
||||
GpuSoftBodyDemoInternalData()
|
||||
:m_instancePosOrnColor(0),
|
||||
m_copyTransformsToVBOKernel(0), m_rigidBodyPipeline(0),
|
||||
m_np(0),
|
||||
m_bp(0),
|
||||
m_clothShapeIndex(-1),
|
||||
m_clothVertices(0)
|
||||
{
|
||||
}
|
||||
};
|
||||
|
||||
#endif//GPU_SOFTBODY_INTERNAL_DATA_H
|
||||
|
||||
@@ -104,6 +104,7 @@ struct InternalDataRenderer : public GLInstanceRendererInternalData
|
||||
int m_mouseButton;
|
||||
|
||||
GLuint m_defaultTexturehandle;
|
||||
b3AlignedObjectArray<GLuint> m_textureHandles;
|
||||
|
||||
InternalDataRenderer() :
|
||||
m_cameraPosition(b3Vector3(0,0,0)),
|
||||
@@ -687,9 +688,46 @@ int GLInstancingRenderer::registerGraphicsInstance(int shapeIndex, const float*
|
||||
}
|
||||
|
||||
|
||||
int GLInstancingRenderer::registerShape(const float* vertices, int numvertices, const int* indices, int numIndices,int primitiveType)
|
||||
int GLInstancingRenderer::registerTexture(const unsigned char* texels, int width, int height)
|
||||
{
|
||||
int textureIndex = m_data->m_textureHandles.size();
|
||||
const GLubyte* image= (const GLubyte*)texels;
|
||||
GLuint textureHandle;
|
||||
glGenTextures(1,(GLuint*)&textureHandle);
|
||||
glBindTexture(GL_TEXTURE_2D,textureHandle);
|
||||
GLenum err;
|
||||
err = glGetError();
|
||||
assert(err==GL_NO_ERROR);
|
||||
err = glGetError();
|
||||
assert(err==GL_NO_ERROR);
|
||||
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width,height,0,GL_RGB,GL_UNSIGNED_BYTE,image);
|
||||
glGenerateMipmap(GL_TEXTURE_2D);
|
||||
m_data->m_textureHandles.push_back(textureHandle);
|
||||
return textureIndex;
|
||||
}
|
||||
|
||||
void GLInstancingRenderer::updateShape(int shapeIndex, const float* vertices)
|
||||
{
|
||||
b3GraphicsInstance* gfxObj = m_graphicsInstances[shapeIndex];
|
||||
int numvertices = gfxObj->m_numVertices;
|
||||
|
||||
glBindBuffer(GL_ARRAY_BUFFER, m_data->m_vbo);
|
||||
char* dest= (char*)glMapBuffer( GL_ARRAY_BUFFER,GL_WRITE_ONLY);//GL_WRITE_ONLY
|
||||
int vertexStrideInBytes = 9*sizeof(float);
|
||||
int sz = numvertices*vertexStrideInBytes;
|
||||
memcpy(dest+vertexStrideInBytes*gfxObj->m_vertexArrayOffset,vertices,sz);
|
||||
glUnmapBuffer( GL_ARRAY_BUFFER);
|
||||
}
|
||||
|
||||
int GLInstancingRenderer::registerShape(const float* vertices, int numvertices, const int* indices, int numIndices,int primitiveType, int textureId)
|
||||
{
|
||||
b3GraphicsInstance* gfxObj = new b3GraphicsInstance;
|
||||
|
||||
if (textureId>=0)
|
||||
{
|
||||
gfxObj->m_texturehandle = m_data->m_textureHandles[textureId];
|
||||
}
|
||||
|
||||
gfxObj->m_primitiveType = primitiveType;
|
||||
|
||||
if (m_graphicsInstances.size())
|
||||
@@ -1222,6 +1260,8 @@ void GLInstancingRenderer::RenderScene(void)
|
||||
}
|
||||
|
||||
int curOffset = 0;
|
||||
GLuint lastBindTexture = 0;
|
||||
|
||||
|
||||
for (int i=0;i<m_graphicsInstances.size();i++)
|
||||
{
|
||||
@@ -1229,6 +1269,21 @@ void GLInstancingRenderer::RenderScene(void)
|
||||
b3GraphicsInstance* gfxObj = m_graphicsInstances[i];
|
||||
if (gfxObj->m_numGraphicsInstances)
|
||||
{
|
||||
|
||||
GLuint curBindTexture = 0;
|
||||
if (gfxObj->m_texturehandle)
|
||||
curBindTexture = gfxObj->m_texturehandle;
|
||||
else
|
||||
curBindTexture = m_data->m_defaultTexturehandle;
|
||||
|
||||
if (lastBindTexture != curBindTexture)
|
||||
{
|
||||
glBindTexture(GL_TEXTURE_2D,curBindTexture);
|
||||
}
|
||||
lastBindTexture = curBindTexture;
|
||||
|
||||
err = glGetError();
|
||||
assert(err==GL_NO_ERROR);
|
||||
// int myOffset = gfxObj->m_instanceOffset*4*sizeof(float);
|
||||
|
||||
int POSITION_BUFFER_SIZE = (totalNumInstances*sizeof(float)*4);
|
||||
|
||||
@@ -53,9 +53,13 @@ public:
|
||||
void RenderScene(void);
|
||||
void CleanupShaders();
|
||||
|
||||
void updateShape(int shapeIndex, const float* vertices);
|
||||
|
||||
///vertices must be in the format x,y,z, nx,ny,nz, u,v
|
||||
int registerShape(const float* vertices, int numvertices, const int* indices, int numIndices, int primitiveType=B3_GL_TRIANGLES);
|
||||
|
||||
int registerShape(const float* vertices, int numvertices, const int* indices, int numIndices, int primitiveType=B3_GL_TRIANGLES, int textureIndex=-1);
|
||||
|
||||
int registerTexture(const unsigned char* texels, int width, int height);
|
||||
|
||||
///position x,y,z, quaternion x,y,z,w, color r,g,b,a, scaling x,y,z
|
||||
int registerGraphicsInstance(int shapeIndex, const float* position, const float* quaternion, const float* color, const float* scaling);
|
||||
|
||||
|
||||
4341
btgui/stb_image/stb_image.cpp
Normal file
4341
btgui/stb_image/stb_image.cpp
Normal file
File diff suppressed because it is too large
Load Diff
332
btgui/stb_image/stb_image.h
Normal file
332
btgui/stb_image/stb_image.h
Normal file
@@ -0,0 +1,332 @@
|
||||
/* stbi-1.33 - public domain JPEG/PNG reader - http://nothings.org/stb_image.c
|
||||
when you control the images you're loading
|
||||
no warranty implied; use at your own risk
|
||||
|
||||
QUICK NOTES:
|
||||
Primarily of interest to game developers and other people who can
|
||||
avoid problematic images and only need the trivial interface
|
||||
|
||||
JPEG baseline (no JPEG progressive)
|
||||
PNG 8-bit only
|
||||
|
||||
TGA (not sure what subset, if a subset)
|
||||
BMP non-1bpp, non-RLE
|
||||
PSD (composited view only, no extra channels)
|
||||
|
||||
GIF (*comp always reports as 4-channel)
|
||||
HDR (radiance rgbE format)
|
||||
PIC (Softimage PIC)
|
||||
|
||||
- decode from memory or through FILE (define STBI_NO_STDIO to remove code)
|
||||
- decode from arbitrary I/O callbacks
|
||||
- overridable dequantizing-IDCT, YCbCr-to-RGB conversion (define STBI_SIMD)
|
||||
|
||||
Latest revisions:
|
||||
1.33 (2011-07-14) minor fixes suggested by Dave Moore
|
||||
1.32 (2011-07-13) info support for all filetypes (SpartanJ)
|
||||
1.31 (2011-06-19) a few more leak fixes, bug in PNG handling (SpartanJ)
|
||||
1.30 (2011-06-11) added ability to load files via io callbacks (Ben Wenger)
|
||||
1.29 (2010-08-16) various warning fixes from Aurelien Pocheville
|
||||
1.28 (2010-08-01) fix bug in GIF palette transparency (SpartanJ)
|
||||
1.27 (2010-08-01) cast-to-uint8 to fix warnings (Laurent Gomila)
|
||||
allow trailing 0s at end of image data (Laurent Gomila)
|
||||
1.26 (2010-07-24) fix bug in file buffering for PNG reported by SpartanJ
|
||||
|
||||
See end of file for full revision history.
|
||||
|
||||
TODO:
|
||||
stbi_info support for BMP,PSD,HDR,PIC
|
||||
|
||||
|
||||
============================ Contributors =========================
|
||||
|
||||
Image formats Optimizations & bugfixes
|
||||
Sean Barrett (jpeg, png, bmp) Fabian "ryg" Giesen
|
||||
Nicolas Schulz (hdr, psd)
|
||||
Jonathan Dummer (tga) Bug fixes & warning fixes
|
||||
Jean-Marc Lienher (gif) Marc LeBlanc
|
||||
Tom Seddon (pic) Christpher Lloyd
|
||||
Thatcher Ulrich (psd) Dave Moore
|
||||
Won Chun
|
||||
the Horde3D community
|
||||
Extensions, features Janez Zemva
|
||||
Jetro Lauha (stbi_info) Jonathan Blow
|
||||
James "moose2000" Brown (iPhone PNG) Laurent Gomila
|
||||
Ben "Disch" Wenger (io callbacks) Aruelien Pocheville
|
||||
Martin "SpartanJ" Golini Ryamond Barbiero
|
||||
David Woo
|
||||
|
||||
|
||||
If your name should be here but isn't, let Sean know.
|
||||
|
||||
*/
|
||||
|
||||
#ifndef STBI_INCLUDE_STB_IMAGE_H
|
||||
#define STBI_INCLUDE_STB_IMAGE_H
|
||||
|
||||
// To get a header file for this, either cut and paste the header,
|
||||
// or create stb_image.h, #define STBI_HEADER_FILE_ONLY, and
|
||||
// then include stb_image.c from it.
|
||||
|
||||
//// begin header file ////////////////////////////////////////////////////
|
||||
//
|
||||
// Limitations:
|
||||
// - no jpeg progressive support
|
||||
// - non-HDR formats support 8-bit samples only (jpeg, png)
|
||||
// - no delayed line count (jpeg) -- IJG doesn't support either
|
||||
// - no 1-bit BMP
|
||||
// - GIF always returns *comp=4
|
||||
//
|
||||
// Basic usage (see HDR discussion below):
|
||||
// int x,y,n;
|
||||
// unsigned char *data = stbi_load(filename, &x, &y, &n, 0);
|
||||
// // ... process data if not NULL ...
|
||||
// // ... x = width, y = height, n = # 8-bit components per pixel ...
|
||||
// // ... replace '0' with '1'..'4' to force that many components per pixel
|
||||
// // ... but 'n' will always be the number that it would have been if you said 0
|
||||
// stbi_image_free(data)
|
||||
//
|
||||
// Standard parameters:
|
||||
// int *x -- outputs image width in pixels
|
||||
// int *y -- outputs image height in pixels
|
||||
// int *comp -- outputs # of image components in image file
|
||||
// int req_comp -- if non-zero, # of image components requested in result
|
||||
//
|
||||
// The return value from an image loader is an 'unsigned char *' which points
|
||||
// to the pixel data. The pixel data consists of *y scanlines of *x pixels,
|
||||
// with each pixel consisting of N interleaved 8-bit components; the first
|
||||
// pixel pointed to is top-left-most in the image. There is no padding between
|
||||
// image scanlines or between pixels, regardless of format. The number of
|
||||
// components N is 'req_comp' if req_comp is non-zero, or *comp otherwise.
|
||||
// If req_comp is non-zero, *comp has the number of components that _would_
|
||||
// have been output otherwise. E.g. if you set req_comp to 4, you will always
|
||||
// get RGBA output, but you can check *comp to easily see if it's opaque.
|
||||
//
|
||||
// An output image with N components has the following components interleaved
|
||||
// in this order in each pixel:
|
||||
//
|
||||
// N=#comp components
|
||||
// 1 grey
|
||||
// 2 grey, alpha
|
||||
// 3 red, green, blue
|
||||
// 4 red, green, blue, alpha
|
||||
//
|
||||
// If image loading fails for any reason, the return value will be NULL,
|
||||
// and *x, *y, *comp will be unchanged. The function stbi_failure_reason()
|
||||
// can be queried for an extremely brief, end-user unfriendly explanation
|
||||
// of why the load failed. Define STBI_NO_FAILURE_STRINGS to avoid
|
||||
// compiling these strings at all, and STBI_FAILURE_USERMSG to get slightly
|
||||
// more user-friendly ones.
|
||||
//
|
||||
// Paletted PNG, BMP, GIF, and PIC images are automatically depalettized.
|
||||
//
|
||||
// ===========================================================================
|
||||
//
|
||||
// iPhone PNG support:
|
||||
//
|
||||
// By default we convert iphone-formatted PNGs back to RGB; nominally they
|
||||
// would silently load as BGR, except the existing code should have just
|
||||
// failed on such iPhone PNGs. But you can disable this conversion by
|
||||
// by calling stbi_convert_iphone_png_to_rgb(0), in which case
|
||||
// you will always just get the native iphone "format" through.
|
||||
//
|
||||
// Call stbi_set_unpremultiply_on_load(1) as well to force a divide per
|
||||
// pixel to remove any premultiplied alpha *only* if the image file explicitly
|
||||
// says there's premultiplied data (currently only happens in iPhone images,
|
||||
// and only if iPhone convert-to-rgb processing is on).
|
||||
//
|
||||
// ===========================================================================
|
||||
//
|
||||
// HDR image support (disable by defining STBI_NO_HDR)
|
||||
//
|
||||
// stb_image now supports loading HDR images in general, and currently
|
||||
// the Radiance .HDR file format, although the support is provided
|
||||
// generically. You can still load any file through the existing interface;
|
||||
// if you attempt to load an HDR file, it will be automatically remapped to
|
||||
// LDR, assuming gamma 2.2 and an arbitrary scale factor defaulting to 1;
|
||||
// both of these constants can be reconfigured through this interface:
|
||||
//
|
||||
// stbi_hdr_to_ldr_gamma(2.2f);
|
||||
// stbi_hdr_to_ldr_scale(1.0f);
|
||||
//
|
||||
// (note, do not use _inverse_ constants; stbi_image will invert them
|
||||
// appropriately).
|
||||
//
|
||||
// Additionally, there is a new, parallel interface for loading files as
|
||||
// (linear) floats to preserve the full dynamic range:
|
||||
//
|
||||
// float *data = stbi_loadf(filename, &x, &y, &n, 0);
|
||||
//
|
||||
// If you load LDR images through this interface, those images will
|
||||
// be promoted to floating point values, run through the inverse of
|
||||
// constants corresponding to the above:
|
||||
//
|
||||
// stbi_ldr_to_hdr_scale(1.0f);
|
||||
// stbi_ldr_to_hdr_gamma(2.2f);
|
||||
//
|
||||
// Finally, given a filename (or an open file or memory block--see header
|
||||
// file for details) containing image data, you can query for the "most
|
||||
// appropriate" interface to use (that is, whether the image is HDR or
|
||||
// not), using:
|
||||
//
|
||||
// stbi_is_hdr(char *filename);
|
||||
//
|
||||
// ===========================================================================
|
||||
//
|
||||
// I/O callbacks
|
||||
//
|
||||
// I/O callbacks allow you to read from arbitrary sources, like packaged
|
||||
// files or some other source. Data read from callbacks are processed
|
||||
// through a small internal buffer (currently 128 bytes) to try to reduce
|
||||
// overhead.
|
||||
//
|
||||
// The three functions you must define are "read" (reads some bytes of data),
|
||||
// "skip" (skips some bytes of data), "eof" (reports if the stream is at the end).
|
||||
|
||||
|
||||
#ifndef STBI_NO_STDIO
|
||||
|
||||
#if defined(_MSC_VER) && _MSC_VER >= 0x1400
|
||||
#define _CRT_SECURE_NO_WARNINGS // suppress bogus warnings about fopen()
|
||||
#endif
|
||||
|
||||
#include <stdio.h>
|
||||
#endif
|
||||
|
||||
#define STBI_VERSION 1
|
||||
|
||||
enum
|
||||
{
|
||||
STBI_default = 0, // only used for req_comp
|
||||
|
||||
STBI_grey = 1,
|
||||
STBI_grey_alpha = 2,
|
||||
STBI_rgb = 3,
|
||||
STBI_rgb_alpha = 4
|
||||
};
|
||||
|
||||
typedef unsigned char stbi_uc;
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
//////////////////////////////////////////////////////////////////////////////
|
||||
//
|
||||
// PRIMARY API - works on images of any type
|
||||
//
|
||||
|
||||
//
|
||||
// load image by filename, open file, or memory buffer
|
||||
//
|
||||
|
||||
extern stbi_uc *stbi_load_from_memory(stbi_uc const *buffer, int len, int *x, int *y, int *comp, int req_comp);
|
||||
|
||||
#ifndef STBI_NO_STDIO
|
||||
extern stbi_uc *stbi_load (char const *filename, int *x, int *y, int *comp, int req_comp);
|
||||
extern stbi_uc *stbi_load_from_file (FILE *f, int *x, int *y, int *comp, int req_comp);
|
||||
// for stbi_load_from_file, file pointer is left pointing immediately after image
|
||||
#endif
|
||||
|
||||
typedef struct
|
||||
{
|
||||
int (*read) (void *user,char *data,int size); // fill 'data' with 'size' bytes. return number of bytes actually read
|
||||
void (*skip) (void *user,unsigned n); // skip the next 'n' bytes
|
||||
int (*eof) (void *user); // returns nonzero if we are at end of file/data
|
||||
} stbi_io_callbacks;
|
||||
|
||||
extern stbi_uc *stbi_load_from_callbacks (stbi_io_callbacks const *clbk, void *user, int *x, int *y, int *comp, int req_comp);
|
||||
|
||||
#ifndef STBI_NO_HDR
|
||||
extern float *stbi_loadf_from_memory(stbi_uc const *buffer, int len, int *x, int *y, int *comp, int req_comp);
|
||||
|
||||
#ifndef STBI_NO_STDIO
|
||||
extern float *stbi_loadf (char const *filename, int *x, int *y, int *comp, int req_comp);
|
||||
extern float *stbi_loadf_from_file (FILE *f, int *x, int *y, int *comp, int req_comp);
|
||||
#endif
|
||||
|
||||
extern float *stbi_loadf_from_callbacks (stbi_io_callbacks const *clbk, void *user, int *x, int *y, int *comp, int req_comp);
|
||||
|
||||
extern void stbi_hdr_to_ldr_gamma(float gamma);
|
||||
extern void stbi_hdr_to_ldr_scale(float scale);
|
||||
|
||||
extern void stbi_ldr_to_hdr_gamma(float gamma);
|
||||
extern void stbi_ldr_to_hdr_scale(float scale);
|
||||
#endif // STBI_NO_HDR
|
||||
|
||||
// stbi_is_hdr is always defined
|
||||
extern int stbi_is_hdr_from_callbacks(stbi_io_callbacks const *clbk, void *user);
|
||||
extern int stbi_is_hdr_from_memory(stbi_uc const *buffer, int len);
|
||||
#ifndef STBI_NO_STDIO
|
||||
extern int stbi_is_hdr (char const *filename);
|
||||
extern int stbi_is_hdr_from_file(FILE *f);
|
||||
#endif // STBI_NO_STDIO
|
||||
|
||||
|
||||
// get a VERY brief reason for failure
|
||||
// NOT THREADSAFE
|
||||
extern const char *stbi_failure_reason (void);
|
||||
|
||||
// free the loaded image -- this is just free()
|
||||
extern void stbi_image_free (void *retval_from_stbi_load);
|
||||
|
||||
// get image dimensions & components without fully decoding
|
||||
extern int stbi_info_from_memory(stbi_uc const *buffer, int len, int *x, int *y, int *comp);
|
||||
extern int stbi_info_from_callbacks(stbi_io_callbacks const *clbk, void *user, int *x, int *y, int *comp);
|
||||
|
||||
#ifndef STBI_NO_STDIO
|
||||
extern int stbi_info (char const *filename, int *x, int *y, int *comp);
|
||||
extern int stbi_info_from_file (FILE *f, int *x, int *y, int *comp);
|
||||
|
||||
#endif
|
||||
|
||||
|
||||
|
||||
// for image formats that explicitly notate that they have premultiplied alpha,
|
||||
// we just return the colors as stored in the file. set this flag to force
|
||||
// unpremultiplication. results are undefined if the unpremultiply overflow.
|
||||
extern void stbi_set_unpremultiply_on_load(int flag_true_if_should_unpremultiply);
|
||||
|
||||
// indicate whether we should process iphone images back to canonical format,
|
||||
// or just pass them through "as-is"
|
||||
extern void stbi_convert_iphone_png_to_rgb(int flag_true_if_should_convert);
|
||||
|
||||
|
||||
// ZLIB client - used by PNG, available for other purposes
|
||||
|
||||
extern char *stbi_zlib_decode_malloc_guesssize(const char *buffer, int len, int initial_size, int *outlen);
|
||||
extern char *stbi_zlib_decode_malloc(const char *buffer, int len, int *outlen);
|
||||
extern int stbi_zlib_decode_buffer(char *obuffer, int olen, const char *ibuffer, int ilen);
|
||||
|
||||
extern char *stbi_zlib_decode_noheader_malloc(const char *buffer, int len, int *outlen);
|
||||
extern int stbi_zlib_decode_noheader_buffer(char *obuffer, int olen, const char *ibuffer, int ilen);
|
||||
|
||||
|
||||
// define faster low-level operations (typically SIMD support)
|
||||
#ifdef STBI_SIMD
|
||||
typedef void (*stbi_idct_8x8)(stbi_uc *out, int out_stride, short data[64], unsigned short *dequantize);
|
||||
// compute an integer IDCT on "input"
|
||||
// input[x] = data[x] * dequantize[x]
|
||||
// write results to 'out': 64 samples, each run of 8 spaced by 'out_stride'
|
||||
// CLAMP results to 0..255
|
||||
typedef void (*stbi_YCbCr_to_RGB_run)(stbi_uc *output, stbi_uc const *y, stbi_uc const *cb, stbi_uc const *cr, int count, int step);
|
||||
// compute a conversion from YCbCr to RGB
|
||||
// 'count' pixels
|
||||
// write pixels to 'output'; each pixel is 'step' bytes (either 3 or 4; if 4, write '255' as 4th), order R,G,B
|
||||
// y: Y input channel
|
||||
// cb: Cb input channel; scale/biased to be 0..255
|
||||
// cr: Cr input channel; scale/biased to be 0..255
|
||||
|
||||
extern void stbi_install_idct(stbi_idct_8x8 func);
|
||||
extern void stbi_install_YCbCr_to_RGB(stbi_YCbCr_to_RGB_run func);
|
||||
#endif // STBI_SIMD
|
||||
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
//
|
||||
//
|
||||
//// end header file /////////////////////////////////////////////////////
|
||||
#endif // STBI_INCLUDE_STB_IMAGE_H
|
||||
BIN
data/bullet_logo.png
Normal file
BIN
data/bullet_logo.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 3.3 KiB |
Reference in New Issue
Block a user