iovxw

开始折腾OpenGL

生命在于折腾啊。生命不息,折腾不止。

嗯,准备写一个类似Mineccraft的游戏

基本设定已经弄好了

不过目测是个大坑

希望到时候别弃坑吧

求有经验的人帮忙啊……

下面是go-gl的example源码

顺便测试一下代码高亮

  1. package main
  2. import (
  3. "errors"
  4. "fmt"
  5. "github.com/go-gl/gl"
  6. glfw "github.com/go-gl/glfw3"
  7. "image"
  8. "image/png"
  9. "io"
  10. "os"
  11. )
  12. const (
  13. Title = "Spinning Gopher"
  14. Width = 640
  15. Height = 480
  16. )
  17. var (
  18. texture gl.Texture
  19. rotx, roty float32
  20. ambient []float32 = []float32{0.5, 0.5, 0.5, 1}
  21. diffuse []float32 = []float32{1, 1, 1, 1}
  22. lightpos []float32 = []float32{-5, 5, 10, 0}
  23. )
  24. func errorCallback(err glfw.ErrorCode, desc string) {
  25. fmt.Printf("%v: %v\n", err, desc)
  26. }
  27. func main() {
  28. glfw.SetErrorCallback(errorCallback)
  29. if !glfw.Init() {
  30. panic("Can't init glfw!")
  31. }
  32. defer glfw.Terminate()
  33. window, err := glfw.CreateWindow(Width, Height, Title, nil, nil)
  34. if err != nil {
  35. panic(err)
  36. }
  37. window.MakeContextCurrent()
  38. glfw.SwapInterval(1)
  39. gl.Init()
  40. if err := initScene(); err != nil {
  41. fmt.Fprintf(os.Stderr, "init: %s\n", err)
  42. return
  43. }
  44. defer destroyScene()
  45. for !window.ShouldClose() {
  46. drawScene()
  47. window.SwapBuffers()
  48. glfw.PollEvents()
  49. }
  50. }
  51. func createTexture(r io.Reader) (gl.Texture, error) {
  52. img, err := png.Decode(r)
  53. if err != nil {
  54. return gl.Texture(0), err
  55. }
  56. rgbaImg, ok := img.(*image.NRGBA)
  57. if !ok {
  58. return gl.Texture(0), errors.New("texture must be an NRGBA image")
  59. }
  60. textureId := gl.GenTexture()
  61. textureId.Bind(gl.TEXTURE_2D)
  62. gl.TexParameterf(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.LINEAR)
  63. gl.TexParameterf(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.LINEAR)
  64. // flip image: first pixel is lower left corner
  65. imgWidth, imgHeight := img.Bounds().Dx(), img.Bounds().Dy()
  66. data := make([]byte, imgWidth*imgHeight*4)
  67. lineLen := imgWidth * 4
  68. dest := len(data) - lineLen
  69. for src := 0; src < len(rgbaImg.Pix); src += rgbaImg.Stride {
  70. copy(data[dest:dest+lineLen], rgbaImg.Pix[src:src+rgbaImg.Stride])
  71. dest -= lineLen
  72. }
  73. gl.TexImage2D(gl.TEXTURE_2D, 0, 4, imgWidth, imgHeight, 0, gl.RGBA, gl.UNSIGNED_BYTE, data)
  74. return textureId, nil
  75. }
  76. func initScene() (err error) {
  77. gl.Enable(gl.TEXTURE_2D)
  78. gl.Enable(gl.DEPTH_TEST)
  79. gl.Enable(gl.LIGHTING)
  80. gl.ClearColor(0.5, 0.5, 0.5, 0.0)
  81. gl.ClearDepth(1)
  82. gl.DepthFunc(gl.LEQUAL)
  83. gl.Lightfv(gl.LIGHT0, gl.AMBIENT, ambient)
  84. gl.Lightfv(gl.LIGHT0, gl.DIFFUSE, diffuse)
  85. gl.Lightfv(gl.LIGHT0, gl.POSITION, lightpos)
  86. gl.Enable(gl.LIGHT0)
  87. gl.Viewport(0, 0, Width, Height)
  88. gl.MatrixMode(gl.PROJECTION)
  89. gl.LoadIdentity()
  90. gl.Frustum(-1, 1, -1, 1, 1.0, 10.0)
  91. gl.MatrixMode(gl.MODELVIEW)
  92. gl.LoadIdentity()
  93. goph, err := os.Open("gopher.png")
  94. if err != nil {
  95. panic(err)
  96. }
  97. defer goph.Close()
  98. texture, err = createTexture(goph)
  99. return
  100. }
  101. func destroyScene() {
  102. texture.Delete()
  103. }
  104. func drawScene() {
  105. gl.Clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT)
  106. gl.MatrixMode(gl.MODELVIEW)
  107. gl.LoadIdentity()
  108. gl.Translatef(0, 0, -3.0)
  109. gl.Rotatef(rotx, 1, 0, 0)
  110. gl.Rotatef(roty, 0, 1, 0)
  111. rotx += 0.5
  112. roty += 0.1
  113. texture.Bind(gl.TEXTURE_2D)
  114. gl.Color4f(1, 1, 1, 1)
  115. gl.Begin(gl.QUADS)
  116. gl.Normal3f(0, 0, 1)
  117. gl.TexCoord2f(0, 0)
  118. gl.Vertex3f(-1, -1, 1)
  119. gl.TexCoord2f(1, 0)
  120. gl.Vertex3f(1, -1, 1)
  121. gl.TexCoord2f(1, 1)
  122. gl.Vertex3f(1, 1, 1)
  123. gl.TexCoord2f(0, 1)
  124. gl.Vertex3f(-1, 1, 1)
  125. gl.Normal3f(0, 0, -1)
  126. gl.TexCoord2f(1, 0)
  127. gl.Vertex3f(-1, -1, -1)
  128. gl.TexCoord2f(1, 1)
  129. gl.Vertex3f(-1, 1, -1)
  130. gl.TexCoord2f(0, 1)
  131. gl.Vertex3f(1, 1, -1)
  132. gl.TexCoord2f(0, 0)
  133. gl.Vertex3f(1, -1, -1)
  134. gl.Normal3f(0, 1, 0)
  135. gl.TexCoord2f(0, 1)
  136. gl.Vertex3f(-1, 1, -1)
  137. gl.TexCoord2f(0, 0)
  138. gl.Vertex3f(-1, 1, 1)
  139. gl.TexCoord2f(1, 0)
  140. gl.Vertex3f(1, 1, 1)
  141. gl.TexCoord2f(1, 1)
  142. gl.Vertex3f(1, 1, -1)
  143. gl.Normal3f(0, -1, 0)
  144. gl.TexCoord2f(1, 1)
  145. gl.Vertex3f(-1, -1, -1)
  146. gl.TexCoord2f(0, 1)
  147. gl.Vertex3f(1, -1, -1)
  148. gl.TexCoord2f(0, 0)
  149. gl.Vertex3f(1, -1, 1)
  150. gl.TexCoord2f(1, 0)
  151. gl.Vertex3f(-1, -1, 1)
  152. gl.Normal3f(1, 0, 0)
  153. gl.TexCoord2f(1, 0)
  154. gl.Vertex3f(1, -1, -1)
  155. gl.TexCoord2f(1, 1)
  156. gl.Vertex3f(1, 1, -1)
  157. gl.TexCoord2f(0, 1)
  158. gl.Vertex3f(1, 1, 1)
  159. gl.TexCoord2f(0, 0)
  160. gl.Vertex3f(1, -1, 1)
  161. gl.Normal3f(-1, 0, 0)
  162. gl.TexCoord2f(0, 0)
  163. gl.Vertex3f(-1, -1, -1)
  164. gl.TexCoord2f(1, 0)
  165. gl.Vertex3f(-1, -1, 1)
  166. gl.TexCoord2f(1, 1)
  167. gl.Vertex3f(-1, 1, 1)
  168. gl.TexCoord2f(0, 1)
  169. gl.Vertex3f(-1, 1, -1)
  170. gl.End()
  171. }