I'm a noob and probably this is a completely silly question... Started using BGE two days ago...
I'm using GLSL to visualize a distance field while running in standalone player. I'm doing it using Vertex program and Shader program on the material of all mesh in the scene (all object with a mesh in fact). The shader program is applied on key pressed:
The transformation is performed through this module:
import bgl
import bge
vertex_shader = """
varying double DEPTH;
uniform double FARPLANE = 14.0; shader
void main() {
gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;
DEPTH = gl_Position.z / FARPLANE ;
}
"""
fragment_shader = """
varying double DEPTH;
void main() {
// far things appear white, near things black
float val = (1-DEPTH) * 2.35;
gl_FragColor.rgb = vec3(val, val, val);
}
"""
# Search all object with a mesh (only one mesh per object) and
# applies the code in the previous line
def enable_depth():
scene = bge.logic.getCurrentScene()
for object in scene.objects:
if object.meshes:
for mat in object.meshes[0].materials:
shader = mat.getShader()
if shader != None:
if not shader.isValid():
shader.setSource(vertex_shader, fragment_shader, 1)
vertex_reset = """
varying vec4 color;
void main() {
color = gl_MultiTexCoord0;
gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;
}
"""
fragment_reset = """
varying vec4 color;
void main() { gl_FragColor = color; }
"""
# this should reset the shader... I've done different test, reported
# in the last lines of this function
def disable_depth():
scene = bge.logic.getCurrentScene()
for object in scene.objects:
if object.meshes:
for mat in object.meshes[0].materials:
shader = mat.getShader()
if shader != None:
if not shader.isValid():
#shader.delSource()
#shader.setSource(vertex_reset, fragment_reset, 1)
#shader.setSource(vertex_shader, fragment_shader, 0)
How can I go back with this transformation? (I mean, from right to left) I tried with some sort of vertex/fragment program (report in code) but simply does not work and I am sure this is not the right way. Maybe I should have two materials for each object? But if a mesh has two materials, how do I change the active material in python? And finally, am I asking the right questions? :)
P.S. Only Blender Game Editor, no Blender Render!