Guest User

Untitled

a guest
Jul 16th, 2018
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.41 KB | None | 0 0
  1. require 'ruboto'
  2.  
  3. java_import "android.opengl.GLSurfaceView"
  4.  
  5. java_import "javax.microedition.khronos.egl.EGL10"
  6. java_import "javax.microedition.khronos.egl.EGLConfig"
  7. java_import "javax.microedition.khronos.opengles.GL10"
  8.  
  9. java_import "java.nio.ByteBuffer"
  10. java_import "java.nio.ByteOrder"
  11. java_import "java.nio.IntBuffer"
  12.  
  13. ruboto_import "org.ruboto.irb.RubotoGLSurfaceViewRenderer"
  14.  
  15. $activity.start_ruboto_activity "$glsurface" do
  16. setTitle "GLSurfaceView"
  17.  
  18. setup_content do
  19. @surface_view = GLSurfaceView.new(self)
  20. @surface_view.setRenderer(@renderer)
  21. @surface_view
  22. end
  23.  
  24. handle_resume do
  25. @surface_view.onResume
  26. end
  27.  
  28. handle_pause do
  29. @surface_view.onPause
  30. end
  31.  
  32. @renderer = RubotoGLSurfaceViewRenderer.new
  33. @translucent_background = false
  34. @cube = Cube.new
  35. @angle = 0.0
  36.  
  37. @renderer.handle_draw_frame do |gl|
  38. gl.glClear(GL10::GL_COLOR_BUFFER_BIT | GL10::GL_DEPTH_BUFFER_BIT)
  39.  
  40. gl.glMatrixMode(GL10::GL_MODELVIEW)
  41. gl.glLoadIdentity
  42. gl.glTranslatef(0, 0, -3.0)
  43. gl.glRotatef(@angle, 0, 1, 0)
  44. gl.glRotatef(@angle*0.25, 1, 0, 0)
  45.  
  46. gl.glEnableClientState(GL10::GL_VERTEX_ARRAY)
  47. gl.glEnableClientState(GL10::GL_COLOR_ARRAY)
  48.  
  49. @cube.draw(gl)
  50.  
  51. gl.glRotatef(@angle*2.0, 0, 1, 1)
  52. gl.glTranslatef(0.5, 0.5, 0.5)
  53.  
  54. @cube.draw(gl)
  55.  
  56. @angle += 1.2
  57. end
  58.  
  59. @renderer.handle_surface_changed do |gl, width, height|
  60. gl.glViewport(0, 0, width, height)
  61. ratio = width.to_f / height.to_f
  62. gl.glMatrixMode(GL10::GL_PROJECTION)
  63. gl.glLoadIdentity
  64. gl.glFrustumf(-ratio, ratio, -1, 1, 1, 10)
  65. end
  66.  
  67. @renderer.handle_surface_created do |gl, config|
  68. gl.glDisable(GL10::GL_DITHER)
  69.  
  70. gl.glHint(GL10::GL_PERSPECTIVE_CORRECTION_HINT, GL10::GL_FASTEST)
  71.  
  72. if (@translucent_background)
  73. gl.glClearColor(0,0,0,0)
  74. else
  75. gl.glClearColor(1,1,1,1)
  76. end
  77. gl.glEnable(GL10::GL_CULL_FACE)
  78. gl.glShadeModel(GL10::GL_SMOOTH)
  79. gl.glEnable(GL10::GL_DEPTH_TEST)
  80. end
  81. end
  82.  
  83. class Cube
  84. def initialize
  85. one = 0x10000
  86. vertices = [
  87. -one, -one, -one,
  88. one, -one, -one,
  89. one, one, -one,
  90. -one, one, -one,
  91. -one, -one, one,
  92. one, -one, one,
  93. one, one, one,
  94. -one, one, one,
  95. ]
  96.  
  97. colors = [
  98. 0, 0, 0, one,
  99. one, 0, 0, one,
  100. one, one, 0, one,
  101. 0, one, 0, one,
  102. 0, 0, one, one,
  103. one, 0, one, one,
  104. one, one, one, one,
  105. 0, one, one, one,
  106. ]
  107.  
  108. indices = [
  109. 0, 4, 5, 0, 5, 1,
  110. 1, 5, 6, 1, 6, 2,
  111. 2, 6, 7, 2, 7, 3,
  112. 3, 7, 4, 3, 4, 0,
  113. 4, 7, 6, 4, 6, 5,
  114. 3, 0, 1, 3, 1, 2
  115. ]
  116.  
  117. vbb = ByteBuffer.allocateDirect(vertices.length*4)
  118. vbb.order(ByteOrder.nativeOrder)
  119. @vertex_buffer = vbb.asIntBuffer
  120. @vertex_buffer.put(vertices.to_java(:int))
  121. @vertex_buffer.position(0)
  122.  
  123. cbb = ByteBuffer.allocateDirect(colors.length*4)
  124. cbb.order(ByteOrder.nativeOrder)
  125. @color_buffer = cbb.asIntBuffer
  126. @color_buffer.put(colors.to_java(:int))
  127. @color_buffer.position(0)
  128.  
  129. @index_buffer = ByteBuffer.allocateDirect(indices.length)
  130. @index_buffer.put(indices.to_java(:byte))
  131. @index_buffer.position(0)
  132. end
  133.  
  134. def draw(gl)
  135. gl.glFrontFace(GL10::GL_CW)
  136. gl.glVertexPointer(3, GL10::GL_FIXED, 0, @vertex_buffer)
  137. gl.glColorPointer(4, GL10::GL_FIXED, 0, @color_buffer)
  138. gl.glDrawElements(GL10::GL_TRIANGLES, 36, GL10::GL_UNSIGNED_BYTE, @index_buffer)
  139. end
  140. end
Add Comment
Please, Sign In to add comment