1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281
|
# $Id: KX_PolygonMaterial.py,v 1.4 2005/03/25 10:33:39 kester Exp $
class KX_PolygonMaterial:
"""
This is the interface to materials in the game engine.
Materials define the render state to be applied to mesh objects.
Warning: Some of the methods/variables are CObjects. If you mix these up,
you will crash blender.
This example requires:
- PyOpenGL http://pyopengl.sourceforge.net/
- GLEWPy http://glewpy.sourceforge.net/
Example::
import GameLogic
import OpenGL
from OpenGL.GL import *
from OpenGL.GLU import *
import glew
from glew import *
glewInit()
vertex_shader = \"\"\"
void main(void)
{
gl_Position = ftransform();
}
\"\"\"
fragment_shader =\"\"\"
void main(void)
{
gl_FragColor = vec4(1.0, 0.0, 0.0, 1.0);
}
\"\"\"
class MyMaterial:
def __init__(self):
self.pass_no = 0
# Create a shader
self.m_program = glCreateProgramObjectARB()
# Compile the vertex shader
self.shader(GL_VERTEX_SHADER_ARB, (vertex_shader))
# Compile the fragment shader
self.shader(GL_FRAGMENT_SHADER_ARB, (fragment_shader))
# Link the shaders together
self.link()
def PrintInfoLog(self, tag, object):
\"\"\"
PrintInfoLog prints the GLSL compiler log
\"\"\"
print "Tag: def PrintGLError(self, tag = ""):
def PrintGLError(self, tag = ""):
\"\"\"
Prints the current GL error status
\"\"\"
if len(tag):
print tag
err = glGetError()
if err != GL_NO_ERROR:
print "GL Error: %s\\n"%(gluErrorString(err))
def shader(self, type, shaders):
\"\"\"
shader compiles a GLSL shader and attaches it to the current
program.
type should be either GL_VERTEX_SHADER_ARB or GL_FRAGMENT_SHADER_ARB
shaders should be a sequence of shader source to compile.
\"\"\"
# Create a shader object
shader_object = glCreateShaderObjectARB(type)
# Add the source code
glShaderSourceARB(shader_object, len(shaders), shaders)
# Compile the shader
glCompileShaderARB(shader_object)
# Print the compiler log
self.PrintInfoLog("vertex shader", shader_object)
# Check if compiled, and attach if it did
compiled = glGetObjectParameterivARB(shader_object, GL_OBJECT_COMPILE_STATUS_ARB)
if compiled:
glAttachObjectARB(self.m_program, shader_object)
# Delete the object (glAttachObjectARB makes a copy)
glDeleteObjectARB(shader_object)
# print the gl error log
self.PrintGLError()
def link(self):
\"\"\"
Links the shaders together.
\"\"\"
# clear error indicator
glGetError()
glLinkProgramARB(self.m_program)
self.PrintInfoLog("link", self.m_program)
linked = glGetObjectParameterivARB(self.m_program, GL_OBJECT_LINK_STATUS_ARB)
if not linked:
print "Shader failed to link"
return
glValidateProgramARB(self.m_program)
valid = glGetObjectParameterivARB(self.m_program, GL_OBJECT_VALIDATE_STATUS_ARB)
if not valid:
print "Shader failed to validate"
return
def activate(self, rasty, cachingInfo, mat):
self.pass_no+=1
if (self.pass_no == 1):
glDisable(GL_COLOR_MATERIAL)
glUseProgramObjectARB(self.m_program)
return True
glEnable(GL_COLOR_MATERIAL)
glUseProgramObjectARB(0)
self.pass_no = 0
return False
obj = GameLogic.getCurrentController().getOwner()
mesh = obj.getMesh(0)
for mat in mesh.materials:
mat.setCustomMaterial(MyMaterial())
print mat.texture
@ivar texture: Texture name
@type texture: string (read only)
@ivar gl_texture: OpenGL texture handle (eg for glBindTexture(GL_TEXTURE_2D, gl_texture)
@type gl_texture: integer (read only)
@ivar material: Material name
@type material: string (read only)
@ivar tface: Texture face properties
@type tface: CObject (read only)
@ivar tile: Texture is tiling
@type tile: boolean
@ivar tilexrep: Number of tile repetitions in x direction.
@type tilexrep: integer
@ivar tileyrep: Number of tile repetitions in y direction.
@type tileyrep: integer
@ivar drawingmode: Drawing mode for the material.
- 2 (drawingmode & 4) Textured
- 4 (drawingmode & 16) Light
- 14 (drawingmode & 16384) 3d Polygon Text
@type drawingmode: bitfield
@ivar transparent: This material is transparent. All meshes with this
material will be rendered after non transparent meshes from back
to front.
@type transparent: boolean
@ivar zsort: Transparent polygons in meshes with this material will be sorted back to
front before rendering.
Non-Transparent polygons will be sorted front to back before rendering.
@type zsort: boolean
@ivar lightlayer: Light layers this material affects.
@type lightlayer: bitfield.
@ivar triangle: Mesh data with this material is triangles. It's probably not safe to change this.
@type triangle: boolean
@ivar diffuse: The diffuse colour of the material. black = [0.0, 0.0, 0.0] white = [1.0, 1.0, 1.0]
@type diffuse: list [r, g, b]
@ivar specular: The specular colour of the material. black = [0.0, 0.0, 0.0] white = [1.0, 1.0, 1.0]
@type specular: list [r, g, b]
@ivar shininess: The shininess (specular exponent) of the material. 0.0 <= shininess <= 128.0
@type shininess: float
@ivar specularity: The amount of specular of the material. 0.0 <= specularity <= 1.0
@type specularity: float
"""
def updateTexture(tface, rasty):
"""
Updates a realtime animation.
@param tface: Texture face (eg mat.tface)
@type tface: CObject
@param rasty: Rasterizer
@type rasty: CObject
"""
def setTexture(tface):
"""
Sets texture render state.
Example::
mat.setTexture(mat.tface)
@param tface: Texture face
@type tface: CObject
"""
def activate(rasty, cachingInfo):
"""
Sets material parameters for this object for rendering.
Material Parameters set:
1. Texture
2. Backface culling
3. Line drawing
4. Specular Colour
5. Shininess
6. Diffuse Colour
7. Polygon Offset.
@param rasty: Rasterizer instance.
@type rasty: CObject
@param cachingInfo: Material cache instance.
@type cachingInfo: CObject
"""
def setCustomMaterial(material):
"""
Sets the material state setup object.
Using this method, you can extend or completely replace the gameengine material
to do your own advanced multipass effects.
Use this method to register your material class. Instead of the normal material,
your class's activate method will be called just before rendering the mesh.
This should setup the texture, material, and any other state you would like.
It should return True to render the mesh, or False if you are finished. You should
clean up any state Blender does not set before returning False.
Activate Method Definition::
def activate(self, rasty, cachingInfo, material):
Example::
class PyMaterial:
def __init__(self):
self.pass_no = -1
def activate(self, rasty, cachingInfo, material):
# Activate the material here.
#
# The activate method will be called until it returns False.
# Every time the activate method returns True the mesh will
# be rendered.
#
# rasty is a CObject for passing to material.updateTexture()
# and material.activate()
# cachingInfo is a CObject for passing to material.activate()
# material is the KX_PolygonMaterial instance this material
# was added to
# default material properties:
self.pass_no += 1
if self.pass_no == 0:
material.activate(rasty, cachingInfo)
# Return True to do this pass
return True
# clean up and return False to finish.
self.pass_no = -1
return False
# Create a new Python Material and pass it to the renderer.
mat.setCustomMaterial(PyMaterial())
@param material: The material object.
@type material: instance
"""
|