Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.personasubrosa
- import org.lwjgl.{
- BufferUtils => BU
- , LWJGLException
- }
- import org.lwjgl.opengl.{
- ARBBufferObject => ARBBO
- , ARBFragmentShader => ARBFS
- , ARBShaderObjects => ARBSO
- , ARBVertexBufferObject => ARBVBO
- , ARBVertexShader => ARBVS
- , Display
- , DisplayMode
- , GLContext
- , GL11 => GL
- }
- object Main {
- def initDisplay(): Unit = {
- try {
- Display.setDisplayMode( new DisplayMode(Config.windowX, Config.windowY) )
- Display.create()
- } catch {
- case e: LWJGLException => {
- e.printStackTrace()
- sys.exit(1)
- }
- }
- }
- def initGL(): Unit = {
- GL.glMatrixMode(GL.GL_PROJECTION)
- GL.glLoadIdentity()
- GL.glOrtho(0, Config.windowX, 0, Config.windowY, 1, -1)
- GL.glMatrixMode(GL.GL_MODELVIEW)
- }
- def createVBOID(): Int = {
- if(GLContext.getCapabilities().GL_ARB_vertex_buffer_object) {
- val buffer = BU.createIntBuffer(1)
- ARBBO.glGenBuffersARB(buffer)
- buffer.get(0)
- } else {
- 0
- }
- }
- def buildVertices(vertexPosBuffer: Int): Unit = {
- var vertices = BU.createFloatBuffer(6).put(Array(-0.5f, -0.5f, 0.5f, -0.5f, 0, 0.5f))
- ARBBO.glBindBufferARB(ARBVBO.GL_ARRAY_BUFFER_ARB, vertexPosBuffer)
- ARBBO.glBufferDataARB(ARBVBO.GL_ARRAY_BUFFER_ARB, vertices, ARBBO.GL_STATIC_DRAW_ARB)
- }
- def createShader(source: String, shaderType: Int): Int = {
- val shader = ARBSO.glCreateShaderObjectARB(shaderType)
- ARBSO.glShaderSourceARB(shader, source)
- ARBSO.glCompileShaderARB(shader)
- if (ARBSO.glGetObjectParameteriARB(shader, ARBSO.GL_OBJECT_COMPILE_STATUS_ARB) == GL.GL_FALSE) {
- throw new Exception("Compile Failed")
- }
- shader
- }
- def createProgram(vstr: String, fstr: String): Int = {
- val program = ARBSO.glCreateProgramObjectARB()
- val vertShader = createShader(vstr, ARBVS.GL_VERTEX_SHADER_ARB)
- val fragShader = createShader(fstr, ARBFS.GL_FRAGMENT_SHADER_ARB)
- ARBSO.glAttachObjectARB(program, vertShader)
- ARBSO.glAttachObjectARB(program, fragShader)
- ARBSO.glLinkProgramARB(program)
- if (ARBSO.glGetObjectParameteriARB(program, ARBSO.GL_OBJECT_LINK_STATUS_ARB) == GL.GL_FALSE) {
- throw new Exception("Link Failed")
- }
- program
- }
- def renderGL(vertexPosBuffer: Int, program: Int): Unit = {
- GL.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT)
- //GL.glClearColor(0,0,0.8f,1);
- ARBSO.glUseProgramObjectARB(program)
- val vertexPosAttrib = ARBVS.glGetAttribLocationARB(program, "pos")
- ARBVS.glEnableVertexAttribArrayARB(vertexPosAttrib)
- ARBVS.glVertexAttribPointerARB(vertexPosAttrib, 2, GL.GL_FLOAT, false, 0, 0)
- GL.glDrawArrays(GL.GL_TRIANGLES, 0, 3)
- }
- def main(args: Array[String]): Unit = {
- initDisplay()
- initGL()
- val vertexPosBuffer = createVBOID()
- buildVertices(vertexPosBuffer)
- val vs = "attribute vec2 pos;" +
- "void main() { gl_Position = vec4(pos, 0, 1); }"
- val fs = "precision mediump float;" +
- "void main() { gl_FragColor = vec4(0, 0.8, 0, 1); }"
- val program = createProgram(vs, fs)
- while (!Display.isCloseRequested()) {
- renderGL(vertexPosBuffer, program)
- Display.update()
- Display.sync(60)
- }
- Display.destroy()
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment