<s> import collada <EOL> import numpy <EOL> import pyglet <EOL> from pyglet . gl import * <EOL> import ctypes <EOL> import glutils <EOL> class OldStyleRenderer : <EOL> def __init__ ( self , dae , window ) : <EOL> self . dae = dae <EOL> self . window = window <EOL> self . z_max = - <NUM_LIT> <EOL> self . z_min = <NUM_LIT> <EOL> self . textures = { } <EOL> glShadeModel ( GL_SMOOTH ) <EOL> glClearColor ( <NUM_LIT:0.0> , <NUM_LIT:0.0> , <NUM_LIT:0.0> , <NUM_LIT:0.5> ) <EOL> glClearDepth ( <NUM_LIT:1.0> ) <EOL> glEnable ( GL_DEPTH_TEST ) <EOL> glDepthFunc ( GL_LEQUAL ) <EOL> glEnable ( GL_MULTISAMPLE ) ; <EOL> glEnable ( GL_LIGHTING ) <EOL> glEnable ( GL_LIGHT0 ) <EOL> glHint ( GL_PERSPECTIVE_CORRECTION_HINT , GL_NICEST ) <EOL> glCullFace ( GL_BACK ) <EOL> glEnable ( GL_TEXTURE_2D ) <EOL> glTexParameterf ( GL_TEXTURE_2D , GL_TEXTURE_MAG_FILTER , GL_NEAREST ) <EOL> glTexParameterf ( GL_TEXTURE_2D , GL_TEXTURE_MIN_FILTER , GL_NEAREST ) <EOL> print '<STR_LIT>' <EOL> print '<STR_LIT>' <EOL> self . displist = glGenLists ( <NUM_LIT:1> ) <EOL> glNewList ( self . displist , GL_COMPILE ) <EOL> self . drawPrimitives ( ) <EOL> glEndList ( ) <EOL> print '<STR_LIT>' <EOL> def drawPrimitives ( self ) : <EOL> glBegin ( GL_TRIANGLES ) <EOL> if self . dae . scene is not None : <EOL> for geom in self . dae . scene . objects ( '<STR_LIT>' ) : <EOL> for prim in geom . primitives ( ) : <EOL> mat = prim . material <EOL> diff_color = ( GLfloat * <NUM_LIT:4> ) ( * ( <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT:0.0> ) ) <EOL> spec_color = None <EOL> shininess = None <EOL> amb_color = None <EOL> tex_id = None <EOL> for prop in mat . effect . supported : <EOL> value = getattr ( mat . effect , prop ) <EOL> if isinstance ( value , collada . material . Map ) : <EOL> colladaimage = value . sampler . surface . image <EOL> img = colladaimage . pilimage <EOL> if img : <EOL> if self . textures . has_key ( colladaimage . id ) : <EOL> tex_id = self . textures [ colladaimage . id ] <EOL> else : <EOL> try : <EOL> ( ix , iy , tex_data ) = ( img . size [ <NUM_LIT:0> ] , img . size [ <NUM_LIT:1> ] , img . tostring ( "<STR_LIT>" , "<STR_LIT>" , <NUM_LIT:0> , - <NUM_LIT:1> ) ) <EOL> except SystemError : <EOL> ( ix , iy , tex_data ) = ( img . size [ <NUM_LIT:0> ] , img . size [ <NUM_LIT:1> ] , img . tostring ( "<STR_LIT>" , "<STR_LIT>" , <NUM_LIT:0> , - <NUM_LIT:1> ) ) <EOL> tid = GLuint ( ) <EOL> glGenTextures ( <NUM_LIT:1> , ctypes . byref ( tid ) ) <EOL> tex_id = tid . value <EOL> glBindTexture ( GL_TEXTURE_2D , tex_id ) <EOL> glTexParameteri ( GL_TEXTURE_2D , GL_TEXTURE_MAG_FILTER , GL_LINEAR ) <EOL> glTexParameteri ( GL_TEXTURE_2D , GL_TEXTURE_MIN_FILTER , GL_LINEAR ) <EOL> glTexParameteri ( GL_TEXTURE_2D , GL_TEXTURE_WRAP_S , GL_REPEAT ) <EOL> glTexParameteri ( GL_TEXTURE_2D , GL_TEXTURE_WRAP_T , GL_REPEAT ) <EOL> glTexImage2D ( GL_TEXTURE_2D , <NUM_LIT:0> , GL_RGBA , ix , iy , <NUM_LIT:0> , GL_RGBA , GL_UNSIGNED_BYTE , tex_data ) <EOL> self . textures [ colladaimage . id ] = tex_id <EOL> else : <EOL> print '<STR_LIT>' % ( <EOL> prop , colladaimage . id ) <EOL> else : <EOL> if prop == '<STR_LIT>' and value is not None : <EOL> diff_color = ( GLfloat * <NUM_LIT:4> ) ( * value ) <EOL> elif prop == '<STR_LIT>' and value is not None : <EOL> spec_color = ( GLfloat * <NUM_LIT:4> ) ( * value ) <EOL> elif prop == '<STR_LIT>' and value is not None : <EOL> amb_color = ( GLfloat * <NUM_LIT:4> ) ( * value ) <EOL> elif prop == '<STR_LIT>' and value is not None : <EOL> shininess = value <EOL> prim_type = type ( prim ) . __name__ <EOL> if prim_type == '<STR_LIT>' : <EOL> triangles = prim <EOL> elif prim_type == '<STR_LIT>' : <EOL> triangles = prim . triangleset ( ) <EOL> else : <EOL> print '<STR_LIT>' , prim_type <EOL> triangles = [ ] <EOL> if tex_id is not None : <EOL> glBindTexture ( GL_TEXTURE_2D , tex_id ) <EOL> else : <EOL> glBindTexture ( GL_TEXTURE_2D , <NUM_LIT:0> ) <EOL> for t in triangles : <EOL> nidx = <NUM_LIT:0> <EOL> if tex_id is not None and len ( t . texcoords ) > <NUM_LIT:0> : <EOL> texcoords = t . texcoords [ <NUM_LIT:0> ] <EOL> else : <EOL> texcoords = None <EOL> for vidx in t . indices : <EOL> if diff_color is not None : <EOL> glMaterialfv ( GL_FRONT , GL_DIFFUSE , diff_color ) <EOL> if spec_color is not None : <EOL> glMaterialfv ( GL_FRONT , GL_SPECULAR , spec_color ) <EOL> if amb_color is not None : <EOL> glMaterialfv ( GL_FRONT , GL_AMBIENT , amb_color ) <EOL> if shininess is not None : <EOL> glMaterialfv ( GL_FRONT , GL_SHININESS , ( GLfloat * <NUM_LIT:1> ) ( shininess ) ) <EOL> glNormal3fv ( ( GLfloat * <NUM_LIT:3> ) ( * t . normals [ nidx ] ) ) <EOL> if texcoords is not None : <EOL> glTexCoord2fv ( ( GLfloat * <NUM_LIT:2> ) ( * texcoords [ nidx ] ) ) <EOL> nidx += <NUM_LIT:1> <EOL> v = prim . vertex [ vidx ] <EOL> glVertex3fv ( ( GLfloat * <NUM_LIT:3> ) ( * v ) ) <EOL> if v [ <NUM_LIT:2> ] > self . z_max : <EOL> self . z_max = v [ <NUM_LIT:2> ] <EOL> elif v [ <NUM_LIT:2> ] < self . z_min : <EOL> self . z_min = v [ <NUM_LIT:2> ] <EOL> glutils . getGLError ( ) <EOL> glEnd ( ) <EOL> def render ( self , rotate_x , rotate_y , rotate_z ) : <EOL> glClear ( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT ) <EOL> glMatrixMode ( GL_PROJECTION ) <EOL> glLoadIdentity ( ) <EOL> if self . window . height == <NUM_LIT:0> : <EOL> gluPerspective ( <NUM_LIT:100> , self . window . width , <NUM_LIT:1.0> , <NUM_LIT> ) <EOL> else : <EOL> gluPerspective ( <NUM_LIT:100> , self . window . width / self . window . height , <NUM_LIT:1.0> , <NUM_LIT> ) <EOL> glMatrixMode ( GL_MODELVIEW ) <EOL> glLoadIdentity ( ) <EOL> z_offset = self . z_min - ( self . z_max - self . z_min ) * <NUM_LIT:3> <EOL> light_pos = ( GLfloat * <NUM_LIT:3> ) ( <NUM_LIT> , <NUM_LIT> , <NUM_LIT> * - z_offset ) <EOL> glLightfv ( GL_LIGHT0 , GL_POSITION , light_pos ) <EOL> glTranslatef ( <NUM_LIT:0> , <NUM_LIT:0> , z_offset ) <EOL> glRotatef ( rotate_x , <NUM_LIT:1.0> , <NUM_LIT:0.0> , <NUM_LIT:0.0> ) <EOL> glRotatef ( rotate_y , <NUM_LIT:0.0> , <NUM_LIT:1.0> , <NUM_LIT:0.0> ) <EOL> glRotatef ( rotate_z , <NUM_LIT:0.0> , <NUM_LIT:0.0> , <NUM_LIT:1.0> ) <EOL> glCallList ( self . displist ) <EOL> def cleanup ( self ) : <EOL> print '<STR_LIT>' <EOL> glDeleteLists ( self . displist , <NUM_LIT:1> ) </s>
