01
May
08

Isosurface Raytracer Vertex Shader (Finally)

After much head-banging, hair-tearing, and forum posting, I’ve finally managed to work out a vertex shader to allow the raytraced isosurface blobs to be rotated. The code was surprisingly simple, in the end.

uniform vec4 M0;		// Rotation matrix column 0
uniform vec4 M1;		// Rotation matrix column 1
uniform vec4 M2;		// Rotation matrix column 2
uniform vec4 M3;		// Rotation matrix column 3
uniform vec3 Camera;	// Camera position (transformed to eye position)
varying vec4 eyePos;	// Eye position to fragment shader

void main()
{
	/*
	Transforms texture coordinates and position of virtual camera to be
	send to raytracing fragment shader code.
	*/

	// Assemble rotation matrix from vec4 inputs
	mat4 rotate = mat4(M0,M1,M2,M3);

	// Set tex to GL texture coordinates
	vec4 tex = gl_TextureMatrix[0] * gl_MultiTexCoord0;
	tex = tex * 2.0 - 1.0;
	// Rotate texture coordinates
	tex = rotate * tex;

	// Transform camera position with rotation matrix
	eyePos = vec4(Camera,1.0);
	// Rotate camera
	eyePos = rotate * eyePos;

	// Transform vertex by modelview and projection matrices
	gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;

	// Forward texture coordinates
	gl_TexCoord[0] = tex;
}

I don’t know if this is the best, or most efficient way of doing it, but it seems to work. I can now rotate the blobs around on any axis, and move the camera around. This method seems much simpler than the VVVV setup I was trying to emulate, too. Perhaps it’s not as flexible. I can’t tell though, as I’ve never managed to get the effect to run in VVVV under XP on my laptop. User Error, almost certainly…

Advertisements

1 Response to “Isosurface Raytracer Vertex Shader (Finally)”


  1. 1 yanomano
    May 1, 2008 at 8:13 pm

    congratulations !
    you are a winner !
    Can’t wait to test this setup !

    🙂

    yanomano


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s


Twitter

May 2008
M T W T F S S
« Apr   Jun »
 1234
567891011
12131415161718
19202122232425
262728293031  

Links

Blog Stats

  • 473,930 hits

%d bloggers like this: