import string
from OpenGL.GL import *
from OpenGL.GLU import *
from OpenGL.GLUT import *
import Image
class Texture2D:
def __init__(self, file, wrap=GL_REPEAT, minFilter=GL_LINEAR, magFilter=GL_LINEAR, texenv=GL_MODULATE):
self.file = file
self.wrapS = wrap
self.wrapT = wrap
self.minFilter = minFilter
self.magFilter = magFilter
self.texenv = texenv
self.defined = False
self.id = 0
def define(self):
img = Image.open(self.file).transpose(Image.FLIP_TOP_BOTTOM).convert('RGBA')
width = 2
while width < img.size[0]: width *= 2
width /= 2
height = 2
while height < img.size[1]: height *= 2
height /= 2
if (width != img.size[0]) or (height != img.size[1]):
img = img.resize((width,height))
self.id = glGenTextures(1)
glBindTexture(GL_TEXTURE_2D, self.id)
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, self.wrapS)
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, self.wrapT)
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, self.minFilter)
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, self.magFilter)
if (self.minFilter == GL_NEAREST_MIPMAP_NEAREST) or \
(self.minFilter == GL_NEAREST_MIPMAP_LINEAR) or \
(self.minFilter == GL_LINEAR_MIPMAP_NEAREST) or \
(self.minFilter == GL_LINEAR_MIPMAP_LINEAR):
gluBuild2DMipmaps(GL_TEXTURE_2D, GL_RGBA, img.size[0], img.size[1],
GL_RGBA, GL_UNSIGNED_BYTE, img.tostring())
else:
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, img.size[0], img.size[1],
0, GL_RGBA, GL_UNSIGNED_BYTE, img.tostring())
glBindTexture(GL_TEXTURE_2D, 0)
self.defined = True
def apply(self):
if not self.defined:
self.define()
glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, self.texenv)
glBindTexture(GL_TEXTURE_2D, self.id)
if self.id != 0:
glEnable(GL_TEXTURE_2D)
else:
glDisable(GL_TEXTURE_2D)
def disable(self):
glBindTexture(GL_TEXTURE_2D, 0)
glDisable(GL_TEXTURE_2D)
#despues tengo el cargador de modelo ke se llama LOADEROBJ.py:
class OBJFace:
def __init__(self, vertices, normals, texcoords, obj):
self.vertices = vertices
self.normals = normals
self.texcoords = texcoords
self.obj = obj
def draw(self):
glBegin(GL_POLYGON)
for i in range(0, len(self.vertices)):
if self.normals[i] > 0:
glNormal3fv(self.obj.normals[self.normals[i] - 1])
if self.texcoords[i] > 0:
glTexCoord2fv(self.obj.texcoords[self.texcoords[i] - 1])
glVertex3fv(self.obj.vertices[self.vertices[i] - 1])
glEnd()
class OBJUseMtl:
def __init__(self, mtl):
self.material = mtl
def draw(self):
if self.material.has_key('Ka'):
glMaterialfv(GL_FRONT, GL_AMBIENT, self.material['Ka'])
else:
glMaterialfv(GL_FRONT, GL_AMBIENT, [0, 0, 0, 0])
if self.material.has_key('Kd'):
glMaterialfv(GL_FRONT, GL_DIFFUSE, self.material['Kd'])
else:
glMaterialfv(GL_FRONT, GL_DIFFUSE, [0, 0, 0, 0])
if self.material.has_key('Ks'):
glMaterialfv(GL_FRONT, GL_SPECULAR, self.material['Ks'])
else:
glMaterialfv(GL_FRONT, GL_SPECULAR, [0, 0, 0, 0])
if self.material.has_key('Ns'):
glMaterialf(GL_FRONT, GL_SHININESS, self.material['Ns'])
if self.material.has_key('map_Kd'):
self.material['map_Kd'].apply()
else:
glDisable(GL_TEXTURE_2D)
class OBJFile:
def __init__(self, filename):
self.vertices = []
self.normals = []
self.texcoords = []
self.materials = {}
self.commands = []
file = open(filename, "r")
lines = file.readlines()
for line in lines:
values = string.split(line)
if len(values) < 1:
continue
if values[0] == 'v':
x = string.atof(values[1])
y = string.atof(values[2])
z = string.atof(values[3])
self.vertices.append([x,y,z])
elif values[0] == 'vn':
x = string.atof(values[1])
y = string.atof(values[2])
z = string.atof(values[3])
self.normals.append([x,y,z])
elif values[0] == 'vt':
s = string.atof(values[1])
t = string.atof(values[2])
self.texcoords.append([s,t])
elif values[0] == 'mtllib':
self.loadMtllib(values[1])
elif values[0] == 'f':
face = []
texcoords = []
for v in values[1:]:
w = string.split(v,'/')
face.append(string.atoi(w[0]))
if len(w) >= 2 and len(w[1]) > 0:
texcoords.append(string.atoi(w[1]))
else:
texcoords.append(0)
def dibujar():
#limpia la pantalla y el buffer de profundidad
glClearColor(0, 0, 0, 0)
glClear(GL_COLOR_BUFFER_BIT| GL_DEPTH_BUFFER_BIT)
glLoadIdentity()
glutSwapBuffers()
def main():
glutInit(())
glutInitDisplayMode(GLUT_DEPTH|GLUT_DOUBLE|GLUT_RGBA)
glutInitWindowSize(600,600)
glutInitWindowPosition(0,0)
glutCreateWindow("hola")
glutDisplayFunc(dibujar)
glutMainLoop()
main()