Class: Terrain

Inherits:
Object
  • Object
show all
Defined in:
lib/examples/GlStuff/terrain.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(surface) ⇒ Terrain

Returns a new instance of Terrain.



5
6
7
8
9
10
# File 'lib/examples/GlStuff/terrain.rb', line 5

def initialize(surface)
	@height_map_image = surface
	@terrain_normals = Array.new
	load_from_surface()
	#compute_normals()
end

Instance Attribute Details

#height_map_imageObject

Returns the value of attribute height_map_image.



3
4
5
# File 'lib/examples/GlStuff/terrain.rb', line 3

def height_map_image
  @height_map_image
end

#terrain_grid_lengthObject

Returns the value of attribute terrain_grid_length.



3
4
5
# File 'lib/examples/GlStuff/terrain.rb', line 3

def terrain_grid_length
  @terrain_grid_length
end

#terrain_grid_widthObject

Returns the value of attribute terrain_grid_width.



3
4
5
# File 'lib/examples/GlStuff/terrain.rb', line 3

def terrain_grid_width
  @terrain_grid_width
end

#terrain_normalsObject

Returns the value of attribute terrain_normals.



3
4
5
# File 'lib/examples/GlStuff/terrain.rb', line 3

def terrain_normals
  @terrain_normals
end

Instance Method Details

#compute_normalsObject



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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
# File 'lib/examples/GlStuff/terrain.rb', line 125

def compute_normals
	 norm1,norm2 = Vector3.new, Vector3.new
	 @terrain_normals = Array.new(@terrain_grid_width * @terrain_grid_length * 3)

	return if @terrain_normals.nil?
	


	for i in 0..@terrain_grid_length - 1
		for j in 0..@terrain_grid_width - 1
			norm1.clear
			norm2.clear
			if i == 0 && j == 0
				norm1 = terrain_cross_product(0,0, 0,1, 1,0)
				norm1.normalize()
			elsif j == @terrain_grid_width-1 && i == @terrain_grid_length-1
				norm1 = terrain_cross_product(j,i, j,i-1, j-1,i)
				norm1.normalize()
			elsif j == 0 && i == @terrain_grid_length-1
				norm1 = terrain_cross_product(j,i, j,i-1, j+1, i)
				norm1.normalize()
			elsif j == @terrain_grid_width - 1 && i == 0
				norm1 = terrain_cross_product(j,i, j,i+1, j-1,i)
			elsif i == 0
				norm1 = terrain_cross_product(j,0, j-1,0, j,1)
				norm1.normalize
				norm2 = terrain_cross_product(j,0, j,1, j+1,0)
				norm2.normalize
				norm1 += norm2
			elsif j == 0
				norm1 = terrain_cross_product(0,i, 1,i, 0,i-1)
				norm1.normalize
				norm2 = terrain_cross_product(j,0, j,1, j+1,0)
				norm2.normalize
				norm1 += norm2
			elsif i == @terrain_grid_length-1
				norm1 = terrain_cross_product(j,i, j,i-1, j+1,i);
				norm1.normalize
				norm2 = terrain_cross_product(j,i, j+1,i, j,i-1);
				norm2.normalize
				norm1 += norm2
			elsif (j == @terrain_grid_width-1)
				norm1 = terrain_cross_product(j,i, j,i-1, j-1,i);
				norm1.normalize
				norm2 = terrain_cross_product(j,i, j-1,i, j,i+1);
				norm2.normalize
				norm1 += norm2;
			else
				norm1 = terrain_cross_product(j,i, j-1,i, j-1,i+1).unit
				norm2 = terrain_cross_product(j,i, j-1,i+1, j,i+1).unit
				norm1 += norm2;

				norm2 = terrain_cross_product(j,i, j,i+1, j+1,i+1).unit
				norm1 += norm2;
				
				norm2 = terrain_cross_product(j,i, j+1,i+1, j+1,i).unit
				norm1 += norm2

				norm2 = terrain_cross_product(j,i, j+1,i, j+1,i-1).unit
				norm1 += norm2

				norm2 = terrain_cross_product(j,i, j+1,i-1, j,i-1).unit
				norm1 += norm2

				norm2 = terrain_cross_product(j,i, j,i-1, j-1,i-1).unit
				norm1 += norm2

				norm2 = terrain_cross_product(j,i, j-1,i-1, j-1,i).unit
				norm1 += norm2
			end
			
			norm1.normalize			
			norm1.z = - norm1.z

			component = 0
			for k in 0..3 
				case k
				when 0
					component = norm1.x
				when 1
					component = norm1.y
				when 2
					component = norm1.z
				end
				@terrain_normals[3*(i*@terrain_grid_width + j) + k] = component;
			end
		end
	end
end

#create_display_list(x_offset, y_offset, z_offset) ⇒ Object



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
# File 'lib/examples/GlStuff/terrain.rb', line 55

def create_display_list(x_offset, y_offset, z_offset)
	
	start_w = (@terrain_grid_width / 2.0) - @terrain_grid_width
	start_l = (-@terrain_grid_length / 2.0) + @terrain_grid_length

	terrain_dl = glGenLists(1)
	
	glNewList(terrain_dl, GL_COMPILE)

	for i in 0..(@terrain_grid_length - 2)
		glBegin(GL_TRIANGLE_STRIP)
		for j in 0..(@terrain_grid_width - 2)

			unless @terrain_normals.empty?
				glNormal3f(@terrain_normals[3*((i+1)*@terrain_grid_width)],
						   @terrain_normals[3*((i+1)*@terrain_grid_width)+1],
						   @terrain_normals[3*((i+1)*@terrain_grid_width)+2] )
			end
			glVertex3f(
				start_w + j,
				@terrain_heights[(i+1)*@terrain_grid_width + (j)],
				start_l - (i+1)
			)			

			unless @terrain_normals.empty?
				glNormal3f(@terrain_normals[3*(i+@terrain_grid_width + j)],
						   @terrain_normals[3*(i+@terrain_grid_width + j)+1],
						   @terrain_normals[3*(i+@terrain_grid_width + j)+2] )
			end

			aux = 3 * ((i+1) * @terrain_grid_length + j)
			glVertex3f( (start_w + j),
						@terrain_heights[(i * @terrain_grid_width) + j],
						(start_l - i)
						)
		end
		glEnd()
	end

	glEndList()

	terrain_dl
end

#load_from_surfaceObject



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
# File 'lib/examples/GlStuff/terrain.rb', line 12

def load_from_surface
	mode = @height_map_image.depth
	puts mode
	@terrain_grid_width = @height_map_image.width
	@terrain_grid_length = @height_map_image.height
	@terrain_heights = Array.new( @terrain_grid_width * @terrain_grid_length)
	pixels = @height_map_image.pixels()
	#puts pixels
	puts pixels.size
	for i in 0..@terrain_grid_length-1
		for j in 0..@terrain_grid_width-1
			#aux = mode * (i * @terrain_grid_width + j)
			#index = aux + (mode - 1)
			#puts@height_map_image.get_at(i,j)[1]
			#if index < pixels.size
				point_height = @height_map_image.get_at(i,j)[0] / 255.0
				#puts point_height
			#end
			@terrain_heights[i * @terrain_grid_width + j] = point_height
		end
	end

	for i in 0..10
		#puts @terrain_heights[i * 1000]
	end
end

#terrain_cross_product(x1, z1, x2, z2, x3, z3) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/examples/GlStuff/terrain.rb', line 39

def terrain_cross_product(x1, z1, x2, z2, x3, z3)
	v1 = v2 =  aux = Vector3.new

	v1.x = (x2 - x1 )
	v1.y = -@terrain_heights[z1 * @terrain_grid_width + x1] + 
				@terrain_heights[z2 * @terrain_grid_width + x2]
	v1.z = (z2 - z1)

	v2.x = (x3 - x1)
	v2.y = -@terrain_heights[z1 * @terrain_grid_width + x1] + 
				@terrain_heights[z3 * @terrain_grid_width + x3]
	v2.z = (z3 - z1)

	v1 -= v1.crossProduct(v2)
end

#terrain_scale(minnum, maxnum) ⇒ Object



100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/examples/GlStuff/terrain.rb', line 100

def terrain_scale(minnum, maxnum)
	if minnum > maxnum
		aux = minnum
		minnum = maxnum
		maxnum = aux
	end

	amp = maxnum - minnum

	total = @terrain_grid_width * @terrain_grid_length

	min_val = @terrain_heights.min
	max_val = @terrain_heights.max
	for i in 0..total-2
		height = (@terrain_heights[i] - min_val) / (max_val -	 min_val)
		if height == nil
			next
		end
		@terrain_heights[i] = (height * amp) - minnum
		#puts @terrain_heights[i]
	end

	compute_normals()
end