ruby-opengl2 0.60.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (117) hide show
  1. data/Rakefile +80 -0
  2. data/examples/NeHe/nehe_lesson02.rb +117 -0
  3. data/examples/NeHe/nehe_lesson03.rb +122 -0
  4. data/examples/NeHe/nehe_lesson04.rb +133 -0
  5. data/examples/NeHe/nehe_lesson05.rb +186 -0
  6. data/examples/NeHe/nehe_lesson36.rb +303 -0
  7. data/examples/OrangeBook/3Dlabs-License.txt +33 -0
  8. data/examples/OrangeBook/brick.frag +36 -0
  9. data/examples/OrangeBook/brick.rb +376 -0
  10. data/examples/OrangeBook/brick.vert +41 -0
  11. data/examples/OrangeBook/particle.frag +17 -0
  12. data/examples/OrangeBook/particle.rb +406 -0
  13. data/examples/OrangeBook/particle.vert +38 -0
  14. data/examples/README +16 -0
  15. data/examples/RedBook/aapoly.rb +142 -0
  16. data/examples/RedBook/aargb.rb +119 -0
  17. data/examples/RedBook/accanti.rb +162 -0
  18. data/examples/RedBook/accpersp.rb +215 -0
  19. data/examples/RedBook/alpha.rb +123 -0
  20. data/examples/RedBook/alpha3D.rb +158 -0
  21. data/examples/RedBook/bezcurve.rb +105 -0
  22. data/examples/RedBook/bezmesh.rb +137 -0
  23. data/examples/RedBook/checker.rb +124 -0
  24. data/examples/RedBook/clip.rb +95 -0
  25. data/examples/RedBook/colormat.rb +135 -0
  26. data/examples/RedBook/cube.rb +69 -0
  27. data/examples/RedBook/depthcue.rb +99 -0
  28. data/examples/RedBook/dof.rb +205 -0
  29. data/examples/RedBook/double.rb +105 -0
  30. data/examples/RedBook/drawf.rb +91 -0
  31. data/examples/RedBook/feedback.rb +145 -0
  32. data/examples/RedBook/fog.rb +167 -0
  33. data/examples/RedBook/font.rb +151 -0
  34. data/examples/RedBook/hello.rb +79 -0
  35. data/examples/RedBook/image.rb +137 -0
  36. data/examples/RedBook/jitter.rb +207 -0
  37. data/examples/RedBook/lines.rb +128 -0
  38. data/examples/RedBook/list.rb +111 -0
  39. data/examples/RedBook/material.rb +275 -0
  40. data/examples/RedBook/mipmap.rb +156 -0
  41. data/examples/RedBook/model.rb +113 -0
  42. data/examples/RedBook/movelight.rb +132 -0
  43. data/examples/RedBook/pickdepth.rb +179 -0
  44. data/examples/RedBook/planet.rb +108 -0
  45. data/examples/RedBook/quadric.rb +158 -0
  46. data/examples/RedBook/robot.rb +115 -0
  47. data/examples/RedBook/select.rb +196 -0
  48. data/examples/RedBook/smooth.rb +95 -0
  49. data/examples/RedBook/stencil.rb +163 -0
  50. data/examples/RedBook/stroke.rb +167 -0
  51. data/examples/RedBook/surface.rb +166 -0
  52. data/examples/RedBook/teaambient.rb +132 -0
  53. data/examples/RedBook/teapots.rb +182 -0
  54. data/examples/RedBook/tess.rb +183 -0
  55. data/examples/RedBook/texbind.rb +147 -0
  56. data/examples/RedBook/texgen.rb +169 -0
  57. data/examples/RedBook/texturesurf.rb +128 -0
  58. data/examples/RedBook/varray.rb +159 -0
  59. data/examples/RedBook/wrap.rb +148 -0
  60. data/examples/misc/OGLBench.rb +337 -0
  61. data/examples/misc/anisotropic.rb +194 -0
  62. data/examples/misc/fbo_test.rb +356 -0
  63. data/examples/misc/font-glut.rb +46 -0
  64. data/examples/misc/glfwtest.rb +30 -0
  65. data/examples/misc/plane.rb +161 -0
  66. data/examples/misc/readpixel.rb +65 -0
  67. data/examples/misc/sdltest.rb +34 -0
  68. data/examples/misc/trislam.rb +828 -0
  69. data/ext/common/common.h +423 -0
  70. data/ext/common/conv.h +243 -0
  71. data/ext/common/funcdef.h +280 -0
  72. data/ext/common/gl-enums.h +10031 -0
  73. data/ext/common/gl-error.h +23 -0
  74. data/ext/common/gl-types.h +63 -0
  75. data/ext/common/glu-enums.h +463 -0
  76. data/ext/gl/gl-1.0-1.1.c +2811 -0
  77. data/ext/gl/gl-1.2.c +814 -0
  78. data/ext/gl/gl-1.3.c +443 -0
  79. data/ext/gl/gl-1.4.c +348 -0
  80. data/ext/gl/gl-1.5.c +225 -0
  81. data/ext/gl/gl-2.0.c +657 -0
  82. data/ext/gl/gl-2.1.c +57 -0
  83. data/ext/gl/gl-enums.c +3354 -0
  84. data/ext/gl/gl-error.c +104 -0
  85. data/ext/gl/gl-ext-3dfx.c +27 -0
  86. data/ext/gl/gl-ext-arb.c +866 -0
  87. data/ext/gl/gl-ext-ati.c +41 -0
  88. data/ext/gl/gl-ext-ext.c +889 -0
  89. data/ext/gl/gl-ext-gremedy.c +41 -0
  90. data/ext/gl/gl-ext-nv.c +679 -0
  91. data/ext/gl/gl.c +214 -0
  92. data/ext/gl/mkrf_conf.rb +37 -0
  93. data/ext/glu/glu-enums.c +164 -0
  94. data/ext/glu/glu.c +1530 -0
  95. data/ext/glu/mkrf_conf.rb +39 -0
  96. data/ext/glut/glut.c +1627 -0
  97. data/ext/glut/mkrf_conf.rb +37 -0
  98. data/lib/opengl.rb +84 -0
  99. data/test/README +10 -0
  100. data/test/tc_common.rb +98 -0
  101. data/test/tc_ext_arb.rb +467 -0
  102. data/test/tc_ext_ati.rb +33 -0
  103. data/test/tc_ext_ext.rb +551 -0
  104. data/test/tc_ext_gremedy.rb +36 -0
  105. data/test/tc_ext_nv.rb +357 -0
  106. data/test/tc_func_10_11.rb +1281 -0
  107. data/test/tc_func_12.rb +186 -0
  108. data/test/tc_func_13.rb +229 -0
  109. data/test/tc_func_14.rb +197 -0
  110. data/test/tc_func_15.rb +270 -0
  111. data/test/tc_func_20.rb +346 -0
  112. data/test/tc_func_21.rb +541 -0
  113. data/test/tc_glu.rb +310 -0
  114. data/test/tc_include_gl.rb +35 -0
  115. data/test/tc_misc.rb +54 -0
  116. data/test/tc_require_gl.rb +34 -0
  117. metadata +164 -0
@@ -0,0 +1,186 @@
1
+ #!/usr/bin/env ruby -rubygems
2
+ # Name: nehe_lesson05.rb
3
+ # Purpose: An implementation of NeHe's OpenGL Lesson #05
4
+ # using ruby-opengl (http://nehe.gamedev.net/)
5
+ #
6
+
7
+ require "gl"
8
+ require "glu"
9
+ require "glut"
10
+ require "mathn"
11
+
12
+ # Add GL and GLUT namespaces in to make porting easier
13
+ include Gl
14
+ include Glu
15
+ include Glut
16
+
17
+ # Placeholder for the window object
18
+ $window = ""
19
+ # Angle for the triangle (Global)
20
+ $pyramid_angle = 0
21
+ # Angle for the quadrilateral (Global)
22
+ $cube_angle = 0
23
+
24
+ def init_gl_window(width = 640, height = 480)
25
+ # Background color to black
26
+ glClearColor(0.0, 0.0, 0.0, 0)
27
+ # Enables clearing of depth buffer
28
+ glClearDepth(1.0)
29
+ # Set type of depth test
30
+ glDepthFunc(GL_LEQUAL)
31
+ # Enable depth testing
32
+ glEnable(GL_DEPTH_TEST)
33
+ # Enable smooth color shading
34
+ glShadeModel(GL_SMOOTH)
35
+
36
+ glMatrixMode(GL_PROJECTION)
37
+ glLoadIdentity
38
+ # Calculate aspect ratio of the window
39
+ gluPerspective(45.0, width / height, 0.1, 100.0)
40
+
41
+ glMatrixMode(GL_MODELVIEW)
42
+
43
+ draw_gl_scene
44
+ end
45
+
46
+ #reshape = Proc.new do |width, height|
47
+ def reshape(width, height)
48
+ height = 1 if height == 0
49
+
50
+ # Reset current viewpoint and perspective transformation
51
+ glViewport(0, 0, width, height)
52
+
53
+ glMatrixMode(GL_PROJECTION)
54
+ glLoadIdentity
55
+
56
+ gluPerspective(45.0, width / height, 0.1, 100.0)
57
+ end
58
+
59
+ #draw_gl_scene = Proc.new do
60
+ def draw_gl_scene
61
+ # Clear the screen and depth buffer
62
+ glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
63
+
64
+ # Reset the view
65
+ glMatrixMode(GL_MODELVIEW)
66
+ glLoadIdentity
67
+
68
+ # Move left 1.5 units and into the screen 6.0 units
69
+ glTranslatef(-1.5, 0.0, -6.0)
70
+
71
+ # Rotate the pyramid on the Y-axis
72
+ glRotatef($pyramid_angle, 0.0, 1.0, 0.0)
73
+ # Draw a pyramid
74
+ glBegin(GL_POLYGON)
75
+ # Draw front side of pyramid
76
+ glColor3f(1.0, 0.0, 0.0)
77
+ glVertex3f( 0.0, 1.0, 0.0)
78
+ glColor3f(0.0, 1.0, 0.0)
79
+ glVertex3f(-1.0, -1.0, 1.0)
80
+ glColor3f(0.0, 0.0, 1.0)
81
+ glVertex3f(1.0, -1.0, 1.0)
82
+ # Draw right side of pyramid
83
+ glColor3f(1.0, 0.0, 0.0)
84
+ glVertex3f( 0.0, 1.0, 0.0)
85
+ glColor3f(0.0, 0.0, 1.0)
86
+ glVertex3f( 1.0, -1.0, 1.0)
87
+ glColor3f(0.0, 1.0, 0.0)
88
+ glVertex3f(1.0, -1.0, -1.0)
89
+ # Draw back side of pyramid
90
+ glColor3f(1.0, 0.0, 0.0)
91
+ glVertex3f( 0.0, 1.0, 0.0)
92
+ glColor3f(0.0, 0.0, 1.0)
93
+ glVertex3f(1.0, -1.0, -1.0)
94
+ glColor3f(0.0, 1.0, 0.0)
95
+ glVertex3f(-1.0, -1.0, -1.0)
96
+ # Draw left side of pyramid
97
+ glColor3f(1.0, 0.0, 0.0)
98
+ glVertex3f( 0.0, 1.0, 0.0)
99
+ glColor3f(0.0, 0.0, 1.0)
100
+ glVertex3f(-1.0, -1.0, -1.0)
101
+ glColor3f(0.0, 1.0, 0.0)
102
+ glVertex3f(-1.0, -1.0, 1.0)
103
+ glEnd
104
+
105
+ glLoadIdentity
106
+ # Move right 3 units
107
+ glTranslatef(1.5, 0.0, -7.0)
108
+
109
+ # Draw a cube
110
+ # Rotate the cube on the X, Y and Z axis
111
+ glRotatef($cube_angle, 1.0, 1.0, 1.0)
112
+ # Set it to a blue color one time only
113
+ glBegin(GL_QUADS)
114
+ # Draw the top side in green
115
+ glColor3f(0.0, 1.0, 0.0)
116
+ glVertex3f( 1.0, 1.0, -1.0)
117
+ glVertex3f(-1.0, 1.0, -1.0)
118
+ glVertex3f(-1.0, 1.0, 1.0)
119
+ glVertex3f( 1.0, 1.0, 1.0)
120
+ # Draw the bottom side in orange
121
+ glColor3f(1.0, 0.5, 0.0)
122
+ glVertex3f( 1.0, -1.0, 1.0)
123
+ glVertex3f(-1.0, -1.0, 1.0)
124
+ glVertex3f(-1.0, -1.0, -1.0)
125
+ glVertex3f( 1.0, -1.0, -1.0)
126
+ # Draw the front side in red
127
+ glColor3f(1.0, 0.0, 0.0)
128
+ glVertex3f( 1.0, 1.0, 1.0)
129
+ glVertex3f(-1.0, 1.0, 1.0)
130
+ glVertex3f(-1.0, -1.0, 1.0)
131
+ glVertex3f( 1.0, -1.0, 1.0)
132
+ # Draw the back side in yellow
133
+ glColor3f(1.0, 1.0, 0.0)
134
+ glVertex3f( 1.0, -1.0, -1.0)
135
+ glVertex3f(-1.0, -1.0, -1.0)
136
+ glVertex3f(-1.0, 1.0, -1.0)
137
+ glVertex3f( 1.0, 1.0, -1.0)
138
+ # Draw the left side in blue
139
+ glColor3f(0.0, 0.0, 1.0)
140
+ glVertex3f(-1.0, 1.0, 1.0)
141
+ glVertex3f(-1.0, 1.0, -1.0)
142
+ glVertex3f(-1.0, -1.0, -1.0)
143
+ glVertex3f(-1.0, -1.0, 1.0)
144
+ # Draw the right side in violet
145
+ glColor3f(1.0, 0.0, 1.0)
146
+ glVertex3f( 1.0, 1.0, -1.0)
147
+ glVertex3f( 1.0, 1.0, 1.0)
148
+ glVertex3f( 1.0, -1.0, 1.0)
149
+ glVertex3f( 1.0, -1.0, -1.0)
150
+ glEnd
151
+
152
+ $pyramid_angle += 0.2
153
+ $cube_angle -= 0.15
154
+ # Swap buffers for display
155
+ glutSwapBuffers
156
+ end
157
+
158
+ # The idle function to handle
159
+ def idle
160
+ glutPostRedisplay
161
+ end
162
+
163
+ # Keyboard handler to exit when ESC is typed
164
+ keyboard = lambda do |key, x, y|
165
+ case(key)
166
+ when ?\e
167
+ glutDestroyWindow($window)
168
+ exit(0)
169
+ end
170
+ glutPostRedisplay
171
+ end
172
+
173
+
174
+ # Initliaze our GLUT code
175
+ glutInit;
176
+ # Setup a double buffer, RGBA color, alpha components and depth buffer
177
+ glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_ALPHA | GLUT_DEPTH);
178
+ glutInitWindowSize(640, 480);
179
+ glutInitWindowPosition(0, 0);
180
+ $window = glutCreateWindow("NeHe Lesson 05 - ruby-opengl version");
181
+ glutDisplayFunc(method(:draw_gl_scene).to_proc);
182
+ glutReshapeFunc(method(:reshape).to_proc);
183
+ glutIdleFunc(method(:idle).to_proc);
184
+ glutKeyboardFunc(keyboard);
185
+ init_gl_window(640, 480)
186
+ glutMainLoop();
@@ -0,0 +1,303 @@
1
+ # Nehe Lesson 36 Code
2
+ # modified from immediate mode to use vertex arrays for helix drawing
3
+ require 'opengl'
4
+ include Gl,Glu,Glut
5
+ include Math
6
+
7
+ def emptyTexture
8
+ # Create Storage Space For Texture Data (128x128x4)
9
+ data = ([0]*4*128*128).pack("f*")
10
+ txtnumber = glGenTextures(1) # Create 1 Texture
11
+ glBindTexture(GL_TEXTURE_2D, txtnumber[0]) # Bind The Texture
12
+ glTexImage2D(GL_TEXTURE_2D, 0, 4, 128, 128, 0,
13
+ GL_RGBA, GL_FLOAT, data) # Build Texture Using Information In data
14
+ glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR)
15
+ glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR)
16
+ return txtnumber[0] # Return The Texture ID
17
+ end
18
+
19
+ def init
20
+ global_ambient = [0.2, 0.2, 0.2, 1.0] # Set Ambient Lighting To Fairly Dark Light (No Color)
21
+ light0pos = [0.0, 5.0, 10.0, 1.0] # Set The Light Position
22
+ light0ambient = [0.2, 0.2, 0.2, 1.0] # More Ambient Light
23
+ light0diffuse = [0.3, 0.3, 0.3, 1.0] # Set The Diffuse Light A Bit Brighter
24
+ light0specular = [0.8, 0.8, 0.8, 1.0] # Fairly Bright Specular Lighting
25
+
26
+ lmodel_ambient = [0.2,0.2,0.2,1.0] # And More Ambient Light
27
+
28
+ $angle = 0.0 # Set Starting Angle To Zero
29
+
30
+ $lasttime = 0
31
+
32
+ $blurTexture = emptyTexture() # Create Our Empty Texture
33
+
34
+ $helix_v,$helix_n = createHelix()
35
+ glVertexPointer(3,GL_FLOAT,0,$helix_v.flatten.pack("f*"))
36
+ glNormalPointer(GL_FLOAT,0,$helix_n.flatten.pack("f*"))
37
+
38
+ glLoadIdentity() # Reset The Modelview Matrix
39
+
40
+ glEnable(GL_DEPTH_TEST) # Enable Depth Testing
41
+
42
+ glLightModelfv(GL_LIGHT_MODEL_AMBIENT,lmodel_ambient) # Set The Ambient Light Model
43
+
44
+ glLightModelfv(GL_LIGHT_MODEL_AMBIENT,global_ambient) # Set The Global Ambient Light Model
45
+ glLightfv(GL_LIGHT0, GL_POSITION, light0pos) # Set The Lights Position
46
+ glLightfv(GL_LIGHT0, GL_AMBIENT, light0ambient) # Set The Ambient Light
47
+ glLightfv(GL_LIGHT0, GL_DIFFUSE, light0diffuse) # Set The Diffuse Light
48
+ glLightfv(GL_LIGHT0, GL_SPECULAR, light0specular) # Set Up Specular Lighting
49
+ glEnable(GL_LIGHTING) # Enable Lighting
50
+ glEnable(GL_LIGHT0) # Enable Light0
51
+
52
+ glShadeModel(GL_SMOOTH) # Select Smooth Shading
53
+
54
+ glMateriali(GL_FRONT, GL_SHININESS, 128)
55
+ glClearColor(0.0, 0.0, 0.0, 0.5) # Set The Clear Color To Black
56
+ end
57
+
58
+ # Keyboard handler to exit when ESC is typed
59
+ keyboard = lambda do |key, x, y|
60
+ case(key)
61
+ when ?\e
62
+ exit(0)
63
+ end
64
+ glutPostRedisplay
65
+ end
66
+
67
+ reshape = lambda do |w,h|
68
+ glMatrixMode(GL_PROJECTION)
69
+ glViewport(0,0,w,h)
70
+ glLoadIdentity()
71
+ width = 0.5
72
+ height = 0.5 * h/w;
73
+ glFrustum(-width,width,-height,height,1.0,2000.0)
74
+ glMatrixMode(GL_MODELVIEW)
75
+ glViewport(0,0,w,h)
76
+ end
77
+
78
+ def viewOrtho
79
+ glMatrixMode(GL_PROJECTION) # Select Projection
80
+ glPushMatrix() # Push The Matrix
81
+ glLoadIdentity() # Reset The Matrix
82
+ width = glutGet(GLUT_WINDOW_WIDTH)
83
+ height = glutGet(GLUT_WINDOW_HEIGHT)
84
+ glOrtho( 0, width , height , 0, -1, 1 ) # Select Ortho Mode (widthxheight)
85
+ glMatrixMode(GL_MODELVIEW) # Select Modelview Matrix
86
+ glPushMatrix() # Push The Matrix
87
+ glLoadIdentity() # Reset The Matrix
88
+ end
89
+
90
+ def viewPerspective # Set Up A Perspective View
91
+ glMatrixMode( GL_PROJECTION ) # Select Projection
92
+ glPopMatrix() # Pop The Matrix
93
+ glMatrixMode( GL_MODELVIEW ) # Select Modelview
94
+ glPopMatrix() # Pop The Matrix
95
+ end
96
+
97
+ def normalize(v)
98
+ len = sqrt( v[0]*v[0] + v[1]*v[1] + v[2]*v[2])
99
+ return v if len==0
100
+ [ v[0] / len, v[1] / len, v[2] / len ]
101
+ end
102
+
103
+ def calcNormal(v) # Calculates Normal For A Quad Using 3 Points
104
+ # Finds The Vector Between 2 Points By Subtracting
105
+ # The x,y,z Coordinates From One Point To Another.
106
+ # Calculate The Vector From Point 1 To Point 0
107
+ v1, v2, out = [], [], []
108
+ x,y,z = 0,1,2
109
+
110
+ v1[x] = v[0][x] - v[1][x] # Vector 1.x=Vertex[0].x-Vertex[1].x
111
+ v1[y] = v[0][y] - v[1][y] # Vector 1.y=Vertex[0].y-Vertex[1].y
112
+ v1[z] = v[0][z] - v[1][z] # Vector 1.z=Vertex[0].y-Vertex[1].z
113
+ # Calculate The Vector From Point 2 To Point 1
114
+ v2[x] = v[1][x] - v[2][x] # Vector 2.x=Vertex[0].x-Vertex[1].x
115
+ v2[y] = v[1][y] - v[2][y] # Vector 2.y=Vertex[0].y-Vertex[1].y
116
+ v2[z] = v[1][z] - v[2][z] # Vector 2.z=Vertex[0].z-Vertex[1].z
117
+ # Compute The Cross Product To Give Us A Surface Normal
118
+ out[x] = v1[y]*v2[z] - v1[z]*v2[y] # Cross Product For Y - Z
119
+ out[y] = v1[z]*v2[x] - v1[x]*v2[z] # Cross Product For X - Z
120
+ out[z] = v1[x]*v2[y] - v1[y]*v2[x] # Cross Product For X - Y
121
+
122
+ normalize(out)
123
+ end
124
+
125
+ def createHelix() # creates helix VA
126
+ twists = 5
127
+ r = 1.5
128
+
129
+ helix_v = []
130
+ helix_n = []
131
+
132
+ 0.step(360,20) do |phi| # 360 Degrees In Steps Of 20
133
+ 0.step(360*twists,20) do |theta| # 360 Degrees * Number Of Twists In Steps Of 20
134
+ v= phi/180.0*PI # Calculate Angle Of First Point ( 0 )
135
+ u= theta/180.0*PI # Calculate Angle Of First Point ( 0 )
136
+
137
+ x= cos(u)*(2.0+cos(v))*r # Calculate x Position (1st Point)
138
+ y= sin(u)*(2.0+cos(v))*r # Calculate y Position (1st Point)
139
+ z=((u-(2.0*PI)) + sin(v))*r # Calculate z Position (1st Point)
140
+
141
+ v0 = [x,y,z]
142
+
143
+ v= phi/180.0*PI # Calculate Angle Of Second Point ( 0 )
144
+ u= (theta+20)/180.0*PI # Calculate Angle Of Second Point ( 20 )
145
+
146
+ x= cos(u)*(2.0+cos(v))*r # Calculate x Position (2nd Point)
147
+ y= sin(u)*(2.0+cos(v))*r # Calculate y Position (2nd Point)
148
+ z= ((u-(2.0*PI)) + sin(v))*r # Calculate z Position (2nd Point)
149
+
150
+ v1 = [x,y,z]
151
+
152
+ v= (phi+20)/180.0*PI # Calculate Angle Of Third Point ( 20 )
153
+ u= (theta+20)/180.0*PI # Calculate Angle Of Third Point ( 20 )
154
+
155
+ x= cos(u)*(2.0+cos(v))*r # Calculate x Position (3rd Point)
156
+ y= sin(u)*(2.0+cos(v))*r # Calculate y Position (3rd Point)
157
+ z= ((u-(2.0*PI)) + sin(v))*r # Calculate z Position (3rd Point)
158
+
159
+ v2 = [x,y,z]
160
+
161
+ v= (phi+20)/180.0*PI # Calculate Angle Of Fourth Point ( 20 )
162
+ u= (theta)/180.0*PI # Calculate Angle Of Fourth Point ( 0 )
163
+
164
+ x= cos(u)*(2.0+cos(v))*r # Calculate x Position (4th Point)
165
+ y= sin(u)*(2.0+cos(v))*r # Calculate y Position (4th Point)
166
+ z= ((u-(2.0*PI)) + sin(v))*r # Calculate z Position (4th Point)
167
+
168
+ v3 = [x,y,z]
169
+
170
+ normal = calcNormal([v0,v1,v2,v3]) # Calculate The Quad Normal
171
+ helix_v << v0 << v1 << v2 << v3
172
+ helix_n << normal << normal << normal << normal
173
+ end
174
+ end
175
+ [helix_v,helix_n]
176
+ end
177
+
178
+ def processHelix() # Draws A Helix
179
+ glfMaterialColor = [0.4,0.2,0.8,1.0] # Set The Material Color
180
+ specular = [1.0,1.0,1.0,1.0] # Sets Up Specular Lighting
181
+
182
+ glLoadIdentity() # Reset The Modelview Matrix
183
+ gluLookAt(0, 5, 50, 0, 0, 0, 0, 1, 0) # Eye Position (0,5,50) Center Of Scene (0,0,0), Up On Y Axis
184
+
185
+ glPushMatrix() # Push The Modelview Matrix
186
+
187
+ glTranslatef(0,0,-50) # Translate 50 Units Into The Screen
188
+ glRotatef($angle/2.0,1,0,0) # Rotate By angle/2 On The X-Axis
189
+ glRotatef($angle/3.0,0,1,0) # Rotate By angle/3 On The Y-Axis
190
+
191
+ glMaterialfv(GL_FRONT_AND_BACK,GL_AMBIENT_AND_DIFFUSE,glfMaterialColor)
192
+ glMaterialfv(GL_FRONT_AND_BACK,GL_SPECULAR,specular)
193
+
194
+ glEnableClientState(GL_VERTEX_ARRAY)
195
+ glEnableClientState(GL_NORMAL_ARRAY)
196
+ glDrawArrays(GL_QUADS,0,$helix_v.size)
197
+ glDisableClientState(GL_VERTEX_ARRAY)
198
+ glDisableClientState(GL_NORMAL_ARRAY)
199
+
200
+ glPopMatrix() # Pop The Matrix
201
+ end
202
+
203
+ def drawBlur(times,inc)
204
+ spost = 0.0 # Starting Texture Coordinate Offset
205
+ alphainc = 0.9 / times # Fade Speed For Alpha Blending
206
+ alpha = 0.2 # Starting Alpha Value
207
+
208
+ width = glutGet(GLUT_WINDOW_WIDTH)
209
+ height = glutGet(GLUT_WINDOW_HEIGHT)
210
+ # Disable AutoTexture Coordinates
211
+ glDisable(GL_TEXTURE_GEN_S)
212
+ glDisable(GL_TEXTURE_GEN_T)
213
+
214
+ glEnable(GL_TEXTURE_2D) # Enable 2D Texture Mapping
215
+ glDisable(GL_DEPTH_TEST) # Disable Depth Testing
216
+ glBlendFunc(GL_SRC_ALPHA,GL_ONE) # Set Blending Mode
217
+ glEnable(GL_BLEND) # Enable Blending
218
+ glBindTexture(GL_TEXTURE_2D,$blurTexture) # Bind To The Blur Texture
219
+ viewOrtho() # Switch To An Ortho View
220
+
221
+ alphainc = alpha / times # alphainc=0.2 / Times To Render Blur
222
+
223
+ glBegin(GL_QUADS) # Begin Drawing Quads
224
+ 0.upto(times-1) do |num| # Number Of Times To Render Blur
225
+ glColor4f(1.0, 1.0, 1.0, alpha) # Set The Alpha Value (Starts At 0.2)
226
+ glTexCoord2f(0+spost,1-spost) # Texture Coordinate ( 0, 1 )
227
+ glVertex2f(0,0) # First Vertex ( 0, 0 )
228
+
229
+ glTexCoord2f(0+spost,0+spost) # Texture Coordinate ( 0, 0 )
230
+ glVertex2f(0,height) # Second Vertex ( 0, height )
231
+
232
+ glTexCoord2f(1-spost,0+spost) # Texture Coordinate ( 1, 0 )
233
+ glVertex2f(width,height) # Third Vertex ( width, height )
234
+
235
+ glTexCoord2f(1-spost,1-spost) # Texture Coordinate ( 1, 1 )
236
+ glVertex2f(width,0) # Fourth Vertex ( width, 0 )
237
+
238
+ spost += inc # Gradually Increase spost (Zooming Closer To Texture Center)
239
+ alpha = alpha - alphainc # Gradually Decrease alpha (Gradually Fading Image Out)
240
+ end
241
+ glEnd() # Done Drawing Quads
242
+
243
+ viewPerspective() # Switch To A Perspective View
244
+
245
+ glEnable(GL_DEPTH_TEST) # Enable Depth Testing
246
+ glDisable(GL_TEXTURE_2D) # Disable 2D Texture Mapping
247
+ glDisable(GL_BLEND) # Disable Blending
248
+ glBindTexture(GL_TEXTURE_2D,0) # Unbind The Blur Texture
249
+ end
250
+
251
+
252
+ def renderToTexture
253
+ glViewport(0,0,128,128); # Set Our Viewport (Match Texture Size)
254
+
255
+ processHelix() # Render The Helix
256
+
257
+ glBindTexture(GL_TEXTURE_2D,$blurTexture) # Bind To The Blur Texture
258
+
259
+ # Copy Our ViewPort To The Blur Texture (From 0,0 To 128,128... No Border)
260
+ glCopyTexImage2D(GL_TEXTURE_2D, 0, GL_LUMINANCE, 0, 0, 128, 128, 0)
261
+
262
+ glClearColor(0.0, 0.0, 0.5, 0.5) # Set The Clear Color To Medium Blue
263
+ glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT) # Clear The Screen And Depth Buffer
264
+ width = glutGet(GLUT_WINDOW_WIDTH)
265
+ height = glutGet(GLUT_WINDOW_HEIGHT)
266
+ glViewport(0 , 0,width,height) # Set Viewport (0,0 to widthxheight)
267
+ end
268
+
269
+ drawGLScene = lambda do # Draw The Scene
270
+ glClearColor(0.0, 0.0, 0.0, 0.5) # Set The Clear Color To Black
271
+ glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT) # Clear Screen And Depth Buffer
272
+ glLoadIdentity() # Reset The View
273
+ renderToTexture() # Render To A Texture
274
+ processHelix() # Draw Our Helix
275
+ drawBlur(25,0.02) # Draw The Blur Effect
276
+ glFlush() # Flush The GL Rendering Pipeline
277
+ glutSwapBuffers()
278
+ sleep(0.001) # don't hog all cpu time
279
+ end
280
+
281
+ idle = lambda do
282
+ now = glutGet(GLUT_ELAPSED_TIME)
283
+ elapsed = now - $lasttime
284
+ $angle += (elapsed * 0.03) # Update angle Based On The Clock
285
+ $lasttime = now
286
+
287
+ glutPostRedisplay()
288
+ end
289
+
290
+ # Main
291
+ glutInit()
292
+ glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_ALPHA | GLUT_DEPTH)
293
+ glutInitWindowPosition(100,100)
294
+ glutInitWindowSize(640,480)
295
+ glutCreateWindow("NeHe's Lesson 36")
296
+ glutDisplayFunc(drawGLScene)
297
+ glutIdleFunc(idle)
298
+ glutReshapeFunc(reshape)
299
+ glutKeyboardFunc(keyboard)
300
+
301
+ init()
302
+
303
+ glutMainLoop()