Author: ArcRiley Date: 2008-05-23 02:49:01 -0400 (Fri, 23 May 2008) New Revision: 1299
Added: trunk/pysoy/examples/AnimBlock.py Removed: trunk/pysoy/src/materials/Animated.pym Modified: trunk/pysoy/include/math.pxd trunk/pysoy/include/soy.materials.pxd trunk/pysoy/include/soy.textures.pxd trunk/pysoy/src/textures/Texture.pym Log: Ticket #955 : * added fabs to math.pxd * removed soy.materials.Animated * added .animate property to soy.textures.Texture * added AnimBlock.py example -- oooh pretty!! Added: trunk/pysoy/examples/AnimBlock.py =================================================================== --- trunk/pysoy/examples/AnimBlock.py (rev 0) +++ trunk/pysoy/examples/AnimBlock.py 2008-05-23 06:49:01 UTC (rev 1299) @@ -0,0 +1,83 @@ +#!/usr/bin/env python +import soy +import blocks +from time import sleep + +sce = soy.scenes.Scene() +cam = soy.bodies.Camera(sce) +cam.position = (0.0, 0.0, 2.0) +lig = soy.bodies.Light(sce) +lig.position = (-10.0,10.0,2.0) + +lava = soy.transports.File('media/lava.soy')['gimp'] +lava.animate=(.5,0,0) +dot3 = soy.transports.File('media/fieldstone-dot3.soy')['gimp'] +colors = { + 'Marble' : (soy.materials.Textured(colormap=lava, bumpmap=dot3), + soy.materials.Material(ambient=soy.colors.black, + diffuse=soy.colors.Color('#222'), + specular=soy.colors.Color('#222'), + shininess=5.0), + (0,0,0)), +} +bks = blocks.blocks(sce, colors) + +fps = soy.textures.Print() + +scr = soy.Screen() +win = soy.Window(scr, 'TexBlocks', background=soy.colors.cyan) +pro = soy.widgets.Projector(win, camera=cam) +can = soy.widgets.Canvas(win, texture=fps) + +def wireframeToggle() : + if cam.wireframe : + cam.wireframe = False + else : + cam.wireframe = True + +def fullscreenToggle() : + if scr.fullscreen : + scr.fullscreen = None + else : + scr.fullscreen = win + +def moreLight() : + lig.diffuse = lig.diffuse + 1.0 + print lig.diffuse + +def lessLight() : + lig.diffuse = lig.diffuse - 1.0 + print lig.diffuse + +key = soy.controllers.Keyboard(win) +key['Q'] = soy.actions.Force(bks['Marble'], -100, 0, 0) +key['R'] = soy.actions.Force(bks['Marble'], 0, 100, 0) +key['S'] = soy.actions.Force(bks['Marble'], 100, 0, 0) +key['T'] = soy.actions.Force(bks['Marble'], 0, -100, 0) +key['U'] = soy.actions.Force(bks['Marble'], 0, 0, -100) +key['V'] = soy.actions.Force(bks['Marble'], 0, 0, 100) +key['q'] = soy.actions.Quit() +key[ 1 ] = soy.actions.Quit() # 9 = esc key +key['f'] = fullscreenToggle +key['w'] = wireframeToggle +key['['] = lessLight +key[']'] = moreLight +wcn = soy.controllers.Window(win) +wcn['close'] = soy.actions.Quit() + +if __name__ == '__main__' : + while True: + sleep(.1) + fps.text = '%sfps' % str(int(cam.fps)).zfill(4) + for bk in bks : + p = bks[bk].position + v = bks[bk].velocity + v = [v[0], v[1], v[2]] + if abs(p[0]) > 5 and ((p[0]>0 and v[0]>0) or (p[0]<0 and v[0]< 0)) : + v[0] = v[0]*-1 + if abs(p[1]) > 5 and ((p[1]>0 and v[1]>0) or (p[1]<0 and v[1]< 0)) : + v[1] = v[1]*-1 + if abs(p[2]) > 5 and ((p[2]>0 and v[2]>0) or (p[2]<0 and v[2]< 0)) : + v[2] = v[2]*-1 + bks[bk].velocity = v + Property changes on: trunk/pysoy/examples/AnimBlock.py ___________________________________________________________________ Added: svn:executable + * Modified: trunk/pysoy/include/math.pxd =================================================================== --- trunk/pysoy/include/math.pxd 2008-05-22 18:31:37 UTC (rev 1298) +++ trunk/pysoy/include/math.pxd 2008-05-23 06:49:01 UTC (rev 1299) @@ -18,6 +18,7 @@ # $Id$ cdef extern from "math.h" nogil : - long int lround (double x) - long int lroundf (float x) - float fabsf (float number) + long int lround ( double ) + long int lroundf ( float ) + double fabs ( double ) + float fabsf ( float ) Modified: trunk/pysoy/include/soy.materials.pxd =================================================================== --- trunk/pysoy/include/soy.materials.pxd 2008-05-22 18:31:37 UTC (rev 1298) +++ trunk/pysoy/include/soy.materials.pxd 2008-05-23 06:49:01 UTC (rev 1299) @@ -41,8 +41,3 @@ cdef soy.textures.Texture _bumpmap cdef soy.textures.Texture _colormap cdef soy.textures.Texture _glowmap - -cdef class Animated (Material) : - cdef float _animateX - cdef float _animateY - cdef float _animateZ Modified: trunk/pysoy/include/soy.textures.pxd =================================================================== --- trunk/pysoy/include/soy.textures.pxd 2008-05-22 18:31:37 UTC (rev 1298) +++ trunk/pysoy/include/soy.textures.pxd 2008-05-23 06:49:01 UTC (rev 1299) @@ -19,6 +19,7 @@ cimport gl cimport py +cimport math cimport ogg cimport cairo cimport stdio @@ -34,6 +35,8 @@ cdef gl.GLsizei _width cdef gl.GLsizei _height cdef gl.GLsizei _depth + cdef int _isAnimated + cdef gl.GLfloat _animate[3] cdef gl.GLfloat _scaleX cdef gl.GLfloat _scaleY cdef gl.GLfloat _scaleZ @@ -41,7 +44,7 @@ cdef int _update cdef gl.GLubyte *_texels # Lookup Arrays - cdef int _types[3] + cdef int _types[5] cdef int _formats[5] # General C functions cdef void _resize ( self, int, int, Deleted: trunk/pysoy/src/materials/Animated.pym =================================================================== --- trunk/pysoy/src/materials/Animated.pym 2008-05-22 18:31:37 UTC (rev 1298) +++ trunk/pysoy/src/materials/Animated.pym 2008-05-23 06:49:01 UTC (rev 1299) @@ -1,278 +0,0 @@ -# PySoy's materials.Animated class -# -# Copyright (C) 2006,2007,2008 PySoy Group -# -# This program is free software; you can redistribute it and/or modify -# it under the terms of the GNU Affero General Public License as published -# by the Free Software Foundation, either version 3 of the License, or -# (at your option) any later version. -# -# This program is distributed in the hope that it will be useful, -# but WITHOUT ANY WARRANTY; without even the implied warranty of -# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -# GNU Affero General Public License for more details. -# -# You should have received a copy of the GNU Affero General Public License -# along with this program; if not, see http://www.gnu.org/licenses -# -# $Id$ - -cdef class Animated (Textured) : - '''soy.materials.Animated - - Textured materials support one or more basic textures in addition to the - standard Material colors (ambient, diffuse, specular, emission) which - are multiplied by the texture color to provide light-based shading. - - Currently .bumpmap .colormap and .glowmap are supported. - ''' - - ############################################################################ - # - # Python functions - # - - 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 bumpmap=None, - soy.textures.Texture colormap=None, - soy.textures.Texture glowmap=None, - - *args, **keywords) : - ###################################### - # - # store the animation arguments - # - - # - ###################################### - - - ############################################################################ - # - # Properties - # - - property animate : - '''soy.materials.Animated.animate - - - Default is (0.0, 0.0, 0.0) (disabled). - ''' - - def __get__(self) : - return (self._animateX, self._animateY, self._animateZ) - - def __set__(self, _value) : - if type(value) != tuple and type(value) != list : - raise TypeError('Must provide a tuple or list') - if len(value) != 3 : - raise TypeError('Must provide animation rate as (x,y,z)') - - def __del__(self) : - self._animateX = 0.0 - self._animateY = 0.0 - self._animateZ = 0.0 - - - ############################################################################ - # - # WindowLoop functions - # - - cdef int _render(self, int _pass, float* _texcoords, float* _tslvs) nogil : - cdef int _i, _anim, _bump, _unit - cdef float _animX, _animY, _animZ - cdef double _t - _unit = 0 - ###################################### - # - # time to transform calc - # - _t = _time() - if self._animateX != 0.0 : - _anim = 1 - _animX = _t * self._animateX - _animX -= math.fabsf(_animX) - if self._animateY != 0.0 : - _anim = 1 - _animY = _t * self._animateY - _animY -= math.fabsf(_animY) - if self._animateZ != 0.0 : - _anim = 1 - _animZ = _t * self._animateZ - _animZ -= math.fabsf(_animZ) - # - ###################################### - # - # if there's no bumpmap, render it like a textured material - # - if self._bumpmap is None : - _pass += 1 - else : - _bump = 1 - # - ###################################### - # - # this will only be true if _pass==0 and bumpmap is not None - # - if _pass == 0 : - #################################### - # - # Texture1 - the normalisation cubemap - # - gl.glActiveTexture(gl.GL_TEXTURE1) - 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.glActiveTexture(gl.GL_TEXTURE0) - gl.glEnableClientState(gl.GL_TEXTURE_COORD_ARRAY) - gl.glClientActiveTexture(gl.GL_TEXTURE0) - gl.glTexCoordPointer(3, gl.GL_FLOAT, 48, _texcoords) - self._bumpmap._enable() - if _anim : - gl.glMatrixMode(gl.GL_TEXTURE) - gl.glTranslatef(_animX, _animY, _animZ) - 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.glActiveTexture(gl.GL_TEXTURE1) - gl.glClientActiveTexture(gl.GL_TEXTURE1) - _normalisation_cube_map._disable() - gl.glDisableClientState(gl.GL_TEXTURE_COORD_ARRAY) - gl.glActiveTexture(gl.GL_TEXTURE0) - 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._emission._rgba) - gl.glMaterialf (gl.GL_FRONT, gl.GL_SHININESS, self._shininess) - # - #################################### - # - # render the colormap - # - if self._colormap is not None : - gl.glActiveTexture(_texunits[_unit]) - 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.glActiveTexture(_texunits[_unit]) - 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.glActiveTexture(_texunits[_unit]) - self._colormap._disable() - _unit += 1 - if self._glowmap is not None : - gl.glActiveTexture(_texunits[_unit]) - self._glowmap._disable() - return 0 - # - ###################################### - \ No newline at end of file Modified: trunk/pysoy/src/textures/Texture.pym =================================================================== --- trunk/pysoy/src/textures/Texture.pym 2008-05-22 18:31:37 UTC (rev 1298) +++ trunk/pysoy/src/textures/Texture.pym 2008-05-23 06:49:01 UTC (rev 1299) @@ -37,6 +37,7 @@ # These are OpenGL format mapping based on number of channels self._types[1] = gl.GL_UNSIGNED_BYTE self._types[2] = gl.GL_UNSIGNED_SHORT + self._types[4] = gl.GL_FLOAT self._formats[1] = gl.GL_LUMINANCE self._formats[2] = gl.GL_LUMINANCE_ALPHA self._formats[3] = gl.GL_RGB @@ -62,6 +63,43 @@ # Properties # + property animate : + '''soy.materials.Animated.animate + + + Default is (0.0, 0.0, 0.0) (disabled). + ''' + + def __get__(self) : + return (self._animate[0], self._animate[1], self._animate[2]) + + def __set__(self, value) : + cdef int _i + if type(value) != tuple and type(value) != list : + raise TypeError('Must provide a tuple or list') + if len(value) != 3 : + raise TypeError('Must provide animation rate as (x,y,z)') + for v in value : + if type(v) != int and type(v) != float : + raise TypeError(v, 'is not a number') + # need lock!!! + if value == (0, 0, 0) : + self._isAnimated = 0 + else : + self._isAnimated = 1 + for _i from 0 <= _i < 3 : + self._animate[_i] = value[_i] + # need unlock!!! + + def __del__(self) : + cdef int _i + # need lock!!! + self._isAnimated = 0 + for _i from 0 <= _i < 3 : + self._animate[_i] = 0.0 + # need unlock!!! + + property aspect : '''Texture's aspect ratio @@ -181,18 +219,33 @@ # cdef void _enable(self) nogil : - cdef int _size + cdef int _i, _size + cdef double _t, _anim[3] + # + ###################################### + # + # don't bother with empty texture + # if self._target == 0 : return # + ###################################### + # # Lock to prevent resizing while we render + # py.PyThread_acquire_lock(self._mutex, 1) # + ###################################### + # # Enable our texture target + # gl.glEnable(self._target) if self._textureID == 0 : # - # If we haven't generated this texture yet + #################################### + # + # if we haven't generated this texture yet, do so now + # gl.glGenTextures(1, &self._textureID) gl.glBindTexture(self._target, self._textureID) gl.glTexParameteri(self._target, gl.GL_TEXTURE_MAG_FILTER, @@ -200,8 +253,12 @@ gl.glTexParameteri(self._target, gl.GL_TEXTURE_MIN_FILTER, gl.GL_LINEAR) # + #################################### + # # flag the texture for updating here so it'll be processed below + # self._update = 1 + # else : # # Texture is already generated, bind it @@ -255,13 +312,28 @@ # Update complete, clear the flag self._update = 0 # - # Do we need to scale the texture? + ###################################### + # + if self._isAnimated : + _t = _time() + for _i from 0 <= _i < 3 : + _anim[_i] = _t * self._animate[_i] + _anim[_i] = _anim[_i] - <int> _anim[_i] + gl.glMatrixMode(gl.GL_TEXTURE) + gl.glTranslatef(_anim[0], _anim[1], _anim[2]) + # + ###################################### + # + # Scale texture if needed + # if self._scaleX : # # Activate the texcoord matrix then scale them by the given amount gl.glMatrixMode(gl.GL_TEXTURE) gl.glScalef(self._scaleX, self._scaleY, self._scaleZ) - + # + ###################################### + # #cdef int max_size #gl.glGetIntegerv(gl.GL_MAX_TEXTURE_SIZE, &max_size) #if max_size > self._width or max_size > self._height : @@ -272,9 +344,12 @@ if self._target == 0 : return gl.glDisable(self._target) - if self._scaleX : - # - # Reset texture scaling + # + ###################################### + # + # undo animation and/or scale + # + if self._isAnimated or self._scaleX : gl.glMatrixMode(gl.GL_TEXTURE) gl.glLoadIdentity() py.PyThread_release_lock(self._mutex) _______________________________________________ PySoy-SVN mailing list PySoy-SVN@pysoy.org http://www.pysoy.org/mailman/listinfo/pysoy-svn