From 606f78da43e829feb48fd01a171d18681fcf97ab Mon Sep 17 00:00:00 2001 From: erwin coumans Date: Tue, 17 May 2016 23:57:19 -0700 Subject: [PATCH] work-in-progress tinyrenderer -> shared memory API synthetic camera image --- data/cube.mtl | 4 +- .../CommonGraphicsAppInterface.h | 5 +- .../ImportMeshUtility/b3ImportMeshUtility.cpp | 84 ++++---- .../ImportMeshUtility/b3ImportMeshUtility.h | 11 ++ examples/OpenGLWindow/SimpleOpenGL2App.h | 1 + examples/OpenGLWindow/SimpleOpenGL3App.cpp | 12 ++ examples/OpenGLWindow/SimpleOpenGL3App.h | 1 + .../RenderingExamples/TinyRendererSetup.cpp | 185 +++++++++++------- examples/SharedMemory/PhysicsClientC_API.cpp | 31 +++ examples/SharedMemory/PhysicsClientC_API.h | 8 +- .../PhysicsServerCommandProcessor.cpp | 7 + examples/SharedMemory/SharedMemoryPublic.h | 10 + examples/TinyRenderer/TinyRenderer.cpp | 43 +++- examples/TinyRenderer/TinyRenderer.h | 6 +- examples/TinyRenderer/model.cpp | 22 +++ examples/TinyRenderer/model.h | 1 + examples/TinyRenderer/our_gl.cpp | 10 +- 17 files changed, 319 insertions(+), 122 deletions(-) diff --git a/data/cube.mtl b/data/cube.mtl index 935e48aa1..aba4ddc5b 100644 --- a/data/cube.mtl +++ b/data/cube.mtl @@ -9,6 +9,6 @@ newmtl cube Kd 0.5880 0.5880 0.5880 Ks 0.0000 0.0000 0.0000 Ke 0.0000 0.0000 0.0000 - map_Ka cube.png - map_Kd cube.png + map_Ka cube.tga + map_Kd cube_diffuse.tga diff --git a/examples/CommonInterfaces/CommonGraphicsAppInterface.h b/examples/CommonInterfaces/CommonGraphicsAppInterface.h index 999a1762c..e16225073 100644 --- a/examples/CommonInterfaces/CommonGraphicsAppInterface.h +++ b/examples/CommonInterfaces/CommonGraphicsAppInterface.h @@ -15,10 +15,10 @@ struct DrawGridData int upAxis; float gridColor[4]; - DrawGridData() + DrawGridData(int upAxis=1) :gridSize(10), upOffset(0.001f), - upAxis(1) + upAxis(upAxis) { gridColor[0] = 0.6f; gridColor[1] = 0.6f; @@ -119,6 +119,7 @@ struct CommonGraphicsApp virtual void swapBuffer() = 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 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 registerGraphicsUnitSphereShape(EnumSphereLevelOfDetail lod, int textureId=-1) = 0; diff --git a/examples/Importers/ImportMeshUtility/b3ImportMeshUtility.cpp b/examples/Importers/ImportMeshUtility/b3ImportMeshUtility.cpp index e97f81018..810099bae 100644 --- a/examples/Importers/ImportMeshUtility/b3ImportMeshUtility.cpp +++ b/examples/Importers/ImportMeshUtility/b3ImportMeshUtility.cpp @@ -12,32 +12,28 @@ #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; - char relativeFileName[1024]; + meshData.m_gfxShape = 0; + meshData.m_textureImage = 0; + meshData.m_textureHeight = 0; + meshData.m_textureWidth = 0; + + + char relativeFileName[1024]; if (b3ResourcePath::findResourcePath(fileName.c_str(), relativeFileName, 1024)) { char pathPrefix[1024]; b3FileUtils::extractPath(relativeFileName, pathPrefix, 1024); - - - - btVector3 shift(0,0,0); - - // int index=10; - - { - + btVector3 shift(0,0,0); + std::vector shapes; std::string err = tinyobj::LoadObj(shapes, relativeFileName, pathPrefix); GLInstanceGraphicsShape* gfxShape = btgCreateGraphicsShapeFromWavefrontObj(shapes); - - int textureIndex = -1; //try to load some texture for (int i=0;iregisterTexture(image,width,height); - if (textureIndex>=0) - { - break; - } - } - } } - - 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); - + meshData.m_gfxShape = gfxShape; + return true; - - - - } } else { 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; +} diff --git a/examples/Importers/ImportMeshUtility/b3ImportMeshUtility.h b/examples/Importers/ImportMeshUtility/b3ImportMeshUtility.h index a2de5e1b6..93b445cde 100644 --- a/examples/Importers/ImportMeshUtility/b3ImportMeshUtility.h +++ b/examples/Importers/ImportMeshUtility/b3ImportMeshUtility.h @@ -3,11 +3,22 @@ #include +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 { public: static int loadAndRegisterMeshFromFile(const std::string& fileName, class CommonRenderInterface* renderer); +static bool loadAndRegisterMeshFromFileInternal(const std::string& fileName, b3ImportMeshData& meshData); + }; diff --git a/examples/OpenGLWindow/SimpleOpenGL2App.h b/examples/OpenGLWindow/SimpleOpenGL2App.h index 9cc3c9a05..960dc1d07 100644 --- a/examples/OpenGLWindow/SimpleOpenGL2App.h +++ b/examples/OpenGLWindow/SimpleOpenGL2App.h @@ -18,6 +18,7 @@ public: virtual void swapBuffer(); 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 int registerCubeShape(float halfExtentsX,float halfExtentsY, float halfExtentsZ, int textureIndex = -1, float textureScaling = 1) { diff --git a/examples/OpenGLWindow/SimpleOpenGL3App.cpp b/examples/OpenGLWindow/SimpleOpenGL3App.cpp index d77c12bef..e951855a0 100644 --- a/examples/OpenGLWindow/SimpleOpenGL3App.cpp +++ b/examples/OpenGLWindow/SimpleOpenGL3App.cpp @@ -425,6 +425,18 @@ void SimpleOpenGL3App::drawText( const char* txt, int posXi, int posYi) 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 { float x,y,z,w; diff --git a/examples/OpenGLWindow/SimpleOpenGL3App.h b/examples/OpenGLWindow/SimpleOpenGL3App.h index 57d5225d1..9295e32c4 100644 --- a/examples/OpenGLWindow/SimpleOpenGL3App.h +++ b/examples/OpenGLWindow/SimpleOpenGL3App.h @@ -32,6 +32,7 @@ struct SimpleOpenGL3App : public CommonGraphicsApp virtual void swapBuffer(); 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 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(); diff --git a/examples/RenderingExamples/TinyRendererSetup.cpp b/examples/RenderingExamples/TinyRendererSetup.cpp index ff7d00358..6495b99a4 100644 --- a/examples/RenderingExamples/TinyRendererSetup.cpp +++ b/examples/RenderingExamples/TinyRendererSetup.cpp @@ -6,25 +6,24 @@ #include "Bullet3Common/b3AlignedObjectArray.h" #include "../CommonInterfaces/CommonRenderInterface.h" #include "../TinyRenderer/TinyRenderer.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" #include "../ExampleBrowser/CollisionShape2TriangleMesh.h" +#include "../Importers/ImportMeshUtility/b3ImportMeshUtility.h" +#include "../../OpenGLWindow/GLInstanceGraphicsShape.h" struct TinyRendererSetup : public CommonExampleInterface { + struct GUIHelperInterface* m_guiHelper; struct CommonGraphicsApp* m_app; struct TinyRendererSetupInternalData* m_internalData; - TinyRendererSetup(struct CommonGraphicsApp* app); + TinyRendererSetup(struct GUIHelperInterface* guiHelper); virtual ~TinyRendererSetup(); @@ -52,8 +51,7 @@ struct TinyRendererSetup : public CommonExampleInterface struct TinyRendererSetupInternalData { - int m_canvasIndex; - struct Common2dCanvasInterface* m_canvas; + TGAImage m_rgbColorBuffer; b3AlignedObjectArray m_depthBuffer; @@ -70,24 +68,30 @@ struct TinyRendererSetupInternalData btScalar m_roll; btScalar m_yaw; + int m_textureHandle; + TinyRendererSetupInternalData(int width, int height) - :m_canvasIndex(-1), - m_canvas(0), - m_roll(0), + :m_roll(0), m_pitch(0), m_yaw(0), m_width(width), 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); btSphereShape* sphere = new btSphereShape(1); - btBoxShape* box = new btBoxShape (btVector3(1,1,1)); - m_shapePtr.push_back(cone); - m_shapePtr.push_back(sphere); + btBoxShape* box = new btBoxShape (btVector3(0.5,0.5,0.5)); 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;icreateCube(0.5,0.5,0.5);//createCube + ob->loadModel("cube.obj"); m_renderObjects.push_back(ob); - ob->registerMesh2(vertexPositions,vertexNormals,indicesOut); + //ob->registerMesh2(vertexPositions,vertexNormals,indicesOut); } //ob->registerMeshShape( updateTransforms(); + #endif } void updateTransforms() { @@ -114,7 +122,8 @@ struct TinyRendererSetupInternalData for (int i=0;igetAppInterface(); + 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() { + m_app->m_renderer->enableBlend(false); delete m_internalData; } @@ -145,53 +213,26 @@ void TinyRendererSetup::initPhysics() { //request a visual bitma/texture we can render to - 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;im_width;i++) - { - for (int j=0;jm_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() { - - if (m_internalData->m_canvas && m_internalData->m_canvasIndex>=0) - { - m_internalData->m_canvas->destroyCanvas(m_internalData->m_canvasIndex); - } + } void TinyRendererSetup::stepSimulation(float deltaTime) { - + m_guiHelper->getRenderInterface()->renderScene(); + m_internalData->updateTransforms(); - + TGAColor clearColor; clearColor.bgra[0] = 255; clearColor.bgra[1] = 255; @@ -209,8 +250,10 @@ void TinyRendererSetup::stepSimulation(float deltaTime) 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); @@ -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_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]); } - - for(int y=0;ym_height;++y) - { - for(int x=0;xm_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); + //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); + } @@ -275,5 +318,5 @@ void TinyRendererSetup::syncPhysicsToGraphics(GraphicsPhysicsBridge& gfxBridge) CommonExampleInterface* TinyRendererCreateFunc(struct CommonExampleOptions& options) { - return new TinyRendererSetup(options.m_guiHelper->getAppInterface()); + return new TinyRendererSetup(options.m_guiHelper); } diff --git a/examples/SharedMemory/PhysicsClientC_API.cpp b/examples/SharedMemory/PhysicsClientC_API.cpp index 33b8593be..52d5c0a12 100644 --- a/examples/SharedMemory/PhysicsClientC_API.cpp +++ b/examples/SharedMemory/PhysicsClientC_API.cpp @@ -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) + { + } +} + diff --git a/examples/SharedMemory/PhysicsClientC_API.h b/examples/SharedMemory/PhysicsClientC_API.h index c6d465348..09f848c56 100644 --- a/examples/SharedMemory/PhysicsClientC_API.h +++ b/examples/SharedMemory/PhysicsClientC_API.h @@ -64,7 +64,13 @@ b3SharedMemoryCommandHandle b3InitRequestDebugLinesCommand(b3PhysicsClientHandle ///Get the pointers to the debug line information, after b3InitRequestDebugLinesCommand returns ///status CMD_DEBUG_LINES_COMPLETED 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); int b3PhysicsParamSetGravity(b3SharedMemoryCommandHandle commandHandle, double gravx,double gravy, double gravz); diff --git a/examples/SharedMemory/PhysicsServerCommandProcessor.cpp b/examples/SharedMemory/PhysicsServerCommandProcessor.cpp index 82040713d..2570a6334 100644 --- a/examples/SharedMemory/PhysicsServerCommandProcessor.cpp +++ b/examples/SharedMemory/PhysicsServerCommandProcessor.cpp @@ -962,6 +962,13 @@ bool PhysicsServerCommandProcessor::processCommand(const struct SharedMemoryComm break; } + case CMD_REQUEST_CAMERA_IMAGE_DATA: + { + serverStatusOut.m_type = CMD_CLIENT_COMMAND_COMPLETED; + hasStatus = true; + break; + } + case CMD_LOAD_URDF: { diff --git a/examples/SharedMemory/SharedMemoryPublic.h b/examples/SharedMemory/SharedMemoryPublic.h index 9f3da3a77..10fc7ebe4 100644 --- a/examples/SharedMemory/SharedMemoryPublic.h +++ b/examples/SharedMemory/SharedMemoryPublic.h @@ -23,6 +23,7 @@ enum EnumSharedMemoryClientCommand CMD_PICK_BODY, CMD_MOVE_PICKED_BODY, CMD_REMOVE_PICKING_CONSTRAINT_BODY, + CMD_REQUEST_CAMERA_IMAGE_DATA, CMD_MAX_CLIENT_COMMANDS }; @@ -105,6 +106,14 @@ struct b3DebugLines 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 ///center of mass (COM) of the link, relative to the world reference frame. ///Orientation is a quaternion x,y,z,w @@ -127,4 +136,5 @@ enum { CONTROL_MODE_POSITION_VELOCITY_PD, }; + #endif//SHARED_MEMORY_PUBLIC_H diff --git a/examples/TinyRenderer/TinyRenderer.cpp b/examples/TinyRenderer/TinyRenderer.cpp index 344316996..251f91ced 100644 --- a/examples/TinyRenderer/TinyRenderer.cpp +++ b/examples/TinyRenderer/TinyRenderer.cpp @@ -70,7 +70,8 @@ struct Shader : public IShader { 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); color = m_model->diffuse(uv)*diff; @@ -90,11 +91,13 @@ m_userIndex(-1) { Vec3f eye(1,1,3); Vec3f center(0,0,0); - Vec3f up(0,1,0); + Vec3f up(0,0,1); m_modelMatrix = Matrix::identity(); 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()); } @@ -113,15 +116,22 @@ 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) { m_model = new Model(); - char relativeFileName[1024]; - if (b3ResourcePath::findResourcePath("floor_diffuse.tga", relativeFileName, 1024)) + if (textureImage) { - m_model->loadDiffuseTexture(relativeFileName); + m_model->setDiffuseTextureFromData(textureImage,textureWidth,textureHeight); + } else + { + char relativeFileName[1024]; + if (b3ResourcePath::findResourcePath("floor_diffuse.tga", relativeFileName, 1024)) + { + m_model->loadDiffuseTexture(relativeFileName); + } } for (int i=0;i& zbuffer = renderData.m_depthBuffer; TGAImage& frame = renderData.m_rgbColorBuffer; @@ -233,7 +257,10 @@ void TinyRenderer::renderObject(TinyRenderObjectData& renderData) Matrix modelViewMatrix = renderData.m_viewMatrix*renderData.m_modelMatrix; Shader shader(model, light_dir_local, modelViewMatrix, renderData.m_projectionMatrix); - for (int i=0; infaces(); i++) { + + + for (int i=0; infaces(); i++) { + for (int j=0; j<3; j++) { shader.vertex(i, j); } diff --git a/examples/TinyRenderer/TinyRenderer.h b/examples/TinyRenderer/TinyRenderer.h index b076119fc..2ae36828c 100644 --- a/examples/TinyRenderer/TinyRenderer.h +++ b/examples/TinyRenderer/TinyRenderer.h @@ -17,6 +17,9 @@ struct TinyRenderObjectData Matrix m_projectionMatrix; Matrix m_viewportMatrix; + btVector3 m_eye; + btVector3 m_center; + //Model (vertices, indices, textures, shader) Matrix m_modelMatrix; class Model* m_model; @@ -33,7 +36,8 @@ struct TinyRenderObjectData void loadModel(const char* fileName); 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& vertices, btAlignedObjectArray& normals,btAlignedObjectArray& indices); diff --git a/examples/TinyRenderer/model.cpp b/examples/TinyRenderer/model.cpp index 36a955acc..a2e19b20a 100644 --- a/examples/TinyRenderer/model.cpp +++ b/examples/TinyRenderer/model.cpp @@ -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 &clipc, IShader &shader, TGAImage &image, float *zbuffer, const Matrix& viewPortMatrix) { - mat<3,4,float> pts = (viewPortMatrix*clipc).transpose(); // transposed to ease access to each of the points - + mat<3,4,float> pts = (viewPortMatrix*clipc).transpose(); // transposed to ease access to each of the points + //we don't clip triangles that cross the near plane, just discard them instead of showing artifacts if (pts[0][3]<0 || pts[1][3] <0 || pts[2][3] <0) @@ -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]); bc_clip = bc_clip/(bc_clip.x+bc_clip.y+bc_clip.z); - float frag_depth = 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; + 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; bool discard = shader.fragment(bc_clip, color); if (!discard) { zbuffer[P.x+P.y*image.get_width()] = frag_depth;