1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 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
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
require 'opengl'
require 'glu'
require 'gosu'
include Gl, Glu

class Gosu::GLTexInfo
	def tex_coord_2d(x, y)
		if x >= 0.0 and x <= 1.0 and y >= 0.0 and y <= 1.0
			tex_x = self.left + x * (self.right - self.left)
			tex_y = (self.bottom + y * (self.top - self.bottom))
			glTexCoord2d(tex_x, tex_y)
		else
			raise("Opengl coordinates must be included in range 0.0 -> 1.0. Error with [#{x}, #{y}]")
		end
	end
end

class Vector2
	attr_accessor :x, :y
	def initialize(x, y)
		@x, @y = x, y
	end
end

class Vector3
	attr_accessor :x, :y, :z
	def initialize(x, y, z)
		@x, @y, @z = x, y, z
	end
end

class CubeModel
	def initialize(filename)
		tex_coords = Hash.new
		size = nil
		File.open(filename, 'r').readlines.each do |line|
			infos = line.chomp.split(':')
			case infos[0]
			when 'texture' then @texture = Gosu::Image.new(infos[1], {:retro=>true, :tileable=>true})
			when 'size' then size = infos[1].split(',').map {|e| e.to_i}
			when 'front' then tex_coords[:front] = infos[1].split(',').map {|e| e.to_i}
			when 'back' then tex_coords[:back] = infos[1].split(',').map {|e| e.to_i}
			when 'left' then tex_coords[:left] = infos[1].split(',').map {|e| e.to_i}
			when 'right' then tex_coords[:right] = infos[1].split(',').map {|e| e.to_i}
			when 'top' then tex_coords[:top] = infos[1].split(',').map {|e| e.to_i}
			when 'bottom' then tex_coords[:bottom] = infos[1].split(',').map {|e| e.to_i}
			end
		end
		size = Vector3.new(size[0], size[1], size[2])
		@cube = Cube.new(Vector3.new(0, 0, 0), size, tex_coords)
	end

	def draw(position = nil)
		@cube.center_point = position if position != nil
		@cube.draw(@texture)
	end
end

class Cube
	attr_accessor :center_point
	def initialize(center_point, size, texture_coordinates)
		@center_point, @size, @texture_coordinates = center_point, size, texture_coordinates
	end

	def get_corners(texture, face)
		top_left, top_right, bottom_left, bottom_right = nil, nil, nil, nil
		if face == :front or face == :back
			top_left = [@texture_coordinates[face][0].to_f / texture.width, 1.0 - (@texture_coordinates[face][1].to_f / texture.height)]
			bottom_right = [(@texture_coordinates[face][0] + @size.x).to_f / texture.width, 1.0 - ((@texture_coordinates[face][1] + @size.y).to_f / texture.height)]
			top_right = [bottom_right[0], top_left[1]]
			bottom_left = [top_left[0], bottom_right[1]]
		elsif face == :left or face == :right
			top_left = [@texture_coordinates[face][0].to_f / texture.width, 1.0 - (@texture_coordinates[face][1].to_f / texture.height)]
			bottom_right = [(@texture_coordinates[face][0] + @size.z).to_f / texture.width, 1.0 - ((@texture_coordinates[face][1] + @size.y).to_f / texture.height)]
			top_right = [bottom_right[0], top_left[1]]
			bottom_left = [top_left[0], bottom_right[1]]
		elsif face == :top or face == :bottom
			top_left = [@texture_coordinates[face][0].to_f / texture.width, 1.0 - (@texture_coordinates[face][1].to_f / texture.height)]
			bottom_right = [(@texture_coordinates[face][0] + @size.x).to_f / texture.width, 1.0 - ((@texture_coordinates[face][1] + @size.z).to_f / texture.height)]
			top_right = [bottom_right[0], top_left[1]]
			bottom_left = [top_left[0], bottom_right[1]]
		end
		[top_left, bottom_left, bottom_right, top_right]
	end

	def draw(texture = nil)
		if texture != nil
			glBindTexture(GL_TEXTURE_2D, texture.gl_tex_info.tex_name)
		end
		glPushMatrix
		glTranslate(@center_point.x, @center_point.y, @center_point.z)
		glScale(@size.x, @size.y, @size.z)

		glBegin(GL_QUADS)
			if @texture_coordinates.has_key?(:back)
				corners = get_corners(texture, :back)
				texture.gl_tex_info.tex_coord_2d(corners[3][0], corners[3][1]); glVertex3f(-0.5, 0.5, -0.5)
				texture.gl_tex_info.tex_coord_2d(corners[2][0], corners[2][1]); glVertex3f(-0.5, -0.5, -0.5)
				texture.gl_tex_info.tex_coord_2d(corners[1][0], corners[1][1]); glVertex3f(0.5, -0.5, -0.5)
				texture.gl_tex_info.tex_coord_2d(corners[0][0], corners[0][1]); glVertex3f(0.5, 0.5, -0.5)
			end
			if @texture_coordinates.has_key?(:front)
				corners = get_corners(texture, :front)
				texture.gl_tex_info.tex_coord_2d(corners[0][0], corners[0][1]); glVertex3f(-0.5, 0.5, 0.5)
				texture.gl_tex_info.tex_coord_2d(corners[1][0], corners[1][1]); glVertex3f(-0.5, -0.5, 0.5)
				texture.gl_tex_info.tex_coord_2d(corners[2][0], corners[2][1]); glVertex3f(0.5, -0.5, 0.5)
				texture.gl_tex_info.tex_coord_2d(corners[3][0], corners[3][1]); glVertex3f(0.5, 0.5, 0.5)
			end
			if @texture_coordinates.has_key?(:left)
			corners = get_corners(texture, :left)
				texture.gl_tex_info.tex_coord_2d(corners[0][0], corners[0][1]); glVertex3f(-0.5, 0.5, -0.5)
				texture.gl_tex_info.tex_coord_2d(corners[1][0], corners[1][1]); glVertex3f(-0.5, -0.5, -0.5)
				texture.gl_tex_info.tex_coord_2d(corners[2][0], corners[2][1]); glVertex3f(-0.5, -0.5, 0.5)
				texture.gl_tex_info.tex_coord_2d(corners[3][0], corners[3][1]); glVertex3f(-0.5, 0.5, 0.5)
			end
			if @texture_coordinates.has_key?(:right)
				corners = get_corners(texture, :right)
				texture.gl_tex_info.tex_coord_2d(corners[3][0], corners[3][1]); glVertex3f(0.5, 0.5, -0.5)
				texture.gl_tex_info.tex_coord_2d(corners[2][0], corners[2][1]); glVertex3f(0.5, -0.5, -0.5)
				texture.gl_tex_info.tex_coord_2d(corners[1][0], corners[1][1]); glVertex3f(0.5, -0.5, 0.5)
				texture.gl_tex_info.tex_coord_2d(corners[0][0], corners[0][1]); glVertex3f(0.5, 0.5, 0.5)
			end
			if @texture_coordinates.has_key?(:top)
				corners = get_corners(texture, :top)
				texture.gl_tex_info.tex_coord_2d(corners[0][0], corners[0][1]); glVertex3f(-0.5, 0.5, -0.5)
				texture.gl_tex_info.tex_coord_2d(corners[1][0], corners[1][1]); glVertex3f(-0.5, 0.5, 0.5)
				texture.gl_tex_info.tex_coord_2d(corners[2][0], corners[2][1]); glVertex3f(0.5, 0.5, 0.5)
				texture.gl_tex_info.tex_coord_2d(corners[3][0], corners[3][1]); glVertex3f(0.5, 0.5, -0.5)
			end
			if @texture_coordinates.has_key?(:bottom)
				corners = get_corners(texture, :bottom)
				texture.gl_tex_info.tex_coord_2d(corners[1][0], corners[1][1]); glVertex3f(-0.5, -0.5, -0.5)
				texture.gl_tex_info.tex_coord_2d(corners[0][0], corners[0][1]); glVertex3f(-0.5, -0.5, 0.5)
				texture.gl_tex_info.tex_coord_2d(corners[3][0], corners[3][1]); glVertex3f(0.5, -0.5, 0.5)
				texture.gl_tex_info.tex_coord_2d(corners[2][0], corners[2][1]); glVertex3f(0.5, -0.5, -0.5)
			end
		glEnd
		glPopMatrix
	end
end

class Window < Gosu::Window
	def initialize
		super(640, 480, false)
		self.caption = "Test Cube"
		@cube_model = CubeModel.new('crate.bgg')
	end

	def needs_cursor?; true; end

	def button_down(id)
		super
		exit if id == Gosu::KbEscape
	end

	def draw
		gl do 
			glEnable(GL_TEXTURE_2D)
			glEnable(GL_DEPTH_TEST)
			glClearColor(0.5, 0.5, 0.5, 1.0)
			glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
			glMatrixMode(GL_PROJECTION)
			glLoadIdentity
			gluPerspective(45.0, self.width.to_f / self.height.to_f, 1.0, 1000.0)
			glMatrixMode(GL_MODELVIEW)
			glLoadIdentity
			gluLookAt(0.0, 48.0, 48.0, 0.0, 0.0, 0.0, 0, 1, 0)
			@angle ||= 0.0
			glRotate(@angle, 1, 1, 0)
			@angle += 0.8

			@cube_model.draw(Vector3.new(0, 0, 0))
		end
	end
end

Window.new.show