[osg-users] Image from shader

Chris Djali krizdjali at gmail.com
Fri Oct 4 09:42:04 PDT 2019


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]

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







More information about the osg-users mailing list