* Adjustment: Update Bullet version to 3.24.

This commit is contained in:
Robert MacGregor 2022-06-27 10:01:08 -04:00
parent 35de012ee7
commit 4a3f31df2a
6148 changed files with 2112532 additions and 56873 deletions

View file

@ -0,0 +1,154 @@
#include "CoordinateSystemDemo.h"
#include "../CommonInterfaces/CommonGraphicsAppInterface.h"
#include "../CommonInterfaces/CommonRenderInterface.h"
#include "../CommonInterfaces/CommonExampleInterface.h"
#include "LinearMath/btTransform.h"
#include "../CommonInterfaces/CommonGUIHelperInterface.h"
///quick demo showing the right-handed coordinate system and positive rotations around each axis
class CoordinateSystemDemo : public CommonExampleInterface
{
CommonGraphicsApp* m_app;
float m_x;
float m_y;
float m_z;
public:
CoordinateSystemDemo(CommonGraphicsApp* app)
: m_app(app),
m_x(0),
m_y(0),
m_z(0)
{
m_app->setUpAxis(2);
{
int boxId = m_app->registerCubeShape(0.1, 0.1, 0.1);
btVector3 pos(0, 0, 0);
btQuaternion orn(0, 0, 0, 1);
btVector4 color(0.3, 0.3, 0.3, 1);
btVector3 scaling(1, 1, 1);
m_app->m_renderer->registerGraphicsInstance(boxId, pos, orn, color, scaling);
}
m_app->m_renderer->writeTransforms();
}
virtual ~CoordinateSystemDemo()
{
}
virtual void initPhysics()
{
}
virtual void exitPhysics()
{
}
virtual void stepSimulation(float deltaTime)
{
m_x += 0.01f;
m_y += 0.01f;
m_z += 0.01f;
}
virtual void renderScene()
{
m_app->m_renderer->renderScene();
m_app->drawText3D("X", 1, 0, 0, 1);
m_app->drawText3D("Y", 0, 1, 0, 1);
m_app->drawText3D("Z", 0, 0, 1, 1);
}
virtual void drawArc(const btVector3& center, const btVector3& normal, const btVector3& axis, btScalar radiusA, btScalar radiusB, btScalar minAngle, btScalar maxAngle,
const btVector3& color, bool drawSect, btScalar stepDegrees = btScalar(10.f))
{
btScalar lineWidth = 3;
const btVector3& vx = axis;
btVector3 vy = normal.cross(axis);
btScalar step = stepDegrees * SIMD_RADS_PER_DEG;
int nSteps = (int)btFabs((maxAngle - minAngle) / step);
if (!nSteps) nSteps = 1;
btVector3 prev = center + radiusA * vx * btCos(minAngle) + radiusB * vy * btSin(minAngle);
if (drawSect)
{
m_app->m_renderer->drawLine(center, prev, color, lineWidth);
}
for (int i = 1; i <= nSteps; i++)
{
btScalar angle = minAngle + (maxAngle - minAngle) * btScalar(i) / btScalar(nSteps);
btVector3 next = center + radiusA * vx * btCos(angle) + radiusB * vy * btSin(angle);
m_app->m_renderer->drawLine(prev, next, color, lineWidth);
prev = next;
}
if (drawSect)
{
m_app->m_renderer->drawLine(center, prev, color, lineWidth);
}
}
virtual void physicsDebugDraw(int debugDrawFlags)
{
btVector3 xUnit(1, 0, 0);
btVector3 yUnit(0, 1, 0);
btVector3 zUnit(0, 0, 1);
btScalar lineWidth = 3;
btQuaternion rotAroundX(xUnit, m_x);
btQuaternion rotAroundY(yUnit, m_y);
btQuaternion rotAroundZ(zUnit, m_z);
btScalar radius = 0.5;
btVector3 toX = radius * quatRotate(rotAroundX, yUnit);
btVector3 toY = radius * quatRotate(rotAroundY, xUnit);
btVector3 toZ = radius * quatRotate(rotAroundZ, xUnit);
m_app->m_renderer->drawLine(xUnit + toX + quatRotate(rotAroundX, btVector3(0, 0.1, -0.2)), xUnit + toX, xUnit, lineWidth);
m_app->m_renderer->drawLine(xUnit + toX + quatRotate(rotAroundX, btVector3(0, -0.2, -0.2)), xUnit + toX, xUnit, lineWidth);
//draw the letter 'x' on the x-axis
//m_app->m_renderer->drawLine(xUnit-0.1*zUnit+0.1*yUnit,xUnit+0.1*zUnit-0.1*yUnit,xUnit,lineWidth);
//m_app->m_renderer->drawLine(xUnit+0.1*zUnit+0.1*yUnit,xUnit-0.1*zUnit-0.1*yUnit,xUnit,lineWidth);
m_app->m_renderer->drawLine(xUnit + toX + quatRotate(rotAroundX, btVector3(0, -0.2, -0.2)), xUnit + toX, xUnit, lineWidth);
m_app->m_renderer->drawLine(yUnit + toY + quatRotate(rotAroundY, btVector3(-0.2, 0, 0.2)), yUnit + toY, yUnit, lineWidth);
m_app->m_renderer->drawLine(yUnit + toY + quatRotate(rotAroundY, btVector3(0.1, 0, 0.2)), yUnit + toY, yUnit, lineWidth);
m_app->m_renderer->drawLine(zUnit + toZ + quatRotate(rotAroundZ, btVector3(0.1, -0.2, 0)), zUnit + toZ, zUnit, lineWidth);
m_app->m_renderer->drawLine(zUnit + toZ + quatRotate(rotAroundZ, btVector3(-0.2, -0.2, 0)), zUnit + toZ, zUnit, lineWidth);
drawArc(xUnit, xUnit, toX.normalized(), radius, radius, 0.4, SIMD_2_PI, xUnit, false);
drawArc(yUnit, yUnit, toY.normalized(), radius, radius, 0.4, SIMD_2_PI, yUnit, false);
drawArc(zUnit, zUnit, toZ.normalized(), radius, radius, 0.4, SIMD_2_PI, zUnit, false);
}
virtual bool mouseMoveCallback(float x, float y)
{
return false;
}
virtual bool mouseButtonCallback(int button, int state, float x, float y)
{
return false;
}
virtual bool keyboardCallback(int key, int state)
{
return false;
}
virtual void resetCamera()
{
float dist = 3.5;
float pitch = -32;
float yaw = 136;
float targetPos[3] = {0, 0, 0};
if (m_app->m_renderer && m_app->m_renderer->getActiveCamera())
{
m_app->m_renderer->getActiveCamera()->setCameraDistance(dist);
m_app->m_renderer->getActiveCamera()->setCameraPitch(pitch);
m_app->m_renderer->getActiveCamera()->setCameraYaw(yaw);
m_app->m_renderer->getActiveCamera()->setCameraTargetPosition(targetPos[0], targetPos[1], targetPos[2]);
}
}
};
CommonExampleInterface* CoordinateSystemCreateFunc(struct CommonExampleOptions& options)
{
return new CoordinateSystemDemo(options.m_guiHelper->getAppInterface());
}

View file

@ -0,0 +1,6 @@
#ifndef COORDINATE_SYSTEM_DEMO_H
#define COORDINATE_SYSTEM_DEMO_H
class CommonExampleInterface* CoordinateSystemCreateFunc(struct CommonExampleOptions& options);
#endif //COORDINATE_SYSTEM_DEMO_H

View file

@ -0,0 +1,130 @@
#include "DynamicTexturedCubeDemo.h"
#include "Bullet3Common/b3Logging.h"
#include "../CommonInterfaces/CommonGraphicsAppInterface.h"
#include "Bullet3Common/b3Quaternion.h"
#include "Bullet3Common/b3AlignedObjectArray.h"
#include "../CommonInterfaces/CommonRenderInterface.h"
#include "../CommonInterfaces/CommonExampleInterface.h"
#include "../CommonInterfaces/CommonGUIHelperInterface.h"
#include "GwenGUISupport/GraphingTexture.h"
#include "../CommonInterfaces/Common2dCanvasInterface.h"
#include "../RenderingExamples/TimeSeriesCanvas.h"
#include "../RenderingExamples/TimeSeriesFontData.h"
#include "../Importers/ImportMeshUtility/b3ImportMeshUtility.h"
#include "../OpenGLWindow/GLInstanceGraphicsShape.h"
#include "TinyVRGui.h"
#include "../CommonInterfaces/CommonParameterInterface.h"
class DynamicTexturedCubeDemo : public CommonExampleInterface
{
CommonGraphicsApp* m_app;
b3AlignedObjectArray<int> m_movingInstances;
TinyVRGui* m_tinyVrGUI;
enum
{
numCubesX = 1,
numCubesY = 1
};
public:
DynamicTexturedCubeDemo(CommonGraphicsApp* app)
: m_app(app),
m_tinyVrGUI(0)
{
m_app->setUpAxis(2);
{
b3Vector3 extents = b3MakeVector3(100, 100, 100);
extents[m_app->getUpAxis()] = 1;
int xres = 20;
int yres = 20;
b3Vector4 color0 = b3MakeVector4(0.1, 0.1, 0.5, 1);
b3Vector4 color1 = b3MakeVector4(0.6, 0.6, 0.6, 1);
m_app->registerGrid(xres, yres, color0, color1);
}
ComboBoxParams comboParams;
comboParams.m_comboboxId = 0;
comboParams.m_numItems = 0;
comboParams.m_startItem = 0;
comboParams.m_callback = 0; //MyComboBoxCallback;
comboParams.m_userPointer = 0; //this;
m_tinyVrGUI = new TinyVRGui(comboParams, m_app->m_renderer);
m_tinyVrGUI->init();
m_app->m_renderer->writeTransforms();
}
virtual ~DynamicTexturedCubeDemo()
{
delete m_tinyVrGUI;
}
virtual void physicsDebugDraw(int debugDrawMode)
{
}
virtual void initPhysics()
{
}
virtual void exitPhysics()
{
}
virtual void stepSimulation(float deltaTime)
{
static b3Transform tr = b3Transform::getIdentity();
static b3Scalar t = 0.f;
t += deltaTime;
tr.setOrigin(b3MakeVector3(0., 0., 2.) + b3MakeVector3(0., 0., 0.02 * b3Sin(t)));
m_tinyVrGUI->tick(deltaTime, tr);
m_app->m_renderer->writeTransforms();
}
virtual void renderScene()
{
m_app->m_renderer->renderScene();
}
virtual void physicsDebugDraw()
{
}
virtual bool mouseMoveCallback(float x, float y)
{
return false;
}
virtual bool mouseButtonCallback(int button, int state, float x, float y)
{
return false;
}
virtual bool keyboardCallback(int key, int state)
{
return false;
}
virtual void resetCamera()
{
float dist = 1.15;
float pitch = -33.7;
float yaw = 396;
float targetPos[3] = {-0.5, 0.7, 1.45};
if (m_app->m_renderer && m_app->m_renderer->getActiveCamera())
{
m_app->m_renderer->getActiveCamera()->setCameraDistance(dist);
m_app->m_renderer->getActiveCamera()->setCameraPitch(pitch);
m_app->m_renderer->getActiveCamera()->setCameraYaw(yaw);
m_app->m_renderer->getActiveCamera()->setCameraTargetPosition(targetPos[0], targetPos[1], targetPos[2]);
}
}
};
class CommonExampleInterface* DynamicTexturedCubeDemoCreateFunc(struct CommonExampleOptions& options)
{
return new DynamicTexturedCubeDemo(options.m_guiHelper->getAppInterface());
}

View file

@ -0,0 +1,6 @@
#ifndef DYNAMIC_TEXTURED_CUBE_DEMO_H
#define DYNAMIC_TEXTURED_CUBE_DEMO_H
class CommonExampleInterface* DynamicTexturedCubeDemoCreateFunc(struct CommonExampleOptions& options);
#endif //DYNAMIC_TEXTURED_CUBE_DEMO_H

View file

@ -0,0 +1,361 @@
#include "RaytracerSetup.h"
#include "../CommonInterfaces/CommonGraphicsAppInterface.h"
#include "Bullet3Common/b3Quaternion.h"
#include "Bullet3Common/b3AlignedObjectArray.h"
#include "../CommonInterfaces/CommonRenderInterface.h"
#include "../CommonInterfaces/Common2dCanvasInterface.h"
//#include "BulletCollision/NarrowPhaseCollision/btVoronoiSimplexSolver.h"
#include "BulletCollision/NarrowPhaseCollision/btSubSimplexConvexCast.h"
//#include "BulletCollision/NarrowPhaseCollision/btGjkConvexCast.h"
//#include "BulletCollision/NarrowPhaseCollision/btContinuousConvexCollision.h"
#include "../CommonInterfaces/CommonExampleInterface.h"
#include "LinearMath/btAlignedObjectArray.h"
#include "btBulletCollisionCommon.h"
#include "../CommonInterfaces/CommonGUIHelperInterface.h"
struct RaytracerPhysicsSetup : public CommonExampleInterface
{
struct CommonGraphicsApp* m_app;
struct RaytracerInternalData* m_internalData;
RaytracerPhysicsSetup(struct CommonGraphicsApp* app);
virtual ~RaytracerPhysicsSetup();
virtual void initPhysics();
virtual void exitPhysics();
virtual void stepSimulation(float deltaTime);
virtual void physicsDebugDraw(int debugFlags);
virtual void syncPhysicsToGraphics(struct GraphicsPhysicsBridge& gfxBridge);
///worldRaytest performs a ray versus all objects in a collision world, returning true is a hit is found (filling in worldNormal and worldHitPoint)
bool worldRaytest(const btVector3& rayFrom, const btVector3& rayTo, btVector3& worldNormal, btVector3& worldHitPoint);
///singleObjectRaytest performs a ray versus one collision shape, returning true is a hit is found (filling in worldNormal and worldHitPoint)
bool singleObjectRaytest(const btVector3& rayFrom, const btVector3& rayTo, btVector3& worldNormal, btVector3& worldHitPoint);
///lowlevelRaytest performs a ray versus convex shape, returning true is a hit is found (filling in worldNormal and worldHitPoint)
bool lowlevelRaytest(const btVector3& rayFrom, const btVector3& rayTo, btVector3& worldNormal, btVector3& worldHitPoint);
virtual bool mouseMoveCallback(float x, float y);
virtual bool mouseButtonCallback(int button, int state, float x, float y);
virtual bool keyboardCallback(int key, int state);
virtual void renderScene()
{
}
};
struct RaytracerInternalData
{
int m_canvasIndex;
struct Common2dCanvasInterface* m_canvas;
int m_width;
int m_height;
btAlignedObjectArray<btConvexShape*> m_shapePtr;
btAlignedObjectArray<btTransform> m_transforms;
btVoronoiSimplexSolver m_simplexSolver;
btScalar m_pitch;
btScalar m_roll;
btScalar m_yaw;
RaytracerInternalData()
: m_canvasIndex(-1),
m_canvas(0),
#ifdef _DEBUG
m_width(64),
m_height(64),
#else
m_width(128),
m_height(128),
#endif
m_pitch(0),
m_roll(0),
m_yaw(0)
{
btConeShape* cone = new btConeShape(1, 1);
btSphereShape* sphere = new btSphereShape(1);
btBoxShape* box = new btBoxShape(btVector3(1, 1, 1));
m_shapePtr.push_back(cone);
m_shapePtr.push_back(sphere);
m_shapePtr.push_back(box);
updateTransforms();
}
void updateTransforms()
{
int numObjects = m_shapePtr.size();
m_transforms.resize(numObjects);
for (int i = 0; i < numObjects; i++)
{
m_transforms[i].setIdentity();
btVector3 pos(0.f, 0.f, -(2.5 * numObjects * 0.5) + i * 2.5f);
m_transforms[i].setIdentity();
m_transforms[i].setOrigin(pos);
btQuaternion orn;
if (i < 2)
{
orn.setEuler(m_yaw, m_pitch, m_roll);
m_transforms[i].setRotation(orn);
}
}
m_pitch += 0.005f;
m_yaw += 0.01f;
}
};
RaytracerPhysicsSetup::RaytracerPhysicsSetup(struct CommonGraphicsApp* app)
{
m_app = app;
m_internalData = new RaytracerInternalData;
}
RaytracerPhysicsSetup::~RaytracerPhysicsSetup()
{
delete m_internalData;
}
void RaytracerPhysicsSetup::initPhysics()
{
//request a visual bitma/texture we can render to
m_internalData->m_canvas = m_app->m_2dCanvasInterface;
if (m_internalData->m_canvas)
{
m_internalData->m_canvasIndex = m_internalData->m_canvas->createCanvas("raytracer", m_internalData->m_width, m_internalData->m_height, 15, 55);
for (int i = 0; i < m_internalData->m_width; i++)
{
for (int j = 0; j < m_internalData->m_height; j++)
{
unsigned char red = 255;
unsigned char green = 255;
unsigned char blue = 255;
unsigned char alpha = 255;
m_internalData->m_canvas->setPixel(m_internalData->m_canvasIndex, i, j, red, green, blue, alpha);
}
}
m_internalData->m_canvas->refreshImageData(m_internalData->m_canvasIndex);
//int bitmapId = gfxBridge.createRenderBitmap(width,height);
}
}
///worldRaytest performs a ray versus all objects in a collision world, returning true is a hit is found (filling in worldNormal and worldHitPoint)
bool RaytracerPhysicsSetup::worldRaytest(const btVector3& rayFrom, const btVector3& rayTo, btVector3& worldNormal, btVector3& worldHitPoint)
{
return false;
}
///singleObjectRaytest performs a ray versus one collision shape, returning true is a hit is found (filling in worldNormal and worldHitPoint)
bool RaytracerPhysicsSetup::singleObjectRaytest(const btVector3& rayFrom, const btVector3& rayTo, btVector3& worldNormal, btVector3& worldHitPoint)
{
return false;
}
///lowlevelRaytest performs a ray versus convex shape, returning true is a hit is found (filling in worldNormal and worldHitPoint)
bool RaytracerPhysicsSetup::lowlevelRaytest(const btVector3& rayFrom, const btVector3& rayTo, btVector3& worldNormal, btVector3& worldHitPoint)
{
btScalar closestHitResults = 1.f;
bool hasHit = false;
btConvexCast::CastResult rayResult;
btSphereShape pointShape(0.0f);
btTransform rayFromTrans;
btTransform rayToTrans;
rayFromTrans.setIdentity();
rayFromTrans.setOrigin(rayFrom);
rayToTrans.setIdentity();
rayToTrans.setOrigin(rayTo);
int numObjects = m_internalData->m_shapePtr.size();
for (int s = 0; s < numObjects; s++)
{
//do some culling, ray versus aabb
btVector3 aabbMin, aabbMax;
m_internalData->m_shapePtr[s]->getAabb(m_internalData->m_transforms[s], aabbMin, aabbMax);
btScalar hitLambda = 1.f;
btVector3 hitNormal;
btCollisionObject tmpObj;
tmpObj.setWorldTransform(m_internalData->m_transforms[s]);
if (btRayAabb(rayFrom, rayTo, aabbMin, aabbMax, hitLambda, hitNormal))
{
//reset previous result
//choose the continuous collision detection method
btSubsimplexConvexCast convexCaster(&pointShape, m_internalData->m_shapePtr[s], &m_internalData->m_simplexSolver);
//btGjkConvexCast convexCaster(&pointShape,shapePtr[s],&simplexSolver);
//btContinuousConvexCollision convexCaster(&pointShape,shapePtr[s],&simplexSolver,0);
if (convexCaster.calcTimeOfImpact(rayFromTrans, rayToTrans, m_internalData->m_transforms[s], m_internalData->m_transforms[s], rayResult))
{
if (rayResult.m_fraction < closestHitResults)
{
closestHitResults = rayResult.m_fraction;
worldNormal = m_internalData->m_transforms[s].getBasis() * rayResult.m_normal;
worldNormal.normalize();
hasHit = true;
}
}
}
}
return hasHit;
}
void RaytracerPhysicsSetup::exitPhysics()
{
if (m_internalData->m_canvas && m_internalData->m_canvasIndex >= 0)
{
m_internalData->m_canvas->destroyCanvas(m_internalData->m_canvasIndex);
}
}
void RaytracerPhysicsSetup::stepSimulation(float deltaTime)
{
m_internalData->updateTransforms();
float top = 1.f;
float bottom = -1.f;
float nearPlane = 1.f;
float tanFov = (top - bottom) * 0.5f / nearPlane;
float fov = 2.0 * atanf(tanFov);
btVector3 cameraPosition(5, 0, 0);
btVector3 cameraTargetPosition(0, 0, 0);
if (m_app->m_renderer && m_app->m_renderer->getActiveCamera())
{
m_app->m_renderer->getActiveCamera()->getCameraPosition(cameraPosition);
m_app->m_renderer->getActiveCamera()->getCameraTargetPosition(cameraTargetPosition);
}
btVector3 rayFrom = cameraPosition;
btVector3 rayForward = cameraTargetPosition - cameraPosition;
rayForward.normalize();
float farPlane = 600.f;
rayForward *= farPlane;
btVector3 rightOffset;
btVector3 vertical(0.f, 1.f, 0.f);
btVector3 hor;
hor = rayForward.cross(vertical);
hor.normalize();
vertical = hor.cross(rayForward);
vertical.normalize();
float tanfov = tanf(0.5f * fov);
hor *= 2.f * farPlane * tanfov;
vertical *= 2.f * farPlane * tanfov;
btVector3 rayToCenter = rayFrom + rayForward;
btVector3 dHor = hor * 1.f / float(m_internalData->m_width);
btVector3 dVert = vertical * 1.f / float(m_internalData->m_height);
// int mode = 0;
int x, y;
for (x = 0; x < m_internalData->m_width; x++)
{
for (y = 0; y < m_internalData->m_height; y++)
{
btVector4 rgba(0, 0, 0, 0);
btVector3 rayTo = rayToCenter - 0.5f * hor + 0.5f * vertical;
rayTo += x * dHor;
rayTo -= y * dVert;
btVector3 worldNormal(0, 0, 0);
btVector3 worldPoint(0, 0, 0);
bool hasHit = false;
int mode = 0;
switch (mode)
{
case 0:
hasHit = lowlevelRaytest(rayFrom, rayTo, worldNormal, worldPoint);
break;
case 1:
hasHit = singleObjectRaytest(rayFrom, rayTo, worldNormal, worldPoint);
break;
case 2:
hasHit = worldRaytest(rayFrom, rayTo, worldNormal, worldPoint);
break;
default:
{
}
}
if (hasHit)
{
float lightVec0 = worldNormal.dot(btVector3(0, -1, -1)); //0.4f,-1.f,-0.4f));
float lightVec1 = worldNormal.dot(btVector3(-1, 0, -1)); //-0.4f,-1.f,-0.4f));
rgba = btVector4(lightVec0, lightVec1, 0, 1.f);
rgba.setMin(btVector3(1, 1, 1));
rgba.setMax(btVector3(0.2, 0.2, 0.2));
rgba[3] = 1.f;
unsigned char red = rgba[0] * 255;
unsigned char green = rgba[1] * 255;
unsigned char blue = rgba[2] * 255;
unsigned char alpha = 255;
m_internalData->m_canvas->setPixel(m_internalData->m_canvasIndex, x, y, red, green, blue, alpha);
}
else
{
// btVector4 rgba = raytracePicture->getPixel(x,y);
}
if (!rgba.length2())
{
m_internalData->m_canvas->setPixel(m_internalData->m_canvasIndex, x, y, 255, 0, 0, 255);
}
}
}
m_internalData->m_canvas->refreshImageData(m_internalData->m_canvasIndex);
}
void RaytracerPhysicsSetup::physicsDebugDraw(int debugDrawFlags)
{
}
bool RaytracerPhysicsSetup::mouseMoveCallback(float x, float y)
{
return false;
}
bool RaytracerPhysicsSetup::mouseButtonCallback(int button, int state, float x, float y)
{
return false;
}
bool RaytracerPhysicsSetup::keyboardCallback(int key, int state)
{
return false;
}
void RaytracerPhysicsSetup::syncPhysicsToGraphics(GraphicsPhysicsBridge& gfxBridge)
{
}
CommonExampleInterface* RayTracerCreateFunc(struct CommonExampleOptions& options)
{
return new RaytracerPhysicsSetup(options.m_guiHelper->getAppInterface());
}

View file

@ -0,0 +1,6 @@
#ifndef RAYTRACER_SETUP_H
#define RAYTRACER_SETUP_H
class CommonExampleInterface* RayTracerCreateFunc(struct CommonExampleOptions& options);
#endif //RAYTRACER_SETUP_H

View file

@ -0,0 +1,139 @@
#ifndef RENDER_INSTANCING_DEMO_H
#define RENDER_INSTANCING_DEMO_H
#include "../CommonInterfaces/CommonGraphicsAppInterface.h"
#include "Bullet3Common/b3Quaternion.h"
#include "Bullet3Common/b3AlignedObjectArray.h"
#include "../CommonInterfaces/CommonRenderInterface.h"
#include "../CommonInterfaces/CommonExampleInterface.h"
#include "../CommonInterfaces/CommonGUIHelperInterface.h"
///quick demo showing the right-handed coordinate system and positive rotations around each axis
class RenderInstancingDemo : public CommonExampleInterface
{
CommonGraphicsApp* m_app;
float m_x;
float m_y;
float m_z;
b3AlignedObjectArray<int> m_movingInstances;
enum
{
numCubesX = 20,
numCubesY = 20
};
public:
RenderInstancingDemo(CommonGraphicsApp* app)
: m_app(app),
m_x(0),
m_y(0),
m_z(0)
{
m_app->setUpAxis(2);
{
b3Vector3 extents = b3MakeVector3(100, 100, 100);
extents[m_app->getUpAxis()] = 1;
int xres = 20;
int yres = 20;
b3Vector4 color0 = b3MakeVector4(0.1, 0.1, 0.1, 1);
b3Vector4 color1 = b3MakeVector4(0.6, 0.6, 0.6, 1);
m_app->registerGrid(xres, yres, color0, color1);
}
{
int boxId = m_app->registerCubeShape(0.1, 0.1, 0.1);
for (int i = -numCubesX / 2; i < numCubesX / 2; i++)
{
for (int j = -numCubesY / 2; j < numCubesY / 2; j++)
{
b3Vector3 pos = b3MakeVector3(i, j, j);
pos[app->getUpAxis()] = 1;
b3Quaternion orn(0, 0, 0, 1);
b3Vector4 color = b3MakeVector4(0.3, 0.3, 0.3, 1);
b3Vector3 scaling = b3MakeVector3(1, 1, 1);
int instanceId = m_app->m_renderer->registerGraphicsInstance(boxId, pos, orn, color, scaling);
m_movingInstances.push_back(instanceId);
}
}
}
m_app->m_renderer->writeTransforms();
}
virtual ~RenderInstancingDemo()
{
}
virtual void physicsDebugDraw(int debugDrawMode)
{
}
virtual void initPhysics()
{
}
virtual void exitPhysics()
{
}
virtual void stepSimulation(float deltaTime)
{
m_x += 0.01f;
m_y += 0.01f;
m_z += 0.01f;
int index = 0;
for (int i = -numCubesX / 2; i < numCubesX / 2; i++)
{
for (int j = -numCubesY / 2; j < numCubesY / 2; j++)
{
b3Vector3 pos = b3MakeVector3(i, j, j);
pos[m_app->getUpAxis()] = 1 + 1 * b3Sin(m_x + i - j);
float orn[4] = {0, 0, 0, 1};
m_app->m_renderer->writeSingleInstanceTransformToCPU(pos, orn, m_movingInstances[index++]);
}
}
m_app->m_renderer->writeTransforms();
}
virtual void renderScene()
{
m_app->m_renderer->renderScene();
}
virtual void physicsDebugDraw()
{
}
virtual bool mouseMoveCallback(float x, float y)
{
return false;
}
virtual bool mouseButtonCallback(int button, int state, float x, float y)
{
return false;
}
virtual bool keyboardCallback(int key, int state)
{
return false;
}
virtual void resetCamera()
{
float dist = 13;
float pitch = -13;
float yaw = 50;
float targetPos[3] = {-1, 0, -0.3};
if (m_app->m_renderer && m_app->m_renderer->getActiveCamera())
{
m_app->m_renderer->getActiveCamera()->setCameraDistance(dist);
m_app->m_renderer->getActiveCamera()->setCameraPitch(pitch);
m_app->m_renderer->getActiveCamera()->setCameraYaw(yaw);
m_app->m_renderer->getActiveCamera()->setCameraTargetPosition(targetPos[0], targetPos[1], targetPos[2]);
}
}
};
class CommonExampleInterface* RenderInstancingCreateFunc(struct CommonExampleOptions& options)
{
return new RenderInstancingDemo(options.m_guiHelper->getAppInterface());
}
#endif //RENDER_INSTANCING_DEMO_H

View file

@ -0,0 +1,6 @@
#ifndef RENDER_INSTANCING_DEMO_H
#define RENDER_INSTANCING_DEMO_H
class CommonExampleInterface* RenderInstancingCreateFunc(struct CommonExampleOptions& options);
#endif //RENDER_INSTANCING_DEMO_H

View file

@ -0,0 +1,330 @@
#include "TimeSeriesCanvas.h"
#include "../CommonInterfaces/Common2dCanvasInterface.h"
#include "LinearMath/btAlignedObjectArray.h"
#include "TimeSeriesFontData.h"
#include "LinearMath/btVector3.h"
#include <stdio.h>
struct DataSource
{
unsigned char m_red;
unsigned char m_green;
unsigned char m_blue;
float m_lastValue;
bool m_hasLastValue;
DataSource()
: m_hasLastValue(false)
{
}
};
struct TimeSeriesInternalData
{
btAlignedObjectArray<DataSource> m_dataSources;
struct Common2dCanvasInterface* m_canvasInterface;
int m_canvasIndex;
int m_width;
int m_height;
float m_pixelsPerUnit;
float m_zero;
int m_timeTicks;
int m_ticksPerSecond;
float m_yScale;
int m_bar;
unsigned char m_backgroundRed;
unsigned char m_backgroundGreen;
unsigned char m_backgroundBlue;
unsigned char m_backgroundAlpha;
unsigned char m_textColorRed;
unsigned char m_textColorGreen;
unsigned char m_textColorBlue;
unsigned char m_textColorAlpha;
float getTime()
{
return m_timeTicks / (float)m_ticksPerSecond;
}
TimeSeriesInternalData(int width, int height)
: m_width(width),
m_height(height),
m_pixelsPerUnit(-100),
m_zero(height / 2.0),
m_timeTicks(0),
m_ticksPerSecond(100),
m_yScale(1),
m_bar(0),
m_backgroundRed(255),
m_backgroundGreen(255),
m_backgroundBlue(255),
m_backgroundAlpha(255),
m_textColorRed(0),
m_textColorGreen(0),
m_textColorBlue(255),
m_textColorAlpha(255)
{
}
};
TimeSeriesCanvas::TimeSeriesCanvas(struct Common2dCanvasInterface* canvasInterface, int width, int height, const char* windowTitle)
{
m_internalData = new TimeSeriesInternalData(width, height);
m_internalData->m_canvasInterface = canvasInterface;
if (canvasInterface)
{
m_internalData->m_canvasIndex = m_internalData->m_canvasInterface->createCanvas(windowTitle, m_internalData->m_width, m_internalData->m_height, 20, 50);
}
}
void TimeSeriesCanvas::addDataSource(const char* dataSourceLabel, unsigned char red, unsigned char green, unsigned char blue)
{
DataSource dataSource;
dataSource.m_red = red;
dataSource.m_green = green;
dataSource.m_blue = blue;
dataSource.m_lastValue = 0;
dataSource.m_hasLastValue = false;
if (dataSourceLabel)
{
int numSources = m_internalData->m_dataSources.size();
int row = numSources % 3;
int column = numSources / 3;
grapicalPrintf(dataSourceLabel, sTimeSeriesFontData, 50 + 200 * column, m_internalData->m_height - 48 + row * 16,
red, green, blue, 255);
}
m_internalData->m_dataSources.push_back(dataSource);
}
void TimeSeriesCanvas::setupTimeSeries(float yScale, int ticksPerSecond, int startTime, bool clearCanvas)
{
if (0 == m_internalData->m_canvasInterface)
return;
m_internalData->m_pixelsPerUnit = -(m_internalData->m_height / 3.f) / yScale;
m_internalData->m_ticksPerSecond = ticksPerSecond;
m_internalData->m_yScale = yScale;
m_internalData->m_dataSources.clear();
if (clearCanvas)
{
for (int i = 0; i < m_internalData->m_width; i++)
{
for (int j = 0; j < m_internalData->m_height; j++)
{
m_internalData->m_canvasInterface->setPixel(m_internalData->m_canvasIndex, i, j,
m_internalData->m_backgroundRed,
m_internalData->m_backgroundGreen,
m_internalData->m_backgroundBlue,
m_internalData->m_backgroundAlpha);
}
}
}
float zeroPixelCoord = m_internalData->m_zero;
float pixelsPerUnit = m_internalData->m_pixelsPerUnit;
float yPos = zeroPixelCoord + pixelsPerUnit * yScale;
float yNeg = zeroPixelCoord + pixelsPerUnit * -yScale;
grapicalPrintf("0", sTimeSeriesFontData, 2, zeroPixelCoord, m_internalData->m_textColorRed, m_internalData->m_textColorGreen, m_internalData->m_textColorBlue, m_internalData->m_textColorAlpha);
char label[1024];
sprintf(label, "%2.1f", yScale);
grapicalPrintf(label, sTimeSeriesFontData, 2, yPos, m_internalData->m_textColorRed, m_internalData->m_textColorGreen, m_internalData->m_textColorBlue, m_internalData->m_textColorAlpha);
sprintf(label, "%2.1f", -yScale);
grapicalPrintf(label, sTimeSeriesFontData, 2, yNeg, m_internalData->m_textColorRed, m_internalData->m_textColorGreen, m_internalData->m_textColorBlue, m_internalData->m_textColorAlpha);
m_internalData->m_canvasInterface->refreshImageData(m_internalData->m_canvasIndex);
}
TimeSeriesCanvas::~TimeSeriesCanvas()
{
if (m_internalData->m_canvasInterface && m_internalData->m_canvasIndex >= 0)
{
m_internalData->m_canvasInterface->destroyCanvas(m_internalData->m_canvasIndex);
}
delete m_internalData;
}
float TimeSeriesCanvas::getCurrentTime() const
{
return m_internalData->getTime();
}
void TimeSeriesCanvas::grapicalPrintf(const char* str, void* fontData, int rasterposx, int rasterposy, unsigned char red, unsigned char green, unsigned char blue, unsigned char alpha)
{
unsigned char c;
int x = 0;
int xx = 0;
while ((c = (unsigned char)*str++))
{
x = xx;
unsigned char* fontPtr = (unsigned char*)fontData;
char ch = c - 32;
int sx = ch % 16;
int sy = ch / 16;
for (int i = sx * 16; i < (sx * 16 + 16); i++)
{
int y = 0;
for (int j = sy * 16; j < (sy * 16 + 16); j++)
{
unsigned char packedColor = (fontPtr[i * 3 + 255 * 256 * 3 - (256 * j) * 3]);
//float colorf = packedColor ? 0.f : 1.f;
float colorf = packedColor / 255.f; // ? 0.f : 1.f;
btVector4 rgba(colorf, colorf, colorf, 1.f);
if (colorf)
{
if ((rasterposx + x >= 0) && (rasterposx + x < m_internalData->m_width) &&
(rasterposy + y >= 0) && (rasterposy + y < m_internalData->m_height))
{
m_internalData->m_canvasInterface->setPixel(m_internalData->m_canvasIndex, rasterposx + x, rasterposy + y,
red, green, blue, alpha);
}
}
y++;
}
x++;
}
xx += 10;
}
}
void TimeSeriesCanvas::shift1PixelToLeft()
{
int resetVal = 10;
int countdown = resetVal;
//shift pixture one pixel to the left
for (int j = 50; j < m_internalData->m_height - 48; j++)
{
for (int i = 40; i < this->m_internalData->m_width; i++)
{
unsigned char red, green, blue, alpha;
m_internalData->m_canvasInterface->getPixel(m_internalData->m_canvasIndex, i, j, red, green, blue, alpha);
m_internalData->m_canvasInterface->setPixel(m_internalData->m_canvasIndex, i - 1, j, red, green, blue, alpha);
}
if (!m_internalData->m_bar)
{
if (!countdown--)
{
countdown = resetVal;
m_internalData->m_canvasInterface->setPixel(m_internalData->m_canvasIndex, m_internalData->m_width - 1, j, 0, 0, 0, 255);
}
else
{
m_internalData->m_canvasInterface->setPixel(m_internalData->m_canvasIndex, m_internalData->m_width - 1, j, 255, 255, 255, 255);
}
}
else
{
m_internalData->m_canvasInterface->setPixel(m_internalData->m_canvasIndex, m_internalData->m_width - 1, j, 255, 255, 255, 255);
}
}
{
int resetVal = 2;
static int countdown = resetVal;
if (!countdown--)
{
countdown = resetVal;
m_internalData->m_canvasInterface->setPixel(m_internalData->m_canvasIndex, m_internalData->m_width - 1, m_internalData->m_zero, 0, 0, 0, 255);
}
}
{
int resetVal = 10;
static int countdown = resetVal;
if (!countdown--)
{
countdown = resetVal;
float zeroPixelCoord = m_internalData->m_zero;
float pixelsPerUnit = m_internalData->m_pixelsPerUnit;
float yPos = zeroPixelCoord + pixelsPerUnit * m_internalData->m_yScale;
float yNeg = zeroPixelCoord + pixelsPerUnit * -m_internalData->m_yScale;
m_internalData->m_canvasInterface->setPixel(m_internalData->m_canvasIndex, m_internalData->m_width - 1,
yPos, 0, 0, 0, 255);
m_internalData->m_canvasInterface->setPixel(m_internalData->m_canvasIndex, m_internalData->m_width - 1,
yNeg, 0, 0, 0, 255);
}
}
if (!m_internalData->m_bar)
{
char buf[1024];
float time = m_internalData->getTime();
sprintf(buf, "%2.0f", time);
grapicalPrintf(buf, sTimeSeriesFontData, m_internalData->m_width - 25, m_internalData->m_zero + 3, 0, 0, 0, 255);
m_internalData->m_bar = m_internalData->m_ticksPerSecond;
}
m_internalData->m_timeTicks++;
m_internalData->m_bar--;
}
void TimeSeriesCanvas::insertDataAtCurrentTime(float orgV, int dataSourceIndex, bool connectToPrevious)
{
if (0 == m_internalData->m_canvasInterface)
return;
btAssert(dataSourceIndex < m_internalData->m_dataSources.size());
float zero = m_internalData->m_zero;
float amp = m_internalData->m_pixelsPerUnit;
//insert some new value(s) in the right-most column
{
// float time = m_internalData->getTime();
float v = zero + amp * orgV;
m_internalData->m_canvasInterface->setPixel(m_internalData->m_canvasIndex, m_internalData->m_width - 1, v,
m_internalData->m_dataSources[dataSourceIndex].m_red,
m_internalData->m_dataSources[dataSourceIndex].m_green,
m_internalData->m_dataSources[dataSourceIndex].m_blue,
255);
if (connectToPrevious && m_internalData->m_dataSources[dataSourceIndex].m_hasLastValue)
{
for (int value = m_internalData->m_dataSources[dataSourceIndex].m_lastValue; value <= v; value++)
{
if (value >= 0 && value < float(m_internalData->m_height - 1))
{
m_internalData->m_canvasInterface->setPixel(m_internalData->m_canvasIndex, m_internalData->m_width - 1, value,
m_internalData->m_dataSources[dataSourceIndex].m_red,
m_internalData->m_dataSources[dataSourceIndex].m_green,
m_internalData->m_dataSources[dataSourceIndex].m_blue,
255);
}
}
for (int value = v; value <= m_internalData->m_dataSources[dataSourceIndex].m_lastValue; value++)
{
if (value >= 0 && value < float(m_internalData->m_height - 1))
{
m_internalData->m_canvasInterface->setPixel(m_internalData->m_canvasIndex, m_internalData->m_width - 1, value,
m_internalData->m_dataSources[dataSourceIndex].m_red,
m_internalData->m_dataSources[dataSourceIndex].m_green,
m_internalData->m_dataSources[dataSourceIndex].m_blue,
255);
}
}
}
m_internalData->m_dataSources[dataSourceIndex].m_lastValue = v;
m_internalData->m_dataSources[dataSourceIndex].m_hasLastValue = true;
}
}
void TimeSeriesCanvas::nextTick()
{
shift1PixelToLeft();
m_internalData->m_canvasInterface->refreshImageData(m_internalData->m_canvasIndex);
}

View file

@ -0,0 +1,23 @@
#ifndef TIME_SERIES_CANVAS_H
#define TIME_SERIES_CANVAS_H
class TimeSeriesCanvas
{
protected:
struct TimeSeriesInternalData* m_internalData;
void shift1PixelToLeft();
public:
TimeSeriesCanvas(struct Common2dCanvasInterface* canvasInterface, int width, int height, const char* windowTitle);
virtual ~TimeSeriesCanvas();
void setupTimeSeries(float yScale, int ticksPerSecond, int startTime, bool clearCanvas = true);
void addDataSource(const char* dataSourceLabel, unsigned char red, unsigned char green, unsigned char blue);
void insertDataAtCurrentTime(float value, int dataSourceIndex, bool connectToPrevious);
float getCurrentTime() const;
void grapicalPrintf(const char* str, void* fontData, int rasterposx, int rasterposy, unsigned char red, unsigned char green, unsigned char blue, unsigned char alpha);
virtual void nextTick();
};
#endif //TIME_SERIES_CANVAS_H

View file

@ -0,0 +1,125 @@
#include "TimeSeriesExample.h"
#include "../CommonInterfaces/CommonGraphicsAppInterface.h"
#include "../CommonInterfaces/CommonRenderInterface.h"
#include "../CommonInterfaces/Common2dCanvasInterface.h"
#include "../CommonInterfaces/CommonExampleInterface.h"
#include "LinearMath/btAlignedObjectArray.h"
#include "btBulletCollisionCommon.h"
#include "../CommonInterfaces/CommonGUIHelperInterface.h"
#include "TimeSeriesCanvas.h"
struct TimeSeriesExample : public CommonExampleInterface
{
struct CommonGraphicsApp* m_app;
struct TimeSeriesExampleInternalData* m_internalData;
TimeSeriesExample(struct CommonGraphicsApp* app);
virtual ~TimeSeriesExample();
virtual void initPhysics();
virtual void exitPhysics();
virtual void stepSimulation(float deltaTime);
virtual void physicsDebugDraw(int debugFlags);
virtual void syncPhysicsToGraphics(struct GraphicsPhysicsBridge& gfxBridge);
virtual bool mouseMoveCallback(float x, float y);
virtual bool mouseButtonCallback(int button, int state, float x, float y);
virtual bool keyboardCallback(int key, int state);
virtual void renderScene()
{
}
};
struct TimeSeriesExampleInternalData
{
TimeSeriesCanvas* m_timeSeriesCanvas;
TimeSeriesExampleInternalData()
: m_timeSeriesCanvas(0)
{
}
};
TimeSeriesExample::TimeSeriesExample(struct CommonGraphicsApp* app)
{
m_app = app;
m_internalData = new TimeSeriesExampleInternalData;
}
TimeSeriesExample::~TimeSeriesExample()
{
delete m_internalData->m_timeSeriesCanvas;
delete m_internalData;
}
void TimeSeriesExample::initPhysics()
{
//request a visual bitma/texture we can render to
m_internalData->m_timeSeriesCanvas = new TimeSeriesCanvas(m_app->m_2dCanvasInterface, 512, 512, "Test");
m_internalData->m_timeSeriesCanvas->setupTimeSeries(3, 100, 0);
m_internalData->m_timeSeriesCanvas->addDataSource("Some sine wave", 255, 0, 0);
m_internalData->m_timeSeriesCanvas->addDataSource("Some cosine wave", 0, 255, 0);
m_internalData->m_timeSeriesCanvas->addDataSource("Delta Time (*10)", 0, 0, 255);
m_internalData->m_timeSeriesCanvas->addDataSource("Tan", 255, 0, 255);
m_internalData->m_timeSeriesCanvas->addDataSource("Some cosine wave2", 255, 255, 0);
m_internalData->m_timeSeriesCanvas->addDataSource("Empty source2", 255, 0, 255);
}
void TimeSeriesExample::exitPhysics()
{
}
void TimeSeriesExample::stepSimulation(float deltaTime)
{
float time = m_internalData->m_timeSeriesCanvas->getCurrentTime();
float v = sinf(time);
m_internalData->m_timeSeriesCanvas->insertDataAtCurrentTime(v, 0, true);
v = cosf(time);
m_internalData->m_timeSeriesCanvas->insertDataAtCurrentTime(v, 1, true);
v = tanf(time);
m_internalData->m_timeSeriesCanvas->insertDataAtCurrentTime(v, 3, true);
m_internalData->m_timeSeriesCanvas->insertDataAtCurrentTime(deltaTime * 10, 2, true);
m_internalData->m_timeSeriesCanvas->nextTick();
}
void TimeSeriesExample::physicsDebugDraw(int debugDrawFlags)
{
}
bool TimeSeriesExample::mouseMoveCallback(float x, float y)
{
return false;
}
bool TimeSeriesExample::mouseButtonCallback(int button, int state, float x, float y)
{
return false;
}
bool TimeSeriesExample::keyboardCallback(int key, int state)
{
return false;
}
void TimeSeriesExample::syncPhysicsToGraphics(GraphicsPhysicsBridge& gfxBridge)
{
}
CommonExampleInterface* TimeSeriesCreateFunc(struct CommonExampleOptions& options)
{
return new TimeSeriesExample(options.m_guiHelper->getAppInterface());
}

View file

@ -0,0 +1,6 @@
#ifndef TIME_SERIES_EXAMPLE_H
#define TIME_SERIES_EXAMPLE_H
class CommonExampleInterface* TimeSeriesCreateFunc(struct CommonExampleOptions& options);
#endif //TIME_SERIES_EXAMPLE_H

File diff suppressed because it is too large Load diff

View file

@ -0,0 +1,6 @@
#ifndef TIME_SERIES_FONT_DATA_H
#define TIME_SERIES_FONT_DATA_H
extern unsigned char sTimeSeriesFontData[];
#endif //TIME_SERIES_FONT_DATA_H

View file

@ -0,0 +1,454 @@
#include "RaytracerSetup.h"
#include "../CommonInterfaces/CommonGraphicsAppInterface.h"
#include "Bullet3Common/b3Quaternion.h"
#include "Bullet3Common/b3AlignedObjectArray.h"
#include "../CommonInterfaces/CommonRenderInterface.h"
#include "../TinyRenderer/TinyRenderer.h"
#include "../CommonInterfaces/Common2dCanvasInterface.h"
#include "BulletCollision/NarrowPhaseCollision/btSubSimplexConvexCast.h"
#include "../CommonInterfaces/CommonExampleInterface.h"
#include "LinearMath/btAlignedObjectArray.h"
#include "btBulletCollisionCommon.h"
#include "../CommonInterfaces/CommonGUIHelperInterface.h"
#include "../ExampleBrowser/CollisionShape2TriangleMesh.h"
#include "../Importers/ImportMeshUtility/b3ImportMeshUtility.h"
#include "../OpenGLWindow/GLInstanceGraphicsShape.h"
#include "../CommonInterfaces/CommonParameterInterface.h"
#include "../Utils/b3BulletDefaultFileIO.h"
struct TinyRendererSetupInternalData
{
TGAImage m_rgbColorBuffer;
b3AlignedObjectArray<float> m_depthBuffer;
b3AlignedObjectArray<float> m_shadowBuffer;
b3AlignedObjectArray<int> m_segmentationMaskBuffer;
int m_width;
int m_height;
btAlignedObjectArray<btConvexShape*> m_shapePtr;
btAlignedObjectArray<btTransform> m_transforms;
btAlignedObjectArray<TinyRenderObjectData*> m_renderObjects;
btVoronoiSimplexSolver m_simplexSolver;
btScalar m_pitch;
btScalar m_roll;
btScalar m_yaw;
int m_textureHandle;
int m_animateRenderer;
btVector3 m_lightPos;
TinyRendererSetupInternalData(int width, int height)
: m_rgbColorBuffer(width, height, TGAImage::RGB),
m_width(width),
m_height(height),
m_pitch(0),
m_roll(0),
m_yaw(0),
m_textureHandle(0),
m_animateRenderer(0)
{
m_lightPos.setValue(-3, 15, 15);
m_depthBuffer.resize(m_width * m_height);
m_shadowBuffer.resize(m_width * m_height);
// m_segmentationMaskBuffer.resize(m_width*m_height);
}
void updateTransforms()
{
int numObjects = m_shapePtr.size();
m_transforms.resize(numObjects);
for (int i = 0; i < numObjects; i++)
{
m_transforms[i].setIdentity();
//btVector3 pos(0.f,-(2.5* numObjects * 0.5)+i*2.5f, 0.f);
btVector3 pos(0.f, +i * 2.5f, 0.f);
m_transforms[i].setIdentity();
m_transforms[i].setOrigin(pos);
btQuaternion orn;
if (i < 2)
{
orn.setEuler(m_yaw, m_pitch, m_roll);
m_transforms[i].setRotation(orn);
}
}
if (m_animateRenderer)
{
m_pitch += 0.005f;
m_yaw += 0.01f;
}
}
};
struct TinyRendererSetup : public CommonExampleInterface
{
struct GUIHelperInterface* m_guiHelper;
struct CommonGraphicsApp* m_app;
struct TinyRendererSetupInternalData* m_internalData;
bool m_useSoftware;
TinyRendererSetup(struct GUIHelperInterface* guiHelper);
virtual ~TinyRendererSetup();
virtual void initPhysics();
virtual void exitPhysics();
virtual void stepSimulation(float deltaTime);
virtual void physicsDebugDraw(int debugFlags);
virtual void syncPhysicsToGraphics(struct GraphicsPhysicsBridge& gfxBridge);
virtual bool mouseMoveCallback(float x, float y);
virtual bool mouseButtonCallback(int button, int state, float x, float y);
virtual bool keyboardCallback(int key, int state);
virtual void renderScene();
void animateRenderer(int animateRendererIndex)
{
m_internalData->m_animateRenderer = animateRendererIndex;
}
void selectRenderer(int rendererIndex)
{
m_useSoftware = (rendererIndex == 0);
}
void resetCamera()
{
float dist = 11;
float pitch = -35;
float yaw = 52;
float targetPos[3] = {0, 0.46, 0};
m_guiHelper->resetCamera(dist, yaw, pitch, targetPos[0], targetPos[1], targetPos[2]);
}
};
TinyRendererSetup::TinyRendererSetup(struct GUIHelperInterface* gui)
{
m_useSoftware = false;
m_guiHelper = gui;
m_app = gui->getAppInterface();
m_internalData = new TinyRendererSetupInternalData(gui->getAppInterface()->m_window->getWidth(), gui->getAppInterface()->m_window->getHeight());
const char* fileName = "textured_sphere_smooth.obj";
fileName = "cube.obj";
fileName = "torus/torus_with_plane.obj";
{
{
int shapeId = -1;
b3ImportMeshData meshData;
b3BulletDefaultFileIO fileIO;
if (b3ImportMeshUtility::loadAndRegisterMeshFromFileInternal(fileName, meshData,&fileIO))
{
int textureIndex = -1;
if (meshData.m_textureImage1)
{
textureIndex = m_guiHelper->getRenderInterface()->registerTexture(meshData.m_textureImage1, meshData.m_textureWidth, meshData.m_textureHeight);
}
shapeId = m_guiHelper->getRenderInterface()->registerShape(&meshData.m_gfxShape->m_vertices->at(0).xyzw[0],
meshData.m_gfxShape->m_numvertices,
&meshData.m_gfxShape->m_indices->at(0),
meshData.m_gfxShape->m_numIndices,
B3_GL_TRIANGLES,
textureIndex);
float position[4] = {0, 0, 0, 1};
float orn[4] = {0, 0, 0, 1};
float color[4] = {1, 1, 1, 1};
float scaling[4] = {1, 1, 1, 1};
m_guiHelper->getRenderInterface()->registerGraphicsInstance(shapeId, position, orn, color, scaling);
m_guiHelper->getRenderInterface()->writeTransforms();
m_internalData->m_shapePtr.push_back(0);
TinyRenderObjectData* ob = new TinyRenderObjectData(
m_internalData->m_rgbColorBuffer,
m_internalData->m_depthBuffer,
&m_internalData->m_shadowBuffer,
&m_internalData->m_segmentationMaskBuffer,
m_internalData->m_renderObjects.size(), -1);
meshData.m_gfxShape->m_scaling[0] = scaling[0];
meshData.m_gfxShape->m_scaling[1] = scaling[1];
meshData.m_gfxShape->m_scaling[2] = scaling[2];
const int* indices = &meshData.m_gfxShape->m_indices->at(0);
ob->registerMeshShape(&meshData.m_gfxShape->m_vertices->at(0).xyzw[0],
meshData.m_gfxShape->m_numvertices,
indices,
meshData.m_gfxShape->m_numIndices, color, meshData.m_textureImage1, meshData.m_textureWidth, meshData.m_textureHeight);
ob->m_localScaling.setValue(scaling[0], scaling[1], scaling[2]);
m_internalData->m_renderObjects.push_back(ob);
delete meshData.m_gfxShape;
if (!meshData.m_isCached)
{
delete meshData.m_textureImage1;
}
}
}
}
}
TinyRendererSetup::~TinyRendererSetup()
{
delete m_internalData;
}
const char* itemsanimate[] = {"Fixed", "Rotate"};
void TinyRendererComboCallbackAnimate(int combobox, const char* item, void* userPointer)
{
TinyRendererSetup* cl = (TinyRendererSetup*)userPointer;
b3Assert(cl);
int index = -1;
int numItems = sizeof(itemsanimate) / sizeof(char*);
for (int i = 0; i < numItems; i++)
{
if (!strcmp(item, itemsanimate[i]))
{
index = i;
}
}
cl->animateRenderer(index);
}
const char* items[] = {"Software", "OpenGL"};
void TinyRendererComboCallback(int combobox, const char* item, void* userPointer)
{
TinyRendererSetup* cl = (TinyRendererSetup*)userPointer;
b3Assert(cl);
int index = -1;
int numItems = sizeof(items) / sizeof(char*);
for (int i = 0; i < numItems; i++)
{
if (!strcmp(item, items[i]))
{
index = i;
}
}
cl->selectRenderer(index);
}
void TinyRendererSetup::initPhysics()
{
//request a visual bitma/texture we can render to
m_app->setUpAxis(2);
CommonRenderInterface* render = m_app->m_renderer;
m_internalData->m_textureHandle = render->registerTexture(m_internalData->m_rgbColorBuffer.buffer(), m_internalData->m_width, m_internalData->m_height);
{
ComboBoxParams comboParams;
comboParams.m_userPointer = this;
comboParams.m_numItems = sizeof(items) / sizeof(char*);
comboParams.m_startItem = 1;
comboParams.m_items = items;
comboParams.m_callback = TinyRendererComboCallback;
m_guiHelper->getParameterInterface()->registerComboBox(comboParams);
}
{
ComboBoxParams comboParams;
comboParams.m_userPointer = this;
comboParams.m_numItems = sizeof(itemsanimate) / sizeof(char*);
comboParams.m_startItem = 0;
comboParams.m_items = itemsanimate;
comboParams.m_callback = TinyRendererComboCallbackAnimate;
m_guiHelper->getParameterInterface()->registerComboBox(comboParams);
}
{
SliderParams slider("LightPosX", &m_internalData->m_lightPos[0]);
slider.m_minVal = -10;
slider.m_maxVal = 10;
if (m_guiHelper->getParameterInterface())
m_guiHelper->getParameterInterface()->registerSliderFloatParameter(slider);
}
{
SliderParams slider("LightPosY", &m_internalData->m_lightPos[1]);
slider.m_minVal = -10;
slider.m_maxVal = 10;
if (m_guiHelper->getParameterInterface())
m_guiHelper->getParameterInterface()->registerSliderFloatParameter(slider);
}
{
SliderParams slider("LightPosZ", &m_internalData->m_lightPos[2]);
slider.m_minVal = -10;
slider.m_maxVal = 10;
if (m_guiHelper->getParameterInterface())
m_guiHelper->getParameterInterface()->registerSliderFloatParameter(slider);
}
}
void TinyRendererSetup::exitPhysics()
{
}
void TinyRendererSetup::stepSimulation(float deltaTime)
{
m_internalData->updateTransforms();
}
void TinyRendererSetup::renderScene()
{
m_internalData->updateTransforms();
btVector4 from(m_internalData->m_lightPos[0], m_internalData->m_lightPos[1], m_internalData->m_lightPos[2], 1);
btVector4 toX(m_internalData->m_lightPos[0] + 0.1, m_internalData->m_lightPos[1], m_internalData->m_lightPos[2], 1);
btVector4 toY(m_internalData->m_lightPos[0], m_internalData->m_lightPos[1] + 0.1, m_internalData->m_lightPos[2], 1);
btVector4 toZ(m_internalData->m_lightPos[0], m_internalData->m_lightPos[1], m_internalData->m_lightPos[2] + 0.1, 1);
btVector4 colorX(1, 0, 0, 1);
btVector4 colorY(0, 1, 0, 1);
btVector4 colorZ(0, 0, 1, 1);
int width = 2;
m_guiHelper->getRenderInterface()->drawLine(from, toX, colorX, width);
m_guiHelper->getRenderInterface()->drawLine(from, toY, colorY, width);
m_guiHelper->getRenderInterface()->drawLine(from, toZ, colorZ, width);
if (!m_useSoftware)
{
btVector3 lightPos(m_internalData->m_lightPos[0], m_internalData->m_lightPos[1], m_internalData->m_lightPos[2]);
m_guiHelper->getRenderInterface()->setLightPosition(lightPos);
for (int i = 0; i < m_internalData->m_transforms.size(); i++)
{
m_guiHelper->getRenderInterface()->writeSingleInstanceTransformToCPU(m_internalData->m_transforms[i].getOrigin(), m_internalData->m_transforms[i].getRotation(), i);
}
m_guiHelper->getRenderInterface()->writeTransforms();
m_guiHelper->getRenderInterface()->renderScene();
}
else
{
TGAColor clearColor;
clearColor.bgra[0] = 200;
clearColor.bgra[1] = 200;
clearColor.bgra[2] = 200;
clearColor.bgra[3] = 255;
for (int y = 0; y < m_internalData->m_height; ++y)
{
for (int x = 0; x < m_internalData->m_width; ++x)
{
m_internalData->m_rgbColorBuffer.set(x, y, clearColor);
m_internalData->m_depthBuffer[x + y * m_internalData->m_width] = -1e30f;
m_internalData->m_shadowBuffer[x + y * m_internalData->m_width] = -1e30f;
}
}
ATTRIBUTE_ALIGNED16(btScalar modelMat2[16]);
ATTRIBUTE_ALIGNED16(float viewMat[16]);
ATTRIBUTE_ALIGNED16(float projMat[16]);
CommonRenderInterface* render = this->m_app->m_renderer;
render->getActiveCamera()->getCameraViewMatrix(viewMat);
render->getActiveCamera()->getCameraProjectionMatrix(projMat);
for (int o = 0; o < this->m_internalData->m_renderObjects.size(); o++)
{
const btTransform& tr = m_internalData->m_transforms[o];
tr.getOpenGLMatrix(modelMat2);
for (int i = 0; i < 4; i++)
{
for (int j = 0; j < 4; j++)
{
m_internalData->m_renderObjects[o]->m_modelMatrix[i][j] = float(modelMat2[i + 4 * j]);
m_internalData->m_renderObjects[o]->m_viewMatrix[i][j] = viewMat[i + 4 * j];
m_internalData->m_renderObjects[o]->m_projectionMatrix[i][j] = projMat[i + 4 * j];
btVector3 lightDirWorld = btVector3(m_internalData->m_lightPos[0], m_internalData->m_lightPos[1], m_internalData->m_lightPos[2]);
m_internalData->m_renderObjects[o]->m_lightDirWorld = lightDirWorld.normalized();
btVector3 lightColor(1.0, 1.0, 1.0);
m_internalData->m_renderObjects[o]->m_lightColor = lightColor;
m_internalData->m_renderObjects[o]->m_lightDistance = 10.0;
m_internalData->m_renderObjects[o]->m_lightAmbientCoeff = 0.6;
m_internalData->m_renderObjects[o]->m_lightDiffuseCoeff = 0.35;
m_internalData->m_renderObjects[o]->m_lightSpecularCoeff = 0.05;
}
}
TinyRenderer::renderObjectDepth(*m_internalData->m_renderObjects[o]);
}
for (int o = 0; o < this->m_internalData->m_renderObjects.size(); o++)
{
const btTransform& tr = m_internalData->m_transforms[o];
tr.getOpenGLMatrix(modelMat2);
for (int i = 0; i < 4; i++)
{
for (int j = 0; j < 4; j++)
{
m_internalData->m_renderObjects[o]->m_modelMatrix[i][j] = float(modelMat2[i + 4 * j]);
m_internalData->m_renderObjects[o]->m_viewMatrix[i][j] = viewMat[i + 4 * j];
m_internalData->m_renderObjects[o]->m_projectionMatrix[i][j] = projMat[i + 4 * j];
btVector3 lightDirWorld = btVector3(m_internalData->m_lightPos[0], m_internalData->m_lightPos[1], m_internalData->m_lightPos[2]);
m_internalData->m_renderObjects[o]->m_lightDirWorld = lightDirWorld.normalized();
btVector3 lightColor(1.0, 1.0, 1.0);
m_internalData->m_renderObjects[o]->m_lightColor = lightColor;
m_internalData->m_renderObjects[o]->m_lightDistance = 10.0;
m_internalData->m_renderObjects[o]->m_lightAmbientCoeff = 0.6;
m_internalData->m_renderObjects[o]->m_lightDiffuseCoeff = 0.35;
m_internalData->m_renderObjects[o]->m_lightSpecularCoeff = 0.05;
}
}
TinyRenderer::renderObject(*m_internalData->m_renderObjects[o]);
}
//m_app->drawText("hello",500,500);
render->activateTexture(m_internalData->m_textureHandle);
render->updateTexture(m_internalData->m_textureHandle, m_internalData->m_rgbColorBuffer.buffer());
float color[4] = {1, 1, 1, 1};
m_app->drawTexturedRect(0, 0, m_app->m_window->getWidth(), m_app->m_window->getHeight(), color, 0, 0, 1, 1, true);
}
}
void TinyRendererSetup::physicsDebugDraw(int debugDrawFlags)
{
}
bool TinyRendererSetup::mouseMoveCallback(float x, float y)
{
return false;
}
bool TinyRendererSetup::mouseButtonCallback(int button, int state, float x, float y)
{
return false;
}
bool TinyRendererSetup::keyboardCallback(int key, int state)
{
return false;
}
void TinyRendererSetup::syncPhysicsToGraphics(GraphicsPhysicsBridge& gfxBridge)
{
}
CommonExampleInterface* TinyRendererCreateFunc(struct CommonExampleOptions& options)
{
return new TinyRendererSetup(options.m_guiHelper);
}

View file

@ -0,0 +1,6 @@
#ifndef TINYRENDERER_SETUP_H
#define TINYRENDERER_SETUP_H
class CommonExampleInterface* TinyRendererCreateFunc(struct CommonExampleOptions& options);
#endif //TINYRENDERER_SETUP_H

View file

@ -0,0 +1,202 @@
#include "TinyVRGui.h"
#include "../CommonInterfaces/CommonGUIHelperInterface.h"
#include "../ExampleBrowser/GwenGUISupport/GraphingTexture.h"
#include "../CommonInterfaces/Common2dCanvasInterface.h"
#include "../RenderingExamples/TimeSeriesCanvas.h"
#include "../RenderingExamples/TimeSeriesFontData.h"
#include "../Importers/ImportMeshUtility/b3ImportMeshUtility.h"
#include "../OpenGLWindow/GLInstanceGraphicsShape.h"
#include "../Utils/b3BulletDefaultFileIO.h"
#include "../CommonInterfaces/CommonRenderInterface.h"
#include "../CommonInterfaces/CommonParameterInterface.h"
struct TestCanvasInterface2 : public Common2dCanvasInterface
{
b3AlignedObjectArray<unsigned char>& m_texelsRGB;
int m_width;
int m_height;
TestCanvasInterface2(b3AlignedObjectArray<unsigned char>& texelsRGB, int width, int height)
: m_texelsRGB(texelsRGB),
m_width(width),
m_height(height)
{
}
virtual ~TestCanvasInterface2()
{
}
virtual int createCanvas(const char* canvasName, int width, int height, int posX, int posY)
{
return 0;
}
virtual void destroyCanvas(int canvasId)
{
}
virtual void setPixel(int canvasId, int x, int y, unsigned char red, unsigned char green, unsigned char blue, unsigned char alpha)
{
if (x >= 0 && x < m_width && y >= 0 && y < m_height)
{
m_texelsRGB[(x + y * m_width) * 3 + 0] = red;
m_texelsRGB[(x + y * m_width) * 3 + 1] = green;
m_texelsRGB[(x + y * m_width) * 3 + 2] = blue;
}
}
virtual void getPixel(int canvasId, int x, int y, unsigned char& red, unsigned char& green, unsigned char& blue, unsigned char& alpha)
{
if (x >= 0 && x < m_width && y >= 0 && y < m_height)
{
red = m_texelsRGB[(x + y * m_width) * 3 + 0];
green = m_texelsRGB[(x + y * m_width) * 3 + 1];
blue = m_texelsRGB[(x + y * m_width) * 3 + 2];
}
}
virtual void refreshImageData(int canvasId)
{
}
};
struct TinyVRGuiInternalData
{
CommonRenderInterface* m_renderer;
b3AlignedObjectArray<unsigned char> m_texelsRGB;
TestCanvasInterface2* m_testCanvas;
TimeSeriesCanvas* m_timeSeries;
int m_src;
int m_textureId;
int m_gfxObjectId;
TinyVRGuiInternalData()
: m_renderer(0),
m_testCanvas(0),
m_timeSeries(0),
m_src(-1),
m_textureId(-1),
m_gfxObjectId(-1)
{
}
};
TinyVRGui::TinyVRGui(struct ComboBoxParams& params, struct CommonRenderInterface* renderer)
{
m_data = new TinyVRGuiInternalData;
m_data->m_renderer = renderer;
}
TinyVRGui::~TinyVRGui()
{
delete m_data->m_timeSeries;
delete m_data->m_testCanvas;
delete m_data;
}
bool TinyVRGui::init()
{
{
int width = 256;
int height = 256;
m_data->m_texelsRGB.resize(width * height * 3);
for (int i = 0; i < width; i++)
for (int j = 0; j < height; j++)
{
m_data->m_texelsRGB[(i + j * width) * 3 + 0] = 155;
m_data->m_texelsRGB[(i + j * width) * 3 + 1] = 155;
m_data->m_texelsRGB[(i + j * width) * 3 + 2] = 255;
}
m_data->m_testCanvas = new TestCanvasInterface2(m_data->m_texelsRGB, width, height);
m_data->m_timeSeries = new TimeSeriesCanvas(m_data->m_testCanvas, width, height, "time series");
bool clearCanvas = false;
m_data->m_timeSeries->setupTimeSeries(3, 100, 0, clearCanvas);
m_data->m_timeSeries->addDataSource("Some sine wave", 255, 0, 0);
m_data->m_timeSeries->addDataSource("Some cosine wave", 0, 255, 0);
m_data->m_timeSeries->addDataSource("Delta Time (*10)", 0, 0, 255);
m_data->m_timeSeries->addDataSource("Tan", 255, 0, 255);
m_data->m_timeSeries->addDataSource("Some cosine wave2", 255, 255, 0);
m_data->m_timeSeries->addDataSource("Empty source2", 255, 0, 255);
m_data->m_textureId = m_data->m_renderer->registerTexture(&m_data->m_texelsRGB[0], width, height);
{
const char* fileName = "cube.obj"; //"textured_sphere_smooth.obj";
//fileName = "cube.obj";
int shapeId = -1;
b3ImportMeshData meshData;
b3BulletDefaultFileIO fileIO;
if (b3ImportMeshUtility::loadAndRegisterMeshFromFileInternal(fileName, meshData,&fileIO))
{
shapeId = m_data->m_renderer->registerShape(&meshData.m_gfxShape->m_vertices->at(0).xyzw[0],
meshData.m_gfxShape->m_numvertices,
&meshData.m_gfxShape->m_indices->at(0),
meshData.m_gfxShape->m_numIndices,
B3_GL_TRIANGLES,
m_data->m_textureId);
float position[4] = {0, 0, 2, 1};
float orn[4] = {0, 0, 0, 1};
float color[4] = {1, 1, 1, 1};
float scaling[4] = {.1, .1, .1, 1};
m_data->m_gfxObjectId = m_data->m_renderer->registerGraphicsInstance(shapeId, position, orn, color, scaling);
m_data->m_renderer->writeTransforms();
meshData.m_gfxShape->m_scaling[0] = scaling[0];
meshData.m_gfxShape->m_scaling[1] = scaling[1];
meshData.m_gfxShape->m_scaling[2] = scaling[2];
delete meshData.m_gfxShape;
if (!meshData.m_isCached)
{
free(meshData.m_textureImage1);
}
}
}
}
m_data->m_renderer->writeTransforms();
return true;
}
void TinyVRGui::tick(b3Scalar deltaTime, const b3Transform& guiWorldTransform)
{
float time = m_data->m_timeSeries->getCurrentTime();
float v = sinf(time);
m_data->m_timeSeries->insertDataAtCurrentTime(v, 0, true);
v = cosf(time);
m_data->m_timeSeries->insertDataAtCurrentTime(v, 1, true);
v = tanf(time);
m_data->m_timeSeries->insertDataAtCurrentTime(v, 3, true);
m_data->m_timeSeries->insertDataAtCurrentTime(deltaTime * 10, 2, true);
m_data->m_timeSeries->nextTick();
m_data->m_renderer->updateTexture(m_data->m_textureId, &m_data->m_texelsRGB[0]);
m_data->m_renderer->writeSingleInstanceTransformToCPU(guiWorldTransform.getOrigin(), guiWorldTransform.getRotation(), m_data->m_gfxObjectId);
m_data->m_renderer->writeTransforms();
}
void TinyVRGui::clearTextArea()
{
int width = 256;
int height = 50;
for (int i = 0; i < width; i++)
for (int j = 0; j < height; j++)
{
m_data->m_texelsRGB[(i + j * width) * 3 + 0] = 155;
m_data->m_texelsRGB[(i + j * width) * 3 + 1] = 155;
m_data->m_texelsRGB[(i + j * width) * 3 + 2] = 255;
}
}
void TinyVRGui::grapicalPrintf(const char* str, int rasterposx, int rasterposy, unsigned char red, unsigned char green, unsigned char blue, unsigned char alpha)
{
m_data->m_timeSeries->grapicalPrintf(str, sTimeSeriesFontData, rasterposx, rasterposy, red, green, blue, alpha);
}

View file

@ -0,0 +1,22 @@
#ifndef TINY_VR_GUI_H
#define TINY_VR_GUI_H
#include "Bullet3Common/b3Transform.h"
class TinyVRGui
{
struct TinyVRGuiInternalData* m_data;
public:
TinyVRGui(struct ComboBoxParams& params, struct CommonRenderInterface* renderer);
virtual ~TinyVRGui();
bool init();
void tick(b3Scalar deltaTime, const b3Transform& guiWorldTransform);
void clearTextArea();
void grapicalPrintf(const char* str, int rasterposx, int rasterposy, unsigned char red, unsigned char green, unsigned char blue, unsigned char alpha);
};
#endif //TINY_VR_GUI_H