Codebase list ruby-fxruby / a045fea lib / fox16 / glshapes.rb
a045fea

Tree @a045fea (Download .tar.gz)

glshapes.rb @a045fearaw · history · blame

  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
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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
require 'opengl'
require 'glu'

module Fox

  HANDLE_SIZE = 4.0

  #
  # OpenGL point object
  #
  class FXGLPoint < FXGLObject

    # Point position, in model coordinates (a 3-element array)
    attr_accessor :pos

    #
    # Returns an initialized FXGLPoint instance.
    # If no arguments are passed to #new, the initial point position is
    # (0.0, 0.0, 0.0). You can specify a different initial position by
    # passing in the x, y and z coordinates individually:
    #
    #     aPoint = FXGLPoint.new(x, y, z)
    #
    # or as a 3-element array:
    #
    #     aPoint = FXGLPoint.new([x, y, z])
    #
    def initialize(*args)
      super()
      if args.length == 0
        @pos = [0.0, 0.0, 0.0]
      elsif args.length == 3
        @pos = [args[0], args[1], args[2]]
      else
        @pos = args[0]
      end
    end

    #
    # Return the bounding box (an FXRangef instance) for this point.
    #
    def bounds
      FXRangef.new(@pos[0], @pos[0], @pos[1], @pos[1], @pos[2], @pos[2])
    end

    #
    # Draw this point into _viewer_ (an FXGLViewer instance).
    #
    def draw(viewer)
      GL::Color(0.0, 0.0, 1.0)
      GL::PointSize(HANDLE_SIZE)
      GL::Begin(GL::POINTS)
      GL::Vertex(@pos)
      GL::End()
    end

    #
    # Perform hit test for this point in _viewer_ (an FXGLViewer instance).
    #
    def hit(viewer)
      GL::Begin(GL::POINTS)
      GL::Vertex(@pos)
      GL::End()
    end
  end

  #
  # OpenGL line object
  #
  class FXGLLine < FXGLObject

    # Starting point for line [FXGLPoint]
    attr_accessor :fm

    # End point for line [FXGLPoint]
    attr_accessor :to

    #
    # Return an initialized FXGLLine instance.
    #
    # If no arguments are passed to #new, the initial starting and ending
    # points for the line are (-0.5, 0.0, 0.0) and (0.5, 0.0, 0.0), respectively.
    # You can specify different initial start and end points by passing in
    # another FXGLLine instance from which to copy the start and end point
    # values, e.g.
    #
    #     aLine = FXGLLine.new(anotherLine)
    #
    # or by passing in the x, y and z coordinates individually:
    #
    #     aLine = FXGLLine.new(x0, y0, z0, x1, y1, z1)
    #
    def initialize(*args)
      super()
      if args.length == 0
	@fm = FXGLPoint.new(-0.5, 0.0, 0.0)
	@to = FXGLPoint.new( 0.5, 0.0, 0.0)
      elsif args.length == 1
	@fm = args[0].fm
	@to = args[0].to
      else
	@fm = FXGLPoint.new(args[0], args[1], args[2])
	@to = FXGLPoint.new(args[3], args[4], args[5])
      end
    end

    #
    # Return the bounding box (an FXRangef instance) for this line.
    #
    def bounds
      FXRangef.new([@fm.pos[0], @to.pos[0]].min,
                  [@fm.pos[0], @to.pos[0]].max,
                  [@fm.pos[1], @to.pos[1]].min,
                  [@fm.pos[1], @to.pos[1]].max,
                  [@fm.pos[2], @to.pos[2]].min,
                  [@fm.pos[2], @to.pos[2]].max)
    end

    #
    # Draw this line into _viewer_ (an FXGLViewer instance).
    #
    def draw(viewer)
      GL::Color(1.0, 0.0, 0.0)
      GL::PointSize(HANDLE_SIZE)
      GL::Begin(GL::LINES)
      GL::Vertex(@fm.pos)
      GL::Vertex(@to.pos)
      GL::End()
    end

    #
    # Perform hit-test for this line in _viewer_ (an FXGLViewer instance).
    #
    def hit(viewer)
      GL::Begin(GL::LINES)
      GL::Vertex(@fm.pos)
      GL::Vertex(@to.pos)
      GL::End()
    end
  end

  #
  # OpenGL cube object
  #
  class FXGLCube < FXGLShape

    # Cube width [Float]
    attr_accessor :width

    # Cube height [Float]
    attr_accessor :height

    # Cube depth [Float]
    attr_accessor :depth

    #
    # Return an initialized FXGLCube instance.
    #
    # One option is to initialize the cube with a specified origin,
    # width, height and depth:
    #
    #     aCube = FXGLCube.new(x, y, z, w, h, d)
    #
    # If left unspecified, the width (_w_), height (_h_) and depth (_d_)
    # default to 1.0.
    #
    # Another option is to initialize the cube with a specified origin,
    # width, height, depth and material:
    #
    #     aCube = FXGLCube.new(x, y, z, w, h, d, material)
    #
    # where the _material_ is an FXMaterial instance.
    #
    def initialize(*args)
      if args.length == 7
	super(args[0], args[1], args[2], SHADING_SMOOTH|STYLE_SURFACE,
              args[6], args[6])
      else
	super(args[0], args[1], args[2], SHADING_SMOOTH|STYLE_SURFACE)
      end
      @width = args[3] ? args[3] : 1.0
      @height = args[4] ? args[4] : 1.0
      @depth = args[5] ? args[5] : 1.0
      setRange(FXRangef.new(-0.5*@width, 0.5*@width,
                           -0.5*@height, 0.5*@height,
                           -0.5*@depth, 0.5*@depth))
    end

    #
    # Draws this cube into _viewer_ (an FXGLViewer instance).
    #
    def drawshape(viewer)
      xmin, xmax = -0.5*@width, 0.5*@width
      ymin, ymax = -0.5*@height, 0.5*@height
      zmin, zmax = -0.5*@depth, 0.5*@depth

      # Draw low face
      GL::Begin(GL::TRIANGLE_STRIP)
	GL::Normal(0.0, 0.0, -1.0)
	GL::Vertex(xmin, ymin, zmin)
	GL::Vertex(xmin, ymax, zmin)
	GL::Vertex(xmax, ymin, zmin)
	GL::Vertex(xmax, ymax, zmin)
      GL::End()

      # Draw east face
      GL::Begin(GL::TRIANGLE_STRIP)
	GL::Normal(1.0, 0.0, 0.0)
	GL::Vertex(xmax, ymin, zmin)
	GL::Vertex(xmax, ymax, zmin)
	GL::Vertex(xmax, ymin, zmax)
	GL::Vertex(xmax, ymax, zmax)
      GL::End()

      # Draw high face
      GL::Begin(GL::TRIANGLE_STRIP)
	GL::Normal(0.0, 0.0, 1.0)
	GL::Vertex(xmax, ymin, zmax)
	GL::Vertex(xmax, ymax, zmax)
	GL::Vertex(xmin, ymin, zmax)
	GL::Vertex(xmin, ymax, zmax)
      GL::End()

      # Draw west face
      GL::Begin(GL::TRIANGLE_STRIP)
	GL::Normal(-1.0, 0.0, 0.0)
	GL::Vertex(xmin, ymin, zmax)
	GL::Vertex(xmin, ymax, zmax)
	GL::Vertex(xmin, ymin, zmin)
	GL::Vertex(xmin, ymax, zmin)
      GL::End()

      # Draw north face
      GL::Begin(GL::TRIANGLE_STRIP)
	GL::Normal(0.0, 1.0, 0.0)
	GL::Vertex(xmin, ymax, zmin)
	GL::Vertex(xmin, ymax, zmax)
	GL::Vertex(xmax, ymax, zmin)
	GL::Vertex(xmax, ymax, zmax)
      GL::End()

      # Draw south face
      GL::Begin(GL::TRIANGLE_STRIP)
	GL::Normal(0.0, -1.0, 0.0)
	GL::Vertex(xmin, ymin, zmax)
	GL::Vertex(xmin, ymin, zmin)
	GL::Vertex(xmax, ymin, zmax)
	GL::Vertex(xmax, ymin, zmin)
      GL::End()
    end
  end

  #
  # OpenGL cone object
  #
  class FXGLCone < FXGLShape
    # Cone fidelity
    SLICES_NUMBER = 20
    STACKS_NUMBER = 20
    LOOPS = 4

    # Cone height [Float]
    attr_accessor :height

    # Cone base radius [Float]
    attr_accessor :radius

    # Number of slices (default is 20) [Integer]
    attr_accessor :slices

    # Number of stacks (default is 20) [Integer]
    attr_accessor :stacks

    # Number of loops (default is 4) [Integer]
    attr_accessor :loops

    #
    # Returns an initialized FXGLCone instance.
    #
    # One option is to initialize the cone with a specified origin,
    # height and radius:
    #
    #     aCone = FXGLCone.new(x, y, z, h, r)
    #
    # If left unspecified, the height (_h_) and radius (_r_) default to 1.0.
    #
    # Another option is to initialize the cone with a specified origin,
    # height, radius and material:
    #
    #     aCone = FXGLCone.new(x, y, z, h, r, material)
    #
    # where the _material_ is an FXMaterial instance.
    #
    def initialize(*args)
      if args.length == 5
	super(args[0], args[1], args[2], SHADING_SMOOTH|STYLE_SURFACE)
      elsif args.length == 6
	super(args[0], args[1], args[2], SHADING_SMOOTH|STYLE_SURFACE,
              args[5], args[5])
      end
      @height = args[3] ? args[3] : 1.0
      @radius = args[4] ? args[4] : 1.0
      @slices = SLICES_NUMBER
      @stacks = STACKS_NUMBER
      @loops  = LOOPS
      setRange(FXRangef.new(-@radius, @radius, 0, @height, -@radius, @radius))
    end

    #
    # Draw this cone into _viewer_ (an FXGLViewer instance).
    #
    def drawshape(viewer)
      quad = GLU::NewQuadric()
      GLU::QuadricDrawStyle(quad, GLU::FILL)
      GL::PushMatrix()
      GL::Rotate(-90, 1, 0, 0)
      GLU::Cylinder(quad, @radius, 0, @height, @slices, @stacks)
      GLU::QuadricOrientation(quad, GLU::INSIDE)
      GLU::Disk(quad, 0, @radius, @slices, @loops)
      GLU::DeleteQuadric(quad)
      GL::PopMatrix()
    end
  end

  #
  # OpenGL cylinder object
  #
  class FXGLCylinder < FXGLShape
    # Cylinder fidelity
    SLICES_NUMBER = 20
    STACKS_NUMBER = 20
    LOOPS = 4

    # Cylinder height [Float]
    attr_accessor :height

    # Cylinder radius [Float]
    attr_accessor :radius

    # Number of slices (default is 20) [Integer]
    attr_accessor :slices

    # Number of stacks (default is 20) [Integer]
    attr_accessor :stacks

    # Number of loops (default is 4) [Integer]
    attr_accessor :loops

    #
    # Returns an initialized FXGLCylinder instance.
    #
    # One option is to initialize the cylinder with a specified origin,
    # height and radius:
    #
    #     aCylinder = FXGLCylinder.new(x, y, z, h, r)
    #
    # If left unspecified, the height (_h_) and radius (_r_) default to 1.0.
    #
    # Another option is to initialize the cylinder with a specified origin,
    # height, radius and material:
    #
    #     aCylinder = FXGLCylinder.new(x, y, z, h, r, material)
    #
    # where the _material_ is an FXMaterial instance.
    #
    def initialize(*args)
      if args.length == 5
	super(args[0], args[1], args[2], SHADING_SMOOTH|STYLE_SURFACE)
      else
	super(args[0], args[1], args[2], SHADING_SMOOTH|STYLE_SURFACE,
              args[5], args[5])
      end
      @height = args[3] ? args[3] : 1.0
      @radius = args[4] ? args[4] : 1.0
      @slices = SLICES_NUMBER
      @stacks = STACKS_NUMBER
      @loops  = LOOPS
      setRange(FXRangef.new(-@radius, @radius, 0, @height, -@radius, @radius))
    end

    #
    # Draw this cylinder into _viewer_ (an FXGLViewer instance).
    #
    def drawshape(viewer)
      quad = GLU::NewQuadric()
      GLU::QuadricDrawStyle(quad, GLU::FILL)
      GL::PushMatrix()
      GL::Rotate(-90, 1, 0, 0)
      GLU::Cylinder(quad, @radius, @radius, @height, @slices, @stacks)
      GLU::QuadricOrientation(quad, GLU::INSIDE)
      GLU::Disk(quad, 0, @radius, @slices, @loops)
      GL::Translate(0, 0, @height)
      GLU::QuadricOrientation(quad, GLU::OUTSIDE)
      GLU::Disk(quad, 0, @radius, @slices, @loops)
      GL::PopMatrix()
      GLU::DeleteQuadric(quad)
    end
  end

  #
  # OpenGL sphere object
  #
  class FXGLSphere < FXGLShape
    # Sphere fidelity
    SLICES_NUMBER = 20
    STACKS_NUMBER = 20

    # Sphere radius [Float]
    attr_accessor :radius

    # Number of slices (default is 20) [Integer]
    attr_accessor :slices

    # Number of stacks (default is 20) [Integer]
    attr_accessor :stacks

    #
    # Returns an initialized FXGLSphere instance.
    #
    # One option is to initialize the sphere with a specified origin and
    # radius:
    #
    #     aSphere = FXGLSphere.new(x, y, z, r)
    #
    # If left unspecified, the radius (_r_) defaults to 1.0.
    #
    # Another option is to initialize the sphere with a specified origin,
    # radius and material:
    #
    #     aSphere = FXGLSphere.new(x, y, z, r, material)
    #
    # where the _material_ is an FXMaterial instance.
    #
    def initialize(*args)
      if args.length == 4
        super(args[0], args[1], args[2], SHADING_SMOOTH|STYLE_SURFACE)
      else
        super(args[0], args[1], args[2], SHADING_SMOOTH|STYLE_SURFACE,
        args[4], args[4])
      end
      @radius = args[3] ? args[3] : 1.0
      @slices = SLICES_NUMBER
      @stacks = STACKS_NUMBER
      setRange(FXRangef.new(-@radius, @radius, -@radius, @radius, -@radius, @radius))
    end

    #
    # Draw this sphere into _viewer_ (an FXGLViewer instance).
    #
    def drawshape(viewer)
      quad = GLU::NewQuadric()
      GLU::QuadricDrawStyle(quad, GLU::FILL)
      GLU::Sphere(quad, @radius, @slices, @stacks)
      GLU::DeleteQuadric(quad)
    end
  end
end