确保C ++中的OpenGL兼容类型 [英] Ensuring OpenGL compatible types in c++

查看:172
本文介绍了确保C ++中的OpenGL兼容类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

OpenGL缓冲区对象支持定义良好宽度的各种数据类型(GL_FLOAT是32位,GL_HALF_FLOAT是16位,GL_INT是32位...)

OpenGL buffer objects support various data types of well defined width (GL_FLOAT is 32 bit, GL_HALF_FLOAT is 16 bit, GL_INT is 32 bit ...)

如何确保OpenGL的跨平台和面向未来的类型? 例如,将float数据从c ++数组馈送到缓冲区对象并说其类型为GL_FLOATfloat不是32位的平台上将不起作用.

How would one go about ensuring cross platform and futureproof types for OpenGL? For example, feeding float data from a c++ array to to a buffer object and saying its type is GL_FLOAT will not work on platforms where float isn't 32 bit.

推荐答案

在对此进行一些研究时,我注意到GL规范中这些类型的定义方式发生了细微但有趣的变化.更改发生在OpenGL 4.1和4.2之间.

While doing some research on this, I noticed a subtle but interesting change in how these types are defined in the GL specs. The change happened between OpenGL 4.1 and 4.2.

在OpenGL 4.1之前,列出数据类型的表(最新规范文档中的表2.2)在size列的标题为 Minimum Bit Width (最小位宽),并且表标题显示(强调由我):

Up to OpenGL 4.1, the table that lists the data types (Table 2.2 in the recent spec documents) has the header Minimum Bit Width for the size column, and the table caption says (emphasis added by me):

GL类型不是C类型.因此,例如,GL类型int在本文档之外称为GLint,不一定与C类型int等效.与表中指示的数字相比,实现可能会使用更多的位来表示GL类型.但是,不需要正确解释最小范围之外的整数值.

GL types are not C types. Thus, for example, GL type int is referred to as GLint outside this document, and is not necessarily equivalent to the C type int. An implementation may use more bits than the number indicated in the table to represent a GL type. Correct interpretation of integer values outside the minimum range is not required, however.

从OpenGL 4.2规范开始,表格标题更改为位宽,表格标题更改为:

Starting with the OpenGL 4.2 spec, the table header changes to Bit Width, and the table caption to:

GL类型不是C类型.因此,例如,GL类型int在本文档之外称为GLint,不一定与C类型int等效.实现必须完全使用表中指示的位数来表示GL类型.

GL types are not C types. Thus, for example, GL type int is referred to as GLint outside this document, and is not necessarily equivalent to the C type int. An implementation must use exactly the number of bits indicated in the table to represent a GL type.

这影响了问题的答案.如果使用最新的定义,则可以在代码中使用标准大小的类型定义而不是GL类型,并安全地假定它们匹配.例如,您可以使用<cstdint>中的int32_t代替GLint.

This influenced the answer to the question. If you go with the latest definition, you can use standard sized type definitions instead of the GL types in your code, and safely assume that they match. For example, you can use int32_t from <cstdint> instead of GLint.

使用GL类型仍然是最直接的解决方案.但是,根据您的代码体系结构和首选项,这可能是不可取的.如果您希望将软件划分为多个组件,并且希望在提供特定级别的抽象的同时将OpenGL渲染隔离在一个组件中,则可能不想在代码中使用GL类型.但是,一旦数据到达渲染代码,就必须匹配相应的GL类型.

Using the GL types is still the most straightforward solution. Depending on your code architecture and preferences, it might be undesirable, though. If you like to divide your software into components, and want to have OpenGL rendering isolated in a single component while providing a certain level of abstraction, you probably don't want to use GL types all over your code. Yet, once the data reaches the rendering code, it has to match the corresponding GL types.

作为一个典型的例子,假设您有一些计算代码可以生成要渲染的数据.您可能不希望整个计算代码都具有GLfloat类型,因为它可以独立于OpenGL使用.但是,一旦准备好显示计算结果,并想将数据放入用于OpenGL渲染的VBO中,则类型必须与GLfloat相同.

As a typical example, say you have computational code that produces data you want to render. You may not want to have GLfloat types all over your computational code, because it can be used independent of OpenGL. Yet, once you're ready to display the result of the computation, and want to drop the data into a VBO for OpenGL rendering, the type has to be the same as GLfloat.

您可以使用多种方法.我上面提到的一种方法是在非渲染代码中使用标准C ++头文件中的大小类型.同样,您可以定义自己的与OpenGL使用的类型匹配的typedef.或者,出于性能方面的考虑而不太理想,您可以在必要时转换数据,可能基于比较传入类型和GL类型之间的sizeof()值.

There are various approaches you can use. One is what I mentioned above, using sized types from standard C++ header files in your non-rendering code. Similarly, you can define your own typedefs that match the types used by OpenGL. Or, less desirable for performance reasons, you can convert the data where necessary, possibly based on comparing the sizeof() values between the incoming types and the GL types.

这篇关于确保C ++中的OpenGL兼容类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆