• Advertisement
Sign in to follow this  

Shader Improvements

This topic is 3457 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi, I am curious to see if anyone has some input on this shader for modeling tree leafs. Patches of leafs are made up of textured billboards that always face the viewer. The only input to the shader is vertex attributes containing the coordinates for each corner of the billboard on the x-y plane. I wonder if anyone can see any performance improvements or has other comments. The shaders are integrated with Geist3D. Anyone interested can download the Geist3D IDE and experiment with the shaders via the GLSL source code editor The vertex shader positions the vertices of a billboard perpendicular to eye vector:
attribute vec2 gs_Attribute;

varying vec3 vLight;
varying vec3 vNormal;

void main()
	vec3 U = vec3(gl_ModelViewMatrixInverse[1][0],gl_ModelViewMatrixInverse[1][1],gl_ModelViewMatrixInverse[1][2]);
	vec3 V = vec3(gl_ModelViewMatrixInverse[0][0],gl_ModelViewMatrixInverse[0][1],gl_ModelViewMatrixInverse[0][2]);

	vNormal = vec3(gl_ModelViewMatrixInverse[3][0],gl_ModelViewMatrixInverse[3][1],gl_ModelViewMatrixInverse[3][2]);
	vLight = vec3(gl_ModelViewMatrixInverse * gl_LightSource[1].position);

	vec3 pos = gs_Attribute.x * V + gs_Attribute.y * U + gl_Vertex.xyz;
	gl_Position = gl_ModelViewProjectionMatrix * vec4(pos, 1.0);
	gl_TexCoord[0] = gl_MultiTexCoord0;

The fragment shader simply maps a texture containing a few dozen leafs onto the quad and then performs phong shading. I think this could be improved a lot by perturbing the normals. Not all leafs in the patch always face the viewer with the same angle. I am thinking of using a noise texture to perturb the normals up to +- 90 degrees:
uniform sampler2D Texture;

varying vec3 vLight;
varying vec3 vEye;
varying vec3 vNormal;

void main (void)
	vec4 color = texture2D (Texture, gl_TexCoord[0].st);

	vec3 light = normalize(vLight);
	vec3 normal = normalize(vNormal);
	float intensity = max(dot(light, normal), 0.0);

	color.rgb *= vec3(intensity);
	gl_FragColor = color;

[Edited by - jochenstier on August 3, 2008 9:32:08 AM]

Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement