Hi all… I’m working on a basic ocean simulation using GLSL. I’ve written a simple shader that deforms a plane using a sine wave…
then to fake a floating object I use the same sin wave to move all the vertexes of the object up in the Z axis…
now this is all fine and hunky dory… but I want to apply the wave deformation and movement to the obejcts using the world co-ordinate space NOT the the local/object space… I know that it involves multiply various martixes but I’m not sure which ones…
here is the vertex shader and fragment shader of the floating object… it has the waved deformation commented out.
VertexShader = """
uniform float time;
varying vec2 texcoords;
varying vec3 lvec[3];
varying vec3 view_vector;
varying vec3 normal;
varying vec4 vcol;
void main(){
vec3 vert = (gl_ModelViewMatrix * gl_Vertex).xyz;
lvec[0]= (gl_LightSource[0].position).xyz - vert;
normal = gl_NormalMatrix *gl_Normal;
view_vector = (vert);
vcol = gl_Color;
// texture info
float offset =(time)/40;
gl_TexCoord[0].t = gl_MultiTexCoord0.t; //+ offset;
gl_TexCoord[0].s = gl_MultiTexCoord0.s; //+ offset;
//waves
vec4 v = gl_Vertex;
//v.z = sin( v.y*4(smaller = longer waves). + time(*this to increase the speed )/10.0(smaller = bigger depth/amplitude);
// wave movement of vertexes in the Z axis
v.z += sin(12.0 + time )/2.0;
//v.z += sin( v.x*12. + time )/20; code for wave deformation
gl_Position = gl_ModelViewProjectionMatrix * v;
}
"""
FragmentShader = """
uniform sampler2D color;
const float AmbF = %f;
const vec4 AmbG = vec4(%f,%f,%f,%f);
varying vec3 lvec[3];
varying vec2 texcoords;
varying vec3 view_vector;
varying vec3 normal;
varying vec4 vcol;
void main(){
vec3 l,v,n;
vec4 diff0,amb;
float diffuse0;
float spec0;
vec4 texture = texture2D(color,texcoords);
// ambient color * factor
amb= AmbG*AmbF;
const float specular_power = 100.0; // > sharp spec
// lighting ...
n= normalize(normal);
v= normalize(view_vector);
l = normalize(lvec[0]);
diffuse0 = max(dot(l,n), 0.0);
diff0 = gl_LightSource[0].diffuse; // lamp color in blender menu
spec0 = pow(max(dot(reflect(v, n), l),0.0),specular_power);
// (texture * vertexcolor)+ ( ambient color + light1 + light2 + light3 ) - 0.5
texture *= vcol;
gl_FragColor = texture+((amb) + (diffuse0*diff0+spec0*diff0))-0.5;
}
and here is a pic of the waves deforming the plane and making a wire frame box move/float inside of the blender game engine.
So if anyone knows how I can make the mesh deform in world space pls. let me know. thanks