work-in-progress tinyrenderer -> shared memory API synthetic camera image

This commit is contained in:
erwin coumans
2016-05-17 23:57:19 -07:00
parent 876c9e57fe
commit 606f78da43
17 changed files with 319 additions and 122 deletions

View File

@@ -9,6 +9,6 @@ newmtl cube
Kd 0.5880 0.5880 0.5880 Kd 0.5880 0.5880 0.5880
Ks 0.0000 0.0000 0.0000 Ks 0.0000 0.0000 0.0000
Ke 0.0000 0.0000 0.0000 Ke 0.0000 0.0000 0.0000
map_Ka cube.png map_Ka cube.tga
map_Kd cube.png map_Kd cube_diffuse.tga

View File

@@ -15,10 +15,10 @@ struct DrawGridData
int upAxis; int upAxis;
float gridColor[4]; float gridColor[4];
DrawGridData() DrawGridData(int upAxis=1)
:gridSize(10), :gridSize(10),
upOffset(0.001f), upOffset(0.001f),
upAxis(1) upAxis(upAxis)
{ {
gridColor[0] = 0.6f; gridColor[0] = 0.6f;
gridColor[1] = 0.6f; gridColor[1] = 0.6f;
@@ -119,6 +119,7 @@ struct CommonGraphicsApp
virtual void swapBuffer() = 0; virtual void swapBuffer() = 0;
virtual void drawText( const char* txt, int posX, int posY) = 0; virtual void drawText( const char* txt, int posX, int posY) = 0;
virtual void drawText3D( const char* txt, float posX, float posZY, float posZ, float size)=0; virtual void drawText3D( const char* txt, float posX, float posZY, float posZ, float size)=0;
virtual void drawTexturedRect(float x0, float y0, float x1, float y1, float color[4], float u0,float v0, float u1, float v1, int useRGBA)=0;
virtual int registerCubeShape(float halfExtentsX,float halfExtentsY, float halfExtentsZ, int textureIndex = -1, float textureScaling = 1)=0; virtual int registerCubeShape(float halfExtentsX,float halfExtentsY, float halfExtentsZ, int textureIndex = -1, float textureScaling = 1)=0;
virtual int registerGraphicsUnitSphereShape(EnumSphereLevelOfDetail lod, int textureId=-1) = 0; virtual int registerGraphicsUnitSphereShape(EnumSphereLevelOfDetail lod, int textureId=-1) = 0;

View File

@@ -12,9 +12,14 @@
#include "stb_image/stb_image.h" #include "stb_image/stb_image.h"
int b3ImportMeshUtility::loadAndRegisterMeshFromFile(const std::string& fileName, CommonRenderInterface* renderer) bool b3ImportMeshUtility::loadAndRegisterMeshFromFileInternal(const std::string& fileName, b3ImportMeshData& meshData)
{ {
int shapeId = -1;
meshData.m_gfxShape = 0;
meshData.m_textureImage = 0;
meshData.m_textureHeight = 0;
meshData.m_textureWidth = 0;
char relativeFileName[1024]; char relativeFileName[1024];
if (b3ResourcePath::findResourcePath(fileName.c_str(), relativeFileName, 1024)) if (b3ResourcePath::findResourcePath(fileName.c_str(), relativeFileName, 1024))
@@ -22,22 +27,13 @@ int b3ImportMeshUtility::loadAndRegisterMeshFromFile(const std::string& fileName
char pathPrefix[1024]; char pathPrefix[1024];
b3FileUtils::extractPath(relativeFileName, pathPrefix, 1024); b3FileUtils::extractPath(relativeFileName, pathPrefix, 1024);
btVector3 shift(0,0,0); btVector3 shift(0,0,0);
// int index=10;
{
std::vector<tinyobj::shape_t> shapes; std::vector<tinyobj::shape_t> shapes;
std::string err = tinyobj::LoadObj(shapes, relativeFileName, pathPrefix); std::string err = tinyobj::LoadObj(shapes, relativeFileName, pathPrefix);
GLInstanceGraphicsShape* gfxShape = btgCreateGraphicsShapeFromWavefrontObj(shapes); GLInstanceGraphicsShape* gfxShape = btgCreateGraphicsShapeFromWavefrontObj(shapes);
int textureIndex = -1; int textureIndex = -1;
//try to load some texture //try to load some texture
for (int i=0;i<shapes.size();i++) for (int i=0;i<shapes.size();i++)
@@ -48,7 +44,7 @@ int b3ImportMeshUtility::loadAndRegisterMeshFromFile(const std::string& fileName
int width,height,n; int width,height,n;
const char* filename = shape.material.diffuse_texname.c_str(); const char* filename = shape.material.diffuse_texname.c_str();
const unsigned char* image=0; unsigned char* image=0;
const char* prefix[]={ pathPrefix,"./","./data/","../data/","../../data/","../../../data/","../../../../data/"}; const char* prefix[]={ pathPrefix,"./","./data/","../data/","../../data/","../../../data/","../../../../data/"};
int numprefix = sizeof(prefix)/sizeof(const char*); int numprefix = sizeof(prefix)/sizeof(const char*);
@@ -61,37 +57,59 @@ int b3ImportMeshUtility::loadAndRegisterMeshFromFile(const std::string& fileName
if (b3ResourcePath::findResourcePath(relativeFileName, relativeFileName2, 1024)) if (b3ResourcePath::findResourcePath(relativeFileName, relativeFileName2, 1024))
{ {
image = stbi_load(relativeFileName, &width, &height, &n, 3); image = stbi_load(relativeFileName, &width, &height, &n, 3);
meshData.m_textureImage = image;
if (image)
{
meshData.m_textureWidth = width;
meshData.m_textureHeight = height;
} else
{
meshData.m_textureWidth = 0;
meshData.m_textureHeight = 0;
}
} else } else
{ {
b3Warning("not found %s\n",relativeFileName); b3Warning("not found %s\n",relativeFileName);
} }
} }
if (image)
{
textureIndex = renderer->registerTexture(image,width,height);
if (textureIndex>=0)
{
break;
}
} }
} }
meshData.m_gfxShape = gfxShape;
return true;
} }
shapeId = renderer->registerShape(&gfxShape->m_vertices->at(0).xyzw[0], gfxShape->m_numvertices, &gfxShape->m_indices->at(0), gfxShape->m_numIndices,B3_GL_TRIANGLES,textureIndex);
}
}
else else
{ {
b3Warning("Cannot find %s\n", fileName.c_str()); b3Warning("Cannot find %s\n", fileName.c_str());
} }
return shapeId;
return false;
} }
int b3ImportMeshUtility::loadAndRegisterMeshFromFile(const std::string& fileName, CommonRenderInterface* renderer)
{
int shapeId = -1;
b3ImportMeshData meshData;
if (b3ImportMeshUtility::loadAndRegisterMeshFromFileInternal(fileName, meshData))
{
int textureIndex = 0;
if (meshData.m_textureImage)
{
textureIndex = renderer->registerTexture(meshData.m_textureImage,meshData.m_textureWidth,meshData.m_textureHeight);
}
shapeId = 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,
textureIndex);
delete meshData.m_gfxShape;
delete meshData.m_textureImage;
}
return shapeId;
}

View File

@@ -3,11 +3,22 @@
#include <string> #include <string>
struct b3ImportMeshData
{
struct GLInstanceGraphicsShape* m_gfxShape;
unsigned char* m_textureImage;//in 3 component 8-bit RGB data
int m_textureWidth;
int m_textureHeight;
};
class b3ImportMeshUtility class b3ImportMeshUtility
{ {
public: public:
static int loadAndRegisterMeshFromFile(const std::string& fileName, class CommonRenderInterface* renderer); static int loadAndRegisterMeshFromFile(const std::string& fileName, class CommonRenderInterface* renderer);
static bool loadAndRegisterMeshFromFileInternal(const std::string& fileName, b3ImportMeshData& meshData);
}; };

View File

@@ -18,6 +18,7 @@ public:
virtual void swapBuffer(); virtual void swapBuffer();
virtual void drawText( const char* txt, int posX, int posY); virtual void drawText( const char* txt, int posX, int posY);
virtual void drawTexturedRect(float x0, float y0, float x1, float y1, float color[4], float u0,float v0, float u1, float v1, int useRGBA){};
virtual void setBackgroundColor(float red, float green, float blue); virtual void setBackgroundColor(float red, float green, float blue);
virtual int registerCubeShape(float halfExtentsX,float halfExtentsY, float halfExtentsZ, int textureIndex = -1, float textureScaling = 1) virtual int registerCubeShape(float halfExtentsX,float halfExtentsY, float halfExtentsZ, int textureIndex = -1, float textureScaling = 1)
{ {

View File

@@ -425,6 +425,18 @@ void SimpleOpenGL3App::drawText( const char* txt, int posXi, int posYi)
glDisable(GL_BLEND); glDisable(GL_BLEND);
} }
void SimpleOpenGL3App::drawTexturedRect(float x0, float y0, float x1, float y1, float color[4], float u0,float v0, float u1, float v1, int useRGBA)
{
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
m_primRenderer->drawTexturedRect(x0,y0,x1,y1,color,u0,v0,u1,v1,useRGBA);
glDisable(GL_BLEND);
}
struct GfxVertex struct GfxVertex
{ {
float x,y,z,w; float x,y,z,w;

View File

@@ -32,6 +32,7 @@ struct SimpleOpenGL3App : public CommonGraphicsApp
virtual void swapBuffer(); virtual void swapBuffer();
virtual void drawText( const char* txt, int posX, int posY); virtual void drawText( const char* txt, int posX, int posY);
virtual void drawText3D( const char* txt, float posX, float posZY, float posZ, float size); virtual void drawText3D( const char* txt, float posX, float posZY, float posZ, float size);
virtual void drawTexturedRect(float x0, float y0, float x1, float y1, float color[4], float u0,float v0, float u1, float v1, int useRGBA);
struct sth_stash* getFontStash(); struct sth_stash* getFontStash();

View File

@@ -6,25 +6,24 @@
#include "Bullet3Common/b3AlignedObjectArray.h" #include "Bullet3Common/b3AlignedObjectArray.h"
#include "../CommonInterfaces/CommonRenderInterface.h" #include "../CommonInterfaces/CommonRenderInterface.h"
#include "../TinyRenderer/TinyRenderer.h" #include "../TinyRenderer/TinyRenderer.h"
#include "../CommonInterfaces/Common2dCanvasInterface.h" #include "../CommonInterfaces/Common2dCanvasInterface.h"
//#include "BulletCollision/NarrowPhaseCollision/btVoronoiSimplexSolver.h"
#include "BulletCollision/NarrowPhaseCollision/btSubSimplexConvexCast.h" #include "BulletCollision/NarrowPhaseCollision/btSubSimplexConvexCast.h"
//#include "BulletCollision/NarrowPhaseCollision/btGjkConvexCast.h"
//#include "BulletCollision/NarrowPhaseCollision/btContinuousConvexCollision.h"
#include "../CommonInterfaces/CommonExampleInterface.h" #include "../CommonInterfaces/CommonExampleInterface.h"
#include "LinearMath/btAlignedObjectArray.h" #include "LinearMath/btAlignedObjectArray.h"
#include "btBulletCollisionCommon.h" #include "btBulletCollisionCommon.h"
#include "../CommonInterfaces/CommonGUIHelperInterface.h" #include "../CommonInterfaces/CommonGUIHelperInterface.h"
#include "../ExampleBrowser/CollisionShape2TriangleMesh.h" #include "../ExampleBrowser/CollisionShape2TriangleMesh.h"
#include "../Importers/ImportMeshUtility/b3ImportMeshUtility.h"
#include "../../OpenGLWindow/GLInstanceGraphicsShape.h"
struct TinyRendererSetup : public CommonExampleInterface struct TinyRendererSetup : public CommonExampleInterface
{ {
struct GUIHelperInterface* m_guiHelper;
struct CommonGraphicsApp* m_app; struct CommonGraphicsApp* m_app;
struct TinyRendererSetupInternalData* m_internalData; struct TinyRendererSetupInternalData* m_internalData;
TinyRendererSetup(struct CommonGraphicsApp* app); TinyRendererSetup(struct GUIHelperInterface* guiHelper);
virtual ~TinyRendererSetup(); virtual ~TinyRendererSetup();
@@ -52,8 +51,7 @@ struct TinyRendererSetup : public CommonExampleInterface
struct TinyRendererSetupInternalData struct TinyRendererSetupInternalData
{ {
int m_canvasIndex;
struct Common2dCanvasInterface* m_canvas;
TGAImage m_rgbColorBuffer; TGAImage m_rgbColorBuffer;
b3AlignedObjectArray<float> m_depthBuffer; b3AlignedObjectArray<float> m_depthBuffer;
@@ -70,24 +68,30 @@ struct TinyRendererSetupInternalData
btScalar m_roll; btScalar m_roll;
btScalar m_yaw; btScalar m_yaw;
int m_textureHandle;
TinyRendererSetupInternalData(int width, int height) TinyRendererSetupInternalData(int width, int height)
:m_canvasIndex(-1), :m_roll(0),
m_canvas(0),
m_roll(0),
m_pitch(0), m_pitch(0),
m_yaw(0), m_yaw(0),
m_width(width), m_width(width),
m_height(height), m_height(height),
m_rgbColorBuffer(width,height,TGAImage::RGB) m_rgbColorBuffer(width,height,TGAImage::RGB),
m_textureHandle(0)
{ {
m_depthBuffer.resize(m_width*m_height);
#if 0
btConeShape* cone = new btConeShape(1,1); btConeShape* cone = new btConeShape(1,1);
btSphereShape* sphere = new btSphereShape(1); btSphereShape* sphere = new btSphereShape(1);
btBoxShape* box = new btBoxShape (btVector3(1,1,1)); btBoxShape* box = new btBoxShape (btVector3(0.5,0.5,0.5));
m_shapePtr.push_back(cone);
m_shapePtr.push_back(sphere);
m_shapePtr.push_back(box); m_shapePtr.push_back(box);
m_depthBuffer.resize(m_width*m_height); m_shapePtr.push_back(box);
//m_shapePtr.push_back(sphere);
//m_shapePtr.push_back(box);
for (int i=0;i<m_shapePtr.size();i++) for (int i=0;i<m_shapePtr.size();i++)
{ {
@@ -99,13 +103,17 @@ struct TinyRendererSetupInternalData
ident.setIdentity(); ident.setIdentity();
CollisionShape2TriangleMesh(m_shapePtr[i],ident,vertexPositions,vertexNormals,indicesOut); CollisionShape2TriangleMesh(m_shapePtr[i],ident,vertexPositions,vertexNormals,indicesOut);
//ob->createCube(0.5,0.5,0.5);//createCube
ob->loadModel("cube.obj");
m_renderObjects.push_back(ob); m_renderObjects.push_back(ob);
ob->registerMesh2(vertexPositions,vertexNormals,indicesOut); //ob->registerMesh2(vertexPositions,vertexNormals,indicesOut);
} }
//ob->registerMeshShape( //ob->registerMeshShape(
updateTransforms(); updateTransforms();
#endif
} }
void updateTransforms() void updateTransforms()
{ {
@@ -114,7 +122,8 @@ struct TinyRendererSetupInternalData
for (int i=0;i<numObjects;i++) for (int i=0;i<numObjects;i++)
{ {
m_transforms[i].setIdentity(); m_transforms[i].setIdentity();
btVector3 pos(0.f,-(2.5* numObjects * 0.5)+i*2.5f, 0.f); //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].setIdentity();
m_transforms[i].setOrigin( pos ); m_transforms[i].setOrigin( pos );
btQuaternion orn; btQuaternion orn;
@@ -124,20 +133,79 @@ struct TinyRendererSetupInternalData
m_transforms[i].setRotation(orn); m_transforms[i].setRotation(orn);
} }
} }
m_pitch += 0.005f; //m_pitch += 0.005f;
m_yaw += 0.01f; //m_yaw += 0.01f;
} }
}; };
TinyRendererSetup::TinyRendererSetup(struct CommonGraphicsApp* app) TinyRendererSetup::TinyRendererSetup(struct GUIHelperInterface* gui)
{ {
m_app = app; m_guiHelper = gui;
m_internalData = new TinyRendererSetupInternalData(128,128); m_app = gui->getAppInterface();
m_internalData = new TinyRendererSetupInternalData(gui->getAppInterface()->m_window->getWidth(),gui->getAppInterface()->m_window->getHeight());
m_app->m_renderer->enableBlend(true);
const char* fileName = "cube.obj";
{
{
int shapeId = -1;
b3ImportMeshData meshData;
if (b3ImportMeshUtility::loadAndRegisterMeshFromFileInternal(fileName, meshData))
{
int textureIndex = 0;
if (meshData.m_textureImage)
{
textureIndex = m_guiHelper->getRenderInterface()->registerTexture(meshData.m_textureImage,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,0.4};
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_width,m_internalData->m_height,
m_internalData->m_rgbColorBuffer,
m_internalData->m_depthBuffer);
//ob->loadModel("cube.obj");
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, meshData.m_textureImage,meshData.m_textureWidth,meshData.m_textureHeight);
m_internalData->m_renderObjects.push_back(ob);
delete meshData.m_gfxShape;
delete meshData.m_textureImage;
}
}
}
} }
TinyRendererSetup::~TinyRendererSetup() TinyRendererSetup::~TinyRendererSetup()
{ {
m_app->m_renderer->enableBlend(false);
delete m_internalData; delete m_internalData;
} }
@@ -145,50 +213,23 @@ void TinyRendererSetup::initPhysics()
{ {
//request a visual bitma/texture we can render to //request a visual bitma/texture we can render to
m_app->setUpAxis(2); m_app->setUpAxis(2);
m_internalData->m_canvas = m_app->m_2dCanvasInterface; CommonRenderInterface* render = m_app->m_renderer;
if (m_internalData->m_canvas)
{
m_internalData->m_canvasIndex = m_internalData->m_canvas->createCanvas("tinyrenderer",m_internalData->m_width,m_internalData->m_height);
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);
}
m_internalData->m_textureHandle = render->registerTexture(m_internalData->m_rgbColorBuffer.buffer(),m_internalData->m_width,m_internalData->m_height);
} }
void TinyRendererSetup::exitPhysics() void TinyRendererSetup::exitPhysics()
{ {
if (m_internalData->m_canvas && m_internalData->m_canvasIndex>=0)
{
m_internalData->m_canvas->destroyCanvas(m_internalData->m_canvasIndex);
}
} }
void TinyRendererSetup::stepSimulation(float deltaTime) void TinyRendererSetup::stepSimulation(float deltaTime)
{ {
m_guiHelper->getRenderInterface()->renderScene();
m_internalData->updateTransforms(); m_internalData->updateTransforms();
@@ -209,8 +250,10 @@ void TinyRendererSetup::stepSimulation(float deltaTime)
ATTRIBUTE_ALIGNED16(btScalar modelMat2[16]); ATTRIBUTE_ALIGNED16(btScalar modelMat2[16]);
ATTRIBUTE_ALIGNED16(float viewMat[16]); ATTRIBUTE_ALIGNED16(float viewMat[16]);
ATTRIBUTE_ALIGNED16(float projMat[16]);
CommonRenderInterface* render = this->m_app->m_renderer; CommonRenderInterface* render = this->m_app->m_renderer;
render->getActiveCamera()->getCameraViewMatrix(viewMat); render->getActiveCamera()->getCameraViewMatrix(viewMat);
render->getActiveCamera()->getCameraProjectionMatrix(projMat);
@@ -227,25 +270,25 @@ void TinyRendererSetup::stepSimulation(float deltaTime)
{ {
m_internalData->m_renderObjects[o]->m_modelMatrix[i][j] = float(modelMat2[i+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_viewMatrix[i][j] = viewMat[i+4*j];
m_internalData->m_renderObjects[o]->m_projectionMatrix[i][j] = projMat[i+4*j];
float eye[4];
float center[4];
render->getActiveCamera()->getCameraPosition(eye);
render->getActiveCamera()->getCameraTargetPosition(center);
m_internalData->m_renderObjects[o]->m_eye.setValue(eye[0],eye[1],eye[2]);
m_internalData->m_renderObjects[o]->m_center.setValue(center[0],center[1],center[2]);
} }
} }
TinyRenderer::renderObject(*m_internalData->m_renderObjects[o]); 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,0.5};
m_app->drawTexturedRect(0,0,m_app->m_window->getWidth(), m_app->m_window->getHeight(),color,0,0,1,1,true);
for(int y=0;y<m_internalData->m_height;++y)
{
for(int x=0;x<m_internalData->m_width;++x)
{
const TGAColor& color = m_internalData->m_rgbColorBuffer.get(x,y);
m_internalData->m_canvas->setPixel(m_internalData->m_canvasIndex,x,(m_internalData->m_height-1-y),
color.bgra[2],color.bgra[1],color.bgra[0],255);
}
}
//m_internalData->m_canvas->setPixel(m_internalData->m_canvasIndex,x,y,255,0,0,255);
m_internalData->m_canvas->refreshImageData(m_internalData->m_canvasIndex);
} }
@@ -275,5 +318,5 @@ void TinyRendererSetup::syncPhysicsToGraphics(GraphicsPhysicsBridge& gfxBridge)
CommonExampleInterface* TinyRendererCreateFunc(struct CommonExampleOptions& options) CommonExampleInterface* TinyRendererCreateFunc(struct CommonExampleOptions& options)
{ {
return new TinyRendererSetup(options.m_guiHelper->getAppInterface()); return new TinyRendererSetup(options.m_guiHelper);
} }

View File

@@ -676,3 +676,34 @@ void b3GetDebugLines(b3PhysicsClientHandle physClient, struct b3DebugLines* l
} }
} }
///request an image from a simulated camera, using a software renderer.
b3SharedMemoryCommandHandle b3InitRequestCameraImage(b3PhysicsClientHandle physClient)
{
PhysicsClient* cl = (PhysicsClient* ) physClient;
b3Assert(cl);
b3Assert(cl->canSubmitCommand());
struct SharedMemoryCommand* command = cl->getAvailableSharedMemoryCommand();
b3Assert(command);
command->m_type =CMD_REQUEST_CAMERA_IMAGE_DATA;
return (b3SharedMemoryCommandHandle) command;
}
void b3RequestCameraImageSetResolution(b3SharedMemoryCommandHandle command, int pixelWidth, int pixelHeight)
{
}
void b3RequestCameraImageSetCameraMatrices(b3SharedMemoryCommandHandle command, float viewMatrix[16], float projectionMatrix[16])
{
}
void b3GetCameraImageData(b3PhysicsClientHandle physClient, struct b3CameraImageData* imageData)
{
PhysicsClient* cl = (PhysicsClient* ) physClient;
if (cl)
{
}
}

View File

@@ -65,6 +65,12 @@ b3SharedMemoryCommandHandle b3InitRequestDebugLinesCommand(b3PhysicsClientHandle
///status CMD_DEBUG_LINES_COMPLETED ///status CMD_DEBUG_LINES_COMPLETED
void b3GetDebugLines(b3PhysicsClientHandle physClient, struct b3DebugLines* lines); void b3GetDebugLines(b3PhysicsClientHandle physClient, struct b3DebugLines* lines);
///request an image from a simulated camera, using a software renderer.
b3SharedMemoryCommandHandle b3InitRequestCameraImage(b3PhysicsClientHandle physClient);
void b3RequestCameraImageSetResolution(b3SharedMemoryCommandHandle command, int pixelWidth, int pixelHeight);
void b3RequestCameraImageSetCameraMatrices(b3SharedMemoryCommandHandle command, float viewMatrix[16], float projectionMatrix[16]);
void b3GetCameraImageData(b3PhysicsClientHandle physClient, struct b3CameraImageData* imageData);
b3SharedMemoryCommandHandle b3InitPhysicsParamCommand(b3PhysicsClientHandle physClient); b3SharedMemoryCommandHandle b3InitPhysicsParamCommand(b3PhysicsClientHandle physClient);
int b3PhysicsParamSetGravity(b3SharedMemoryCommandHandle commandHandle, double gravx,double gravy, double gravz); int b3PhysicsParamSetGravity(b3SharedMemoryCommandHandle commandHandle, double gravx,double gravy, double gravz);

View File

@@ -962,6 +962,13 @@ bool PhysicsServerCommandProcessor::processCommand(const struct SharedMemoryComm
break; break;
} }
case CMD_REQUEST_CAMERA_IMAGE_DATA:
{
serverStatusOut.m_type = CMD_CLIENT_COMMAND_COMPLETED;
hasStatus = true;
break;
}
case CMD_LOAD_URDF: case CMD_LOAD_URDF:
{ {

View File

@@ -23,6 +23,7 @@ enum EnumSharedMemoryClientCommand
CMD_PICK_BODY, CMD_PICK_BODY,
CMD_MOVE_PICKED_BODY, CMD_MOVE_PICKED_BODY,
CMD_REMOVE_PICKING_CONSTRAINT_BODY, CMD_REMOVE_PICKING_CONSTRAINT_BODY,
CMD_REQUEST_CAMERA_IMAGE_DATA,
CMD_MAX_CLIENT_COMMANDS CMD_MAX_CLIENT_COMMANDS
}; };
@@ -105,6 +106,14 @@ struct b3DebugLines
const float* m_linesColor;//float red,green,blue times 'm_numDebugLines'. const float* m_linesColor;//float red,green,blue times 'm_numDebugLines'.
}; };
struct b3CameraImageData
{
int m_pixelWidth;
int m_pixelHeight;
const unsigned char* m_rgbColorData;//3*m_pixelWidth*m_pixelHeight bytes
const float* m_depthValues;//m_pixelWidth*m_pixelHeight floats
};
///b3LinkState provides extra information such as the Cartesian world coordinates ///b3LinkState provides extra information such as the Cartesian world coordinates
///center of mass (COM) of the link, relative to the world reference frame. ///center of mass (COM) of the link, relative to the world reference frame.
///Orientation is a quaternion x,y,z,w ///Orientation is a quaternion x,y,z,w
@@ -127,4 +136,5 @@ enum {
CONTROL_MODE_POSITION_VELOCITY_PD, CONTROL_MODE_POSITION_VELOCITY_PD,
}; };
#endif//SHARED_MEMORY_PUBLIC_H #endif//SHARED_MEMORY_PUBLIC_H

View File

@@ -70,7 +70,8 @@ struct Shader : public IShader {
Vec3f n = (B*m_model->normal(uv)).normalize(); Vec3f n = (B*m_model->normal(uv)).normalize();
float diff = b3Min(b3Max(0.f, n*m_light_dir_local+0.3f),1.f); float diff = 1;//b3Min(b3Max(0.f, n*0.3f),1.f);
//float diff = b3Min(b3Max(0.f, n*m_light_dir_local+0.3f),1.f);
//float diff = b3Max(0.f, n*m_light_dir_local); //float diff = b3Max(0.f, n*m_light_dir_local);
color = m_model->diffuse(uv)*diff; color = m_model->diffuse(uv)*diff;
@@ -90,11 +91,13 @@ m_userIndex(-1)
{ {
Vec3f eye(1,1,3); Vec3f eye(1,1,3);
Vec3f center(0,0,0); Vec3f center(0,0,0);
Vec3f up(0,1,0); Vec3f up(0,0,1);
m_modelMatrix = Matrix::identity(); m_modelMatrix = Matrix::identity();
m_viewMatrix = lookat(eye, center, up); m_viewMatrix = lookat(eye, center, up);
m_viewportMatrix = viewport(width/8, height/8, width*3/4, height*3/4); //m_viewportMatrix = viewport(width/8, height/8, width*3/4, height*3/4);
//m_viewportMatrix = viewport(width/8, height/8, width*3/4, height*3/4);
m_viewportMatrix = viewport(0,0,width,height);
m_projectionMatrix = projection(-1.f/(eye-center).norm()); m_projectionMatrix = projection(-1.f/(eye-center).norm());
} }
@@ -113,16 +116,23 @@ void TinyRenderObjectData::loadModel(const char* fileName)
} }
void TinyRenderObjectData::registerMeshShape(const float* vertices, int numVertices,const int* indices, int numIndices) void TinyRenderObjectData::registerMeshShape(const float* vertices, int numVertices,const int* indices, int numIndices,
unsigned char* textureImage, int textureWidth, int textureHeight)
{ {
if (0==m_model) if (0==m_model)
{ {
m_model = new Model(); m_model = new Model();
if (textureImage)
{
m_model->setDiffuseTextureFromData(textureImage,textureWidth,textureHeight);
} else
{
char relativeFileName[1024]; char relativeFileName[1024];
if (b3ResourcePath::findResourcePath("floor_diffuse.tga", relativeFileName, 1024)) if (b3ResourcePath::findResourcePath("floor_diffuse.tga", relativeFileName, 1024))
{ {
m_model->loadDiffuseTexture(relativeFileName); m_model->loadDiffuseTexture(relativeFileName);
} }
}
for (int i=0;i<numVertices;i++) for (int i=0;i<numVertices;i++)
{ {
@@ -223,6 +233,20 @@ void TinyRenderer::renderObject(TinyRenderObjectData& renderData)
if (0==model) if (0==model)
return; return;
Vec3f eye(renderData.m_eye[0],renderData.m_eye[1],renderData.m_eye[2]);
Vec3f center(renderData.m_center[0],renderData.m_center[1],renderData.m_center[2]);
Vec3f up(0,0,1);
//renderData.m_viewMatrix = lookat(eye, center, up);
int width = renderData.m_width;
int height = renderData.m_height;
//renderData.m_viewportMatrix = viewport(width/8, height/8, width*3/4, height*3/4);
renderData.m_viewportMatrix = viewport(0,0,renderData.m_width,renderData.m_height);
//renderData.m_projectionMatrix = projection(-1.f/(eye-center).norm());
b3AlignedObjectArray<float>& zbuffer = renderData.m_depthBuffer; b3AlignedObjectArray<float>& zbuffer = renderData.m_depthBuffer;
TGAImage& frame = renderData.m_rgbColorBuffer; TGAImage& frame = renderData.m_rgbColorBuffer;
@@ -233,7 +257,10 @@ void TinyRenderer::renderObject(TinyRenderObjectData& renderData)
Matrix modelViewMatrix = renderData.m_viewMatrix*renderData.m_modelMatrix; Matrix modelViewMatrix = renderData.m_viewMatrix*renderData.m_modelMatrix;
Shader shader(model, light_dir_local, modelViewMatrix, renderData.m_projectionMatrix); Shader shader(model, light_dir_local, modelViewMatrix, renderData.m_projectionMatrix);
for (int i=0; i<model->nfaces(); i++) { for (int i=0; i<model->nfaces(); i++) {
for (int j=0; j<3; j++) { for (int j=0; j<3; j++) {
shader.vertex(i, j); shader.vertex(i, j);
} }

View File

@@ -17,6 +17,9 @@ struct TinyRenderObjectData
Matrix m_projectionMatrix; Matrix m_projectionMatrix;
Matrix m_viewportMatrix; Matrix m_viewportMatrix;
btVector3 m_eye;
btVector3 m_center;
//Model (vertices, indices, textures, shader) //Model (vertices, indices, textures, shader)
Matrix m_modelMatrix; Matrix m_modelMatrix;
class Model* m_model; class Model* m_model;
@@ -33,7 +36,8 @@ struct TinyRenderObjectData
void loadModel(const char* fileName); void loadModel(const char* fileName);
void createCube(float HalfExtentsX,float HalfExtentsY,float HalfExtentsZ); void createCube(float HalfExtentsX,float HalfExtentsY,float HalfExtentsZ);
void registerMeshShape(const float* vertices, int numVertices,const int* indices, int numIndices); void registerMeshShape(const float* vertices, int numVertices,const int* indices, int numIndices,
unsigned char* textureImage=0, int textureWidth=0, int textureHeight=0);
void registerMesh2(btAlignedObjectArray<btVector3>& vertices, btAlignedObjectArray<btVector3>& normals,btAlignedObjectArray<int>& indices); void registerMesh2(btAlignedObjectArray<btVector3>& vertices, btAlignedObjectArray<btVector3>& normals,btAlignedObjectArray<int>& indices);

View File

@@ -49,6 +49,28 @@ Model::Model():verts_(), faces_(), norms_(), uv_(), diffusemap_(), normalmap_(),
{ {
} }
void Model::setDiffuseTextureFromData(unsigned char* textureImage,int textureWidth,int textureHeight)
{
diffusemap_ = TGAImage(textureWidth, textureHeight, TGAImage::RGB);
for (int i=0;i<textureWidth;i++)
{
for (int j=0;j<textureHeight;j++)
{
TGAColor color;
color.bgra[0] = textureImage[(i+j*textureWidth)*3+0];
color.bgra[1] = textureImage[(i+j*textureWidth)*3+1];
color.bgra[2] = textureImage[(i+j*textureWidth)*3+2];
color.bgra[3] = 255;
color.bytespp = 3;
diffusemap_.set(i,j,color);
}
}
diffusemap_.flip_vertically();
}
void Model::loadDiffuseTexture(const char* relativeFileName) void Model::loadDiffuseTexture(const char* relativeFileName)
{ {
diffusemap_.read_tga_file(relativeFileName); diffusemap_.read_tga_file(relativeFileName);

View File

@@ -19,6 +19,7 @@ public:
Model(const char *filename); Model(const char *filename);
Model(); Model();
void loadDiffuseTexture(const char* relativeFileName); void loadDiffuseTexture(const char* relativeFileName);
void setDiffuseTextureFromData(unsigned char* textureImage,int textureWidth,int textureHeight);
void addVertex(float x,float y,float z, float normalX, float normalY, float normalZ, float u, float v); void addVertex(float x,float y,float z, float normalX, float normalY, float normalZ, float u, float v);
void addTriangle(int vertexposIndex0, int normalIndex0, int uvIndex0, void addTriangle(int vertexposIndex0, int normalIndex0, int uvIndex0,
int vertexposIndex1, int normalIndex1, int uvIndex1, int vertexposIndex1, int normalIndex1, int uvIndex1,

View File

@@ -93,8 +93,10 @@ void triangle(mat<4,3,float> &clipc, IShader &shader, TGAImage &image, float *zb
Vec3f bc_clip = Vec3f(bc_screen.x/pts[0][3], bc_screen.y/pts[1][3], bc_screen.z/pts[2][3]); Vec3f bc_clip = Vec3f(bc_screen.x/pts[0][3], bc_screen.y/pts[1][3], bc_screen.z/pts[2][3]);
bc_clip = bc_clip/(bc_clip.x+bc_clip.y+bc_clip.z); bc_clip = bc_clip/(bc_clip.x+bc_clip.y+bc_clip.z);
float frag_depth = clipc[2]*bc_clip; float frag_depth = -1*(clipc[2]*bc_clip);
if (bc_screen.x<0 || bc_screen.y<0 || bc_screen.z<0 || zbuffer[P.x+P.y*image.get_width()]>frag_depth) continue; if (bc_screen.x<0 || bc_screen.y<0 || bc_screen.z<0 ||
zbuffer[P.x+P.y*image.get_width()]>frag_depth)
continue;
bool discard = shader.fragment(bc_clip, color); bool discard = shader.fragment(bc_clip, color);
if (!discard) { if (!discard) {
zbuffer[P.x+P.y*image.get_width()] = frag_depth; zbuffer[P.x+P.y*image.get_width()] = frag_depth;