Author: ArcRiley Date: 2008-05-20 03:07:36 -0400 (Tue, 20 May 2008) New Revision: 1291
Modified: trunk/pysoy/examples/TexBlocks.py trunk/pysoy/include/soy.materials.pxd trunk/pysoy/src/materials/Bumpmapped.pym trunk/pysoy/src/materials/Material.pym trunk/pysoy/src/materials/soy.materials.pyx Log: Ticket #955 : * more work on Bumpmapped * this is a GREAT MISTAKE! rainbow blocks! Modified: trunk/pysoy/examples/TexBlocks.py =================================================================== --- trunk/pysoy/examples/TexBlocks.py 2008-05-20 05:50:32 UTC (rev 1290) +++ trunk/pysoy/examples/TexBlocks.py 2008-05-20 07:07:36 UTC (rev 1291) @@ -5,14 +5,14 @@ sce = soy.scenes.Scene() cam = soy.bodies.Camera(sce) -cam.position = (0.0, 0.0, 3.0) +cam.position = (0.0, 0.0, 2.0) lig = soy.bodies.Light(sce) lig.position = (-10.0,10.0,2.0) mrbl = soy.transports.File('media/marble.soy')['gimp'] dot3 = soy.transports.File('media/fieldstone-dot3.soy')['gimp'] colors = { - 'Marble' : (soy.materials.Material(color=mrbl, normal=dot3), + 'Marble' : (soy.materials.Bumpmapped(colormap=mrbl, bumpmap=dot3), soy.materials.Material(ambient=soy.colors.black, diffuse=soy.colors.Color('#222'), specular=soy.colors.Color('#222'), Modified: trunk/pysoy/include/soy.materials.pxd =================================================================== --- trunk/pysoy/include/soy.materials.pxd 2008-05-20 05:50:32 UTC (rev 1290) +++ trunk/pysoy/include/soy.materials.pxd 2008-05-20 07:07:36 UTC (rev 1291) @@ -40,3 +40,6 @@ cdef class Textured (Material) : cdef soy.textures.Texture _colormap cdef soy.textures.Texture _glowmap + +cdef class Bumpmapped (Textured) : + cdef soy.textures.Texture _bumpmap Modified: trunk/pysoy/src/materials/Bumpmapped.pym =================================================================== --- trunk/pysoy/src/materials/Bumpmapped.pym 2008-05-20 05:50:32 UTC (rev 1290) +++ trunk/pysoy/src/materials/Bumpmapped.pym 2008-05-20 07:07:36 UTC (rev 1291) @@ -1,4 +1,4 @@ -# PySoy's materials.Material class +# PySoy's materials.Bumpmapped class # # Copyright (C) 2006,2007,2008 PySoy Group # @@ -17,10 +17,14 @@ # # $Id$ -cdef class Material : - '''PySoy Material +cdef class Bumpmapped(Textured) : + '''soy.materials.Bumpmapped - shared material for many visible objects + Bumpmapped materials add support for bumpmap textures to the basic + Textured material. These classes are split so that other classes can + inherit Textured which are incompatible with bumpmapped textures. + + Currently .colormap, .glowmap, and .bumpmap are supported. ''' ############################################################################ @@ -28,280 +32,235 @@ # Python functions # - def __cinit__(self, color=None, ambient=None, diffuse=None, specular=None, - shininess=0.5, - normal=None, normalisation_cube_map=None) : - import soy.colors - if color : - self._color = color - else : - self._color = None + def __cinit__(self, + soy.colors.Color ambient=None, + soy.colors.Color diffuse=None, + soy.colors.Color specular=None, + soy.colors.Color emission=None, + float shininess=0.5, + soy.textures.Texture colormap=None, + soy.textures.Texture glowmap=None, + soy.textures.Texture bumpmap=None, + *args, **keywords) : + ###################################### + # + # store the bumpmap + # + self._bumpmap = bumpmap + # + ###################################### - if normal : - self._normal = normal - else : - self._normal = None - if ambient : - self._ambient = ambient - else : - self._ambient = soy.colors.white - - if diffuse : - self._diffuse = diffuse - else : - self._diffuse = soy.colors.white - - if specular : - self._specular = specular - else : - self._specular = soy.colors.white - - self._shininess = shininess - - self._blend_func_src = gl.GL_SRC_ALPHA - self._blend_func_dst = gl.GL_ONE_MINUS_SRC_ALPHA - self._shadeless = 0 - - self._emissive = soy.colors.black - - - def __repr__(self) : - return '<Material>' - - ############################################################################ # # Properties # - property color : - def __get__(self) : - return self._color + property bumpmap : + '''soy.materials.Textured.bumpmap - def __set__(self, soy.textures.Texture value) : - self._color = value + When a texture is assigned to this property a bumpmap texture is rendered. - def __del__(self) : - self._color = None + This allows artists to add fine-grained detail to 3d objects or the give + otherwise smooth/flat surfaces a pattered appearance that changes with + the angle of light (shadows/highlights). - - property normal: - def __get__(self): - return self._normal + In OpenGL terms, this texture is a dot3 normal map in tangent-space which + is mapped to object space by combining it with a normalisation cubemap. - def __set__(self, soy.textures.Texture val): - self._normal = val - - def __del__(self): - self._normal = None - - - property ambient : - def __get__(self) : - return self._ambient - - def __set__(self, soy.colors.Color value) : - self._ambient = value - - def __del__(self) : - self._ambient = soy.colors.gray - - - property diffuse : - def __get__(self) : - return self._diffuse - - def __set__(self, soy.colors.Color value) : - self._diffuse = value - - def __del__(self) : - self._diffuse = soy.colors.white - - - property shades : - '''Number of Shades - - This property detirmines how many "shades" a face may have: - 0 == "smooth" - 1 == "flat" - - In the future 2+ will may be permitted for cartoon shading effects. + Default is None (disabled). ''' + def __get__(self) : - return self._shades + return self._bumpmap - def __set__(self, int value) : - if value<1 or value>1 : - self._shades = 0 - else : - self._shades = value + def __set__(self, soy.textures.Texture _value) : + if _value._chans != 3 : + raise ValueError('bumpmaps must be RGB') + self._bumpmap = _value def __del__(self) : - self._shades = 0 + self._bumpmap = None - property shininess : - '''Shininess + ############################################################################ + # + # General C functions + # + cdef int _needsTSLVs(self) nogil : + ###################################### + # + # this material usually needs TSLV + # + return 1 + # + ###################################### - This property is how large the specular "dot" is between 0.0 and 1.0 - ''' - def __get__(self) : - return self._shininess - def __set__(self, float value) : - self._shininess = value - - def __del__(self) : - self._shininess = 0.0 - - - property specular : - def __get__(self) : - return self._specular - - def __set__(self, val) : - if isinstance(val, soy.colors.Color) : - self._specular=val - else : - raise TypeError('must be a soy.color') - - def __del__(self) : - self._specular=soy.colors.white - - - property emissive : - def __get__(self) : - return self._emissive - - def __set__(self, val) : - if isinstance(val, soy.colors.Color) : - self._emissive=val - else : - raise TypeError('must be a soy.color') - - def __del__(self) : - self._emissive=soy.colors.black - - - property blend_func_src: - def __get__(self): - return self._blend_func_src - - def __set__(self,val): - self._blend_func_src = val - - - property blend_func_dst: - def __get__(self): - return self._blend_func_dst - - def __set__(self,val): - self._blend_func_dst = val - - - property transparent: - def __get__(self): - return self._isTransparent() - - - property shadeless: - def __get__(self): - return self._shadeless - - def __set__(self,val): - self._shadeless = val - - ############################################################################ # # WindowLoop functions # - cdef int _isTransparent(self): + cdef int _render(self, int _pass, float* _texcoords, float* _tslvs) nogil : + cdef int _i, _bump, _unit + _unit = 0 + ###################################### # - # check color texture - if self._color and (<soy.textures.Texture> self._color)._chans %2 == 0 : - return 1 - if (<soy.colors.Color> self._ambient)._rgba[3] != 1.0 : - return 1 - if (<soy.colors.Color> self._diffuse)._rgba[3] != 1.0 : - return 1 - if (<soy.colors.Color> self._specular)._rgba[3] != 1.0 : - return 1 - return 0 - - - cdef void _coreBind(self) : + # if there's no bumpmap, render it like a textured material # - # set _shades value - if self._shades == 0 : - gl.glShadeModel(gl.GL_SMOOTH) + if self._bumpmap is None : + _pass += 1 else : - gl.glShadeModel(gl.GL_FLAT) + _bump = 1 # + ###################################### # - if self._shadeless: - gl.glDisable(gl.GL_LIGHTING) - gl.glColor4f(self._diffuse._rgba[0], self._diffuse._rgba[1], - self._diffuse._rgba[2], self._diffuse._rgba[3]) - else: + # this will only be true if _pass==0 and bumpmap is not None + # + if _pass == 0 : + #################################### + # + # Texture1 - the normalisation cubemap + # + gl.glClientActiveTexture(gl.GL_TEXTURE1) + gl.glEnableClientState(gl.GL_TEXTURE_COORD_ARRAY) + gl.glTexCoordPointer(3, gl.GL_FLOAT, 0, _tslvs) + _normalisation_cube_map._enable() + gl.glTexEnvi(gl.GL_TEXTURE_ENV, gl.GL_TEXTURE_ENV_MODE, gl.GL_COMBINE) + gl.glTexEnvi(gl.GL_TEXTURE_ENV, gl.GL_SOURCE0_RGB, gl.GL_TEXTURE) + gl.glTexEnvi(gl.GL_TEXTURE_ENV, gl.GL_SOURCE1_RGB, gl.GL_PREVIOUS) + gl.glTexEnvi(gl.GL_TEXTURE_ENV, gl.GL_COMBINE_RGB, gl.GL_DOT3_RGB) + gl.glTexParameteri(gl.GL_TEXTURE_CUBE_MAP, + gl.GL_TEXTURE_MAG_FILTER, gl.GL_LINEAR) + gl.glTexParameteri(gl.GL_TEXTURE_CUBE_MAP, + gl.GL_TEXTURE_MIN_FILTER, gl.GL_LINEAR) + gl.glTexParameteri(gl.GL_TEXTURE_CUBE_MAP, + gl.GL_TEXTURE_WRAP_S, gl.GL_CLAMP_TO_EDGE) + gl.glTexParameteri(gl.GL_TEXTURE_CUBE_MAP, + gl.GL_TEXTURE_WRAP_T, gl.GL_CLAMP_TO_EDGE) + gl.glTexParameteri(gl.GL_TEXTURE_CUBE_MAP, + gl.GL_TEXTURE_WRAP_R, gl.GL_CLAMP_TO_EDGE) + # + #################################### + # + # Texture0 - the dot3 normal map (bumpmap) + # + gl.glClientActiveTexture(gl.GL_TEXTURE0) + gl.glEnableClientState(gl.GL_TEXTURE_COORD_ARRAY) + gl.glTexCoordPointer(3, gl.GL_FLOAT, 48, _texcoords) + self._bumpmap._enable() + gl.glTexEnvi(gl.GL_TEXTURE_ENV, gl.GL_TEXTURE_ENV_MODE, gl.GL_COMBINE) + gl.glTexEnvi(gl.GL_TEXTURE_ENV, gl.GL_SOURCE0_RGB, gl.GL_TEXTURE) + gl.glTexEnvi(gl.GL_TEXTURE_ENV, gl.GL_COMBINE_RGB, gl.GL_REPLACE) + # + #################################### + # + # either return now and render 2-pass or play a trick + # no tricks yet + # + return 1 + # + ###################################### + # + # Render non-bumpmap passes + # + if _pass == 1 : + if _bump : + ################################## + # + # start by cleaning up from last pass + # + gl.glClientActiveTexture(gl.GL_TEXTURE1) + gl.glDisableClientState(gl.GL_TEXTURE_COORD_ARRAY) + _normalisation_cube_map._disable() + gl.glClientActiveTexture(gl.GL_TEXTURE0) + self._bumpmap._disable() + # + ################################## + # + # also setup blending for this pass + # + gl.glEnable(gl.GL_BLEND) + gl.glBlendFunc(gl.GL_DST_COLOR, gl.GL_ZERO) + gl.glEnable(gl.GL_POLYGON_OFFSET_FILL) + gl.glPolygonOffset(0,-2) + # + #################################### + # + # number of shades, currently just 0 or 1 + # + if self._shades == 0 : + gl.glShadeModel(gl.GL_SMOOTH) + else : + gl.glShadeModel(gl.GL_FLAT) + # + #################################### + # + # lit material colors and settings + # gl.glMaterialfv(gl.GL_FRONT, gl.GL_AMBIENT, self._ambient._rgba) gl.glMaterialfv(gl.GL_FRONT, gl.GL_DIFFUSE, self._diffuse._rgba) gl.glMaterialfv(gl.GL_FRONT, gl.GL_SPECULAR, self._specular._rgba) - gl.glMaterialfv(gl.GL_FRONT, gl.GL_EMISSION, self._emissive._rgba) + gl.glMaterialfv(gl.GL_FRONT, gl.GL_EMISSION, self._emission._rgba) gl.glMaterialf (gl.GL_FRONT, gl.GL_SHININESS, self._shininess) - - if self._color : - (<soy.textures.Texture> self._color)._enable() - - if self._normal : - gl.glEnable(gl.GL_BLEND) - gl.glBlendFunc(gl.GL_DST_COLOR, gl.GL_ZERO) - gl.glEnable(gl.GL_POLYGON_OFFSET_FILL) - gl.glPolygonOffset(0,-2) - elif self._isTransparent(): - gl.glEnable(gl.GL_BLEND) - gl.glBlendFunc(self._blend_func_src, self._blend_func_dst) - - - cdef void _coreUnBind(self) : - if self._color : - (<soy.textures.Texture> self._color)._disable() - if self._normal : - gl.glDisable(gl.GL_BLEND) - gl.glDisable(gl.GL_POLYGON_OFFSET_FILL) - gl.glPolygonOffset(0,0) - elif self._isTransparent(): - gl.glDisable(gl.GL_BLEND) - - if self._shadeless: - gl.glEnable(gl.GL_LIGHTING) - - cdef void _coreBindBumpPass(self) : - if not self._normal : - return - if not _normalisation_cube_map._was_created : - _normalisation_cube_map._generate(32,32) - gl.glActiveTexture(gl.GL_TEXTURE0) - (<soy.textures.Texture> self._normal)._enable() - gl.glTexEnvi(gl.GL_TEXTURE_ENV, gl.GL_TEXTURE_ENV_MODE, gl.GL_COMBINE); - gl.glTexEnvi(gl.GL_TEXTURE_ENV, gl.GL_SOURCE0_RGB, gl.GL_TEXTURE); - gl.glTexEnvi(gl.GL_TEXTURE_ENV, gl.GL_COMBINE_RGB, gl.GL_REPLACE); - gl.glActiveTexture(gl.GL_TEXTURE1) - _normalisation_cube_map._enable() - gl.glTexEnvi(gl.GL_TEXTURE_ENV, gl.GL_TEXTURE_ENV_MODE, gl.GL_COMBINE); - gl.glTexEnvi(gl.GL_TEXTURE_ENV, gl.GL_SOURCE0_RGB, gl.GL_TEXTURE); - gl.glTexEnvi(gl.GL_TEXTURE_ENV, gl.GL_COMBINE_RGB, gl.GL_DOT3_RGB); - gl.glTexEnvi(gl.GL_TEXTURE_ENV, gl.GL_SOURCE1_RGB, gl.GL_PREVIOUS); - gl.glActiveTexture(gl.GL_TEXTURE0) - - cdef void _coreUnBindBumpPass(self) : - if not self._normal : - return - gl.glActiveTexture(gl.GL_TEXTURE0) - (<soy.textures.Texture> self._normal)._disable() - gl.glTexEnvi(gl.GL_TEXTURE_ENV, gl.GL_TEXTURE_ENV_MODE, gl.GL_MODULATE) - gl.glActiveTexture(gl.GL_TEXTURE1) - _normalisation_cube_map._disable() - gl.glActiveTexture(gl.GL_TEXTURE0) + # + #################################### + # + # render the colormap + # + if self._colormap is not None : + gl.glClientActiveTexture(_texunits[_unit]) + gl.glEnableClientState(gl.GL_TEXTURE_COORD_ARRAY) + gl.glTexCoordPointer(3, gl.GL_FLOAT, 48, _texcoords) + gl.glTexEnvi(gl.GL_TEXTURE_ENV, gl.GL_TEXTURE_ENV_MODE, gl.GL_MODULATE) + self._colormap._enable() + _unit += 1 + # + #################################### + # + # render the glowmap + # + if self._glowmap is not None : + gl.glClientActiveTexture(_texunits[_unit]) + gl.glEnableClientState(gl.GL_TEXTURE_COORD_ARRAY) + gl.glTexCoordPointer(3, gl.GL_FLOAT, 48, _texcoords) + gl.glTexEnvi(gl.GL_TEXTURE_ENV, gl.GL_TEXTURE_ENV_MODE, gl.GL_ADD) + gl.glEnable(gl.GL_BLEND) + gl.glBlendFunc(gl.GL_ONE, gl.GL_ONE) + self._glowmap._enable() + _unit += 1 + # + #################################### + # + # return 1 + # + return 1 + # + ###################################### + # + # disable textures from last pass + # + else : + if _bump : + ################################## + # + # disable bump-blending + # + gl.glDisable(gl.GL_BLEND) + gl.glDisable(gl.GL_POLYGON_OFFSET_FILL) + gl.glPolygonOffset(0,0) + # + #################################### + # + _unit = 0 + if self._colormap is not None : + gl.glClientActiveTexture(_texunits[_unit]) + self._colormap._disable() + _unit += 1 + if self._glowmap is not None : + gl.glClientActiveTexture(_texunits[_unit]) + self._glowmap._disable() + return 0 + # + ###################################### Modified: trunk/pysoy/src/materials/Material.pym =================================================================== --- trunk/pysoy/src/materials/Material.pym 2008-05-20 05:50:32 UTC (rev 1290) +++ trunk/pysoy/src/materials/Material.pym 2008-05-20 07:07:36 UTC (rev 1291) @@ -266,7 +266,7 @@ # disable what we're not using and return # for _i from 0 <= _i < 4 : - gl.glActiveTexture(_texunits[_i]) + gl.glClientActiveTexture(_texunits[_i]) gl.glDisableClientState(gl.GL_TEXTURE_COORD_ARRAY) return 1 # Modified: trunk/pysoy/src/materials/soy.materials.pyx =================================================================== --- trunk/pysoy/src/materials/soy.materials.pyx 2008-05-20 05:50:32 UTC (rev 1290) +++ trunk/pysoy/src/materials/soy.materials.pyx 2008-05-20 07:07:36 UTC (rev 1291) @@ -26,5 +26,6 @@ include "__init__.pym" include "Material.pym" include "Textured.pym" +include "Bumpmapped.pym" include "_normalisationCubemap.pym" include "_postinit.pym" _______________________________________________ PySoy-SVN mailing list PySoy-SVN@pysoy.org http://www.pysoy.org/mailman/listinfo/pysoy-svn