Re: [osg-users] Problems with osgUtil::TangentSpaceGenerator

2011-06-29 Thread Guo Chow
Daniel Borchart  writes:

> 
> Hi,
> 
> I'm trying to implement a bump mapping shader. That works well for a simple 
shape like that plane, where I
> define the tangent attribute manually:
> 
> Code:
>   Geometry* plane = new Geometry();
>Vec3Array* vertices = new Vec3Array();
>   Vec3Array* normals = new Vec3Array();
>   DrawElementsUInt* faces = new DrawElementsUInt
(PrimitiveSet::TRIANGLES, 0);
>   Vec3Array* texcoords = new Vec3Array();
>   Vec3Array* tangents = new Vec3Array();
> 
>   vertices->push_back(Vec3f(1.0f, 0.0f, 1.0f));
>   vertices->push_back(Vec3f(-1.0f, 0.0f, 1.0f));
>   vertices->push_back(Vec3f(1.0f, 0.0f, -1.0f));
>   vertices->push_back(Vec3f(-1.0f, 0.0f, -1.0f));
> 
>   normals->push_back(Vec3f(0.0f, -1.0f, 0.0f));
> 
>   texcoords->push_back(Vec3f(1.0f, 1.0f, 1.0f));
>   texcoords->push_back(Vec3f(0.0f, 1.0f, 1.0f));
>   texcoords->push_back(Vec3f(1.0f, 0.0f, 1.0f));
>   texcoords->push_back(Vec3f(0.0f, 0.0f, 1.0f));
> 
>   faces->push_back(0);
>   faces->push_back(1);
>   faces->push_back(3);
>   faces->push_back(3);
>   faces->push_back(2);
>   faces->push_back(0);
> 
>   tangents->push_back(Vec3(-1,0,0));
> 
>   plane->setVertexArray(vertices);
>   plane->setNormalArray(normals);
>   plane->setTexCoordArray(0, texcoords);
>   plane->addPrimitiveSet(faces);
>   plane->setNormalBinding(Geometry::BIND_OVERALL);
> 
>   plane->setVertexAttribData(6, Geometry::ArrayData(tangents, 
Geometry::BIND_OVERALL));
> 
> But for more complex shapes I have to use the TangentGenerator. When I try 
to run this:
> 
> Code:
> ref_ptr tsg = new 
osgUtil::TangentSpaceGenerator();
> tsg->generate(plane, 0);
> 
> I get an "Debug Assertion Failed" with "Expression: vector subscript out of 
range" in generate().
> 
> Did I forget something? I cannot see the fault.
> 
> Daniel
> 
> --
> Read this topic online here:
> http://forum.openscenegraph.org/viewtopic.php?p=35575#35575
> 
> 

Hi Daniel,

I come up against the same problem of utilizing osgUtil::TangentSpaceGenerator 
in the same way. I trace into TangentGenerator.cpp then I find that on line 
223 the subscript of normal array(nx) is out of range when there is only one 
normal in it. TangentSpaceGenerator is assumed to work for normal binding of 
BIND_PER_VERTEX instead of BIND_OVERALL. So you have to set normal for EVERY 
vertex. If I miss something, please let me know.

Guo

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


Re: [osg-users] Shader Update Latency?!

2011-01-11 Thread Guo Chow
Robert Osfield  writes:

> 
> Hi Thorsten,
> 
> By default the OSG computes the near/far planes on the fly during the
> cull traversal on every single frame.  You can disable this.
> Alternatively you could just use the gl_ProjectionMatrix directly on
> the GPU to get the near/far planes - this is how I'd do it, far more
> flexible and never needs any additional uniforms or callbacks.
> 
> Robert.
> 
> On Wed, Dec 1, 2010 at 6:15 PM, Thorsten Roth
>  wrote:
> > Hi,
> >
> > I currently have a problem with a shader update callback I do not
> > understand. I have a vertex and fragment shader which calculate linear 
depth
> > in [0,1] for me, also respecting dynamic clipping planes. To achieve this, 
I
> > pass zNear and zFar as uniform parameters to the shader. To have them
> > updated, I have the following callback methods (zFar is looking
> > accordingly):
> >
> > class UpdateShaderZNear: public osg::Uniform::Callback {
> > public:
> >        virtual void operator()(osg::Uniform* uniform, osg::NodeVisitor* nv)
> > {
> >                double x, zNear;
> >        viewer->getCamera()->getProjectionMatrixAsPerspective(x,x,zNear,x);
> >                uniform->set((float)zFar);
> >        }
> > };
> >
> > Now when I move my camera towards and away from the object, it seems like
> > the shader update is one frame (or so) "too late", as I get values that do
> > not correspond to the [0,1]-normalization and the problem disappears as 
soon
> > as the camera stops.
> >
> > Is there any reason for that/does anyone have an idea what I'm doing wrong?
> > If more information or code is necessary, just tell me 
> >
> > -Thorsten
> > ___
> > osg-users mailing list
> > osg-us...@...
> > http://lists.openscenegraph.org/listinfo.cgi/osg-users-openscenegraph.org
> >
> 

Hi Robert,

I encounter a similar latency problem when I try to update a uniform using 
camera's view matrix in the uniform's callback. Since this uniform is needed 
to compute only once per frame, I decide to compute it on CPU before it's 
submitted to GPU. 

It seems that when the uniform's callback is called, the camera has not been 
updated yet, right?

Currently I solve this problem by updating the uniform in a PreDrawCallback of 
the camera. But is this a graceful way to achieve it?

Thanks in advance.

Guo

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