Re: [osg-users] ClipNode Opposite Behavior

2015-08-17 Thread Christian Buchner
whatever float your boat... ;)

Here's a shader routine that evaluates if a fragment coordinate in object
space is within a polygon described by up to 16 vertices in the x/y plane.
We use it to cut a hole into a city scenario, so we can see into the
basement of a house. The shader does a simple point in polygon test.

The weird osgShadow_ naming for my uniforms comes from the fact that
we've extended the one of OSG's shadow implementations of OSG with support
for cutting holes into the shadowed scene.

const char MyShadowMap::fragmentShaderSource_HoleCutter[] =
{
#version 120\n
uniform vec2 osgShadow_polygon[16]; \n
uniform float osgShadow_nvert; \n
\n
bool pnpoly(vec2 test)\n
{\n
bool c = false;
if (osgShadow_nvert = 3)\n
{\n
int i, j;\n
for (i = 0, j = int(osgShadow_nvert) - 1; i 
int(osgShadow_nvert); j = i++) {\n
if (((osgShadow_polygon[i].ytest.y) !=
(osgShadow_polygon[j].ytest.y)) \n
(test.x  (osgShadow_polygon[j].x -
osgShadow_polygon[i].x) * (test.y - osgShadow_polygon[i].y) /
(osgShadow_polygon[j].y - osgShadow_polygon[i].y) +
osgShadow_polygon[i].x))\n
c = !c;\n
}\n
}\n
return c;\n
}\n
\n
bool is_hole(vec3 pos)\n
{\n
return pnpoly(pos.xy);\n
}\n
};

static const char fragmentShaderSource[] =
#version 120\n
\n
bool is_hole(vec3 pos);\n
\n
void main(void) \n
{ \n
\n
//   early discard
//   NOTE: we've previously set up gl_TexCoord[4].xyz to contain vertex
coordinates
//   so we don't require a vertex shader here that passes us a varying
of vertex coords.
if (is_hole(gl_TexCoord[4].xyz)) discard;\n
\n
 // remainder of shader is here
}\n;



2015-08-17 3:03 GMT+02:00 Erik Hensens ehens...@hunter.com:

 I just wanted to mention that I never got the fragment shader approach to
 work for what I wanted. I ended up using polygon tessellation to build the
 geometry out of triangles.

 Thanks anyway to everyone who attempted to help!

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





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

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


Re: [osg-users] ClipNode Opposite Behavior

2015-08-16 Thread Erik Hensens
I just wanted to mention that I never got the fragment shader approach to work 
for what I wanted. I ended up using polygon tessellation to build the geometry 
out of triangles.

Thanks anyway to everyone who attempted to help!

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





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


Re: [osg-users] ClipNode Opposite Behavior

2015-07-27 Thread Christian Buchner
If you want the hole to be specified in object local coordinates, use
gl_Vertex.
Getting world coordinates is a bit more tricky, you would need to have the
transformation matrix between object coordinates and world coordinates
accessible
in the vertex (or fragment) shader.

Definitely do not use gl_FragCoord - because this one is in 2D screen space.

Christian



2015-07-26 15:46 GMT+02:00 Glenn Waldron gwald...@gmail.com:

 Erick,
 gl_FragCoord is almost certainly not what you want (Google it). Try
 something like this:

 Vertex shader:

 uniform vec3 center;
 varying float dist;
 void main()
 {
 vec4 vertex = gl_ModelViewMatrix * gl_Vertex;
 dist = length(center - vertex.xyz);
 }

 Fragment shader:

 uniform float rad;
 varying float dist;
 void main()
 {
 if ( rad  dist )
 discard;
 else
 gl_FragColor = ...;
 }



 Glenn Waldron / @glennwaldron

 On Fri, Jul 24, 2015 at 9:22 AM, Erik Hensens ehens...@hunter.com wrote:

 Thanks Christian. It's funny you mention that because since I'm not
 setting the color, the quad looks like an old television tuned to a
 frequency not in service, i.e. static or snow. I think every time the
 quad is rendered each fragment's color is set to whatever value is in some
 uninitialized portion of memory, and each fragment changes color each time
 it is rendered. Quite a funny effect!

 I added the following line to set the color, and now it is always blue,
 but it's still not putting a hole in my quad, it's all there or all gone
 depending on whether I say if (fDist  rad) or if (fDist  rad):


 Code:

 // The fragment shader program source code
 std::string szFragSource(
 uniform vec3 center;\n
 uniform float rad;\n
 void main()\n
 {\n
 float fDistX = gl_FragCoord.x - center.x;\n
 float fDistY = gl_FragCoord.y - center.y;\n
 float fDistZ = gl_FragCoord.z - center.z;\n
 float fDist = sqrt(fDistX * fDistX + fDistY * fDistY + fDistZ *
 fDistZ);\n
 gl_FragColor = vec4(0.0,0.0,1.0,1.0);\n
 if (fDist  rad) discard;\n
 }\n
 );




 Any other ideas about why my code doesn't achieve what I want? For
 example, am I using gl_FragCoord correctly? Are there other errors in my
 frag source?

 Thanks again!


 cbuchner1 wrote:
  I believe your fragment shader is not setting the output fragment color
 at all, which is a minimum requirement for a fragment shader to work.
 
 
  Christian
 
 
 
  2015-07-23 23:59 GMT+02:00 Erik Hensens  ():
 
   Sorry for the triplicate post, I received an error message on trying
 to post until I removed the quotes...
  
   --
   Read this topic online here:
   http://forum.openscenegraph.org/viewtopic.php?p=64458#64458 (
 http://forum.openscenegraph.org/viewtopic.php?p=64458#64458)
  
  
  
  
  
   ___
   osg-users mailing list
()
  
 http://lists.openscenegraph.org/listinfo.cgi/osg-users-openscenegraph.org
 (
 http://lists.openscenegraph.org/listinfo.cgi/osg-users-openscenegraph.org
 )
  
  
  
 
 
   --
  Post generated by Mail2Forum


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





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



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


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


Re: [osg-users] ClipNode Opposite Behavior

2015-07-26 Thread Glenn Waldron
Erick,
gl_FragCoord is almost certainly not what you want (Google it). Try
something like this:

Vertex shader:

uniform vec3 center;
varying float dist;
void main()
{
vec4 vertex = gl_ModelViewMatrix * gl_Vertex;
dist = length(center - vertex.xyz);
}

Fragment shader:

uniform float rad;
varying float dist;
void main()
{
if ( rad  dist )
discard;
else
gl_FragColor = ...;
}



Glenn Waldron / @glennwaldron

On Fri, Jul 24, 2015 at 9:22 AM, Erik Hensens ehens...@hunter.com wrote:

 Thanks Christian. It's funny you mention that because since I'm not
 setting the color, the quad looks like an old television tuned to a
 frequency not in service, i.e. static or snow. I think every time the
 quad is rendered each fragment's color is set to whatever value is in some
 uninitialized portion of memory, and each fragment changes color each time
 it is rendered. Quite a funny effect!

 I added the following line to set the color, and now it is always blue,
 but it's still not putting a hole in my quad, it's all there or all gone
 depending on whether I say if (fDist  rad) or if (fDist  rad):


 Code:

 // The fragment shader program source code
 std::string szFragSource(
 uniform vec3 center;\n
 uniform float rad;\n
 void main()\n
 {\n
 float fDistX = gl_FragCoord.x - center.x;\n
 float fDistY = gl_FragCoord.y - center.y;\n
 float fDistZ = gl_FragCoord.z - center.z;\n
 float fDist = sqrt(fDistX * fDistX + fDistY * fDistY + fDistZ *
 fDistZ);\n
 gl_FragColor = vec4(0.0,0.0,1.0,1.0);\n
 if (fDist  rad) discard;\n
 }\n
 );




 Any other ideas about why my code doesn't achieve what I want? For
 example, am I using gl_FragCoord correctly? Are there other errors in my
 frag source?

 Thanks again!


 cbuchner1 wrote:
  I believe your fragment shader is not setting the output fragment color
 at all, which is a minimum requirement for a fragment shader to work.
 
 
  Christian
 
 
 
  2015-07-23 23:59 GMT+02:00 Erik Hensens  ():
 
   Sorry for the triplicate post, I received an error message on trying
 to post until I removed the quotes...
  
   --
   Read this topic online here:
   http://forum.openscenegraph.org/viewtopic.php?p=64458#64458 (
 http://forum.openscenegraph.org/viewtopic.php?p=64458#64458)
  
  
  
  
  
   ___
   osg-users mailing list
()
  
 http://lists.openscenegraph.org/listinfo.cgi/osg-users-openscenegraph.org
 (http://lists.openscenegraph.org/listinfo.cgi/osg-users-openscenegraph.org
 )
  
  
  
 
 
   --
  Post generated by Mail2Forum


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





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

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


Re: [osg-users] ClipNode Opposite Behavior

2015-07-24 Thread Christian Buchner
I believe your fragment shader is not setting the output fragment color at
all, which is a minimum requirement for a fragment shader to work.

Christian


2015-07-23 23:59 GMT+02:00 Erik Hensens ehens...@hunter.com:

 Sorry for the triplicate post, I received an error message on trying to
 post until I removed the quotes...

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





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

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


Re: [osg-users] ClipNode Opposite Behavior

2015-07-24 Thread Erik Hensens
Thanks Christian. It's funny you mention that because since I'm not setting the 
color, the quad looks like an old television tuned to a frequency not in 
service, i.e. static or snow. I think every time the quad is rendered each 
fragment's color is set to whatever value is in some uninitialized portion of 
memory, and each fragment changes color each time it is rendered. Quite a funny 
effect!

I added the following line to set the color, and now it is always blue, but 
it's still not putting a hole in my quad, it's all there or all gone depending 
on whether I say if (fDist  rad) or if (fDist  rad):


Code:

// The fragment shader program source code
std::string szFragSource(
uniform vec3 center;\n
uniform float rad;\n
void main()\n
{\n
float fDistX = gl_FragCoord.x - center.x;\n
float fDistY = gl_FragCoord.y - center.y;\n
float fDistZ = gl_FragCoord.z - center.z;\n
float fDist = sqrt(fDistX * fDistX + fDistY * fDistY + fDistZ * fDistZ);\n
gl_FragColor = vec4(0.0,0.0,1.0,1.0);\n
if (fDist  rad) discard;\n
}\n
);




Any other ideas about why my code doesn't achieve what I want? For example, am 
I using gl_FragCoord correctly? Are there other errors in my frag source?

Thanks again!


cbuchner1 wrote:
 I believe your fragment shader is not setting the output fragment color at 
 all, which is a minimum requirement for a fragment shader to work.
 
 
 Christian
 
 
 
 2015-07-23 23:59 GMT+02:00 Erik Hensens  ():
 
  Sorry for the triplicate post, I received an error message on trying to 
  post until I removed the quotes...
  
  --
  Read this topic online here:
  http://forum.openscenegraph.org/viewtopic.php?p=64458#64458 
  (http://forum.openscenegraph.org/viewtopic.php?p=64458#64458)
  
  
  
  
  
  ___
  osg-users mailing list
   ()
  http://lists.openscenegraph.org/listinfo.cgi/osg-users-openscenegraph.org 
  (http://lists.openscenegraph.org/listinfo.cgi/osg-users-openscenegraph.org)
  
  
  
 
 
  --
 Post generated by Mail2Forum


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





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


Re: [osg-users] ClipNode Opposite Behavior

2015-07-23 Thread Erik Hensens
Sorry for the triplicate post, I received an error message on trying to post 
until I removed the quotes...

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





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


Re: [osg-users] ClipNode Opposite Behavior

2015-07-23 Thread Erik Hensens
Thank you both for your input! I'm learning a lot.

I'm new to fragment shaders, so I think that maybe I'm doing something 
obviously wrong...

It seems that what I'm doing below should be pretty simple, but it doesn't 
work. When I reverse the if (fDist  rad) to if (fDist  rad) then the entire 
thing disappears. Otherwise (the way it is written below), nothing disappears.

Thanks in advance for your help!


Code:

// The group node that contains the test geode
osg::ref_ptrosg::Group pTestGroup = new osg::Group;

// The test geode
osg::ref_ptrosg::Geode pTestGeode = new osg::Geode;

// The geode's geometry
osg::ref_ptrosg::Geometry pTestGeometry = new osg::Geometry;

// The geometry's test vertices
osg::ref_ptrosg::Vec3Array pTestVerticesArray = new osg::Vec3Array;
pTestVerticesArray-push_back(osg::Vec3(0.0f, -2.0f, -2.0f));
pTestVerticesArray-push_back(osg::Vec3(0.0f, -2.0f, 2.0f));
pTestVerticesArray-push_back(osg::Vec3(0.0f, 2.0f, 2.0f));
pTestVerticesArray-push_back(osg::Vec3(0.0f, 2.0f, -2.0f));

// Set this as the vertex array
pTestGeometry-setVertexArray(pTestVerticesArray);

// Define the primitive set
osg::ref_ptrosg::DrawArrays pTestDrawArrays = new 
osg::DrawArrays(osg::PrimitiveSet::QUADS, 0, 
static_castGLsizei(pTestVerticesArray-size()));
pTestGeometry-addPrimitiveSet(pTestDrawArrays);

// Normalize
osg::ref_ptrosg::StateSet pTestStateSet = 
pTestGeometry-getOrCreateStateSet();
pTestStateSet-setMode(GL_NORMALIZE, osg::StateAttribute::ON);

// Add the test geometry to the geode
pTestGeode-addDrawable(pTestGeometry);

// Add the geode to the group
pTestGroup-addChild(pTestGeode);

// The fragment shader program source code
std::string szFragSource(
uniform vec3 center;\n
uniform float rad;\n
void main()\n
{\n
float fDistX = gl_FragCoord.x - center.x;\n
float fDistY = gl_FragCoord.y - center.y;\n
float fDistZ = gl_FragCoord.z - center.z;\n
float fDist = sqrt(fDistX * fDistX + fDistY * fDistY + fDistZ * fDistZ);\n
if (fDist  rad) discard;\n
}\n
);

// Create the fragment shader
osg::ref_ptrosg::Shader pFragmentShader = new 
osg::Shader(osg::Shader::FRAGMENT, szFragSource.c_str());

// Create the fragment program
osg::ref_ptrosg::Program pProgram = new osg::Program;

// Add the shader to the program
pProgram-addShader(pFragmentShader);

// Get the group node's stateset
osg::StateSet *pStateSet = pTestGroup-getOrCreateStateSet();

// The center of the sphere that I want to define the region that is not drawn
osg::Vec3 tCenter(0.0f, 0.0f, 0.0f);

// The radius of the sphere that I want to define the region that is not drawn
float fRadius = 1.0f;

// Add the center and radius uniforms
osg::ref_ptrosg::Uniform pCenterUniform = new osg::Uniform(center, tCenter);
osg::ref_ptrosg::Uniform pRadiusUniform = new osg::Uniform(rad, fRadius);
pStateSet-addUniform(pCenterUniform);
pStateSet-addUniform(pRadiusUniform);

// Enact this fragment shader on the stateset
pStateSet-setAttributeAndModes(pProgram);





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





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


Re: [osg-users] ClipNode Opposite Behavior

2015-07-23 Thread Christian Buchner
I think you only need to flip the clipping plane around (reverse the
plane's normal vector) to get the desired effect.


2015-07-22 21:28 GMT+02:00 Erik Hensens ehens...@hunter.com:

 Hi everyone!

 I have a geode that is just a quad rectangular surface and I need to be
 able to put circular holes in it. To better visualize what I'm talking
 about, think about a flat slice of Swiss cheese.

 I just discovered the ClipNode node and I figured I could achieve what I
 want by adding clip nodes that were the roughly circular, but ClipNode
 apparently behaves opposite to how I originally thought (I'm new to this).

 Is there a way to make ClipNode remove parts of the scene instead or
 keeping only those parts in the clip node region?

 Or, is there a better way to achieve this flat slice of Swiss cheese
 shape? Thanks very much in advance!

 Thank you!

 Cheers,
 Erik

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





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

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


Re: [osg-users] ClipNode Opposite Behavior

2015-07-23 Thread Werner Modenbach
Are you sure? I think this will leed to nothing being displayed. But I 
didn't verify it. It is just how I understand clipping.


Am 23.07.2015 um 10:35 schrieb Christian Buchner:


I think you only need to flip the clipping plane around (reverse the 
plane's normal vector) to get the desired effect.



2015-07-22 21:28 GMT+02:00 Erik Hensens ehens...@hunter.com 
mailto:ehens...@hunter.com:


Hi everyone!

I have a geode that is just a quad rectangular surface and I need
to be able to put circular holes in it. To better visualize what
I'm talking about, think about a flat slice of Swiss cheese.

I just discovered the ClipNode node and I figured I could achieve
what I want by adding clip nodes that were the roughly circular,
but ClipNode apparently behaves opposite to how I originally
thought (I'm new to this).

Is there a way to make ClipNode remove parts of the scene instead
or keeping only those parts in the clip node region?

Or, is there a better way to achieve this flat slice of Swiss
cheese shape? Thanks very much in advance!

Thank you!

Cheers,
Erik

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





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




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


--
*TEXION Software Solutions, Rotter Bruch 26a, D-52068 Aachen*
Phone: +49 241 475757-0
Fax: +49 241 475757-29
Web: http://texion.eu
eMail: i...@texion.eu
___
osg-users mailing list
osg-users@lists.openscenegraph.org
http://lists.openscenegraph.org/listinfo.cgi/osg-users-openscenegraph.org


Re: [osg-users] ClipNode Opposite Behavior

2015-07-23 Thread Christian Buchner
For creating a circular hole into a plane, clip planes indeed will not
work. But generally speaking, flipping a clipping plane reverses its effect
(boolean negation)

To punch holes in arbitrary geometry at render time, a fragment shader will
be a good solution.

You can check either object or world coordinates, e.g. distance to a given
centerpoint and discard the fragment entirely if the computed distance is
below a threshold radius.

If you need multiple holes, you need to pass a list of such centerpoints
and radii to the shader, e.g. in a Uniform array.

Christian




2015-07-23 10:49 GMT+02:00 Werner Modenbach werner.modenb...@texion.eu:

  Are you sure? I think this will leed to nothing being displayed. But I
 didn't verify it. It is just how I understand clipping.


 Am 23.07.2015 um 10:35 schrieb Christian Buchner:


 I think you only need to flip the clipping plane around (reverse the
 plane's normal vector) to get the desired effect.


 2015-07-22 21:28 GMT+02:00 Erik Hensens ehens...@hunter.com:

 Hi everyone!

 I have a geode that is just a quad rectangular surface and I need to be
 able to put circular holes in it. To better visualize what I'm talking
 about, think about a flat slice of Swiss cheese.

 I just discovered the ClipNode node and I figured I could achieve what I
 want by adding clip nodes that were the roughly circular, but ClipNode
 apparently behaves opposite to how I originally thought (I'm new to this).

 Is there a way to make ClipNode remove parts of the scene instead or
 keeping only those parts in the clip node region?

 Or, is there a better way to achieve this flat slice of Swiss cheese
 shape? Thanks very much in advance!

 Thank you!

 Cheers,
 Erik

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





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




 ___
 osg-users mailing 
 listosg-users@lists.openscenegraph.orghttp://lists.openscenegraph.org/listinfo.cgi/osg-users-openscenegraph.org


 --
 *TEXION Software Solutions, Rotter Bruch 26a, D-52068 Aachen*
 Phone: +49 241 475757-0
 Fax: +49 241 475757-29
 Web: http://texion.eu
 eMail: i...@texion.eu

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


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