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

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

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版本。

我认为相关的部分是:

func setup() {
    gl.ClearColor(0.0, 0.0, 0.4, 0.0)

    makeProgram(vertexShaderSource, fragmentShaderSource)

    vertexBufferData := []float32{
        -1,-1,0,
         1,-1,0,
         0, 1,0,
    }

    vertexBuffer = gl.GenBuffer()
    vertexBuffer.Bind(gl.ARRAY_BUFFER)
    gl.BufferData(gl.ARRAY_BUFFER, len(vertexBufferData)*4, vertexBufferData, gl.STATIC_DRAW)
}

func draw() {
    gl.Clear(gl.COLOR_BUFFER_BIT)

    program.Use()
    // 第一个属性缓冲区:顶点
    var vertexAttrib = program.GetAttribLocation("vertexPosition_modelspace")
    vertexAttrib.EnableArray()
    vertexBuffer.Bind(gl.ARRAY_BUFFER)
    var f float32 = 0.0

    vertexAttrib.AttribPointer(
        3,     // 大小
        gl.FLOAT, // 类型
        false, // 是否归一化
        0,     // 步长
        &f) // 数组缓冲区偏移量

    // 绘制三角形
    gl.DrawArrays(gl.TRIANGLES, 0, 3)

    vertexAttrib.DisableArray()
}

完整代码链接: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:

func setup() {
	gl.ClearColor(0.0, 0.0, 0.4, 0.0)

	makeProgram(vertexShaderSource,fragmentShaderSource)

	vertexBufferData := []float32{
		-1,-1,0,
		 1,-1,0,
		 0, 1,0,
	}

	vertexBuffer = gl.GenBuffer()
	vertexBuffer.Bind(gl.ARRAY_BUFFER)
	gl.BufferData(gl.ARRAY_BUFFER, len(vertexBufferData)*4, vertexBufferData, gl.STATIC_DRAW)
}

func draw() {
	gl.Clear(gl.COLOR_BUFFER_BIT)

	program.Use()
    // first attribute buffer: vertices
    var vertexAttrib = program.GetAttribLocation("vertexPosition_modelspace")
    vertexAttrib.EnableArray()
    vertexBuffer.Bind(gl.ARRAY_BUFFER)
    var f float32 = 0.0
    
    vertexAttrib.AttribPointer(
        3,     // size
        gl.FLOAT, // type
        false, // normalized
        0,     // stride
        &f) // array buffer offset

    // draw the triangle
    gl.DrawArrays(gl.TRIANGLES, 0, 3)

    vertexAttrib.DisableArray()
	
}

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

错误出现在:

var f float32 = 0.0

vertexAttrib.AttribPointer(
    3,     // 大小
    gl.FLOAT, // 类型
    false, // 是否归一化
    0,     // 步长
    &f) // 数组缓冲区偏移量

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

正确的代码:

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

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

The error was in:

var f float32 = 0.0

vertexAttrib.AttribPointer(
    3,     // size
    gl.FLOAT, // type
    false, // normalized
    0,     // stride
    &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)

vertexAttrib.AttribPointer(
    3,     // size
    gl.FLOAT, // type
    false, // normalized
    0,     // stride
    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:

确定