WP7 XNA - 只绘制了我的球体模型的一半

发布于 2024-10-31 03:09:10 字数 2275 浏览 1 评论 0原文

我在 Windows Phone 7 上使用 XNA/OpenGL 时遇到了一个非常奇怪的问题。我正在使用以下代码绘制一个球体:

 if (Radius < 0f)
            Radius = -Radius;
        if (Radius == 0f)
            throw new DivideByZeroException("DrawSphere: Radius cannot be 0f.");
        if (Precision == 0)
            throw new DivideByZeroException("DrawSphere: Precision of 8 or greater is required.");

        const float HalfPI = (float)(Math.PI * 0.5);
        float OneThroughPrecision = 1.0f / Precision;
        float TwoPIThroughPrecision = (float)(Math.PI * 2.0 * OneThroughPrecision);

        float theta1, theta2, theta3;
        Vector3 Normal = new Vector3(0,0,0), Position = new Vector3();

        for (uint j = 0; j < Precision / 2; j++)
        {
            theta1 = (j * TwoPIThroughPrecision) - HalfPI;
            theta2 = ((j + 1) * TwoPIThroughPrecision) - HalfPI;

            GL.Begin(BeginMode.TriangleStrip);
            for (uint i = 0; i <= Precision; i++)
            {
                theta3 = i * TwoPIThroughPrecision;

                Normal.X = (float)(Math.Cos(theta2) * Math.Cos(theta3));
                Normal.Y = (float)Math.Sin(theta2);
                Normal.Z = (float)(Math.Cos(theta2) * Math.Sin(theta3));
                Position.X = Center.X + Radius * Normal.X;
                Position.Y = Center.Y + Radius * Normal.Y;
                Position.Z = Center.Z + Radius * Normal.Z;

                GL.Normal3(Normal);
                GL.TexCoord2(i * OneThroughPrecision, 2.0f * (j + 1) * OneThroughPrecision);
                GL.Vertex3(Position);

                Normal.X = (float)(Math.Cos(theta1) * Math.Cos(theta3));
                Normal.Y = (float)Math.Sin(theta1);
                Normal.Z = (float)(Math.Cos(theta1) * Math.Sin(theta3));
                Position.X = Center.X + Radius * Normal.X;
                Position.Y = Center.Y + Radius * Normal.Y;
                Position.Z = Center.Z + Radius * Normal.Z;

                GL.Normal3(Normal);
                GL.TexCoord2(i * OneThroughPrecision, 2.0f * j * OneThroughPrecision);
                GL.Vertex3(Position);
            }
            GL.End();
        }

球体最终看起来像这样(在模拟器和设备 (HTC HD7) 上): 奇怪的球体
有什么建议吗?

I'm having a very strange problem with XNA/OpenGL on Windows Phone 7. I'm drawing a sphere using the following code:

 if (Radius < 0f)
            Radius = -Radius;
        if (Radius == 0f)
            throw new DivideByZeroException("DrawSphere: Radius cannot be 0f.");
        if (Precision == 0)
            throw new DivideByZeroException("DrawSphere: Precision of 8 or greater is required.");

        const float HalfPI = (float)(Math.PI * 0.5);
        float OneThroughPrecision = 1.0f / Precision;
        float TwoPIThroughPrecision = (float)(Math.PI * 2.0 * OneThroughPrecision);

        float theta1, theta2, theta3;
        Vector3 Normal = new Vector3(0,0,0), Position = new Vector3();

        for (uint j = 0; j < Precision / 2; j++)
        {
            theta1 = (j * TwoPIThroughPrecision) - HalfPI;
            theta2 = ((j + 1) * TwoPIThroughPrecision) - HalfPI;

            GL.Begin(BeginMode.TriangleStrip);
            for (uint i = 0; i <= Precision; i++)
            {
                theta3 = i * TwoPIThroughPrecision;

                Normal.X = (float)(Math.Cos(theta2) * Math.Cos(theta3));
                Normal.Y = (float)Math.Sin(theta2);
                Normal.Z = (float)(Math.Cos(theta2) * Math.Sin(theta3));
                Position.X = Center.X + Radius * Normal.X;
                Position.Y = Center.Y + Radius * Normal.Y;
                Position.Z = Center.Z + Radius * Normal.Z;

                GL.Normal3(Normal);
                GL.TexCoord2(i * OneThroughPrecision, 2.0f * (j + 1) * OneThroughPrecision);
                GL.Vertex3(Position);

                Normal.X = (float)(Math.Cos(theta1) * Math.Cos(theta3));
                Normal.Y = (float)Math.Sin(theta1);
                Normal.Z = (float)(Math.Cos(theta1) * Math.Sin(theta3));
                Position.X = Center.X + Radius * Normal.X;
                Position.Y = Center.Y + Radius * Normal.Y;
                Position.Z = Center.Z + Radius * Normal.Z;

                GL.Normal3(Normal);
                GL.TexCoord2(i * OneThroughPrecision, 2.0f * j * OneThroughPrecision);
                GL.Vertex3(Position);
            }
            GL.End();
        }

The sphere ends up looking like this (on BOTH the emulator AND the device (HTC HD7):
Strange sphere

Any suggestions?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

败给现实 2024-11-07 03:09:10

尝试使用这个。由于它更多地是 OpenGL 问题而不是 Android 问题,请尝试查找手动球体生成的算法。检查顶点顺序是否正确可能也是值得的。

Try using this. Since it's more of an OpenGL issue than Android, try looking up an algorithm for manual sphere generation. It might be worthwhile to check if vertex order is correct as well.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文