opengl 0.7.0.pre1-x86-mingw32

Sign up to get free protection for your applications and to get access to all the features.
Files changed (141) hide show
  1. data/.gemtest +0 -0
  2. data/.gitignore +8 -0
  3. data/History.txt +36 -0
  4. data/MIT-LICENSE +18 -0
  5. data/Manifest.txt +140 -0
  6. data/README.rdoc +51 -0
  7. data/Rakefile +129 -0
  8. data/Rakefile.cross +104 -0
  9. data/doc/build_install.txt +119 -0
  10. data/doc/extensions.txt.in +348 -0
  11. data/doc/history.txt +66 -0
  12. data/doc/requirements_and_design.txt +117 -0
  13. data/doc/roadmap.txt +28 -0
  14. data/doc/scientific_use.txt +35 -0
  15. data/doc/supplies/page_template.html +71 -0
  16. data/doc/thanks.txt +29 -0
  17. data/doc/tutorial.txt +469 -0
  18. data/examples/NeHe/nehe_lesson02.rb +117 -0
  19. data/examples/NeHe/nehe_lesson03.rb +122 -0
  20. data/examples/NeHe/nehe_lesson04.rb +133 -0
  21. data/examples/NeHe/nehe_lesson05.rb +186 -0
  22. data/examples/NeHe/nehe_lesson36.rb +303 -0
  23. data/examples/OrangeBook/3Dlabs-License.txt +33 -0
  24. data/examples/OrangeBook/brick.frag +36 -0
  25. data/examples/OrangeBook/brick.rb +376 -0
  26. data/examples/OrangeBook/brick.vert +41 -0
  27. data/examples/OrangeBook/particle.frag +17 -0
  28. data/examples/OrangeBook/particle.rb +406 -0
  29. data/examples/OrangeBook/particle.vert +38 -0
  30. data/examples/README +16 -0
  31. data/examples/RedBook/aapoly.rb +142 -0
  32. data/examples/RedBook/aargb.rb +119 -0
  33. data/examples/RedBook/accanti.rb +162 -0
  34. data/examples/RedBook/accpersp.rb +215 -0
  35. data/examples/RedBook/alpha.rb +123 -0
  36. data/examples/RedBook/alpha3D.rb +158 -0
  37. data/examples/RedBook/bezcurve.rb +105 -0
  38. data/examples/RedBook/bezmesh.rb +137 -0
  39. data/examples/RedBook/checker.rb +124 -0
  40. data/examples/RedBook/clip.rb +95 -0
  41. data/examples/RedBook/colormat.rb +135 -0
  42. data/examples/RedBook/cube.rb +69 -0
  43. data/examples/RedBook/depthcue.rb +99 -0
  44. data/examples/RedBook/dof.rb +205 -0
  45. data/examples/RedBook/double.rb +105 -0
  46. data/examples/RedBook/drawf.rb +91 -0
  47. data/examples/RedBook/feedback.rb +145 -0
  48. data/examples/RedBook/fog.rb +167 -0
  49. data/examples/RedBook/font.rb +151 -0
  50. data/examples/RedBook/hello.rb +79 -0
  51. data/examples/RedBook/image.rb +137 -0
  52. data/examples/RedBook/jitter.rb +207 -0
  53. data/examples/RedBook/lines.rb +128 -0
  54. data/examples/RedBook/list.rb +111 -0
  55. data/examples/RedBook/material.rb +275 -0
  56. data/examples/RedBook/mipmap.rb +156 -0
  57. data/examples/RedBook/model.rb +113 -0
  58. data/examples/RedBook/movelight.rb +132 -0
  59. data/examples/RedBook/pickdepth.rb +179 -0
  60. data/examples/RedBook/planet.rb +108 -0
  61. data/examples/RedBook/quadric.rb +158 -0
  62. data/examples/RedBook/robot.rb +115 -0
  63. data/examples/RedBook/select.rb +196 -0
  64. data/examples/RedBook/smooth.rb +95 -0
  65. data/examples/RedBook/stencil.rb +163 -0
  66. data/examples/RedBook/stroke.rb +167 -0
  67. data/examples/RedBook/surface.rb +166 -0
  68. data/examples/RedBook/teaambient.rb +132 -0
  69. data/examples/RedBook/teapots.rb +182 -0
  70. data/examples/RedBook/tess.rb +183 -0
  71. data/examples/RedBook/texbind.rb +147 -0
  72. data/examples/RedBook/texgen.rb +169 -0
  73. data/examples/RedBook/texturesurf.rb +128 -0
  74. data/examples/RedBook/varray.rb +159 -0
  75. data/examples/RedBook/wrap.rb +148 -0
  76. data/examples/misc/OGLBench.rb +337 -0
  77. data/examples/misc/anisotropic.rb +194 -0
  78. data/examples/misc/fbo_test.rb +356 -0
  79. data/examples/misc/font-glut.rb +46 -0
  80. data/examples/misc/glfwtest.rb +30 -0
  81. data/examples/misc/plane.rb +161 -0
  82. data/examples/misc/readpixel.rb +65 -0
  83. data/examples/misc/sdltest.rb +34 -0
  84. data/examples/misc/trislam.rb +828 -0
  85. data/ext/common/common.h +448 -0
  86. data/ext/common/conv.h +234 -0
  87. data/ext/common/funcdef.h +280 -0
  88. data/ext/common/gl-enums.h +10031 -0
  89. data/ext/common/gl-error.h +23 -0
  90. data/ext/common/gl-types.h +67 -0
  91. data/ext/common/glu-enums.h +463 -0
  92. data/ext/gl/extconf.rb +43 -0
  93. data/ext/gl/gl-1.0-1.1.c +2811 -0
  94. data/ext/gl/gl-1.2.c +814 -0
  95. data/ext/gl/gl-1.3.c +443 -0
  96. data/ext/gl/gl-1.4.c +348 -0
  97. data/ext/gl/gl-1.5.c +225 -0
  98. data/ext/gl/gl-2.0.c +657 -0
  99. data/ext/gl/gl-2.1.c +57 -0
  100. data/ext/gl/gl-enums.c +3354 -0
  101. data/ext/gl/gl-error.c +104 -0
  102. data/ext/gl/gl-ext-3dfx.c +27 -0
  103. data/ext/gl/gl-ext-arb.c +866 -0
  104. data/ext/gl/gl-ext-ati.c +41 -0
  105. data/ext/gl/gl-ext-ext.c +889 -0
  106. data/ext/gl/gl-ext-gremedy.c +41 -0
  107. data/ext/gl/gl-ext-nv.c +679 -0
  108. data/ext/gl/gl.c +216 -0
  109. data/ext/glu/extconf.rb +51 -0
  110. data/ext/glu/glu-enums.c +164 -0
  111. data/ext/glu/glu.c +1530 -0
  112. data/ext/glut/extconf.rb +67 -0
  113. data/ext/glut/glut.c +1624 -0
  114. data/lib/opengl.rb +89 -0
  115. data/test/README +10 -0
  116. data/test/tc_common.rb +98 -0
  117. data/test/tc_ext_arb.rb +467 -0
  118. data/test/tc_ext_ati.rb +33 -0
  119. data/test/tc_ext_ext.rb +551 -0
  120. data/test/tc_ext_gremedy.rb +36 -0
  121. data/test/tc_ext_nv.rb +357 -0
  122. data/test/tc_func_10_11.rb +1281 -0
  123. data/test/tc_func_12.rb +186 -0
  124. data/test/tc_func_13.rb +229 -0
  125. data/test/tc_func_14.rb +197 -0
  126. data/test/tc_func_15.rb +270 -0
  127. data/test/tc_func_20.rb +346 -0
  128. data/test/tc_func_21.rb +541 -0
  129. data/test/tc_glu.rb +310 -0
  130. data/test/tc_include_gl.rb +35 -0
  131. data/test/tc_misc.rb +54 -0
  132. data/test/tc_require_gl.rb +34 -0
  133. data/utils/README +11 -0
  134. data/utils/enumgen.rb +112 -0
  135. data/utils/extlistgen.rb +90 -0
  136. data/utils/mkdn2html.rb +59 -0
  137. data/utils/post-mkdn2html.rb +91 -0
  138. data/website/images/ogl.jpg +0 -0
  139. data/website/images/tab_bottom.gif +0 -0
  140. data/website/style.css +198 -0
  141. metadata +274 -0
@@ -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()
@@ -0,0 +1,33 @@
1
+ Copyright (C) 2002-2005 3Dlabs Inc. Ltd.
2
+ All rights reserved.
3
+
4
+ Redistribution and use in source and binary forms, with or without
5
+ modification, are permitted provided that the following conditions
6
+ are met:
7
+
8
+ Redistributions of source code must retain the above copyright
9
+ notice, this list of conditions and the following disclaimer.
10
+
11
+ Redistributions in binary form must reproduce the above
12
+ copyright notice, this list of conditions and the following
13
+ disclaimer in the documentation and/or other materials provided
14
+ with the distribution.
15
+
16
+ Neither the name of 3Dlabs Inc. Ltd. nor the names of its
17
+ contributors may be used to endorse or promote products derived
18
+ from this software without specific prior written permission.
19
+
20
+ THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21
+ "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22
+ LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
23
+ FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
24
+ COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
25
+ INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
26
+ BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
27
+ LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
28
+ CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29
+ LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
30
+ ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31
+ POSSIBILITY OF SUCH DAMAGE.
32
+
33
+
@@ -0,0 +1,36 @@
1
+ //
2
+ // Fragment shader for procedural bricks
3
+ //
4
+ // Authors: Dave Baldwin, Steve Koren, Randi Rost
5
+ // based on a shader by Darwyn Peachey
6
+ //
7
+ // Copyright (c) 2002-2004 3Dlabs Inc. Ltd.
8
+ //
9
+ // See 3Dlabs-License.txt for license information
10
+ //
11
+
12
+ uniform vec3 BrickColor, MortarColor;
13
+ uniform vec2 BrickSize;
14
+ uniform vec2 BrickPct;
15
+
16
+ varying vec2 MCposition;
17
+ varying float LightIntensity;
18
+
19
+ void main(void)
20
+ {
21
+ vec3 color;
22
+ vec2 position, useBrick;
23
+
24
+ position = MCposition / BrickSize;
25
+
26
+ if (fract(position.y * 0.5) > 0.5)
27
+ position.x += 0.5;
28
+
29
+ position = fract(position);
30
+
31
+ useBrick = step(position, BrickPct);
32
+
33
+ color = mix(MortarColor, BrickColor, useBrick.x * useBrick.y);
34
+ color *= LightIntensity;
35
+ gl_FragColor = vec4 (color, 1.0);
36
+ }
@@ -0,0 +1,376 @@
1
+ # Copyright (C) 2002-2005 3Dlabs Inc. Ltd.
2
+ # All rights reserved.
3
+ #
4
+ # Redistribution and use in source and binary forms, with or without
5
+ # modification, are permitted provided that the following conditions
6
+ # are met:
7
+ #
8
+ # Redistributions of source code must retain the above copyright
9
+ # notice, this list of conditions and the following disclaimer.
10
+ #
11
+ # Redistributions in binary form must reproduce the above
12
+ # copyright notice, this list of conditions and the following
13
+ # disclaimer in the documentation and/or other materials provided
14
+ # with the distribution.
15
+ #
16
+ # Neither the name of 3Dlabs Inc. Ltd. nor the names of its
17
+ # contributors may be used to endorse or promote products derived
18
+ # from this software without specific prior written permission.
19
+ #
20
+ # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21
+ # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22
+ # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
23
+ # FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
24
+ # COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
25
+ # INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
26
+ # BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
27
+ # LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
28
+ # CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29
+ # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
30
+ # ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31
+ # POSSIBILITY OF SUCH DAMAGE.
32
+ # ===========================================================================
33
+ #
34
+ #
35
+ # The source of the shaders itself are in plain text files. Files ending in
36
+ # .vert are vertex shaders, and files ending in .frag are fragment shaders.
37
+
38
+ require 'opengl'
39
+ include Gl,Glu,Glut
40
+
41
+ $rotl = 1 * Math::PI / 180
42
+ $last_time = 0
43
+
44
+ $fXDiff = 206
45
+ $fYDiff = 16
46
+ $fZDiff = 10
47
+ $xLastIncr = 0
48
+ $yLastIncr = 0
49
+ $fXInertia = -0.5
50
+ $fYInertia = 0
51
+ $fXInertiaOld = 0
52
+ $fYInertiaOld = 0
53
+ $fScale = 1.0
54
+ $ftime = 0
55
+ $xLast = -1
56
+ $yLast = -1
57
+ $bmModifiers = 0
58
+ $rotate = true
59
+
60
+ INERTIA_THRESHOLD = 1.0
61
+ INERTIA_FACTOR = 0.5
62
+ SCALE_FACTOR = 0.01
63
+ SCALE_INCREMENT = 0.5
64
+ TIMER_FREQUENCY_MILLIS = 20
65
+
66
+
67
+ $gleModel = [:cube, :teapot,:torus,:sphere]
68
+ $clearColor = [[0,0,0,1], [0.2,0.2,0.3,1], [0.7,0.7,0.7,1]]
69
+
70
+ def drawCube
71
+ size = 1.0
72
+ scale = 0.2
73
+ delta = 0.1
74
+
75
+ v = [
76
+ [ size, size, size * scale + delta ],
77
+ [ size, size, -size * scale + delta ],
78
+ [ size, -size, -size * scale ],
79
+ [ size, -size, size * scale ],
80
+ [-size, size, size * scale + delta ],
81
+ [-size, size, -size * scale + delta ],
82
+ [-size, -size, -size * scale ],
83
+ [-size, -size, size * scale ]
84
+ ]
85
+
86
+ cube = [
87
+ [ [1,0,0], v[3],v[2],v[1],v[0] ], # normal, vertices
88
+ [ [-1,0,0], v[6],v[7],v[4],v[5] ],
89
+ [ [0,0,-1], v[2],v[6],v[5],v[1] ],
90
+ [ [0,0,1], v[7],v[3],v[0],v[4] ],
91
+ [ [0,1,0], v[4],v[0],v[1],v[5] ],
92
+ [ [0,-1,0], v[6],v[2],v[3],v[7] ]
93
+ ]
94
+
95
+ glBegin(GL_QUADS)
96
+ cube.each do |side|
97
+ glNormal3fv(side[0])
98
+
99
+ glTexCoord2f(1,1)
100
+ glVertex3fv(side[1])
101
+ glTexCoord2f(0,1)
102
+ glVertex3fv(side[2])
103
+ glTexCoord2f(0,0)
104
+ glVertex3fv(side[3])
105
+ glTexCoord2f(1,0)
106
+ glVertex3fv(side[4])
107
+ end
108
+ glEnd()
109
+ end
110
+
111
+ def nextClearColor
112
+ glClearColor($clearColor[0][0],
113
+ $clearColor[0][1],
114
+ $clearColor[0][2],
115
+ $clearColor[0][3])
116
+ $clearColor << $clearColor.shift # rotate
117
+ end
118
+
119
+
120
+ play = lambda do
121
+ this_time = glutGet(GLUT_ELAPSED_TIME)
122
+
123
+ $rotl+=(this_time - $last_time) * -0.001
124
+ $last_time = this_time
125
+
126
+ glutPostRedisplay()
127
+ end
128
+
129
+ display = lambda do
130
+ glLoadIdentity()
131
+ glTranslatef(0.0, 0.0, -5.0)
132
+
133
+ glRotatef($fYDiff, 1,0,0)
134
+ glRotatef($fXDiff, 0,1,0)
135
+ glRotatef($fZDiff, 0,0,1)
136
+
137
+ glScalef($fScale, $fScale, $fScale)
138
+
139
+ glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
140
+
141
+ case $gleModel[0]
142
+ when :teapot :
143
+ glutSolidTeapot(0.6)
144
+ when :torus :
145
+ glutSolidTorus(0.2, 0.6, 64, 64)
146
+ when :sphere :
147
+ glutSolidSphere(0.6, 64, 64)
148
+ when :cube :
149
+ drawCube()
150
+ end
151
+ glFlush()
152
+ glutSwapBuffers()
153
+ end
154
+
155
+ key = lambda do |key,x,y|
156
+ case(key)
157
+ when ?b
158
+ nextClearColor()
159
+ when ?q, ?\e # esc
160
+ exit(0)
161
+ when ?t
162
+ $gleModel << $gleModel.shift # rotate the array
163
+ when ?\s # space
164
+ $rotate = !$rotate
165
+
166
+ if ($rotate==false)
167
+ $fXInertiaOld = $fXInertia
168
+ $fYInertiaOld = $fYInertia
169
+ else
170
+ $fXInertia = $fXInertiaOld
171
+ $fYInertia = $fYInertiaOld
172
+
173
+ # To prevent confusion, force some rotation
174
+ if ($fXInertia == 0 && $fYInertia == 0)
175
+ $fXInertia = -0.5
176
+ end
177
+ end
178
+ when ?+
179
+ $fScale += SCALE_INCREMENT
180
+ when ?-
181
+ $fScale -= SCALE_INCREMENT
182
+ else
183
+ puts "Keyboard commands:\n"
184
+ puts "b - Toggle among background clear colors"
185
+ puts "q, <esc> - Quit"
186
+ puts "t - Toggle among models to render"
187
+ puts "? - Help"
188
+ puts "<home> - reset zoom and rotation"
189
+ puts "<space> or <click> - stop rotation"
190
+ puts "<+>, <-> or <ctrl + drag> - zoom model"
191
+ puts "<arrow keys> or <drag> - rotate model\n"
192
+ end
193
+ end
194
+
195
+ reshape = lambda do |w,h|
196
+ vp = 0.8
197
+ aspect = w.to_f/h.to_f
198
+
199
+ glViewport(0, 0, w, h)
200
+ glMatrixMode(GL_PROJECTION)
201
+ glLoadIdentity()
202
+
203
+ glFrustum(-vp, vp, -vp / aspect, vp / aspect, 3, 10)
204
+
205
+ glMatrixMode(GL_MODELVIEW)
206
+ glLoadIdentity()
207
+ glTranslatef(0.0, 0.0, -5.0)
208
+ end
209
+
210
+ motion = lambda do |x,y|
211
+ if ($xLast != -1 || $yLast != -1)
212
+ $xLastIncr = x - $xLast
213
+ $yLastIncr = y - $yLast
214
+ if ($bmModifiers & GLUT_ACTIVE_CTRL != 0)
215
+ if ($xLast != -1)
216
+ $fZDiff += $xLastIncr
217
+ $fScale += $yLastIncr*SCALE_FACTOR
218
+ end
219
+ else
220
+ if ($xLast != -1)
221
+ $fXDiff += $xLastIncr
222
+ $fYDiff += $yLastIncr
223
+ end
224
+ end
225
+ end
226
+ $xLast = x
227
+ $yLast = y
228
+ end
229
+
230
+ mouse = lambda do |button,state,x,y|
231
+ $bmModifiers = glutGetModifiers()
232
+ if (button == GLUT_LEFT_BUTTON)
233
+ if (state == GLUT_UP)
234
+ $xLast = -1
235
+ $yLast = -1
236
+ if $xLastIncr > INERTIA_THRESHOLD
237
+ $fXInertia = ($xLastIncr - INERTIA_THRESHOLD)*INERTIA_FACTOR
238
+ end
239
+ if -$xLastIncr > INERTIA_THRESHOLD
240
+ $fXInertia = ($xLastIncr + INERTIA_THRESHOLD)*INERTIA_FACTOR
241
+ end
242
+
243
+ if $yLastIncr > INERTIA_THRESHOLD
244
+ $fYInertia = ($yLastIncr - INERTIA_THRESHOLD)*INERTIA_FACTOR
245
+ end
246
+ if -$yLastIncr > INERTIA_THRESHOLD
247
+ $fYInertia = ($yLastIncr + INERTIA_THRESHOLD)*INERTIA_FACTOR
248
+ end
249
+ else
250
+ $fXInertia = 0
251
+ $fYInertia = 0
252
+ end
253
+ $xLastIncr = 0
254
+ $yLastIncr = 0
255
+ end
256
+ end
257
+
258
+ special = lambda do |key,x,y|
259
+ case key
260
+ when GLUT_KEY_HOME:
261
+ $fXDiff = 0
262
+ $fYDiff = 35
263
+ $fZDiff = 0
264
+ $xLastIncr = 0
265
+ $yLastIncr = 0
266
+ $fXInertia = -0.5
267
+ $fYInertia = 0
268
+ $fScale = 1.0
269
+ when GLUT_KEY_LEFT:
270
+ $fXDiff -= 1
271
+ when GLUT_KEY_RIGHT:
272
+ $fXDiff += 1
273
+ when GLUT_KEY_UP:
274
+ $fYDiff -= 1
275
+ when GLUT_KEY_DOWN:
276
+ $fYDiff += 1
277
+ end
278
+ end
279
+
280
+ timer = lambda do |value|
281
+ $ftime += 0.01
282
+ if $rotate
283
+ $fXDiff += $fXInertia
284
+ $fYDiff += $fYInertia
285
+ end
286
+ glutTimerFunc(TIMER_FREQUENCY_MILLIS , timer, 0)
287
+ end
288
+
289
+ def getUniLoc(program, name)
290
+ loc = glGetUniformLocation(program, name)
291
+
292
+ if (loc == -1)
293
+ puts "No such uniform named #{name}"
294
+ end
295
+ return loc
296
+ end
297
+
298
+ def installBrickShaders(vs_fname,fs_fname)
299
+ # Create a vertex shader object and a fragment shader object
300
+ brickVS = glCreateShader(GL_VERTEX_SHADER)
301
+ brickFS = glCreateShader(GL_FRAGMENT_SHADER)
302
+
303
+ # Load source code strings into shaders
304
+ glShaderSource(brickVS, File.read(vs_fname))
305
+ glShaderSource(brickFS, File.read(fs_fname))
306
+
307
+ # Compile the brick vertex shader, and print out
308
+ # the compiler log file.
309
+ glCompileShader(brickVS)
310
+ vertCompiled = glGetShaderiv(brickVS, GL_COMPILE_STATUS)
311
+ puts "Shader InfoLog:\n#{glGetShaderInfoLog(brickVS)}\n"
312
+
313
+ # Compile the brick fragment shader, and print out
314
+ # the compiler log file.
315
+ glCompileShader(brickFS)
316
+ fragCompiled = glGetShaderiv(brickFS, GL_COMPILE_STATUS)
317
+ puts "Shader InfoLog:\n#{glGetShaderInfoLog(brickFS)}\n"
318
+
319
+ return false if (vertCompiled == 0 || fragCompiled == 0)
320
+ # Create a program object and attach the two compiled shaders
321
+ brickProg = glCreateProgram()
322
+ glAttachShader(brickProg,brickVS)
323
+ glAttachShader(brickProg,brickFS)
324
+ # Link the program object and print out the info log
325
+ glLinkProgram(brickProg)
326
+ linked = glGetProgramiv(brickProg,GL_LINK_STATUS)
327
+ puts "Program InfoLog:\n#{glGetProgramInfoLog(brickProg)}\n"
328
+ return false if linked==0
329
+
330
+ # Install program object as part of current state
331
+ glUseProgram(brickProg)
332
+
333
+ # Set up initial uniform values
334
+ glUniform3f(getUniLoc(brickProg, "BrickColor"), 1.0, 0.3, 0.2)
335
+ glUniform3f(getUniLoc(brickProg, "MortarColor"), 0.85, 0.86, 0.84)
336
+ glUniform2f(getUniLoc(brickProg, "BrickSize"), 0.30, 0.15)
337
+ glUniform2f(getUniLoc(brickProg, "BrickPct"), 0.90, 0.85)
338
+ glUniform3f(getUniLoc(brickProg, "LightPosition"), 0.0, 0.0, 4.0)
339
+
340
+ return true
341
+ end
342
+
343
+ # Main
344
+
345
+ glutInit()
346
+ glutInitDisplayMode( GLUT_RGB | GLUT_DEPTH | GLUT_DOUBLE)
347
+ glutInitWindowPosition(100,100)
348
+ glutInitWindowSize(500, 500)
349
+ glutCreateWindow( "3Dlabs Brick Shader")
350
+
351
+ glutIdleFunc(play)
352
+ glutDisplayFunc(display)
353
+ glutKeyboardFunc(key)
354
+ glutReshapeFunc(reshape)
355
+ glutMotionFunc(motion)
356
+ glutMouseFunc(mouse)
357
+ glutSpecialFunc(special)
358
+ glutTimerFunc(TIMER_FREQUENCY_MILLIS , timer, 0)
359
+
360
+ # Make sure that OpenGL 2.0 is supported by the driver
361
+ if Gl.is_available?(2.0)==false
362
+ major,minor,*rest = glGetString(GL_VERSION).split(/\.| /)
363
+ puts "GL_VERSION major=#{major} minor=#{minor}"
364
+ puts "Support for OpenGL 2.0 is required for this demo...exiting"
365
+ exit(1)
366
+ end
367
+
368
+ glDepthFunc(GL_LESS)
369
+ glEnable(GL_DEPTH_TEST)
370
+
371
+ nextClearColor()
372
+
373
+ key.call('?', 0, 0)
374
+
375
+ success = installBrickShaders("brick.vert","brick.frag")
376
+ glutMainLoop() if success == true