Guest User

Untitled

a guest
Feb 9th, 2016
79
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. ############################# set the sampler
  2.  
  3. ############## main program
  4.  
  5. def main():
  6. # import bge
  7.     import bge
  8.  
  9.     # get the current controller
  10.     cont = bge.logic.getCurrentController()
  11.  
  12.     # get object that owns this controller
  13.     obj = cont.owner
  14.      
  15.     # Get a list of the mesh
  16.     meshList = obj.meshes
  17.  
  18.     # Get the first mesh on the object
  19.     mesh = meshList[0]
  20.      
  21.     # only one material on mesh
  22.     mat = mesh.materials[0]
  23.  
  24.     # Using Blender GLSL Materials or Blender Multitexture Materials?
  25.     if hasattr( mat, 'getMaterialIndex') == True:
  26.      
  27.     # get shader envelope
  28.         shader = mat.getShader()
  29.  
  30.         # did it get the shader envelope?
  31.         if shader != None:
  32.          
  33.             # set the source and use it
  34.             shader.setSource(VertexProgram,FragmentProgram, True)
  35.  
  36.             # "color" is a texture called by the Fragment Program just loaded
  37.             # 0 is the 1st Blender texture channel
  38.             shader.setSampler("color", 0)
  39.  
  40. ####### end of main program
  41.  
  42. VertexProgram =  """
  43.  
  44. void main()
  45. {
  46. // OpenGL glsl code
  47. gl_TexCoord[0] = gl_MultiTexCoord0;
  48. gl_Position = ftransform();
  49.  
  50. //Vertex snapping
  51. float4 snapToPixel = gl_ModelViewProjectionMatrix * gl_Vertex;
  52. float4 vertex =  snapToPixel;
  53. vertex.xyz = snapToPixel.xyz/snapToPixel.w;
  54. vertex.x = floor(160*vertex.x)/160;
  55. vertex.y = floor(120*vertex.y)/120;
  56. vertex.xyz*=snapToPixel.w;
  57. gl_Position = vertex;
  58.  
  59. }
  60. """
  61.  
  62. FragmentProgram = """
  63.  
  64. // texture passed from main part of program
  65. uniform sampler2D color;
  66.  
  67. void main()
  68. {
  69.  
  70. // Get texture from color map
  71. vec3 texture   = (texture2D(color, gl_TexCoord[0].st).xyz);        
  72.  
  73. // Use it
  74. gl_FragColor = vec4(texture, 1.0);
  75.  
  76. }
  77. """
  78.  
  79. ############  Run Program
  80.  
  81. main()
RAW Paste Data