Class: Cosmos::GlShape

Inherits:
Object show all
Defined in:
lib/cosmos/gui/opengl/gl_shape.rb

Direct Known Subclasses

StlShape, TextureMappedSphere

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(x, y, z) ⇒ GlShape

Constructor for the StlShape class



29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/cosmos/gui/opengl/gl_shape.rb', line 29

def initialize(x, y, z)
  @color = [0.5, 0.5, 0.5, 1.0]
  @base_color = [0.5, 0.5, 0.5, 1.0]
  @position = [x, y, z]
  @rotation_x = nil
  @rotation_y = nil
  @rotation_z = nil
  @viewer = nil
  @front_material = GlMaterial.new
  @back_material = nil
  @tipText = nil
  @dragable = true
end

Instance Attribute Details

#base_colorObject

Returns the value of attribute base_color.



17
18
19
# File 'lib/cosmos/gui/opengl/gl_shape.rb', line 17

def base_color
  @base_color
end

#click_handlerObject

Returns the value of attribute click_handler.



18
19
20
# File 'lib/cosmos/gui/opengl/gl_shape.rb', line 18

def click_handler
  @click_handler
end

#colorObject

Returns the value of attribute color.



20
21
22
# File 'lib/cosmos/gui/opengl/gl_shape.rb', line 20

def color
  @color
end

#doubleclick_handlerObject

Returns the value of attribute doubleclick_handler.



19
20
21
# File 'lib/cosmos/gui/opengl/gl_shape.rb', line 19

def doubleclick_handler
  @doubleclick_handler
end

#dragableObject

Returns the value of attribute dragable.



26
27
28
# File 'lib/cosmos/gui/opengl/gl_shape.rb', line 26

def dragable
  @dragable
end

#positionObject

Returns the value of attribute position.



21
22
23
# File 'lib/cosmos/gui/opengl/gl_shape.rb', line 21

def position
  @position
end

#rotation_xObject

Returns the value of attribute rotation_x.



22
23
24
# File 'lib/cosmos/gui/opengl/gl_shape.rb', line 22

def rotation_x
  @rotation_x
end

#rotation_yObject

Returns the value of attribute rotation_y.



23
24
25
# File 'lib/cosmos/gui/opengl/gl_shape.rb', line 23

def rotation_y
  @rotation_y
end

#rotation_zObject

Returns the value of attribute rotation_z.



24
25
26
# File 'lib/cosmos/gui/opengl/gl_shape.rb', line 24

def rotation_z
  @rotation_z
end

#tipTextObject

Returns the value of attribute tipText.



25
26
27
# File 'lib/cosmos/gui/opengl/gl_shape.rb', line 25

def tipText
  @tipText
end

Instance Method Details

#drag(viewer, fx, fy, tx, ty) ⇒ Object



98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/cosmos/gui/opengl/gl_shape.rb', line 98

def drag(viewer, fx, fy, tx, ty)
  if @dragable
    zz = viewer.worldToEyeZ(@position)
    wf = viewer.eyeToWorld(viewer.screenToEye(fx, fy, zz))
    wt = viewer.eyeToWorld(viewer.screenToEye(tx, ty, zz))
    wt_minus_wf = [wt[0] - wf[0], wt[1] - wf[1], wt[2] - wf[2]]
    @position = [@position[0] + wt_minus_wf[0], @position[1] + wt_minus_wf[1], @position[2] + wt_minus_wf[2]]
    return true
  else
    return false
  end
end

#draw(viewer) ⇒ Object



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
# File 'lib/cosmos/gui/opengl/gl_shape.rb', line 43

def draw(viewer)
  GL.PushAttrib(GL::CURRENT_BIT | GL::LIGHTING_BIT | GL::POINT_BIT | GL::LINE_BIT)
  GL.PushMatrix

  # Object position
  GL::Translatef(@position[0], @position[1], @position[2])

  # Shading
  GL.Enable(GL::LIGHTING)
  GL.Enable(GL::AUTO_NORMAL)
  GL.ShadeModel(GL::SMOOTH)

  # Material
  if @back_material
    GL.Material(GL::FRONT, GL::AMBIENT, @front_material.ambient)
    GL.Material(GL::FRONT, GL::DIFFUSE, @front_material.diffuse)
    GL.Material(GL::FRONT, GL::SPECULAR, @front_material.specular)
    GL.Material(GL::FRONT, GL::EMISSION, @front_material.emission)
    GL.Materialf(GL::FRONT, GL::SHININESS, @front_material.shininess)
    GL.Material(GL::BACK, GL::AMBIENT, @back_material.ambient)
    GL.Material(GL::BACK, GL::DIFFUSE, @back_material.diffuse)
    GL.Material(GL::BACK, GL::SPECULAR, @back_material.specular)
    GL.Material(GL::BACK, GL::EMISSION, @back_material.emission)
    GL.Materialf(GL::BACK, GL::SHININESS, @back_material.shininess)
  else
    GL.Material(GL::FRONT_AND_BACK, GL::AMBIENT, @front_material.ambient)
    GL.Material(GL::FRONT_AND_BACK, GL::DIFFUSE, @front_material.diffuse)
    GL.Material(GL::FRONT_AND_BACK, GL::SPECULAR, @front_material.specular)
    GL.Material(GL::FRONT_AND_BACK, GL::EMISSION, @front_material.emission)
    GL.Materialf(GL::FRONT_AND_BACK, GL::SHININESS, @front_material.shininess)
  end

  # Surface
  GL.PolygonMode(GL::FRONT_AND_BACK, GL::FILL)
  GL.Disable(GL::CULL_FACE);
  drawshape(viewer)

  # Restore attributes and matrix
  GL.PopMatrix
  GL.PopAttrib
end

#drawshape(viewer) ⇒ Object

Draw the StlShape



86
87
88
# File 'lib/cosmos/gui/opengl/gl_shape.rb', line 86

def drawshape(viewer)
  raise "drawshape must be implemented by subclass"
end

#exportObject



140
141
142
# File 'lib/cosmos/gui/opengl/gl_shape.rb', line 140

def export
  raise "export must be defined by subclass"
end

#handle_clickObject



111
112
113
# File 'lib/cosmos/gui/opengl/gl_shape.rb', line 111

def handle_click
  @click_handler.call() if @click_handler
end

#handle_doubleclickObject



115
116
117
# File 'lib/cosmos/gui/opengl/gl_shape.rb', line 115

def handle_doubleclick
  @doubleclick_handler.call() if @doubleclick_handler
end

#hit(viewer) ⇒ Object



90
91
92
# File 'lib/cosmos/gui/opengl/gl_shape.rb', line 90

def hit(viewer)
  draw(viewer)
end

#identifyObject



94
95
96
# File 'lib/cosmos/gui/opengl/gl_shape.rb', line 94

def identify
  return self
end