tofu

Making something with OpenGL in Go
Log | Files | Refs

main.go (3193B)


      1 package main
      2 
      3 import (
      4 	"image/color"
      5 	"log"
      6 	"math"
      7 	"path/filepath"
      8 	"runtime"
      9 
     10 	"github.com/go-gl/gl/v3.3-core/gl"
     11 	"github.com/go-gl/glfw/v3.3/glfw"
     12 
     13 	"git.mtkn.jp/tofu"
     14 )
     15 
     16 const (
     17 	winW, winH = 800, 600
     18 )
     19 
     20 func init() {
     21 	runtime.LockOSThread()
     22 }
     23 
     24 func framebufferSizeCallback(w *glfw.Window, width int, height int) {
     25 	if width > math.MaxInt32 || height > math.MaxInt32 {
     26 		log.Fatal("framebufferSizeCallback: integer overflow")
     27 	}
     28 	gl.Viewport(0, 0, int32(width), int32(height))
     29 }
     30 
     31 func processInput(w *glfw.Window) {
     32 	if w.GetKey(glfw.KeyQ) == glfw.Press {
     33 		w.SetShouldClose(true)
     34 	}
     35 	if w.GetKey(glfw.KeyUp) == glfw.Press {
     36 		alpha += 0.01
     37 		if alpha > 1.0 {
     38 			alpha = 1.0
     39 		}
     40 	}
     41 	if w.GetKey(glfw.KeyDown) == glfw.Press {
     42 		alpha -= 0.01
     43 		if alpha < 0 {
     44 			alpha = 0
     45 		}
     46 	}
     47 }
     48 
     49 var object = tofu.Object{
     50 	Vertices: []tofu.Point3D{
     51 		tofu.Point3D{0.5, 0.5, 0.0},
     52 		tofu.Point3D{0.5, -0.5, 0.0},
     53 		tofu.Point3D{-0.5, -0.5, 0.0},
     54 		tofu.Point3D{-0.5, 0.5, 0.0},
     55 	},
     56 	Colors: []color.Color{
     57 		color.RGBA{255, 0, 0, 255},
     58 		color.RGBA{0, 255, 0, 255},
     59 		color.RGBA{0, 0, 255, 255},
     60 		color.RGBA{255, 255, 0, 255},
     61 	},
     62 	TexCoords: []tofu.Point2D{
     63 		tofu.Point2D{1.0, 1.0},
     64 		tofu.Point2D{1.0, 0.0},
     65 		tofu.Point2D{0.0, 0.0},
     66 		tofu.Point2D{0.0, 1.0},
     67 	},
     68 	Faces: [][3]uint32{
     69 		{0, 1, 2},
     70 		{0, 2, 3},
     71 	},
     72 }
     73 
     74 var alpha float32 = 0.2
     75 
     76 func main() {
     77 	err := glfw.Init()
     78 	if err != nil {
     79 		log.Fatalf("init glfw: %v", err)
     80 	}
     81 	defer glfw.Terminate()
     82 	glfw.WindowHint(glfw.ContextVersionMajor, 3)
     83 	glfw.WindowHint(glfw.ContextVersionMinor, 3)
     84 	glfw.WindowHint(glfw.OpenGLProfile, glfw.OpenGLCoreProfile)
     85 
     86 	window, err := glfw.CreateWindow(winW, winH, "Testing", nil, nil)
     87 	if err != nil {
     88 		log.Fatalf("create window: %v", err)
     89 	}
     90 	window.MakeContextCurrent()
     91 	if err = gl.Init(); err != nil {
     92 		log.Fatalf("init gl: %v", err)
     93 	}
     94 	gl.Viewport(0, 0, winW, winH)
     95 
     96 	_, f, _, ok := runtime.Caller(0)
     97 	if !ok {
     98 		log.Fatalf("unable to get source file information")
     99 	}
    100 	vpath := filepath.Join(filepath.Dir(f), "vertex.glsl")
    101 	fpath := filepath.Join(filepath.Dir(f), "fragment.glsl")
    102 	texpath1 := filepath.Join(filepath.Dir(f), "container.jpg")
    103 	texpath2 := filepath.Join(filepath.Dir(f), "awesomeface.png")
    104 
    105 	object.Load()
    106 
    107 	program, err := tofu.NewProgram(vpath, fpath)
    108 	if err != nil {
    109 		log.Fatalf("create shader program: %v", err)
    110 	}
    111 	texture1, err := tofu.NewTexture(texpath1)
    112 	if err != nil {
    113 		log.Fatalf("create texture: %v", err)
    114 	}
    115 	texture2, err := tofu.NewTextureFlip(texpath2, true, false)
    116 	if err != nil {
    117 		log.Fatalf("create texture: %v", err)
    118 	}
    119 	if err := program.SetTexture(texture1, "texture1"); err != nil {
    120 		log.Fatalf("set texture: %v", err)
    121 	}
    122 	if err := program.SetTexture(texture2, "texture2"); err != nil {
    123 		log.Fatalf("set texture: %v", err)
    124 	}
    125 
    126 	window.SetFramebufferSizeCallback(framebufferSizeCallback)
    127 
    128 	transform := tofu.Rotate(math.Pi/4, tofu.Vec3{0, 0, 1})
    129 
    130 	for !window.ShouldClose() {
    131 		processInput(window)
    132 		gl.ClearColor(0.2, 0.3, 0.3, 1.0)
    133 		gl.Clear(gl.COLOR_BUFFER_BIT)
    134 		program.SetFloat32("alpha", alpha)
    135 		program.SetMat4("transform", transform)
    136 		gl.DrawElements(gl.TRIANGLES, 6, gl.UNSIGNED_INT, nil)
    137 		window.SwapBuffers()
    138 		glfw.PollEvents()
    139 	}
    140 }