OpenSceneGraph Forum Forum Index OpenSceneGraph Forum
Official forum which mirrors the existent OSG mailing lists. Messages posted here are forwarded to the mailing list and vice versa.
 
   FAQFAQ    SearchSearch    MemberlistMemberlist    RulesRules    UsergroupsUsergroups    RegisterRegister 
 Mail2Forum SettingsMail2Forum Settings  ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 
   AlbumAlbum  OpenSceneGraph IRC ChatOpenSceneGraph IRC Chat   SmartFeedSmartFeed 

Image from shader


 
Post new topic   Reply to topic    OpenSceneGraph Forum Forum Index -> General
View previous topic :: View next topic  
Author Message
lucasamparo (Lucas Amparo)
Newbie


Joined: 04 Jun 2019
Posts: 11

PostPosted: Thu Oct 03, 2019 5:46 pm    Post subject:
Image from shader
Reply with quote

Hi everyone,

I'm trying to retrieve an image from a shader using OpenGL in OSG/OSGOcean.

I can retrieve the depth from the simulation but can't access properly the color from each vertex.

Using this code on *.vert

Code:

out vec3 pos
out vec4 color

void main() {
pos = (gl_ModelViewMatrix * gl_Vertex).xyz;
color = gl_Color;
}


and this in *.frag

Code:

in vec3 pos;
in vec4 color;

uniform float farPlane;

void main() {
float linearDepth = length(pos);

float dist_ratio = linearDepth / farPlane;

gl_FragDepth = dist_ratio;
gl_FragColor = color;
}


I'm achieving this image (image2.png on attachment) when I'm trying to produce that image (image1.png on attachment).

Could someone bring me to light? I'm very lost on this...

Thank you!

Cheers,
Lucas
Back to top
View user's profile Send private message
lucasamparo (Lucas Amparo)
Newbie


Joined: 04 Jun 2019
Posts: 11

PostPosted: Thu Oct 03, 2019 5:49 pm    Post subject:
Reply with quote

The depth works properly...
Back to top
View user's profile Send private message
lucasamparo (Lucas Amparo)
Newbie


Joined: 04 Jun 2019
Posts: 11

PostPosted: Thu Oct 03, 2019 5:53 pm    Post subject:
Reply with quote

An information:
The image1.png was produced when I make something wrong on my shader and the compilation brokes.

I believe which the default shader, from OSG take their place and rendering this image.
Back to top
View user's profile Send private message
gwaldron (Glenn Waldron)
Appreciator


Joined: 25 Aug 2011
Posts: 252

PostPosted: Thu Oct 03, 2019 6:01 pm    Post subject:
Image from shader
Reply with quote

I think you want:
gl_FragColor = color;



Or, if you'd rather use the built-ins, get rid of your "out vec4 color" and write to gl_FrontColor in the vertex shader:
gl_FrontColor = gl_Color;


And read from gl_Color in the fragment shader:
gl_FragColor = gl_Color;
Glenn Waldron / osgEarth






On Thu, Oct 3, 2019 at 1:54 PM Lucas Amparo < (
Only registered users can see emails on this board!
Get registred or enter the forums!
)> wrote:

Quote:
Hi everyone,

I'm trying to retrieve an image from a shader using OpenGL in OSG/OSGOcean.

I can retrieve the depth from the simulation but can't access properly the color from each vertex.

Using this code on *.vert


Code:

out vec3 pos
out vec4 color

void main() {
pos = (gl_ModelViewMatrix * gl_Vertex).xyz;
color = gl_Color;
}




and this in *.frag


Code:

in vec3 pos;
in vec4 color;

uniform float farPlane;

void main() {
float linearDepth = length(pos);

float dist_ratio = linearDepth / farPlane;

gl_FragDepth = dist_ratio;
gl_FragColor = gl_FrontColor;
}




I'm achieving this image (image2.png on attachment) when I'm trying to produce that image (image1.png on attachment).

Could someone bring me to light? I'm very lost on this...

Thank you!

Cheers,
Lucas

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




Attachments:
http://forum.openscenegraph.org//files/image1_169.png
http://forum.openscenegraph.org//files/image2_731.png


_______________________________________________
osg-users mailing list
(
Only registered users can see emails on this board!
Get registred or enter the forums!
)
http://lists.openscenegraph.org/listinfo.cgi/osg-users-openscenegraph.org


------------------
Post generated by Mail2Forum
Back to top
View user's profile Send private message
lucasamparo (Lucas Amparo)
Newbie


Joined: 04 Jun 2019
Posts: 11

PostPosted: Thu Oct 03, 2019 6:08 pm    Post subject:
Reply with quote

Hi, thanks for the answer.

changing

color = gl_Color to gl_FrontColor = gl_Color

and

gl_FragColor = color to gl_FragColor = gl_Color

same results Sad
Back to top
View user's profile Send private message
AnyOldName3 (Chris Djali)
User


Joined: 08 Sep 2017
Posts: 71

PostPosted: Fri Oct 04, 2019 12:06 am    Post subject:
Reply with quote

Hi,

I'm pretty sure you're not using your shaders at all. You're not setting gl_Position in the vertex shader, so it's not going to compile, and OSG will fall back to either its basic shader or fixed-function mode depending on how you've set it up. You should be seeing errors printed to the console when you run your application.

Cheers,
Chris
Back to top
View user's profile Send private message
lucasamparo (Lucas Amparo)
Newbie


Joined: 04 Jun 2019
Posts: 11

PostPosted: Fri Oct 04, 2019 11:40 am    Post subject:
Reply with quote

AnyOldName3 wrote:
Hi,

I'm pretty sure you're not using your shaders at all. You're not setting gl_Position in the vertex shader, so it's not going to compile, and OSG will fall back to either its basic shader or fixed-function mode depending on how you've set it up. You should be seeing errors printed to the console when you run your application.

Cheers,
Chris


Hi, thanks for your answer.
The shader works for my purpose.
Before, just a depthmap from simulation.

The console don't show any error, just deprecated warnings.

Do you believe if I set gl_Position, the colors could be solved?
Back to top
View user's profile Send private message
lucasamparo (Lucas Amparo)
Newbie


Joined: 04 Jun 2019
Posts: 11

PostPosted: Fri Oct 04, 2019 1:07 pm    Post subject:
Reply with quote

Guys,

It's possible to take a simples "screenshot" from an osg::Viewer?

Thank you!

Cheers,
Lucas
Back to top
View user's profile Send private message
AnyOldName3 (Chris Djali)
User


Joined: 08 Sep 2017
Posts: 71

PostPosted: Fri Oct 04, 2019 4:41 pm    Post subject:
Reply with quote

Hi,

If you're not seeing any errors in the console, I don't think you've added your shader to the scene properly. You have to assign something to gl_Position otherwise the rasteriser has no idea where your vertex is, so doesn't know which pixels any triangles cover, and nothing will get drawn.

Also, I reckon two things that you've got wrong that stop you from getting image2 are:
  • image2 looks like it has some basic lighting, hence why the cylinder's colour varies from left to right. You're not doing any lighting, just forwarding the vertex colour.
  • image2 looks like it has a texture, at least on the brown bit. You're not sampling from a texture anywhere.


I'd start with these shaders generated by 3DLabs GLSL ShaderGen and comment out things or add things until you get the result you're after:

Vertex:
Code:

/*******************************************************
*  Fixed.vert Fixed Function Equivalent Vertex Shader  *
*        Automatically Generated by GLSL ShaderGen     *
*          https://github.com/mojocorp/ShaderGen       *
*******************************************************/
vec4 Ambient;
vec4 Diffuse;
vec4 Specular;


void pointLight(in int i, in vec3 normal, in vec3 eye, in vec3 ecPosition3)
{
   float nDotVP;       // normal . light direction
   float nDotHV;       // normal . light half vector
   float pf;           // power factor
   float attenuation;  // computed attenuation factor
   float d;            // distance from surface to light source
   vec3  VP;           // direction from surface to light position
   vec3  halfVector;   // direction of maximum highlights

   // Compute vector from surface to light position
   VP = vec3 (gl_LightSource[i].position) - ecPosition3;

   // Compute distance between surface and light position
   d = length(VP);

   // Normalize the vector from surface to light position
   VP = normalize(VP);

   // Compute attenuation
   attenuation = 1.0 / (gl_LightSource[i].constantAttenuation +
                        gl_LightSource[i].linearAttenuation * d +
                        gl_LightSource[i].quadraticAttenuation * d * d);

   halfVector = normalize(VP + eye);

   nDotVP = max(0.0, dot(normal, VP));
   nDotHV = max(0.0, dot(normal, halfVector));

   if (nDotVP == 0.0)
   {
       pf = 0.0;
   }
   else
   {
       pf = pow(nDotHV, gl_FrontMaterial.shininess);

   }
   Ambient  += gl_LightSource[i].ambient * attenuation;
   Diffuse  += gl_LightSource[i].diffuse * nDotVP * attenuation;
   Specular += gl_LightSource[i].specular * pf * attenuation;
}

vec3 fnormal(void)
{
    //Compute the normal
    vec3 normal = gl_NormalMatrix * gl_Normal;
    normal = normalize(normal);
    return normal;
}

void ftexgen(in vec3 normal, in vec4 ecPosition)
{

    gl_TexCoord[0] = gl_MultiTexCoord0;
}

void flight(in vec3 normal, in vec4 ecPosition, float alphaFade)
{
    vec4 color;
    vec3 ecPosition3;
    vec3 eye;

    ecPosition3 = (vec3 (ecPosition)) / ecPosition.w;
    eye = vec3 (0.0, 0.0, 1.0);

    // Clear the light intensity accumulators
    Ambient  = vec4 (0.0);
    Diffuse  = vec4 (0.0);
    Specular = vec4 (0.0);

    pointLight(0, normal, eye, ecPosition3);

    color = gl_FrontLightModelProduct.sceneColor +
            Ambient  * gl_FrontMaterial.ambient +
            Diffuse  * gl_FrontMaterial.diffuse;
    color += Specular * gl_FrontMaterial.specular;
    color = clamp( color, 0.0, 1.0 );
    gl_FrontColor = color;

    gl_FrontColor.a *= alphaFade;
}


void main (void)
{
    vec3  transformedNormal;
    float alphaFade = 1.0;

    // Eye-coordinate position of vertex, needed in various calculations
    vec4 ecPosition = gl_ModelViewMatrix * gl_Vertex;

    // Do fixed functionality vertex transform
    gl_Position = gl_ProjectionMatrix * gl_ModelViewMatrix * gl_Vertex;
    transformedNormal = fnormal();
    flight(transformedNormal, ecPosition, alphaFade);
    ftexgen(transformedNormal, ecPosition);
}


Fragment:
Code:

/*******************************************************
* Fixed.frag Fixed Function Equivalent Fragment Shader *
*        Automatically Generated by GLSL ShaderGen     *
*          https://github.com/mojocorp/ShaderGen       *
*******************************************************/
uniform sampler2D texUnit0;

void main (void)
{
    vec4 color;

    color = gl_Color;

    color *= texture2D(texUnit0, gl_TexCoord[0].xy);

    gl_FragColor = color;
}



Cheers,
Chris[/code]
Back to top
View user's profile Send private message
lucasamparo (Lucas Amparo)
Newbie


Joined: 04 Jun 2019
Posts: 11

PostPosted: Fri Oct 04, 2019 6:03 pm    Post subject:
Reply with quote

Hi, Chris. Thanks for the answer.

I've inserted "gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;" in my vertex shader before... and forgot to mention this here. Sorry.

Thanks for the code but, with my poor knowledge about OSG and OpenGL I need to pass a texture for fragment shader, right? In the texUnit0?

My simulation was a little bit complex (and confuse), not made by me. I'm just adding a feature to a finished plugin, made by the Older-Me haha

All the texture, I believe are passed by the OSGOcean, because when I load my camera with the osg::Node without my shaders, it's works properly.
But, when I load my node with shader, "brokes"...

Thank you!

Cheers,
Lucas
Back to top
View user's profile Send private message
AnyOldName3 (Chris Djali)
User


Joined: 08 Sep 2017
Posts: 71

PostPosted: Fri Oct 04, 2019 6:09 pm    Post subject:
Reply with quote

Hi,

OSGOcean is probably setting up the necessary fixed-function stuff to put its texture in texture unit zero, but you'll need to tell the fragment shader that its texUnit0 uniform refers to that texture unit. It should be enough just to add a uniform to the same stateset as you're adding your shader that sets texUnit0 to 0 e.g.
Code:
stateSet->addUniform(new osg::Uniform("texUnit0", 0));


Cheers,
Chris
Back to top
View user's profile Send private message
lucasamparo (Lucas Amparo)
Newbie


Joined: 04 Jun 2019
Posts: 11

PostPosted: Mon Oct 07, 2019 12:14 pm    Post subject:
Reply with quote

AnyOldName3 wrote:
Hi,

OSGOcean is probably setting up the necessary fixed-function stuff to put its texture in texture unit zero, but you'll need to tell the fragment shader that its texUnit0 uniform refers to that texture unit. It should be enough just to add a uniform to the same stateset as you're adding your shader that sets texUnit0 to 0 e.g.
Code:
stateSet->addUniform(new osg::Uniform("texUnit0", 0));


Cheers,
Chris


Chris, do you know some way to "debug" the variables to find the right name used on this texture?
I'll try your approach and come back with the result.

Thanks a lot.
Back to top
View user's profile Send private message
AnyOldName3 (Chris Djali)
User


Joined: 08 Sep 2017
Posts: 71

PostPosted: Mon Oct 07, 2019 1:18 pm    Post subject:
Reply with quote

Hi,

The name texUnit0 comes from the shader, which you're providing. The texture unit index (which my example snippet sets to 0) comes from your scene graph, which you said came from osgOcean, and will almost certainly be 0 for a diffuse map. If I was trying to check that, I'd run the application through APITrace and see which units have textures bound to them during the draw calls for the meshes I'm interested in, but that might not be the simplest approach.

Cheers,
Chris
Back to top
View user's profile Send private message
lucasamparo (Lucas Amparo)
Newbie


Joined: 04 Jun 2019
Posts: 11

PostPosted: Mon Oct 07, 2019 1:38 pm    Post subject:
Reply with quote

AnyOldName3 wrote:
Hi,

The name texUnit0 comes from the shader, which you're providing. The texture unit index (which my example snippet sets to 0) comes from your scene graph, which you said came from osgOcean, and will almost certainly be 0 for a diffuse map. If I was trying to check that, I'd run the application through APITrace and see which units have textures bound to them during the draw calls for the meshes I'm interested in, but that might not be the simplest approach.

Cheers,
Chris


Chris, I've achieved an improvement, using your approach.

I've looking for a variable on osgocean and find uTextureMap.

But, some textures on the model wasn't loaded yet. It isn't correct yet but is a little step forward. Thanks a lot.
Back to top
View user's profile Send private message
lucasamparo (Lucas Amparo)
Newbie


Joined: 04 Jun 2019
Posts: 11

PostPosted: Mon Oct 07, 2019 7:36 pm    Post subject:
Reply with quote

Guys,

Thanks a lot for everyone.

To solve my problem, I need to add

Code:

...
// In the cpp OSG file
ss->addUniform(new osg::Uniform("uTextureMap", 0));
...


Code:

...
// On the vertex shader
gl_TexCoord[0] = gl_MultiTexCoord0;
...


Code:

...
// And this on the fragment shader
color_data = texture2D( uTextureMap, gl_TexCoord[0].st );
...


Now I'm facing problems to render the light on the image hehe
But it's scenes from the next chapter.

Thank you all, again.

Cheers,
Lucas
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    OpenSceneGraph Forum Forum Index -> General All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You cannot download files in this forum

Similar Topics
Topic Author Forum Replies Posted
No new posts Render normal vectors to image saedrna1 General 0 Mon Nov 11, 2019 8:43 am View latest post
No new posts Render normal vectors to image saedrna1 General 7 Mon Nov 11, 2019 8:42 am View latest post
No new posts What is wrong when add hdr effects sh... mirro General 2 Thu Oct 24, 2019 12:45 pm View latest post
No new posts [OSG 3.6.3 Core Profile GL 3+] Weird ... johansson_gbg General 2 Thu Sep 12, 2019 1:17 pm View latest post
No new posts problem about reconstructing world co... swaggydong General 0 Sun Jun 23, 2019 8:26 am View latest post


Board Security Anti Bot Question MOD - phpBB MOD against Spam Bots
Powered by phpBB © 2001, 2005 phpBB Group
Protected by Anti-Spam ACP