From 08548e1ef0b0a66866183c5e49575bb0f6263113 Mon Sep 17 00:00:00 2001 From: yunfeibai Date: Sun, 11 Feb 2018 13:50:45 -0800 Subject: [PATCH 1/7] Add example for projective texture. --- data/cube.mtl | 2 +- .../CommonInterfaces/CommonRenderInterface.h | 1 + .../OpenGLWindow/GLInstancingRenderer.cpp | 107 +++++++++++++++++- .../Shaders/useShadowMapInstancingPS.glsl | 8 +- .../Shaders/useShadowMapInstancingPS.h | 8 +- .../Shaders/useShadowMapInstancingVS.glsl | 4 +- .../Shaders/useShadowMapInstancingVS.h | 4 +- .../pybullet/examples/projective_texture.py | 49 ++++++++ 8 files changed, 163 insertions(+), 20 deletions(-) create mode 100644 examples/pybullet/examples/projective_texture.py diff --git a/data/cube.mtl b/data/cube.mtl index ffce2975d..d9c0d88cd 100644 --- a/data/cube.mtl +++ b/data/cube.mtl @@ -10,7 +10,7 @@ newmtl cube Ks 0.0000 0.0000 0.0000 Ke 0.0000 0.0000 0.0000 map_Ka cube.tga - map_Kd cube.png + map_Kd checker_blue.png diff --git a/examples/CommonInterfaces/CommonRenderInterface.h b/examples/CommonInterfaces/CommonRenderInterface.h index d8c9e37ce..bc828b1aa 100644 --- a/examples/CommonInterfaces/CommonRenderInterface.h +++ b/examples/CommonInterfaces/CommonRenderInterface.h @@ -16,6 +16,7 @@ enum B3_CREATE_SHADOWMAP_RENDERMODE, B3_USE_SHADOWMAP_RENDERMODE, B3_USE_SHADOWMAP_RENDERMODE_REFLECTION, + B3_USE_PROJECTIVE_TEXTURE_RENDERMODE, }; diff --git a/examples/OpenGLWindow/GLInstancingRenderer.cpp b/examples/OpenGLWindow/GLInstancingRenderer.cpp index c7cac6709..24a10f4b7 100644 --- a/examples/OpenGLWindow/GLInstancingRenderer.cpp +++ b/examples/OpenGLWindow/GLInstancingRenderer.cpp @@ -16,7 +16,9 @@ subject to the following restrictions: ///todo: make this configurable in the gui -bool useShadowMap = true;// true;//false;//true; +bool useShadowMap = false;// true;//false;//true; +float projectiveTextureViewSize = 10; +bool useProjectiveTexture = true; int shadowMapWidth= 4096; int shadowMapHeight= 4096; float shadowMapWorldSize=10; @@ -224,6 +226,9 @@ struct InternalDataRenderer : public GLInstanceRendererInternalData b3Vector3 m_lightPos; b3Vector3 m_lightSpecularIntensity; + + b3Vector3 m_projectorPos; + b3Vector3 m_projectorDir; GLuint m_defaultTexturehandle; b3AlignedObjectArray m_textureHandles; @@ -1530,7 +1535,13 @@ void GLInstancingRenderer::renderScene() //renderSceneInternal(B3_USE_SHADOWMAP_RENDERMODE_REFLECTION); renderSceneInternal(B3_USE_SHADOWMAP_RENDERMODE); - } else + } + else if (useProjectiveTexture) + { + renderSceneInternal(B3_CREATE_SHADOWMAP_RENDERMODE); + renderSceneInternal(B3_USE_PROJECTIVE_TEXTURE_RENDERMODE); + } + else { renderSceneInternal(); } @@ -1970,11 +1981,16 @@ void GLInstancingRenderer::renderSceneInternal(int orgRenderMode) reflectionPass = true; renderMode = B3_USE_SHADOWMAP_RENDERMODE; } - + if (!useShadowMap) { renderMode = B3_DEFAULT_RENDERMODE; } + + if (orgRenderMode==B3_USE_PROJECTIVE_TEXTURE_RENDERMODE) + { + renderMode = B3_USE_PROJECTIVE_TEXTURE_RENDERMODE; + } // glEnable(GL_DEPTH_TEST); @@ -2003,6 +2019,10 @@ void GLInstancingRenderer::renderSceneInternal(int orgRenderMode) float depthProjectionMatrix[4][4]; GLfloat depthModelViewMatrix[4][4]; //GLfloat depthModelViewMatrix2[4][4]; + + // For projective texture mapping + float textureProjectionMatrix[4][4]; + GLfloat textureModelViewMatrix[4][4]; // Compute the MVP matrix from the light's point of view if (renderMode==B3_CREATE_SHADOWMAP_RENDERMODE) @@ -2113,6 +2133,18 @@ void GLInstancingRenderer::renderSceneInternal(int orgRenderMode) GLfloat depthBiasMVP[4][4]; b3Matrix4x4Mul(biasMatrix,depthMVP,depthBiasMVP); + // Compute projection matrix for texture projector + float textureViewMatrix[4][4]; + b3Vector3 projectorDir = m_data->m_projectorDir.normalize(); + float projectorDist = 3.0; + b3Vector3 projectorUp = b3MakeVector3(0,0,1.0); + b3CreateLookAt(m_data->m_projectorPos, m_data->m_projectorDir*projectorDist, projectorUp, &textureViewMatrix[0][0]); + GLfloat textureModelMatrix[4][4]; + b3CreateDiagonalMatrix(1.f, textureModelMatrix); + b3Matrix4x4Mul(textureViewMatrix, textureModelMatrix, textureModelViewMatrix); + b3CreateOrtho(-projectiveTextureViewSize,projectiveTextureViewSize,-projectiveTextureViewSize,projectiveTextureViewSize,1,300,textureProjectionMatrix); + GLfloat textureMVP[4][4]; + b3Matrix4x4Mul(textureProjectionMatrix, textureModelViewMatrix, textureMVP); //float m_frustumZNear=0.1; //float m_frustumZFar=100.f; @@ -2459,6 +2491,75 @@ b3Assert(glGetError() ==GL_NO_ERROR); glActiveTexture(GL_TEXTURE1); glBindTexture(GL_TEXTURE_2D,0); + glActiveTexture(GL_TEXTURE0); + glBindTexture(GL_TEXTURE_2D,0); + break; + } + case B3_USE_PROJECTIVE_TEXTURE_RENDERMODE: + { + if ( gfxObj->m_flags&eGfxTransparency) + { + glDepthMask(false); + glEnable (GL_BLEND); + glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); + } + + glUseProgram(useShadowMapInstancingShader); + glUniformMatrix4fv(useShadow_ProjectionMatrix, 1, false, &m_data->m_projectionMatrix[0]); + glUniform3f(useShadow_lightSpecularIntensity, m_data->m_lightSpecularIntensity[0],m_data->m_lightSpecularIntensity[1],m_data->m_lightSpecularIntensity[2]); + glUniform3f(useShadow_materialSpecularColor, gfxObj->m_materialSpecularColor[0],gfxObj->m_materialSpecularColor[1],gfxObj->m_materialSpecularColor[2]); + + float MVP[16]; + if (reflectionPass) + { + float tmp[16]; + float reflectionMatrix[16] = {1,0,0,0, + 0,1,0,0, + 0,0,-1,0, + 0,0,0,1}; + glCullFace(GL_FRONT); + b3Matrix4x4Mul16(m_data->m_viewMatrix,reflectionMatrix,tmp); + b3Matrix4x4Mul16(m_data->m_projectionMatrix,tmp,MVP); + } else + { + b3Matrix4x4Mul16(m_data->m_projectionMatrix,m_data->m_viewMatrix,MVP); + glCullFace(GL_BACK); + } + + glUniformMatrix4fv(useShadow_MVP, 1, false, &MVP[0]); + glUniform3f(useShadow_lightPosIn,m_data->m_lightPos[0],m_data->m_lightPos[1],m_data->m_lightPos[2]); + float camPos[3]; + m_data->m_activeCamera->getCameraPosition(camPos); + glUniform3f(useShadow_cameraPositionIn,camPos[0],camPos[1],camPos[2]); + glUniform1f(useShadow_materialShininessIn,gfxObj->m_materialShinyNess); + + glUniformMatrix4fv(useShadow_DepthBiasModelViewMatrix, 1, false, &textureMVP[0][0]); + glActiveTexture(GL_TEXTURE1); + glBindTexture(GL_TEXTURE_2D, m_data->m_shadowTexture); + glUniform1i(useShadow_shadowMap,1); + + //sort transparent objects + if ( gfxObj->m_flags&eGfxTransparency) + { + int instanceId = transparentInstances[i].m_instanceId; + glVertexAttribPointer(1, 4, GL_FLOAT, GL_FALSE, 0, (GLvoid *)((instanceId)*4*sizeof(float)+m_data->m_maxShapeCapacityInBytes)); + glVertexAttribPointer(2, 4, GL_FLOAT, GL_FALSE, 0, (GLvoid *)((instanceId)*4*sizeof(float)+m_data->m_maxShapeCapacityInBytes+POSITION_BUFFER_SIZE)); + glVertexAttribPointer(5, 4, GL_FLOAT, GL_FALSE, 0, (GLvoid *)((instanceId)*4*sizeof(float)+m_data->m_maxShapeCapacityInBytes+POSITION_BUFFER_SIZE+ORIENTATION_BUFFER_SIZE)); + glVertexAttribPointer(6, 3, GL_FLOAT, GL_FALSE, 0, (GLvoid *)((instanceId)*3*sizeof(float)+m_data->m_maxShapeCapacityInBytes+POSITION_BUFFER_SIZE+ORIENTATION_BUFFER_SIZE+COLOR_BUFFER_SIZE)); + glDrawElements(GL_TRIANGLES, indexCount, GL_UNSIGNED_INT, 0); + } else + { + glDrawElementsInstanced(GL_TRIANGLES, indexCount, GL_UNSIGNED_INT, indexOffset, gfxObj->m_numGraphicsInstances); + } + + if ( gfxObj->m_flags&eGfxTransparency) + { + glDisable (GL_BLEND); + glDepthMask(true); + } + glActiveTexture(GL_TEXTURE1); + glBindTexture(GL_TEXTURE_2D,0); + glActiveTexture(GL_TEXTURE0); glBindTexture(GL_TEXTURE_2D,0); break; diff --git a/examples/OpenGLWindow/Shaders/useShadowMapInstancingPS.glsl b/examples/OpenGLWindow/Shaders/useShadowMapInstancingPS.glsl index 809494902..4e11e3762 100644 --- a/examples/OpenGLWindow/Shaders/useShadowMapInstancingPS.glsl +++ b/examples/OpenGLWindow/Shaders/useShadowMapInstancingPS.glsl @@ -12,11 +12,9 @@ in Vert } vert; uniform sampler2D Diffuse; -uniform sampler2DShadow shadowMap; uniform mat4 ViewMatrixInverse; in vec3 lightPos,cameraPosition, normal,ambient; -in vec4 ShadowCoord; in vec4 vertexPos; in float materialShininess; in vec3 lightSpecularIntensity; @@ -28,7 +26,7 @@ out vec4 color; void main(void) { - vec4 texel = fragment.color*texture(Diffuse,vert.texcoord); + vec4 texel = fragment.color*texture(Diffuse,vert.texcoord.xy); vec3 ct,cf; float intensity,at,af; if (fragment.color.w==0) @@ -65,9 +63,7 @@ void main(void) } - float visibility = texture(shadowMap, vec3(ShadowCoord.xy,(ShadowCoord.z)/ShadowCoord.w)); - if (intensity<0.5) - visibility = 0; + float visibility = 1.0; intensity = 0.7*intensity + 0.3*intensity*visibility; diff --git a/examples/OpenGLWindow/Shaders/useShadowMapInstancingPS.h b/examples/OpenGLWindow/Shaders/useShadowMapInstancingPS.h index e34883d85..43407e156 100644 --- a/examples/OpenGLWindow/Shaders/useShadowMapInstancingPS.h +++ b/examples/OpenGLWindow/Shaders/useShadowMapInstancingPS.h @@ -11,10 +11,8 @@ static const char* useShadowMapInstancingFragmentShader= \ " vec2 texcoord;\n" "} vert;\n" "uniform sampler2D Diffuse;\n" -"uniform sampler2DShadow shadowMap;\n" "uniform mat4 ViewMatrixInverse;\n" "in vec3 lightPos,cameraPosition, normal,ambient;\n" -"in vec4 ShadowCoord;\n" "in vec4 vertexPos;\n" "in float materialShininess;\n" "in vec3 lightSpecularIntensity;\n" @@ -22,7 +20,7 @@ static const char* useShadowMapInstancingFragmentShader= \ "out vec4 color;\n" "void main(void)\n" "{\n" -" vec4 texel = fragment.color*texture(Diffuse,vert.texcoord);\n" +" vec4 texel = fragment.color*texture(Diffuse,vert.texcoord.xy);\n" " vec3 ct,cf;\n" " float intensity,at,af;\n" " if (fragment.color.w==0)\n" @@ -58,9 +56,7 @@ static const char* useShadowMapInstancingFragmentShader= \ " \n" " }\n" " \n" -" float visibility = texture(shadowMap, vec3(ShadowCoord.xy,(ShadowCoord.z)/ShadowCoord.w));\n" -" if (intensity<0.5)\n" -" visibility = 0;\n" +" float visibility = 1.0;\n" " intensity = 0.7*intensity + 0.3*intensity*visibility;\n" " \n" " cf = intensity*(vec3(1.0,1.0,1.0)-ambient)+ambient+specularReflection*visibility;\n" diff --git a/examples/OpenGLWindow/Shaders/useShadowMapInstancingVS.glsl b/examples/OpenGLWindow/Shaders/useShadowMapInstancingVS.glsl index 83ed4839f..01d73c2d4 100644 --- a/examples/OpenGLWindow/Shaders/useShadowMapInstancingVS.glsl +++ b/examples/OpenGLWindow/Shaders/useShadowMapInstancingVS.glsl @@ -91,9 +91,9 @@ void main(void) vec4 vertexLoc = MVP* vec4((instance_position+localcoord).xyz,1); gl_Position = vertexLoc; - ShadowCoord = DepthBiasModelViewProjectionMatrix * vec4((instance_position+localcoord).xyz,1); fragment.color = instance_color; - vert.texcoord = uvcoords; + vec4 projcoords = DepthBiasModelViewProjectionMatrix * vec4((instance_position+localcoord).xyz,1); + vert.texcoord = projcoords.xy; } diff --git a/examples/OpenGLWindow/Shaders/useShadowMapInstancingVS.h b/examples/OpenGLWindow/Shaders/useShadowMapInstancingVS.h index 20c188edb..672a83ae5 100644 --- a/examples/OpenGLWindow/Shaders/useShadowMapInstancingVS.h +++ b/examples/OpenGLWindow/Shaders/useShadowMapInstancingVS.h @@ -79,8 +79,8 @@ static const char* useShadowMapInstancingVertexShader= \ " \n" " vec4 vertexLoc = MVP* vec4((instance_position+localcoord).xyz,1);\n" " gl_Position = vertexLoc;\n" -" ShadowCoord = DepthBiasModelViewProjectionMatrix * vec4((instance_position+localcoord).xyz,1);\n" " fragment.color = instance_color;\n" -" vert.texcoord = uvcoords;\n" +" vec4 projcoords = DepthBiasModelViewProjectionMatrix * vec4((instance_position+localcoord).xyz,1);\n" +" vert.texcoord = projcoords.xy;\n" "}\n" ; diff --git a/examples/pybullet/examples/projective_texture.py b/examples/pybullet/examples/projective_texture.py new file mode 100644 index 000000000..b2d11f425 --- /dev/null +++ b/examples/pybullet/examples/projective_texture.py @@ -0,0 +1,49 @@ +import pybullet as p +from time import sleep +from PIL import Image +import matplotlib.pyplot as plt +import numpy as np + +physicsClient = p.connect(p.GUI) + +p.setGravity(0,0,-10) +planeId = p.loadURDF("plane.urdf") +cubeStartPos = [0,0,1] +cubeStartOrientation = p.getQuaternionFromEuler([0,0,0]) +boxId = p.loadURDF("cube.urdf",cubeStartPos, cubeStartOrientation) +cubePos, cubeOrn = p.getBasePositionAndOrientation(boxId) +textureId = p.loadTexture("kcam_vga_scaled_direct.png") +p.changeVisualShape(objectUniqueId=0, linkIndex=-1, textureUniqueId=textureId) +p.changeVisualShape(objectUniqueId=1, linkIndex=-1, textureUniqueId=textureId) + +#p.resetDebugVisualizerCamera(cameraDistance=3, cameraYaw=30, cameraPitch=52, cameraTargetPosition=[10,0,0]) +fov = 70 +pixelWidth = 640 +pixelHeight = 512 +aspect = pixelWidth / pixelHeight; +nearPlane = 0.01 +farPlane = 100 +projectionMatrix = p.computeProjectionMatrixFOV(fov, aspect, nearPlane, farPlane); +viewMatrixLeft = p.computeViewMatrix(cameraEyePosition=[0.045, 3.0, 3.0], cameraTargetPosition=[0.045,0,0], cameraUpVector=[0,0,1.0]) +viewMatrixRight = p.computeViewMatrix(cameraEyePosition=[-0.045, 3.0, 3.0], cameraTargetPosition=[-0.045,0,0], cameraUpVector=[0,0,1.0]) +leftImage = p.getCameraImage(width=640,height=512,viewMatrix=viewMatrixLeft,projectionMatrix=projectionMatrix,renderer=p.ER_BULLET_HARDWARE_OPENGL) +rightImage = p.getCameraImage(width=640,height=512,viewMatrix=viewMatrixRight,projectionMatrix=projectionMatrix,renderer=p.ER_BULLET_HARDWARE_OPENGL) + +left_img = np.reshape(np.asarray(leftImage[2], dtype=np.float32), (512, 640, 4)) +left_img /= 255 +plt.imsave("left_image_new.png", left_img) +right_img = np.reshape(np.asarray(rightImage[2], dtype=np.float32), (512, 640, 4)) +right_img /= 255 +plt.imsave("right_image_new.png", right_img) + +useRealTimeSimulation = 1 + +if (useRealTimeSimulation): + p.setRealTimeSimulation(1) + +while 1: + if (useRealTimeSimulation): + p.setGravity(0,0,-10) + sleep(0.01) # Time in seconds. + else: + p.stepSimulation() From 2947cd54ce225b7c60689ae363f08dc38f8b9d61 Mon Sep 17 00:00:00 2001 From: yunfeibai Date: Sun, 11 Feb 2018 21:29:02 -0800 Subject: [PATCH 2/7] Add shader for projective texture. --- build3/stringify.bat | 3 +- build3/stringify.sh | 2 + build3/stringifyShaders.bat | 2 + data/cube.mtl | 2 +- .../OpenGLWindow/GLInstancingRenderer.cpp | 70 ++++++++++++++----- .../Shaders/useShadowMapInstancingPS.glsl | 8 ++- .../Shaders/useShadowMapInstancingPS.h | 8 ++- .../Shaders/useShadowMapInstancingVS.glsl | 6 +- .../Shaders/useShadowMapInstancingVS.h | 6 +- .../pybullet/examples/projective_texture.py | 11 ++- 10 files changed, 85 insertions(+), 33 deletions(-) mode change 100644 => 100755 examples/OpenGLWindow/Shaders/useShadowMapInstancingPS.glsl mode change 100644 => 100755 examples/OpenGLWindow/Shaders/useShadowMapInstancingPS.h mode change 100644 => 100755 examples/OpenGLWindow/Shaders/useShadowMapInstancingVS.glsl mode change 100644 => 100755 examples/OpenGLWindow/Shaders/useShadowMapInstancingVS.h diff --git a/build3/stringify.bat b/build3/stringify.bat index 8876515c7..bc4f822f3 100644 --- a/build3/stringify.bat +++ b/build3/stringify.bat @@ -56,7 +56,8 @@ premake4 --file=stringifyKernel.lua --kernelfile="../btgui/OpenGLWindow/Shaders/ premake4 --file=stringifyKernel.lua --kernelfile="../btgui/OpenGLWindow/Shaders/useShadowMapInstancingPS.glsl" --headerfile="../btgui/OpenGLWindow/Shaders/useShadowMapInstancingPS.h" --stringname="useShadowMapInstancingFragmentShader" stringify premake4 --file=stringifyKernel.lua --kernelfile="../btgui/OpenGLWindow/Shaders/useShadowMapInstancingVS.glsl" --headerfile="../btgui/OpenGLWindow/Shaders/useShadowMapInstancingVS.h" --stringname="useShadowMapInstancingVertexShader" stringify - +premake4 --file=stringifyKernel.lua --kernelfile="../btgui/OpenGLWindow/Shaders/projectiveTextureInstancingPS.glsl" --headerfile="../btgui/OpenGLWindow/Shaders/projectiveTextureInstancingPS.h" --stringname="projectiveTextureInstancingFragmentShader" stringify +premake4 --file=stringifyKernel.lua --kernelfile="../btgui/OpenGLWindow/Shaders/projectiveTextureInstancingVS.glsl" --headerfile="../btgui/OpenGLWindow/Shaders/projectiveTextureInstancingVS.h" --stringname="projectiveTextureInstancingVertexShader" stringify premake4 --file=stringifyKernel.lua --kernelfile="../Demos3/GpuDemos/broadphase/pairsKernel.cl" --headerfile="../Demos3/GpuDemos/broadphase/pairsKernel.h" --stringname="pairsKernelsCL" stringify diff --git a/build3/stringify.sh b/build3/stringify.sh index b5112fce2..16e01825d 100755 --- a/build3/stringify.sh +++ b/build3/stringify.sh @@ -62,6 +62,8 @@ eval '$mypremake --file=stringifyKernel.lua --kernelfile="../examples/OpenGLWin eval '$mypremake --file=stringifyKernel.lua --kernelfile="../examples/OpenGLWindow/Shaders/useShadowMapInstancingPS.glsl" --headerfile="../examples/OpenGLWindow/Shaders/useShadowMapInstancingPS.h" --stringname="useShadowMapInstancingFragmentShader" stringify' eval '$mypremake --file=stringifyKernel.lua --kernelfile="../examples/OpenGLWindow/Shaders/useShadowMapInstancingVS.glsl" --headerfile="../examples/OpenGLWindow/Shaders/useShadowMapInstancingVS.h" --stringname="useShadowMapInstancingVertexShader" stringify' +eval '$mypremake --file=stringifyKernel.lua --kernelfile="../examples/OpenGLWindow/Shaders/projectiveTextureInstancingPS.glsl" --headerfile="../examples/OpenGLWindow/Shaders/projectiveTextureInstancingPS.h" --stringname="projectiveTextureInstancingFragmentShader" stringify' +eval '$mypremake --file=stringifyKernel.lua --kernelfile="../examples/OpenGLWindow/Shaders/projectiveTextureInstancingVS.glsl" --headerfile="../examples/OpenGLWindow/Shaders/projectiveTextureInstancingVS.h" --stringname="projectiveTextureInstancingVertexShader" stringify' eval '$mypremake --file=stringifyKernel.lua --kernelfile="../examples/OpenCL/broadphase/pairsKernel.cl" --headerfile="../examples/OpenCL/broadphase/pairsKernel.h" --stringname="pairsKernelsCL" stringify' diff --git a/build3/stringifyShaders.bat b/build3/stringifyShaders.bat index 9727bc0ac..1636fbe4e 100644 --- a/build3/stringifyShaders.bat +++ b/build3/stringifyShaders.bat @@ -10,6 +10,8 @@ premake4 --file=stringifyKernel.lua --kernelfile="../examples/OpenGLWindow/Shade premake4 --file=stringifyKernel.lua --kernelfile="../examples/OpenGLWindow/Shaders/createShadowMapInstancingVS.glsl" --headerfile="../examples/OpenGLWindow/Shaders/createShadowMapInstancingVS.h" --stringname="createShadowMapInstancingVertexShader" stringify premake4 --file=stringifyKernel.lua --kernelfile="../examples/OpenGLWindow/Shaders/useShadowMapInstancingPS.glsl" --headerfile="../examples/OpenGLWindow/Shaders/useShadowMapInstancingPS.h" --stringname="useShadowMapInstancingFragmentShader" stringify premake4 --file=stringifyKernel.lua --kernelfile="../examples/OpenGLWindow/Shaders/useShadowMapInstancingVS.glsl" --headerfile="../examples/OpenGLWindow/Shaders/useShadowMapInstancingVS.h" --stringname="useShadowMapInstancingVertexShader" stringify +premake4 --file=stringifyKernel.lua --kernelfile="../examples/OpenGLWindow/Shaders/projectiveTextureInstancingPS.glsl" --headerfile="../examples/OpenGLWindow/Shaders/projectiveTextureInstancingPS.h" --stringname="projectiveTextureInstancingFragmentShader" stringify +premake4 --file=stringifyKernel.lua --kernelfile="../examples/OpenGLWindow/Shaders/projectiveTextureInstancingVS.glsl" --headerfile="../examples/OpenGLWindow/Shaders/projectiveTextureInstancingVS.h" --stringname="projectiveTextureInstancingVertexShader" stringify premake4 --file=stringifyKernel.lua --kernelfile="../examples/OpenGLWindow/Shaders/linesVS.glsl" --headerfile="../examples/OpenGLWindow/Shaders/linesVS.h" --stringname="linesVertexShader" stringify premake4 --file=stringifyKernel.lua --kernelfile="../examples/OpenGLWindow/Shaders/linesPS.glsl" --headerfile="../examples/OpenGLWindow/Shaders/linesPS.h" --stringname="linesFragmentShader" stringify diff --git a/data/cube.mtl b/data/cube.mtl index d9c0d88cd..ffce2975d 100644 --- a/data/cube.mtl +++ b/data/cube.mtl @@ -10,7 +10,7 @@ newmtl cube Ks 0.0000 0.0000 0.0000 Ke 0.0000 0.0000 0.0000 map_Ka cube.tga - map_Kd checker_blue.png + map_Kd cube.png diff --git a/examples/OpenGLWindow/GLInstancingRenderer.cpp b/examples/OpenGLWindow/GLInstancingRenderer.cpp index 24a10f4b7..edbe0ee7d 100644 --- a/examples/OpenGLWindow/GLInstancingRenderer.cpp +++ b/examples/OpenGLWindow/GLInstancingRenderer.cpp @@ -16,9 +16,9 @@ subject to the following restrictions: ///todo: make this configurable in the gui -bool useShadowMap = false;// true;//false;//true; +bool useShadowMap = true;// true;//false;//true; float projectiveTextureViewSize = 10; -bool useProjectiveTexture = true; +bool useProjectiveTexture = false; int shadowMapWidth= 4096; int shadowMapHeight= 4096; float shadowMapWorldSize=10; @@ -74,6 +74,8 @@ float shadowMapWorldSize=10; #include "Shaders/createShadowMapInstancingPS.h" #include "Shaders/useShadowMapInstancingVS.h" #include "Shaders/useShadowMapInstancingPS.h" +#include "Shaders/projectiveTextureInstancingVS.h" +#include "Shaders/projectiveTextureInstancingPS.h" #include "Shaders/linesPS.h" #include "Shaders/linesVS.h" @@ -250,6 +252,8 @@ struct InternalDataRenderer : public GLInstanceRendererInternalData { m_lightPos=b3MakeVector3(-50,30,40); m_lightSpecularIntensity.setValue(1,1,1); + + m_projectorPos=b3MakeVector3(-50,30,40); //clear to zero to make it obvious if the matrix is used uninitialized for (int i=0;i<16;i++) @@ -284,6 +288,7 @@ static GLuint triangleIndexVbo=0; static GLuint linesShader; // The line renderer static GLuint useShadowMapInstancingShader; // The shadow instancing renderer static GLuint createShadowMapInstancingShader; // The shadow instancing renderer +static GLuint projectiveTextureInstancingShader; // The projective texture instancing renderer static GLuint instancingShader; // The instancing renderer static GLuint instancingShaderPointSprite; // The point sprite instancing renderer @@ -321,6 +326,20 @@ static GLint useShadow_shadowMap = 0; static GLint createShadow_depthMVP=0; +static GLint projectiveTexture_ViewMatrixInverse=0; +static GLint projectiveTexture_ModelViewMatrix=0; +static GLint projectiveTexture_lightSpecularIntensity = 0; +static GLint projectiveTexture_materialSpecularColor = 0; +static GLint projectiveTexture_MVP=0; +static GLint projectiveTexture_lightPosIn=0; +static GLint projectiveTexture_cameraPositionIn = 0; +static GLint projectiveTexture_materialShininessIn = 0; + +static GLint projectiveTexture_ProjectionMatrix=0; +static GLint projectiveTexture_DepthBiasModelViewMatrix=0; +static GLint projectiveTexture_uniform_texture_diffuse = 0; +static GLint projectiveTexture_shadowMap = 0; + static GLint ModelViewMatrix=0; static GLint ProjectionMatrix=0; static GLint regularLightDirIn=0; @@ -1212,7 +1231,24 @@ void GLInstancingRenderer::InitShaders() glGetIntegerv(GL_SMOOTH_LINE_WIDTH_RANGE, lineWidthRange); - + projectiveTextureInstancingShader = gltLoadShaderPair(projectiveTextureInstancingVertexShader,projectiveTextureInstancingFragmentShader); + + glLinkProgram(projectiveTextureInstancingShader); + glUseProgram(projectiveTextureInstancingShader); + projectiveTexture_ViewMatrixInverse = glGetUniformLocation(projectiveTextureInstancingShader, "ViewMatrixInverse"); + projectiveTexture_ModelViewMatrix = glGetUniformLocation(projectiveTextureInstancingShader, "ModelViewMatrix"); + projectiveTexture_lightSpecularIntensity = glGetUniformLocation(projectiveTextureInstancingShader, "lightSpecularIntensityIn"); + projectiveTexture_materialSpecularColor = glGetUniformLocation(projectiveTextureInstancingShader, "materialSpecularColorIn"); + projectiveTexture_MVP = glGetUniformLocation(projectiveTextureInstancingShader, "MVP"); + projectiveTexture_ProjectionMatrix = glGetUniformLocation(projectiveTextureInstancingShader, "ProjectionMatrix"); + projectiveTexture_DepthBiasModelViewMatrix = glGetUniformLocation(projectiveTextureInstancingShader, "DepthBiasModelViewProjectionMatrix"); + projectiveTexture_uniform_texture_diffuse = glGetUniformLocation(projectiveTextureInstancingShader, "Diffuse"); + projectiveTexture_shadowMap = glGetUniformLocation(projectiveTextureInstancingShader,"shadowMap"); + projectiveTexture_lightPosIn = glGetUniformLocation(projectiveTextureInstancingShader,"lightPosIn"); + projectiveTexture_cameraPositionIn = glGetUniformLocation(projectiveTextureInstancingShader,"cameraPositionIn"); + projectiveTexture_materialShininessIn = glGetUniformLocation(projectiveTextureInstancingShader,"materialShininessIn"); + + glUseProgram(0); useShadowMapInstancingShader = gltLoadShaderPair(useShadowMapInstancingVertexShader,useShadowMapInstancingFragmentShader); @@ -1538,7 +1574,7 @@ void GLInstancingRenderer::renderScene() } else if (useProjectiveTexture) { - renderSceneInternal(B3_CREATE_SHADOWMAP_RENDERMODE); + //renderSceneInternal(B3_CREATE_SHADOWMAP_RENDERMODE); renderSceneInternal(B3_USE_PROJECTIVE_TEXTURE_RENDERMODE); } else @@ -2136,9 +2172,8 @@ void GLInstancingRenderer::renderSceneInternal(int orgRenderMode) // Compute projection matrix for texture projector float textureViewMatrix[4][4]; b3Vector3 projectorDir = m_data->m_projectorDir.normalize(); - float projectorDist = 3.0; - b3Vector3 projectorUp = b3MakeVector3(0,0,1.0); - b3CreateLookAt(m_data->m_projectorPos, m_data->m_projectorDir*projectorDist, projectorUp, &textureViewMatrix[0][0]); + float projectorDist = 0; + b3CreateLookAt(m_data->m_projectorPos,center,up, &textureViewMatrix[0][0]); GLfloat textureModelMatrix[4][4]; b3CreateDiagonalMatrix(1.f, textureModelMatrix); b3Matrix4x4Mul(textureViewMatrix, textureModelMatrix, textureModelViewMatrix); @@ -2497,6 +2532,7 @@ b3Assert(glGetError() ==GL_NO_ERROR); } case B3_USE_PROJECTIVE_TEXTURE_RENDERMODE: { + printf("PROJECTIVE TEXTURE!!\n"); if ( gfxObj->m_flags&eGfxTransparency) { glDepthMask(false); @@ -2504,10 +2540,10 @@ b3Assert(glGetError() ==GL_NO_ERROR); glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); } - glUseProgram(useShadowMapInstancingShader); - glUniformMatrix4fv(useShadow_ProjectionMatrix, 1, false, &m_data->m_projectionMatrix[0]); - glUniform3f(useShadow_lightSpecularIntensity, m_data->m_lightSpecularIntensity[0],m_data->m_lightSpecularIntensity[1],m_data->m_lightSpecularIntensity[2]); - glUniform3f(useShadow_materialSpecularColor, gfxObj->m_materialSpecularColor[0],gfxObj->m_materialSpecularColor[1],gfxObj->m_materialSpecularColor[2]); + glUseProgram(projectiveTextureInstancingShader); + glUniformMatrix4fv(projectiveTexture_ProjectionMatrix, 1, false, &m_data->m_projectionMatrix[0]); + glUniform3f(projectiveTexture_lightSpecularIntensity, m_data->m_lightSpecularIntensity[0],m_data->m_lightSpecularIntensity[1],m_data->m_lightSpecularIntensity[2]); + glUniform3f(projectiveTexture_materialSpecularColor, gfxObj->m_materialSpecularColor[0],gfxObj->m_materialSpecularColor[1],gfxObj->m_materialSpecularColor[2]); float MVP[16]; if (reflectionPass) @@ -2526,17 +2562,17 @@ b3Assert(glGetError() ==GL_NO_ERROR); glCullFace(GL_BACK); } - glUniformMatrix4fv(useShadow_MVP, 1, false, &MVP[0]); - glUniform3f(useShadow_lightPosIn,m_data->m_lightPos[0],m_data->m_lightPos[1],m_data->m_lightPos[2]); + glUniformMatrix4fv(projectiveTexture_MVP, 1, false, &MVP[0]); + glUniform3f(projectiveTexture_lightPosIn,m_data->m_lightPos[0],m_data->m_lightPos[1],m_data->m_lightPos[2]); float camPos[3]; m_data->m_activeCamera->getCameraPosition(camPos); - glUniform3f(useShadow_cameraPositionIn,camPos[0],camPos[1],camPos[2]); - glUniform1f(useShadow_materialShininessIn,gfxObj->m_materialShinyNess); + glUniform3f(projectiveTexture_cameraPositionIn,camPos[0],camPos[1],camPos[2]); + glUniform1f(projectiveTexture_materialShininessIn,gfxObj->m_materialShinyNess); - glUniformMatrix4fv(useShadow_DepthBiasModelViewMatrix, 1, false, &textureMVP[0][0]); + glUniformMatrix4fv(projectiveTexture_DepthBiasModelViewMatrix, 1, false, &textureMVP[0][0]); glActiveTexture(GL_TEXTURE1); glBindTexture(GL_TEXTURE_2D, m_data->m_shadowTexture); - glUniform1i(useShadow_shadowMap,1); + glUniform1i(projectiveTexture_shadowMap,1); //sort transparent objects if ( gfxObj->m_flags&eGfxTransparency) diff --git a/examples/OpenGLWindow/Shaders/useShadowMapInstancingPS.glsl b/examples/OpenGLWindow/Shaders/useShadowMapInstancingPS.glsl old mode 100644 new mode 100755 index 4e11e3762..809494902 --- a/examples/OpenGLWindow/Shaders/useShadowMapInstancingPS.glsl +++ b/examples/OpenGLWindow/Shaders/useShadowMapInstancingPS.glsl @@ -12,9 +12,11 @@ in Vert } vert; uniform sampler2D Diffuse; +uniform sampler2DShadow shadowMap; uniform mat4 ViewMatrixInverse; in vec3 lightPos,cameraPosition, normal,ambient; +in vec4 ShadowCoord; in vec4 vertexPos; in float materialShininess; in vec3 lightSpecularIntensity; @@ -26,7 +28,7 @@ out vec4 color; void main(void) { - vec4 texel = fragment.color*texture(Diffuse,vert.texcoord.xy); + vec4 texel = fragment.color*texture(Diffuse,vert.texcoord); vec3 ct,cf; float intensity,at,af; if (fragment.color.w==0) @@ -63,7 +65,9 @@ void main(void) } - float visibility = 1.0; + float visibility = texture(shadowMap, vec3(ShadowCoord.xy,(ShadowCoord.z)/ShadowCoord.w)); + if (intensity<0.5) + visibility = 0; intensity = 0.7*intensity + 0.3*intensity*visibility; diff --git a/examples/OpenGLWindow/Shaders/useShadowMapInstancingPS.h b/examples/OpenGLWindow/Shaders/useShadowMapInstancingPS.h old mode 100644 new mode 100755 index 43407e156..e34883d85 --- a/examples/OpenGLWindow/Shaders/useShadowMapInstancingPS.h +++ b/examples/OpenGLWindow/Shaders/useShadowMapInstancingPS.h @@ -11,8 +11,10 @@ static const char* useShadowMapInstancingFragmentShader= \ " vec2 texcoord;\n" "} vert;\n" "uniform sampler2D Diffuse;\n" +"uniform sampler2DShadow shadowMap;\n" "uniform mat4 ViewMatrixInverse;\n" "in vec3 lightPos,cameraPosition, normal,ambient;\n" +"in vec4 ShadowCoord;\n" "in vec4 vertexPos;\n" "in float materialShininess;\n" "in vec3 lightSpecularIntensity;\n" @@ -20,7 +22,7 @@ static const char* useShadowMapInstancingFragmentShader= \ "out vec4 color;\n" "void main(void)\n" "{\n" -" vec4 texel = fragment.color*texture(Diffuse,vert.texcoord.xy);\n" +" vec4 texel = fragment.color*texture(Diffuse,vert.texcoord);\n" " vec3 ct,cf;\n" " float intensity,at,af;\n" " if (fragment.color.w==0)\n" @@ -56,7 +58,9 @@ static const char* useShadowMapInstancingFragmentShader= \ " \n" " }\n" " \n" -" float visibility = 1.0;\n" +" float visibility = texture(shadowMap, vec3(ShadowCoord.xy,(ShadowCoord.z)/ShadowCoord.w));\n" +" if (intensity<0.5)\n" +" visibility = 0;\n" " intensity = 0.7*intensity + 0.3*intensity*visibility;\n" " \n" " cf = intensity*(vec3(1.0,1.0,1.0)-ambient)+ambient+specularReflection*visibility;\n" diff --git a/examples/OpenGLWindow/Shaders/useShadowMapInstancingVS.glsl b/examples/OpenGLWindow/Shaders/useShadowMapInstancingVS.glsl old mode 100644 new mode 100755 index 01d73c2d4..3f9601f44 --- a/examples/OpenGLWindow/Shaders/useShadowMapInstancingVS.glsl +++ b/examples/OpenGLWindow/Shaders/useShadowMapInstancingVS.glsl @@ -11,6 +11,8 @@ layout (location = 5) in vec4 instance_color; layout (location = 6) in vec3 instance_scale; +uniform mat4 ModelViewMatrix; +uniform mat4 ProjectionMatrix; uniform mat4 DepthBiasModelViewProjectionMatrix; uniform mat4 MVP; uniform vec3 lightPosIn; @@ -91,9 +93,9 @@ void main(void) vec4 vertexLoc = MVP* vec4((instance_position+localcoord).xyz,1); gl_Position = vertexLoc; + ShadowCoord = DepthBiasModelViewProjectionMatrix * vec4((instance_position+localcoord).xyz,1); fragment.color = instance_color; - vec4 projcoords = DepthBiasModelViewProjectionMatrix * vec4((instance_position+localcoord).xyz,1); - vert.texcoord = projcoords.xy; + vert.texcoord = uvcoords; } diff --git a/examples/OpenGLWindow/Shaders/useShadowMapInstancingVS.h b/examples/OpenGLWindow/Shaders/useShadowMapInstancingVS.h old mode 100644 new mode 100755 index 672a83ae5..0800c2cd0 --- a/examples/OpenGLWindow/Shaders/useShadowMapInstancingVS.h +++ b/examples/OpenGLWindow/Shaders/useShadowMapInstancingVS.h @@ -9,6 +9,8 @@ static const char* useShadowMapInstancingVertexShader= \ "layout (location = 4) in vec3 vertexnormal;\n" "layout (location = 5) in vec4 instance_color;\n" "layout (location = 6) in vec3 instance_scale;\n" +"uniform mat4 ModelViewMatrix;\n" +"uniform mat4 ProjectionMatrix;\n" "uniform mat4 DepthBiasModelViewProjectionMatrix;\n" "uniform mat4 MVP;\n" "uniform vec3 lightPosIn;\n" @@ -79,8 +81,8 @@ static const char* useShadowMapInstancingVertexShader= \ " \n" " vec4 vertexLoc = MVP* vec4((instance_position+localcoord).xyz,1);\n" " gl_Position = vertexLoc;\n" +" ShadowCoord = DepthBiasModelViewProjectionMatrix * vec4((instance_position+localcoord).xyz,1);\n" " fragment.color = instance_color;\n" -" vec4 projcoords = DepthBiasModelViewProjectionMatrix * vec4((instance_position+localcoord).xyz,1);\n" -" vert.texcoord = projcoords.xy;\n" +" vert.texcoord = uvcoords;\n" "}\n" ; diff --git a/examples/pybullet/examples/projective_texture.py b/examples/pybullet/examples/projective_texture.py index b2d11f425..e9574e4f7 100644 --- a/examples/pybullet/examples/projective_texture.py +++ b/examples/pybullet/examples/projective_texture.py @@ -12,11 +12,10 @@ cubeStartPos = [0,0,1] cubeStartOrientation = p.getQuaternionFromEuler([0,0,0]) boxId = p.loadURDF("cube.urdf",cubeStartPos, cubeStartOrientation) cubePos, cubeOrn = p.getBasePositionAndOrientation(boxId) -textureId = p.loadTexture("kcam_vga_scaled_direct.png") +textureId = p.loadTexture("checker_blue_test.png") p.changeVisualShape(objectUniqueId=0, linkIndex=-1, textureUniqueId=textureId) p.changeVisualShape(objectUniqueId=1, linkIndex=-1, textureUniqueId=textureId) -#p.resetDebugVisualizerCamera(cameraDistance=3, cameraYaw=30, cameraPitch=52, cameraTargetPosition=[10,0,0]) fov = 70 pixelWidth = 640 pixelHeight = 512 @@ -24,17 +23,17 @@ aspect = pixelWidth / pixelHeight; nearPlane = 0.01 farPlane = 100 projectionMatrix = p.computeProjectionMatrixFOV(fov, aspect, nearPlane, farPlane); -viewMatrixLeft = p.computeViewMatrix(cameraEyePosition=[0.045, 3.0, 3.0], cameraTargetPosition=[0.045,0,0], cameraUpVector=[0,0,1.0]) -viewMatrixRight = p.computeViewMatrix(cameraEyePosition=[-0.045, 3.0, 3.0], cameraTargetPosition=[-0.045,0,0], cameraUpVector=[0,0,1.0]) +viewMatrixLeft = p.computeViewMatrix(cameraEyePosition=[0.045, 3.0, 3.0], cameraTargetPosition=[0.045,0,0], cameraUpVector=[0,0,1]) +viewMatrixRight = p.computeViewMatrix(cameraEyePosition=[-0.045, 3.0, 3.0], cameraTargetPosition=[-0.045,0,0], cameraUpVector=[0,0,1]) leftImage = p.getCameraImage(width=640,height=512,viewMatrix=viewMatrixLeft,projectionMatrix=projectionMatrix,renderer=p.ER_BULLET_HARDWARE_OPENGL) rightImage = p.getCameraImage(width=640,height=512,viewMatrix=viewMatrixRight,projectionMatrix=projectionMatrix,renderer=p.ER_BULLET_HARDWARE_OPENGL) left_img = np.reshape(np.asarray(leftImage[2], dtype=np.float32), (512, 640, 4)) left_img /= 255 -plt.imsave("left_image_new.png", left_img) +plt.imsave("left_image.png", left_img) right_img = np.reshape(np.asarray(rightImage[2], dtype=np.float32), (512, 640, 4)) right_img /= 255 -plt.imsave("right_image_new.png", right_img) +plt.imsave("right_image.png", right_img) useRealTimeSimulation = 1 From 7dbc5626fb3f66ffd9fa08e660a78c5ec1b68e48 Mon Sep 17 00:00:00 2001 From: yunfeibai Date: Mon, 12 Feb 2018 18:19:05 -0800 Subject: [PATCH 3/7] Add the shader files. --- .../projectiveTextureInstancingPS.glsl | 72 ++++++++++++++ .../Shaders/projectiveTextureInstancingPS.h | 65 ++++++++++++ .../projectiveTextureInstancingVS.glsl | 99 +++++++++++++++++++ .../Shaders/projectiveTextureInstancingVS.h | 86 ++++++++++++++++ 4 files changed, 322 insertions(+) create mode 100644 examples/OpenGLWindow/Shaders/projectiveTextureInstancingPS.glsl create mode 100644 examples/OpenGLWindow/Shaders/projectiveTextureInstancingPS.h create mode 100644 examples/OpenGLWindow/Shaders/projectiveTextureInstancingVS.glsl create mode 100644 examples/OpenGLWindow/Shaders/projectiveTextureInstancingVS.h diff --git a/examples/OpenGLWindow/Shaders/projectiveTextureInstancingPS.glsl b/examples/OpenGLWindow/Shaders/projectiveTextureInstancingPS.glsl new file mode 100644 index 000000000..4e11e3762 --- /dev/null +++ b/examples/OpenGLWindow/Shaders/projectiveTextureInstancingPS.glsl @@ -0,0 +1,72 @@ +#version 330 core +//precision highp float; + +in Fragment +{ + vec4 color; +} fragment; + +in Vert +{ + vec2 texcoord; +} vert; + +uniform sampler2D Diffuse; +uniform mat4 ViewMatrixInverse; + +in vec3 lightPos,cameraPosition, normal,ambient; +in vec4 vertexPos; +in float materialShininess; +in vec3 lightSpecularIntensity; +in vec3 materialSpecularColor; + +out vec4 color; + + + +void main(void) +{ + vec4 texel = fragment.color*texture(Diffuse,vert.texcoord.xy); + vec3 ct,cf; + float intensity,at,af; + if (fragment.color.w==0) + discard; + vec3 lightDir = normalize(lightPos); + + vec3 normalDir = normalize(normal); + + intensity = 0.5+0.5*clamp( dot( normalDir,lightDir ), -1,1 ); + + af = 1.0; + + ct = texel.rgb; + at = texel.a; + + //float bias = 0.005f; + + vec3 specularReflection; + + if (dot(normalDir, lightDir) < 0.0) + { + specularReflection = vec3(0.0, 0.0, 0.0); + } + else // light source on the right side + { + vec3 surfaceToLight = normalize(lightPos - vertexPos.xyz); + vec3 surfaceToCamera = normalize(cameraPosition - vertexPos.xyz); + + + float specularCoefficient = 0.0; + specularCoefficient = pow(max(0.0, dot(surfaceToCamera, reflect(-surfaceToLight, normalDir))), materialShininess); + specularReflection = specularCoefficient * materialSpecularColor * lightSpecularIntensity; + + } + + + float visibility = 1.0; + + intensity = 0.7*intensity + 0.3*intensity*visibility; + + cf = intensity*(vec3(1.0,1.0,1.0)-ambient)+ambient+specularReflection*visibility; + color = vec4(ct * cf, fragment.color.w); +} diff --git a/examples/OpenGLWindow/Shaders/projectiveTextureInstancingPS.h b/examples/OpenGLWindow/Shaders/projectiveTextureInstancingPS.h new file mode 100644 index 000000000..2bdb30b64 --- /dev/null +++ b/examples/OpenGLWindow/Shaders/projectiveTextureInstancingPS.h @@ -0,0 +1,65 @@ +//this file is autogenerated using stringify.bat (premake --stringify) in the build folder of this project +static const char* projectiveTextureInstancingFragmentShader= \ +"#version 330 core\n" +"//precision highp float;\n" +"in Fragment\n" +"{\n" +" vec4 color;\n" +"} fragment;\n" +"in Vert\n" +"{\n" +" vec2 texcoord;\n" +"} vert;\n" +"uniform sampler2D Diffuse;\n" +"uniform mat4 ViewMatrixInverse;\n" +"in vec3 lightPos,cameraPosition, normal,ambient;\n" +"in vec4 vertexPos;\n" +"in float materialShininess;\n" +"in vec3 lightSpecularIntensity;\n" +"in vec3 materialSpecularColor;\n" +"out vec4 color;\n" +"void main(void)\n" +"{\n" +" vec4 texel = fragment.color*texture(Diffuse,vert.texcoord.xy);\n" +" vec3 ct,cf;\n" +" float intensity,at,af;\n" +" if (fragment.color.w==0)\n" +" discard;\n" +" vec3 lightDir = normalize(lightPos);\n" +" \n" +" vec3 normalDir = normalize(normal);\n" +" \n" +" intensity = 0.5+0.5*clamp( dot( normalDir,lightDir ), -1,1 );\n" +" \n" +" af = 1.0;\n" +" \n" +" ct = texel.rgb;\n" +" at = texel.a;\n" +" \n" +" //float bias = 0.005f;\n" +" \n" +" vec3 specularReflection;\n" +" \n" +" if (dot(normalDir, lightDir) < 0.0) \n" +" {\n" +" specularReflection = vec3(0.0, 0.0, 0.0);\n" +" }\n" +" else // light source on the right side\n" +" {\n" +" vec3 surfaceToLight = normalize(lightPos - vertexPos.xyz);\n" +" vec3 surfaceToCamera = normalize(cameraPosition - vertexPos.xyz);\n" +" \n" +" \n" +" float specularCoefficient = 0.0;\n" +" specularCoefficient = pow(max(0.0, dot(surfaceToCamera, reflect(-surfaceToLight, normalDir))), materialShininess);\n" +" specularReflection = specularCoefficient * materialSpecularColor * lightSpecularIntensity;\n" +" \n" +" }\n" +" \n" +" float visibility = 1.0;\n" +" intensity = 0.7*intensity + 0.3*intensity*visibility;\n" +" \n" +" cf = intensity*(vec3(1.0,1.0,1.0)-ambient)+ambient+specularReflection*visibility;\n" +" color = vec4(ct * cf, fragment.color.w);\n" +"}\n" +; diff --git a/examples/OpenGLWindow/Shaders/projectiveTextureInstancingVS.glsl b/examples/OpenGLWindow/Shaders/projectiveTextureInstancingVS.glsl new file mode 100644 index 000000000..01d73c2d4 --- /dev/null +++ b/examples/OpenGLWindow/Shaders/projectiveTextureInstancingVS.glsl @@ -0,0 +1,99 @@ +#version 330 +precision highp float; + + +layout (location = 0) in vec4 position; +layout (location = 1) in vec4 instance_position; +layout (location = 2) in vec4 instance_quaternion; +layout (location = 3) in vec2 uvcoords; +layout (location = 4) in vec3 vertexnormal; +layout (location = 5) in vec4 instance_color; +layout (location = 6) in vec3 instance_scale; + + +uniform mat4 DepthBiasModelViewProjectionMatrix; +uniform mat4 MVP; +uniform vec3 lightPosIn; +uniform vec3 cameraPositionIn; +uniform mat4 ViewMatrixInverse; +uniform float materialShininessIn; +uniform vec3 lightSpecularIntensityIn; +uniform vec3 materialSpecularColorIn; + +out vec4 ShadowCoord; + +out Fragment +{ + vec4 color; +} fragment; + +out Vert +{ + vec2 texcoord; +} vert; + + +vec4 quatMul ( in vec4 q1, in vec4 q2 ) +{ + vec3 im = q1.w * q2.xyz + q1.xyz * q2.w + cross ( q1.xyz, q2.xyz ); + vec4 dt = q1 * q2; + float re = dot ( dt, vec4 ( -1.0, -1.0, -1.0, 1.0 ) ); + return vec4 ( im, re ); +} + +vec4 quatFromAxisAngle(vec4 axis, in float angle) +{ + float cah = cos(angle*0.5); + float sah = sin(angle*0.5); + float d = inversesqrt(dot(axis,axis)); + vec4 q = vec4(axis.x*sah*d,axis.y*sah*d,axis.z*sah*d,cah); + return q; +} +// +// vector rotation via quaternion +// +vec4 quatRotate3 ( in vec3 p, in vec4 q ) +{ + vec4 temp = quatMul ( q, vec4 ( p, 0.0 ) ); + return quatMul ( temp, vec4 ( -q.x, -q.y, -q.z, q.w ) ); +} +vec4 quatRotate ( in vec4 p, in vec4 q ) +{ + vec4 temp = quatMul ( q, p ); + return quatMul ( temp, vec4 ( -q.x, -q.y, -q.z, q.w ) ); +} + +out vec3 lightPos,normal,ambient; +out vec4 vertexPos; +out vec3 cameraPosition; +out float materialShininess; +out vec3 lightSpecularIntensity; +out vec3 materialSpecularColor; + + +void main(void) +{ + vec4 q = instance_quaternion; + ambient = vec3(0.5,.5,0.5); + + vec4 worldNormal = (quatRotate3( vertexnormal,q)); + + normal = worldNormal.xyz; + + lightPos = lightPosIn; + cameraPosition = cameraPositionIn; + materialShininess = materialShininessIn; + lightSpecularIntensity = lightSpecularIntensityIn; + materialSpecularColor = materialSpecularColorIn; + + vec4 localcoord = quatRotate3( position.xyz*instance_scale,q); + vertexPos = vec4((instance_position+localcoord).xyz,1); + + vec4 vertexLoc = MVP* vec4((instance_position+localcoord).xyz,1); + gl_Position = vertexLoc; + + fragment.color = instance_color; + vec4 projcoords = DepthBiasModelViewProjectionMatrix * vec4((instance_position+localcoord).xyz,1); + vert.texcoord = projcoords.xy; +} + diff --git a/examples/OpenGLWindow/Shaders/projectiveTextureInstancingVS.h b/examples/OpenGLWindow/Shaders/projectiveTextureInstancingVS.h new file mode 100644 index 000000000..3f929e44b --- /dev/null +++ b/examples/OpenGLWindow/Shaders/projectiveTextureInstancingVS.h @@ -0,0 +1,86 @@ +//this file is autogenerated using stringify.bat (premake --stringify) in the build folder of this project +static const char* projectiveTextureInstancingVertexShader= \ +"#version 330 \n" +"precision highp float;\n" +"layout (location = 0) in vec4 position;\n" +"layout (location = 1) in vec4 instance_position;\n" +"layout (location = 2) in vec4 instance_quaternion;\n" +"layout (location = 3) in vec2 uvcoords;\n" +"layout (location = 4) in vec3 vertexnormal;\n" +"layout (location = 5) in vec4 instance_color;\n" +"layout (location = 6) in vec3 instance_scale;\n" +"uniform mat4 DepthBiasModelViewProjectionMatrix;\n" +"uniform mat4 MVP;\n" +"uniform vec3 lightPosIn;\n" +"uniform vec3 cameraPositionIn;\n" +"uniform mat4 ViewMatrixInverse;\n" +"uniform float materialShininessIn;\n" +"uniform vec3 lightSpecularIntensityIn;\n" +"uniform vec3 materialSpecularColorIn;\n" +"out vec4 ShadowCoord;\n" +"out Fragment\n" +"{\n" +" vec4 color;\n" +"} fragment;\n" +"out Vert\n" +"{\n" +" vec2 texcoord;\n" +"} vert;\n" +"vec4 quatMul ( in vec4 q1, in vec4 q2 )\n" +"{\n" +" vec3 im = q1.w * q2.xyz + q1.xyz * q2.w + cross ( q1.xyz, q2.xyz );\n" +" vec4 dt = q1 * q2;\n" +" float re = dot ( dt, vec4 ( -1.0, -1.0, -1.0, 1.0 ) );\n" +" return vec4 ( im, re );\n" +"}\n" +"vec4 quatFromAxisAngle(vec4 axis, in float angle)\n" +"{\n" +" float cah = cos(angle*0.5);\n" +" float sah = sin(angle*0.5);\n" +" float d = inversesqrt(dot(axis,axis));\n" +" vec4 q = vec4(axis.x*sah*d,axis.y*sah*d,axis.z*sah*d,cah);\n" +" return q;\n" +"}\n" +"//\n" +"// vector rotation via quaternion\n" +"//\n" +"vec4 quatRotate3 ( in vec3 p, in vec4 q )\n" +"{\n" +" vec4 temp = quatMul ( q, vec4 ( p, 0.0 ) );\n" +" return quatMul ( temp, vec4 ( -q.x, -q.y, -q.z, q.w ) );\n" +"}\n" +"vec4 quatRotate ( in vec4 p, in vec4 q )\n" +"{\n" +" vec4 temp = quatMul ( q, p );\n" +" return quatMul ( temp, vec4 ( -q.x, -q.y, -q.z, q.w ) );\n" +"}\n" +"out vec3 lightPos,normal,ambient;\n" +"out vec4 vertexPos;\n" +"out vec3 cameraPosition;\n" +"out float materialShininess;\n" +"out vec3 lightSpecularIntensity;\n" +"out vec3 materialSpecularColor;\n" +"void main(void)\n" +"{\n" +" vec4 q = instance_quaternion;\n" +" ambient = vec3(0.5,.5,0.5);\n" +" \n" +" vec4 worldNormal = (quatRotate3( vertexnormal,q));\n" +" \n" +" normal = worldNormal.xyz;\n" +" lightPos = lightPosIn;\n" +" cameraPosition = cameraPositionIn;\n" +" materialShininess = materialShininessIn;\n" +" lightSpecularIntensity = lightSpecularIntensityIn;\n" +" materialSpecularColor = materialSpecularColorIn;\n" +" \n" +" vec4 localcoord = quatRotate3( position.xyz*instance_scale,q);\n" +" vertexPos = vec4((instance_position+localcoord).xyz,1);\n" +" \n" +" vec4 vertexLoc = MVP* vec4((instance_position+localcoord).xyz,1);\n" +" gl_Position = vertexLoc;\n" +" fragment.color = instance_color;\n" +" vec4 projcoords = DepthBiasModelViewProjectionMatrix * vec4((instance_position+localcoord).xyz,1);\n" +" vert.texcoord = projcoords.xy;\n" +"}\n" +; From cf4a0a35031f1cc931d112f4375f5ab4f89b49a8 Mon Sep 17 00:00:00 2001 From: yunfeibai Date: Thu, 15 Feb 2018 13:19:41 -0800 Subject: [PATCH 4/7] Add improvement/fix in SortableTransparentInstance back. --- .../OpenGLWindow/GLInstancingRenderer.cpp | 46 +++++++++++-------- 1 file changed, 26 insertions(+), 20 deletions(-) diff --git a/examples/OpenGLWindow/GLInstancingRenderer.cpp b/examples/OpenGLWindow/GLInstancingRenderer.cpp index edbe0ee7d..6edb4a69f 100644 --- a/examples/OpenGLWindow/GLInstancingRenderer.cpp +++ b/examples/OpenGLWindow/GLInstancingRenderer.cpp @@ -1989,22 +1989,21 @@ void GLInstancingRenderer::drawLine(const float from[4], const float to[4], cons glUseProgram(0); } -struct SortableTransparentInstance +B3_ATTRIBUTE_ALIGNED16(struct) SortableTransparentInstance { + b3Scalar m_projection; + int m_shapeIndex; int m_instanceId; - b3Vector3 m_centerPosition; }; -struct TransparentDistanceSortPredicate +B3_ATTRIBUTE_ALIGNED16(struct) TransparentDistanceSortPredicate { - b3Vector3 m_camForwardVec; - - inline bool operator() (const SortableTransparentInstance& a, const SortableTransparentInstance& b) const + + inline bool operator() (const SortableTransparentInstance& a, const SortableTransparentInstance& b) const { - b3Scalar projA = a.m_centerPosition.dot(m_camForwardVec); - b3Scalar projB = b.m_centerPosition.dot(m_camForwardVec); - return (projA > projB); + + return (a.m_projection > b.m_projection); } }; @@ -2225,6 +2224,11 @@ b3Assert(glGetError() ==GL_NO_ERROR); //GLuint lastBindTexture = 0; transparentInstances.reserve(totalNumInstances); + + float fwd[3]; + m_data->m_activeCamera->getCameraForwardVector(fwd); + b3Vector3 camForwardVec; + camForwardVec.setValue(fwd[0],fwd[1],fwd[2]); for (int obj=0;objm_flags&eGfxTransparency)==0) { inst.m_instanceId = curOffset; - inst.m_centerPosition.setValue(m_data->m_instance_positions_ptr[inst.m_instanceId*4+0], - m_data->m_instance_positions_ptr[inst.m_instanceId*4+1], - m_data->m_instance_positions_ptr[inst.m_instanceId*4+2]); - inst.m_centerPosition *= -1;//reverse sort opaque instances + b3Vector3 centerPosition; + centerPosition.setValue(m_data->m_instance_positions_ptr[inst.m_instanceId*4+0], + m_data->m_instance_positions_ptr[inst.m_instanceId*4+1], + m_data->m_instance_positions_ptr[inst.m_instanceId*4+2]); + centerPosition *= -1;//reverse sort opaque instances + inst.m_projection = centerPosition.dot(camForwardVec); transparentInstances.push_back(inst); } else { for (int i=0;im_numGraphicsInstances;i++) { inst.m_instanceId = curOffset+i; - inst.m_centerPosition.setValue(m_data->m_instance_positions_ptr[inst.m_instanceId*4+0], - m_data->m_instance_positions_ptr[inst.m_instanceId*4+1], - m_data->m_instance_positions_ptr[inst.m_instanceId*4+2]); + b3Vector3 centerPosition; + + centerPosition.setValue(m_data->m_instance_positions_ptr[inst.m_instanceId*4+0], + m_data->m_instance_positions_ptr[inst.m_instanceId*4+1], + m_data->m_instance_positions_ptr[inst.m_instanceId*4+2]); + inst.m_projection = centerPosition.dot(camForwardVec); transparentInstances.push_back(inst); } } @@ -2259,9 +2268,7 @@ b3Assert(glGetError() ==GL_NO_ERROR); } } TransparentDistanceSortPredicate sorter; - float fwd[3]; - m_data->m_activeCamera->getCameraForwardVector(fwd); - sorter.m_camForwardVec.setValue(fwd[0],fwd[1],fwd[2]); + transparentInstances.quickSort(sorter); } @@ -2532,7 +2539,6 @@ b3Assert(glGetError() ==GL_NO_ERROR); } case B3_USE_PROJECTIVE_TEXTURE_RENDERMODE: { - printf("PROJECTIVE TEXTURE!!\n"); if ( gfxObj->m_flags&eGfxTransparency) { glDepthMask(false); From 36dcabbaf5507b2c64ae36798a79db73be4a8030 Mon Sep 17 00:00:00 2001 From: yunfeibai Date: Thu, 15 Feb 2018 14:31:34 -0800 Subject: [PATCH 5/7] Merge remote-tracking branch 'upstream/master' --- Extras/ConvexDecomposition/premake4.lua | 3 + Extras/HACD/premake4.lua | 3 + Extras/InverseDynamics/premake4.lua | 3 + .../Serialize/BulletFileLoader/premake4.lua | 4 + .../BulletWorldImporter/premake4.lua | 6 +- .../BulletXmlWorldImporter/premake4.lua | 4 +- Extras/VHACD/src/premake4.lua | 3 + data/multibody.bullet | Bin 16836 -> 16836 bytes examples/ExampleBrowser/premake4.lua | 10 ++- .../OpenGLWindow/GLInstancingRenderer.cpp | 21 +++--- examples/OpenGLWindow/premake4.lua | 6 +- .../b3RobotSimulatorClientAPI.cpp | 2 +- examples/SharedMemory/CMakeLists.txt | 9 ++- examples/SharedMemory/b3PluginManager.cpp | 29 ++++--- examples/SharedMemory/premake4.lua | 3 + examples/SharedMemory/tcp/premake4.lua | 1 + examples/SharedMemory/udp/premake4.lua | 4 + examples/ThirdPartyLibs/BussIK/premake4.lua | 3 + examples/ThirdPartyLibs/Gwen/premake4.lua | 3 + examples/ThirdPartyLibs/clsocket/premake4.lua | 3 + examples/ThirdPartyLibs/enet/premake4.lua | 3 + .../ThirdPartyLibs/lua-5.2.3/premake4.lua | 3 + examples/ThirdPartyLibs/serial/premake4.lua | 3 + .../enjoy_kuka_diverse_object_grasping.py | 2 +- .../pybullet_envs/bullet/cartpole_bullet.py | 12 ++- .../gym/pybullet_envs/bullet/kukaCamGymEnv.py | 49 ++++++------ .../gym/pybullet_envs/bullet/kukaGymEnv.py | 71 ++++++++++-------- .../bullet/kuka_diverse_object_gym_env.py | 11 ++- .../bullet/minitaur_duck_gym_env.py | 5 ++ .../pybullet_envs/bullet/minitaur_gym_env.py | 7 ++ .../gym/pybullet_envs/bullet/racecarGymEnv.py | 39 ++++++---- .../pybullet_envs/bullet/racecarZEDGymEnv.py | 37 +++++---- .../bullet/simpleHumanoidGymEnv.py | 24 +++--- .../pybullet/gym/pybullet_envs/env_bases.py | 21 +++++- .../examples/minitaur_gym_env_example.py | 6 ++ .../gym/pybullet_envs/examples/testBike.py | 6 ++ src/Bullet3Collision/premake4.lua | 3 + src/Bullet3Common/premake4.lua | 4 + src/Bullet3Dynamics/premake4.lua | 3 + src/Bullet3Geometry/premake4.lua | 3 + src/Bullet3OpenCL/premake4.lua | 3 + .../Bullet2FileLoader/premake4.lua | 4 + src/BulletCollision/premake4.lua | 3 + src/BulletDynamics/premake4.lua | 3 + src/BulletInverseDynamics/premake4.lua | 3 + src/BulletSoftBody/premake4.lua | 3 + src/LinearMath/premake4.lua | 3 + test/SharedMemory/CMakeLists.txt | 6 +- test/SharedMemory/premake4.lua | 6 ++ test/gtest-1.7.0/premake4.lua | 4 + 50 files changed, 332 insertions(+), 138 deletions(-) diff --git a/Extras/ConvexDecomposition/premake4.lua b/Extras/ConvexDecomposition/premake4.lua index 05ae0f685..9310593a7 100644 --- a/Extras/ConvexDecomposition/premake4.lua +++ b/Extras/ConvexDecomposition/premake4.lua @@ -3,6 +3,9 @@ kind "StaticLib" includedirs {".","../../src"} + if os.is("Linux") then + buildoptions{"-fPIC"} + end files { "**.cpp", "**.h" diff --git a/Extras/HACD/premake4.lua b/Extras/HACD/premake4.lua index 14b83929a..c2f03c173 100644 --- a/Extras/HACD/premake4.lua +++ b/Extras/HACD/premake4.lua @@ -3,6 +3,9 @@ kind "StaticLib" includedirs {"."} + if os.is("Linux") then + buildoptions{"-fPIC"} + end files { "**.cpp", "**.h" diff --git a/Extras/InverseDynamics/premake4.lua b/Extras/InverseDynamics/premake4.lua index 0a524d46a..c1e263bfb 100644 --- a/Extras/InverseDynamics/premake4.lua +++ b/Extras/InverseDynamics/premake4.lua @@ -6,6 +6,9 @@ "../../src" } + if os.is("Linux") then + buildoptions{"-fPIC"} + end files { "*.cpp", "*.hpp" diff --git a/Extras/Serialize/BulletFileLoader/premake4.lua b/Extras/Serialize/BulletFileLoader/premake4.lua index a1a814278..9fa0cb00c 100644 --- a/Extras/Serialize/BulletFileLoader/premake4.lua +++ b/Extras/Serialize/BulletFileLoader/premake4.lua @@ -2,6 +2,10 @@ kind "StaticLib" + if os.is("Linux") then + buildoptions{"-fPIC"} + end + includedirs { "../../../src" } diff --git a/Extras/Serialize/BulletWorldImporter/premake4.lua b/Extras/Serialize/BulletWorldImporter/premake4.lua index 4d2d9d879..e7dbd9a77 100644 --- a/Extras/Serialize/BulletWorldImporter/premake4.lua +++ b/Extras/Serialize/BulletWorldImporter/premake4.lua @@ -6,7 +6,11 @@ "../BulletFileLoader", "../../../src" } - + + if os.is("Linux") then + buildoptions{"-fPIC"} + end + files { "**.cpp", "**.h" diff --git a/Extras/Serialize/BulletXmlWorldImporter/premake4.lua b/Extras/Serialize/BulletXmlWorldImporter/premake4.lua index 147f3e951..d940dfd8a 100644 --- a/Extras/Serialize/BulletXmlWorldImporter/premake4.lua +++ b/Extras/Serialize/BulletXmlWorldImporter/premake4.lua @@ -1,7 +1,7 @@ project "BulletXmlWorldImporter" kind "StaticLib" - targetdir "../../lib" + --targetdir "../../lib" includedirs { "../BulletWorldImporter", "../BulletFileLoader", @@ -11,4 +11,4 @@ files { "**.cpp", "**.h" - } \ No newline at end of file + } diff --git a/Extras/VHACD/src/premake4.lua b/Extras/VHACD/src/premake4.lua index 3566943be..10083cffe 100644 --- a/Extras/VHACD/src/premake4.lua +++ b/Extras/VHACD/src/premake4.lua @@ -1,6 +1,9 @@ project "vhacd" kind "StaticLib" + if os.is("Linux") then + buildoptions{"-fPIC"} + end includedirs { "../inc","../public", } diff --git a/data/multibody.bullet b/data/multibody.bullet index 9f8e7ab893719b5d0e076f582540084391b20a3d..d621a77ca9620a49ce57bb971d25f9081f75dc1a 100644 GIT binary patch delta 624 zcmX@o%y^`kaRUz{s{#W9gW_aC#^}j5>>Ty~sRV$Ag29r4z5756B8}A~Qr-XmlVzAv zJaMVUWg{+e#>t6{+5*f#X%L2)I@y3#Y_b%yR6R^!fM{Qe?adczwt-=wSG^|Nv9e7L jU}5p0*kmLFfClSKKEuMn0TbJNhsA=8@y%vY?)7E>h~*9V delta 136 zcmX@o%y^`kaRUz{YY77bL*`^b#^{L`L?#z-xv(rL*t>6XB3H=7Cqk3;n1Uu}a(PTF z5t{74B*n@MRLeZskV$f~9V^FVDP}34q(4xT2T)d%Cts-928Jhp#$P0-K+(7_c$E-Ym+!-V6ZwSSYIi diff --git a/examples/ExampleBrowser/premake4.lua b/examples/ExampleBrowser/premake4.lua index 65e38ece8..3c38d756d 100644 --- a/examples/ExampleBrowser/premake4.lua +++ b/examples/ExampleBrowser/premake4.lua @@ -3,7 +3,11 @@ project "App_BulletExampleBrowser" language "C++" kind "ConsoleApp" - + + if os.is("Linux") then + buildoptions{"-fPIC"} + end + hasCL = findOpenCL("clew") if (hasCL) then @@ -209,6 +213,10 @@ project "BulletExampleBrowserLib" "../../src", "../ThirdPartyLibs", } + + if os.is("Linux") then + buildoptions{"-fPIC"} + end if _OPTIONS["lua"] then includedirs{"../ThirdPartyLibs/lua-5.2.3/src"} diff --git a/examples/OpenGLWindow/GLInstancingRenderer.cpp b/examples/OpenGLWindow/GLInstancingRenderer.cpp index 6edb4a69f..33e54d0fa 100644 --- a/examples/OpenGLWindow/GLInstancingRenderer.cpp +++ b/examples/OpenGLWindow/GLInstancingRenderer.cpp @@ -1992,15 +1992,15 @@ void GLInstancingRenderer::drawLine(const float from[4], const float to[4], cons B3_ATTRIBUTE_ALIGNED16(struct) SortableTransparentInstance { b3Scalar m_projection; - + int m_shapeIndex; int m_instanceId; }; B3_ATTRIBUTE_ALIGNED16(struct) TransparentDistanceSortPredicate { - - inline bool operator() (const SortableTransparentInstance& a, const SortableTransparentInstance& b) const + + inline bool operator() (const SortableTransparentInstance& a, const SortableTransparentInstance& b) const { return (a.m_projection > b.m_projection); @@ -2218,7 +2218,6 @@ b3Assert(glGetError() ==GL_NO_ERROR); } b3AlignedObjectArray transparentInstances; - { int curOffset = 0; //GLuint lastBindTexture = 0; @@ -2229,7 +2228,8 @@ b3Assert(glGetError() ==GL_NO_ERROR); m_data->m_activeCamera->getCameraForwardVector(fwd); b3Vector3 camForwardVec; camForwardVec.setValue(fwd[0],fwd[1],fwd[2]); - + + for (int obj=0;objm_instance_positions_ptr[inst.m_instanceId*4+0], - m_data->m_instance_positions_ptr[inst.m_instanceId*4+1], - m_data->m_instance_positions_ptr[inst.m_instanceId*4+2]); + m_data->m_instance_positions_ptr[inst.m_instanceId*4+1], + m_data->m_instance_positions_ptr[inst.m_instanceId*4+2]); centerPosition *= -1;//reverse sort opaque instances inst.m_projection = centerPosition.dot(camForwardVec); transparentInstances.push_back(inst); @@ -2256,10 +2256,10 @@ b3Assert(glGetError() ==GL_NO_ERROR); { inst.m_instanceId = curOffset+i; b3Vector3 centerPosition; - + centerPosition.setValue(m_data->m_instance_positions_ptr[inst.m_instanceId*4+0], - m_data->m_instance_positions_ptr[inst.m_instanceId*4+1], - m_data->m_instance_positions_ptr[inst.m_instanceId*4+2]); + m_data->m_instance_positions_ptr[inst.m_instanceId*4+1], + m_data->m_instance_positions_ptr[inst.m_instanceId*4+2]); inst.m_projection = centerPosition.dot(camForwardVec); transparentInstances.push_back(inst); } @@ -2270,6 +2270,7 @@ b3Assert(glGetError() ==GL_NO_ERROR); TransparentDistanceSortPredicate sorter; transparentInstances.quickSort(sorter); + } diff --git a/examples/OpenGLWindow/premake4.lua b/examples/OpenGLWindow/premake4.lua index 17dfdda09..e0b163678 100644 --- a/examples/OpenGLWindow/premake4.lua +++ b/examples/OpenGLWindow/premake4.lua @@ -12,7 +12,11 @@ "../ThirdPartyLibs", "../../src", } - + + if os.is("Linux") then + buildoptions{"-fPIC"} + end + --links { --} diff --git a/examples/RobotSimulator/b3RobotSimulatorClientAPI.cpp b/examples/RobotSimulator/b3RobotSimulatorClientAPI.cpp index f22e4c0aa..3268e351e 100644 --- a/examples/RobotSimulator/b3RobotSimulatorClientAPI.cpp +++ b/examples/RobotSimulator/b3RobotSimulatorClientAPI.cpp @@ -1,7 +1,7 @@ #include "b3RobotSimulatorClientAPI.h" #include "../SharedMemory/PhysicsClientC_API.h" -#include "b3RobotSimulatorClientAPI_InternalData.h"" +#include "b3RobotSimulatorClientAPI_InternalData.h" #ifdef BT_ENABLE_ENET #include "../SharedMemory/PhysicsClientUDP_C_API.h" #endif //PHYSICS_UDP diff --git a/examples/SharedMemory/CMakeLists.txt b/examples/SharedMemory/CMakeLists.txt index 4ea326d98..d1ab3af24 100644 --- a/examples/SharedMemory/CMakeLists.txt +++ b/examples/SharedMemory/CMakeLists.txt @@ -92,13 +92,14 @@ INCLUDE_DIRECTORIES( ${BULLET_PHYSICS_SOURCE_DIR}/examples/ThirdPartyLibs ) +IF (USE_SOFT_BODY_MULTI_BODY_DYNAMICS_WORLD) + LINK_LIBRARIES(BulletSoftBody) +ENDIF() + LINK_LIBRARIES( Bullet3Common BulletWorldImporter BulletFileLoader BulletInverseDynamicsUtils BulletInverseDynamics BulletDynamics BulletCollision LinearMath BussIK ) -IF (USE_SOFT_BODY_MULTI_BODY_DYNAMICS_WORLD) - LINK_LIBRARIES(BulletSoftBody) -ENDIF() IF (WIN32) ADD_EXECUTABLE(App_PhysicsServer_SharedMemory @@ -149,7 +150,7 @@ INCLUDE_DIRECTORIES( ADD_DEFINITIONS(-DB3_USE_STANDALONE_EXAMPLE) LINK_LIBRARIES( - Bullet3Common BulletWorldImporter BulletInverseDynamicsUtils BulletInverseDynamics BulletDynamics BulletCollision LinearMath BussIK OpenGLWindow + BulletSoftBody Bullet3Common BulletWorldImporter BulletInverseDynamicsUtils BulletInverseDynamics BulletDynamics BulletCollision LinearMath BussIK OpenGLWindow ) diff --git a/examples/SharedMemory/b3PluginManager.cpp b/examples/SharedMemory/b3PluginManager.cpp index 7814bfee5..66050767e 100644 --- a/examples/SharedMemory/b3PluginManager.cpp +++ b/examples/SharedMemory/b3PluginManager.cpp @@ -78,7 +78,7 @@ typedef b3PoolBodyHandle b3PluginHandle; struct b3PluginManagerInternalData { b3ResizablePool m_plugins; - b3HashMap m_pluginMap; + b3HashMap m_pluginMap; PhysicsDirect* m_physicsDirect; b3AlignedObjectArray m_keyEvents; b3AlignedObjectArray m_vrEvents; @@ -102,8 +102,12 @@ b3PluginManager::~b3PluginManager() { while (m_data->m_pluginMap.size()) { - b3PluginHandle** plugin = m_data->m_pluginMap.getAtIndex(0); - unloadPlugin((*plugin)->m_pluginUniqueId); + int* pluginUidPtr = m_data->m_pluginMap.getAtIndex(0); + if (pluginUidPtr) + { + int pluginUid = *pluginUidPtr; + unloadPlugin(*pluginUidPtr); + } } delete m_data->m_physicsDirect; m_data->m_pluginMap.clear(); @@ -140,11 +144,11 @@ int b3PluginManager::loadPlugin(const char* pluginPath, const char* postFixStr) { int pluginUniqueId = -1; - b3PluginHandle** pluginOrgPtr = m_data->m_pluginMap.find(pluginPath); - if (pluginOrgPtr) + int* pluginUidPtr = m_data->m_pluginMap.find(pluginPath); + if (pluginUidPtr) { //already loaded - pluginUniqueId = (*pluginOrgPtr)->m_pluginUniqueId; + pluginUniqueId = *pluginUidPtr; } else { @@ -185,7 +189,7 @@ int b3PluginManager::loadPlugin(const char* pluginPath, const char* postFixStr) plugin->m_ownsPluginHandle = true; plugin->m_pluginHandle = pluginHandle; plugin->m_pluginPath = pluginPath; - m_data->m_pluginMap.insert(pluginPath, plugin); + m_data->m_pluginMap.insert(pluginPath, pluginUniqueId); } else { @@ -246,16 +250,19 @@ void b3PluginManager::tickPlugins(double timeStep, bool isPreTick) { for (int i=0;im_pluginMap.size();i++) { - b3PluginHandle** pluginPtr = m_data->m_pluginMap.getAtIndex(i); + int* pluginUidPtr = m_data->m_pluginMap.getAtIndex(i); b3PluginHandle* plugin = 0; - if (pluginPtr && *pluginPtr) + + if (pluginUidPtr) { - plugin = *pluginPtr; + int pluginUid = *pluginUidPtr; + plugin = m_data->m_plugins.getHandle(pluginUid); } else { continue; } + PFN_TICK tick = isPreTick? plugin->m_preTickFunc : plugin->m_postTickFunc; if (tick) { @@ -319,7 +326,7 @@ int b3PluginManager::registerStaticLinkedPlugin(const char* pluginPath, PFN_INIT pluginHandle->m_userPointer = 0; - m_data->m_pluginMap.insert(pluginPath, pluginHandle); + m_data->m_pluginMap.insert(pluginPath, pluginUniqueId); { b3PluginContext context; diff --git a/examples/SharedMemory/premake4.lua b/examples/SharedMemory/premake4.lua index a81acd4b9..fd5f87543 100644 --- a/examples/SharedMemory/premake4.lua +++ b/examples/SharedMemory/premake4.lua @@ -12,6 +12,9 @@ includedirs {".","../../src", "../ThirdPartyLibs"} links { "BulletSoftBody", "Bullet3Common","BulletInverseDynamicsUtils", "BulletInverseDynamics", "BulletDynamics","BulletCollision", "LinearMath", "BussIK" } +if os.is("Linux") then + links{"dl"} +end language "C++" diff --git a/examples/SharedMemory/tcp/premake4.lua b/examples/SharedMemory/tcp/premake4.lua index 790294228..46596cec1 100644 --- a/examples/SharedMemory/tcp/premake4.lua +++ b/examples/SharedMemory/tcp/premake4.lua @@ -73,6 +73,7 @@ links { end if os.is("Linux") then defines {"_LINUX"} + links{"dl"} end if os.is("MacOSX") then defines {"_DARWIN"} diff --git a/examples/SharedMemory/udp/premake4.lua b/examples/SharedMemory/udp/premake4.lua index 1255c236c..dd8f64d61 100644 --- a/examples/SharedMemory/udp/premake4.lua +++ b/examples/SharedMemory/udp/premake4.lua @@ -14,6 +14,7 @@ project ("App_PhysicsServerSharedMemoryBridgeUDP") links {"Ws2_32","Winmm"} end if os.is("Linux") then + links{"dl"} end if os.is("MacOSX") then end @@ -68,6 +69,9 @@ if os.is("Windows") then defines { "WIN32" } links {"Ws2_32","Winmm"} end + if os.is("Linux") then + links{"dl"} + end language "C++" diff --git a/examples/ThirdPartyLibs/BussIK/premake4.lua b/examples/ThirdPartyLibs/BussIK/premake4.lua index 74a0630a0..a999b0e0f 100644 --- a/examples/ThirdPartyLibs/BussIK/premake4.lua +++ b/examples/ThirdPartyLibs/BussIK/premake4.lua @@ -5,6 +5,9 @@ includedirs { "." } + if os.is("Linux") then + buildoptions{"-fPIC"} + end files { "*.cpp", "*.h", diff --git a/examples/ThirdPartyLibs/Gwen/premake4.lua b/examples/ThirdPartyLibs/Gwen/premake4.lua index 74d16aac9..c456a8da3 100644 --- a/examples/ThirdPartyLibs/Gwen/premake4.lua +++ b/examples/ThirdPartyLibs/Gwen/premake4.lua @@ -11,6 +11,9 @@ end defines { "GWEN_COMPILE_STATIC" } defines { "DONT_USE_GLUT"} + if os.is("Linux") then + buildoptions{"-fPIC"} + end includedirs { ".",".." } diff --git a/examples/ThirdPartyLibs/clsocket/premake4.lua b/examples/ThirdPartyLibs/clsocket/premake4.lua index 57447b1fa..9e3ce2d0b 100644 --- a/examples/ThirdPartyLibs/clsocket/premake4.lua +++ b/examples/ThirdPartyLibs/clsocket/premake4.lua @@ -16,6 +16,9 @@ includedirs { ".","include","src" } + if os.is("Linux") then + buildoptions{"-fPIC"} + end files { "src/SimpleSocket.cpp", "src/ActiveSocket.cpp", diff --git a/examples/ThirdPartyLibs/enet/premake4.lua b/examples/ThirdPartyLibs/enet/premake4.lua index 05390e766..4eb2036b6 100644 --- a/examples/ThirdPartyLibs/enet/premake4.lua +++ b/examples/ThirdPartyLibs/enet/premake4.lua @@ -18,6 +18,9 @@ includedirs { ".","include" } + if os.is("Linux") then + buildoptions{"-fPIC"} + end files { "callbacks.c", "compress.c", diff --git a/examples/ThirdPartyLibs/lua-5.2.3/premake4.lua b/examples/ThirdPartyLibs/lua-5.2.3/premake4.lua index 504ccb83c..a82fff668 100644 --- a/examples/ThirdPartyLibs/lua-5.2.3/premake4.lua +++ b/examples/ThirdPartyLibs/lua-5.2.3/premake4.lua @@ -18,6 +18,9 @@ includedirs { "src" } + if os.is("Linux") then + buildoptions{"-fPIC"} + end files { "src/*.c", "src/*.h" diff --git a/examples/ThirdPartyLibs/serial/premake4.lua b/examples/ThirdPartyLibs/serial/premake4.lua index f57a6618f..d2dfeb637 100644 --- a/examples/ThirdPartyLibs/serial/premake4.lua +++ b/examples/ThirdPartyLibs/serial/premake4.lua @@ -4,6 +4,9 @@ includedirs {"include"} + if os.is("Linux") then + buildoptions{"-fPIC"} + end if os.is("Windows") then files{ "src/impl/win.cc", diff --git a/examples/pybullet/gym/pybullet_envs/baselines/enjoy_kuka_diverse_object_grasping.py b/examples/pybullet/gym/pybullet_envs/baselines/enjoy_kuka_diverse_object_grasping.py index 9e4af6a08..da40d6e94 100644 --- a/examples/pybullet/gym/pybullet_envs/baselines/enjoy_kuka_diverse_object_grasping.py +++ b/examples/pybullet/gym/pybullet_envs/baselines/enjoy_kuka_diverse_object_grasping.py @@ -47,7 +47,7 @@ def main(): print(obs) episode_rew = 0 while not done: - env.render() + env.render(mode='human') act = policy.sample_action(obs, .1) print("Action") print(act) diff --git a/examples/pybullet/gym/pybullet_envs/bullet/cartpole_bullet.py b/examples/pybullet/gym/pybullet_envs/bullet/cartpole_bullet.py index e28a9e962..38145c9bf 100644 --- a/examples/pybullet/gym/pybullet_envs/bullet/cartpole_bullet.py +++ b/examples/pybullet/gym/pybullet_envs/bullet/cartpole_bullet.py @@ -17,7 +17,7 @@ import time import subprocess import pybullet as p import pybullet_data - +from pkg_resources import parse_version logger = logging.getLogger(__name__) @@ -64,10 +64,10 @@ class CartPoleBulletEnv(gym.Env): # time.sleep(self.timeStep) self.state = p.getJointState(self.cartpole, 1)[0:2] + p.getJointState(self.cartpole, 0)[0:2] theta, theta_dot, x, x_dot = self.state - + dv = 0.1 deltav = [-10.*dv,-5.*dv, -2.*dv, -0.1*dv, 0, 0.1*dv, 2.*dv,5.*dv, 10.*dv][action] - + p.setJointMotorControl2(self.cartpole, 0, p.VELOCITY_CONTROL, targetVelocity=(deltav + self.state[3])) done = x < -self.x_threshold \ @@ -99,3 +99,9 @@ class CartPoleBulletEnv(gym.Env): def _render(self, mode='human', close=False): return + + if parse_version(gym.__version__)>=parse_version('0.9.6'): + render = _render + reset = _reset + seed = _seed + step = _step diff --git a/examples/pybullet/gym/pybullet_envs/bullet/kukaCamGymEnv.py b/examples/pybullet/gym/pybullet_envs/bullet/kukaCamGymEnv.py index 4e7d7e70e..ed49cff59 100644 --- a/examples/pybullet/gym/pybullet_envs/bullet/kukaCamGymEnv.py +++ b/examples/pybullet/gym/pybullet_envs/bullet/kukaCamGymEnv.py @@ -14,6 +14,8 @@ import pybullet as p from . import kuka import random import pybullet_data +from pkg_resources import parse_version + maxSteps = 1000 RENDER_HEIGHT = 720 @@ -56,8 +58,8 @@ class KukaCamGymEnv(gym.Env): observationDim = len(self.getExtendedObservation()) #print("observationDim") #print(observationDim) - - observation_high = np.array([np.finfo(np.float32).max] * observationDim) + + observation_high = np.array([np.finfo(np.float32).max] * observationDim) if (self._isDiscrete): self.action_space = spaces.Discrete(7) else: @@ -74,15 +76,15 @@ class KukaCamGymEnv(gym.Env): p.setPhysicsEngineParameter(numSolverIterations=150) p.setTimeStep(self._timeStep) p.loadURDF(os.path.join(self._urdfRoot,"plane.urdf"),[0,0,-1]) - + p.loadURDF(os.path.join(self._urdfRoot,"table/table.urdf"), 0.5000000,0.00000,-.820000,0.000000,0.000000,0.0,1.0) - + xpos = 0.5 +0.2*random.random() ypos = 0 +0.25*random.random() ang = 3.1415925438*random.random() orn = p.getQuaternionFromEuler([0,0,ang]) self.blockUid =p.loadURDF(os.path.join(self._urdfRoot,"block.urdf"), xpos,ypos,-0.1,orn[0],orn[1],orn[2],orn[3]) - + p.setGravity(0,0,-10) self._kuka = kuka.Kuka(urdfRootPath=self._urdfRoot, timeStep=self._timeStep) self._envStepCounter = 0 @@ -98,7 +100,7 @@ class KukaCamGymEnv(gym.Env): return [seed] def getExtendedObservation(self): - + #camEyePos = [0.03,0.236,0.54] #distance = 1.06 #pitch=-56 @@ -118,13 +120,13 @@ class KukaCamGymEnv(gym.Env): viewMat = [-0.5120397806167603, 0.7171027660369873, -0.47284144163131714, 0.0, -0.8589617609977722, -0.42747554183006287, 0.28186774253845215, 0.0, 0.0, 0.5504802465438843, 0.8348482847213745, 0.0, 0.1925382763147354, -0.24935829639434814, -0.4401884973049164, 1.0] #projMatrix = camInfo[3]#[0.7499999403953552, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, -1.0000200271606445, -1.0, 0.0, 0.0, -0.02000020071864128, 0.0] projMatrix = [0.75, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, -1.0000200271606445, -1.0, 0.0, 0.0, -0.02000020071864128, 0.0] - + img_arr = p.getCameraImage(width=self._width,height=self._height,viewMatrix=viewMat,projectionMatrix=projMatrix) rgb=img_arr[2] np_img_arr = np.reshape(rgb, (self._height, self._width, 4)) self._observation = np_img_arr return self._observation - + def _step(self, action): if (self._isDiscrete): dv = 0.01 @@ -142,7 +144,7 @@ class KukaCamGymEnv(gym.Env): realAction = [dx,dy,-0.002,da,f] return self.step2( realAction) - + def step2(self, action): for i in range(self._actionRepeat): self._kuka.applyAction(action) @@ -158,11 +160,11 @@ class KukaCamGymEnv(gym.Env): #print("self._envStepCounter") #print(self._envStepCounter) - + done = self._termination() reward = self._reward() #print("len=%r" % len(self._observation)) - + return np.array(self._observation), reward, done, {} def _render(self, mode='human', close=False): @@ -190,18 +192,18 @@ class KukaCamGymEnv(gym.Env): #print (self._kuka.endEffectorPos[2]) state = p.getLinkState(self._kuka.kukaUid,self._kuka.kukaEndEffectorIndex) actualEndEffectorPos = state[0] - + #print("self._envStepCounter") #print(self._envStepCounter) if (self.terminated or self._envStepCounter>maxSteps): self._observation = self.getExtendedObservation() return True - maxDist = 0.005 + maxDist = 0.005 closestPoints = p.getClosestPoints(self._kuka.trayUid, self._kuka.kukaUid,maxDist) - + if (len(closestPoints)):#(actualEndEffectorPos[2] <= -0.43): self.terminated = 1 - + #print("closing gripper, attempting grasp") #start grasp and terminate fingerAngle = 0.3 @@ -212,7 +214,7 @@ class KukaCamGymEnv(gym.Env): fingerAngle = fingerAngle-(0.3/100.) if (fingerAngle<0): fingerAngle=0 - + for i in range (1000): graspAction = [0,0,0.001,0,fingerAngle] self._kuka.applyAction(graspAction) @@ -227,18 +229,18 @@ class KukaCamGymEnv(gym.Env): if (actualEndEffectorPos[2]>0.5): break - + self._observation = self.getExtendedObservation() return True return False - + def _reward(self): - + #rewards is height of target object blockPos,blockOrn=p.getBasePositionAndOrientation(self.blockUid) - closestPoints = p.getClosestPoints(self.blockUid,self._kuka.kukaUid,1000, -1, self._kuka.kukaEndEffectorIndex) + closestPoints = p.getClosestPoints(self.blockUid,self._kuka.kukaUid,1000, -1, self._kuka.kukaEndEffectorIndex) - reward = -1000 + reward = -1000 numPt = len(closestPoints) #print(numPt) if (numPt>0): @@ -254,3 +256,8 @@ class KukaCamGymEnv(gym.Env): #print(reward) return reward + if parse_version(gym.__version__)>=parse_version('0.9.6'): + render = _render + reset = _reset + seed = _seed + step = _step diff --git a/examples/pybullet/gym/pybullet_envs/bullet/kukaGymEnv.py b/examples/pybullet/gym/pybullet_envs/bullet/kukaGymEnv.py index b9ef16017..848f10a90 100644 --- a/examples/pybullet/gym/pybullet_envs/bullet/kukaGymEnv.py +++ b/examples/pybullet/gym/pybullet_envs/bullet/kukaGymEnv.py @@ -13,6 +13,7 @@ import pybullet as p from . import kuka import random import pybullet_data +from pkg_resources import parse_version largeValObservation = 100 @@ -44,7 +45,7 @@ class KukaGymEnv(gym.Env): self._maxSteps = maxSteps self.terminated = 0 self._cam_dist = 1.3 - self._cam_yaw = 180 + self._cam_yaw = 180 self._cam_pitch = -40 self._p = p @@ -61,8 +62,8 @@ class KukaGymEnv(gym.Env): observationDim = len(self.getExtendedObservation()) #print("observationDim") #print(observationDim) - - observation_high = np.array([largeValObservation] * observationDim) + + observation_high = np.array([largeValObservation] * observationDim) if (self._isDiscrete): self.action_space = spaces.Discrete(7) else: @@ -80,15 +81,15 @@ class KukaGymEnv(gym.Env): p.setPhysicsEngineParameter(numSolverIterations=150) p.setTimeStep(self._timeStep) p.loadURDF(os.path.join(self._urdfRoot,"plane.urdf"),[0,0,-1]) - + p.loadURDF(os.path.join(self._urdfRoot,"table/table.urdf"), 0.5000000,0.00000,-.820000,0.000000,0.000000,0.0,1.0) - + xpos = 0.55 +0.12*random.random() ypos = 0 +0.2*random.random() ang = 3.14*0.5+3.1415925438*random.random() orn = p.getQuaternionFromEuler([0,0,ang]) self.blockUid =p.loadURDF(os.path.join(self._urdfRoot,"block.urdf"), xpos,ypos,-0.15,orn[0],orn[1],orn[2],orn[3]) - + p.setGravity(0,0,-10) self._kuka = kuka.Kuka(urdfRootPath=self._urdfRoot, timeStep=self._timeStep) self._envStepCounter = 0 @@ -115,7 +116,7 @@ class KukaGymEnv(gym.Env): dir0 = [gripperMat[0],gripperMat[3],gripperMat[6]] dir1 = [gripperMat[1],gripperMat[4],gripperMat[7]] dir2 = [gripperMat[2],gripperMat[5],gripperMat[8]] - + gripperEul = p.getEulerFromQuaternion(gripperOrn) #print("gripperEul") #print(gripperEul) @@ -126,17 +127,17 @@ class KukaGymEnv(gym.Env): #print(projectedBlockPos2D) #print("blockEulerInGripper") #print(blockEulerInGripper) - + #we return the relative x,y position and euler angle of block in gripper space blockInGripperPosXYEulZ =[blockPosInGripper[0],blockPosInGripper[1],blockEulerInGripper[2]] - + #p.addUserDebugLine(gripperPos,[gripperPos[0]+dir0[0],gripperPos[1]+dir0[1],gripperPos[2]+dir0[2]],[1,0,0],lifeTime=1) #p.addUserDebugLine(gripperPos,[gripperPos[0]+dir1[0],gripperPos[1]+dir1[1],gripperPos[2]+dir1[2]],[0,1,0],lifeTime=1) #p.addUserDebugLine(gripperPos,[gripperPos[0]+dir2[0],gripperPos[1]+dir2[1],gripperPos[2]+dir2[2]],[0,0,1],lifeTime=1) - + self._observation.extend(list(blockInGripperPosXYEulZ)) return self._observation - + def _step(self, action): if (self._isDiscrete): dv = 0.005 @@ -154,7 +155,7 @@ class KukaGymEnv(gym.Env): f = 0.3 realAction = [dx,dy,-0.002,da,f] return self.step2( realAction) - + def step2(self, action): for i in range(self._actionRepeat): self._kuka.applyAction(action) @@ -168,7 +169,7 @@ class KukaGymEnv(gym.Env): #print("self._envStepCounter") #print(self._envStepCounter) - + done = self._termination() npaction = np.array([action[3]]) #only penalize rotation until learning works well [action[0],action[1],action[3]]) actionCost = np.linalg.norm(npaction)*10. @@ -177,14 +178,15 @@ class KukaGymEnv(gym.Env): reward = self._reward()-actionCost #print("reward") #print(reward) - + #print("len=%r" % len(self._observation)) - + return np.array(self._observation), reward, done, {} def _render(self, mode="rgb_array", close=False): if mode != "rgb_array": return np.array([]) + base_pos,orn = self._p.getBasePositionAndOrientation(self._kuka.kukaUid) view_matrix = self._p.computeViewMatrixFromYawPitchRoll( cameraTargetPosition=base_pos, @@ -199,7 +201,12 @@ class KukaGymEnv(gym.Env): (_, _, px, _, _) = self._p.getCameraImage( width=RENDER_WIDTH, height=RENDER_HEIGHT, viewMatrix=view_matrix, projectionMatrix=proj_matrix, renderer=self._p.ER_BULLET_HARDWARE_OPENGL) - rgb_array = np.array(px) + #renderer=self._p.ER_TINY_RENDERER) + + + rgb_array = np.array(px, dtype=np.uint8) + rgb_array = np.reshape(rgb_array, (RENDER_WIDTH, RENDER_HEIGHT, 4)) + rgb_array = rgb_array[:, :, :3] return rgb_array @@ -208,18 +215,18 @@ class KukaGymEnv(gym.Env): #print (self._kuka.endEffectorPos[2]) state = p.getLinkState(self._kuka.kukaUid,self._kuka.kukaEndEffectorIndex) actualEndEffectorPos = state[0] - + #print("self._envStepCounter") #print(self._envStepCounter) if (self.terminated or self._envStepCounter>self._maxSteps): self._observation = self.getExtendedObservation() return True - maxDist = 0.005 + maxDist = 0.005 closestPoints = p.getClosestPoints(self._kuka.trayUid, self._kuka.kukaUid,maxDist) - + if (len(closestPoints)):#(actualEndEffectorPos[2] <= -0.43): self.terminated = 1 - + #print("terminating, closing gripper, attempting grasp") #start grasp and terminate fingerAngle = 0.3 @@ -230,7 +237,7 @@ class KukaGymEnv(gym.Env): fingerAngle = fingerAngle-(0.3/100.) if (fingerAngle<0): fingerAngle=0 - + for i in range (1000): graspAction = [0,0,0.001,0,fingerAngle] self._kuka.applyAction(graspAction) @@ -245,19 +252,19 @@ class KukaGymEnv(gym.Env): if (actualEndEffectorPos[2]>0.5): break - + self._observation = self.getExtendedObservation() return True return False - + def _reward(self): - + #rewards is height of target object blockPos,blockOrn=p.getBasePositionAndOrientation(self.blockUid) - closestPoints = p.getClosestPoints(self.blockUid,self._kuka.kukaUid,1000, -1, self._kuka.kukaEndEffectorIndex) + closestPoints = p.getClosestPoints(self.blockUid,self._kuka.kukaUid,1000, -1, self._kuka.kukaEndEffectorIndex) reward = -1000 - + numPt = len(closestPoints) #print(numPt) if (numPt>0): @@ -276,10 +283,8 @@ class KukaGymEnv(gym.Env): #print(reward) return reward - def reset(self): - """Resets the state of the environment and returns an initial observation. - - Returns: observation (object): the initial observation of the - space. - """ - return self._reset() \ No newline at end of file + if parse_version(gym.__version__)>=parse_version('0.9.6'): + render = _render + reset = _reset + seed = _seed + step = _step diff --git a/examples/pybullet/gym/pybullet_envs/bullet/kuka_diverse_object_gym_env.py b/examples/pybullet/gym/pybullet_envs/bullet/kuka_diverse_object_gym_env.py index b49e0d006..46ac77e73 100644 --- a/examples/pybullet/gym/pybullet_envs/bullet/kuka_diverse_object_gym_env.py +++ b/examples/pybullet/gym/pybullet_envs/bullet/kuka_diverse_object_gym_env.py @@ -10,7 +10,8 @@ import pybullet_data import pdb import distutils.dir_util import glob - +from pkg_resources import parse_version +import gym class KukaDiverseObjectEnv(KukaGymEnv): """Class for Kuka environment with diverse objects. @@ -323,4 +324,10 @@ class KukaDiverseObjectEnv(KukaGymEnv): selected_objects_filenames = [] for object_index in selected_objects: selected_objects_filenames += [found_object_directories[object_index]] - return selected_objects_filenames \ No newline at end of file + return selected_objects_filenames + + if parse_version(gym.__version__)>=parse_version('0.9.6'): + + reset = _reset + + step = _step \ No newline at end of file diff --git a/examples/pybullet/gym/pybullet_envs/bullet/minitaur_duck_gym_env.py b/examples/pybullet/gym/pybullet_envs/bullet/minitaur_duck_gym_env.py index 44b41dd7d..b5c3375b4 100644 --- a/examples/pybullet/gym/pybullet_envs/bullet/minitaur_duck_gym_env.py +++ b/examples/pybullet/gym/pybullet_envs/bullet/minitaur_duck_gym_env.py @@ -385,3 +385,8 @@ class MinitaurBulletDuckEnv(gym.Env): scale=self._observation_noise_stdev, size=observation.shape) * self.minitaur.GetObservationUpperBound()) return observation + + render = _render + reset = _reset + seed = _seed + step = _step diff --git a/examples/pybullet/gym/pybullet_envs/bullet/minitaur_gym_env.py b/examples/pybullet/gym/pybullet_envs/bullet/minitaur_gym_env.py index 38ec631a5..d82774d14 100644 --- a/examples/pybullet/gym/pybullet_envs/bullet/minitaur_gym_env.py +++ b/examples/pybullet/gym/pybullet_envs/bullet/minitaur_gym_env.py @@ -20,6 +20,7 @@ from . import minitaur import os import pybullet_data from . import minitaur_env_randomizer +from pkg_resources import parse_version NUM_SUBSTEPS = 5 NUM_MOTORS = 8 @@ -376,3 +377,9 @@ class MinitaurBulletEnv(gym.Env): scale=self._observation_noise_stdev, size=observation.shape) * self.minitaur.GetObservationUpperBound()) return observation + + if parse_version(gym.__version__)>=parse_version('0.9.6'): + render = _render + reset = _reset + seed = _seed + step = _step diff --git a/examples/pybullet/gym/pybullet_envs/bullet/racecarGymEnv.py b/examples/pybullet/gym/pybullet_envs/bullet/racecarGymEnv.py index 7537d54c1..c0e275774 100644 --- a/examples/pybullet/gym/pybullet_envs/bullet/racecarGymEnv.py +++ b/examples/pybullet/gym/pybullet_envs/bullet/racecarGymEnv.py @@ -14,6 +14,7 @@ from . import racecar import random from . import bullet_client import pybullet_data +from pkg_resources import parse_version RENDER_HEIGHT = 720 RENDER_WIDTH = 960 @@ -50,8 +51,8 @@ class RacecarGymEnv(gym.Env): #self.reset() observationDim = 2 #len(self.getExtendedObservation()) #print("observationDim") - #print(observationDim) - # observation_high = np.array([np.finfo(np.float32).max] * observationDim) + #print(observationDim) + # observation_high = np.array([np.finfo(np.float32).max] * observationDim) observation_high = np.ones(observationDim) * 1000 #np.inf if (isDiscrete): self.action_space = spaces.Discrete(9) @@ -59,7 +60,7 @@ class RacecarGymEnv(gym.Env): action_dim = 2 self._action_bound = 1 action_high = np.array([self._action_bound] * action_dim) - self.action_space = spaces.Box(-action_high, action_high) + self.action_space = spaces.Box(-action_high, action_high) self.observation_space = spaces.Box(-observation_high, observation_high) self.viewer = None @@ -74,14 +75,14 @@ class RacecarGymEnv(gym.Env): # pos,orn = self._p.getBasePositionAndOrientation(i) # newpos = [pos[0],pos[1],pos[2]-0.1] # self._p.resetBasePositionAndOrientation(i,newpos,orn) - + dist = 5 +2.*random.random() ang = 2.*3.1415925438*random.random() - + ballx = dist * math.sin(ang) bally = dist * math.cos(ang) ballz = 1 - + self._ballUniqueId = self._p.loadURDF(os.path.join(self._urdfRoot,"sphere2.urdf"),[ballx,bally,ballz]) self._p.setGravity(0,0,-10) self._racecar = racecar.Racecar(self._p,urdfRootPath=self._urdfRoot, timeStep=self._timeStep) @@ -101,18 +102,18 @@ class RacecarGymEnv(gym.Env): def getExtendedObservation(self): self._observation = [] #self._racecar.getObservation() carpos,carorn = self._p.getBasePositionAndOrientation(self._racecar.racecarUniqueId) - ballpos,ballorn = self._p.getBasePositionAndOrientation(self._ballUniqueId) + ballpos,ballorn = self._p.getBasePositionAndOrientation(self._ballUniqueId) invCarPos,invCarOrn = self._p.invertTransform(carpos,carorn) ballPosInCar,ballOrnInCar = self._p.multiplyTransforms(invCarPos,invCarOrn,ballpos,ballorn) - + self._observation.extend([ballPosInCar[0],ballPosInCar[1]]) return self._observation - + def _step(self, action): if (self._renders): basePos,orn = self._p.getBasePositionAndOrientation(self._racecar.racecarUniqueId) #self._p.resetDebugVisualizerCamera(1, 30, -40, basePos) - + if (self._isDiscrete): fwd = [-1,-1,-1,0,0,0,1,1,1] steerings = [-0.6,0,0.6,-0.6,0,0.6,-0.6,0,0.6] @@ -128,14 +129,14 @@ class RacecarGymEnv(gym.Env): if self._renders: time.sleep(self._timeStep) self._observation = self.getExtendedObservation() - + if self._termination(): break self._envStepCounter += 1 reward = self._reward() done = self._termination() #print("len=%r" % len(self._observation)) - + return np.array(self._observation), reward, done, {} def _render(self, mode='human', close=False): @@ -159,13 +160,13 @@ class RacecarGymEnv(gym.Env): rgb_array = rgb_array[:, :, :3] return rgb_array - + def _termination(self): return self._envStepCounter>1000 - + def _reward(self): - closestPoints = self._p.getClosestPoints(self._racecar.racecarUniqueId,self._ballUniqueId,10000) - + closestPoints = self._p.getClosestPoints(self._racecar.racecarUniqueId,self._ballUniqueId,10000) + numPt = len(closestPoints) reward=-1000 #print(numPt) @@ -174,3 +175,9 @@ class RacecarGymEnv(gym.Env): reward = -closestPoints[0][8] #print(reward) return reward + + if parse_version(gym.__version__)>=parse_version('0.9.6'): + render = _render + reset = _reset + seed = _seed + step = _step diff --git a/examples/pybullet/gym/pybullet_envs/bullet/racecarZEDGymEnv.py b/examples/pybullet/gym/pybullet_envs/bullet/racecarZEDGymEnv.py index d9546de8c..9185b2373 100644 --- a/examples/pybullet/gym/pybullet_envs/bullet/racecarZEDGymEnv.py +++ b/examples/pybullet/gym/pybullet_envs/bullet/racecarZEDGymEnv.py @@ -14,6 +14,7 @@ from . import bullet_client from . import racecar import random import pybullet_data +from pkg_resources import parse_version RENDER_HEIGHT = 720 RENDER_WIDTH = 960 @@ -40,7 +41,7 @@ class RacecarZEDGymEnv(gym.Env): self._renders = renders self._width = 100 self._height = 10 - + self._isDiscrete = isDiscrete if self._renders: self._p = bullet_client.BulletClient( @@ -53,8 +54,8 @@ class RacecarZEDGymEnv(gym.Env): observationDim = len(self.getExtendedObservation()) #print("observationDim") #print(observationDim) - - observation_high = np.array([np.finfo(np.float32).max] * observationDim) + + observation_high = np.array([np.finfo(np.float32).max] * observationDim) if (isDiscrete): self.action_space = spaces.Discrete(9) else: @@ -77,14 +78,14 @@ class RacecarZEDGymEnv(gym.Env): pos,orn = self._p.getBasePositionAndOrientation(i) newpos = [pos[0],pos[1],pos[2]+0.1] self._p.resetBasePositionAndOrientation(i,newpos,orn) - + dist = 5 +2.*random.random() ang = 2.*3.1415925438*random.random() - + ballx = dist * math.sin(ang) bally = dist * math.cos(ang) ballz = 1 - + self._ballUniqueId = self._p.loadURDF(os.path.join(self._urdfRoot,"sphere2red.urdf"),[ballx,bally,ballz]) self._p.setGravity(0,0,-10) self._racecar = racecar.Racecar(self._p,urdfRootPath=self._urdfRoot, timeStep=self._timeStep) @@ -104,13 +105,13 @@ class RacecarZEDGymEnv(gym.Env): def getExtendedObservation(self): carpos,carorn = self._p.getBasePositionAndOrientation(self._racecar.racecarUniqueId) carmat = self._p.getMatrixFromQuaternion(carorn) - ballpos,ballorn = self._p.getBasePositionAndOrientation(self._ballUniqueId) + ballpos,ballorn = self._p.getBasePositionAndOrientation(self._ballUniqueId) invCarPos,invCarOrn = self._p.invertTransform(carpos,carorn) ballPosInCar,ballOrnInCar = self._p.multiplyTransforms(invCarPos,invCarOrn,ballpos,ballorn) dist0 = 0.3 dist1 = 1. eyePos = [carpos[0]+dist0*carmat[0],carpos[1]+dist0*carmat[3],carpos[2]+dist0*carmat[6]+0.3] - targetPos = [carpos[0]+dist1*carmat[0],carpos[1]+dist1*carmat[3],carpos[2]+dist1*carmat[6]+0.3] + targetPos = [carpos[0]+dist1*carmat[0],carpos[1]+dist1*carmat[3],carpos[2]+dist1*carmat[6]+0.3] up = [carmat[2],carmat[5],carmat[8]] viewMat = self._p.computeViewMatrix(eyePos,targetPos,up) #viewMat = self._p.computeViewMatrixFromYawPitchRoll(carpos,1,0,0,0,2) @@ -122,12 +123,12 @@ class RacecarZEDGymEnv(gym.Env): np_img_arr = np.reshape(rgb, (self._height, self._width, 4)) self._observation = np_img_arr return self._observation - + def _step(self, action): if (self._renders): basePos,orn = self._p.getBasePositionAndOrientation(self._racecar.racecarUniqueId) #self._p.resetDebugVisualizerCamera(1, 30, -40, basePos) - + if (self._isDiscrete): fwd = [-1,-1,-1,0,0,0,1,1,1] steerings = [-0.6,0,0.6,-0.6,0,0.6,-0.6,0,0.6] @@ -143,14 +144,14 @@ class RacecarZEDGymEnv(gym.Env): if self._renders: time.sleep(self._timeStep) self._observation = self.getExtendedObservation() - + if self._termination(): break self._envStepCounter += 1 reward = self._reward() done = self._termination() #print("len=%r" % len(self._observation)) - + return np.array(self._observation), reward, done, {} def _render(self, mode='human', close=False): @@ -177,10 +178,10 @@ class RacecarZEDGymEnv(gym.Env): def _termination(self): return self._envStepCounter>1000 - + def _reward(self): - closestPoints = self._p.getClosestPoints(self._racecar.racecarUniqueId,self._ballUniqueId,10000) - + closestPoints = self._p.getClosestPoints(self._racecar.racecarUniqueId,self._ballUniqueId,10000) + numPt = len(closestPoints) reward=-1000 #print(numPt) @@ -189,3 +190,9 @@ class RacecarZEDGymEnv(gym.Env): reward = -closestPoints[0][8] #print(reward) return reward + + if parse_version(gym.__version__)>=parse_version('0.9.6'): + render = _render + reset = _reset + seed = _seed + step = _step diff --git a/examples/pybullet/gym/pybullet_envs/bullet/simpleHumanoidGymEnv.py b/examples/pybullet/gym/pybullet_envs/bullet/simpleHumanoidGymEnv.py index c632e7f31..293e8c0c8 100644 --- a/examples/pybullet/gym/pybullet_envs/bullet/simpleHumanoidGymEnv.py +++ b/examples/pybullet/gym/pybullet_envs/bullet/simpleHumanoidGymEnv.py @@ -12,7 +12,7 @@ import time import pybullet as p from . import simpleHumanoid import random - +from pkg_resources import parse_version import pybullet_data @@ -46,8 +46,8 @@ class SimpleHumanoidGymEnv(gym.Env): observationDim = len(self.getExtendedObservation()) #print("observationDim") #print(observationDim) - - observation_high = np.array([np.finfo(np.float32).max] * observationDim) + + observation_high = np.array([np.finfo(np.float32).max] * observationDim) self.action_space = spaces.Discrete(9) self.observation_space = spaces.Box(-observation_high, observation_high) self.viewer = None @@ -57,14 +57,14 @@ class SimpleHumanoidGymEnv(gym.Env): #p.setPhysicsEngineParameter(numSolverIterations=300) p.setTimeStep(self._timeStep) p.loadURDF(os.path.join(self._urdfRoot,"plane.urdf")) - + dist = 5 +2.*random.random() ang = 2.*3.1415925438*random.random() - + ballx = dist * math.sin(ang) bally = dist * math.cos(ang) ballz = 1 - + p.setGravity(0,0,-10) self._humanoid = simpleHumanoid.SimpleHumanoid(urdfRootPath=self._urdfRoot, timeStep=self._timeStep) self._envStepCounter = 0 @@ -82,7 +82,7 @@ class SimpleHumanoidGymEnv(gym.Env): def getExtendedObservation(self): self._observation = self._humanoid.getObservation() return self._observation - + def _step(self, action): self._humanoid.applyAction(action) for i in range(self._actionRepeat): @@ -96,7 +96,7 @@ class SimpleHumanoidGymEnv(gym.Env): reward = self._reward() done = self._termination() #print("len=%r" % len(self._observation)) - + return np.array(self._observation), reward, done, {} def _render(self, mode='human', close=False): @@ -104,8 +104,14 @@ class SimpleHumanoidGymEnv(gym.Env): def _termination(self): return self._envStepCounter>1000 - + def _reward(self): reward=self._humanoid.distance print(reward) return reward + + if parse_version(gym.__version__)>=parse_version('0.9.6'): + render = _render + reset = _reset + seed = _seed + step = _step diff --git a/examples/pybullet/gym/pybullet_envs/env_bases.py b/examples/pybullet/gym/pybullet_envs/env_bases.py index 5d49c22c3..c03f0e608 100644 --- a/examples/pybullet/gym/pybullet_envs/env_bases.py +++ b/examples/pybullet/gym/pybullet_envs/env_bases.py @@ -1,7 +1,7 @@ import gym, gym.spaces, gym.utils, gym.utils.seeding import numpy as np import pybullet as p - +from pkg_resources import parse_version class MJCFBaseBulletEnv(gym.Env): """ @@ -43,7 +43,7 @@ class MJCFBaseBulletEnv(gym.Env): conInfo = p.getConnectionInfo() if (conInfo['isConnected']): self.ownsPhysicsClient = False - + self.physicsClientId = 0 else: self.ownsPhysicsClient = True @@ -75,12 +75,12 @@ class MJCFBaseBulletEnv(gym.Env): self.isRender = True if mode != "rgb_array": return np.array([]) - + base_pos=[0,0,0] if (hasattr(self,'robot')): if (hasattr(self.robot,'body_xyz')): base_pos = self.robot.body_xyz - + view_matrix = p.computeViewMatrixFromYawPitchRoll( cameraTargetPosition=base_pos, distance=self._cam_dist, @@ -100,6 +100,7 @@ class MJCFBaseBulletEnv(gym.Env): rgb_array = rgb_array[:, :, :3] return rgb_array + def _close(self): if (self.ownsPhysicsClient): if (self.physicsClientId>=0): @@ -109,6 +110,18 @@ class MJCFBaseBulletEnv(gym.Env): def HUD(self, state, a, done): pass + # backwards compatibility for gym >= v0.9.x + # for extension of this class. + def step(self, *args, **kwargs): + return self._step(*args, **kwargs) + + if parse_version(gym.__version__)>=parse_version('0.9.6'): + close = _close + render = _render + reset = _reset + seed = _seed + + class Camera: def __init__(self): pass diff --git a/examples/pybullet/gym/pybullet_envs/examples/minitaur_gym_env_example.py b/examples/pybullet/gym/pybullet_envs/examples/minitaur_gym_env_example.py index 2aebfd79a..8057b2dc5 100644 --- a/examples/pybullet/gym/pybullet_envs/examples/minitaur_gym_env_example.py +++ b/examples/pybullet/gym/pybullet_envs/examples/minitaur_gym_env_example.py @@ -1,6 +1,12 @@ r"""An example to run of the minitaur gym environment with sine gaits. """ +import os +import inspect +currentdir = os.path.dirname(os.path.abspath(inspect.getfile(inspect.currentframe()))) +parentdir = os.path.dirname(os.path.dirname(currentdir)) +os.sys.path.insert(0,parentdir) + import math import numpy as np from pybullet_envs.bullet import minitaur_gym_env diff --git a/examples/pybullet/gym/pybullet_envs/examples/testBike.py b/examples/pybullet/gym/pybullet_envs/examples/testBike.py index 3d6e89c91..b62a8668e 100644 --- a/examples/pybullet/gym/pybullet_envs/examples/testBike.py +++ b/examples/pybullet/gym/pybullet_envs/examples/testBike.py @@ -1,3 +1,9 @@ +import os +import inspect +currentdir = os.path.dirname(os.path.abspath(inspect.getfile(inspect.currentframe()))) +parentdir = os.path.dirname(os.path.dirname(currentdir)) +os.sys.path.insert(0,parentdir) + import pybullet as p import math import time diff --git a/src/Bullet3Collision/premake4.lua b/src/Bullet3Collision/premake4.lua index 0b47f8ea5..bd0da541e 100644 --- a/src/Bullet3Collision/premake4.lua +++ b/src/Bullet3Collision/premake4.lua @@ -6,6 +6,9 @@ includedirs {".."} + if os.is("Linux") then + buildoptions{"-fPIC"} + end files { "**.cpp", diff --git a/src/Bullet3Common/premake4.lua b/src/Bullet3Common/premake4.lua index 1331c6327..f03573ec2 100644 --- a/src/Bullet3Common/premake4.lua +++ b/src/Bullet3Common/premake4.lua @@ -4,6 +4,10 @@ kind "StaticLib" + if os.is("Linux") then + buildoptions{"-fPIC"} + end + includedirs {".."} files { diff --git a/src/Bullet3Dynamics/premake4.lua b/src/Bullet3Dynamics/premake4.lua index 669336a6a..e05b2cd19 100644 --- a/src/Bullet3Dynamics/premake4.lua +++ b/src/Bullet3Dynamics/premake4.lua @@ -8,6 +8,9 @@ ".." } + if os.is("Linux") then + buildoptions{"-fPIC"} + end files { "**.cpp", diff --git a/src/Bullet3Geometry/premake4.lua b/src/Bullet3Geometry/premake4.lua index 1a230f8c0..cce93f7ee 100644 --- a/src/Bullet3Geometry/premake4.lua +++ b/src/Bullet3Geometry/premake4.lua @@ -6,6 +6,9 @@ includedirs {".."} + if os.is("Linux") then + buildoptions{"-fPIC"} + end files { "**.cpp", diff --git a/src/Bullet3OpenCL/premake4.lua b/src/Bullet3OpenCL/premake4.lua index 55a861363..ee35fdb52 100644 --- a/src/Bullet3OpenCL/premake4.lua +++ b/src/Bullet3OpenCL/premake4.lua @@ -9,6 +9,9 @@ function createProject(vendor) kind "StaticLib" + if os.is("Linux") then + buildoptions{"-fPIC"} + end includedirs { ".",".." diff --git a/src/Bullet3Serialize/Bullet2FileLoader/premake4.lua b/src/Bullet3Serialize/Bullet2FileLoader/premake4.lua index ec2f0a51a..b9ee301be 100644 --- a/src/Bullet3Serialize/Bullet2FileLoader/premake4.lua +++ b/src/Bullet3Serialize/Bullet2FileLoader/premake4.lua @@ -5,6 +5,10 @@ includedirs { "../../../src" } + + if os.is("Linux") then + buildoptions{"-fPIC"} + end files { "**.cpp", diff --git a/src/BulletCollision/premake4.lua b/src/BulletCollision/premake4.lua index 70407771d..6297bf3df 100644 --- a/src/BulletCollision/premake4.lua +++ b/src/BulletCollision/premake4.lua @@ -1,6 +1,9 @@ project "BulletCollision" kind "StaticLib" + if os.is("Linux") then + buildoptions{"-fPIC"} + end includedirs { "..", } diff --git a/src/BulletDynamics/premake4.lua b/src/BulletDynamics/premake4.lua index 32414dce3..fdaf0513d 100644 --- a/src/BulletDynamics/premake4.lua +++ b/src/BulletDynamics/premake4.lua @@ -3,6 +3,9 @@ includedirs { "..", } + if os.is("Linux") then + buildoptions{"-fPIC"} + end files { "Dynamics/*.cpp", "Dynamics/*.h", diff --git a/src/BulletInverseDynamics/premake4.lua b/src/BulletInverseDynamics/premake4.lua index 774e037b3..fdd176b09 100644 --- a/src/BulletInverseDynamics/premake4.lua +++ b/src/BulletInverseDynamics/premake4.lua @@ -1,6 +1,9 @@ project "BulletInverseDynamics" kind "StaticLib" + if os.is("Linux") then + buildoptions{"-fPIC"} + end includedirs { "..", } diff --git a/src/BulletSoftBody/premake4.lua b/src/BulletSoftBody/premake4.lua index 57a575fb1..a75e33619 100644 --- a/src/BulletSoftBody/premake4.lua +++ b/src/BulletSoftBody/premake4.lua @@ -5,6 +5,9 @@ includedirs { "..", } + if os.is("Linux") then + buildoptions{"-fPIC"} + end files { "**.cpp", "**.h" diff --git a/src/LinearMath/premake4.lua b/src/LinearMath/premake4.lua index 524e2c316..5f0fda6bf 100644 --- a/src/LinearMath/premake4.lua +++ b/src/LinearMath/premake4.lua @@ -1,6 +1,9 @@ project "LinearMath" kind "StaticLib" + if os.is("Linux") then + buildoptions{"-fPIC"} + end includedirs { "..", } diff --git a/test/SharedMemory/CMakeLists.txt b/test/SharedMemory/CMakeLists.txt index cba4b587c..e0798123f 100644 --- a/test/SharedMemory/CMakeLists.txt +++ b/test/SharedMemory/CMakeLists.txt @@ -11,13 +11,13 @@ INCLUDE_DIRECTORIES( #ADD_DEFINITIONS(-DGTEST_HAS_PTHREAD=1) ADD_DEFINITIONS(-DPHYSICS_LOOP_BACK -DPHYSICS_SERVER_DIRECT -DENABLE_GTEST -D_VARIADIC_MAX=10) +IF (USE_SOFT_BODY_MULTI_BODY_DYNAMICS_WORLD) + LINK_LIBRARIES(BulletSoftBody) +ENDIF() LINK_LIBRARIES( BulletInverseDynamicsUtils BulletInverseDynamics BulletFileLoader BulletWorldImporter Bullet3Common BulletDynamics BulletCollision LinearMath gtest BussIK ) -IF (USE_SOFT_BODY_MULTI_BODY_DYNAMICS_WORLD) - LINK_LIBRARIES(BulletSoftBody) -ENDIF() IF (NOT WIN32) LINK_LIBRARIES( pthread ) diff --git a/test/SharedMemory/premake4.lua b/test/SharedMemory/premake4.lua index 31f19ada2..57f2adf49 100644 --- a/test/SharedMemory/premake4.lua +++ b/test/SharedMemory/premake4.lua @@ -164,6 +164,9 @@ project ("Test_PhysicsServerLoopBack") "BussIK", "LinearMath" } + if os.is("Linux") then + links{"dl"} + end files { "test.c", @@ -250,6 +253,9 @@ end "BussIK", "LinearMath" } + if os.is("Linux") then + links{"dl"} + end files { "test.c", diff --git a/test/gtest-1.7.0/premake4.lua b/test/gtest-1.7.0/premake4.lua index 79aade120..1d96f8315 100644 --- a/test/gtest-1.7.0/premake4.lua +++ b/test/gtest-1.7.0/premake4.lua @@ -16,3 +16,7 @@ includedirs { ".","include" } + if os.is("Linux") then + buildoptions{"-fPIC"} + end + \ No newline at end of file From ef0ae4935b90210bc91637c1bcf9d2c2906d67b4 Mon Sep 17 00:00:00 2001 From: yunfeibai Date: Thu, 15 Feb 2018 18:37:23 -0800 Subject: [PATCH 6/7] Minor change. --- examples/pybullet/examples/projective_texture.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/pybullet/examples/projective_texture.py b/examples/pybullet/examples/projective_texture.py index e9574e4f7..d339bf5a0 100644 --- a/examples/pybullet/examples/projective_texture.py +++ b/examples/pybullet/examples/projective_texture.py @@ -12,7 +12,7 @@ cubeStartPos = [0,0,1] cubeStartOrientation = p.getQuaternionFromEuler([0,0,0]) boxId = p.loadURDF("cube.urdf",cubeStartPos, cubeStartOrientation) cubePos, cubeOrn = p.getBasePositionAndOrientation(boxId) -textureId = p.loadTexture("checker_blue_test.png") +textureId = p.loadTexture("checker_blue.png") p.changeVisualShape(objectUniqueId=0, linkIndex=-1, textureUniqueId=textureId) p.changeVisualShape(objectUniqueId=1, linkIndex=-1, textureUniqueId=textureId) From 5494aa72fafc4a6ec9cb458a2b5f5621e32b9ddc Mon Sep 17 00:00:00 2001 From: yunfeibai Date: Tue, 20 Feb 2018 18:32:09 -0800 Subject: [PATCH 7/7] Set the perspective projection for projective texture mapping. Change the demo to two bears with the same view projection matrix for the camera and the projector. --- data/teddy_large.urdf | 32 +++++++++++++++++ data/teddy_vhacd.urdf | 1 - .../OpenGLWindow/GLInstancingRenderer.cpp | 31 ++++------------ .../projectiveTextureInstancingVS.glsl | 6 ++-- .../Shaders/projectiveTextureInstancingVS.h | 6 ++-- .../pybullet/examples/projective_texture.py | 35 +++++-------------- 6 files changed, 53 insertions(+), 58 deletions(-) create mode 100644 data/teddy_large.urdf diff --git a/data/teddy_large.urdf b/data/teddy_large.urdf new file mode 100644 index 000000000..3be8c547b --- /dev/null +++ b/data/teddy_large.urdf @@ -0,0 +1,32 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/data/teddy_vhacd.urdf b/data/teddy_vhacd.urdf index dd6cb2c40..ca99c9851 100644 --- a/data/teddy_vhacd.urdf +++ b/data/teddy_vhacd.urdf @@ -29,4 +29,3 @@ - diff --git a/examples/OpenGLWindow/GLInstancingRenderer.cpp b/examples/OpenGLWindow/GLInstancingRenderer.cpp index 1ff4b8fdf..cf7553172 100644 --- a/examples/OpenGLWindow/GLInstancingRenderer.cpp +++ b/examples/OpenGLWindow/GLInstancingRenderer.cpp @@ -17,7 +17,6 @@ subject to the following restrictions: ///todo: make this configurable in the gui bool useShadowMap = true;// true;//false;//true; -float projectiveTextureViewSize = 10; bool useProjectiveTexture = false; int shadowMapWidth= 4096; int shadowMapHeight= 4096; @@ -228,9 +227,6 @@ struct InternalDataRenderer : public GLInstanceRendererInternalData b3Vector3 m_lightPos; b3Vector3 m_lightSpecularIntensity; - - b3Vector3 m_projectorPos; - b3Vector3 m_projectorDir; GLuint m_defaultTexturehandle; b3AlignedObjectArray m_textureHandles; @@ -252,8 +248,6 @@ struct InternalDataRenderer : public GLInstanceRendererInternalData { m_lightPos=b3MakeVector3(-50,30,40); m_lightSpecularIntensity.setValue(1,1,1); - - m_projectorPos=b3MakeVector3(-50,30,40); //clear to zero to make it obvious if the matrix is used uninitialized for (int i=0;i<16;i++) @@ -336,7 +330,7 @@ static GLint projectiveTexture_cameraPositionIn = 0; static GLint projectiveTexture_materialShininessIn = 0; static GLint projectiveTexture_ProjectionMatrix=0; -static GLint projectiveTexture_DepthBiasModelViewMatrix=0; +static GLint projectiveTexture_TextureMVP=0; static GLint projectiveTexture_uniform_texture_diffuse = 0; static GLint projectiveTexture_shadowMap = 0; @@ -1241,7 +1235,7 @@ void GLInstancingRenderer::InitShaders() projectiveTexture_materialSpecularColor = glGetUniformLocation(projectiveTextureInstancingShader, "materialSpecularColorIn"); projectiveTexture_MVP = glGetUniformLocation(projectiveTextureInstancingShader, "MVP"); projectiveTexture_ProjectionMatrix = glGetUniformLocation(projectiveTextureInstancingShader, "ProjectionMatrix"); - projectiveTexture_DepthBiasModelViewMatrix = glGetUniformLocation(projectiveTextureInstancingShader, "DepthBiasModelViewProjectionMatrix"); + projectiveTexture_TextureMVP = glGetUniformLocation(projectiveTextureInstancingShader, "TextureMVP"); projectiveTexture_uniform_texture_diffuse = glGetUniformLocation(projectiveTextureInstancingShader, "Diffuse"); projectiveTexture_shadowMap = glGetUniformLocation(projectiveTextureInstancingShader,"shadowMap"); projectiveTexture_lightPosIn = glGetUniformLocation(projectiveTextureInstancingShader,"lightPosIn"); @@ -2168,17 +2162,9 @@ void GLInstancingRenderer::renderSceneInternal(int orgRenderMode) GLfloat depthBiasMVP[4][4]; b3Matrix4x4Mul(biasMatrix,depthMVP,depthBiasMVP); - // Compute projection matrix for texture projector - float textureViewMatrix[4][4]; - b3Vector3 projectorDir = m_data->m_projectorDir.normalize(); - float projectorDist = 0; - b3CreateLookAt(m_data->m_projectorPos,center,up, &textureViewMatrix[0][0]); - GLfloat textureModelMatrix[4][4]; - b3CreateDiagonalMatrix(1.f, textureModelMatrix); - b3Matrix4x4Mul(textureViewMatrix, textureModelMatrix, textureModelViewMatrix); - b3CreateOrtho(-projectiveTextureViewSize,projectiveTextureViewSize,-projectiveTextureViewSize,projectiveTextureViewSize,1,300,textureProjectionMatrix); - GLfloat textureMVP[4][4]; - b3Matrix4x4Mul(textureProjectionMatrix, textureModelViewMatrix, textureMVP); + // TODO: Expose the projective texture matrix setup. Temporarily set it to be the same as camera view projection matrix. + GLfloat textureMVP[16]; + b3Matrix4x4Mul16(m_data->m_projectionMatrix,m_data->m_viewMatrix,textureMVP); //float m_frustumZNear=0.1; //float m_frustumZFar=100.f; @@ -2576,10 +2562,7 @@ b3Assert(glGetError() ==GL_NO_ERROR); glUniform3f(projectiveTexture_cameraPositionIn,camPos[0],camPos[1],camPos[2]); glUniform1f(projectiveTexture_materialShininessIn,gfxObj->m_materialShinyNess); - glUniformMatrix4fv(projectiveTexture_DepthBiasModelViewMatrix, 1, false, &textureMVP[0][0]); - glActiveTexture(GL_TEXTURE1); - glBindTexture(GL_TEXTURE_2D, m_data->m_shadowTexture); - glUniform1i(projectiveTexture_shadowMap,1); + glUniformMatrix4fv(projectiveTexture_TextureMVP, 1, false, &textureMVP[0]); //sort transparent objects if ( gfxObj->m_flags&eGfxTransparency) @@ -2600,8 +2583,6 @@ b3Assert(glGetError() ==GL_NO_ERROR); glDisable (GL_BLEND); glDepthMask(true); } - glActiveTexture(GL_TEXTURE1); - glBindTexture(GL_TEXTURE_2D,0); glActiveTexture(GL_TEXTURE0); glBindTexture(GL_TEXTURE_2D,0); diff --git a/examples/OpenGLWindow/Shaders/projectiveTextureInstancingVS.glsl b/examples/OpenGLWindow/Shaders/projectiveTextureInstancingVS.glsl index 01d73c2d4..c3417ed55 100644 --- a/examples/OpenGLWindow/Shaders/projectiveTextureInstancingVS.glsl +++ b/examples/OpenGLWindow/Shaders/projectiveTextureInstancingVS.glsl @@ -11,7 +11,7 @@ layout (location = 5) in vec4 instance_color; layout (location = 6) in vec3 instance_scale; -uniform mat4 DepthBiasModelViewProjectionMatrix; +uniform mat4 TextureMVP; uniform mat4 MVP; uniform vec3 lightPosIn; uniform vec3 cameraPositionIn; @@ -93,7 +93,7 @@ void main(void) gl_Position = vertexLoc; fragment.color = instance_color; - vec4 projcoords = DepthBiasModelViewProjectionMatrix * vec4((instance_position+localcoord).xyz,1); - vert.texcoord = projcoords.xy; + vec4 projcoords = TextureMVP * vec4((instance_position+localcoord).xyz,1); + vert.texcoord = projcoords.xy/projcoords.z; } diff --git a/examples/OpenGLWindow/Shaders/projectiveTextureInstancingVS.h b/examples/OpenGLWindow/Shaders/projectiveTextureInstancingVS.h index 3f929e44b..b1de7880d 100644 --- a/examples/OpenGLWindow/Shaders/projectiveTextureInstancingVS.h +++ b/examples/OpenGLWindow/Shaders/projectiveTextureInstancingVS.h @@ -9,7 +9,7 @@ static const char* projectiveTextureInstancingVertexShader= \ "layout (location = 4) in vec3 vertexnormal;\n" "layout (location = 5) in vec4 instance_color;\n" "layout (location = 6) in vec3 instance_scale;\n" -"uniform mat4 DepthBiasModelViewProjectionMatrix;\n" +"uniform mat4 TextureMVP;\n" "uniform mat4 MVP;\n" "uniform vec3 lightPosIn;\n" "uniform vec3 cameraPositionIn;\n" @@ -80,7 +80,7 @@ static const char* projectiveTextureInstancingVertexShader= \ " vec4 vertexLoc = MVP* vec4((instance_position+localcoord).xyz,1);\n" " gl_Position = vertexLoc;\n" " fragment.color = instance_color;\n" -" vec4 projcoords = DepthBiasModelViewProjectionMatrix * vec4((instance_position+localcoord).xyz,1);\n" -" vert.texcoord = projcoords.xy;\n" +" vec4 projcoords = TextureMVP * vec4((instance_position+localcoord).xyz,1);\n" +" vert.texcoord = projcoords.xy/projcoords.z;\n" "}\n" ; diff --git a/examples/pybullet/examples/projective_texture.py b/examples/pybullet/examples/projective_texture.py index d339bf5a0..576a7a095 100644 --- a/examples/pybullet/examples/projective_texture.py +++ b/examples/pybullet/examples/projective_texture.py @@ -6,34 +6,17 @@ import numpy as np physicsClient = p.connect(p.GUI) -p.setGravity(0,0,-10) -planeId = p.loadURDF("plane.urdf") -cubeStartPos = [0,0,1] -cubeStartOrientation = p.getQuaternionFromEuler([0,0,0]) -boxId = p.loadURDF("cube.urdf",cubeStartPos, cubeStartOrientation) -cubePos, cubeOrn = p.getBasePositionAndOrientation(boxId) -textureId = p.loadTexture("checker_blue.png") +p.setGravity(0,0,0) +bearStartPos1 = [-3.3,0,0] +bearStartOrientation1 = p.getQuaternionFromEuler([0,0,0]) +bearId1 = p.loadURDF("teddy_large.urdf", bearStartPos1, bearStartOrientation1) +bearStartPos2 = [0,0,0] +bearStartOrientation2 = p.getQuaternionFromEuler([0,0,0]) +bearId2 = p.loadURDF("teddy_large.urdf",bearStartPos2, bearStartOrientation2) +textureId = p.loadTexture("checker_grid.jpg") p.changeVisualShape(objectUniqueId=0, linkIndex=-1, textureUniqueId=textureId) p.changeVisualShape(objectUniqueId=1, linkIndex=-1, textureUniqueId=textureId) -fov = 70 -pixelWidth = 640 -pixelHeight = 512 -aspect = pixelWidth / pixelHeight; -nearPlane = 0.01 -farPlane = 100 -projectionMatrix = p.computeProjectionMatrixFOV(fov, aspect, nearPlane, farPlane); -viewMatrixLeft = p.computeViewMatrix(cameraEyePosition=[0.045, 3.0, 3.0], cameraTargetPosition=[0.045,0,0], cameraUpVector=[0,0,1]) -viewMatrixRight = p.computeViewMatrix(cameraEyePosition=[-0.045, 3.0, 3.0], cameraTargetPosition=[-0.045,0,0], cameraUpVector=[0,0,1]) -leftImage = p.getCameraImage(width=640,height=512,viewMatrix=viewMatrixLeft,projectionMatrix=projectionMatrix,renderer=p.ER_BULLET_HARDWARE_OPENGL) -rightImage = p.getCameraImage(width=640,height=512,viewMatrix=viewMatrixRight,projectionMatrix=projectionMatrix,renderer=p.ER_BULLET_HARDWARE_OPENGL) - -left_img = np.reshape(np.asarray(leftImage[2], dtype=np.float32), (512, 640, 4)) -left_img /= 255 -plt.imsave("left_image.png", left_img) -right_img = np.reshape(np.asarray(rightImage[2], dtype=np.float32), (512, 640, 4)) -right_img /= 255 -plt.imsave("right_image.png", right_img) useRealTimeSimulation = 1 @@ -42,7 +25,7 @@ if (useRealTimeSimulation): while 1: if (useRealTimeSimulation): - p.setGravity(0,0,-10) + p.setGravity(0,0,0) sleep(0.01) # Time in seconds. else: p.stepSimulation()