int to void *-避免使用C样式转换?

Mat*_*reu 4 c++ opengl pointers casting void-pointers

我需要将int(指定一个字节偏移量)强制转换为const void*。真正适合我的唯一解决方案是c样式强制转换:

int offset = 6*sizeof(GLfloat);
glVertexAttribPointer(1,3,GL_FLOAT,GL_FALSE,0,(void*)offset);
Run Code Online (Sandbox Code Playgroud)

我想摆脱c风格的转换,但找不到有效的解决方案。我试过了

static_cast<void*>(&offset)
Run Code Online (Sandbox Code Playgroud)

并进行编译,但这不是正确的解决方案(此方法的整体输出有所不同)。正确的解决方案是什么?

链接至的文档glVertexAttribPointer链接

Ulr*_*rdt 5

考虑到这是一个绝望的情况(请参阅derhass提供的链接),可能最好的方法是将可疑代码集中在一个地方,在其上打上适当的烙印,并至少保持其余代码干净:

/**
 * Overload/wrapper around OpenGL's glVertexAttribIPointer, which avoids
 * code smell due to shady pointer arithmetic at the place of call.
 *
 * See also:
 * https://www.opengl.org/registry/specs/ARB/vertex_buffer_object.txt
 * https://www.opengl.org/sdk/docs/man/html/glVertexAttribPointer.xhtml
 */
void glVertexAttribPointer(GLuint index, GLint size, GLenum type, GLsizei stride, size_t offset)
{
    GLvoid const* pointer = static_cast<char const*>(0) + offset;
    return glVertexAttribPointer(index, size, type, stride, offset);
}
Run Code Online (Sandbox Code Playgroud)