~ubuntu-branches/ubuntu/precise/gnome-games/precise-proposed

« back to all changes in this revision

Viewing changes to glchess/src/lib/scene/opengl/texture.py

  • Committer: Package Import Robot
  • Author(s): Rodrigo Moya
  • Date: 2011-05-30 13:32:04 UTC
  • mfrom: (1.3.4)
  • mto: (163.1.3 precise)
  • mto: This revision was merged to the branch mainline in revision 143.
  • Revision ID: package-import@ubuntu.com-20110530133204-celaq1v1dsxc48q1
Tags: upstream-3.0.2
ImportĀ upstreamĀ versionĀ 3.0.2

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# -*- coding: utf-8 -*-
2
 
from OpenGL.GL import *
3
 
from OpenGL.GLU import *
4
 
import gtk.gdk
5
 
 
6
 
class Texture:
7
 
    """
8
 
    """
9
 
    
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),
15
 
                 shininess = 0.0):
16
 
        """Constructor for an openGL texture.
17
 
        
18
 
        'fileName' is the name of the image file to use for the texture (string).
19
 
        
20
 
        An IOError is raised if the file does not exist.
21
 
        This does not need an openGL context.
22
 
        """
23
 
        self.__ambient = ambient
24
 
        self.__diffuse = diffuse
25
 
        self.__specular = specular
26
 
        self.__emission = emission
27
 
        self.__shininess = shininess
28
 
 
29
 
        # OpenGL texture ID
30
 
        self.__texture = None
31
 
 
32
 
        try:
33
 
            self.image = gtk.gdk.pixbuf_new_from_file(fileName);
34
 
        except IOError, e:
35
 
            print 'Error loading texture file: %s: %s' % (fileName, e.strerror)
36
 
            self.image = None
37
 
 
38
 
    def __generate(self):
39
 
        """
40
 
        """
41
 
        if self.__texture is not None:
42
 
            return
43
 
 
44
 
        # Return null texture if failed to load data        
45
 
        if self.image is None:
46
 
            self.__texture = 0
47
 
            return
48
 
        
49
 
        # FIXME: Can fail
50
 
        texture = glGenTextures(1)
51
 
            
52
 
        glBindTexture(GL_TEXTURE_2D, texture)
53
 
 
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)
59
 
 
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()]
62
 
 
63
 
        # Generate mipmaps
64
 
        try:
65
 
            gluBuild2DMipmaps(GL_TEXTURE_2D,
66
 
                              GL_LUMINANCE,
67
 
                              self.image.get_width(),
68
 
                              self.image.get_height(),
69
 
                              format,
70
 
                              data_type,
71
 
                              self.image.get_pixels())
72
 
        except GLUerror, e:
73
 
            glTexImage2D(GL_TEXTURE_2D,
74
 
                         0, # Level
75
 
                         self.image.get_depth(),
76
 
                         self.image.get_width(),
77
 
                         self.image.get_height(),
78
 
                         0, # Border
79
 
                         data_type,
80
 
                         GL_UNSIGNED_BYTE,
81
 
                         self.image.get_pixels())
82
 
 
83
 
        del self.image
84
 
        self.__texture = texture
85
 
 
86
 
    def bind(self):
87
 
        """Bind this texture to the current surface.
88
 
        
89
 
        This requires an openGL context.
90
 
        """
91
 
        self.__generate()
92
 
 
93
 
        # Use texture
94
 
        glBindTexture(GL_TEXTURE_2D, self.__texture)
95
 
        
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)