Skip to content

Commit 5ea59c3

Browse files
committed
init commit
1 parent c7dd682 commit 5ea59c3

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

61 files changed

+17388
-0
lines changed

Chapter 11/11-1.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
from math import log, ceil
2+
def next_power_of_2(size):
3+
return 2 ** ceil(log(size, 2))

Chapter 11/11-2.py

Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
from math import radians
2+
3+
from OpenGL.GL import *
4+
from OpenGL.GLU import *
5+
6+
import pygame
7+
from pygame.locals import *
8+
9+
SCREEN_SIZE = (800, 600)
10+
11+
def resize(width, height):
12+
13+
glViewport(0, 0, width, height)
14+
glMatrixMode(GL_PROJECTION)
15+
glLoadIdentity()
16+
gluPerspective(60.0, float(width)/height, .1, 1000.)
17+
glMatrixMode(GL_MODELVIEW)
18+
glLoadIdentity()
19+
20+
def init():
21+
22+
glEnable(GL_TEXTURE_2D)
23+
glClearColor(1.0, 1.0, 1.0, 0.0)
24+
25+
def run():
26+
27+
pygame.init()
28+
screen = pygame.display.set_mode(SCREEN_SIZE, HWSURFACE|OPENGL|DOUBLEBUF)
29+
30+
resize(*SCREEN_SIZE)
31+
init()
32+
33+
# Load the textures
34+
texture_surface = pygame.image.load("sushitex.png")
35+
# Retrieve the texture data
36+
texture_data = pygame.image.tostring(texture_surface, 'RGB', True)
37+
38+
# Generate a texture id
39+
texture_id = glGenTextures(1)
40+
# Tell OpenGL we will be using this texture id for texture operations
41+
glBindTexture(GL_TEXTURE_2D, texture_id)
42+
43+
# Tell OpenGL how to scale images
44+
glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR )
45+
glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR )
46+
47+
# Tell OpenGL that data is aligned to byte boundries
48+
glPixelStorei(GL_UNPACK_ALIGNMENT, 1)
49+
50+
# Get the dimensions of the image
51+
width, height = texture_surface.get_rect().size
52+
53+
# Upload the image to OpenGL
54+
glTexImage2D( GL_TEXTURE_2D,
55+
0,
56+
3,
57+
width,
58+
height,
59+
0,
60+
GL_RGB,
61+
GL_UNSIGNED_BYTE,
62+
texture_data)
63+
64+
65+
clock = pygame.time.Clock()
66+
67+
tex_rotation = 0.0
68+
69+
while True:
70+
71+
for event in pygame.event.get():
72+
if event.type == QUIT:
73+
pygame.quit()
74+
quit()
75+
76+
77+
time_passed = clock.tick()
78+
time_passed_seconds = time_passed / 1000.
79+
tex_rotation += time_passed_seconds * 360.0 / 8.0
80+
81+
82+
# Clear the screen (similar to fill)
83+
glClear(GL_COLOR_BUFFER_BIT)
84+
85+
# Clear the model-view matrix
86+
glLoadIdentity()
87+
88+
# Set the modelview matrix
89+
glTranslatef(0.0, 0.0, -600.0)
90+
glRotate(tex_rotation, 1, 0, 0)
91+
92+
# Draw a quad (4 vertices, 4 texture coords)
93+
glBegin(GL_QUADS)
94+
95+
glTexCoord2f(0, 1)
96+
glVertex3f(-300, 300, 0)
97+
98+
glTexCoord2f(1, 1)
99+
glVertex3f(300, 300, 0)
100+
101+
glTexCoord2f(1, 0)
102+
glVertex3f(300, -300, 0)
103+
104+
glTexCoord2f(0, 0)
105+
glVertex3f(-300, -300, 0)
106+
107+
glEnd()
108+
109+
pygame.display.flip()
110+
111+
glDeleteTextures(texture_id)
112+
113+
if __name__ == "__main__":
114+
run()

Chapter 11/11-3.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
from OpenGL.GL import *
2+
from OpenGL.GLU import *
3+
4+
import pygame
5+
import os.path
6+
7+
class Material(object):
8+
9+
def init (self):
10+
11+
self.name = ""
12+
self.texture_fname = None
13+
self.texture_id = None
14+
15+
class FaceGroup(object):
16+
17+
def init (self):
18+
19+
self.tri_indices = []
20+
self.material_name = ""
21+
22+
class Model3D(object):
23+
24+
def init (self):
25+
26+
self.vertices = []
27+
self.tex_coords = []
28+
self.normals = []
29+
self.materials = {}
30+
self.face_groups = []
31+
# Display list id for quick rendering
32+
self.display_list_id = None

Chapter 11/11-4.py

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
def read_obj(self, fname):
2+
3+
current_face_group = None
4+
5+
file_in = open(fname)
6+
7+
for line in file_in:
8+
9+
# Parse command and data from each line
10+
words = line.split()
11+
command = words[0]
12+
data = words[1:]
13+
14+
if command == 'mtllib': # Material library
15+
16+
model_path = os.path.split(fname)[0]
17+
mtllib_path = os.path.join( model_path, data[0] )
18+
self.read_mtllib(mtllib_path)
19+
20+
elif command == 'v': # Vertex
21+
x, y, z = data
22+
vertex = (float(x), float(y), float(z))
23+
self.vertices.append(vertex)
24+
25+
elif command == 'vt': # Texture coordinate
26+
27+
s, t = data
28+
tex_coord = (float(s), float(t))
29+
self.tex_coords.append(tex_coord)
30+
31+
elif command == 'vn': # Normal
32+
33+
x, y, z = data
34+
normal = (float(x), float(y), float(z))
35+
self.normals.append(normal)
36+
37+
elif command == 'usemtl' : # Use material
38+
39+
current_face_group = FaceGroup()
40+
current_face_group.material_name = data[0]
41+
self.face_groups.append( current_face_group )
42+
43+
elif command == 'f':
44+
45+
assert len(data) == 3, "Sorry, only triangles are supported"
46+
47+
# Parse indices from triples
48+
for word in data:
49+
vi, ti, ni = word.split('/')
50+
indices = (int(vi) - 1, int(ti) - 1, int(ni) - 1)
51+
current_face_group.tri_indices.append(indices)
52+
53+
54+
for material in self.materials.values():
55+
56+
model_path = os.path.split(fname)[0]
57+
texture_path = os.path.join(model_path, material.texture_fname)
58+
texture_surface = pygame.image.load(texture_path)
59+
texture_data = pygame.image.tostring(texture_surface, 'RGB', True)
60+
61+
material.texture_id = glGenTextures(1)
62+
glBindTexture(GL_TEXTURE_2D, material.texture_id)
63+
64+
glTexParameteri( GL_TEXTURE_2D,
65+
GL_TEXTURE_MAG_FILTER,
66+
GL_LINEAR)
67+
glTexParameteri( GL_TEXTURE_2D,
68+
GL_TEXTURE_MIN_FILTER,
69+
GL_LINEAR_MIPMAP_LINEAR)
70+
71+
glPixelStorei(GL_UNPACK_ALIGNMENT,1)
72+
width, height = texture_surface.get_rect().size
73+
gluBuild2DMipmaps( GL_TEXTURE_2D,
74+
3,
75+
width,
76+
height,
77+
GL_RGB,
78+
GL_UNSIGNED_BYTE,
79+
texture_data)

Chapter 11/11-5.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
def read_mtllib(self, mtl_fname):
2+
3+
file_mtllib = open(mtl_fname)
4+
for line in file_mtllib:
5+
6+
words = line.split()
7+
command = words[0]
8+
data = words[1:]
9+
10+
if command == 'newmtl':
11+
material = Material()
12+
material.name = data[0]
13+
self.materials[data[0]] = material
14+
15+
elif command == 'map_Kd':
16+
material.texture_fname = data[0]

Chapter 11/11-6.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
def draw(self):
2+
3+
vertices = self.vertices
4+
tex_coords = self.tex_coords
5+
normals = self.normals
6+
7+
for face_group in self.face_groups:
8+
9+
material = self.materials[face_group.material_name]
10+
glBindTexture(GL_TEXTURE_2D, material.texture_id)
11+
12+
glBegin(GL_TRIANGLES)
13+
for vi, ti, ni in face_group.tri_indices:
14+
glTexCoord2fv( tex_coords[ti] )
15+
glNormal3fv( normals[ni] )
16+
glVertex3fv( vertices[vi] )
17+
glEnd()
18+
19+
20+
def draw_quick(self):
21+
22+
if self.display_list_id is None:
23+
self.display_list_id = glGenLists(1)
24+
glNewList(self.display_list_id, GL_COMPILE)
25+
self.draw()
26+
glEndList()
27+
28+
glCallList(self.display_list_id)

Chapter 11/11-7.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
def __del__(self):
2+
3+
#Called when the model is cleaned up by Python
4+
self.free_resources()
5+
6+
def free_resources(self):
7+
8+
# Delete the display list and textures
9+
if self.display_list_id is not None:
10+
glDeleteLists(self.display_list_id, 1)
11+
self.display_list_id = None
12+
13+
# Delete any textures we used
14+
for material in self.materials.values():
15+
if material.texture_id is not None:
16+
glDeleteTextures(material.texture_id)
17+
18+
# Clear all the materials
19+
self.materials.clear()
20+
21+
# Clear the geometry lists
22+
del self.vertices[:]
23+
del self.tex_coords[:]
24+
del self.normals[:]
25+
del self.face_groups[:]

Chapter 11/11-8.py

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
from math import radians
2+
3+
from OpenGL.GL import *
4+
from OpenGL.GLU import *
5+
6+
import pygame
7+
from pygame.locals import *
8+
9+
# Import the Model3D class
10+
import model3d
11+
12+
SCREEN_SIZE = (800, 600)
13+
14+
def resize(width, height):
15+
16+
glViewport(0, 0, width, height)
17+
glMatrixMode(GL_PROJECTION)
18+
glLoadIdentity()
19+
gluPerspective(60.0, float(width)/height, .1, 1000.)
20+
glMatrixMode(GL_MODELVIEW)
21+
glLoadIdentity()
22+
23+
24+
def init():
25+
26+
# Enable the GL features we will be using
27+
glEnable(GL_DEPTH_TEST)
28+
glEnable(GL_LIGHTING)
29+
glEnable(GL_COLOR_MATERIAL)
30+
glEnable(GL_TEXTURE_2D)
31+
glEnable(GL_CULL_FACE)
32+
33+
glShadeModel(GL_SMOOTH)
34+
glClearColor(1.0, 1.0, 1.0, 0.0) # white
35+
36+
# Set the material
37+
glMaterial(GL_FRONT, GL_AMBIENT, (0.0, 0.0, 0.0, 1.0))
38+
glMaterial(GL_FRONT, GL_DIFFUSE, (0.2, 0.2, 0.2, 1.0))
39+
glMaterial(GL_FRONT, GL_SPECULAR, (1.0, 1.0, 1.0, 1.0))
40+
glMaterial(GL_FRONT, GL_SHININESS, 10.0)
41+
42+
# Set light parameters
43+
glLight(GL_LIGHT0, GL_AMBIENT, (0.0, 0.0, 0.0, 1.0))
44+
glLight(GL_LIGHT0, GL_DIFFUSE, (0.4, 0.4, 0.4, 1.0))
45+
glLight(GL_LIGHT0, GL_SPECULAR, (1.0, 1.0, 1.0, 1.0))
46+
47+
# Enable light 1 and set position
48+
glEnable(GL_LIGHT0)
49+
glLight(GL_LIGHT0, GL_POSITION, (0, .5, 1, 0))
50+
51+
52+
def run():
53+
54+
pygame.init()
55+
screen = pygame.display.set_mode(SCREEN_SIZE, HWSURFACE|OPENGL|DOUBLEBUF)
56+
57+
resize(*SCREEN_SIZE)
58+
init()
59+
60+
clock = pygame.time.Clock()
61+
62+
# Read the model
63+
tank_model = model3d.Model3D()
64+
tank_model.read_obj('mytank.obj')
65+
66+
rotation = 0.0
67+
68+
while True:
69+
70+
for event in pygame.event.get():
71+
if event.type == QUIT:
72+
pygame.quit()
73+
quit()
74+
75+
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
76+
77+
time_passed = clock.tick()
78+
time_passed_seconds = time_passed / 1000.0
79+
80+
glLoadIdentity()
81+
glRotatef(15, 1, 0, 0)
82+
glTranslatef(0.0, -1.5, -3.5)
83+
84+
rotation += time_passed_seconds * 45.0
85+
glRotatef(rotation, 0, 1, 0)
86+
87+
tank_model.draw_quick()
88+
89+
pygame.display.flip()
90+
91+
92+
if __name__ == "__main__":
93+
run()

Chapter 11/bodytex.jpg

220 KB
Loading

Chapter 11/booktex.png

217 KB
Loading

0 commit comments

Comments
 (0)