-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.rb
166 lines (127 loc) · 6.04 KB
/
utils.rb
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
# encoding: UTF-8
class Vector2D
attr_accessor :x, :y
#--------------------------------------------------------------------------------------------------------------------------------
# initialize
#--------------------------------------------------------------------------------------------------------------------------------
def initialize(x, y)
@x, @y = x, y
end
end
class Vector3D < Vector2D
attr_accessor :z
#--------------------------------------------------------------------------------------------------------------------------------
# initialize
#--------------------------------------------------------------------------------------------------------------------------------
def initialize(x, y, z)
super(x,y)
@z = z
end
def normalise()
length = Math.sqrt((@x * @x) + (@y * @y) + (@z * @z))
@x /= length
@y /= length
@z /= length
end
def self.add(vector1, vector2)
end
end
class Vector4D < Vector3D
attr_accessor :t
#--------------------------------------------------------------------------------------------------------------------------------
# initialize
#--------------------------------------------------------------------------------------------------------------------------------
def initialize(x, y, z, t)
super(x,y,z)
@t = t
end
#--------------------------------------------------------------------------------------------------------------------------------
# to_matrix
#--------------------------------------------------------------------------------------------------------------------------------
def to_matrix()
return Matrix.column_vector([@x, @y, @z, @t])
end
end
class GLTexture
attr_reader :width, :height
#~ # GOSU IMAGE -> OPENGL TEXTURE FIX
#~ # SOLVES PROBLEM WITH REPETITION / CROPING
#~ #--------------------------------------------------------------------------------------------------------------------------------
#~ # initialize
#~ #--------------------------------------------------------------------------------------------------------------------------------
def initialize(p_win, filename, save_gosu_image = false, alpha = true)
filename.is_a?(Gosu::Image) ? gosu_image = filename : gosu_image = Gosu::Image.new(p_win, filename, true)
@width, @height = gosu_image.width, gosu_image.height
array_of_pixels = gosu_image.to_blob
@texture_id = glGenTextures(1)
glBindTexture(GL_TEXTURE_2D, @texture_id[0])
if alpha
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, gosu_image.width, gosu_image.height, 0, GL_RGBA, GL_UNSIGNED_BYTE, array_of_pixels)
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST)
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST)
else
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, gosu_image.width, gosu_image.height, 0, GL_RGB, GL_UNSIGNED_BYTE, array_of_pixels)
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST)
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST)
end
if save_gosu_image
@gosu_image = gosu_image
else
gosu_image = nil
end
end
#~ #--------------------------------------------------------------------------------------------------------------------------------
#~ # draw_2d
#~ #--------------------------------------------------------------------------------------------------------------------------------
def draw_2d(x, y, z, zoom = 1, color = Gosu::Color.new(255, 255, 255, 255))
if @gosu_image != nil
@gosu_image.draw(x, y, z, zoom, zoom, color)
end
end
#~ #--------------------------------------------------------------------------------------------------------------------------------
#~ # get_id
#~ #--------------------------------------------------------------------------------------------------------------------------------
def get_id
return @texture_id[0]
end
end
class ObjModel
#--------------------------------------------------------------------------------------------------------------------------------
# initialize
#--------------------------------------------------------------------------------------------------------------------------------
def initialize(filename, pivot = [0, 0, 0])
@v, @vt, @vn = Array.new, Array.new, Array.new
@pivot = pivot
v, vt, vn = Array.new, Array.new, Array.new
File.open(filename).readlines.each do |line|
if line.include?("v ")
ajusted = line.chomp.scanf("v %f %f %f")
v << [ajusted[0] - pivot[0], ajusted[1] - pivot[1], ajusted[2] - pivot[2]]
elsif line.include?("vt ")
vt << line.chomp.scanf("vt %f %f %f")
vt.last[1] = 1.0 - vt.last[1]
elsif line.include?("vn ")
vn << line.chomp.scanf("vn %f %f %f")
elsif line.include?("f ")
t = line.chomp.scanf("f %d/%d/%d %d/%d/%d %d/%d/%d")
@v += v[t[0] - 1]; @v += v[t[3] - 1]; @v += v[t[6] - 1]
@vt += vt[t[1] - 1]; @vt += vt[t[4] - 1]; @vt += vt[t[7] - 1]
@vn += vn[t[2] - 1]; @vn += vn[t[5] - 1]; @vn += vn[t[8] - 1]
end
end
end
#--------------------------------------------------------------------------------------------------------------------------------
# draw
#--------------------------------------------------------------------------------------------------------------------------------
def draw(position = [0, 0, 0], rot = 0, color = nil)
glPushMatrix
glTranslate(position[0], position[1], position[2])
glRotate(rot, 0, 1, 0)
glColor3ub(color[0], color[1], color[2]) if color != nil
glVertexPointer(3, GL_FLOAT, 0, @v)
glTexCoordPointer(3, GL_FLOAT, 0, @vt)
glNormalPointer(GL_FLOAT, 0, @vn)
glDrawArrays(GL_TRIANGLES, 0, @v.size / 3)
glPopMatrix
end
end