PDA

View Full Version : changing spaces for wave deformations..??



Kirado
06-13-2007, 02:26 AM
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

http://img513.imageshack.us/img513/5532/bobbingobjectrb8.jpg

hseb
06-14-2007, 11:37 PM
You should ues a modelMatrix and not a modelView martrix to transform your object. Then apply sin wave offset to vertices coordinate. Finally, tranfsform to view space.

V = Vin*ModelMatrix;
V += waveOffsetForPosition(V);
Vout = V*ViewMatrix;
(then don't forget the projection...)

Else, you can try to express your sin wave deformation in view space. thus, you just need to transform object's vertices in view space and, then, apply sin wave offsets express in view space.

Kirado
06-15-2007, 12:45 AM
thanks for the help.. will try it out!! ;-)