2012-05-26 60 views
2

我的目標是讓透視圖中的照明平滑。我對透視和正射影像都使用相同的光線設置。下面的圖片顯示字形照明看起來很棒。 當我旋轉它時,透視照明看起來很毛糙並閃爍。我錯過了什麼,會使透視圖照明看起來好嗎?OpenTK C#透視圖照明看起來有點毛骨悚然

處理器: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

這裏是我的繪製方法中的相關代碼。

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 
} 
+0

好問題似乎有幫助專家解決它所需的所有相關信息。我絕對不是那個專家;我向你保證,你比我更瞭解這些東西。當我看到一個人時,我只知道一個好Q ... –

+0

您確定要將100.0f作爲light_position.w而不是1.0f? –

+0

將W更改爲1.0使得從塊的一側到另一側的光強度變化不太明顯。不過,透視圖中的閃爍仍然存在。 – user1334960

回答

0

我發現了一個現在可以使用的解決方案。在透視圖中,我將立方體的大小從200.0減小到5.0。然後,我將相機移近一些,使它們看起來大小相同。有些人還說我創建我的視角視野時應該減少遠Z平面。

減少我的模型的大小工作,但我也減少了遠距離z平面的距離用於約定目的。我也可能創建一個算法,根據我當前的模型大小和角度確定要使用的z平面距離。

用戶'dflemstr'詢問我的GL.Normal3f調用。答案是我沒有。他們做什麼,是否有必要?

+0

法線是一個長度爲1的矢量,指出「從」你的表面。每個頂點都有一個法線。例如,對於立方體的頂部正方形,每個頂點的法線將爲(0,1,0),因爲曲面向上(正Y)。如果你沒有指定法線,司機必須猜測它們,而光線計算將是錯誤的。 – dflemstr

1

完全基於圖像,它看起來像深度測試被禁用或一些法線翻轉或不歸。

drawCube是做什麼的?

+0

DrawCube()包含GL.Begin(BeginMode.Quads),繪製立方體的6個面,然後繪製GL.End()。就這些。有沒有辦法確保法線是正確的? – user1334960

+0

目前你的GL.Normal3f調用的是什麼?它們必須包含單位長度的向量。只需發佈代碼! – dflemstr

相關問題