Module cube_object
[hide private]
[frames] | no frames]

Source Code for Module cube_object

  1  import os 
  2  from OpenGL.GL import * 
  3  from OpenGL.GLU import * 
  4   
  5  from matrix333 import * 
  6  from gameobjects.matrix44 import * 
  7   
  8  import pygame 
  9   
 10  #cargamos las texturas del cubo 
11 -def load_texture(filename):
12 texture_path=os.path.join('Images',filename) 13 texture_surface=pygame.image.load(texture_path) 14 texture_data=pygame.image.tostring(texture_surface,"RGBA", True) 15 width=texture_surface.get_width() 16 height=texture_surface.get_height() 17 18 material_texture=glGenTextures(1) 19 20 glBindTexture(GL_TEXTURE_2D, material_texture) 21 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR) 22 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR) 23 24 glTexImage2D( GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, 25 GL_RGBA, GL_UNSIGNED_BYTE, texture_data ) 26 return int(material_texture)
27
28 -def use_texture(texture_id):
29 glBindTexture(GL_TEXTURE_2D,texture_id)
30 31 #construimos el cubo
32 -def build_cube(side_colors):
33 list_id=glGenLists(1) 34 glNewList(list_id, GL_COMPILE) 35 36 """ 37 pegatinas = [[]] 38 for colores in range(len(side_colors)): 39 use_texture(side_colors[colores]) 40 glBegin(GL_QUADS) 41 glTexCoord2f(0.0, 0.0); glVertex3f(0.5, 0.5, 0.5) 42 glTexCoord2f(1.0, 0.0); glVertex3f(0.5, 0.5, 0.5) 43 glTexCoord2f(1.0, 1.0); glVertex3f(0.5, 0.5, 0.5) 44 glTexCoord2f(0.0, 1.0); glVertex3f(0.5, 0.5, 0.5) 45 glEnd() 46 """ 47 48 #color rojo 49 use_texture(side_colors[0]) 50 glBegin(GL_QUADS) 51 glTexCoord2f(0.0, 0.0); glVertex3f(-0.5, -0.5, -0.5) 52 glTexCoord2f(1.0, 0.0); glVertex3f(0.5, -0.5, -0.5) 53 glTexCoord2f(1.0, 1.0); glVertex3f(0.5, 0.5, -0.5) 54 glTexCoord2f(0.0, 1.0); glVertex3f(-0.5, 0.5, -0.5) 55 glEnd() 56 57 #azul 58 use_texture(side_colors[1]) 59 glBegin(GL_QUADS) 60 glTexCoord2f(0.0, 0.0); glVertex3f(0.5, -0.5, -0.5) 61 glTexCoord2f(1.0, 0.0); glVertex3f(0.5, -0.5, 0.5) 62 glTexCoord2f(1.0, 1.0); glVertex3f(0.5, 0.5, 0.5) 63 glTexCoord2f(0.0, 1.0); glVertex3f(0.5, 0.5, -0.5) 64 glEnd() 65 66 #naranja 67 use_texture(side_colors[2]) 68 glBegin(GL_QUADS) 69 glTexCoord2f(0.0, 0.0); glVertex3f(0.5, -0.5, 0.5) 70 glTexCoord2f(1.0, 0.0); glVertex3f(-0.5, -0.5, 0.5) 71 glTexCoord2f(1.0, 1.0); glVertex3f(-0.5, 0.5, 0.5) 72 glTexCoord2f(0.0, 1.0); glVertex3f(0.5, 0.5, 0.5) 73 glEnd() 74 75 #verde 76 use_texture(side_colors[3]) 77 glBegin(GL_QUADS) 78 glTexCoord2f(0.0, 0.0); glVertex3f(-0.5, -0.5, 0.5) 79 glTexCoord2f(1.0, 0.0); glVertex3f(-0.5, -0.5, -0.5) 80 glTexCoord2f(1.0, 1.0); glVertex3f(-0.5, 0.5, -0.5) 81 glTexCoord2f(0.0, 1.0); glVertex3f(-0.5, 0.5, 0.5) 82 glEnd() 83 84 #Blanco 85 use_texture(side_colors[4]) 86 glBegin(GL_QUADS) 87 glTexCoord2f(0.0, 0.0); glVertex3f(-0.5, 0.5, -0.5) 88 glTexCoord2f(1.0, 0.0); glVertex3f(0.5, 0.5, -0.5) 89 glTexCoord2f(1.0, 1.0); glVertex3f(0.5, 0.5, 0.5) 90 glTexCoord2f(0.0, 1.0); glVertex3f(-0.5, 0.5, 0.5) 91 glEnd() 92 93 #Amarillo 94 use_texture(side_colors[5]) 95 glBegin(GL_QUADS) 96 glTexCoord2f(0.0, 0.0); glVertex3f(-0.5, -0.5, -0.5) 97 glTexCoord2f(1.0, 0.0); glVertex3f(0.5, -0.5, -0.5) 98 glTexCoord2f(1.0, 1.0); glVertex3f(0.5, -0.5, 0.5) 99 glTexCoord2f(0.0, 1.0); glVertex3f(-0.5, -0.5, 0.5) 100 glEnd() 101 glEndList() 102 return int(list_id)
103 104
105 -def build_cube_lists():
106 Red=load_texture('Red256.png'); 107 Orange=load_texture('Orange256.png') 108 Yellow=load_texture('Yellow256.png'); 109 Green=load_texture('Green256.png') 110 Blue=load_texture('Blue256.png'); 111 White=load_texture('White256.png') 112 Notex=load_texture('None64.png') 113 114 cube_lists=Matrix333(0.) 115 for i in range(3): 116 for j in range(3): 117 for k in range(3): 118 side_colors=[Red, Blue, Orange, Green, White, Yellow] 119 if i<2: side_colors[1]=Notex 120 if i>0: side_colors[3]=Notex 121 if j<2: side_colors[4]=Notex 122 if j>0: side_colors[5]=Notex 123 if k<2: side_colors[2]=Notex 124 if k>0: side_colors[0]=Notex 125 cube_lists.set_element(i,j,k, build_cube(side_colors)) 126 return cube_lists
127 128 129
130 -class RubiksCube(object):
131
132 - def __init__(self):
133 self.sides=build_cube_lists() # returns a Matrix333 object 134 identity=Matrix44() 135 self.rotations=Matrix333(identity)
136
137 - def draw(self,x_angle,y_angle,view_distance,viewmode, 138 angle=0,sidex=-99,sidey=-99,sidez=-99):
139 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT) 140 glLoadIdentity() 141 glTranslatef(0.0, 0.0, -view_distance) 142 glRotatef(x_angle, 1.0, 0.0, 0.0) 143 glRotatef(y_angle, 0.0, 1.0, 0.0) 144 145 for i in range(-1,2): 146 for j in range(-1,2): 147 for k in range(-1,2): 148 if not ((i==0) and (j==0) and (k==0)): 149 # if ((i==0) and (j==1) and (k==0)): 150 glPushMatrix() 151 if (i==sidex) and (sidex!=0): 152 glRotatef(angle,1.,0.,0.) 153 if (j==sidey) and (sidey!=0): 154 glRotatef(angle,0.,1.,0.) 155 if (k==sidez) and (sidez!=0): 156 glRotatef(angle,0.,0.,1.) 157 glTranslatef(i*viewmode, j*viewmode, k*viewmode) 158 rotation=self.rotations[i+1,j+1,k+1].to_opengl() 159 glMultMatrixf(rotation) 160 glCallList(self.sides[i+1,j+1,k+1]) 161 glPopMatrix()
162 163
164 - def find_cubie(self,cubie_list_id):
165 for i in xrange(3): 166 for j in xrange(3): 167 for k in xrange(3): 168 if self.sides[i,j,k]==cubie_list_id: 169 return (i,j,k) 170 return "Error"
171