c ===============================
        subroutine pretvori_geom_model_v_GL_gradnike

	include 'fgl.h'

        integer p(5,4)
        real t(8, 3), n(5,3)

        integer i,j

        common /geometrija/ t, p, n

  	call fglInitNames
        call fglPushName(-1)
	do 10 i=1,5
	  call fglLoadName(i)
          call fglBegin(GL_QUADS)
            do 20 j=1,4
              call fglNormal3f( n(i,1),n(i,2),n(i,3))
              call fglVertex3f(
     *         t(p(i,j), 1), t(p(i,j), 2), t(p(i,j), 3) )
20          continue
          call fglEnd
10	continue
  	call fglPopName

        return
        end
c ===============================
	subroutine risi

	include 'fgl.h'
	include 'fglut.h'	

        real fi_x, fi_y

        common /rotacije  / fi_x, fi_y

        data fi_x/30.0/, fi_y/-30.0/

	call fglClear(GL_COLOR_BUFFER_BIT+GL_DEPTH_BUFFER_BIT)
	call fglColor3f(1.0, 0.4, 1.0)

	call fglLoadIdentity
        call fglScalef(0.5, 0.5, 0.5)
        call fglRotatef(fi_x, 1.0, 0.0, 0.0)
        call fglRotatef(fi_y, 0.0, 1.0, 0.0)

        call pretvori_geom_model_v_GL_gradnike

        call fglutSwapBuffers
	return
	end
c ===============================
	subroutine miska(x, y)

	include 'fglut.h'	
	include 'fgl.h'	

        integer x, y

        integer stari_x, stari_y

        real fi_x, fi_y

        common /rotacije/ fi_x, fi_y

        save stari_x, stari_y

        data stari_x /0/, stari_y/0/

        if (x .gt. stari_x) fi_y = fi_y + 1.0
        if (x .lt. stari_x) fi_y = fi_y - 1.0
        if (y .gt. stari_y) fi_x = fi_x + 1.0
        if (y .lt. stari_y) fi_x = fi_x - 1.0

	stari_x = x
	stari_y = y

	call fglutPostRedisplay
	return
	end	

c ===============================
        subroutine obdelaj_zadetke(stevilo_zadetkov, pomnilnik)

        integer i, j, ii, stevilo_nivojev
        integer stevilo_zadetkov, pomnilnik(512)

        character*20 ime_objekta(5)

        common /ime/ ime_objekta

        write (*,*) '==== obdelaj zadetke: ======'
        write (*,*) 'stevilo_zadetkov = ', stevilo_zadetkov
        write (*,*) (pomnilnik(i), i=1,10)
        ii = 0
        do 10 i=1, stevilo_zadetkov
          ii = ii + 1
          stevilo_nivojev = pomnilnik(ii)
          ii = ii+3
          write(*,*) 'Imena so ',
     *          (pomnilnik(j), j=ii, ii+stevilo_nivojev-1)
          write(*,*) 'Interpretacija imena:',
     *          ime_objekta(pomnilnik(ii))
10      continue

        return
        end
c ===============================
        real*8 function doubler(a)
        real a
        doubler = a
        return
        end
        real*8 function doublei(a)
        integer a
        doublei = a
        return
        end
c ===============================
	subroutine pritisk_na_gumb_miske(stevilka_gumba, status, x, y)

        include 'fgl.h'
        include 'fglu.h'
        include 'fglut.h'

	integer stevilka_gumba, status, x, y
        real*8 doubler, doublei

        integer selectBuf(512), stevilo_zadetkov, viewport(4)

        if (stevilka_gumba .eq. GLUT_MIDDLE_BUTTON 
     *            .and. status .eq. 1) then
	call fglGetIntegerv(GL_VIEWPORT, viewport)
        call fglSelectBuffer(512, selectBuf)
        stevilo_zadetkov = fglRenderMode(GL_SELECT)

	call fglMatrixMode(GL_PROJECTION)
	call fglPushMatrix
	call fglLoadIdentity
	
        call fgluPickMatrix( doublei(x), doublei(viewport(4) - y),
     *                   doubler(5.0),  doubler(5.0), viewport)
        call fglOrtho( doubler(-1.0),  doubler(1.0),
     *                 doubler(-1.0),  doubler(1.0),
     *                 doubler(-1.0),  doubler(1.0) )
     *
	call fglMatrixMode(GL_MODELVIEW)
	call risi
	call fglFlush

	call fglMatrixMode(GL_PROJECTION)
	call fglPopMatrix

	call fglMatrixMode(GL_MODELVIEW)

        stevilo_zadetkov = fglRenderMode(GL_RENDER)
        write (*,*) 'stevilo_zadetkov=', stevilo_zadetkov
        if (stevilo_zadetkov .gt. 0)
     *    call obdelaj_zadetke(stevilo_zadetkov, selectBuf)
	call fglutPostRedisplay
	endif

	return
	end
c =====================================
        subroutine izdelaj_geom_model

	real tocke(8, 3), normale(5,3)
        integer ploskve(5,4)
        character*20 ime_objekta(5)

        common /geometrija/ tocke, ploskve, normale
        common /ime/ ime_objekta


        data tocke   / -0.5, 0.5, 0.5,-0.5,-0.5, 0.5, 0.5,-0.5, 
     *                 -0.5,-0.5, 0.5, 0.5,-0.5,-0.5, 0.5, 0.5,
     *                 -0.8,-0.8,-0.8,-0.8, 0.8, 0.8, 0.8, 0.8 / 
	data ploskve / 1, 5, 6, 1, 1,
     *                 4, 6, 2, 5, 2,
     *                 3, 7, 3, 8, 6,
     *                 2, 8, 7, 4, 5 /
	data normale / 0.0, 0.0, 1.0,-1.0, 0.0,
     *                 0.0, 0.0, 0.0, 0.0,-1.0,
     *                -1.0, 1.0, 0.0, 0.0, 0.0 /
        data ime_objekta / 'stranica xy1',
     *                     'stranica xy2',
     *                     'stranica yz1',
     *                     'stranica yz2',
     *                     'stranica spd' /

        return
        end
c ===============================
        subroutine inicializiraj_gl

        include 'fgl.h'

        real*8 a

        a = 1.0
	call fglEnable(GL_LIGHTING)
	call fglEnable(GL_LIGHT0)
	call fglEnable(GL_DEPTH_TEST)
	call fglEnable(GL_COLOR_MATERIAL)
        call fglLightModeli(GL_LIGHT_MODEL_TWO_SIDE, GL_TRUE)
        call fglMatrixMode(GL_PROJECTION)
        call fglLoadIdentity
        call fglOrtho(-a,a, -a,a, -a,a)
        call fglMatrixMode(GL_MODELVIEW)
        call fglLoadIdentity

        return
        end
c ===============================
	program glavni

	implicit none

	external risi
	external miska
	external pritisk_na_gumb_miske

	include 'fglut.h'	

        integer moje_okno

        call izdelaj_geom_model


	call fglutInit
        call fglutInitWindowSize(500, 500)
        call fglutInitDisplayMode(GLUT_DOUBLE+GLUT_DEPTH)
        moje_okno = fglutCreateWindow('risi_miska')

        call inicializiraj_gl

	call fglutDisplayFunc(risi)
	call fglutMotionFunc(miska)
	call fglutMouseFunc(pritisk_na_gumb_miske)

	call fglutMainLoop

	stop
	end	

		
