ここ↓で発表したやつです:)
https://go-beginners.connpass.com/event/47481/
急ぎで作ったので、超雑ですが、ご勘弁を。。。
自己紹介
- @afroscriptといいます。
- teratailのDevRelやってます
- 仕事としてプログラムを書いたことはないです
- Go入門ハンズオンとGo入門者LT大会の運営してます
- 運営にもかかわらず、合宿に4時間くらい遅刻したことあります
- おかげで、僕の席は後ろの端っこのほうで、しかも1人席でした
- GO暦
- 2015年10月にハンズオン
- 11月に入門LT大会で発表(←このときも粉砕されてます)
- 1年半以上触ってなかったです、すみません
今回はここを元に進めてみました。
まずは必要なパッケージのインストール
$ go get github.com/go-gl/gl/v3.3-core/gl
$ go get github.com/go-gl/glfw/v3.1/glfw
$ go get strings
go-glは、GoでOpenGLを使うためのpackage。
詳しくは元ネタを見てください。
go_3d/src/main
にmain.goを作成。
まずはウィンドウを立ち上げてみます
方法
元ネタからコピー&ペースト\(^o^)/
("strings"だけまだ使わないので、スコープアウトしました。)
main.go
package main
import (
"fmt"
"runtime"
// "strings"
"github.com/go-gl/gl/v3.3-core/gl"
"github.com/go-gl/glfw/v3.1/glfw"
)
const windowWidth = 800
const windowHeight = 600
func init() {
runtime.LockOSThread()
}
func main() {
//init glfw
if err := glfw.Init(); err != nil {
panic(err)
}
defer glfw.Terminate()
glfw.WindowHint(glfw.Resizable, glfw.False)
glfw.WindowHint(glfw.ContextVersionMajor, 4)
glfw.WindowHint(glfw.ContextVersionMinor, 1)
glfw.WindowHint(glfw.OpenGLProfile, glfw.OpenGLCoreProfile)
glfw.WindowHint(glfw.OpenGLForwardCompatible, glfw.True)
// make an application window
window, err := glfw.CreateWindow(windowWidth, windowHeight, "Hello", nil, nil)
if err != nil {
panic(err)
}
window.MakeContextCurrent()
// init gl
if err := gl.Init(); err != nil {
panic(err)
}
fmt.Println("OpenGL version", gl.GoStr(gl.GetString(gl.VERSION)))
gl.ClearColor(1.0, 1.0, 1.0, 1.0)
for !window.ShouldClose() {
gl.Clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT)
window.SwapBuffers()
glfw.PollEvents()
}
}
コードを追ってみて分かったこと
ここでWindowをCreateしてるんだ〜:)
window, err := glfw.CreateWindow(windowWidth, windowHeight, "Hello", nil, nil)
ここでデフォルトのWindowの色決めてるんだ〜:)
gl.ClearColor(1.0, 1.0, 1.0, 1.0)
あとよく分かんない。/(^o^)\
図形を描画してみよう
方法
元ネタからコピー&ペースト\(^o^)/
実行してみます
図形ができた!!\(^o^)/
コードを追ってみて分かったこと
↓ここで頂点を決めてる。
points := []float32{
-0.5, -0.5,
0.5, 0.5,
0.5, -0.5,
-0.5, 0.5,
}
↓ここで描画する座標の順番決めてる。
vertices := []uint32{
0, 2, 1, 3,
}
あとは、OpenGL読めないと分からない\(^o^)/
まとめ
- Goで3Dはgo-glからOpenGLを扱うことで実現可能
- ほぼほぼ書いてる内容はOpenGLと同じ感じ(推定)
- だからOpenGL勉強してないと無理だと悟った
- ありがとう、コピー&ペースト