Hello Tiago,

do you mind showing us the shader & relevant OpenSG code. We are
thinking about implementing a splat modul for our CAE tool ... maybe we
could avoid some pitfalls this way :-)

Bests,
Stefan Wundrak

Stefan, it's currently in development (i only have the visibility pass half done, i'll have to add at least another pass for shading)
anyway, this is what i got:
 
OpenSG Setup:

    osg::GeoPTypesPtr type = osg::GeoPTypesUI8::create();       
    beginEditCP(type, osg::GeoPTypesUI8::GeoPropDataFieldMask);
    {
        type->push_back(GL_POINTS);       
    }
    endEditCP  (type, osg::GeoPTypesUI8::GeoPropDataFieldMask);

   // create the shader material
    char *fs=textFileRead("splat.frag");
    char *vs=textFileRead("splat.vert");
  
    osg::ChunkMaterialPtr cmat = osg::ChunkMaterial::create();
    osg::SHLChunkPtr _shl = osg::SHLChunk::create();
    beginEditCP(_shl);
        _shl->setVertexProgram(vs);
        _shl->setFragmentProgram(fs);
        _shl->setPointSize(true); //this doesn't compile - i don't know yet how to do it right
        _shl->setUniformParameter("VPWidth", (float)mwin->getWidth());
        _shl->setUniformParameter("VPHeight", (float)mwin->getHeight());
    endEditCP(_shl);

    _pt = osg::PointChunk::create();
    beginEditCP(_pt);
        _pt->setMaxSize(100.0);
        _pt->setSize(20.0);        //this sets the point size correctly, but it's fixed (can't be changed in the shader) - anyway should depend on the distance from the eye to the points
        _pt->setMinSize(1.0);
    endEditCP(_pt);

    beginEditCP(cmat);
        cmat->addChunk(_shl);
        cmat->addChunk(_pt);
    endEditCP(cmat);

    //put the properties in the geometry
   //i'm not yet using indices but it's an obvious thing to add
    geo=osg::Geometry::create();  
    beginEditCP(geo, osg::Geometry::TypesFieldMask     |
                     osg::Geometry::LengthsFieldMask   |                    
                     osg::Geometry::PositionsFieldMask |
                     osg::Geometry::ColorsFieldMask    |
                     osg::Geometry::NormalsFieldMask   |
                     osg::Geometry::MaterialFieldMask  |
                     osg::Geometry::DlistCacheFieldMask );
    {
       
        geo->setTypes    (type);
        geo->setLengths  (lens); 
        geo->setPositions(pnts);
        geo->setColors   (colors);
        geo->setNormals  (norms);       
        geo->setMaterial (cmat );    
        geo->setDlistCache(false);
    }
    endEditCP  (geo, osg::Geometry::TypesFieldMask     |
                     osg::Geometry::LengthsFieldMask   |                    
                     osg::Geometry::PositionsFieldMask |
                     osg::Geometry::ColorsFieldMask    |
                     osg::Geometry::NormalsFieldMask   |
                     osg::Geometry::MaterialFieldMask  |
                     osg::Geometry::DlistCacheFieldMask );


Vertex Shader:

varying vec3 normal;
varying vec4 center;
uniform float VPWidth;
uniform float VPHeight;

void main()
{       
    gl_FrontColor = gl_Color;   
    normal = gl_NormalMatrix * gl_Normal;      
    vec4 ecPos=gl_ModelViewMatrix*gl_Vertex;
    float dist=length(ecPos)-2;
    if(dist!=0.0)
         gl_PointSize=30.0/dist;  // this formula isn't right. it's just to get an idea
     else
         gl_PointSize=1;
     gl_Position = ftransform();
     center=
gl_Position;
}

Fragment Shader:


varying vec3 normal;
varying vec4 center;
uniform float VPWidth;
uniform float VPHeight;

void main()
{   
    vec4 wpos; //eye space position of current fragment
    vec4 wcenter; //eye space position of point center
    vec2 screenCoord; //Corrected screen coordinate of the fragment
    vec2 halfScreenSizeRecip;
    float t,temp;
    vec4 intersectionPoint; //intersection between the ray (from eye to fragment position) and the splat plane

    vec3 n = normalize(normal);
   
    gl_FragDepth=gl_FragCoord.z; //this isn't right but the zbuffer needs to be updated
   
    if(n.z<0.0)  //backfacing
        discard;                                       
   
    halfScreenSizeRecip.x=2.0 / VPWidth;
    halfScreenSizeRecip.y=2.0 / VPHeight;
   
    screenCoord = vec2(gl_FragCoord) * halfScreenSizeRecip - 1.0;
    wpos=gl_ProjectionMatrixInverse * vec4(screenCoord.x, screenCoord.y, gl_FragCoord.z * 2.0 - 1.0 , 1.0);
    wpos/=wpos.w;           
   
    temp=dot(n,wpos.xyz);
    if(temp!=0.0){
        wcenter=gl_ProjectionMatrixInverse * center;
        t=dot(n,wcenter.xyz)/temp;
    }
    else{
        discard;
    }
    intersectionPoint=wpos*t;       
       
    float dist = length(intersectionPoint.xyz - wcenter.xyz);   
  
    if(dist>0.1)          //0.1 is the maximum distance between my points, so i use it as radius for the splats to avoid holes.
        discard;
    else{
        float intensity = dot(vec3(gl_LightSource[0].position),n);
        //vec4 ipoint=gl_ProjectionMatrix * intersectionPoint;               
        //gl_FragDepth = (ipoint.z +1.0)*0.5;         //should work, but doesn't :p
        gl_FragColor = gl_Color*intensity;                //in this pass the color is not important, it's just for testing purposes
    }
}
-------------------------------------------------------------------------
Using Tomcat but need to do more? Need to support web services, security?
Get stuff done quickly with pre-integrated technology to make your job easier
Download IBM WebSphere Application Server v.1.0.1 based on Apache Geronimo
http://sel.as-us.falkag.net/sel?cmd=lnk&kid=120709&bid=263057&dat=121642
_______________________________________________
Opensg-users mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/opensg-users

Reply via email to