source: src/materials/Textured.pym @ 1246:b7467c84b0a3

Revision 1246:b7467c84b0a3, 5.9 KB checked in by Arc Riley <arcriley@…>, 6 years ago (diff)

Ticket #955 :

  • this is sort of a joke, RainbowBlocks? shows TSLVs via normalisation cubemap
  • not really a joke, as it shows how to build spiffy new materials!
Line 
1# PySoy's materials.Textured class
2#
3# Copyright (C) 2006,2007,2008 PySoy Group
4#
5#  This program is free software; you can redistribute it and/or modify
6#  it under the terms of the GNU Affero General Public License as published
7#  by the Free Software Foundation, either version 3 of the License, or
8#  (at your option) any later version.
9#
10#  This program is distributed in the hope that it will be useful,
11#  but WITHOUT ANY WARRANTY; without even the implied warranty of
12#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13#  GNU Affero General Public License for more details.
14#
15#  You should have received a copy of the GNU Affero General Public License
16#  along with this program; if not, see http://www.gnu.org/licenses
17#
18# $Id: Textured.pym 1292 2008-05-20 07:30:51Z ArcRiley $
19
20cdef class Textured(Material) :
21  '''soy.materials.Textured
22
23    Textured materials support one or more basic textures in addition to the
24    standard Material colors (ambient, diffuse, specular, emission) which
25    are multiplied by the texture color to provide light-based shading.
26   
27    Currently .colormap and .glowmap are supported.
28  '''
29
30  ############################################################################
31  #
32  # Python functions
33  #
34
35  def __cinit__(self,
36                soy.colors.Color ambient=None,
37                soy.colors.Color diffuse=None,
38                soy.colors.Color specular=None,
39                soy.colors.Color emission=None,
40                float shininess=0.5,
41                soy.textures.Texture colormap=None,
42                soy.textures.Texture glowmap=None,
43                *args, **keywords) :
44    ######################################
45    #
46    # store the texture arguments
47    #
48    self._colormap = colormap
49    self._glowmap  = glowmap
50    #
51    ######################################
52
53
54  ############################################################################
55  #
56  # Properties
57  #
58
59  property colormap :
60    '''soy.materials.Textured.colormap
61
62    When a texture is assigned to this property a colormap texture is rendered.
63
64    In OpenGL terms, this texture is set GL_MODULATE to the lit material color
65    which provides it's shading.  Textures with an alpha component will be
66    translucent to the material's lit color, so to achieve actual translucency
67    this material's colors (ambient/diffuse) must also be translucent.
68
69    Default is None (disabled).
70    '''
71   
72    def __get__(self) :
73      return self._colormap
74
75    def __set__(self, soy.textures.Texture _value) :
76      self._colormap = _value
77
78    def __del__(self) :
79      self._colormap = None
80
81
82  property glowmap :
83    '''soy.materials.Textured.glowmap
84
85    When a texture is assigned to this property a glowmap texture is rendered.
86
87    In OpenGL terms, this texture is set GL_ADD.
88
89    Default is None (disabled).
90    '''
91   
92    def __get__(self) :
93      return self._glowmap
94
95    def __set__(self, soy.textures.Texture _value) :
96      self._glowmap = _value
97
98    def __del__(self) :
99      self._glowmap = None
100
101
102  ############################################################################
103  #
104  # General C functions
105  #
106  cdef int _isTransparent(self) nogil :
107    #
108    # textures are only translucent to the colors, so if either lack
109    # translucency this check will return False
110    #
111    if self._colormap is not None and self._colormap._chans %2 == 1 :
112      return 0
113    if self._ambient._rgba[3] != 1.0 :
114      return 1
115    if self._diffuse._rgba[3] != 1.0 :
116      return 1
117    return 0
118
119
120  ############################################################################
121  #
122  # WindowLoop functions
123  #
124
125  cdef int _render(self, int _pass, float* _texcoords, float* _tslvs) nogil :
126    cdef int _i, _unit
127    _unit = 0
128    ######################################
129    #
130    # Textured materials are 1-pass
131    #
132    if _pass == 0 :
133      ####################################
134      #
135      # number of shades, currently just 0 or 1
136      #
137      if self._shades == 0 :
138        gl.glShadeModel(gl.GL_SMOOTH)
139      else :
140        gl.glShadeModel(gl.GL_FLAT)
141      #
142      ####################################
143      #
144      # lit material colors and settings
145      #
146      gl.glMaterialfv(gl.GL_FRONT, gl.GL_AMBIENT,   self._ambient._rgba)
147      gl.glMaterialfv(gl.GL_FRONT, gl.GL_DIFFUSE,   self._diffuse._rgba)
148      gl.glMaterialfv(gl.GL_FRONT, gl.GL_SPECULAR,  self._specular._rgba)
149      gl.glMaterialfv(gl.GL_FRONT, gl.GL_EMISSION,  self._emission._rgba)
150      gl.glMaterialf (gl.GL_FRONT, gl.GL_SHININESS, self._shininess)   
151      #
152      ####################################
153      #
154      # render the colormap
155      #
156      if self._colormap is not None :
157        gl.glClientActiveTexture(_texunits[_unit])
158        gl.glEnableClientState(gl.GL_TEXTURE_COORD_ARRAY)
159        gl.glTexCoordPointer(3,  gl.GL_FLOAT, 48, _texcoords)
160        gl.glTexEnvi(gl.GL_TEXTURE_ENV, gl.GL_TEXTURE_ENV_MODE, gl.GL_MODULATE)
161        self._colormap._enable()
162        _unit += 1
163      #
164      ####################################
165      #
166      # render the glowmap
167      #
168      if self._glowmap is not None :
169        gl.glClientActiveTexture(_texunits[_unit])
170        gl.glEnableClientState(gl.GL_TEXTURE_COORD_ARRAY)
171        gl.glTexCoordPointer(3,  gl.GL_FLOAT, 48, _texcoords)
172        gl.glTexEnvi(gl.GL_TEXTURE_ENV, gl.GL_TEXTURE_ENV_MODE, gl.GL_ADD)
173        gl.glEnable(gl.GL_BLEND)
174        gl.glBlendFunc(gl.GL_ONE, gl.GL_ONE)
175        self._glowmap._enable()
176        _unit += 1
177      #
178      ####################################
179      #
180      # return 1
181      #
182      return 1
183    #
184    ######################################
185    #
186    # disable textures from last pass
187    #
188    else :
189      _unit = 0
190      if self._colormap is not None :
191        gl.glClientActiveTexture(_texunits[_unit])
192        self._colormap._disable()
193        _unit += 1
194      if self._glowmap is not None :
195        gl.glClientActiveTexture(_texunits[_unit])
196        self._glowmap._disable()
197      return 0
198    #
199    ######################################
Note: See TracBrowser for help on using the repository browser.