Guest User

No render in Scala

a guest
Jul 11th, 2012
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.38 KB | None | 0 0
  1. package com.personasubrosa
  2.  
  3. import org.lwjgl.{
  4. BufferUtils => BU
  5. , LWJGLException
  6. }
  7. import org.lwjgl.opengl.{
  8. ARBBufferObject => ARBBO
  9. , ARBFragmentShader => ARBFS
  10. , ARBShaderObjects => ARBSO
  11. , ARBVertexBufferObject => ARBVBO
  12. , ARBVertexShader => ARBVS
  13. , Display
  14. , DisplayMode
  15. , GLContext
  16. , GL11 => GL
  17. }
  18.  
  19. object Main {
  20.  
  21. def initDisplay(): Unit = {
  22.  
  23. try {
  24. Display.setDisplayMode( new DisplayMode(Config.windowX, Config.windowY) )
  25. Display.create()
  26. } catch {
  27. case e: LWJGLException => {
  28. e.printStackTrace()
  29. sys.exit(1)
  30. }
  31. }
  32.  
  33. }
  34.  
  35. def initGL(): Unit = {
  36. GL.glMatrixMode(GL.GL_PROJECTION)
  37. GL.glLoadIdentity()
  38. GL.glOrtho(0, Config.windowX, 0, Config.windowY, 1, -1)
  39. GL.glMatrixMode(GL.GL_MODELVIEW)
  40. }
  41.  
  42. def createVBOID(): Int = {
  43. if(GLContext.getCapabilities().GL_ARB_vertex_buffer_object) {
  44. val buffer = BU.createIntBuffer(1)
  45. ARBBO.glGenBuffersARB(buffer)
  46. buffer.get(0)
  47. } else {
  48. 0
  49. }
  50. }
  51.  
  52. def buildVertices(vertexPosBuffer: Int): Unit = {
  53.  
  54. var vertices = BU.createFloatBuffer(6).put(Array(-0.5f, -0.5f, 0.5f, -0.5f, 0, 0.5f))
  55.  
  56. ARBBO.glBindBufferARB(ARBVBO.GL_ARRAY_BUFFER_ARB, vertexPosBuffer)
  57. ARBBO.glBufferDataARB(ARBVBO.GL_ARRAY_BUFFER_ARB, vertices, ARBBO.GL_STATIC_DRAW_ARB)
  58.  
  59. }
  60.  
  61. def createShader(source: String, shaderType: Int): Int = {
  62. val shader = ARBSO.glCreateShaderObjectARB(shaderType)
  63. ARBSO.glShaderSourceARB(shader, source)
  64. ARBSO.glCompileShaderARB(shader)
  65. if (ARBSO.glGetObjectParameteriARB(shader, ARBSO.GL_OBJECT_COMPILE_STATUS_ARB) == GL.GL_FALSE) {
  66. throw new Exception("Compile Failed")
  67. }
  68. shader
  69. }
  70.  
  71. def createProgram(vstr: String, fstr: String): Int = {
  72.  
  73. val program = ARBSO.glCreateProgramObjectARB()
  74. val vertShader = createShader(vstr, ARBVS.GL_VERTEX_SHADER_ARB)
  75. val fragShader = createShader(fstr, ARBFS.GL_FRAGMENT_SHADER_ARB)
  76. ARBSO.glAttachObjectARB(program, vertShader)
  77. ARBSO.glAttachObjectARB(program, fragShader)
  78. ARBSO.glLinkProgramARB(program)
  79. if (ARBSO.glGetObjectParameteriARB(program, ARBSO.GL_OBJECT_LINK_STATUS_ARB) == GL.GL_FALSE) {
  80. throw new Exception("Link Failed")
  81. }
  82. program
  83.  
  84. }
  85.  
  86. def renderGL(vertexPosBuffer: Int, program: Int): Unit = {
  87.  
  88. GL.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT)
  89. //GL.glClearColor(0,0,0.8f,1);
  90.  
  91. ARBSO.glUseProgramObjectARB(program)
  92. val vertexPosAttrib = ARBVS.glGetAttribLocationARB(program, "pos")
  93. ARBVS.glEnableVertexAttribArrayARB(vertexPosAttrib)
  94. ARBVS.glVertexAttribPointerARB(vertexPosAttrib, 2, GL.GL_FLOAT, false, 0, 0)
  95.  
  96. GL.glDrawArrays(GL.GL_TRIANGLES, 0, 3)
  97.  
  98. }
  99.  
  100. def main(args: Array[String]): Unit = {
  101.  
  102. initDisplay()
  103. initGL()
  104.  
  105. val vertexPosBuffer = createVBOID()
  106. buildVertices(vertexPosBuffer)
  107.  
  108. val vs = "attribute vec2 pos;" +
  109. "void main() { gl_Position = vec4(pos, 0, 1); }"
  110. val fs = "precision mediump float;" +
  111. "void main() { gl_FragColor = vec4(0, 0.8, 0, 1); }"
  112. val program = createProgram(vs, fs)
  113.  
  114.  
  115.  
  116. while (!Display.isCloseRequested()) {
  117. renderGL(vertexPosBuffer, program)
  118. Display.update()
  119. Display.sync(60)
  120. }
  121. Display.destroy()
  122.  
  123. }
  124.  
  125. }
Advertisement
Add Comment
Please, Sign In to add comment