Guest User

Untitled

a guest
Apr 20th, 2018
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.49 KB | None | 0 0
  1. require 'opengl'
  2. include Gl,Glu,Glut
  3.  
  4. MAXZ=1.0
  5. MINZ=-2.0
  6. ZINC=0.1
  7.  
  8. $solidZ = MAXZ
  9. $transparentZ = MINZ
  10. $sphereList=nil
  11. $cubeList=nil
  12.  
  13. def init
  14. mat_specular = [ 1.0, 1.0, 1.0, 0.15 ]
  15. mat_shininess = [ 100.0 ]
  16. position = [ 0.5, 0.5, 1.0, 0.0 ]
  17.  
  18. glMaterial(GL_FRONT, GL_SPECULAR, mat_specular)
  19. glMaterial(GL_FRONT, GL_SHININESS, mat_shininess)
  20. glLight(GL_LIGHT0, GL_POSITION, position)
  21.  
  22. glEnable(GL_LIGHTING)
  23. glEnable(GL_LIGHT0)
  24. glEnable(GL_DEPTH_TEST)
  25.  
  26. $sphereList = glGenLists(1)
  27. glNewList($sphereList, GL_COMPILE)
  28. glutSolidSphere(0.4, 16, 16)
  29. glEndList()
  30.  
  31. $cubeList = glGenLists(1)
  32. glNewList($cubeList, GL_COMPILE)
  33. glutSolidCube(0.6)
  34. glEndList()
  35. end
  36.  
  37. display = proc do
  38. mat_solid = [ 0.75, 0.75, 0.0, 1.0 ]
  39. mat_zero = [ 0.0, 0.0, 0.0, 1.0 ]
  40. mat_transparent = [ 0.0, 0.8, 0.8, 0.6 ]
  41. mat_emission = [ 0.0, 0.3, 0.3, 0.6 ]
  42.  
  43. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
  44.  
  45. glPushMatrix()
  46. glTranslate(-0.15, -0.15, $solidZ)
  47. glMaterial(GL_FRONT, GL_EMISSION, mat_zero)
  48. glMaterial(GL_FRONT, GL_DIFFUSE, mat_solid)
  49. glCallList($sphereList)
  50. glPopMatrix()
  51.  
  52. glPushMatrix()
  53. glTranslate(0.15, 0.15, $transparentZ)
  54. glRotate(15.0, 1.0, 1.0, 0.0)
  55. glRotate(30.0, 0.0, 1.0, 0.0)
  56. glMaterial(GL_FRONT, GL_EMISSION, mat_emission)
  57. glMaterial(GL_FRONT, GL_DIFFUSE, mat_transparent)
  58. glEnable(GL_BLEND)
  59. glDepthMask(GL_FALSE)
  60. glBlendFunc(GL_SRC_ALPHA, GL_ONE)
  61. glCallList($cubeList)
  62. glDepthMask(GL_TRUE)
  63. glDisable(GL_BLEND)
  64. glPopMatrix()
  65.  
  66. glutSwapBuffers()
  67. end
  68.  
  69. reshape = proc do |w, h|
  70. glViewport(0, 0, w, h)
  71. glMatrixMode(GL_PROJECTION)
  72. glLoadIdentity()
  73. if (w <= h)
  74. glOrtho(-1.5, 1.5, -1.5*h.to_f/w, 1.5*h.to_f/w, -10.0, 10.0)
  75. else
  76. glOrtho(-1.5*w.to_f/h, 1.5*w.to_f/h, -1.5, 1.5, -10.0, 10.0)
  77. end
  78. glMatrixMode(GL_MODELVIEW)
  79. glLoadIdentity()
  80. end
  81.  
  82. animate = proc do
  83. if ($solidZ <= MINZ || $transparentZ >= MAXZ)
  84. glutIdleFunc(nil)
  85. else
  86. $solidZ -= ZINC
  87. $transparentZ += ZINC
  88. glutPostRedisplay()
  89. end
  90. end
  91.  
  92. keyboard = proc do |key, x, y|
  93. case (key)
  94. when ?a,?A
  95. $solidZ = MAXZ
  96. $transparentZ = MINZ
  97. glutIdleFunc(animate)
  98. when ?r, ?R
  99. $solidZ = MAXZ
  100. $transparentZ = MINZ
  101. glutPostRedisplay()
  102. when 27
  103. exit(0)
  104. end
  105. end
  106.  
  107. glutInit()
  108. glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH)
  109. glutInitWindowSize(500, 500)
  110. glutInitWindowPosition(100, 100)
  111. glutCreateWindow($0)
  112. init()
  113. glutReshapeFunc(reshape)
  114. glutKeyboardFunc(keyboard)
  115. glutDisplayFunc(display)
  116. glutMainLoop()
Add Comment
Please, Sign In to add comment