dqrb4228 2013-01-10 06:04
浏览 70
已采纳

VBO状态调用的结构

I'm converting my OpenGL 2 program to OpenGL 3, one small step at a time. The current step is to get rid of immediate mode in favor of VBOs. I'm stumbling on the proper placement of all the VBO state management calls.

Let's assume I'm going to have a bunch of objects, each with its own VBOs for vertex and element data. Each object has a load function that sets up its VBOs and loads data into them; and it has a draw function that binds its VBOs and issues the appropriate draw command. To keep the objects from interfering with each other, my main loop should call PushClientAttrib(CLIENT_VERTEX_ARRAY_BIT) before either draw or load is called, correct? (And of course PopClientAttrib() after.)

When I push/pop that state to protect my load function, the object doesn't get drawn. It seems like there's some state I need to (re)set in my draw function, but I can't figure out what it could be.

Here's code to add a new object e to the scene, letting it call its load function:


    gl.PushClientAttrib(gl.CLIENT_VERTEX_ARRAY_BIT)
    e.SceneAdded()
    gl.PopClientAttrib()

And here's how I call each object's draw function:


    gl.PushClientAttrib(gl.CLIENT_VERTEX_ARRAY_BIT)
    gl.PushMatrix()
    p.Paint()
    gl.PopMatrix()
    gl.PopClientAttrib()

Here's the object's load function:


    // Make the vertex & texture data known to GL.
    gl.GenBuffers(1, &vboId)
    gl.BindBuffer(gl.ARRAY_BUFFER, vboId)
    gl.BufferData(gl.ARRAY_BUFFER,
        gl.Sizeiptr(unsafe.Sizeof(gldata[0])*uintptr(len(gldata))),
        gl.Pointer(&gldata[0].x), gl.STATIC_DRAW)
    gl.VertexPointer(3, gl.DOUBLE, gl.Sizei(unsafe.Sizeof(gldata[0])),
        gl.Pointer(unsafe.Offsetof(gldata[0].x)))
    gl.TexCoordPointer(2, gl.DOUBLE, gl.Sizei(unsafe.Sizeof(gldata[0])),
        gl.Pointer(unsafe.Offsetof(gldata[0].s)))

    // Make the index data known to GL.
    gl.GenBuffers(1, &iboId)
    gl.BindBuffer(gl.ELEMENT_ARRAY_BUFFER, iboId)
    gl.BufferData(gl.ELEMENT_ARRAY_BUFFER,
        gl.Sizeiptr(unsafe.Sizeof(sd.indices[0])*uintptr(len(sd.indices))),
        gl.Pointer(&sd.indices[0]), gl.STATIC_DRAW)

Finally, here's the object's draw function:


func draw() {
    gl.BindBuffer(gl.ARRAY_BUFFER, vboId)
    gl.BindBuffer(gl.ELEMENT_ARRAY_BUFFER, iboId)
    gl.EnableClientState(gl.VERTEX_ARRAY)
    gl.EnableClientState(gl.TEXTURE_COORD_ARRAY)

    gl.DrawElements(gl.TRIANGLES, indexcount, gl.UNSIGNED_SHORT, nil)
}

Just to be clear, if I move the two EnableClientState calls from draw to load, and if I don't protect load with PushClientState, everything works fine.

  • 写回答

2条回答 默认 最新

  • dongyaobo9081 2013-01-16 05:07
    关注

    The answer to the question I actually asked is that gl.VertexPointer() and friends need to be called from the object's draw function. My main loop is pushing and popping all state related to client vertices, so of course that gets lost.

    The question I implied is, "How can I convert my old, GL2 program to new, GL3 code?" That's answered very elegantly here.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥20 易康econgnition精度验证
  • ¥15 msix packaging tool打包问题
  • ¥28 微信小程序开发页面布局没问题,真机调试的时候页面布局就乱了
  • ¥15 python的qt5界面
  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败