Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package main
- import (
- "github.com/go-gl/gl/v2.1/gl"
- "github.com/go-gl/glfw/v3.2/glfw"
- "log"
- "math"
- "runtime"
- "time"
- )
- const (
- width = 1000
- height = 1000
- angleMove = 5
- positionMove = 0.1
- )
- type point struct {
- x float64
- y float64
- z float64
- }
- func (point *point) glVertex3d() {
- gl.Vertex3d(point.x, point.y, point.z)
- }
- type color struct {
- r float64
- g float64
- b float64
- }
- func (color *color) glColor3d() {
- gl.Color3d(color.r, color.g, color.b)
- }
- type figure struct {
- positAbsolute point
- angleX float64
- angleY float64
- angleZ float64
- polygonMode uint32
- scale float64
- projection string
- zNear float64
- zFar float64
- }
- func (figure *figure) getProjectionFunc() func(left, right, bottom, top, zNear, zFar float64) {
- switch figure.projection {
- case "Ortho":
- return gl.Ortho
- case "Frustum":
- return gl.Frustum
- }
- return nil
- }
- func (figure *figure) glProjection() {
- if projection := figure.getProjectionFunc(); projection != nil {
- projection(-1, 1, -1, 1, figure.zNear, figure.zFar)
- }
- }
- func (figure *figure) prepareToDraw() {
- figure.glProjection()
- gl.PolygonMode(gl.FRONT_AND_BACK, figure.polygonMode)
- gl.Translated(figure.positAbsolute.x, figure.positAbsolute.y, figure.positAbsolute.z)
- gl.Scaled(figure.scale, figure.scale, figure.scale)
- gl.Rotated(figure.angleX, 1, 0, 0)
- gl.Rotated(figure.angleY, 0, 1, 0)
- gl.Rotated(figure.angleZ, 0, 0, 1)
- }
- type hyperboloid struct {
- figure figure
- colors []color
- ratioY float64
- ratioZ float64
- mulPoligon int
- }
- func (hyperboloid *hyperboloid) getPoint(h, w int) *point {
- teta := math.Pi * float64(h) / float64(hyperboloid.mulPoligon*len(hyperboloid.colors))
- phi := 2 * math.Pi * float64(w) / float64(2*hyperboloid.mulPoligon*len(hyperboloid.colors))
- return &point{
- x: math.Sin(teta) * math.Cos(phi),
- y: hyperboloid.ratioY * math.Sin(teta) * math.Sin(phi),
- z: hyperboloid.ratioZ * math.Cos(teta) ,
- }
- }
- func (hyperboloid *hyperboloid) draw() {
- gl.PushMatrix()
- defer gl.PopMatrix()
- hyperboloid.figure.prepareToDraw()
- gl.Begin(gl.TRIANGLE_FAN)
- hyperboloid.colors[0].glColor3d()
- hyperboloid.getPoint(0, 0).glVertex3d()
- for i := 0; i <= 2*hyperboloid.mulPoligon*len(hyperboloid.colors); i++ {
- hyperboloid.getPoint(1, i).glVertex3d()
- }
- gl.End()
- for j := 1; j < hyperboloid.mulPoligon*len(hyperboloid.colors)-1; j++ {
- gl.Begin(gl.QUAD_STRIP)
- hyperboloid.colors[j/hyperboloid.mulPoligon].glColor3d()
- for i := 0; i <= 2*hyperboloid.mulPoligon*len(hyperboloid.colors); i++ {
- hyperboloid.getPoint(j, i).glVertex3d()
- hyperboloid.getPoint(j+1, i).glVertex3d()
- }
- gl.End()
- }
- gl.Begin(gl.TRIANGLE_FAN)
- hyperboloid.colors[len(hyperboloid.colors)-1].glColor3d()
- hyperboloid.getPoint(hyperboloid.mulPoligon*len(hyperboloid.colors), 0).glVertex3d()
- for i := 0; i <= 2*hyperboloid.mulPoligon*len(hyperboloid.colors); i++ {
- hyperboloid.getPoint(hyperboloid.mulPoligon*len(hyperboloid.colors)-1, i).glVertex3d()
- }
- gl.End()
- }
- func main() {
- runtime.LockOSThread()
- if err := glfw.Init(); err != nil {
- panic(err)
- }
- defer glfw.Terminate()
- glfw.WindowHint(glfw.Resizable, glfw.False)
- window, err := glfw.CreateWindow(width, height, "Lab 2_2 | Shalikiani", nil, nil)
- if err != nil {
- panic(err)
- }
- window.MakeContextCurrent()
- if err := gl.Init(); err != nil {
- panic(err)
- }
- c := hyperboloid{
- figure: figure{
- positAbsolute: point{0, 0, -50},
- polygonMode: gl.LINE,
- scale: 1,
- projection: "Frustum",
- zNear: 10,
- zFar: 120,
- },
- mulPoligon: 1,
- ratioY: 2,
- ratioZ: 3,
- colors: []color{
- {1, 0, 0},
- {1, 0.64, 0},
- {1, 1, 0},
- {0, 1, 0},
- {0, 1, 1},
- {0, 0, 1},
- {1, 0, 1},
- },
- }
- staticelip := hyperboloid{
- figure: figure{
- positAbsolute: point{-0.8, -0.8, 0},
- polygonMode: gl.LINE,
- scale: 0.15,
- projection: "Ortho",
- zNear: -1,
- zFar: 1,
- angleX: 45,
- angleY: 0,
- },
- ratioY: 1,
- ratioZ: 1,
- colors: []color{
- {0, 0, 0},
- },
- }
- window.SetKeyCallback(glfw.KeyCallback(func(w *glfw.Window, key glfw.Key, scancode int, action glfw.Action, mods glfw.ModifierKey) {
- if key == glfw.KeyEscape {
- window.SetShouldClose(true)
- } else if action == glfw.Press || action == glfw.Repeat {
- switch key {
- case glfw.KeyP:
- if c.figure.polygonMode == gl.LINE {
- c.figure.polygonMode = gl.FILL
- } else {
- c.figure.polygonMode = gl.LINE
- }
- case glfw.KeyA:
- c.figure.angleY += angleMove
- case glfw.KeyD:
- c.figure.angleY -= angleMove
- case glfw.KeyS:
- c.figure.angleX += angleMove
- case glfw.KeyW:
- c.figure.angleX -= angleMove
- case glfw.KeyQ:
- c.figure.angleZ += angleMove
- case glfw.KeyE:
- c.figure.angleZ -= angleMove
- case glfw.KeyUp:
- c.figure.positAbsolute.y += positionMove
- case glfw.KeyDown:
- c.figure.positAbsolute.y -= positionMove
- case glfw.KeyRight:
- c.figure.positAbsolute.x += positionMove
- case glfw.KeyLeft:
- c.figure.positAbsolute.x -= positionMove
- case glfw.KeyEqual:
- c.figure.positAbsolute.z += 10 * positionMove
- case glfw.KeyMinus:
- c.figure.positAbsolute.z -= 10 * positionMove
- case glfw.Key9:
- if c.mulPoligon > 1 {
- c.mulPoligon -= 1
- }
- case glfw.Key0:
- if c.mulPoligon < 20 {
- c.mulPoligon += 1
- }
- default:
- log.Println(key, action)
- }
- } else if action == glfw.Release {
- }
- }))
- gl.Enable(gl.DEPTH_TEST)
- gl.ClearColor(1, 1, 1, 0.5)
- t0 := time.Now()
- for !window.ShouldClose() {
- gl.Clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT)
- gl.LoadIdentity()
- staticelip.draw()
- c.draw()
- c.figure.angleY += 20 * float64(time.Since(t0).Nanoseconds()) / 1e9
- t0 = time.Now()
- window.SwapBuffers()
- glfw.PollEvents()
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement