[tuhopuu-devel] IRIX Tuhopuu gameengine crash
Carsten Wartmann
tuhopuu-devel@blender.org
Fri, 13 Aug 2004 13:11:59 +0200
Kester Maddock schrieb:
> Hi Carsten,
>
[...]
> Then you need to use Python to load a shader, bind the texture uniforms, bind
> your own uniforms.
>
> The Python reference has an example shader pair and the Python to load them.
> http://projects.blender.org/viewcvs/viewcvs.cgi/tuhopuu2/source/gameengine/PyDoc/KX_PolygonMaterial.py?rev=1.4&cvsroot=tuhopuu&content-type=text/vnd.viewcvs-markup
I tried this way, its all very new to me, so maybe completely nonsense:
---------------------------------------------------------------------------------
vss="""
B{vertex.vs}::
uniform mat4 MVI; // Inverse ModelView Matrix
attribute vec4 vertex_tangent;
varying vec3 lightvec;
varying vec3 viewvec;
void main ()
{
// Transform vertex to clip space
gl_Position = gl_ModelViewProjectionMatrix*gl_Vertex;
// Create Tangent Space Matrix
mat4 tangent;
tangent[1].xyz = cross(vertex_tangent.xyz,
gl_Normal.xyz)*vertex_tangent.w;
tangent[0].xyz = vertex_tangent.xyz;
tangent[2].xyz = gl_Normal;
tangent[3] = -gl_Vertex;
vec4 lightpos = MVI*gl_LightSource[0].position;
lightvec = (lightpos*tangent).xyz;
viewvec = (MVI[3]*tangent).xyz;
// UV Coordinates
gl_TexCoord[0] = gl_MultiTexCoord0;
// Vertex colours
gl_FrontColor = gl_Color;
}
"""
fss="""
B{fragment.fs}::
uniform sampler2D colourmap;
uniform sampler2D normap;
varying vec3 lightvec;
varying vec3 viewvec;
void main()
{
// Tangent space light vector
vec3 lv = normalize(lightvec);
// Tangent space view vector
vec3 vv = normalize(viewvec);
// Lookup normal
// Scale & bias normal
// Normalise normal
vec3 normal = normalize(2.0*texture2D(normap, gl_TexCoord[0]).xyz - 1.0);
// Compute diffuse lighting
float diffuse = dot(normal, lv);
vec3 refl = normalize(2.0*diffuse*normal - lv);
// Compute specular lighting
float spec = pow(max(dot(refl, vv), 0.0), gl_FrontMaterial.shininess);
// Lookup colour map
vec4 colour = texture2D(colourmap, gl_TexCoord[0]) *
gl_LightSource[0].diffuse * diffuse;
// light colour map
gl_FragColor = spec * gl_FrontMaterial.specular *
gl_LightSource[0].specular + colour;
// Pass alpha
gl_FragColor.a = colour.a;
}
"""
import GameLogic
mats = GameLogic.getCurrentController().getOwner().getMesh().materials
shaders = [(mats[0].VERTEX_SHADER, vss),
(mats[0].FRAGMENT_SHADER, fss)]
#print shaders
for mat in mats:
for shader in shaders:
mat.loadShader(shader[0], shader[1], shader[1])
# Set Textures...
mat.setUniform("colourmap", 0)
mat.setUniform("normap", 1)
# Set MV inverse matrix
mat.genUniform("MVI", 2)
# Bind tangent space
mat.bindAttribute("vertex_tangent", 1)
----------------------------------------------------------------------------------------
Am I on the right track?
Carsten.