fortran66のブログ

fortran について書きます。

OpenGLによる3次元CGプログラミング
作者: 林武文, 加藤清敬
出版社/メーカー: コロナ社

MODULE m_callback
USE opengl_gl
USE opengl_glu
USE opengl_glut
IMPLICIT NONE
INTEGER(GLCINT), PARAMETER :: KEY_ESC = 27
REAL(glfloat) :: theta = 0.0_glfloat
CONTAINS
!---------------------------
SUBROUTINE display()
IMPLICIT NONE
CALL glClear( GL_COLOR_BUFFER_BIT )
CALL glPushMatrix()
CALL glTranslatef( 0.0_glfloat, 0.0_glfloat, -5.0_glfloat )
CALL glRotatef( theta, 0.0_glfloat, 1.0_glfloat, 0.0_glfloat )
CALL glColor3f( 1.0_glfloat, 1.0_glfloat, 1.0_glfloat )
CALL glRectf( -1.0_glfloat, -1.0_glfloat, 1.0_glfloat, 1.0_glfloat )
CALL glPopMatrix()
CALL glutSwapBuffers()
RETURN
END SUBROUTINE display
!---------------------------
SUBROUTINE idle()
IMPLICIT NONE
theta = AMOD(theta + 0.5_glfloat, 360.0_glfloat)
CALL glutPostRedisplay()
RETURN
END SUBROUTINE idle
!---------------------------
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
CALL glutPostRedisplay()
RETURN
END SUBROUTINE myKbd
!---------------------------
END MODULE m_callback
!=========================================================
MODULE m_subs
USE m_callback
IMPLICIT NONE
CONTAINS
!---------------------------
SUBROUTINE myInit(progname)
IMPLICIT NONE
CHARACTER(LEN = *), INTENT(IN) :: progname
INTEGER(GLCINT) :: iwidth = 640, iheight = 480
INTEGER(GLCINT) :: iwin
REAL(GLDOUBLE) :: aspect
aspect = REAL(iwidth, glfloat) / REAL(iheight, glfloat)
CALL glutInitWindowPosition( 0_glfloat, 0_glfloat )
CALL glutInitWindowSize( iwidth, iheight )
CALL glutInitDisplayMode( IOR(GLUT_RGBA, GLUT_DOUBLE) )
iwin = glutCreateWindow( progname )
CALL glClearColor( 0.0_glfloat, 0.0_glfloat, 0.0_glfloat, 1.0_glfloat )
!
CALL glutKeyboardFunc( myKbd )
RETURN
END SUBROUTINE myInit
!---------------------------
SUBROUTINE myReshape(iwidth, iheight)
IMPLICIT NONE
INTEGER(GLCINT), INTENT(IN) :: 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( 45.0_gldouble, aspect, 1.0_gldouble, 10.0_gldouble )
CALL glMatrixMode( GL_MODELVIEW )
RETURN
END SUBROUTINE myReshape
!---------------------------
END MODULE m_subs
!=========================================================
PROGRAM GLUT5
USE m_subs
IMPLICIT NONE
CALL glutInit()
CALL myInit( 'GLUT5-1' )
CALL glutReshapeFunc( myReshape )
CALL glutDisplayFunc( display )
CALL glutIdleFunc( idle )
CALL glutMainLoop()
STOP
END PROGRAM GLUT5