为什么这个OpenGL程序不能绘制一个三角形?

huangapple go评论108阅读模式
英文:

Why doesn't this OpenGL program draw a triangle?

问题

我正在尝试学习现代OpenGL,并想要绘制一个如下所示的三角形:为什么这个OpenGL程序不能绘制一个三角形?

我正在按照这个教程进行操作:www.opengl-tutorial.org/beginners-tutorials/tutorial-2-the-first-triangle/,但是我只能得到一个深蓝色的背景(清除颜色)。这段代码可能有什么问题?

我是用Go语言编写的,并尝试在Ubuntu和OS X上运行它。
注意:我使用的是glfw 3库,而不是教程中使用的glfw 2.7版本。

我认为相关的部分是:

  1. func setup() {
  2. gl.ClearColor(0.0, 0.0, 0.4, 0.0)
  3. makeProgram(vertexShaderSource, fragmentShaderSource)
  4. vertexBufferData := []float32{
  5. -1,-1,0,
  6. 1,-1,0,
  7. 0, 1,0,
  8. }
  9. vertexBuffer = gl.GenBuffer()
  10. vertexBuffer.Bind(gl.ARRAY_BUFFER)
  11. gl.BufferData(gl.ARRAY_BUFFER, len(vertexBufferData)*4, vertexBufferData, gl.STATIC_DRAW)
  12. }
  13. func draw() {
  14. gl.Clear(gl.COLOR_BUFFER_BIT)
  15. program.Use()
  16. // 第一个属性缓冲区:顶点
  17. var vertexAttrib = program.GetAttribLocation("vertexPosition_modelspace")
  18. vertexAttrib.EnableArray()
  19. vertexBuffer.Bind(gl.ARRAY_BUFFER)
  20. var f float32 = 0.0
  21. vertexAttrib.AttribPointer(
  22. 3, // 大小
  23. gl.FLOAT, // 类型
  24. false, // 是否归一化
  25. 0, // 步长
  26. &f) // 数组缓冲区偏移量
  27. // 绘制三角形
  28. gl.DrawArrays(gl.TRIANGLES, 0, 3)
  29. vertexAttrib.DisableArray()
  30. }

完整代码链接:https://gist.github.com/mbertschler/8672365

我已经尝试使用OSX上的OpenGL Profiler进行调试,但是目前没有显示任何错误。

英文:

I am trying to learn modern OpenGL and want to draw a triangle like this: 为什么这个OpenGL程序不能绘制一个三角形?

I am following this tutorial: www.opengl-tutorial.org/beginners-tutorials/tutorial-2-the-first-triangle/, but all I get is the dark blue background (clear color). What could be wrong with this code?

I am writing this in Go and tried to run it on Ubuntu and OS X.
Note: I am using the glfw 3 library instead of glfw 2.7 that is used in the tutorial.

I think the relevant parts are:

  1. func setup() {
  2. gl.ClearColor(0.0, 0.0, 0.4, 0.0)
  3. makeProgram(vertexShaderSource,fragmentShaderSource)
  4. vertexBufferData := []float32{
  5. -1,-1,0,
  6. 1,-1,0,
  7. 0, 1,0,
  8. }
  9. vertexBuffer = gl.GenBuffer()
  10. vertexBuffer.Bind(gl.ARRAY_BUFFER)
  11. gl.BufferData(gl.ARRAY_BUFFER, len(vertexBufferData)*4, vertexBufferData, gl.STATIC_DRAW)
  12. }
  13. func draw() {
  14. gl.Clear(gl.COLOR_BUFFER_BIT)
  15. program.Use()
  16. // first attribute buffer: vertices
  17. var vertexAttrib = program.GetAttribLocation("vertexPosition_modelspace")
  18. vertexAttrib.EnableArray()
  19. vertexBuffer.Bind(gl.ARRAY_BUFFER)
  20. var f float32 = 0.0
  21. vertexAttrib.AttribPointer(
  22. 3, // size
  23. gl.FLOAT, // type
  24. false, // normalized
  25. 0, // stride
  26. &f) // array buffer offset
  27. // draw the triangle
  28. gl.DrawArrays(gl.TRIANGLES, 0, 3)
  29. vertexAttrib.DisableArray()
  30. }

Link to the full code: https://gist.github.com/mbertschler/8672365

I already tried to debug this with OpenGL Profiler on OSX, but it shows me no errors so far.

答案1

得分: 0

错误出现在:

  1. var f float32 = 0.0
  2. vertexAttrib.AttribPointer(
  3. 3, // 大小
  4. gl.FLOAT, // 类型
  5. false, // 是否归一化
  6. 0, // 步长
  7. &f) // 数组缓冲区偏移量

尽管这段代码在这个问题的原始帖子中似乎有效:
https://stackoverflow.com/questions/11284055/opengl-vertex-buffer-doesnt-draw-anything-in-golang 但在我的程序中却不行。被接受的答案给出了正确的调用版本。它使用nil作为数组缓冲区偏移量。

正确的代码:

(至少对于github.com/go-gl/gl绑定)

  1. vertexAttrib.AttribPointer(
  2. 3, // 大小
  3. gl.FLOAT, // 类型
  4. false, // 是否归一化
  5. 0, // 步长
  6. nil) // 数组缓冲区偏移量
英文:

The error was in:

  1. var f float32 = 0.0
  2. vertexAttrib.AttribPointer(
  3. 3, // size
  4. gl.FLOAT, // type
  5. false, // normalized
  6. 0, // stride
  7. &f) // array buffer offset

Although it seemed to work for the OP of this question:
https://stackoverflow.com/questions/11284055/opengl-vertex-buffer-doesnt-draw-anything-in-golang it doesn't with my program. The accepted answer had the right version of this call. It works with nil as array buffer offset.

Working Code:

(At least for github.com/go-gl/gl bindings)

  1. vertexAttrib.AttribPointer(
  2. 3, // size
  3. gl.FLOAT, // type
  4. false, // normalized
  5. 0, // stride
  6. nil) // array buffer offset

huangapple
  • 本文由 发表于 2014年1月29日 01:41:55
  • 转载请务必保留本文链接:https://go.coder-hub.com/21412844.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定