Advertisement
shek15470

Untitled

Jun 8th, 2021
1,101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Go 6.07 KB | None | 0 0
  1. package main
  2.  
  3. import (
  4.     "github.com/go-gl/gl/v2.1/gl"
  5.     "github.com/go-gl/glfw/v3.2/glfw"
  6.     "log"
  7.     "math"
  8.     "runtime"
  9.     "time"
  10. )
  11.  
  12. const (
  13.     width        = 1000
  14.     height       = 1000
  15.     angleMove    = 5
  16.     positionMove = 0.1
  17. )
  18.  
  19. type point struct {
  20.     x float64
  21.     y float64
  22.     z float64
  23. }
  24.  
  25. func (point *point) glVertex3d() {
  26.     gl.Vertex3d(point.x, point.y, point.z)
  27. }
  28.  
  29. type color struct {
  30.     r float64
  31.     g float64
  32.     b float64
  33. }
  34.  
  35. func (color *color) glColor3d() {
  36.     gl.Color3d(color.r, color.g, color.b)
  37. }
  38.  
  39. type figure struct {
  40.     positAbsolute point
  41.     angleX        float64
  42.     angleY        float64
  43.     angleZ        float64
  44.     polygonMode   uint32
  45.     scale         float64
  46.     projection    string
  47.     zNear         float64
  48.     zFar          float64
  49. }
  50.  
  51. func (figure *figure) getProjectionFunc() func(left, right, bottom, top, zNear, zFar float64) {
  52.     switch figure.projection {
  53.     case "Ortho":
  54.         return gl.Ortho
  55.     case "Frustum":
  56.         return gl.Frustum
  57.     }
  58.     return nil
  59. }
  60.  
  61. func (figure *figure) glProjection() {
  62.     if projection := figure.getProjectionFunc(); projection != nil {
  63.         projection(-1, 1, -1, 1, figure.zNear, figure.zFar)
  64.     }
  65. }
  66.  
  67. func (figure *figure) prepareToDraw() {
  68.     figure.glProjection()
  69.     gl.PolygonMode(gl.FRONT_AND_BACK, figure.polygonMode)
  70.     gl.Translated(figure.positAbsolute.x, figure.positAbsolute.y, figure.positAbsolute.z)
  71.     gl.Scaled(figure.scale, figure.scale, figure.scale)
  72.     gl.Rotated(figure.angleX, 1, 0, 0)
  73.     gl.Rotated(figure.angleY, 0, 1, 0)
  74.     gl.Rotated(figure.angleZ, 0, 0, 1)
  75. }
  76.  
  77. type hyperboloid struct {
  78.     figure      figure
  79.     colors      []color
  80.     ratioY      float64
  81.     ratioZ      float64
  82.     mulPoligon  int
  83. }
  84.  
  85. func (hyperboloid *hyperboloid) getPoint(h, w int) *point {
  86.     teta := math.Pi * float64(h) / float64(hyperboloid.mulPoligon*len(hyperboloid.colors))
  87.     phi := 2 * math.Pi * float64(w) / float64(2*hyperboloid.mulPoligon*len(hyperboloid.colors))
  88.     return &point{
  89.         x: math.Sin(teta) * math.Cos(phi),
  90.         y: hyperboloid.ratioY * math.Sin(teta) * math.Sin(phi),
  91.         z: hyperboloid.ratioZ * math.Cos(teta)                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                  ,
  92.     }
  93. }
  94.  
  95. func (hyperboloid *hyperboloid) draw() {
  96.     gl.PushMatrix()
  97.     defer gl.PopMatrix()
  98.  
  99.     hyperboloid.figure.prepareToDraw()
  100.  
  101.     gl.Begin(gl.TRIANGLE_FAN)
  102.     hyperboloid.colors[0].glColor3d()
  103.     hyperboloid.getPoint(0, 0).glVertex3d()
  104.     for i := 0; i <= 2*hyperboloid.mulPoligon*len(hyperboloid.colors); i++ {
  105.         hyperboloid.getPoint(1, i).glVertex3d()
  106.     }
  107.     gl.End()
  108.  
  109.     for j := 1; j < hyperboloid.mulPoligon*len(hyperboloid.colors)-1; j++ {
  110.         gl.Begin(gl.QUAD_STRIP)
  111.         hyperboloid.colors[j/hyperboloid.mulPoligon].glColor3d()
  112.         for i := 0; i <= 2*hyperboloid.mulPoligon*len(hyperboloid.colors); i++ {
  113.             hyperboloid.getPoint(j, i).glVertex3d()
  114.             hyperboloid.getPoint(j+1, i).glVertex3d()
  115.         }
  116.         gl.End()
  117.     }
  118.  
  119.     gl.Begin(gl.TRIANGLE_FAN)
  120.     hyperboloid.colors[len(hyperboloid.colors)-1].glColor3d()
  121.     hyperboloid.getPoint(hyperboloid.mulPoligon*len(hyperboloid.colors), 0).glVertex3d()
  122.     for i := 0; i <= 2*hyperboloid.mulPoligon*len(hyperboloid.colors); i++ {
  123.         hyperboloid.getPoint(hyperboloid.mulPoligon*len(hyperboloid.colors)-1, i).glVertex3d()
  124.     }
  125.     gl.End()
  126. }
  127.  
  128. func main() {
  129.     runtime.LockOSThread()
  130.  
  131.     if err := glfw.Init(); err != nil {
  132.         panic(err)
  133.     }
  134.     defer glfw.Terminate()
  135.  
  136.     glfw.WindowHint(glfw.Resizable, glfw.False)
  137.     window, err := glfw.CreateWindow(width, height, "Lab 2_2 | Shalikiani", nil, nil)
  138.     if err != nil {
  139.         panic(err)
  140.     }
  141.     window.MakeContextCurrent()
  142.  
  143.     if err := gl.Init(); err != nil {
  144.         panic(err)
  145.     }
  146.  
  147.     c := hyperboloid{
  148.         figure: figure{
  149.             positAbsolute: point{0, 0, -50},
  150.             polygonMode:   gl.LINE,
  151.             scale:         1,
  152.             projection:    "Frustum",
  153.             zNear:         10,
  154.             zFar:          120,
  155.         },
  156.         mulPoligon:  1,
  157.         ratioY:      2,
  158.         ratioZ:      3,
  159.         colors: []color{
  160.             {1, 0, 0},
  161.             {1, 0.64, 0},
  162.             {1, 1, 0},
  163.             {0, 1, 0},
  164.             {0, 1, 1},
  165.             {0, 0, 1},
  166.             {1, 0, 1},
  167.         },
  168.     }
  169.     staticelip := hyperboloid{
  170.         figure: figure{
  171.             positAbsolute: point{-0.8, -0.8, 0},
  172.             polygonMode:   gl.LINE,
  173.             scale:         0.15,
  174.             projection:    "Ortho",
  175.             zNear:         -1,
  176.             zFar:          1,
  177.             angleX:        45,
  178.             angleY:        0,
  179.         },
  180.         ratioY:      1,
  181.         ratioZ:      1,
  182.         colors: []color{
  183.             {0, 0, 0},
  184.         },
  185.     }
  186.  
  187.     window.SetKeyCallback(glfw.KeyCallback(func(w *glfw.Window, key glfw.Key, scancode int, action glfw.Action, mods glfw.ModifierKey) {
  188.         if key == glfw.KeyEscape {
  189.             window.SetShouldClose(true)
  190.         } else if action == glfw.Press || action == glfw.Repeat {
  191.             switch key {
  192.             case glfw.KeyP:
  193.                 if c.figure.polygonMode == gl.LINE {
  194.                     c.figure.polygonMode = gl.FILL
  195.                 } else {
  196.                     c.figure.polygonMode = gl.LINE
  197.                 }
  198.             case glfw.KeyA:
  199.                 c.figure.angleY += angleMove
  200.             case glfw.KeyD:
  201.                 c.figure.angleY -= angleMove
  202.             case glfw.KeyS:
  203.                 c.figure.angleX += angleMove
  204.             case glfw.KeyW:
  205.                 c.figure.angleX -= angleMove
  206.             case glfw.KeyQ:
  207.                 c.figure.angleZ += angleMove
  208.             case glfw.KeyE:
  209.                 c.figure.angleZ -= angleMove
  210.             case glfw.KeyUp:
  211.                 c.figure.positAbsolute.y += positionMove
  212.             case glfw.KeyDown:
  213.                 c.figure.positAbsolute.y -= positionMove
  214.             case glfw.KeyRight:
  215.                 c.figure.positAbsolute.x += positionMove
  216.             case glfw.KeyLeft:
  217.                 c.figure.positAbsolute.x -= positionMove
  218.             case glfw.KeyEqual:
  219.                 c.figure.positAbsolute.z += 10 * positionMove
  220.             case glfw.KeyMinus:
  221.                 c.figure.positAbsolute.z -= 10 * positionMove
  222.             case glfw.Key9:
  223.                 if c.mulPoligon > 1 {
  224.                     c.mulPoligon -= 1
  225.                 }
  226.             case glfw.Key0:
  227.                 if c.mulPoligon < 20 {
  228.                     c.mulPoligon += 1
  229.                 }
  230.             default:
  231.                 log.Println(key, action)
  232.             }
  233.         } else if action == glfw.Release {
  234.  
  235.         }
  236.     }))
  237.  
  238.     gl.Enable(gl.DEPTH_TEST)
  239.  
  240.     gl.ClearColor(1, 1, 1, 0.5)
  241.  
  242.     t0 := time.Now()
  243.     for !window.ShouldClose() {
  244.         gl.Clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT)
  245.         gl.LoadIdentity()
  246.  
  247.         staticelip.draw()
  248.         c.draw()
  249.  
  250.         c.figure.angleY += 20 * float64(time.Since(t0).Nanoseconds()) / 1e9
  251.         t0 = time.Now()
  252.  
  253.         window.SwapBuffers()
  254.         glfw.PollEvents()
  255.     }
  256. }
  257.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement