【问题标题】:OpenTK C# Perspective Lighting looks glitchyOpenTK C# 透视照明看起来有问题
【发布时间】:2012-10-19 21:47:53
【问题描述】:

我的目标是使透视图中的光照平滑。我对透视和正交使用相同的光照设置。下图显示正交照明看起来很棒。 当我旋转透视照明时,它看起来会出现故障和闪烁。我遗漏了什么会使透视照明看起来不错?

处理器:AMD FX 6100 6 核 3.31GHz

显卡:AMD Radeon HD 6800

注意:我引用了一个 OpenGL 示例。我正在使用不同的灯光设置来了解它们是如何工作的。我在本文末尾粘贴的代码是创建图像的代码。

视角:http://i.stack.imgur.com/pSXul.png

正交:http://i.stack.imgur.com/Q7Yr2.png

这是我的paint方法中的相关代码。

private void glControl1_Paint(object sender, PaintEventArgs e)
{
    if (!glLoaded)
        return;

    GL.Enable(EnableCap.DepthTest);
    //GL.Enable(EnableCap.Blend);
    GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit);

    //SET PERSPECTIVE OR ORTHOGRAPHIC VIEW
    if (chkPerspective.Checked)
    {
        double aspect = glControl1.Width / (double)glControl1.Height;

        Matrix4 perspective = Matrix4.CreatePerspectiveFieldOfView(MathHelper.PiOver4, (float)aspect, 0.0001f, 5000.0f);
        GL.MatrixMode(MatrixMode.Projection);
        GL.LoadIdentity();
        GL.LoadMatrix(ref perspective);

        Matrix4 lookat = Matrix4.LookAt(eyeOffset.X, eyeOffset.Y, eyeOffset.Z, 0, 0, 0, 0, 1, 0);

        GL.MatrixMode(MatrixMode.Modelview);
        GL.LoadMatrix(ref lookat);


        //GL.Translate(-boxOffset);
    }
    else
    {
        setupViewPort(); //Orthographic settings
        GL.MatrixMode(MatrixMode.Modelview); 
        GL.LoadIdentity();
    }

    GL.Rotate(angleY, 1.0f, 0, 0);
    GL.Rotate(angleX, 0, 1.0f, 0);

    //LIGHTING
    if (chkLighting.Checked)
    {
        float[] mat_specular = { 1.0f, 1.0f, 1.0f, 1.0f };
        float[] mat_shininess = { 50.0f };
        float[] light_position = { 1000.0f, 1000.0f, 1000.0f, 100.0f };
        float[] light_ambient = { 0.5f, 0.5f, 0.5f, 1.0f };

        GL.ClearColor(0.0f, 0.0f, 0.0f, 1.0f);
        //GL.ShadeModel(ShadingModel.Smooth);

        //GL.Material(MaterialFace.Front, MaterialParameter.Specular, mat_specular);
        //GL.Material(MaterialFace.Front, MaterialParameter.Shininess, mat_shininess);
        GL.Light(LightName.Light0, LightParameter.Position, light_position);
        //GL.Light(LightName.Light0, LightParameter.Ambient, light_ambient);
        //GL.Light(LightName.Light0, LightParameter.Diffuse, mat_specular);

        GL.Enable(EnableCap.Lighting);
        GL.Enable(EnableCap.Light0);
        GL.Enable(EnableCap.ColorMaterial);
        //GL.Enable(EnableCap.CullFace);
    }
    else
    {
        GL.Disable(EnableCap.Lighting);
        GL.Disable(EnableCap.Light0);
    }

    foreach (Cube cube in cubes)
    {
        cube.drawCube(selectionCubeRadius);
    }

    glControl1.SwapBuffers();
}


这是我的正交视口代码,以防万一。

private void setupViewPort()
{
    if (chkPerspective.Checked)
        return;

    int w = glControl1.Width;
    int h = glControl1.Height;
    GL.MatrixMode(MatrixMode.Projection);
    GL.LoadIdentity();
    GL.Ortho(-w, w, -h, h, -5000, 5000); // Bottom-left corner pixel has coordinate (0, 0)
    GL.Viewport(0, 0, w, h); // Use all of the glControl paintingarea
}

【问题讨论】:

  • 好问题似乎包含帮助专家解决问题所需的所有相关信息。我绝对不是那个专家;我向你保证,你比我更了解编码这些东西!当我看到一个时,我只知道一个很好的 Q…
  • 您确定要将 100.0f 作为 light_position.w,而不是 1.0f 吗?
  • 将 W 更改为 1.0 会使光强度从块的一侧到另一侧的变化不太明显。不过,透视图中的闪烁仍然存在。

标签: c# opengl perspective lighting opentk


【解决方案1】:

仅根据图像,深度测试似乎已被禁用,或者某些法线被翻转或未归一化。

drawCube 到底是做什么的?

【讨论】:

  • DrawCube() 包含 GL.Begin(BeginMode.Quads),绘制立方体的 6 个面,然后是 GL.End()。就这些。有什么方法可以确保法线正确吗?
  • 您的GL.Normal3f 呼叫什么?它们必须包含单位长度的向量。只需发布代码!
【解决方案2】:

我找到了一个现在可以使用的解决方案。在透视图中,我将立方体的大小从 200.0 减小到 5.0。然后我将相机移近,使它们看起来大小相同。也有人说我在创建透视视野时应该减小远 Z 平面。

减小模型的大小是可行的,但出于约定目的,我还减小了远 z 平面的距离。我还可能会创建一个算法,根据我当前的模型大小和角度确定要使用的 z 平面距离。

用户“dflemstr”询问了我的 GL.Normal3f 调用。答案是我没有。它们是做什么的,它们是否必要?

【讨论】:

  • 法线是一个长度为 1 的向量,它指向您的表面“外”。每个顶点都有一个法线。例如,对于立方体的顶部正方形,每个顶点的法线为 (0, 1, 0),因为表面指向上方(正 Y)。如果不指定法线,驱动程序必须猜测它们,光的计算就会出错。
猜你喜欢
  • 1970-01-01
  • 2013-02-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-07-15
  • 2013-06-21
  • 2021-04-11
相关资源
最近更新 更多