Class: BgUtils::Skybox

Inherits:
Object
  • Object
show all
Defined in:
lib/bg_utils.rb

Instance Method Summary collapse

Constructor Details

#initialize(filename) ⇒ Skybox

Returns a new instance of Skybox.



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/bg_utils.rb', line 3

def initialize(filename)
	# filename is supposed to lead to a composed texture like below :
	#   X
	#  XXXX
	#   X

	texture = Gosu::Image.new(filename, :retro=>true)
	@tile_size = texture.width / 4
	texture = nil # we don't need it anymore
	@textures = {
		:y_pos => Gosu::Image.new(filename, :rect => [@tile_size, 0, @tile_size, @tile_size]),
		:x_neg => Gosu::Image.new(filename, :rect => [0, @tile_size, @tile_size, @tile_size]),
		:z_neg => Gosu::Image.new(filename, :rect => [@tile_size, @tile_size, @tile_size, @tile_size]),
		:x_pos => Gosu::Image.new(filename, :rect => [2 * @tile_size, @tile_size, @tile_size, @tile_size]),
		:z_pos => Gosu::Image.new(filename, :rect => [3 * @tile_size, @tile_size, @tile_size, @tile_size]),
		:y_neg => Gosu::Image.new(filename, :rect => [@tile_size, 2 * @tile_size, @tile_size, @tile_size])
	}
	self.gen_list
end

Instance Method Details

#draw(x = 0, y = 0, z = 0, rot_pitch = 0, rot_yaw = 0) ⇒ Object



88
89
90
91
92
93
94
95
96
# File 'lib/bg_utils.rb', line 88

def draw(x = 0, y = 0, z = 0, rot_pitch = 0, rot_yaw = 0)		
	glPushMatrix
		glTranslatef(x, y, z)
		glRotatef(rot_pitch, 1, 0, 0)
		glRotatef(rot_yaw, 0, 1, 0)
		glCallList(@display_list)
	glPopMatrix
	
end

#gen_listObject



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/bg_utils.rb', line 33

def gen_list
	@display_list = glGenLists(1)
  	    glNewList(@display_list, GL_COMPILE)
		glDepthMask(GL_FALSE)
		tex = switch_texture(:z_neg)
		glBegin(GL_QUADS)
			glTexCoord2d(tex.left, tex.top); 		glVertex3f(-1, 1, -1)
			glTexCoord2d(tex.left, tex.bottom); 	glVertex3f(-1, -1, -1)
			glTexCoord2d(tex.right, tex.bottom); 	glVertex3f(1, -1, -1)
			glTexCoord2d(tex.right, tex.top); 		glVertex3f(1, 1, -1)
		glEnd
		tex = switch_texture(:z_pos)
		glBindTexture(GL_TEXTURE_2D, tex.tex_name)
		glBegin(GL_QUADS)
			glTexCoord2d(tex.left, tex.top); 		glVertex3f(1, 1, 1)
			glTexCoord2d(tex.left, tex.bottom); 	glVertex3f(1, -1, 1)
			glTexCoord2d(tex.right, tex.bottom); 	glVertex3f(-1, -1, 1)
			glTexCoord2d(tex.right, tex.top); 		glVertex3f(-1, 1, 1)
		glEnd
		tex = switch_texture(:x_neg)
		glBindTexture(GL_TEXTURE_2D, tex.tex_name)
		glBegin(GL_QUADS)
			glTexCoord2d(tex.left, tex.top); 		glVertex3f(-1, 1, 1)
			glTexCoord2d(tex.left, tex.bottom); 	glVertex3f(-1, -1, 1)
			glTexCoord2d(tex.right, tex.bottom); 	glVertex3f(-1, -1, -1)
			glTexCoord2d(tex.right, tex.top); 		glVertex3f(-1, 1, -1)
		glEnd
		tex = switch_texture(:x_pos)
		glBindTexture(GL_TEXTURE_2D, tex.tex_name)
		glBegin(GL_QUADS)
			glTexCoord2d(tex.left, tex.top); 		glVertex3f(1, 1, -1)
			glTexCoord2d(tex.left, tex.bottom); 	glVertex3f(1, -1, -1)
			glTexCoord2d(tex.right, tex.bottom); 	glVertex3f(1, -1, 1)
			glTexCoord2d(tex.right, tex.top); 		glVertex3f(1, 1, 1)
		glEnd
		tex = switch_texture(:y_pos)
		glBindTexture(GL_TEXTURE_2D, tex.tex_name)
		glBegin(GL_QUADS)
			glTexCoord2d(tex.left, tex.top); 		glVertex3f(-1, 1, 1)
			glTexCoord2d(tex.left, tex.bottom); 	glVertex3f(-1, 1, -1)
			glTexCoord2d(tex.right, tex.bottom); 	glVertex3f(1, 1, -1)
			glTexCoord2d(tex.right, tex.top); 		glVertex3f(1, 1, 1)
		glEnd
		tex = switch_texture(:y_neg)
		glBindTexture(GL_TEXTURE_2D, tex.tex_name)
		glBegin(GL_QUADS)
			glTexCoord2d(tex.left, tex.top); 		glVertex3f(-1, -1, -1)
			glTexCoord2d(tex.left, tex.bottom); 	glVertex3f(-1, -1, 1)
			glTexCoord2d(tex.right, tex.bottom); 	glVertex3f(1, -1, 1)
			glTexCoord2d(tex.right, tex.top); 		glVertex3f(1, -1, -1)
		glEnd
		glDepthMask(GL_TRUE)	        	
      	glEndList
end

#switch_texture(face) ⇒ Object



23
24
25
26
27
28
29
30
31
# File 'lib/bg_utils.rb', line 23

def switch_texture(face)
	tex = @textures[face].gl_tex_info
	glBindTexture(GL_TEXTURE_2D, tex.tex_name)
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST)
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST)
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP)
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP)
	return tex
end