From 98d50250ff5a95499cf29b18ca8617cc9601e366 Mon Sep 17 00:00:00 2001 From: Robert Osfield Date: Thu, 27 Oct 2011 16:33:18 +0000 Subject: [PATCH] Updated volume shaders to use the GL_LIGHT 0 values to control the direction of the light source --- src/osgVolume/Shaders/volume_iso_frag.cpp | 5 +++-- src/osgVolume/Shaders/volume_lit_frag.cpp | 5 ++--- src/osgVolume/Shaders/volume_lit_tf_frag.cpp | 3 ++- src/osgVolume/Shaders/volume_tf_iso_frag.cpp | 4 ++-- src/osgVolume/Shaders/volume_vert.cpp | 17 ++++++++++++++++- 5 files changed, 25 insertions(+), 9 deletions(-) diff --git a/src/osgVolume/Shaders/volume_iso_frag.cpp b/src/osgVolume/Shaders/volume_iso_frag.cpp index 5a7ae8271..e55117492 100644 --- a/src/osgVolume/Shaders/volume_iso_frag.cpp +++ b/src/osgVolume/Shaders/volume_iso_frag.cpp @@ -5,13 +5,13 @@ char volume_iso_frag[] = "uniform sampler3D baseTexture;\n" "\n" "varying vec4 cameraPos;\n" "varying vec4 vertexPos;\n" + "varying vec3 lightDirection;\n" "varying mat4 texgen;\n" "\n" "void main(void)\n" "{ \n" " vec4 t0 = vertexPos;\n" " vec4 te = cameraPos;\n" - " vec3 eyeDirection = normalize((te-t0).xyz);\n" "\n" " if (te.x>=0.0 && te.x<=1.0 &&\n" " te.y>=0.0 && te.y<=1.0 &&\n" @@ -103,13 +103,14 @@ char volume_iso_frag[] = "uniform sampler3D baseTexture;\n" " if (grad.x!=0.0 || grad.y!=0.0 || grad.z!=0.0)\n" " {\n" " vec3 normal = normalize(grad);\n" - " float lightScale = 0.1 + abs(dot(normal.xyz, eyeDirection))*0.9;\n" + " float lightScale = 0.1 + max(0.0, dot(normal.xyz, lightDirection))*0.9;\n" "\n" " color.x = lightScale;\n" " color.y = lightScale;\n" " color.z = lightScale;\n" " }\n" "\n" + "\n" " color.a = 1.0;\n" "\n" " gl_FragColor = color;\n" diff --git a/src/osgVolume/Shaders/volume_lit_frag.cpp b/src/osgVolume/Shaders/volume_lit_frag.cpp index 83aeb9a8a..6955a8b97 100644 --- a/src/osgVolume/Shaders/volume_lit_frag.cpp +++ b/src/osgVolume/Shaders/volume_lit_frag.cpp @@ -5,6 +5,7 @@ char volume_lit_frag[] = "uniform sampler3D baseTexture;\n" "\n" "varying vec4 cameraPos;\n" "varying vec4 vertexPos;\n" + "varying vec3 lightDirection;\n" "varying mat4 texgen;\n" "\n" "void main(void)\n" @@ -60,8 +61,6 @@ char volume_lit_frag[] = "uniform sampler3D baseTexture;\n" " t0 = t0 * texgen;\n" " te = te * texgen;\n" "\n" - " vec3 eyeDirection = normalize((te-t0).xyz);\n" - "\n" " const float max_iteratrions = 2048.0;\n" " float num_iterations = ceil(length((te-t0).xyz)/SampleDensityValue);\n" " if (num_iterations<2.0) num_iterations = 2.0;\n" @@ -97,7 +96,7 @@ char volume_lit_frag[] = "uniform sampler3D baseTexture;\n" " if (grad.x!=0.0 || grad.y!=0.0 || grad.z!=0.0)\n" " {\n" " vec3 normal = normalize(grad);\n" - " float lightScale = 0.1 + abs(dot(normal.xyz, eyeDirection))*0.9;\n" + " float lightScale = 0.1 + max(0.0, dot(normal.xyz, lightDirection))*0.9;\n" "\n" " color.x *= lightScale;\n" " color.y *= lightScale;\n" diff --git a/src/osgVolume/Shaders/volume_lit_tf_frag.cpp b/src/osgVolume/Shaders/volume_lit_tf_frag.cpp index 92c57be47..7f97dfee9 100644 --- a/src/osgVolume/Shaders/volume_lit_tf_frag.cpp +++ b/src/osgVolume/Shaders/volume_lit_tf_frag.cpp @@ -10,6 +10,7 @@ char volume_lit_tf_frag[] = "uniform sampler3D baseTexture;\n" "\n" "varying vec4 cameraPos;\n" "varying vec4 vertexPos;\n" + "varying vec3 lightDirection;\n" "varying mat4 texgen;\n" "\n" "void main(void)\n" @@ -103,7 +104,7 @@ char volume_lit_tf_frag[] = "uniform sampler3D baseTexture;\n" " if (grad.x!=0.0 || grad.y!=0.0 || grad.z!=0.0)\n" " {\n" " vec3 normal = normalize(grad);\n" - " float lightScale = 0.1 + abs(dot(normal.xyz, eyeDirection))*0.9;\n" + " float lightScale = 0.1 + max(0.0, dot(normal.xyz, lightDirection))*0.9;\n" "\n" " color.x *= lightScale;\n" " color.y *= lightScale;\n" diff --git a/src/osgVolume/Shaders/volume_tf_iso_frag.cpp b/src/osgVolume/Shaders/volume_tf_iso_frag.cpp index 60eeb9ed7..25f6f1887 100644 --- a/src/osgVolume/Shaders/volume_tf_iso_frag.cpp +++ b/src/osgVolume/Shaders/volume_tf_iso_frag.cpp @@ -10,13 +10,13 @@ char volume_tf_iso_frag[] = "uniform sampler3D baseTexture;\n" "\n" "varying vec4 cameraPos;\n" "varying vec4 vertexPos;\n" + "varying vec3 lightDirection;\n" "varying mat4 texgen;\n" "\n" "void main(void)\n" "{ \n" " vec4 t0 = vertexPos;\n" " vec4 te = cameraPos;\n" - " vec3 eyeDirection = normalize((te-t0).xyz);\n" "\n" " if (te.x>=0.0 && te.x<=1.0 &&\n" " te.y>=0.0 && te.y<=1.0 &&\n" @@ -110,7 +110,7 @@ char volume_tf_iso_frag[] = "uniform sampler3D baseTexture;\n" " if (grad.x!=0.0 || grad.y!=0.0 || grad.z!=0.0)\n" " {\n" " vec3 normal = normalize(grad);\n" - " float lightScale = 0.1 + abs(dot(normal.xyz, eyeDirection))*0.9;\n" + " float lightScale = 0.1 + max(0.0, dot(normal.xyz, lightDirection))*0.9;\n" "\n" " color.x *= lightScale;\n" " color.y *= lightScale;\n" diff --git a/src/osgVolume/Shaders/volume_vert.cpp b/src/osgVolume/Shaders/volume_vert.cpp index 17a6fa6e7..14ee7f553 100644 --- a/src/osgVolume/Shaders/volume_vert.cpp +++ b/src/osgVolume/Shaders/volume_vert.cpp @@ -1,15 +1,30 @@ char volume_vert[] = "#version 110\n" "varying vec4 cameraPos;\n" "varying vec4 vertexPos;\n" + "varying vec3 lightDirection;\n" "varying mat4 texgen;\n" "\n" + "\n" "void main(void)\n" "{\n" " gl_Position = ftransform();\n" "\n" - " cameraPos = gl_ModelViewMatrixInverse*vec4(0,0,0,1);\n" + " cameraPos = gl_ModelViewMatrixInverse * vec4(0,0,0,1);\n" " vertexPos = gl_Vertex;\n" "\n" + " vec4 lightPosition = gl_ModelViewMatrixInverse * gl_LightSource[0].position;\n" + " if (lightPosition[3]==0.0)\n" + " {\n" + " // directional light source\n" + " lightDirection = -normalize(lightPosition.xyz);\n" + " }\n" + " else\n" + " {\n" + " // positional light source\n" + " lightDirection = normalize((lightPosition-vertexPos).xyz);\n" + " }\n" + "\n" + "\n" " texgen = mat4(gl_ObjectPlaneS[0], \n" " gl_ObjectPlaneT[0],\n" " gl_ObjectPlaneR[0],\n"