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