dongtu7205 2017-03-17 17:06
浏览 26

vertxattribpointer中的不安全指针

I've been working through some opengl in golang learning and have the following fragments:

import(
    "github.com/go-gl/gl/v3.3-core/gl"
)

vertices := []float32{
        // Position // Colors // Texture Coords
        1.0, 1.0, 0.0, 1.0, 0.0, 0.0, 1.0, 1.0, // Top Right
        1.0, -1.0, 0.0, 0.0, 1.0, 0.0, 1.0, 0.0, // Bottom Right
        -1.0, -1.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, // Bottom Left
        -1.0, 1.0, 0.0, 1.0, 0.0, 0.0, 0.0, 1.0, // Top Left
    }

vlen := int32(len(vertices) * 4 / 4)

var offset uint32 = 0

//position
gl.VertexAttribPointer(0, 3, gl.FLOAT, false, vlen, nil)
gl.EnableVertexAttribArray(0)

//color
offset = offset + 12
gl.VertexAttribPointer(1, 3, gl.FLOAT, false, vlen, unsafe.Pointer(&offset))
gl.EnableVertexAttribArray(1)

offset = 0

So I get my basic square I've been working on -- nil in the 5th position parameter for VertexAttribPointer for position seems to work, but I get nothing with color in the larger program. And if I try to specify an offset unsafe pointer of 0 for poistion I get nothing but empty space.

Specifying an unsafe pointer there is something I'm not understanding at the moment -- I think I need to specify where in vertices the offset for color is, but I'm not following. I am not sure vlen is correct either. Any input appreciated.

EDIT:

Self found solution -- what I needed was gl.PtrOffset, which I wasnt aware of when initially creating the quetion.

gl.VertexAttribPointer(1, 3, gl.FLOAT, false, vlen, gl.PtrOffset(offset))
  • 写回答

1条回答 默认 最新

  • douyi3767 2017-03-17 18:22
    关注

    Your problem comes from VertexAttribPointer(att, size, type, normalized, stride, pointer)

    size is the number of components per generic vertex attribute. Position and color have 3 components, texture coords only 2.
    stride is number of bytes offset between two consecutive values for the attribute you want to read in the shader.

    Your buffer is PPPCCCTT, PPPCCCTT, etc (i.e. position, color, texture coords, position, color, texture coords, etc). Let's put it in bytes (a "float" is 4-bytes sized):
    444 444 44, 444 444 44, etc

    I detail size and stride:
    Position: 3 floats, (3x4 +2x4)=20 bytes, 3 floats, 20 bytes, etc
    Color: 3 floats, (2x4 + 3x4)=20 bytes, 3 floats, 20 bytes, etc
    TextCoo: 2 floats, (3x4 + 3x4)=24 bytes, 2 floats, 24 bytes, etc

    Position attribute: VertexAttribPointer(att1, 3, gl.FLOAT, false, 20, nil)
    Color attribute: VertexAttribPointer(att2, 3, gl.FLOAT, false, 20, nil)
    Texture coords attribute: VertexAttribPointer(att3, 2, gl.FLOAT, false, 24, nil)


    att is the location of the attribute in your shader.

    评论

报告相同问题?

悬赏问题

  • ¥20 数学建模,尽量用matlab回答,论文格式
  • ¥15 昨天挂载了一下u盘,然后拔了
  • ¥30 win from 窗口最大最小化,控件放大缩小,闪烁问题
  • ¥20 易康econgnition精度验证
  • ¥15 msix packaging tool打包问题
  • ¥28 微信小程序开发页面布局没问题,真机调试的时候页面布局就乱了
  • ¥15 python的qt5界面
  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能