Advertisement
Guest User

Untitled

a guest
Apr 7th, 2020
180
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Kotlin 11.48 KB | None | 0 0
  1. package example
  2.  
  3. import org.lwjgl.glfw.GLFW
  4. import org.lwjgl.glfw.GLFWErrorCallback
  5. import org.lwjgl.glfw.GLFWKeyCallback
  6. import org.lwjgl.opengl.GL
  7. import org.lwjgl.opengl.GL11.*
  8. import org.lwjgl.system.MemoryUtil
  9. import kotlin.math.*
  10.  
  11.  
  12. class Lab3 {
  13.     private var window: Long = 0
  14.     private var keyCallback: GLFWKeyCallback? = null
  15.     private var errorCallback: GLFWErrorCallback? = null
  16.     private var colorMain = floatArrayOf(.0f, .0f, .0f)
  17.     private var rotationX = .0f
  18.     private var rotationY = .0f
  19.     private var rotationZ = .0f
  20.     private var moveX = .0f
  21.     private var moveY = .0f
  22.     private var moveZ = .0f
  23.     private var sizeX = .6f
  24.     private var sizeY = .6f
  25.     private var sizeZ = .6f
  26.     private var mode = GL_LINE
  27.     private val small = .15f
  28.     private var faces = 3
  29.     private var step = (2 * PI / faces.toFloat()).toFloat()
  30.     private var height = 1f
  31.     private var top = ArrayList<ArrayList<Triple<Float, Float, Float>>>()
  32.     private var bottom = ArrayList<ArrayList<Triple<Float, Float, Float>>>()
  33.     private var sidesTop = ArrayList<Triple<Float, Float, Float>>()
  34.     private var sidesBottom = ArrayList<Triple<Float, Float, Float>>()
  35.     private var linesCount = 2
  36.     private var lines = ArrayList<ArrayList<Triple<Float, Float, Float>>>()
  37.  
  38.     private fun init() {
  39.         GLFW.glfwSetErrorCallback(GLFWErrorCallback.createPrint(System.err).also { errorCallback = it })
  40.         check(GLFW.glfwInit().toInt() == GLFW.GLFW_TRUE) { "Unable to initialize GLFW" }
  41.         GLFW.glfwDefaultWindowHints()
  42.         GLFW.glfwWindowHint(GLFW.GLFW_VISIBLE, GLFW.GLFW_TRUE)
  43.         GLFW.glfwWindowHint(GLFW.GLFW_RESIZABLE, GLFW.GLFW_TRUE)
  44.         val WIDTH = 1500
  45.         val HEIGHT = 1000
  46.         window = GLFW.glfwCreateWindow(WIDTH, HEIGHT, "LAB2", MemoryUtil.NULL, MemoryUtil.NULL)
  47.         if (window == MemoryUtil.NULL) throw RuntimeException("Failed to create the GLFW window")
  48.         GLFW.glfwSetKeyCallback(window, object : GLFWKeyCallback() {
  49.             override fun invoke(window: Long, key: Int, scancode: Int, action: Int, mods: Int) {
  50.                 if (action == GLFW.GLFW_RELEASE && key == GLFW.GLFW_KEY_ESCAPE) {
  51.                     GLFW.glfwSetWindowShouldClose(
  52.                         window,
  53.                         GLFW.GLFW_TRUE.toBoolean()
  54.                     )
  55.                 } else if (action == GLFW.GLFW_PRESS || action == GLFW.GLFW_REPEAT) {
  56.                     when (key) {
  57.                         GLFW.GLFW_KEY_UP -> rotationX += 5f
  58.                         GLFW.GLFW_KEY_DOWN -> rotationX -= 5f
  59.                         GLFW.GLFW_KEY_LEFT -> rotationY += 5f
  60.                         GLFW.GLFW_KEY_RIGHT -> rotationY -= 5f
  61.                         GLFW.GLFW_KEY_SPACE -> changeMode()
  62.                         GLFW.GLFW_KEY_W -> moveY += .15f
  63.                         GLFW.GLFW_KEY_S -> moveY -= .15f
  64.                         GLFW.GLFW_KEY_A -> moveX -= .15f
  65.                         GLFW.GLFW_KEY_D -> moveX += .15f
  66.                         GLFW.GLFW_KEY_O -> moveZ += .15f
  67.                         GLFW.GLFW_KEY_K -> moveZ -= .15f
  68.                         GLFW.GLFW_KEY_P -> {
  69.                             sizeX += .01f
  70.                             sizeY += .01f
  71.                             sizeZ += .01f
  72.                         }
  73.                         GLFW.GLFW_KEY_M -> {
  74.                             sizeX -= .01f
  75.                             sizeY -= .01f
  76.                             sizeZ -= .01f
  77.                         }
  78.                         GLFW.GLFW_KEY_PAGE_UP -> {
  79.                             faces += 1
  80.                             step = (2 * PI / faces).toFloat()
  81.                             calc()
  82.                         }
  83.                         GLFW.GLFW_KEY_PAGE_DOWN -> {
  84.                             if (faces > 3) {
  85.                                 faces -= 1
  86.                                 step = (2 * PI / faces).toFloat()
  87.                                 calc()
  88.                             }
  89.                         }
  90.                         GLFW.GLFW_KEY_HOME -> {
  91.                             if (linesCount > 2) {
  92.                                 linesCount--
  93.                                 calc()
  94.                             }
  95.                         }
  96.                         GLFW.GLFW_KEY_END -> {
  97.                             linesCount++
  98.                             calc()
  99.                         }
  100.                     }
  101.                 }
  102.             }
  103.         }.also { keyCallback = it })
  104.  
  105.         val vidMode = GLFW.glfwGetVideoMode(GLFW.glfwGetPrimaryMonitor())
  106.         GLFW.glfwSetWindowPos(window, (vidMode!!.width() - WIDTH) / 2, (vidMode.height() - HEIGHT) / 2)
  107.         GLFW.glfwMakeContextCurrent(window)
  108.         GLFW.glfwSwapInterval(1)
  109.         GLFW.glfwShowWindow(window)
  110.     }
  111.  
  112.     private fun render() {
  113.         drawCoords()
  114.         drawPyramid()
  115.         drawCube()
  116.     }
  117.  
  118.     private fun calc() {
  119.         var angle = 0f
  120.         val lineStep = height / linesCount
  121.  
  122.         //SIDE
  123.         sidesTop.clear()
  124.         sidesBottom.clear()
  125.         while (angle <= 2 * PI) {
  126.             val x = cos(angle)
  127.             val y = sin(angle)
  128.             sidesTop.add(Triple(x/2, height, y/2))
  129.             sidesBottom.add(Triple(x, 0f, y))
  130.             angle += step
  131.         }
  132.         sidesTop.add(sidesTop[0])
  133.         sidesBottom.add(sidesBottom[0])
  134.  
  135.  
  136.         //LINES
  137.         lines.clear()
  138.         var i: Float = lineStep
  139.         var j = 0
  140.         while (i < height) {
  141.             angle = 0f
  142.             val level = ArrayList<Triple<Float, Float, Float>>()
  143.             while (angle < 2*PI){
  144.                 val k = 1 - i / 2
  145.                 val x = cos(angle)
  146.                 val y = sin(angle)
  147.                 level.add(Triple(x * k, i, y * k))
  148.                 angle+=step
  149.                 println("$k")
  150.             }
  151.             j++
  152.             lines.add(level)
  153.             println("--- $i")
  154.             i += lineStep
  155.         }
  156.  
  157.         //TOP
  158.         top.clear()
  159.         var k = (.5f / linesCount)
  160.         while(k <= .5f) {
  161.             val tmp = ArrayList<Triple<Float, Float, Float>>()
  162.             tmp.add(Triple(0f, height, 0f))
  163.             angle = 0f
  164.             while (angle <= 2 * PI) {
  165.                 tmp.add(Triple(k * cos(angle), height,  k * sin(angle)))
  166.                 angle += step
  167.             }
  168.             tmp.add(tmp[1])
  169.             top.add(tmp)
  170.             k += (.5f/linesCount)
  171.         }
  172.  
  173.         //BOTTOM
  174.         bottom.clear()
  175.         k = 1f / linesCount
  176.         while (k <= 1) {
  177.             val tmp = ArrayList<Triple<Float, Float, Float>>()
  178.             tmp.add(Triple(0f, 0f, 0f))
  179.             angle = 0f
  180.             while (angle <= 2 * PI) {
  181.                 tmp.add(Triple(cos(angle) * k, 0f, sin(angle) * k))
  182.                 angle += step
  183.             }
  184.             tmp.add(tmp[1])
  185.             bottom.add(tmp)
  186.             k += 1f / linesCount
  187.         }
  188.     }
  189.  
  190.     private fun drawCoords() {
  191.         glPushMatrix()
  192.         glLoadIdentity()
  193.  
  194.         glColor3f(.5f, .5f, .5f)
  195.  
  196.         glBegin(GL_LINE_STRIP)
  197.         glVertex3f(0f, 0f, 0f)
  198.         glVertex3f(10f, 0f, 0f)
  199.         glEnd()
  200.  
  201.         glBegin(GL_LINE_STRIP)
  202.         glVertex3f(0f, 0f, 0f)
  203.         glVertex3f(0f, 10f, 0f)
  204.         glEnd()
  205.  
  206.         glBegin(GL_LINE_STRIP)
  207.         glVertex3f(0f, 0f, 0f)
  208.         glVertex3f(0f, 0f, 10f)
  209.         glEnd()
  210.     }
  211.  
  212.     private fun drawPyramid() {
  213.         glPushMatrix()
  214.  
  215.         glTranslatef(moveX, moveY, moveZ)
  216.         glRotatef(rotationX, 1f, 0f, 0f)
  217.         glRotatef(rotationY, 0f, 1f, 0f)
  218.         glRotatef(rotationZ, 0f, 0f, 1f)
  219.         glScalef(sizeX, sizeY, sizeZ)
  220.  
  221.         // ***** //
  222.  
  223.         //SIDES
  224.         glBegin(GL_QUAD_STRIP)
  225.         for (s in sidesTop.indices) {
  226.             glColor3f(0f, .5f, .3f)
  227.             glVertex3f(sidesBottom[s].first, sidesBottom[s].second, sidesBottom[s].third)
  228.             glVertex3f(sidesTop[s].first, sidesTop[s].second, sidesTop[s].third)
  229. //            glVertex3f(0f, height*2, 0f)
  230.         }
  231.         glEnd()
  232.  
  233.         //LINES
  234.  
  235.         for (line in lines) {
  236.             glBegin(GL_LINE_STRIP)
  237.             for (l in line) {
  238.                 glVertex3f(l.first, l.second, l.third)
  239.             }
  240.             glVertex3f(line[0].first, line[0].second, line[0].third)
  241.             glEnd()
  242.         }
  243.  
  244.  
  245.         //TOP
  246.  
  247.  
  248.         for (tmp in top) {
  249.             glBegin(GL_TRIANGLE_FAN)
  250.             glColor3f(0.0f, 0.0f, 1.0f)
  251.             for (t in tmp) {
  252.                 glColor3f(1.0f, 0.0f, 0.0f)
  253.                 glVertex3f(t.first, t.second, t.third)
  254.             }
  255.             glEnd()
  256.         }
  257.  
  258.         //BOTTOM
  259.         for (tmp in bottom) {
  260.             glBegin(GL_TRIANGLE_FAN)
  261.             glColor3f(0.0f, 0.0f, 1.0f)
  262.             for (b in tmp) {
  263.                 glColor3f(1.0f, 0.0f, 0.0f)
  264.                 glVertex3f(b.first, b.second, b.third)
  265.             }
  266.  
  267.             glEnd()
  268.         }
  269.  
  270.         glPopMatrix()
  271.     }
  272.  
  273.  
  274.     private fun drawCube() {
  275.         glPushMatrix()
  276.         glLoadIdentity()
  277.  
  278.         glTranslatef(-.7f, -.7f, 0f)
  279.  
  280.         glBegin(GL_QUADS)
  281.  
  282.         glColor3f(1f, 0f, 0f)
  283.         glVertex3f(-small, -small, -small)
  284.         glVertex3f(-small, small, -small)
  285.         glVertex3f(small, small, -small)
  286.         glVertex3f(small, -small, -small)
  287.  
  288.         glColor3f(0f, 1f, 0f)
  289.         glVertex3f(-small, -small, small)
  290.         glVertex3f(-small, small, small)
  291.         glVertex3f(small, small, small)
  292.         glVertex3f(small, -small, small)
  293.  
  294.         glColor3f(0f, 0f, 1f)
  295.         glVertex3f(-small, small, -small)
  296.         glVertex3f(small, small, -small)
  297.         glVertex3f(small, small, small)
  298.         glVertex3f(-small, small, small)
  299.  
  300.         glColor3f(1f, 1f, 0f)
  301.         glVertex3f(-small, -small, -small)
  302.         glVertex3f(small, -small, -small)
  303.         glVertex3f(small, -small, small)
  304.         glVertex3f(-small, -small, small)
  305.  
  306.         glColor3f(1f, 1f, 1f)
  307.         glVertex3f(-small, -small, -small)
  308.         glVertex3f(-small, small, -small)
  309.         glVertex3f(-small, small, small)
  310.         glVertex3f(-small, -small, small)
  311.  
  312.         glColor3f(1f, 0f, .5f)
  313.         glVertex3f(small, -small, -small)
  314.         glVertex3f(small, small, -small)
  315.         glVertex3f(small, small, small)
  316.         glVertex3f(small, -small, small)
  317.         glEnd()
  318.  
  319.         glPopMatrix()
  320.     }
  321.  
  322.     private fun rotateCoords() {
  323.         val cabView = floatArrayOf(
  324.             1f, 0f, 0f, 0f,
  325.             0f, 1f, 0f, 0f,
  326.             -.5f * cos(PI / 4).toFloat(), -.5f * cos(PI / 4).toFloat(), -1f, 0f,
  327.             0f, 0f, 0f, 1f
  328.         )
  329.         glMultMatrixf(cabView)
  330.         glMatrixMode(GL_MODELVIEW)
  331.         glLoadIdentity()
  332.     }
  333.  
  334.     private fun loop() {
  335.         GL.createCapabilities()
  336.  
  337.         glEnable(GL_DEPTH_TEST)
  338.         glDepthFunc(GL_LEFT)
  339.  
  340.         calc()
  341.  
  342.         while (GLFW.glfwWindowShouldClose(window).toInt() == GLFW.GLFW_FALSE) {
  343.             glViewport(0, 0, 1000, 1000)
  344.             glMatrixMode(GL_PROJECTION)
  345.             glLoadIdentity()
  346.  
  347.             rotateCoords()
  348.  
  349.             glClearColor(colorMain[0], colorMain[1], colorMain[2], 0.0f)
  350.             glClear(GL_COLOR_BUFFER_BIT or GL_DEPTH_BUFFER_BIT)
  351.  
  352.             glPolygonMode(GL_FRONT_AND_BACK, mode)
  353.  
  354.             render()
  355.             GLFW.glfwSwapBuffers(window)
  356.             GLFW.glfwPollEvents()
  357.         }
  358.     }
  359.  
  360.     fun run() {
  361.         println("running LWJGL")
  362.         try {
  363.             init()
  364.             loop()
  365.             GLFW.glfwDestroyWindow(window)
  366.             keyCallback!!.free()
  367.         } finally {
  368.             GLFW.glfwTerminate()
  369.             errorCallback!!.free()
  370.         }
  371.     }
  372.  
  373.     fun changeMode() {
  374.         mode = if (mode == GL_FILL) {
  375.             GL_LINE
  376.         } else GL_FILL
  377.     }
  378.  
  379. }
  380.  
  381. fun main() {
  382.     Lab3().run()
  383. }
  384.  
  385. private fun Boolean.toInt() = if (this) 1 else 0
  386. private fun Int.toBoolean() = this != 0
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement