Vertex program / shaders

BlitzMax Forums/BlitzMax OpenGL Programming/Vertex program / shaders

Hi,

Sure thing....

Important: BACKUP YOUR WORK BEFORE USING OTHER PEOPLES CODE!!

Note: I use 2 space TAB size when coding (just warning you! :)

Here's my 'ongoing' GLSL Module.
http://www.tomspeed.com/glsl.bmx

GLSL Shaders come in 2 flavours, Low level & High Level. Low level shader source kinda looks like ASM, where as High level looks more like C (more readable!). My GLSL system relies on shaders being High level and won't (untested but pretty sure) work if you give it Low level shader source, because the 2 are applied differently in GL.


Here's a brief explanation on how I implement it

'Each surface in my Mesh entitys
'can have their own 'tShaderObject'
'i.e: myMesh.surfShader


'cubeSurf is a surface on a mesh (a cluster of Tris)

cubeSurf.surfShader = tProgramObject.Create()

Local vertShader:tShaderObject = CreateVertShader("Lattice.vert")
Local fragShader:tShaderObject = CreateFragShader("Lattice.frag")

cubeSurf.surfShader.AttachVertShader(vertShader)
cubeSurf.surfShader.AttachFragShader(fragShader)

'Yes, we want to use the shader when rendering
cubeSurf.useShader = True



cubeSurf.setShaderVars = ToonShaderVars

'^^
'A cool feature in blitzmax, setShaderVars is a Function Pointer
'In this case, I use them to point the surface shader to a user
'defined 'callback' Function to change variable settings within
'the shader!
'
'example, in my surface process I do something like this
'
'			If surf.useShader 'use the shader?
'				surf.EnableShader() ' enable this surfaces shader
'				surf.setShaderVars(surf.surfShader) ' set the shader Variables (call LatticeShaderVars())
'			End If
'
'     DrawSurfaceGeometry()
'
'     disable shader e.t.c
'
'So the function below would be called automaticaly, and the user
'can tweak shader variables easily this way!


Function LatticeShaderVars(s:tProgramObject)
	'Here's how you tweak shader variables!
	s.SetUF3("LightPosition",1.0,4.0,3.0)
	s.SetUF3("LightColor",1.0,0.8,0.9)	
	
	s.SetUF3("EyePosition",myCam.GetX(3), myCam.GetY(3), myCam.GetZ(3))

	s.SetUF3("AmbientColor",0.2,0.2,0.2)		
	s.SetUF3("Specular",0.2,0.2,0.2)
	s.SetUF1("Kd",0.8)	

	s.SetUF2("Scale",10.0,10.0)	
	s.SetUF2("Threshold",0.13, 0.13)
	s.SetUF3("SurfaceColor",1.0,0.0,1.0)
End Function


Any Q's ? :)
Tom