Commit 04a8e73e authored by Dirk Albrecht's avatar Dirk Albrecht

[CHANGE] Minor changes for lighting. Bad style in code corrected. Doc completed.

parent a7ef1a85
...@@ -405,41 +405,50 @@ private: ...@@ -405,41 +405,50 @@ private:
* of a tube from aside * of a tube from aside
* \param rootState The uniforms will be applied to this state. * \param rootState The uniforms will be applied to this state.
*/ */
void create1DTextureRectLightning( osg::StateSet* rootState ) const; void create1DTextureRectLighting( osg::StateSet* rootState ) const;
/** /**
* Creates a texture to simmulate diffuse and specular lightning * Creates a texture to simmulate diffuse and specular lightning
* of a tube from top or bottom * of a tube from top or bottom
* \param rootState The uniforms will be applied to this state. * \param rootState The uniforms will be applied to this state.
*/ */
void create2DTextureCycleLightning( osg::StateSet* rootState ) const; void create2DTextureCycleLighting( osg::StateSet* rootState ) const;
/** /**
* Creates a texture to simmulate diffuse and specular lightning * Creates a texture to simmulate diffuse and specular lightning
* of a tube from top or bottom * of a tube from top or bottom
* \param rootState The uniforms will be applied to this state. * \param rootState The uniforms will be applied to this state.
*/ */
void create2DTexDiffuseLightning( osg::StateSet* rootState ) const; void create2DTexturesBlinn( osg::StateSet* rootState ) const;
/** /**
* Creates a texture to simmulate diffuse and specular lightning * Creates a texture to simmulate diffuse and specular lightning
* of a tube from top or bottom * of a tube from top or bottom
* \param rootState The uniforms will be applied to this state. * \param rootState The uniforms will be applied to this state.
*/ */
void create2DTexSpecularLightning( osg::StateSet* rootState ) const; void createTextures( osg::StateSet* rootState ) const;
/** /**
* Creates a texture to simmulate diffuse and specular lightning * Creates a texture to simmulate diffuse and specular lightning
* of a tube from top or bottom * of a tube from top or bottom
* \param rootState The uniforms will be applied to this state.
*/ */
void createTextures( osg::StateSet* rootState ) const; void createTubeData();
/** /**
* Creates a texture to simmulate diffuse and specular lightning * Computes integrand for specular blinn lighting texture
* of a tube from top or bottom * \param beta
* \param n specular exponent
* \param theta
*/ */
void createTubeData(); double computeIntegrand( double beta, int n, double theta ) const;
/**
* Computes integral for specular blinn lighting texture
* \param alpha
* \param beta
* \param n specular exponent
*/
double computeSpecIntegral( double alpha, double beta, int n ) const;
/** /**
* Wrapper class for userData to prevent cyclic destructor calls * Wrapper class for userData to prevent cyclic destructor calls
......
...@@ -80,35 +80,9 @@ void WTubeGeometry::drawImplementation( osg::RenderInfo& renderInfo ) const ...@@ -80,35 +80,9 @@ void WTubeGeometry::drawImplementation( osg::RenderInfo& renderInfo ) const
if (checkForGLErrors) state.checkGLErrors( "start of Geometry::drawImplementation()" ); if (checkForGLErrors) state.checkGLErrors( "start of Geometry::drawImplementation()" );
bool useFastPath = areFastPathsUsed(); bool useFastPath = areFastPathsUsed();
useFastPath = false;
bool usingVertexBufferObjects = _useVertexBufferObjects && state.isVertexBufferObjectSupported();
bool handleVertexAttributes = !_vertexAttribList.empty();
osg::ArrayDispatchers& arrayDispatchers = state.getArrayDispatchers();
arrayDispatchers.reset();
arrayDispatchers.setUseVertexAttribAlias(useFastPath && state.getUseVertexAttributeAliasing());
arrayDispatchers.setUseGLBeginEndAdapter(!useFastPath);
arrayDispatchers.activateNormalArray(_normalData.binding, _normalData.array.get(), _normalData.indices.get());
arrayDispatchers.activateColorArray(_colorData.binding, _colorData.array.get(), _colorData.indices.get());
arrayDispatchers.activateSecondaryColorArray(_secondaryColorData.binding, _secondaryColorData.array.get(), _secondaryColorData.indices.get());
arrayDispatchers.activateFogCoordArray(_fogCoordData.binding, _fogCoordData.array.get(), _fogCoordData.indices.get());
if (handleVertexAttributes)
{
for(unsigned int unit=0;unit<_vertexAttribList.size();++unit)
{
arrayDispatchers.activateVertexAttribArray(_vertexAttribList[unit].binding,
unit, _vertexAttribList[unit].array.get(), _vertexAttribList[unit].indices.get());
}
}
state.disableAllVertexArrays(); state.disableAllVertexArrays();
// dispatch any attributes that are bound overall
arrayDispatchers.dispatch(BIND_OVERALL,0);
state.lazyDisablingOfVertexAttributes(); state.lazyDisablingOfVertexAttributes();
// set up arrays // set up arrays
...@@ -137,9 +111,6 @@ void WTubeGeometry::drawImplementation( osg::RenderInfo& renderInfo ) const ...@@ -137,9 +111,6 @@ void WTubeGeometry::drawImplementation( osg::RenderInfo& renderInfo ) const
state.applyDisablingOfVertexAttributes(); state.applyDisablingOfVertexAttributes();
bool bindPerPrimitiveSetActive = arrayDispatchers.active(BIND_PER_PRIMITIVE_SET);
bool bindPerPrimitiveActive = arrayDispatchers.active(BIND_PER_PRIMITIVE);
unsigned int primitiveNum = 0; unsigned int primitiveNum = 0;
if ( checkForGLErrors ) state.checkGLErrors("Geometry::drawImplementation() after vertex arrays setup."); if ( checkForGLErrors ) state.checkGLErrors("Geometry::drawImplementation() after vertex arrays setup.");
...@@ -169,8 +140,7 @@ void WTubeGeometry::drawImplementation( osg::RenderInfo& renderInfo ) const ...@@ -169,8 +140,7 @@ void WTubeGeometry::drawImplementation( osg::RenderInfo& renderInfo ) const
{ {
glDisable( GL_VERTEX_PROGRAM_POINT_SIZE ); glDisable( GL_VERTEX_PROGRAM_POINT_SIZE );
glDisable( GL_POINT_SPRITE ); glDisable( GL_POINT_SPRITE );
glEnable( GL_ALPHA_TEST ); glDisable( GL_ALPHA_TEST );
glBlendFunc( GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA );
glAlphaFunc( GL_NOTEQUAL, 0.0 ); glAlphaFunc( GL_NOTEQUAL, 0.0 );
break; break;
} }
...@@ -181,16 +151,16 @@ void WTubeGeometry::drawImplementation( osg::RenderInfo& renderInfo ) const ...@@ -181,16 +151,16 @@ void WTubeGeometry::drawImplementation( osg::RenderInfo& renderInfo ) const
boost::shared_ptr< std::vector< bool > > active = WKernel::getRunningKernel()->getRoiManager()->getBitField(); boost::shared_ptr< std::vector< bool > > active = WKernel::getRunningKernel()->getRoiManager()->getBitField();
boost::shared_ptr< std::vector< size_t > > pointsPerLine = m_dataset->getLineLengths(); boost::shared_ptr< std::vector< size_t > > pointsPerLine = m_dataset->getLineLengths();
//useFastPath = false;
if ( useFastPath ) if ( useFastPath )
{ {
for( unsigned int primitiveSetNum = 0; primitiveSetNum != _primitives.size(); ++primitiveSetNum ) for( unsigned int primitiveSetNum = 0; primitiveSetNum != _primitives.size(); ++primitiveSetNum )
{ {
// dispatch any attributes that are bound per primitive
//if (bindPerPrimitiveSetActive) arrayDispatchers.dispatch(BIND_PER_PRIMITIVE_SET, primitiveSetNum);
const osg::PrimitiveSet* primitiveset = _primitives[primitiveSetNum].get(); const osg::PrimitiveSet* primitiveset = _primitives[primitiveSetNum].get();
primitiveset->draw( state, true ); if( (*active)[primitiveSetNum] )
{
primitiveset->draw( state, true );
}
} }
} }
else else
......
varying vec3 tangentR3; // Tangent vector in world space
varying float tangent_dot_view;
uniform bool globalColor; uniform bool globalColor;
uniform sampler1D texture; uniform sampler1D texture;
uniform sampler2D texturePS; uniform sampler2D texturePS;
...@@ -11,6 +9,7 @@ varying float NdotL; ...@@ -11,6 +9,7 @@ varying float NdotL;
varying vec3 normal; varying vec3 normal;
varying vec3 halfVector; varying vec3 halfVector;
varying float endPoint; varying float endPoint;
varying vec3 tangentR3;
varying float z; varying float z;
varying float zNear; varying float zNear;
...@@ -21,20 +20,29 @@ uniform bool useLightModel; ...@@ -21,20 +20,29 @@ uniform bool useLightModel;
uniform bool usePointSprites; uniform bool usePointSprites;
uniform bool useQuadStripes; uniform bool useQuadStripes;
uniform bool useProjection; uniform bool useProjection;
uniform float lowerDepthCueingFactor;
uniform float upperDepthCueingFactor;
varying vec3 eyeDirection;
varying vec3 lightDirection;
void main() void main()
{ {
vec3 L, V, T, N, halfV; vec3 L,V, T, N, halfV;
vec3 specular = 0.0; float specular = 0.0;
float NdotL,NdotHV; float NdotL,NdotHV;
float alpha, beta, lt; float alpha, beta, lt;
T = normalize(tangentR3); T = normalize(tangentR3);
V = normalize(view); V = normalize(eyeDirection);
L = normalize(lightDir); L = normalize(lightDirection);
N = normalize(normal); N = normalize(normal);
float depthCueingFactor = 1.0; float depthCueingFactor = 1.0;
NdotL = max(dot(N,L),0.0); NdotL = saturate(dot(N, L));
vec3 eyeDir = normalize(eyeDirection);
vec4 color; vec4 color;
vec2 newTexCoords; vec2 newTexCoords;
if ( useLightModel ) if ( useLightModel )
{ {
...@@ -43,39 +51,38 @@ void main() ...@@ -43,39 +51,38 @@ void main()
beta = dot(halfVector,N) / sqrt( 1 - pow( dot(halfVector, T), 2)); beta = dot(halfVector,N) / sqrt( 1 - pow( dot(halfVector, T), 2));
lt = dot(L, T); lt = dot(L, T);
} }
else
//compute rotation in imageplane for pointsprite
vec2 imageTangentNorm = normalize(imageTangent);
newTexCoords.x = (imageTangentNorm.y * (gl_PointCoord.y - 0.5) + imageTangentNorm.x * (gl_PointCoord.x - 0.5)) + 0.5;
newTexCoords.y = (imageTangentNorm.y * (gl_PointCoord.x - 0.5) - imageTangentNorm.x * (gl_PointCoord.y - 0.5)) + 0.5;
color = texture2D(texturePS, newTexCoords.xy);
gl_FragColor.a = color.z;
if ( useDepthCueing )
{ {
//compute rotation in imageplane for pointsprite depthCueingFactor = clamp(( 1 - z + zNear ) / ( zNear + zFar ), lowerDepthCueingFactor, upperDepthCueingFactor );
vec2 imageTangentNorm = normalize(imageTangent);
newTexCoords.x = (imageTangentNorm.y * (gl_PointCoord.y - 0.5) + imageTangentNorm.x * (gl_PointCoord.x - 0.5)) + 0.5;
newTexCoords.y = (imageTangentNorm.y * (gl_PointCoord.x - 0.5) - imageTangentNorm.x * (gl_PointCoord.y - 0.5)) + 0.5;
} }
if(endPoint == 0.0) if(endPoint == 0.0)
{ {
color = texture2D(texturePS, newTexCoords.xy); gl_FragDepth = gl_FragCoord.z - 0.01;
color.x = 1.0; gl_FragColor.rgb = (vec3(0.2,0.2,0.2)+vec3(0.6,0.6,0.6)*NdotL) * tubeColor.rgb * depthCueingFactor * depthCueingFactor;
} }
else else
{ {
color = texture2D(texturePS, newTexCoords.xy); /* compute the specular term if NdotL is larger than zero */
} if (NdotL > 0.0)
gl_FragColor.a = color.z; {
// normalize the half-vector, and then compute the
/* compute the specular term if NdotL is larger than zero */ // cosine (dot product) with the normal
if (NdotL > 0.0) halfV = normalize(halfVector);
{
// normalize the half-vector, and then compute the
// cosine (dot product) with the normal
halfV = normalize(halfVector);
NdotHV = max(dot(normal, halfV),0.0); NdotHV = saturate(dot(N, halfV));
specular = pow(NdotHV,16) * color.y; specular = pow(NdotHV,16)*color.y;
}
if ( useDepthCueing ) }
{ gl_FragColor.rgb = (tubeColor.rgb * ((vec3(0.2,0.2,0.2)+vec3(0.6,0.6,0.6)*NdotL*color.r) + vec3(0.6,0.6,0.6)*specular )) * depthCueingFactor * depthCueingFactor;
depthCueingFactor = clamp((1 - (z+zNear)/(zNear+zFar)),0.5,0.8);
} }
gl_FragColor.rgb = tubeColor.rgb * ((color.x) + specular) * depthCueingFactor * depthCueingFactor;
} }
...@@ -28,23 +28,36 @@ uniform bool useLightModel; ...@@ -28,23 +28,36 @@ uniform bool useLightModel;
uniform bool usePointSprites; uniform bool usePointSprites;
uniform bool useQuadStripes; uniform bool useQuadStripes;
uniform bool useProjection; uniform bool useProjection;
uniform float lowerDepthCueingFactor;
uniform float upperDepthCueingFactor;
// Auch die Richtung zum Betrachter ist in der Regel
// für jedes Fragment unterschiedlich.
varying vec3 eyeDirection;
varying vec3 lightDirection;
void main() void main()
{ {
float thickness; vec4 pos = gl_ModelViewProjectionMatrix * gl_Vertex;
vec4 pos = gl_ModelViewProjectionMatrix * gl_Vertex; vec3 tangent = ( gl_ModelViewProjectionMatrix * vec4(gl_Normal, 1. ) ).xyz; //!< transform our tangent vector
vec3 tangent = (gl_ModelViewProjectionMatrix * vec4(gl_Normal,0.)).xyz; // transform our tangent vector vec3 tangentR3 = normalize(gl_NormalMatrix * gl_Normal);
float tmp = dot(-pos.xyz, tangent); vec3 eyeDir = normalize(-(gl_ModelViewMatrix * gl_Vertex).xyz);
gl_ClipVertex = gl_ModelViewMatrix * gl_Vertex; // make clipping planes work gl_ClipVertex = gl_ModelViewMatrix * gl_Vertex;//!< make clipping planes work
//scalar product between viewDir and tangent
float tmp = dot( tangent, eyeDir);
//calculation for pointsprites //calculation for pointsprites
if(tmp > 0.93 ||psTexCoords.t == 0.0 ) if(tmp > 0.93 ||psTexCoords.t == 0.0 || !useQuadStripes)
{ {
vec3 lightPosition = vec3(0., 0., -1.); float thickness;
tangentR3 = gl_Normal; vec3 lightPos = vec3(0., 0., 0.);
vec3 lightDir = normalize(lightPos+eyeDir);
vec3 ez = vec3(0., 0., -1.0);
endPoint = 1.0; endPoint = 1.0;
if ( useDepthCueing ) if ( useDepthCueing || useProjection )
{ {
z = pos.z; z = pos.z;
float z1 =0.5 + 0.5 * ( ( gl_ModelViewProjectionMatrix * vec4( nearPos, 1.0 ) ).z ); float z1 =0.5 + 0.5 * ( ( gl_ModelViewProjectionMatrix * vec4( nearPos, 1.0 ) ).z );
...@@ -60,43 +73,35 @@ void main() ...@@ -60,43 +73,35 @@ void main()
zNear = min( z1, min( z2, min( z3, min( z4, min( z5, min( z6, min( z7,z8))))))); zNear = min( z1, min( z2, min( z3, min( z4, min( z5, min( z6, min( z7,z8)))))));
} }
view = - pos.xyz; vec3 cameraPosition = vec3(gl_ModelViewProjectionMatrix * vec4(0.0, 0.0, 1.0, 0.0));
vec3 referencePosition = vec3(gl_ModelViewProjectionMatrix * vec4(0.0, 0.0, -1.0, 0.0));
lightDir = normalize( lightPosition - pos.xyz);
vec3 cameraPosition = vec3(gl_ModelViewMatrixInverse * vec4(0,0,0,1.0)); halfVector = normalize( eyeDir + lightDir );
vec3 cameraVector = cameraPosition - gl_Vertex.xyz; vec3 binormal = normalize( cross( tangentR3, eyeDir ) );
vec3 referencePosition = vec3(gl_ModelViewProjectionMatrix * vec4(1.0, 1.0, 1.0, 0.0)); normal = normalize( cross( binormal, tangentR3));
halfVector = normalize(cross(view,tangent)); lightDirection = lightDir;
normal = cross( halfVector, tangent); eyeDirection = eyeDir;
// color of tube // color of tube
tubeColor = gl_Color; tubeColor = gl_Color;
vec3 offset = normalize(cross( eyeDir, tangentR3));
vec3 offsetNN;
if ( useProjection ) if ( useProjection )
{ {
offsetNN = cross( (tangent.xyz), vec3(.0, .0, -1.)); thickness *= clamp(( 1 - z + zNear ) / ( zNear + zFar ), lowerDepthCueingFactor, upperDepthCueingFactor );
}
else
{
offsetNN = cross( normalize(tangent.xyz), vec3(.0, .0, -1.));
} }
vec3 offset = normalize(offsetNN);
tangent_dot_view = length(offsetNN);
//scalar product between viewDir and tangent //scalar product between viewDir and tangent
// compute thickness // compute thickness
thickness *= length(referencePosition) * u_thickness * 0.001; thickness = max(distance(referencePosition, cameraPosition), 0.1) * u_thickness * 0.001;
offset.x *= thickness*u_viewportWidth; offset.x *= thickness*u_viewportWidth;
offset.y *= thickness*u_viewportHeight; offset.y *= thickness*u_viewportHeight;
vec4 pos1 = gl_Vertex; vec4 pos1 = gl_Vertex;
vec4 pos2 = gl_Vertex + vec4(gl_Normal,1); vec4 pos2 = gl_Vertex + vec4(gl_Normal,1);
vec4 pos1p = gl_ModelViewProjectionMatrix * pos1.xyzw; vec4 pos1p = gl_ModelViewProjectionMatrix * pos1;
vec4 pos2p = gl_ModelViewProjectionMatrix * pos2.xyzw; vec4 pos2p = gl_ModelViewProjectionMatrix * pos2;
imageTangent.xy = (pos2p.x/pos2p.w - pos1p.x/pos1p.w, pos1p.y/pos1p.w - pos2p.y/pos2p.w); imageTangent.xy = (pos2p.x/pos2p.w - pos1p.x/pos1p.w, pos1p.y/pos1p.w - pos2p.y/pos2p.w);
imageTangent = normalize(imageTangent); imageTangent = normalize(imageTangent);
......
varying vec3 tangentR3; // Tangent vector in world space
varying float s_param; // s parameter of texture [-1..1]
varying float tangent_dot_view;
uniform bool globalColor; uniform bool globalColor;
uniform sampler1D texture; uniform sampler1D texture;
uniform sampler2D texturePS; uniform sampler2D texturePS;
varying float usePointSprite; uniform sampler2D textureDiff2D;
varying vec2 imageTangent; uniform sampler2D textureSpec2D;
varying vec4 tubeColor; varying vec4 tubeColor;
varying vec3 view; varying vec3 view;
varying vec3 lightDir; varying vec3 lightDir;
...@@ -16,6 +13,7 @@ varying float endPoint; ...@@ -16,6 +13,7 @@ varying float endPoint;
varying float z; varying float z;
varying float zNear; varying float zNear;
varying float zFar; varying float zFar;
varying vec3 tangentR3;
uniform bool useDepthCueing; uniform bool useDepthCueing;
uniform bool useLightModel; uniform bool useLightModel;
...@@ -23,48 +21,62 @@ uniform bool usePointSprites; ...@@ -23,48 +21,62 @@ uniform bool usePointSprites;
uniform bool useQuadStripes; uniform bool useQuadStripes;
uniform bool useProjection; uniform bool useProjection;
uniform float lowerDepthCueingFactor;
uniform float upperDepthCueingFactor;
varying vec3 eyeDirection;
varying vec3 lightDirection;
void main() void main()
{ {
vec3 L,V, T, N, halfV; vec3 L,V, T, N, halfV;
vec3 specular = 0.0; float specular = 0.0;
float NdotL,NdotHV; float NdotL,NdotHV;
float alpha, beta, lt; float alpha, beta, lt;
T = normalize(tangentR3); T = normalize(tangentR3);
V = normalize(view); V = normalize(eyeDirection);
L = normalize(lightDir); L = normalize(lightDirection);
N = normalize(normal); N = normalize(normal);
float depthCueingFactor = 1.0; float depthCueingFactor = 1.0;
//NdotL = max(dot(N,L),0.0); NdotL = saturate(dot(N, L));
vec4 color;
if ( useDepthCueing )
{
depthCueingFactor = clamp( ( 1 - z + zNear ) / ( zNear + zFar ), lowerDepthCueingFactor, upperDepthCueingFactor );
}
if ( useLightModel ) if ( useLightModel )
{ {
// koordinaten für illuminated lines texturen // koordinaten für illuminated lines texturen
halfV = normalize(halfVector);
alpha = dot(L,N) / sqrt(1 - pow( dot(L,T),2)); alpha = dot(L,N) / sqrt(1 - pow( dot(L,T),2));
beta = dot(halfVector,N) / sqrt( 1 - pow( dot(halfVector, T), 2)); float ht = sqrt( 1 - pow( dot(halfV, T), 2));
beta = dot(halfV,N) / ht;
lt = dot(L, T); lt = dot(L, T);
vec4 colorDiff = texture2D(textureDiff2D, vec2( alpha, lt ));
vec4 colorSpec = texture2D(textureSpec2D, vec2( alpha, beta ));
gl_FragColor.rgb = (tubeColor.rgb * ((vec3(0.2,0.2,0.2)+vec3(0.6,0.6,0.6)*pow(ht,64)*colorDiff.x) + vec3(0.6,0.6,0.6)*colorSpec.x )) * depthCueingFactor * depthCueingFactor;
} }
else else
{ {
color = texture1D(texture, gl_TexCoord[1].s); vec4 color = texture1D(texture, (gl_TexCoord[1].s ));
gl_FragColor.a = tubeColor.a; gl_FragColor.a = tubeColor.a;
} /* compute the specular term if NdotL is larger than zero */
if (NdotL > 0.0)
{
// normalize the half-vector, and then compute the
// cosine (dot product) with the normal
halfV = normalize(halfVector);
/* compute the specular term if NdotL is larger than zero */ NdotHV = saturate(dot(N, halfV));
if (NdotL > 0.0) specular = pow(NdotHV,16) * color.y;
{
// normalize the half-vector, and then compute the
// cosine (dot product) with the normal
halfV = normalize(halfVector);
NdotHV = max(dot(N, halfV),0.0); }
specular = pow(NdotHV,16) * color.y; gl_FragColor.rgb = (tubeColor.rgb * ((vec3(0.2,0.2,0.2)+vec3(0.6,0.6,0.6)*NdotL*color.r) + vec3(0.6,0.6,0.6)*specular )) * depthCueingFactor * depthCueingFactor;
}
if ( useDepthCueing )
{
depthCueingFactor = clamp((1 - (z+zNear)/(zNear+zFar)),0.5,0.8);
} }
gl_FragColor.rgb = ( tubeColor.rgb * color.x + specular ) * depthCueingFactor * depthCueingFactor;
} }
...@@ -11,6 +11,10 @@ varying vec3 normal; ...@@ -11,6 +11,10 @@ varying vec3 normal;
varying vec3 halfVector;