0
    for (iy = 0; iy < h; iy++)
    {
        double angy = (camera.fov_y / h) * iy;
        for (ix = 0; ix < w; ix++)
        {
            double angx = (camera.fov_x / w) * ix;
            //output[ix,iy].r = (int)Math.Round(255 * (angy / camera.fov_y);
            //output[ix,iy].b = (int)Math.Round(255 * (angy / camera.fov_y); 
            double tr = (angx / camera.fov_x) * 255D;
            double tb = (angy / camera.fov_y) * 255D;
            Console.Write("({0},{1})",Math.Round(tr),Math.Round(tb));

            output.SetPixel(ix, iy, Color.FromArgb(Convert.ToInt32(tr), 0, Convert.ToInt32(tb)) );
            Console.Write(".");
        }
        Console.WriteLine();
    }

任何人都可以看到该代码的任何直接问题吗?变量trtb始终评估为 0。

如果需要,我很乐意提供更多信息。

4

3 回答 3

1

您还没有给出其他变量的类型 - 特别是 和 的类型是camera.fov_x什么camera.fov_y?如果它们都是整数,则初始化的angx行将angy使用整数算术进行评估。

这可以通过强制转换操作数之一来解决:

double angy = ((double) camera.fov_y / h) * iy;

和变量已经加倍了,这不是问题fovyfovx

你能给出一个完整的例子,我们可以自己编译和测试吗?

编辑:Koistya Navin 的编辑太过分了。您只需要一个表达式的一个操作数是一个双精度数,就可以使用双精度算术计算整个事物。(但它必须是正确的表达式 - 如果你这样做(a/b) * c并转换c为双精度,则乘法将使用双精度算术完成,但 a/b 可能仍以整数形式完成。)

下面的清单进行了适当的更改,以确保在应该使用的任何地方都使用双重算术:

// Changed loops to declare the variable, for stylistic purposes
for (int iy = 0; iy < h; iy++)
{
    // Changed here - cast camera.fov_y
    double angy = ((double) camera.fov_y / h) * iy;
    for (int ix = 0; ix < w; ix++)
    {
        // Changed here - cast camera.fov_x
        double angx = ((double) camera.fov_x / w) * ix;
        //output[ix,iy].r = (int)Math.Round(255 * (angy / camera.fov_y);
        //output[ix,iy].b = (int)Math.Round(255 * (angy / camera.fov_y); 
        double tr = (angx / camera.fov_x) * 255D;
        double tb = (angy / camera.fov_y) * 255D;
        Console.Write("({0},{1})", Math.Round(tr), Math.Round(tb));

        output.SetPixel(ix, iy, Color.FromArgb(Convert.ToInt32(tr), 
                                               0,
                                               Convert.ToInt32(tb)) );
        Console.Write(".");
    }
    Console.WriteLine();
}
于 2009-04-05T08:31:37.490 回答
0

不要忘记将整数转换为双精度数。例如:

for (iy = 0; iy < h; iy++)
{
    double angy = ((double) camera.fov_y / h) * iy;
    for (ix = 0; ix < w; ix++)
    {
        double angx = ((double) camera.fov_x / (double) w) * (double) ix;
        output[ix,iy].r = (int) Math.Round(255 * (angy / camera.fov_y);
        output[ix,iy].b = (int) Math.Round(255 * (angy / camera.fov_y);
        double tr = (angx / camera.fov_x) * 255D;
        double tb = (angy / camera.fov_y) * 255D;
        Console.Write("({0},{1})",Math.Round(tr), Math.Round(tb));
        output.SetPixel(ix, iy, Color.FromArgb(
            Convert.ToInt32(tr), 0, Convert.ToInt32(tb)) );
        Console.Write(".");
    }
    Console.WriteLine();
} 

快速参考:

 int * double = double
 int / double = double
 double * int = double
 double / int = double

 int * int = int
 int / int = int // be carefull here!

 1 / 10 = 0 (not 0.1D)
 10 / 11 = 0 (not 1)
 1D / 10 = 0.1D
 1 / 10D = 0.1D
 1D / 10D = 0.1D
于 2009-04-05T08:27:50.353 回答
0

它知道这不是您原始问题的一部分,但是使用 SetPixel(..) 效率不是很高,如果您打算在光线跟踪引擎中使用它可能会出现问题。

您可能想使用 LockBits() 方法,请参阅此答案答案以了解更多详细信息。他们的另一种方法是使用“不安全”的 C# 代码访问数据,它允许您使用指向数据的指针。有关更多信息,请参阅此问题,我通过使用“不安全”代码获得了 ~x2 加速。

于 2009-04-06T10:44:04.247 回答