02-20-2020, 01:28 PM
(This post was last modified: 02-20-2020, 01:36 PM by Brian Beuken.)
Here's a simple vertex light shader for you
and the frag for it
Code:
#version 100
uniform mat4 MVP; // A constant representing the combined model/view/projection matrix.
uniform mat4 MV; // A constant representing the combined model/view matrix.
uniform vec4 Ambient; // we will discard this for now, feel free to re add it.
attribute vec2 a_texCoord;
attribute vec4 a_Position; // Per-vertex position information we will pass in.
attribute vec3 a_Normal; // Per-vertex normal information we will pass in.
// These will be passed into the fragment shader.
varying vec2 v_texCoord;
varying float v_Colour;
void main()
{
vec4 u_LightPos = vec4(2.0, 5.0, -10.0, 1.0); // The position of a light in world space. should really be a uniform passed but for test, make it a fixed point in space.
vec4 modelViewVertex = vec4(MV * a_Position); // Transform the vertex into camera space.
vec4 modelViewNormal = vec4(MV * vec4( a_Normal,0.0)); // Transform the normal's orientation into camera space.
u_LightPos = MV * u_LightPos; // put light in camera space (this could be done on the CPU but leaving it here in case you want to do other things)
// Get a lighting direction vector from the light to the vertex.
vec4 lightVector = normalize(u_LightPos - modelViewVertex);
// pass the output values to the fragment
v_Colour = max(dot(modelViewNormal, lightVector), 0.05); // dont allow totally black ie 0.0
v_texCoord = a_texCoord;
gl_Position = MVP * a_Position; // usual positon calc
}
and the frag for it
Code:
#version 100
uniform sampler2D s_texture;
varying vec2 v_texCoord;
varying float v_Colour;
void main()
{
gl_FragColor = texture2D( s_texture, v_texCoord )*v_Colour;
gl_FragColor.a = 1.0;
}
Brian Beuken
Lecturer in Game Programming at Breda University of Applied Sciences.
Author of The Fundamentals of C/C++ Game Programming: Using Target-based Development on SBC's
Lecturer in Game Programming at Breda University of Applied Sciences.
Author of The Fundamentals of C/C++ Game Programming: Using Target-based Development on SBC's