From b032a1fd604aceef9fca0677e149eca6571b386f Mon Sep 17 00:00:00 2001 From: YunfeiBai Date: Mon, 9 Apr 2018 17:15:42 -0700 Subject: [PATCH] Update the projective texture shader to solve the texture interpolation artifact. --- .../Shaders/projectiveTextureInstancingPS.glsl | 11 ++++------- .../Shaders/projectiveTextureInstancingPS.h | 9 ++++----- .../Shaders/projectiveTextureInstancingVS.glsl | 2 -- .../Shaders/projectiveTextureInstancingVS.h | 2 -- examples/pybullet/examples/projective_texture.py | 3 +-- 5 files changed, 9 insertions(+), 18 deletions(-) diff --git a/examples/OpenGLWindow/Shaders/projectiveTextureInstancingPS.glsl b/examples/OpenGLWindow/Shaders/projectiveTextureInstancingPS.glsl index 4e11e3762..e2da4b4aa 100644 --- a/examples/OpenGLWindow/Shaders/projectiveTextureInstancingPS.glsl +++ b/examples/OpenGLWindow/Shaders/projectiveTextureInstancingPS.glsl @@ -6,13 +6,9 @@ in Fragment vec4 color; } fragment; -in Vert -{ - vec2 texcoord; -} vert; - uniform sampler2D Diffuse; uniform mat4 ViewMatrixInverse; +uniform mat4 TextureMVP; in vec3 lightPos,cameraPosition, normal,ambient; in vec4 vertexPos; @@ -23,10 +19,11 @@ in vec3 materialSpecularColor; out vec4 color; - void main(void) { - vec4 texel = fragment.color*texture(Diffuse,vert.texcoord.xy); + vec4 projcoords = TextureMVP * vertexPos; + vec2 texturecoords = projcoords.xy/max(projcoords.z,0.1); + vec4 texel = fragment.color*texture(Diffuse,texturecoords); vec3 ct,cf; float intensity,at,af; if (fragment.color.w==0) diff --git a/examples/OpenGLWindow/Shaders/projectiveTextureInstancingPS.h b/examples/OpenGLWindow/Shaders/projectiveTextureInstancingPS.h index 2bdb30b64..0c5f72438 100644 --- a/examples/OpenGLWindow/Shaders/projectiveTextureInstancingPS.h +++ b/examples/OpenGLWindow/Shaders/projectiveTextureInstancingPS.h @@ -6,12 +6,9 @@ static const char* projectiveTextureInstancingFragmentShader= \ "{\n" " vec4 color;\n" "} fragment;\n" -"in Vert\n" -"{\n" -" vec2 texcoord;\n" -"} vert;\n" "uniform sampler2D Diffuse;\n" "uniform mat4 ViewMatrixInverse;\n" +"uniform mat4 TextureMVP;\n" "in vec3 lightPos,cameraPosition, normal,ambient;\n" "in vec4 vertexPos;\n" "in float materialShininess;\n" @@ -20,7 +17,9 @@ static const char* projectiveTextureInstancingFragmentShader= \ "out vec4 color;\n" "void main(void)\n" "{\n" -" vec4 texel = fragment.color*texture(Diffuse,vert.texcoord.xy);\n" +" vec4 projcoords = TextureMVP * vertexPos;\n" +" vec2 texturecoords = projcoords.xy/max(projcoords.z,0.1);\n" +" vec4 texel = fragment.color*texture(Diffuse,texturecoords);\n" " vec3 ct,cf;\n" " float intensity,at,af;\n" " if (fragment.color.w==0)\n" diff --git a/examples/OpenGLWindow/Shaders/projectiveTextureInstancingVS.glsl b/examples/OpenGLWindow/Shaders/projectiveTextureInstancingVS.glsl index c3417ed55..8a080395c 100644 --- a/examples/OpenGLWindow/Shaders/projectiveTextureInstancingVS.glsl +++ b/examples/OpenGLWindow/Shaders/projectiveTextureInstancingVS.glsl @@ -93,7 +93,5 @@ void main(void) gl_Position = vertexLoc; fragment.color = instance_color; - 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 b1de7880d..449c410be 100644 --- a/examples/OpenGLWindow/Shaders/projectiveTextureInstancingVS.h +++ b/examples/OpenGLWindow/Shaders/projectiveTextureInstancingVS.h @@ -80,7 +80,5 @@ 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 = 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 00ae2320d..c4b21b9d0 100644 --- a/examples/pybullet/examples/projective_texture.py +++ b/examples/pybullet/examples/projective_texture.py @@ -1,6 +1,5 @@ import pybullet as p from time import sleep -from PIL import Image import matplotlib.pyplot as plt import numpy as np @@ -9,7 +8,7 @@ physicsClient = p.connect(p.GUI) p.setGravity(0,0,0) bearStartPos1 = [-3.3,0,0] bearStartOrientation1 = p.getQuaternionFromEuler([0,0,0]) -bearId1 = p.loadURDF("teddy_large.urdf", bearStartPos1, bearStartOrientation1) +bearId1 = p.loadURDF("plane.urdf", bearStartPos1, bearStartOrientation1) bearStartPos2 = [0,0,0] bearStartOrientation2 = p.getQuaternionFromEuler([0,0,0]) bearId2 = p.loadURDF("teddy_large.urdf",bearStartPos2, bearStartOrientation2)