chrisd wrote:
> 
> Paul Martz wrote:
> > On 12/10/2010 5:21 AM, Chris Denham wrote:
> > 
> > > I've been trying to apply a 2d texture to an object where I want the UVs 
> > > to correspond to screen position. A bit like using EYE_LINEAR texgen, but 
> > > with the texture projected 'orthographically' through the screen.
> > > What I want would in effect be like applying the texture so it always 
> > > spans the window, but without applying the perspective of the camera. I 
> > > guess, a bit like using the texture as a stencil.
> > > 
> > > I've tried using EYE_LINEAR, and that very nearly does what I want, but I 
> > > want to remove the effect of perspective on the way the texcoords are 
> > > calculated.
> > > 
> > > In other words, I think I want calculated texture UV's to be something 
> > > like:
> > > texcoord.x = (worldPosition * (projectionMatrix * modelViewMatrix)).x
> > > texcoord.y = (worldPosition * (projectionMatrix * modelViewMatrix)).y
> > > 
> > > What's the best way to implement this without using stencil buffer or 
> > > shaders?
> > > Is there a way to do it by adjusting the texture matrix object to remove 
> > > the perspective projection?
> > > Or would it be better to recalculate texCoords on geometries prior to 
> > > each frame?
> > > 
> > > Hope this makes some kind of sense.
> > > 
> > 
> > With those restrictions, it seems like the texture matrix is your only path 
> > to a 
> > solution, though I couldn't type it here off the top of my head. It would 
> > take 
> > some dev time to get it right.
> > 
> > What you want to do would be trivial with a shader. Is there a reason why 
> > you've 
> > ruled out use of shaders for this problem?
> > 
> > -- 
> > -Paul Martz      Skew Matrix Software
> > http://www.skew-matrix.com/
> > 
> 
> 
> Thanks Paul,
> I may use a shader if it comes down to it, but I was trying (if possible) to 
> get a solution that works on Joe Public's bargin basement laptop that 
> probably has poor (if any) shader support.
> 
> I have experimented with texmat object to get the projection right, but I 
> can't seem to get the matrix right (probably cos of my flakey maths). Maybe 
> I'll post a cut down example an someone can see if they spot the problem.
> 
> I've had some success with recalculating the texcoords on geometries and 
> assigning them with UVs based on screen coords. It's very nearly what I want 
> but I'm getting some odd distortions due to uv interpolation (I think). 
> Possibly a result of the texture mapping perspective correction, though I 
> tried switching that off with GL_PERSPECTIVE_CORRECTION_HINT = GL_FASTEST to 
> no avail  :( 
> 
> Ho hum.... maybe as you say, a shader is the answer, well, if I can make it 
> fail tidy on old craptops.
> 
> Cheers
> Chris.


Well... I gave up on trying to do it that hard way, and as Paul suggested, it 
was fairly trivial to do in a shader. e.g.


Code:

osg::Program* programShader = new osg::Program();
stateSet->setAttribute(programShader, osg::StateAttribute::ON);
stateSet->addUniform(new osg::Uniform("viewport", osg::Vec4(0, 0, 800, 600)));
stateSet->addUniform(new osg::Uniform("screen_texture", 0));
stateSet->addUniform(new osg::Uniform("tinge", osg::Vec4(0.9, 0.9, 0.9, 1.0)));

stateSet->setUpdateCallback(new ViewportTracker(pViewer->getCamera())); 

osg::Shader *frag = new osg::Shader(osg::Shader::FRAGMENT);
frag->setShaderSource(
 "uniform sampler2D screen_texture;" \
 "uniform vec4 viewport;" \
 "uniform vec4 tinge;" \
 "void main()" \
 "{" \
 " vec2 origin = vec2(viewport[0], viewport[1]);" \
 " vec2 size = vec2(viewport[2], viewport[3]);" \
 " vec2 coord = (gl_FragCoord.xy - origin) / size;" \
 " vec4 color = texture2D(screen_texture, coord);" \
 " gl_FragColor = color * tinge;" \
 " if (!gl_FrontFacing) gl_FragColor = tinge;" \
 "}");

programShader->addShader(frag);




Though I did wonder if there was a better way to get or convert to normalized 
screen units because otherwise I have to put in a callback to track the 
viewport definition and transfer it to the Uniform attribute?
Not a big issue, but any ideas on how to avoid needing to do that?

Chris Denham

------------------
Read this topic online here:
http://forum.openscenegraph.org/viewtopic.php?p=34765#34765





_______________________________________________
osg-users mailing list
osg-users@lists.openscenegraph.org
http://lists.openscenegraph.org/listinfo.cgi/osg-users-openscenegraph.org

Reply via email to