fortran66のブログ

fortran について書きます。

Fortranへの移植。


MODULE m_callback
USE opengl_gl
USE opengl_glu
USE opengl_glut
IMPLICIT NONE
INTEGER(GLint), PARAMETER :: KEY_ESC = 27
INTEGER(GLint) :: ixBegin = 0, iyBegin = 0
INTEGER(GLint) :: mButton
REAL(GLfloat) :: distance, twist, elevation, azimuth
REAL(GLdouble) :: xnear = 1.0_gldouble, farw = 30.0_gldouble, fovy = 60.0_gldouble
REAL(GLfloat) :: knotvec(8) = [0.0_glfloat, 0.0_glfloat, 0.0_glfloat, 0.0_glfloat, &
                               1.0_glfloat, 1.0_glfloat, 1.0_glfloat, 1.0_glfloat   ]
REAL(GLfloat) :: cpoint(4, 4) = &
[ [ 1.0_glfloat, -1.0_glfloat,  0.0_glfloat, 1.0_glfloat], &
  [ 0.5_glfloat, -0.5_glfloat,  1.0_glfloat, 1.0_glfloat], &
  [-0.5_glfloat,  0.5_glfloat, -1.0_glfloat, 1.0_glfloat], &
  [-1.0_glfloat,  1.0_glfloat,  0.0_glfloat, 1.0_glfloat]  ]
TYPE (GLUnurbsObj), POINTER :: nrb_obj
CONTAINS
!---------------------------------------------------------
SUBROUTINE create_nurbs()
IMPLICIT NONE
nrb_obj => gluNewNurbsRenderer()
CALL gluNurbsProperty( nrb_obj, GLU_SAMPLING_TOLERANCE, 25.0_glfloat )
RETURN
END SUBROUTINE create_nurbs
!---------------------------------------------------------
SUBROUTINE drawCP()
IMPLICIT NONE
INTEGER :: i
REAL(glfloat) :: x, y, z
CALL glColor3f( 1.0_glfloat, 1.0_glfloat, 0.0_glfloat )
CALL glPointSize( 5.0_glfloat )
CALL glBegin( GL_POINTS )
DO i = 1, 4
 x = cpoint(1, i) / cpoint(4, i)
 y = cpoint(2, i) / cpoint(4, i)
 z = cpoint(3, i) / cpoint(4, i)
 CALL glVertex3f( x, y, z )
END DO
CALL glEnd()
RETURN
END SUBROUTINE drawCP
!---------------------------------------------------------
SUBROUTINE display()
IMPLICIT NONE
CALL glClear( IOR(GL_COLOR_BUFFER_BIT, GL_DEPTH_BUFFER_BIT) )
CALL glPushMatrix()
CALL polarview( )
CALL glEnable( GL_DEPTH_TEST )
!
CALL glColor3f( 1.0_glfloat, 0.0_glfloat, 0.0_glfloat )
CALL glPushMatrix()
!
CALL gluBeginCurve( nrb_obj )
CALL gluNurbsCurve( nrb_obj, 8, knotvec, 4, cpoint(:, 1), 4, GL_MAP1_VERTEX_4 )
CALL gluEndCurve( nrb_obj )
!
CALL glPopMatrix()
CALL drawCP()
CALL glDisable( GL_DEPTH_TEST )
CALL glPopMatrix()
CALL glutSwapBuffers()
RETURN
END SUBROUTINE display
!---------------------------------------------------------
SUBROUTINE myKbd(key, ix, iy)
IMPLICIT NONE
INTEGER(GLcint), INTENT(IN OUT) :: key, ix, iy
SELECT CASE(key)
 CASE (KEY_ESC)
  STOP
 CASE DEFAULT
END SELECT
RETURN
END SUBROUTINE myKbd
!---------------------------------------------------------
SUBROUTINE myMouse( ibutton, istate, ix, iy )
IMPLICIT NONE
INTEGER(GLcint), INTENT(IN OUT) :: ibutton, istate, ix, iy
IF (istate == GLUT_DOWN) THEN
 ixBegin = ix
 iyBegin = iy
 mButton = ibutton
END IF
RETURN
END SUBROUTINE myMouse
!---------------------------------------------------------
SUBROUTINE myMotion( ix, iy )
IMPLICIT NONE
INTEGER(GLcint), INTENT(IN OUT) :: ix, iy
INTEGER(GLcint) :: ixDisp, iyDisp
ixDisp = ix - ixBegin
iyDisp = iy - iyBegin
SELECT CASE (mButton)
 CASE (GLUT_LEFT_BUTTON)
  azimuth   = azimuth   + ixDisp / 2.0_GLfloat
  elevation = elevation - iyDisp / 2.0_GLfloat
 CASE (GLUT_MIDDLE_BUTTON)
  twist = AMOD( twist + ixDisp, 360.0_GLfloat )
 CASE (GLUT_RIGHT_BUTTON)
  distance = distance - iyDisp / 40.0_GLfloat
 CASE DEFAULT
  CONTINUE
END SELECT
ixBegin = ix
iyBegin = iy
CALL glutPostRedisplay()
RETURN
END SUBROUTINE myMotion
!---------------------------------------------------------
SUBROUTINE myInit(progname)
IMPLICIT NONE
CHARACTER(LEN = *), INTENT(IN) :: progname
INTEGER(GLcint) :: iwidth = 400, iheight = 400
INTEGER(GLcint) :: iwin
CALL glutInitWindowPosition( 0_glfloat, 0_glfloat )
CALL glutInitWindowSize( iwidth, iheight )
CALL glutInitDisplayMode( IOR(IOR(GLUT_RGBA, GLUT_DEPTH), GLUT_DOUBLE) )
iwin = glutCreateWindow( progname )
CALL glClearColor( 0.0_glfloat, 0.0_glfloat, 0.0_glfloat, 1.0_glfloat )
CALL glutKeyboardFunc( myKbd )
CALL glutMouseFunc( myMouse )
CALL glutMotionFunc( myMotion )
CALL resetview()
CALL glShadeModel( GL_SMOOTH )
CALL glEnable( GL_LIGHT0 )
RETURN
END SUBROUTINE myInit
!---------------------------------------------------------
SUBROUTINE myReshape(iwidth, iheight)
IMPLICIT NONE
INTEGER(glcint), INTENT(IN OUT) :: iwidth, iheight
REAL(GLDOUBLE) :: aspect
aspect = REAL(iwidth, glfloat) / REAL(iheight, glfloat)
CALL glViewport( 0, 0, iwidth, iheight )
CALL glMatrixMode( GL_PROJECTION )
CALL glLoadIdentity()
CALL gluPerspective( fovy, aspect, xnear, farw )
CALL glMatrixMode( GL_MODELVIEW )
RETURN
END SUBROUTINE myReshape
!---------------------------------------------------------
SUBROUTINE polarview()
IMPLICIT NONE
CALL glTranslatef( 0.0_glfloat, 0.0_glfloat, -distance )
CALL glRotatef(-twist    , 0.0_glfloat, 0.0_glfloat, 1.0_glfloat )
CALL glRotatef(-elevation, 1.0_glfloat, 0.0_glfloat, 0.0_glfloat )
CALL glRotatef(-azimuth  , 0.0_glfloat, 1.0_glfloat, 0.0_glfloat )
RETURN
END SUBROUTINE polarview
!---------------------------------------------------------
SUBROUTINE resetview()
IMPLICIT NONE
distance  =   3.0_glfloat
twist     =   0.0_glfloat
elevation = -45.0_glfloat
azimuth   =  30.0_glfloat
RETURN
END SUBROUTINE resetview
!---------------------------------------------------------
END MODULE m_callback
!=========================================================
PROGRAM GLUT9
USE m_callback
IMPLICIT NONE
CALL glutInit()
CALL myInit( 'GLUT9-1' )
CALL create_nurbs()
CALL glutReshapeFunc( myReshape )
CALL glutDisplayFunc( display )
CALL glutMainLoop()
STOP
END PROGRAM GLUT9