【发布时间】:2018-06-10 23:37:43
【问题描述】:
我有一个整数列表,通过 C++ 代码获得,代表不同的 D3DCOLORVALUE 环境等...
在 Delphi 中,从这个数字获得相同颜色的正确方法是什么?
例如,使用 C++ (Visual Studio 2012) 中的程序,通过调用 C++ 中的库例程(单独的 dll 文件),我可以获得以下值:
-1.44852785e-016 = 2770796799 (represent the color)
1.57844226e+11 = 1376977151 (represent the color)
-1.98938735e+034 = 4168431103 (represent the color)
3.39617733e+038 = 4294967295 (represent the color)
@Rudy Velthuis,原代码为:(数组中包含的一些值如上所示)
int32_t index = ifcObject->indicesForFaces[0];
uint32_t ambient = ((int32_t*) vertices)[index * (vertexElementSize / sizeof(float)) + 6],
diffuse = ((int32_t*) vertices)[index * (vertexElementSize / sizeof(float)) + 7],
emissive = ((int32_t*) vertices)[index * (vertexElementSize / sizeof(float)) + 8],
specular = ((int32_t*) vertices)[index * (vertexElementSize / sizeof(float)) + 9];
然后在Delphi XE6中使用
PInteger (@ColorsArray [index * sizeOfVertices + 6])^
我得到相同的值,但在 delphi 中,颜色是黑色或空值。有时会得到负数,理解为在C++中可以用负值表示颜色,但在delphi中不行。
如何将以 D3DCOLORVALUE 格式获得的颜色值转换为 Delphi 的 TColor?
这些值用于获取对象的颜色 D3DMATERIAL9, D3DCOLORVALUE Ambient(环境色 RGB)
FLOAT r;
FLOAT g;
FLOAT b;
FLOAT a;
这个函数使颜色:
void SetColor(
D3DCOLORVALUE * iColor,
uint32_t color
)
{
iColor->r = (float) (color & ((unsigned int) 255 * 256 * 256 * 256)) / (256 * 256 * 256);
iColor->r /= 255.f;
iColor->g = (float) (color & (255 * 256 * 256)) / (256 * 256);
iColor->g /= 255.f;
iColor->b = (float) (color & (255 * 256)) / 256;
iColor->b /= 255.f;
iColor->a = (float) (color & (255));
iColor->a /= 255.f;
}
但是在Delphi中不行,得到的颜色是错误的。
颜色与@Dsm 解决方案的比较:
左:得到的颜色右:想要的颜色
我做了一个简单的例子来说明差异:
第一种颜色对应于柱脚,第二种颜色对应于柱子。带有平台的图是带有库查看器的示例,该版本具有正确的颜色,右图是 Visual Studio 2012 中手表的图像,以显示数组的内容和值相同使用 cast 获得的结果也是相同的,但是当将这些数字转换为颜色时,它们不会给出相同的结果。
【问题讨论】:
-
C++ 标准库不包含任何与图形相关的东西,因此它没有颜色的概念。您使用什么库来获取值?
-
将浮点值转换为
int32_t *(IOW,转换为指针!)非常可疑。到底是谁写了这段代码? -
来自Lights and Materials (Direct3D 9):
Ambient light color takes the form of an RGBA value, where each component is an integer value from 0 to 255. This is unlike most color values in Direct3D. -
如果那是你的原始代码,你为什么不把它发布到
int32*上,而不是奇怪的、可能是错误的转换?原始代码将指针转换为指针,假设vertices是一个数组,而不是指向指针的浮点数。 -
@LURD:这些是命名的颜色。但是 R、G 和 B 的任意组合都是可能的。