OSDN Git Service

first
[opengano/Gano.git] / gutil.py
1
2 import pygame
3
4 from OpenGL.GL import *
5 from OpenGL.GLU import *
6
7 def init(w, h):
8     pygame.init()
9     pygame.display.set_mode((w,h), pygame.OPENGL|pygame.DOUBLEBUF|pygame.FULLSCREEN)
10 #    pygame.display.set_mode((w,h),pygame.OPENGL|pygame.DOUBLEBUF)
11
12     glClearColor(0.0, 0.0, 0.0, 1.0)
13     glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT)
14
15     glMatrixMode(GL_PROJECTION);
16     glLoadIdentity();
17     gluOrtho2D(0, w, 0, h);
18     glMatrixMode(GL_MODELVIEW);
19
20     #set up texturing
21     glEnable(GL_TEXTURE_2D)
22     glEnable(GL_BLEND);
23     glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA)
24
25
26 def load_image(image):
27     textureSurface = pygame.image.load(image)
28
29     textureData = pygame.image.tostring(textureSurface, "RGBA", 1)
30
31     width = textureSurface.get_width()
32     height = textureSurface.get_height()
33
34     texture = glGenTextures(1)
35     glBindTexture(GL_TEXTURE_2D, texture)
36     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR)
37     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR)
38     glTexImage2D( GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, textureData )
39
40     return texture, width, height
41
42
43 def delTexture(texture):
44     glDeleteTextures(texture)
45
46
47 def createTexDL(texture, width, height):
48     newList = glGenLists(1)
49     glNewList(newList,GL_COMPILE);
50     glBindTexture(GL_TEXTURE_2D, texture)
51     glBegin(GL_QUADS)
52     glTexCoord2f(0, 0); glVertex2f(0, 0)    # Bottom Left Of The Texture and Quad
53     glTexCoord2f(0, 1); glVertex2f(0, height)    # Top Left Of The Texture and Quad
54     glTexCoord2f(1, 1); glVertex2f( width,  height)    # Top Right Of The Texture and Quad
55     glTexCoord2f(1, 0); glVertex2f(width, 0)    # Bottom Right Of The Texture and Quad
56     glEnd()
57     glEndList()
58
59     return newList
60
61 def create_texture(image):
62     textureSurface = pygame.image.load(image)
63
64     textureData = pygame.image.tostring(textureSurface, "RGBA", 1)
65
66     width = textureSurface.get_width()
67     height = textureSurface.get_height()
68
69     texture = glGenTextures(1)
70     glBindTexture(GL_TEXTURE_2D, texture)
71     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR)
72     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR)
73     glTexImage2D( GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, textureData )
74
75     newList = glGenLists(1)
76     glNewList(newList,GL_COMPILE);
77     glBindTexture(GL_TEXTURE_2D, texture)
78     glBegin(GL_QUADS)
79     glTexCoord2f(0, 0); glVertex2f(0, 0)    # Bottom Left Of The Texture and Quad
80     glTexCoord2f(0, 1); glVertex2f(0, height)    # Top Left Of The Texture and Quad
81     glTexCoord2f(1, 1); glVertex2f( width,  height)    # Top Right Of The Texture and Quad
82     glTexCoord2f(1, 0); glVertex2f(width, 0)    # Bottom Right Of The Texture and Quad
83     glEnd()
84     glEndList()
85
86     return newList
87
88
89
90 def delDL(list):
91     glDeleteLists(list, 1)