1
# -*- coding: utf-8 -*-
2
from OpenGL.GL import *
3
from OpenGL.GLU import *
10
def __init__(self, fileName,
11
ambient = (0.2, 0.2, 0.2, 1.0),
12
diffuse = (0.8, 0.8, 0.8, 1.0),
13
specular = (0.0, 0.0, 0.0, 1.0),
14
emission = (0.0, 0.0, 0.0, 1.0),
16
"""Constructor for an openGL texture.
18
'fileName' is the name of the image file to use for the texture (string).
20
An IOError is raised if the file does not exist.
21
This does not need an openGL context.
23
self.__ambient = ambient
24
self.__diffuse = diffuse
25
self.__specular = specular
26
self.__emission = emission
27
self.__shininess = shininess
33
self.image = gtk.gdk.pixbuf_new_from_file(fileName);
35
print 'Error loading texture file: %s: %s' % (fileName, e.strerror)
41
if self.__texture is not None:
44
# Return null texture if failed to load data
45
if self.image is None:
50
texture = glGenTextures(1)
52
glBindTexture(GL_TEXTURE_2D, texture)
54
glPixelStorei(GL_UNPACK_ALIGNMENT, 1)
55
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT)
56
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT)
57
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR)
58
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR)
60
format = {1: GL_LUMINANCE, 2: GL_LUMINANCE_ALPHA, 3: GL_RGB, 4: GL_RGBA}[self.image.get_n_channels()]
61
data_type = {1: GL_BITMAP, 8: GL_UNSIGNED_BYTE, 16: GL_UNSIGNED_SHORT}[self.image.get_bits_per_sample()]
65
gluBuild2DMipmaps(GL_TEXTURE_2D,
67
self.image.get_width(),
68
self.image.get_height(),
71
self.image.get_pixels())
73
glTexImage2D(GL_TEXTURE_2D,
75
self.image.get_depth(),
76
self.image.get_width(),
77
self.image.get_height(),
81
self.image.get_pixels())
84
self.__texture = texture
87
"""Bind this texture to the current surface.
89
This requires an openGL context.
94
glBindTexture(GL_TEXTURE_2D, self.__texture)
96
# Use material properties
97
glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT, self.__ambient)
98
glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, self.__diffuse)
99
glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, self.__specular)
100
glMaterialfv(GL_FRONT_AND_BACK, GL_EMISSION, self.__emission)
101
glMaterialf(GL_FRONT_AND_BACK, GL_SHININESS, self.__shininess)