SHARE
TWEET

Untitled

a guest Apr 20th, 2019 73 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import org.lwjgl.glfw.GLFW.*
  2. import org.lwjgl.glfw.GLFWErrorCallback
  3. import org.lwjgl.opengl.GL
  4. import org.lwjgl.system.MemoryStack
  5. import org.lwjgl.system.MemoryStack.stackPush
  6. import org.lwjgl.glfw.GLFW.glfwPollEvents
  7. import java.awt.SystemColor.window
  8. import org.lwjgl.glfw.GLFW.glfwSwapBuffers
  9. import org.lwjgl.opengl.GL11
  10. import org.lwjgl.opengl.GL11.*
  11. import org.lwjgl.opengl.GL15.*
  12. import org.lwjgl.opengl.GL20
  13. import org.lwjgl.opengl.GL30.*
  14. import kotlin.math.sin
  15. import org.lwjgl.glfw.GLFW.glfwPollEvents
  16. import java.awt.SystemColor.window
  17. import org.lwjgl.glfw.GLFW.glfwSwapBuffers
  18.  
  19.  
  20. class App(var width: Int, var height: Int) {
  21.     private val window: Long
  22.  
  23.     init {
  24.         GLFWErrorCallback.createPrint(System.err).set()
  25.         if (!glfwInit()) throw IllegalStateException("Unable to initialize GLFW")
  26.         glfwWindowHint(GLFW_RESIZABLE, GLFW_TRUE)
  27.  
  28.         glfwDefaultWindowHints()
  29.         glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3)
  30.         glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 2)
  31.         glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE)
  32.         glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GLFW_TRUE)
  33.  
  34.         window = glfwCreateWindow(width, height, "Amazing test game", 0, 0)
  35.         if (window <= 0) throw IllegalStateException("Unable to create GLFW window")
  36.  
  37.         glfwSetKeyCallback(window) { window, key, _, action, _ ->
  38.             if (key == GLFW_KEY_ESCAPE && action == GLFW_RELEASE) {
  39.                 glfwSetWindowShouldClose(window, true)
  40.             }
  41.         }
  42.         val videoMod = glfwGetVideoMode(glfwGetPrimaryMonitor())!!
  43.  
  44.         glfwSetWindowPos(window, (videoMod.width() - width) / 2, (videoMod.height() - height) / 2)
  45.         glfwMakeContextCurrent(window)
  46.         glfwSwapInterval(1)
  47.         glfwShowWindow(window)
  48.  
  49.     }
  50.  
  51.     fun loop1() {
  52.  
  53.  
  54.     }
  55.  
  56.     fun loop() {
  57.         GL.createCapabilities()
  58.         val vao = glGenVertexArrays()
  59.         stackPush().use {
  60.             val vertices = it.mallocFloat(3 * 6)
  61.             vertices.put(-0.6f).put(-0.4f).put(0f).put(1f).put(0f).put(0f)
  62.             vertices.put(0.6f).put(-0.4f).put(0f).put(0f).put(1f).put(0f)
  63.             vertices.put(0f).put(0.6f).put(0f).put(0f).put(0f).put(1f)
  64.             vertices.flip()
  65.             val vbo = glGenBuffers()
  66.  
  67.             glBindVertexArray(vao)
  68.             glBindBuffer(GL_ARRAY_BUFFER, vbo)
  69.             glBufferData(GL_ARRAY_BUFFER, vertices, GL_STATIC_DRAW)
  70.             val floatSize = 4
  71.  
  72.             val posAttrib = glGetAttribLocation(shaderProgram, "position")
  73.             glVertexAttribPointer(posAttrib, 3, GL_FLOAT, false, 6 * floatSize, 0)
  74.             glEnableVertexAttribArray(posAttrib)
  75.  
  76.             val colAttrib = glGetAttribLocation(shaderProgram, "color")
  77.             glVertexAttribPointer(colAttrib, 3, GL_FLOAT, false, 6 * floatSize, 3L * floatSize)
  78.             glEnableVertexAttribArray(colAttrib)
  79.  
  80.         }
  81.  
  82.         val vertexShader = GL20.glCreateShader(GL20.GL_VERTEX_SHADER)
  83.         GL20.glShaderSource(vertexShader, ClassLoader.getSystemResource("shaders/test.vert").readText())
  84.         GL20.glCompileShader(vertexShader)
  85.  
  86.         val fragShader = GL20.glCreateShader(GL20.GL_FRAGMENT_SHADER)
  87.         GL20.glShaderSource(fragShader, ClassLoader.getSystemResource("shaders/test.frag").readText())
  88.         GL20.glCompileShader(fragShader)
  89.  
  90.         val vertStatus = GL20.glGetShaderi(vertexShader, GL20.GL_COMPILE_STATUS)
  91.         if (vertStatus != GL11.GL_TRUE) throw RuntimeException(GL20.glGetProgramInfoLog(vertexShader))
  92.  
  93.         val fragStatus = GL20.glGetShaderi(fragShader, GL20.GL_COMPILE_STATUS)
  94.         if (fragStatus != GL11.GL_TRUE) throw RuntimeException(GL20.glGetProgramInfoLog(fragShader))
  95.         val shaderProgram = glCreateProgram()
  96.         GL20.glAttachShader(shaderProgram, fragShader)
  97.         GL20.glAttachShader(shaderProgram, vertexShader)
  98.         glBindFragDataLocation(shaderProgram, 0, "fragColor")
  99.         GL20.glLinkProgram(shaderProgram)
  100.         val status = glGetProgrami(shaderProgram, GL_LINK_STATUS)
  101.         if (status != GL_TRUE) {
  102.             throw RuntimeException(glGetProgramInfoLog(shaderProgram))
  103.         }
  104.  
  105.  
  106.  
  107.  
  108.  
  109.         while (!glfwWindowShouldClose(window)) {
  110.             glClear(GL_COLOR_BUFFER_BIT or GL_DEPTH_BUFFER_BIT)
  111.             glUseProgram(shaderProgram)
  112.             glBindVertexArray(vao)
  113.             glDrawArrays(GL_TRIANGLES, 0, 3)
  114.             glfwPollEvents()
  115.             glfwSwapBuffers(window)
  116.         }
  117.  
  118.     }
  119. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top