gpt4 book ai didi

c++ - 像 glColorPointer 这样的 OpenGL API 如何知道作为指针传递的数组的大小?

转载 作者:太空狗 更新时间:2023-10-29 23:31:06 25 4
gpt4 key购买 nike

这是来自 opengl.org 的 glColorPointer 的定义:

void glColorPointer(GLint size,GLenum type, GLsizei stride, const GLvoid * pointer); Parameters

size Specifies the number of components per color. Must be 3 or 4. The initial value is 4.

type Specifies the data type of each color component in the array. Symbolic constants GL_BYTE, GL_UNSIGNED_BYTE, GL_SHORT, GL_UNSIGNED_SHORT, GL_INT, GL_UNSIGNED_INT, GL_FLOAT, and GL_DOUBLE are accepted. The initial value is GL_FLOAT.

stride Specifies the byte offset between consecutive colors. If stride is 0, the colors are understood to be tightly packed in the array. The initial value is 0.

pointer Specifies a pointer to the first component of the first color element in the array. The initial value is 0.

简单来说,我们可以说sizetype参数指定一个颜色单元的字节数,stride指定字节数连续颜色单元之间的偏移量。

但是,这个函数如何知道总单元数?众所周知,如果将数组作为函数的指针传递,则无法在函数内部使用 sizeof() 来获取数组的大小。那么当指针参数只是一个 GLvoid* 指针时,glColorPointer 如何知道数组的大小呢?

最佳答案

因为函数本身不访问数组。它只是将给定的参数保存在 OpenGL 内部状态机中。只有在调用 glDrawArraysglDrawElements 时才会访问数组中的数据。只有这样,每个绘制顶点的颜色才会从您在 glColorPointer 中指定的位置读取。

另一方面,

glDrawArrays 确实包含指定顶点数的 count 参数。不用说,颜色数组中必须有足够的数据来提供绘制的每个顶点。

这同样适用于任何其他 gl*Pointer 函数,如 glNormalPointerglTexCoordPointer

关于c++ - 像 glColorPointer 这样的 OpenGL API 如何知道作为指针传递的数组的大小?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9382720/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com