1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
|
module olympic
use opengl_gl
use opengl_glu
use opengl_glut
integer, parameter :: &
XSIZE = 100, &
YSIZE = 75, &
RINGS = 5, &
BLUERING = 0, &
BLACKRING = 1, &
REDRING = 2, &
YELLOWRING = 3, &
GREENRING = 4, &
BLACK = 0, &
RED = 1, &
GREEN = 2, &
YELLOW = 3, &
BLUE = 4, &
MAGENTA = 5, &
CYAN = 6, &
WHITE = 7
real(glfloat), parameter :: BACKGROUND = 8.
integer, parameter :: double = kind(0.0d0)
real, parameter :: M_PI = 3.141592654
integer(glenum) rgb, doubleBuffer, directRender
integer(glubyte) rgb_colors(0:RINGS-1,0:2)
integer(glint) mapped_colors(0:RINGS-1)
real(glfloat) dests(0:RINGS-1,0:2)
real(glfloat) offsets(0:RINGS-1,0:2)
real(glfloat) angs(0:RINGS-1)
real(glfloat) rotAxis(0:RINGS-1,0:2)
integer iters(0:RINGS-1)
integer(gluint) theTorus
contains
subroutine FillTorus(rc, numc, rt, numt)
real, intent(in) :: rc, rt
integer, intent(in) :: numc, numt
integer :: i, j, k
real :: s, t
real(glfloat) x, y, z
real pi, twopi
pi = M_PI
twopi = 2 * pi
do i = 0, numc-1
call glBegin(GL_QUAD_STRIP)
do j = 0, numt
do k = 1, 0, -1
s = mod((i + k), numc) + 0.5
t = mod(j, numt)
x = cos(t * twopi / numt) * cos(s * twopi / numc)
y = sin(t * twopi / numt) * cos(s * twopi / numc)
z = sin(s * twopi / numc)
call glNormal3f(x, y, z)
x = (rt + rc * cos(s * twopi / numc)) * cos(t * twopi / numt)
y = (rt + rc * cos(s * twopi / numc)) * sin(t * twopi / numt)
z = rc * sin(s * twopi / numc)
call glVertex3f(x, y, z)
end do
end do
call glEnd()
end do
return
end subroutine filltorus
function Clamp(iters_left,t)
real :: clamp
integer, intent(in) :: iters_left
real, intent(in) :: t
if (iters_left < 3) then
clamp = 0.0
else
clamp = (iters_left - 2) * t / iters_left
endif
return
end function clamp
function MyRand()
real :: myrand, rval
call random_number(rval)
MyRand = 10.0 * (rval - 0.5)
return
end function MyRand
subroutine ReInit()
integer :: i
real :: deviation
deviation = MyRand() / 2
deviation = deviation * deviation
do i=0,RINGS-1
offsets(i,0) = MyRand()
offsets(i,1) = MyRand()
offsets(i,2) = MyRand()
angs(i) = 260.0 * MyRand()
rotAxis(i,0) = MyRand()
rotAxis(i,1) = MyRand()
rotAxis(i,2) = MyRand()
iters(i) = (deviation * MyRand() + 60.0)
enddo
return
end subroutine reinit
subroutine Init()
real(glfloat) :: top_y = 1.0
real(glfloat) :: bottom_y = 0.0
real(glfloat) :: top_z = 0.15
real(glfloat) :: bottom_z = 0.69
real(glfloat) :: spacing = 2.5
real(glfloat), save :: lmodel_ambient(4) = (/0.0, 0.0, 0.0, 0.0/)
real(glfloat), save :: lmodel_twoside(1) = (/GL_FALSE/)
real(glfloat), save :: lmodel_local(1) = (/GL_FALSE/)
real(glfloat), save :: light0_ambient(4) = (/0.1, 0.1, 0.1, 1.0/)
real(glfloat), save :: light0_diffuse(4) = (/1.0, 1.0, 1.0, 0.0/)
real(glfloat), save :: light0_position(4) = (/0.8660254, 0.5, 1.0, 0.0/)
real(glfloat), save :: light0_specular(4) = (/1.0, 1.0, 1.0, 0.0/)
real(glfloat), save :: bevel_mat_ambient(4) = (/0.0, 0.0, 0.0, 1.0/)
real(glfloat), save :: bevel_mat_shininess(1) = (/40.0/)
real(glfloat), save :: bevel_mat_specular(4) = (/1.0, 1.0, 1.0, 0.0/)
real(glfloat), save :: bevel_mat_diffuse(4) = (/1.0, 0.0, 0.0, 0.0/)
integer(glubyte), parameter :: boz_zff = z'ff'
call random_seed()
call ReInit()
rgb_colors = 0
!rgb_colors(BLUERING,2) = z'ff' <= ifort does not like this
rgb_colors(BLUERING,2) = boz_zff
rgb_colors(REDRING,0) = boz_zff
rgb_colors(GREENRING,1) = boz_zff
rgb_colors(YELLOWRING,0) = boz_zff
rgb_colors(YELLOWRING,1) = boz_zff
mapped_colors(BLUERING) = BLUE
mapped_colors(REDRING) = RED
mapped_colors(GREENRING) = GREEN
mapped_colors(YELLOWRING) = YELLOW
mapped_colors(BLACKRING) = BLACK
dests(BLUERING,:) = (/-spacing, top_y, top_z/)
dests(BLACKRING,:) = (/0.0, top_y, top_z/)
dests(REDRING,:) = (/spacing, top_y, top_z/)
dests(YELLOWRING,:) = (/-spacing / 2.0, bottom_y, bottom_z/)
dests(GREENRING,:) = (/spacing / 2.0, bottom_y, bottom_z/)
theTorus = glGenLists(1)
call glNewList(theTorus, GL_COMPILE)
call FillTorus(0.1, 6, 1.0, 25)
call glEndList()
call glEnable(GL_CULL_FACE)
call glCullFace(GL_BACK)
call glEnable(GL_DEPTH_TEST)
call glClearDepth(1.0_glclampd)
if( rgb == GL_TRUE )then
call glClearColor(0.5_glclampf, 0.5_glclampf, 0.5_glclampf, 0.0_glclampf)
call glLightfv(GL_LIGHT0, GL_AMBIENT, light0_ambient)
call glLightfv(GL_LIGHT0, GL_DIFFUSE, light0_diffuse)
call glLightfv(GL_LIGHT0, GL_SPECULAR, light0_specular)
call glLightfv(GL_LIGHT0, GL_POSITION, light0_position)
call glEnable(GL_LIGHT0)
call glLightModelfv(GL_LIGHT_MODEL_LOCAL_VIEWER, lmodel_local)
call glLightModelfv(GL_LIGHT_MODEL_TWO_SIDE, lmodel_twoside)
call glLightModelfv(GL_LIGHT_MODEL_AMBIENT, lmodel_ambient)
call glEnable(GL_LIGHTING)
call glMaterialfv(GL_FRONT, GL_AMBIENT, bevel_mat_ambient)
call glMaterialfv(GL_FRONT, GL_SHININESS, bevel_mat_shininess)
call glMaterialfv(GL_FRONT, GL_SPECULAR, bevel_mat_specular)
call glMaterialfv(GL_FRONT, GL_DIFFUSE, bevel_mat_diffuse)
call glColorMaterial(GL_FRONT_AND_BACK, GL_DIFFUSE)
call glEnable(GL_COLOR_MATERIAL)
call glShadeModel(GL_SMOOTH)
else
call glClearIndex(BACKGROUND)
call glShadeModel(GL_FLAT)
endif
call glMatrixMode(GL_PROJECTION)
call gluPerspective(45._gldouble, 1.33_gldouble, 0.1_gldouble, 100.0_gldouble)
call glMatrixMode(GL_MODELVIEW)
return
end subroutine init
subroutine glutnullfunc() bind(C)
end subroutine glutnullfunc
end module olympic
subroutine Idle() bind(C)
use olympic
integer :: i, j
integer(glenum) :: more = GL_FALSE
do i=0,RINGS-1
if( iters(i) /= 0 )then
do j = 0, 2
offsets(i,j) = Clamp(iters(i), offsets(i,j))
enddo
angs(i) = Clamp(iters(i), angs(i))
iters(i) = iters(i) - 1
more = GL_TRUE
end if
enddo
if( more == GL_TRUE )then
call glutPostRedisplay()
else
call glutIdleFunc(glutnullfunc)
endif
return
end subroutine idle
subroutine Reshape(width,height) bind(C)
use olympic
integer(glInt), value :: width, height
! if glInt is not the same as glsizei, width and height will
! need to be copied to variables of the later kind
call glViewport(0_glint, 0_glint, width, height)
return
end subroutine reshape
subroutine Key(ikey,x,y) bind(C)
use olympic
integer(GLubyte), value :: ikey
integer(GLint), value :: x, y
integer :: i
interface
subroutine idle() bind(C)
end subroutine idle
end interface
i = ikey
select case(i)
case(iachar(achar(27))) ! esc
stop
case(iachar(' '))
call ReInit()
call glutIdleFunc(Idle)
end select
return
end subroutine key
! fortran handling of command line arguments is nonstandard, so
! this feature is omitted. Here is the original C code.
!GLenum
!Args(int argc, char **argv)
!{
! GLint i;
!
! rgb = GL_TRUE;
! doubleBuffer = GL_TRUE;
!
! for (i = 1; i < argc; i++) {
! if (strcmp(argv[i], "-ci") == 0) {
! rgb = GL_FALSE;
! } else if (strcmp(argv[i], "-rgb") == 0) {
! rgb = GL_TRUE;
! } else if (strcmp(argv[i], "-sb") == 0) {
! doubleBuffer = GL_FALSE;
! } else if (strcmp(argv[i], "-db") == 0) {
! doubleBuffer = GL_TRUE;
! } else {
! printf("%s (Bad option).\n", argv[i]);
! return GL_FALSE;
! }
! }
! return GL_TRUE;
!}
subroutine visible(vis) bind(C)
use olympic
integer(glInt), value:: vis
interface
subroutine idle() bind(C)
end subroutine idle
end interface
if( vis == GLUT_VISIBLE )then
call glutIdleFunc(Idle)
else
call glutIdleFunc(glutnullfunc)
endif
return
end subroutine visible
subroutine DrawScene() bind(C)
use olympic
integer :: i
real(4) :: x,y,z
call glPushMatrix()
call glClear(ior(GL_COLOR_BUFFER_BIT,GL_DEPTH_BUFFER_BIT))
call gluLookAt(0._gldouble, 0._gldouble, 10._gldouble, &
0._gldouble, 0._gldouble, 0._gldouble, &
0._gldouble, 1._gldouble, 0._gldouble)
do i=0,RINGS-1
if( rgb == GL_TRUE )then
call glColor3ub(rgb_colors(i,0),rgb_colors(i,1),rgb_colors(i,2))
else
call glIndexi(mapped_colors(i))
endif
call glPushMatrix()
x=dests(i,0) + offsets(i,0)
y=dests(i,1) + offsets(i,1)
z=dests(i,2) + offsets(i,2)
call glTranslatef(x, y, z)
call glRotatef(angs(i), rotAxis(i,0), rotAxis(i,1), rotAxis(i,2))
call glCallList(theTorus)
call glPopMatrix()
enddo
call glPopMatrix()
if( doubleBuffer == GL_TRUE )then
call glutSwapBuffers()
else
call glFlush()
endif
return
end subroutine drawscene
program main
use olympic
integer(glenum) :: type
integer :: i
interface
subroutine Reshape(width,height) bind(C)
use olympic
integer(GLint), value :: width, height
end subroutine reshape
subroutine Key(ikey,x,y) bind(C)
use olympic
integer(GLubyte), value :: ikey
integer(glInt), value :: x, y
end subroutine key
subroutine visible(vis) bind(C)
use olympic
integer(glInt), value :: vis
end subroutine visible
subroutine drawscene() bind(C)
end subroutine drawscene
end interface
call glutInitWindowSize(400_glInt, 300_glInt)
! not checking command line arguments
! glutInit(&argc, argv);
! if (Args(argc, argv) == GL_FALSE) {
! exit(1);
! }
call glutinit()
rgb = GL_TRUE ! default values which could have been
doubleBuffer = GL_TRUE ! overwritten by command line arguments
if( rgb == GL_TRUE )then
type = GLUT_RGB
else
type = GLUT_INDEX
endif
if( doubleBuffer == GL_TRUE )then
type = ior(type,GLUT_DOUBLE)
else
type = ior(type,GLUT_SINGLE)
endif
call glutInitDisplayMode(type)
i = glutCreateWindow("Olympic Rings"//char(0))
call Init()
call glutReshapeFunc(Reshape)
call glutKeyboardFunc(Key)
call glutDisplayFunc(DrawScene)
call glutVisibilityFunc(visible)
call glutMainLoop()
end program main
|