PDA

View Full Version : Something very basic I've overlooked.



Dan M
08-17-2010, 05:15 AM
Hi there, and thanks for your patience...

So I'm feeling pretty stupid. Over the last few days I've looked over GLSL and felt ready to start implementing a shader that fades out the edges of my terrain. But I haven't been able to get even the most basic shader to look right, clearly I've missed something fundamental.

I've been using OpenGL Shader Builder on Mac OSX to preview and test out code. And after ambitiously going straight for glory with a whole solution have ended up back at the beginning with this:

Vertex:

varying float posX;

void main()
{
posX = gl_Position.x;
gl_FrontColor = gl_Color;
gl_TexCoord[0] = gl_MultiTexCoord0;
gl_Position = ftransform();
}


Fragment:

uniform sampler2D tex;
varying float posX;

void main()
{
if(posX >= 0.1 &amp;&amp; posX <= 0.9) gl_FragColor = vec4(0.0,0.0,0.0,1.0);
else gl_FragColor = gl_Color * texture2D(tex, gl_TexCoord[0].xy);
}


In my understanding any fragment between 0.1 and 0.9 along the x axis should be black and any other should have the colour of the texture. What actually happens is I get a bunch of black noise-like pixels, organised semi randomly, across the surface. When I move the mouse the black areas change to other locations, seemingly equally randomly. I doubt this is a problem with the Shader Builder!

Further, if I get this resolved in my head, would I be right in thinking the best way to fade out my terrain at the edges would be to set an intensity value based on the distance from the vertex to the nearest edge in the vertex shader and then use this value (interpolated) to multiply against the final colour in the fragment shader? For example, the intensity at the very edge would be zero and at, say, "5 in" would be 1.

Pre-thanks for any light you can shed,
Dan.

carsten neumann
08-17-2010, 07:57 AM
You are reading posX from gl_Position which is a vertex shader output variable. The vertex position input is called gl_Vertex.

Dan M
08-17-2010, 12:08 PM
Wow, I was secretly hoping it wasn't something so obvious after all!

Many thanks,
Dan.

ZbuffeR
08-17-2010, 02:13 PM
On the plus side, you discovered a cheap rand() implementation ...

Dan M
08-17-2010, 03:51 PM
I'm pretty sure I could have passed it off as some advanced cellular automaton.