danabr75-opengl 0.10.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (149) hide show
  1. checksums.yaml +7 -0
  2. data/.autotest +29 -0
  3. data/.gemtest +0 -0
  4. data/.gitignore +6 -0
  5. data/.travis.yml +19 -0
  6. data/Gemfile +8 -0
  7. data/History.md +101 -0
  8. data/MIT-LICENSE +18 -0
  9. data/Manifest.txt +142 -0
  10. data/README.rdoc +105 -0
  11. data/Rakefile +170 -0
  12. data/danabr75-opengl.gemspec +32 -0
  13. data/examples/NeHe/NeHe.png +0 -0
  14. data/examples/NeHe/crate.png +0 -0
  15. data/examples/NeHe/glass.png +0 -0
  16. data/examples/NeHe/nehe_lesson02.rb +117 -0
  17. data/examples/NeHe/nehe_lesson03.rb +123 -0
  18. data/examples/NeHe/nehe_lesson04.rb +132 -0
  19. data/examples/NeHe/nehe_lesson05.rb +182 -0
  20. data/examples/NeHe/nehe_lesson06.rb +186 -0
  21. data/examples/NeHe/nehe_lesson07.rb +240 -0
  22. data/examples/NeHe/nehe_lesson08.rb +255 -0
  23. data/examples/NeHe/nehe_lesson09.rb +203 -0
  24. data/examples/NeHe/nehe_lesson11.rb +176 -0
  25. data/examples/NeHe/nehe_lesson12.rb +203 -0
  26. data/examples/NeHe/nehe_lesson16.rb +211 -0
  27. data/examples/NeHe/nehe_lesson19.rb +209 -0
  28. data/examples/NeHe/nehe_lesson36.rb +308 -0
  29. data/examples/NeHe/particle.png +0 -0
  30. data/examples/NeHe/star.png +0 -0
  31. data/examples/NeHe/tim.png +0 -0
  32. data/examples/OrangeBook/3Dlabs-License.txt +33 -0
  33. data/examples/OrangeBook/brick.frag +36 -0
  34. data/examples/OrangeBook/brick.rb +378 -0
  35. data/examples/OrangeBook/brick.vert +41 -0
  36. data/examples/OrangeBook/particle.frag +17 -0
  37. data/examples/OrangeBook/particle.rb +408 -0
  38. data/examples/OrangeBook/particle.vert +38 -0
  39. data/examples/README +16 -0
  40. data/examples/RedBook/aapoly.rb +143 -0
  41. data/examples/RedBook/aargb.rb +121 -0
  42. data/examples/RedBook/accanti.rb +164 -0
  43. data/examples/RedBook/accpersp.rb +217 -0
  44. data/examples/RedBook/alpha.rb +125 -0
  45. data/examples/RedBook/alpha3D.rb +160 -0
  46. data/examples/RedBook/bezcurve.rb +107 -0
  47. data/examples/RedBook/bezmesh.rb +139 -0
  48. data/examples/RedBook/checker.rb +126 -0
  49. data/examples/RedBook/clip.rb +97 -0
  50. data/examples/RedBook/colormat.rb +137 -0
  51. data/examples/RedBook/cube.rb +71 -0
  52. data/examples/RedBook/depthcue.rb +101 -0
  53. data/examples/RedBook/dof.rb +206 -0
  54. data/examples/RedBook/double.rb +107 -0
  55. data/examples/RedBook/drawf.rb +93 -0
  56. data/examples/RedBook/feedback.rb +147 -0
  57. data/examples/RedBook/fog.rb +168 -0
  58. data/examples/RedBook/font.rb +153 -0
  59. data/examples/RedBook/hello.rb +81 -0
  60. data/examples/RedBook/image.rb +139 -0
  61. data/examples/RedBook/jitter.rb +207 -0
  62. data/examples/RedBook/light.rb +157 -0
  63. data/examples/RedBook/lines.rb +130 -0
  64. data/examples/RedBook/list.rb +113 -0
  65. data/examples/RedBook/material.rb +276 -0
  66. data/examples/RedBook/mipmap.rb +158 -0
  67. data/examples/RedBook/model.rb +115 -0
  68. data/examples/RedBook/movelight.rb +134 -0
  69. data/examples/RedBook/pickdepth.rb +181 -0
  70. data/examples/RedBook/planet.rb +110 -0
  71. data/examples/RedBook/quadric.rb +160 -0
  72. data/examples/RedBook/robot.rb +117 -0
  73. data/examples/RedBook/select.rb +198 -0
  74. data/examples/RedBook/smooth.rb +97 -0
  75. data/examples/RedBook/stencil.rb +165 -0
  76. data/examples/RedBook/stroke.rb +169 -0
  77. data/examples/RedBook/surface.rb +168 -0
  78. data/examples/RedBook/teaambient.rb +134 -0
  79. data/examples/RedBook/teapots.rb +184 -0
  80. data/examples/RedBook/tess.rb +185 -0
  81. data/examples/RedBook/texbind.rb +149 -0
  82. data/examples/RedBook/texgen.rb +171 -0
  83. data/examples/RedBook/texturesurf.rb +130 -0
  84. data/examples/RedBook/varray.rb +161 -0
  85. data/examples/RedBook/wrap.rb +150 -0
  86. data/examples/misc/OGLBench.rb +338 -0
  87. data/examples/misc/anisotropic.rb +196 -0
  88. data/examples/misc/fbo_test.rb +357 -0
  89. data/examples/misc/font-glut.rb +47 -0
  90. data/examples/misc/glfwtest.rb +30 -0
  91. data/examples/misc/plane.rb +161 -0
  92. data/examples/misc/readpixel.rb +66 -0
  93. data/examples/misc/sdltest.rb +36 -0
  94. data/examples/misc/trislam.rb +829 -0
  95. data/ext/opengl/GL/gl.h +2115 -0
  96. data/ext/opengl/GL/glext.h +11770 -0
  97. data/ext/opengl/common.h +372 -0
  98. data/ext/opengl/conv.h +247 -0
  99. data/ext/opengl/extconf.rb +19 -0
  100. data/ext/opengl/fptr_struct.h +912 -0
  101. data/ext/opengl/funcdef.h +279 -0
  102. data/ext/opengl/gl-1.0-1.1.c +3345 -0
  103. data/ext/opengl/gl-1.2.c +155 -0
  104. data/ext/opengl/gl-1.3.c +447 -0
  105. data/ext/opengl/gl-1.4.c +346 -0
  106. data/ext/opengl/gl-1.5.c +225 -0
  107. data/ext/opengl/gl-2.0.c +665 -0
  108. data/ext/opengl/gl-2.1.c +57 -0
  109. data/ext/opengl/gl-3.0.c +502 -0
  110. data/ext/opengl/gl-enums.c +4853 -0
  111. data/ext/opengl/gl-enums.h +14528 -0
  112. data/ext/opengl/gl-error.c +110 -0
  113. data/ext/opengl/gl-error.h +25 -0
  114. data/ext/opengl/gl-ext-3dfx.c +27 -0
  115. data/ext/opengl/gl-ext-arb.c +880 -0
  116. data/ext/opengl/gl-ext-ati.c +41 -0
  117. data/ext/opengl/gl-ext-ext.c +885 -0
  118. data/ext/opengl/gl-ext-gremedy.c +41 -0
  119. data/ext/opengl/gl-ext-nv.c +679 -0
  120. data/ext/opengl/gl.c +265 -0
  121. data/ext/opengl/gl_buffer.c +202 -0
  122. data/ext/opengl/glimpl.c +187 -0
  123. data/ext/opengl/glimpl.h +47 -0
  124. data/ext/opengl/opengl.c +9 -0
  125. data/lib/gl.rb +1 -0
  126. data/lib/opengl.rb +79 -0
  127. data/lib/opengl/bindings_version.rb +4 -0
  128. data/lib/opengl/implementation.rb +38 -0
  129. data/lib/opengl/test_case.rb +86 -0
  130. data/test/dummy.xorg.conf +140 -0
  131. data/test/test_gl.rb +45 -0
  132. data/test/test_gl_10_11.rb +1363 -0
  133. data/test/test_gl_12.rb +71 -0
  134. data/test/test_gl_13.rb +221 -0
  135. data/test/test_gl_14.rb +218 -0
  136. data/test/test_gl_15.rb +258 -0
  137. data/test/test_gl_20.rb +417 -0
  138. data/test/test_gl_21.rb +436 -0
  139. data/test/test_gl_ext_arb.rb +508 -0
  140. data/test/test_gl_ext_ati.rb +28 -0
  141. data/test/test_gl_ext_ext.rb +608 -0
  142. data/test/test_gl_ext_gremedy.rb +29 -0
  143. data/test/test_gl_ext_nv.rb +349 -0
  144. data/test/test_glimpl.rb +23 -0
  145. data/test/test_opengl_buffer.rb +122 -0
  146. data/utils/README +6 -0
  147. data/utils/enumgen.rb +108 -0
  148. data/utils/extlistgen.rb +90 -0
  149. metadata +326 -0
@@ -0,0 +1,47 @@
1
+ require 'opengl'
2
+ require 'glut'
3
+ include Gl,Glut
4
+
5
+ display = Proc.new do
6
+ glClear(GL_COLOR_BUFFER_BIT)
7
+ GL.LoadIdentity
8
+
9
+ glRasterPos2d(100,100)
10
+ "Hello Bitmap".each_byte { |x| glutBitmapCharacter(GLUT_BITMAP_9_BY_15, x) }
11
+
12
+ GL.Translate(100, 250, 0)
13
+ GL.Scale(0.5, 0.5, 1)
14
+ "Hello Stroke".each_byte { |x| glutStrokeCharacter(GLUT_STROKE_ROMAN, x) }
15
+
16
+ glutSwapBuffers()
17
+ end
18
+
19
+ reshape = Proc.new do |w, h|
20
+ glViewport(0, 0, w, h)
21
+ glMatrixMode(GL_PROJECTION)
22
+ glLoadIdentity()
23
+ glOrtho(0.0, w, 0.0, h, -1.0, 1.0)
24
+ glMatrixMode(GL_MODELVIEW)
25
+ end
26
+
27
+ keyboard = Proc.new do |key, x, y|
28
+ case (key)
29
+ when ?\e
30
+ exit(0)
31
+ end
32
+ end
33
+
34
+ # Main Loop
35
+ # Open window with initial window size, title bar,
36
+ # color index display mode, and handle input events.
37
+ #
38
+ glutInit
39
+ glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB)
40
+ glutInitWindowSize(500, 500)
41
+ glutInitWindowPosition(100, 100)
42
+ glutCreateWindow($0)
43
+
44
+ glutReshapeFunc(reshape)
45
+ glutDisplayFunc(display)
46
+ glutKeyboardFunc(keyboard)
47
+ glutMainLoop
@@ -0,0 +1,30 @@
1
+ require 'opengl'
2
+ require 'glfw'
3
+
4
+ #init
5
+ Glfw.glfwOpenWindow( 500,500, 0,0,0,0, 32,0, Glfw::GLFW_WINDOW )
6
+
7
+ # main loop
8
+ while true
9
+ if( Glfw.glfwGetWindowParam( Glfw::GLFW_OPENED ) == false ||
10
+ Glfw.glfwGetKey(Glfw::GLFW_KEY_ESC) == Glfw::GLFW_PRESS )
11
+ break
12
+ end
13
+
14
+ Gl.glClear( Gl::GL_COLOR_BUFFER_BIT | Gl::GL_DEPTH_BUFFER_BIT )
15
+
16
+ Gl.glBegin( Gl::GL_POLYGON )
17
+ Gl.glColor3f( 1.0, 0.0, 0.0 )
18
+ Gl.glVertex2f( -0.5, -0.5 )
19
+ Gl.glColor3f( 0.0, 1.0, 0.0 )
20
+ Gl.glVertex2f( -0.5, 0.5 )
21
+ Gl.glColor3f( 0.0, 0.0, 1.0 )
22
+ Gl.glVertex2f( 0.5, 0.5 )
23
+ Gl.glColor3f( 1.0, 0.0, 1.0 )
24
+ Gl.glVertex2f( 0.5, -0.5 )
25
+ Gl.glEnd
26
+
27
+ Glfw.glfwSwapBuffers()
28
+
29
+ sleep 0.01 # to avoid consuming all CPU power
30
+ end
@@ -0,0 +1,161 @@
1
+ #!/usr/bin/env ruby -rubygems
2
+ #/* Copyright (c) Mark J. Kilgard, 1994. */
3
+ #
4
+ #/*
5
+ # * (c) Copyright 1993, Silicon Graphics, Inc.
6
+ # * ALL RIGHTS RESERVED
7
+ # * Permission to use, copy, modify, and distribute this software for
8
+ # * any purpose and without fee is hereby granted, provided that the above
9
+ # * copyright notice appear in all copies and that both the copyright notice
10
+ # * and this permission notice appear in supporting documentation, and that
11
+ # * the name of Silicon Graphics, Inc. not be used in advertising
12
+ # * or publicity pertaining to distribution of the software without specific,
13
+ # * written prior permission.
14
+ # *
15
+ # * THE MATERIAL EMBODIED ON THIS SOFTWARE IS PROVIDED TO YOU "AS-IS"
16
+ # * AND WITHOUT WARRANTY OF ANY KIND, EXPRESS, IMPLIED OR OTHERWISE,
17
+ # * INCLUDING WITHOUT LIMITATION, ANY WARRANTY OF MERCHANTABILITY OR
18
+ # * FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL SILICON
19
+ # * GRAPHICS, INC. BE LIABLE TO YOU OR ANYONE ELSE FOR ANY DIRECT,
20
+ # * SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY
21
+ # * KIND, OR ANY DAMAGES WHATSOEVER, INCLUDING WITHOUT LIMITATION,
22
+ # * LOSS OF PROFIT, LOSS OF USE, SAVINGS OR REVENUE, OR THE CLAIMS OF
23
+ # * THIRD PARTIES, WHETHER OR NOT SILICON GRAPHICS, INC. HAS BEEN
24
+ # * ADVISED OF THE POSSIBILITY OF SUCH LOSS, HOWEVER CAUSED AND ON
25
+ # * ANY THEORY OF LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE
26
+ # * POSSESSION, USE OR PERFORMANCE OF THIS SOFTWARE.
27
+ # *
28
+ # * US Government Users Restricted Rights
29
+ # * Use, duplication, or disclosure by the Government is subject to
30
+ # * restrictions set forth in FAR 52.227.19(c)(2) or subparagraph
31
+ # * (c)(1)(ii) of the Rights in Technical Data and Computer Software
32
+ # * clause at DFARS 252.227-7013 and/or in similar or successor
33
+ # * clauses in the FAR or the DOD or NASA FAR Supplement.
34
+ # * Unpublished-- rights reserved under the copyright laws of the
35
+ # * United States. Contractor/manufacturer is Silicon Graphics,
36
+ # * Inc., 2011 N. Shoreline Blvd., Mountain View, CA 94039-7311.
37
+ # *
38
+ # * OpenGL(TM) is a trademark of Silicon Graphics, Inc.
39
+ # */
40
+ #/*
41
+ # * plane.c
42
+ # * This program demonstrates the use of local versus
43
+ # * infinite lighting on a flat plane.
44
+ # */
45
+
46
+ require "gl"
47
+ require "glut"
48
+ require "mathn"
49
+
50
+ # /* Initialize material property, light source, and lighting model.
51
+ # */
52
+ def myinit
53
+ mat_ambient = [ 0.0, 0.0, 0.0, 1.0 ];
54
+ #/* mat_specular and mat_shininess are NOT default values */
55
+ mat_diffuse = [ 0.4, 0.4, 0.4, 1.0 ];
56
+ mat_specular = [ 1.0, 1.0, 1.0, 1.0 ];
57
+ mat_shininess = [ 15.0 ];
58
+
59
+ light_ambient = [ 0.0, 0.0, 0.0, 1.0 ];
60
+ light_diffuse = [ 1.0, 1.0, 1.0, 1.0 ];
61
+ light_specular = [ 1.0, 1.0, 1.0, 1.0 ];
62
+ lmodel_ambient = [ 0.2, 0.2, 0.2, 1.0 ];
63
+
64
+ Gl.glMaterial(Gl::GL_FRONT, Gl::GL_AMBIENT, mat_ambient);
65
+ Gl.glMaterial(Gl::GL_FRONT, Gl::GL_DIFFUSE, mat_diffuse);
66
+ Gl.glMaterial(Gl::GL_FRONT, Gl::GL_SPECULAR, mat_specular);
67
+ Gl.glMaterial(Gl::GL_FRONT, Gl::GL_SHININESS, *mat_shininess);
68
+ Gl.glLight(Gl::GL_LIGHT0, Gl::GL_AMBIENT, light_ambient);
69
+ Gl.glLight(Gl::GL_LIGHT0, Gl::GL_DIFFUSE, light_diffuse);
70
+ Gl.glLight(Gl::GL_LIGHT0, Gl::GL_SPECULAR, light_specular);
71
+ Gl.glLightModel(Gl::GL_LIGHT_MODEL_AMBIENT, lmodel_ambient);
72
+
73
+ Gl.glEnable(Gl::GL_LIGHTING);
74
+ Gl.glEnable(Gl::GL_LIGHT0);
75
+ Gl.glDepthFunc(Gl::GL_LESS);
76
+ Gl.glEnable(Gl::GL_DEPTH_TEST);
77
+ end
78
+
79
+ def drawPlane
80
+ Gl.glBegin(Gl::GL_QUADS);
81
+ Gl.glNormal(0.0, 0.0, 1.0);
82
+ Gl.glVertex(-1.0, -1.0, 0.0);
83
+ Gl.glVertex(0.0, -1.0, 0.0);
84
+ Gl.glVertex(0.0, 0.0, 0.0);
85
+ Gl.glVertex(-1.0, 0.0, 0.0);
86
+
87
+ Gl.glNormal(0.0, 0.0, 1.0);
88
+ Gl.glVertex(0.0, -1.0, 0.0);
89
+ Gl.glVertex(1.0, -1.0, 0.0);
90
+ Gl.glVertex(1.0, 0.0, 0.0);
91
+ Gl.glVertex(0.0, 0.0, 0.0);
92
+
93
+ Gl.glNormal(0.0, 0.0, 1.0);
94
+ Gl.glVertex(0.0, 0.0, 0.0);
95
+ Gl.glVertex(1.0, 0.0, 0.0);
96
+ Gl.glVertex(1.0, 1.0, 0.0);
97
+ Gl.glVertex(0.0, 1.0, 0.0);
98
+
99
+ Gl.glNormal(0.0, 0.0, 1.0);
100
+ Gl.glVertex(0.0, 0.0, 0.0);
101
+ Gl.glVertex(0.0, 1.0, 0.0);
102
+ Gl.glVertex(-1.0, 1.0, 0.0);
103
+ Gl.glVertex(-1.0, 0.0, 0.0);
104
+ Gl.glEnd();
105
+ end
106
+
107
+ display = Proc.new {
108
+ infinite_light = [ 1.0, 1.0, 1.0, 0.0 ];
109
+ local_light = [ 1.0, 1.0, 1.0, 1.0 ];
110
+
111
+ Gl.glClear(Gl::GL_COLOR_BUFFER_BIT | Gl::GL_DEPTH_BUFFER_BIT);
112
+
113
+ Gl.glPushMatrix();
114
+ Gl.glTranslate(-1.5, 0.0, 0.0);
115
+ Gl.glLight(Gl::GL_LIGHT0, Gl::GL_POSITION, infinite_light);
116
+ drawPlane();
117
+ Gl.glPopMatrix();
118
+
119
+ Gl.glPushMatrix();
120
+ Gl.glTranslate(1.5, 0.0, 0.0);
121
+ Gl.glLight(Gl::GL_LIGHT0, Gl::GL_POSITION, local_light);
122
+ drawPlane();
123
+ Gl.glPopMatrix();
124
+ Gl.glFlush();
125
+ }
126
+
127
+ myReshape = Proc.new {|w, h|
128
+ Gl.glViewport(0, 0, w, h);
129
+ Gl.glMatrixMode(Gl::GL_PROJECTION);
130
+ Gl.glLoadIdentity();
131
+ if (w <= h)
132
+ Gl.glOrtho(-1.5, 1.5, -1.5*h/w, 1.5*h/w, -10.0, 10.0);
133
+ else
134
+ Gl.glOrtho(-1.5*w/h, 1.5*w/h, -1.5, 1.5, -10.0, 10.0);
135
+ end
136
+ Gl.glMatrixMode(Gl::GL_MODELVIEW);
137
+ }
138
+
139
+ # Keyboard handler to exit when ESC is typed
140
+ keyboard = lambda do |key, x, y|
141
+ case(key)
142
+ when ?\e
143
+ exit(0)
144
+ end
145
+ end
146
+
147
+ #/* Main Loop
148
+ # * Open window with initial window size, title bar,
149
+ # * RGBA display mode, and handle input events.
150
+ # */
151
+ #int main(int argc, char** argv)
152
+ #{
153
+ Glut.glutInit
154
+ Glut.glutInitDisplayMode(Glut::GLUT_SINGLE | Glut::GLUT_RGB | Glut::GLUT_DEPTH);
155
+ Glut.glutInitWindowSize(500, 200);
156
+ Glut.glutCreateWindow($0);
157
+ myinit();
158
+ Glut.glutReshapeFunc(myReshape);
159
+ Glut.glutDisplayFunc(display);
160
+ Glut.glutKeyboardFunc(keyboard);
161
+ Glut.glutMainLoop();
@@ -0,0 +1,66 @@
1
+ require 'opengl'
2
+ require 'glu'
3
+ require 'glut'
4
+ include Gl,Glu,Glut
5
+
6
+ begin
7
+ require "RMagick"
8
+ rescue Exception
9
+ print "This sample needs RMagick Module.\n"
10
+ exit
11
+ end
12
+
13
+ WIDTH = 500
14
+ HEIGHT = 500
15
+
16
+ display = Proc.new do
17
+ glClear(GL_COLOR_BUFFER_BIT)
18
+
19
+ glBegin(GL_LINES)
20
+ glVertex(0.5, 0.5)
21
+ glVertex(-0.5, -0.5)
22
+ glEnd
23
+
24
+ glFlush()
25
+
26
+ pixels = glReadPixels(0, 0, WIDTH, HEIGHT, GL_RGBA, GL_UNSIGNED_SHORT)
27
+
28
+ image = Magick::Image.new(WIDTH, HEIGHT)
29
+ image.import_pixels(0, 0, WIDTH, HEIGHT, "RGBA", pixels,Magick::ShortPixel)
30
+ image.flip!
31
+ image.write("opengl_window.gif")
32
+ end
33
+
34
+ reshape = Proc.new do |w, h|
35
+ glViewport(0, 0, w, h)
36
+ glMatrixMode(GL_PROJECTION)
37
+ glLoadIdentity()
38
+ if (w <= h)
39
+ gluOrtho2D(-1.0, 1.0, -h.to_f/w.to_f, h.to_f/w.to_f)
40
+ else
41
+ gluOrtho2D(w.to_f/h.to_f, w.to_f/h.to_f, -1.0, 1.0)
42
+ end
43
+ glMatrixMode(GL_MODELVIEW)
44
+ glLoadIdentity()
45
+ end
46
+
47
+
48
+ keyboard = Proc.new do |key, x, y|
49
+ case (key)
50
+ when ?\e
51
+ exit(0);
52
+ end
53
+ end
54
+
55
+ # Main Loop
56
+ # Open window with initial window size, title bar,
57
+ # color index display mode, and handle input events.
58
+ glutInit
59
+ glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB | GLUT_ALPHA)
60
+ glutInitWindowSize(WIDTH, HEIGHT)
61
+ glutInitWindowPosition(100, 100)
62
+ glutCreateWindow($0)
63
+ glutReshapeFunc(reshape)
64
+ glutDisplayFunc(display)
65
+ glutKeyboardFunc(keyboard)
66
+ glutMainLoop
@@ -0,0 +1,36 @@
1
+ require 'opengl'
2
+ require 'glu'
3
+ require 'glut'
4
+ require 'sdl'
5
+
6
+ #init
7
+ SDL.init(SDL::INIT_VIDEO)
8
+ SDL.setGLAttr(SDL::GL_DOUBLEBUFFER,1)
9
+ SDL.setVideoMode(512,512,32,SDL::OPENGL)
10
+
11
+ # main loop
12
+ while true
13
+ while event = SDL::Event2.poll
14
+ case event
15
+ when SDL::Event2::KeyDown, SDL::Event2::Quit
16
+ exit
17
+ end
18
+ end
19
+
20
+ Gl.glClear( Gl::GL_COLOR_BUFFER_BIT | Gl::GL_DEPTH_BUFFER_BIT )
21
+
22
+ Gl.glBegin( Gl::GL_POLYGON )
23
+ Gl.glColor3f( 1.0, 0.0, 0.0 )
24
+ Gl.glVertex2f( -0.5, -0.5 )
25
+ Gl.glColor3f( 0.0, 1.0, 0.0 )
26
+ Gl.glVertex2f( -0.5, 0.5 )
27
+ Gl.glColor3f( 0.0, 0.0, 1.0 )
28
+ Gl.glVertex2f( 0.5, 0.5 )
29
+ Gl.glColor3f( 1.0, 0.0, 1.0 )
30
+ Gl.glVertex2f( 0.5, -0.5 )
31
+ Gl.glEnd
32
+
33
+ SDL.GLSwapBuffers()
34
+
35
+ sleep 0.01 # to avoid consuming all CPU power
36
+ end
@@ -0,0 +1,829 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ # Purpose: Determine performance curves for various methods of pushing
4
+ # triangles and quads through the OpenGL pipeline
5
+
6
+ # Copyright (c) 2004-2006, Geoff Broadwell; this script is released
7
+ # as open source and may be distributed and modified under the terms
8
+ # of either the Artistic License or the GNU General Public License,
9
+ # in the same manner as Perl itself. These licenses should have been
10
+ # distributed to you as part of your Perl distribution, and can be
11
+ # read using `perldoc perlartistic` and `perldoc perlgpl` respectively.
12
+
13
+ # Conversion to ruby by Jan Dvorak <jan.dvorak@kraxnet.cz>
14
+
15
+ require 'opengl'
16
+ require 'glu'
17
+ require 'glut'
18
+ include Gl,Glu,Glut
19
+
20
+ require 'mathn'
21
+
22
+ require_relative 'OGLBench'
23
+
24
+ VER = '0.1.24-ruby-p1'
25
+
26
+ $test = 0
27
+ $run = 0
28
+ $done = false
29
+ $ready = false
30
+
31
+ ### USER CONFIG
32
+
33
+ # Primitive sizes (and therefore counts) are integer divisors of
34
+ # (A^i * B^j * C^k ...) where good A, B, C, ... are relatively prime;
35
+ # this number is used for the draw area height and width and defaults to:
36
+ # 2^4 * 3^2 * 5 = 720
37
+ # You may also want to get fewer data points across the same range by
38
+ # directly using higher powers; for example:
39
+ # 16 * 9 * 5 = 720
40
+ #
41
+ # my @max_powers = (16 => 1, 9 => 1, 5 => 1);
42
+ $max_powers = { 2 => 4, 3 => 2, 5 => 1 }.to_a.flatten
43
+
44
+ # Maximum quads along each axis for known slow versus usually fast tests;
45
+ # chosen to be somewhat reasonable for most common settings of @max_powers
46
+ # my $max_count_slow = 60;
47
+ $max_count_slow = 154
48
+ $max_count_fast = 154
49
+
50
+ # Font to use to label graphs
51
+ $font_style = GLUT_BITMAP_HELVETICA_10
52
+
53
+ ### MISC GLOBALS
54
+
55
+ $conf = $app = $gl_info = nil
56
+ $MIN_FRAMES = $MIN_SECONDS = 0
57
+ $w = $h = 0
58
+ $dls = {}
59
+ $vas = {}
60
+ $combos = $slow = $fast = 0
61
+ $showing_graph = false
62
+ $empty_time = $empty_frames = $total = 0
63
+ $max = []
64
+ $stats = []
65
+ $stats_fixed = []
66
+
67
+ ### CODE
68
+
69
+ def main
70
+ init()
71
+ print "Benchmarks:"
72
+
73
+ glutDisplayFunc(method(:cbDraw).to_proc)
74
+ glutIdleFunc(method(:cbDraw).to_proc)
75
+ glutKeyboardFunc(method(:cbKeyPressed).to_proc)
76
+ glutMainLoop()
77
+ end
78
+
79
+ def init
80
+ $stdout.sync = true
81
+
82
+ $combos = recurse_combos($max_powers)
83
+ $combos.sort!
84
+ $slow = $combos.select { |a| a <= $max_count_slow }
85
+ $fast = $combos.select { |a| a > $max_count_slow &&
86
+ a <= $max_count_slow }
87
+
88
+ # Choose drawing area size to match counts
89
+ $h = $w = $combos.last
90
+
91
+ default_conf = {
92
+ :title => 'Triangle Slammer OpenGL Benchmark',
93
+ :geometry => "#{$w}x#{$h}",
94
+ :frames => 10,
95
+ :seconds => 1,
96
+ }
97
+ $conf, $app, $gl_info = OGLBench.basic_init(default_conf)
98
+
99
+
100
+ # Reduce indirections in inner loops
101
+ $MIN_FRAMES, $MIN_SECONDS = $conf[:frames], $conf[:seconds]
102
+
103
+ # Let user know what's going on
104
+ show_user_message()
105
+
106
+ # Change projection to integer-pixel ortho
107
+ glMatrixMode(GL_PROJECTION)
108
+ glOrtho(0, $w, 0, $h, -1, 1)
109
+ glMatrixMode(GL_MODELVIEW)
110
+
111
+ # Make sure GL state is consistent for VA and DL creation
112
+ start_frame()
113
+
114
+ # Create vertex arrays and display lists outside timing loop
115
+ init_vertex_arrays()
116
+ init_display_lists()
117
+
118
+ # Clean up GL state
119
+ end_frame()
120
+ end
121
+
122
+ def recurse_combos(powers)
123
+ base, max_power, *rest = powers
124
+
125
+ return [1] if powers.size<1
126
+
127
+ combos = []
128
+ (0..max_power).each do |power|
129
+ multiplier = base ** power
130
+ recurse_combos(rest).each do |item|
131
+ combos << (item*multiplier)
132
+ end
133
+ end
134
+ combos
135
+ end
136
+
137
+ def show_user_message
138
+ print <<"EOM";
139
+ TRISLAM benchmarks several methods of pushing OpenGL primitives,
140
+ testing each method with various primitive counts and sizes.
141
+ During the benchmark, the test window will start out black, slowly
142
+ brightening to white as testing progresses. Once benchmarking is
143
+ complete, the collected data will be dumped in tabular form.
144
+
145
+ The configuration for this series of tests will be as follows:
146
+
147
+ EOM
148
+
149
+ OGLBench.show_basic_config($conf, $gl_info, VER)
150
+
151
+ puts "standard runs: #{$slow.join(' ')}"
152
+ puts "extra fast runs: #{$fast.join(' ')}"
153
+ puts '-' * 79
154
+ end
155
+
156
+
157
+ def init_vertex_arrays
158
+ print "Init vertex arrays:"
159
+
160
+ $va_types.keys.sort.each do |type|
161
+ print " #{type}"
162
+ ($slow + $fast).each do |count|
163
+ data = $va_types[type].call(count, $w / count.to_f)
164
+ va = data.pack("f*")
165
+ $vas["#{type}_#{count}"] = va
166
+ end
167
+ end
168
+
169
+ print ".\n";
170
+ end
171
+
172
+ def init_display_lists
173
+ print "Init display lists:"
174
+
175
+ num_lists = $dl_types.size * ($slow + $fast).size
176
+ current = glGenLists(num_lists)
177
+
178
+ $dl_types.keys.sort.each do |type|
179
+ print " #{type}"
180
+ ($slow + $fast).each do |count|
181
+ $dls["#{type}_#{count}"] = current
182
+ glNewList(current, GL_COMPILE)
183
+ $dl_types[type].call(count, $w / count.to_f)
184
+ glEndList()
185
+ current += 1
186
+ end
187
+ end
188
+ puts "."
189
+ end
190
+
191
+ def benchmark
192
+ if ($test >= $tests.size)
193
+ print ".\n" if (!$done)
194
+ $done = true
195
+ return
196
+ end
197
+ name,draw,stats,class_ = $tests[$test]
198
+
199
+ counts = class_ == 'single' ? [1] : (class_ == 'slow' ? $slow : $slow + $fast)
200
+
201
+ if ($run == 0)
202
+ print " #{name}";
203
+
204
+ # After printing current test name, busy wait for a second
205
+ # so that the terminal can catch up and not do work while
206
+ # the GL timing is in progress
207
+ a = Time.now
208
+ while 1 > (Time.now - a) do end
209
+ end
210
+
211
+ count = counts[$run]
212
+ size = $w / count
213
+
214
+ OGLBench.fade_to_white(($test + ($run.to_f / counts.size)) / $tests.size)
215
+
216
+ run_done = 0
217
+ frames = 0
218
+ start = Time.now
219
+
220
+ while (run_done==0)
221
+ start_frame()
222
+ draw.call(count, size.to_f)
223
+ end_frame()
224
+
225
+ frames += 1
226
+ run_done = 1 if ($MIN_FRAMES <= frames &&
227
+ $MIN_SECONDS <= Time.now - start)
228
+ end
229
+ glFinish()
230
+ end_ = Time.now
231
+ time = end_ - start
232
+
233
+ $stats << [name,count,time,frames] + stats.call(count,size.to_f)
234
+
235
+ $run += 1
236
+ if ($run >= counts.size)
237
+ $test += 1
238
+ $run = 0
239
+ end
240
+ end
241
+
242
+ def cleanup
243
+ fixup_stats()
244
+ show_stats()
245
+ draw_stats()
246
+ end
247
+
248
+ def start_frame
249
+ glClear(GL_COLOR_BUFFER_BIT |
250
+ GL_DEPTH_BUFFER_BIT)
251
+ end
252
+
253
+ def end_frame
254
+ glFinish()
255
+ end
256
+
257
+ def fixup_stats
258
+ empty = $stats.shift
259
+
260
+ $empty_time = empty[2]
261
+ $empty_frames = empty[3]
262
+ empty_tpf = $empty_time.to_f / $empty_frames
263
+
264
+ $total = ['totl,','avg'] + [0] * 12
265
+ $max = ['max','max'] + [0] * 12
266
+
267
+ $stats.each do |stat|
268
+ name, count, time, frames, pixpf, prmpf, tpf, vpf = stat
269
+
270
+ # Subtract out empty loop time, and loop if negative result
271
+ # $time -= $empty_tpf * $frames;
272
+ if (time <= 0)
273
+ stat += [0] * 5
274
+ next
275
+ end
276
+
277
+ # Calc "work", the geometric mean of pixels and vertices
278
+ workpf = Math::sqrt(pixpf * vpf)
279
+
280
+ # Calc fps
281
+ fps = frames / time
282
+
283
+ # Calc other perf stats
284
+ pixps = pixpf * fps
285
+ prmps = prmpf * fps
286
+ tps = tpf * fps
287
+ vps = vpf * fps
288
+ wps = workpf * fps
289
+
290
+ # Add them to stat row
291
+ stat += [fps, pixps, prmps, tps, vps, wps]
292
+
293
+ # Convert per frame counts to totals
294
+ (4..7).each { |i| stat[i] *= frames }
295
+
296
+ # Update running totals
297
+ (2..7).each { |i| $total[i] += stat[i] }
298
+
299
+ # Update running maximums
300
+ (2..13).each do |i|
301
+ $max[i] = stat[i] if $max[i] < stat[i]
302
+ end
303
+
304
+ $stats_fixed << stat
305
+ end
306
+
307
+ # Calc averages for totals line
308
+ (8..13).each { |i| $total[i] = $total[i-5] / $total[2].to_f }
309
+
310
+ $ready = true
311
+ $stats = $stats_fixed
312
+ end
313
+
314
+
315
+ def show_stats
316
+ basic = ["Name","Cnt","Time"]
317
+ raw = ["Frms","Mpix","Kprim","Ktri","Kvert"]
318
+ calc = raw
319
+ scale = [ 0, 6, 3, 3, 3, ] * 2
320
+ header = basic + raw + calc
321
+ scale.map! {|n| 10 ** n }
322
+
323
+ g_form = "%9s%-*s %s\n"
324
+ h_form = '%-5s%3s %6s' + ' %5s' * raw.size + ' ' + ' %5s' * calc.size + "\n"
325
+ format = '%-5s%3s %6.3f' + ' %5d' * raw.size + ' ' + ' %5d' * calc.size + "\n"
326
+
327
+ printf(g_form, '', 6 * 5 + 8, 'MEASURED', 'PER SECOND')
328
+ printf(h_form, *header)
329
+ printf(format, "empty", 1, $empty_time, $empty_frames, *([0] * 9))
330
+ #
331
+
332
+ ($stats + [$total]).each do |stat|
333
+ st = stat.clone()
334
+ (0..scale.size-1).each do |i|
335
+ st[i + 3] /= scale[i]
336
+ end
337
+ printf(format,*st)
338
+ end
339
+ end
340
+
341
+
342
+ def make_quads_va(count,size)
343
+ data = []
344
+ (0 .. count-1).each do |y|
345
+ (0 .. count-1).each do |x|
346
+ data << x * size << y * size + size
347
+ data << x * size << y * size
348
+ data << x * size + size << y * size
349
+ data << x * size + size << y * size + size
350
+ end
351
+ end
352
+ data
353
+ end
354
+
355
+ def make_tris_va(count,size)
356
+ data = []
357
+ (0 .. count-1).each do |y|
358
+ (0 .. count-1).each do |x|
359
+ data << x * size << y * size + size
360
+ data << x * size << y * size
361
+ data << x * size + size << y * size + size
362
+
363
+ data << x * size + size << y * size + size
364
+ data << x * size << y * size
365
+ data << x * size + size << y * size
366
+ end
367
+ end
368
+ data
369
+ end
370
+
371
+ def make_qs_va(count,size)
372
+ data = []
373
+ (0 .. count-1).each do |y|
374
+ (0 .. count).each do |x|
375
+ data << x * size << y * size + size
376
+ data << x * size << y * size
377
+ end
378
+ end
379
+ data
380
+ end
381
+
382
+ def make_ts_va(count,size)
383
+ data = []
384
+ (0 .. count-1).each do |y|
385
+ (0 .. count).each do |x|
386
+ data << x * size << y * size + size
387
+ data << x * size << y * size
388
+ end
389
+ end
390
+ data
391
+ end
392
+
393
+ def draw_qs(count,size)
394
+ (0 .. count-1).each do |y|
395
+ glBegin(GL_QUAD_STRIP)
396
+ (0 .. count).each do |x|
397
+ glVertex2f(x * size, y * size + size)
398
+ glVertex2f(x * size, y * size )
399
+ end
400
+ glEnd()
401
+ end
402
+ end
403
+
404
+ def draw_ts(count,size)
405
+ (0 .. count-1).each do |y|
406
+ glBegin(GL_TRIANGLE_STRIP)
407
+ (0 .. count).each do |x|
408
+ glVertex2f(x * size, y * size + size)
409
+ glVertex2f(x * size, y * size )
410
+ end
411
+ glEnd()
412
+ end
413
+ end
414
+
415
+ def draw_qs_va(count,size)
416
+ va = $vas["qs_#{count}"]
417
+ row = 2 * (count + 1)
418
+
419
+ glEnableClientState(GL_VERTEX_ARRAY)
420
+ glVertexPointer(2, GL_FLOAT, 0, va)
421
+ (0 .. count-1).each do |y|
422
+ glDrawArrays(GL_QUAD_STRIP, y * row, row)
423
+ end
424
+ glDisableClientState(GL_VERTEX_ARRAY)
425
+ end
426
+
427
+ def draw_ts_va(count,size)
428
+ va = $vas["ts_#{count}"]
429
+ row = 2 * (count + 1)
430
+
431
+ glEnableClientState(GL_VERTEX_ARRAY)
432
+ glVertexPointer(2, GL_FLOAT, 0, va)
433
+ (0 .. count-1).each do |y|
434
+ glDrawArrays(GL_TRIANGLE_STRIP, y * row, row)
435
+ end
436
+ glDisableClientState(GL_VERTEX_ARRAY)
437
+ end
438
+
439
+ def draw_tris(count,size)
440
+ glBegin(GL_TRIANGLES)
441
+ (0 .. count-1).each do |y|
442
+ (0 .. count-1).each do |x|
443
+ glVertex2f(x * size , y * size + size)
444
+ glVertex2f(x * size , y * size )
445
+ glVertex2f(x * size + size, y * size + size)
446
+
447
+ glVertex2f(x * size + size, y * size + size)
448
+ glVertex2f(x * size , y * size )
449
+ glVertex2f(x * size + size, y * size )
450
+ end
451
+ end
452
+ glEnd
453
+ end
454
+
455
+ def stats_tris(count,size)
456
+ length = size * count
457
+ area = length * length
458
+ prims = 2 * count * count
459
+ tris = prims
460
+ verts = 3 * prims
461
+
462
+ [area, prims, tris, verts]
463
+ end
464
+
465
+ def draw_empty(count,size)
466
+ end
467
+
468
+ def stats_empty(count,size)
469
+ [0,0,0,0]
470
+ end
471
+
472
+ def draw_quads(count,size)
473
+ glBegin(GL_QUADS)
474
+ (0 .. count-1).each do |y|
475
+ (0 .. count-1).each do |x|
476
+ glVertex2f(x * size , y * size + size)
477
+ glVertex2f(x * size , y * size )
478
+ glVertex2f(x * size + size, y * size )
479
+ glVertex2f(x * size + size, y * size + size)
480
+ end
481
+ end
482
+ glEnd
483
+ end
484
+
485
+ def stats_quads(count,size)
486
+ length = size * count
487
+ area = length * length
488
+ prims = count * count
489
+ tris = 2 * prims
490
+ verts = 4 * prims
491
+
492
+ [area, prims, tris, verts]
493
+ end
494
+
495
+ def stats_ts(count,size)
496
+ length = size * count
497
+ area = length * length
498
+ prims = count
499
+ tris = 2 * count * prims
500
+ verts = 2 * (count + 1) * prims
501
+
502
+ [area, prims, tris, verts]
503
+ end
504
+
505
+ def stats_qs(count,size)
506
+ length = size * count
507
+ area = length * length
508
+ prims = count
509
+ tris = 2 * count * prims
510
+ verts = 2 * (count + 1) * prims
511
+
512
+ [area, prims, tris, verts]
513
+ end
514
+
515
+ def draw_ts_dl(count,size)
516
+ glCallList($dls["ts_#{count}"]);
517
+ end
518
+
519
+ def draw_qs_dl(count,size)
520
+ glCallList($dls["qs_#{count}"]);
521
+ end
522
+
523
+ def draw_tris_va(count,size)
524
+ va = $vas["t_#{count}"]
525
+
526
+ glVertexPointer(2, GL_FLOAT, 0, va)
527
+
528
+ glEnableClientState(GL_VERTEX_ARRAY)
529
+ glDrawArrays(GL_TRIANGLES, 0, 6 * count * count)
530
+ glDisableClientState(GL_VERTEX_ARRAY)
531
+ end
532
+
533
+ def draw_quads_va(count,size)
534
+ va = $vas["q_#{count}"]
535
+
536
+ glVertexPointer(2, GL_FLOAT, 0, va)
537
+
538
+ glEnableClientState(GL_VERTEX_ARRAY)
539
+ glDrawArrays(GL_QUADS, 0, 4 * count * count)
540
+ glDisableClientState(GL_VERTEX_ARRAY)
541
+ end
542
+
543
+
544
+ def draw_ts_va_dl(count,size)
545
+ va = $vas["ts_#{count}"]
546
+
547
+ glVertexPointer(2, GL_FLOAT, 0, va)
548
+
549
+ glEnableClientState(GL_VERTEX_ARRAY)
550
+ glCallList($dls["tsv_#{count}"])
551
+ glDisableClientState(GL_VERTEX_ARRAY)
552
+ end
553
+
554
+ def draw_qs_va_dl(count,size)
555
+ va = $vas["qs_#{count}"]
556
+
557
+ glVertexPointer(2, GL_FLOAT, 0, va)
558
+
559
+ glEnableClientState(GL_VERTEX_ARRAY)
560
+ glCallList($dls["qsv_#{count}"])
561
+ glDisableClientState(GL_VERTEX_ARRAY)
562
+ end
563
+
564
+
565
+ def draw_stats
566
+ return if (!$ready)
567
+
568
+ # Graph config
569
+ x_off = 10
570
+ y_off = 10
571
+ tick_size = 3
572
+ val_space = 50
573
+ key_size = 20
574
+ x_scale = ($w - 4 * x_off) / (2 * ($fast.last || $slow.last))
575
+ key_scale = ($h - 4 * y_off) / (2 * $tests.size)
576
+
577
+ # Get a fresh black frame for graphing
578
+ glClearColor(0, 0, 0, 1)
579
+ start_frame()
580
+
581
+ # Use antialiased lines
582
+ glEnable(GL_BLEND)
583
+ glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA)
584
+ glEnable(GL_LINE_SMOOTH)
585
+ glHint(GL_LINE_SMOOTH_HINT, GL_NICEST)
586
+
587
+ # Draw axis ticks
588
+ glColor3f(1, 1, 1);
589
+ glBegin(GL_LINES);
590
+
591
+ ([0] + $slow + $fast).each do |count|
592
+ x_tick = count * x_scale + x_off
593
+ glVertex2f(x_tick, y_off)
594
+ glVertex2f(x_tick, y_off - tick_size)
595
+ glVertex2f(x_tick, y_off + $h / 2)
596
+ glVertex2f(x_tick, y_off + $h / 2 - tick_size)
597
+ glVertex2f(x_tick + $w / 2, y_off + $h / 2)
598
+ glVertex2f(x_tick + $w / 2, y_off + $h / 2 - tick_size)
599
+ end
600
+ glEnd
601
+
602
+ x_tick = x_off + 3
603
+ val_max = (($h / 2 - 2 * y_off) / val_space).to_i
604
+
605
+ # Work
606
+ (0..val_max).each do |value|
607
+ y_tick = value * val_space + y_off
608
+
609
+ glBegin(GL_LINES)
610
+ glVertex2f(x_off, y_tick)
611
+ glVertex2f(x_off - tick_size, y_tick)
612
+ glEnd
613
+ end
614
+
615
+ # Pixels
616
+ value = 0
617
+ val_max = $max[9] / mag_scale($max[9])
618
+ y_scale = ($h - 4 * y_off) / (2 * val_max)
619
+ val_inc = tick_inc(val_max,5)
620
+
621
+ while (value < val_max)
622
+ y_tick = (value * y_scale) + y_off
623
+
624
+ glBegin(GL_LINES)
625
+ glVertex2f(x_off, y_tick + $h / 2)
626
+ glVertex2f(x_off - tick_size, y_tick + $h / 2)
627
+ glEnd
628
+ OGLBench.draw_string($font_style, value.to_s, x_tick, y_tick + $h / 2) if (value!=0)
629
+ value += val_inc
630
+ end
631
+
632
+ # Vertices
633
+ value = 0
634
+ val_max = $max[12] / mag_scale($max[12])
635
+ y_scale = ($h - 4 * y_off) / (2 * val_max)
636
+ val_inc = tick_inc(val_max,5)
637
+ while (value < val_max)
638
+ y_tick = (value * y_scale) + y_off
639
+
640
+ glBegin(GL_LINES)
641
+ glVertex2f(x_off + $w / 2, y_tick + $h / 2)
642
+ glVertex2f(x_off + $w / 2 - tick_size, y_tick + $h / 2)
643
+ glEnd
644
+
645
+ OGLBench.draw_string($font_style, value.to_s, x_tick + $w / 2, y_tick + $h / 2) if (value!=0)
646
+ value += val_inc
647
+ end
648
+
649
+ # Draw axes
650
+ glBegin(GL_LINE_STRIP)
651
+ glVertex2f(x_off, $h / 2 - y_off)
652
+ glVertex2f(x_off, y_off)
653
+ glVertex2f($w / 2 - x_off, y_off)
654
+ glEnd
655
+ glBegin(GL_LINE_STRIP)
656
+ glVertex2f(x_off, $h - y_off)
657
+ glVertex2f(x_off, $h / 2 + y_off)
658
+ glVertex2f($w / 2 - x_off, $h / 2 + y_off)
659
+ glEnd
660
+ glBegin(GL_LINE_STRIP)
661
+ glVertex2f($w / 2 + x_off, $h - y_off)
662
+ glVertex2f($w / 2 + x_off, $h / 2 + y_off)
663
+ glVertex2f($w - x_off, $h / 2 + y_off)
664
+ glEnd
665
+
666
+ # Draw color key
667
+ (0..$tests.size - 1).each do |num|
668
+ test = $tests[num]
669
+ name,color,stipple = [test[0]] + test[-2,2]
670
+ glEnable(GL_LINE_STIPPLE)
671
+ glLineStipple(3, stipple)
672
+
673
+ glBegin(GL_LINES)
674
+ glColor3fv(color)
675
+ glVertex2f(x_off + $w / 2, y_off + num * key_scale)
676
+ glVertex2f(x_off + $w / 2 + key_size, y_off + num * key_scale)
677
+ glEnd()
678
+
679
+ glDisable(GL_LINE_STIPPLE)
680
+
681
+ OGLBench.draw_string($font_style, name, x_off + $w / 2 + key_size * 2, y_off + num * key_scale)
682
+ end
683
+
684
+ # Draw performance graph lines
685
+
686
+ # Pixels per second
687
+ draw_one_stat(x_off, y_off + $h / 2, y_off, x_scale, 9)
688
+ glColor3f(1, 1, 1)
689
+
690
+ OGLBench.draw_string($font_style, mag_char($max[9]) + " Pixels/Sec", $w / 4, $h - 2 * y_off)
691
+
692
+ # Vertices per second
693
+ draw_one_stat(x_off + $w / 2, y_off + $h / 2, y_off, x_scale, 12)
694
+ glColor3f(1, 1, 1)
695
+ OGLBench.draw_string($font_style, mag_char($max[12]) + " Vertices/Sec", 3 * $w / 4, $h - 2 * y_off)
696
+
697
+ # "Work" per second, the geometric mean of pixels and vertices
698
+ draw_one_stat(x_off, y_off, y_off, x_scale, 13)
699
+ glColor3f(1, 1, 1)
700
+
701
+ OGLBench.draw_string($font_style, "Work/Sec", $w / 4, $h / 2 - 2 * y_off)
702
+
703
+ # Show our graph
704
+ end_frame();
705
+ $showing_graph = true
706
+ end
707
+
708
+ def draw_one_stat(x_loc,y_loc,y_off,x_scale,num)
709
+ max = $max[num]
710
+ y_scale = ($h - 4 * y_off) / (2 * max)
711
+ colors = {}
712
+ $tests.each do |test| colors[test[0]] = test[-2] end
713
+
714
+ stipple = {}
715
+ $tests.each do |test| stipple[test[0]] = test[-1] end
716
+
717
+ last = ''
718
+
719
+ glEnable(GL_LINE_STIPPLE)
720
+ glBegin(GL_LINE_STRIP)
721
+ $stats.each_with_index do |stat,run|
722
+ name,count, st = stat[0,2] + [stat[num]]
723
+
724
+ if name != last
725
+ glEnd
726
+ glLineStipple(3, stipple[name])
727
+ glBegin(GL_LINE_STRIP)
728
+ last = name
729
+ end
730
+
731
+ glColor3fv(colors[name])
732
+ glVertex2f(count * x_scale + x_loc, st * y_scale + y_loc)
733
+ end
734
+ glEnd
735
+ glDisable(GL_LINE_STIPPLE)
736
+ end
737
+
738
+
739
+ def kilo_mag(num)
740
+ mag = (Math::log(num) / Math::log(10)).to_i
741
+ (mag / 3)
742
+ end
743
+
744
+ def mag_char(num)
745
+ ['','K','M','G','T','P','E','Z','Y'][kilo_mag(num)]
746
+ end
747
+
748
+ def mag_scale(num)
749
+ 10 ** (3*kilo_mag(num))
750
+ end
751
+
752
+ def tick_inc(max,parts = 5)
753
+ return (max / parts.to_f) if (max < 1)
754
+
755
+ mag = (Math::log(max) / Math::log(10)).to_i
756
+ scl = (10 ** (mag - 1))
757
+ inc = max / (scl * parts)
758
+
759
+ if (inc > 7.5)
760
+ inc = 10
761
+ elsif (inc > 3.5)
762
+ inc = 5
763
+ elsif (inc > 1.5)
764
+ inc = 2
765
+ else
766
+ inc = 1
767
+ end
768
+ (inc * scl.to_f)
769
+ end
770
+
771
+ # State engine
772
+ def cbDraw
773
+ if (!$done)
774
+ benchmark()
775
+ elsif (!$ready)
776
+ cleanup()
777
+ else
778
+ sleep(1)
779
+ draw_stats()
780
+ end
781
+ end
782
+
783
+ # Keyboard handler
784
+ def cbKeyPressed(key,x,y)
785
+ if (key == ?\e or key == ?q)
786
+ glutDestroyWindow($app)
787
+ exit(0)
788
+ end
789
+ if ($done && key == ?r)
790
+ draw_stats()
791
+ end
792
+ end
793
+
794
+
795
+ ### METHODS TO BENCHMARK
796
+
797
+ $va_types = {
798
+ "q" => method(:make_quads_va),
799
+ "t" => method(:make_tris_va),
800
+ "qs" => method(:make_qs_va),
801
+ "ts" => method(:make_ts_va),
802
+ }
803
+
804
+ $dl_types = {
805
+ "qs" => method(:draw_qs),
806
+ "ts" => method(:draw_ts),
807
+ "qsv" => method(:draw_qs_va),
808
+ "tsv" => method(:draw_ts_va),
809
+ }
810
+
811
+ $tests = [
812
+ # Nick Draw Routine Stats Calc Type Graph Color
813
+ # ["empty",method(:draw_empty), method(:stats_empty),'single',[1 , 1, 1], 0xFFFF],
814
+ ["t" ,method(:draw_tris), method(:stats_tris) ,'slow', [1 , 0, 0], 0xAAAA],
815
+ ["q" ,method(:draw_quads), method(:stats_quads),'slow', [1 ,0.5, 0], 0xAAAA],
816
+ ["ts" ,method(:draw_ts), method(:stats_ts), 'slow', [1 , 1, 0], 0xAAAA],
817
+ ["qs" ,method(:draw_qs), method(:stats_qs), 'slow', [0 , 1, 0], 0xAAAA],
818
+ ["tsd" ,method(:draw_ts_dl), method(:stats_ts), 'fast', [0 , 1, 1], 0xAAAA],
819
+ ["qsd" ,method(:draw_qs_dl), method(:stats_qs), 'fast', [0 , 0, 1], 0xAAAA],
820
+ ["tv" ,method(:draw_tris_va), method(:stats_tris), 'fast', [0.8, 0, 0], 0xFFFF],
821
+ ["qv" ,method(:draw_quads_va),method(:stats_quads),'fast', [0.8,0.4, 0], 0xFFFF],
822
+ ["tsv" ,method(:draw_ts_va), method(:stats_ts), 'fast', [0.8,0.8, 0], 0xFFFF],
823
+ ["qsv" ,method(:draw_qs_va), method(:stats_qs), 'fast', [0 ,0.8, 0], 0xFFFF],
824
+ ["tsvd" ,method(:draw_ts_va_dl),method(:stats_ts), 'fast', [0 ,0.8,0.8], 0xFFFF],
825
+ ["qsvd" ,method(:draw_qs_va_dl),method(:stats_qs), 'fast', [0 , 0,0.8], 0xFFFF],
826
+ ]
827
+
828
+ # Start from main function ()
829
+ main()