Thread: Trying to multiply point with ModelvewProject...

1. Trying to multiply point with ModelvewProject...

I'm creating a shader for vehicle headlights and I want to alter the alpha value of each fragment based on it's distance from the origin. I have defined the origin as a point using an uniform vec3. When I attempt to calculate the distance, it seems that I am only getting 2d coordinates from the framebuffer.

I'm transforming the point here, in the vertex shader:
Code :
`lposit = vec3( gl_ModelViewProjectionMatrix * vec4(posit, 1.0) );`

I'm attempting to use the transformed point here, in my fragment shader.
Code :
`float dist = distance( lposit, gl_FragCoord.xyz);`

It seems that I'm not getting the expected results from glFragCoord. Thanks.

2. Re: Trying to multiply point with ModelvewProject...

The modelviewprojection matrix transforms a vertex/point to the canonical view volume [-1..1]x[-1..1]x[-1..1] while gl_FragCoord coordinates are in the range [0.5 .. window_width-0.5]x[0.5 .. window_height-0.5]x[0..1].

N.

3. Re: Trying to multiply point with ModelvewProject...

Well, I was hoping I could get the actual value of the point in 3D space, without transforming the point outside the shader and sending it in as a uniform. Is that not possible? I'm using Typhoon Lab's Shader Designer, and I'm not sure of the window-width or window-height. Thanks again.

4. Re: Trying to multiply point with ModelvewProject...

All I'm trying to say is that you want to calculate the distance between two points in the same coordinate system in order for it to have a meaningful value. Maybe this is what you're trying to do:

varying vec4 mvpVertex;
mvpVertex = gl_ModelViewProjectionMatrix*gl_Vertex;

float dist = distance( lposit, mvpVertex.xyz);

or something similar...

N.

5. Re: Trying to multiply point with ModelvewProject...

Yeah, maybe I'm going about this completely wrong. Here's a simple example of what I'm trying to accomplish.

I have a cube model at (0,0,0). I want to find each fragment's distance in 3d space from the center of the cube model, so I can multiply the light intensity by that value. The effect would be that the fragments that are bound within the sphere, would have low alpha, while the fragments outside the sphere would have no change at all. I'm trying to simulate a (fake)volumetric/fading headlight, by slapping a pyramid model on the front of the vehicles and fading out based on the distance from the origin.

6. Re: Trying to multiply point with ModelvewProject...

So you have a cube model...but what sphere are you talking about?
From what I hear, I think you meant to use gl_ModelViewMatrix instead of gl_ModelViewProjectionMatrix, no?

N.

7. Re: Trying to multiply point with ModelvewProject.

Well, I just mean a sphere that I define to test against the distance. It's called "Radius" in the fragment shader. I've posted the shaders, and if you run them, you can see that it seems to work only in the lower left hand corner of the the framebuffer, which is basically ortho coords (0,0). Man, I haven't had this much trouble for a while. Thanks for your time.

Code :
```uniform vec3 posit;
varying vec3 lposit;

void main()
{
lposit = vec3( gl_ModelViewMatrix * vec4(posit, 1.0) );

gl_Position = ftransform();

gl_TexCoord[0] = gl_MultiTexCoord0;
}```

Code :
```varying vec3 lposit;
uniform sampler2D Texture;

void main()
{

vec4 tColor = texture2D( Texture, gl_TexCoord[0].st );

float dist = distance( lposit, vec3(gl_FragCoord));

float alpha = 1.0;
if ( dist <= Radius )
{
}

tColor*=alpha;
gl_FragColor = vec4(tColor.rgb, alpha);

}```

8. Re: Trying to multiply point with ModelvewProject.

Maybe you can give these a try:

Code :
```uniform vec3 posit;
varying vec3 lposit;
varying vec3 mVertex;

void main()
{
lposit = vec3( gl_ModelViewMatrix * vec4(posit, 1.0) );
mVertex = vec3( gl_ModelViewMatrix * gl_Vertex );

gl_Position = ftransform();

gl_TexCoord[0] = gl_MultiTexCoord0;
}```

Code :
```varying vec3 lposit;
varying vec3 mVertex;
uniform sampler2D Texture;

void main()
{

vec4 tColor = texture2D( Texture, gl_TexCoord[0].st );

float dist = distance( lposit, mVertex);

float alpha = 1.0;
if ( dist <= Radius )
{
}

tColor*=alpha;
gl_FragColor = vec4(tColor.rgb, alpha);

}```

N.

9. Re: Trying to multiply point with ModelvewProject.

Hmm... Thanks. that produced the effect I was expecting, but I don't understand how. I thought I would have to check the distance of each fragment. It seems to be calculating the value for each fragment, but the value in the calculation is the vertex?

10. Re: Trying to multiply point with ModelvewProject.

Yes, but because mVertex is a varying it's being interpolated for every pixel. lposit is also a varying so it will be interpolated too, but because the value for lposit is the same regardless of the vertex position, it is a constant.

N.

Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•