chris_c
Posts: 156
Joined: Sun May 06, 2012 10:23 am

GLSL for tutorial framework

Wed Jun 06, 2012 11:48 am

after putting together a GLES 1.1 framework, I did a bit of a U turn and decided a should really join the rest of you in this century and learn GLES 2.0 - and you know what despite the comparable simplicity of 1.1 I think 2.0 has won me over...!

My reasons for putting together a C framework was both for rapid prototyping - clone the framework as a quick starting point - and also a basis for a set of tutorials for teaching.

Its a lightweight framework for teaching, something that can be entirely described within a few tutorials not a do everything for everyone engine

I have a simple shader with some kinda hinkey fragment lighting and I was wondering if people could suggest improvements - it looks quite good and its simple enough it can be quite easily explained. Its no good having a fantastically accurate light model with all sorts of features if it's not simple and can't be easily explained in a portion of a tutorial

anyhow here's my first stab at a simple lit texture shader

vert shader

Code: Select all

attribute vec3		vertex_attrib;
attribute vec2		uv_attrib;
attribute vec3		norm_attrib;

uniform mat4		mvp_uniform;    // combined model view projection
uniform mat4		mv_uniform;      // combined model view

varying vec2		v_frag_uv;
varying vec3		v_Position;
varying vec3		v_Normal;

void main(void) {

	v_frag_uv = uv_attrib;

    v_Position = vec3(mv_uniform * vec4(vertex_attrib,0));
    v_Normal = vec3(mv_uniform * vec4(norm_attrib, 0.0));
    gl_Position = mvp_uniform * vec4(vertex_attrib,1);

}
frag shader

Code: Select all

uniform sampler2D		texture_uniform;

vec3 u_LightPos=vec3(0,1,2);       // The position of the light in eye space.
									// TODO should probably be a uniform
varying vec3 v_Position;
varying vec3 v_Normal;
varying vec2 v_frag_uv; 

void main() 
{
    vec3 lightVector = normalize(u_LightPos - v_Position);
    float diffuse = max(dot(v_Normal, lightVector), 0.1);
 
//    float distance = length(u_LightPos - v_Position);
//    diffuse = diffuse * (1.0 / (1.0 + (0.25 * distance * distance)));
 
    gl_FragColor = texture2D(texture_uniform, v_frag_uv) * diffuse;
}

Return to “Other programming languages”