Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/opengl/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 为什么一个顶点的着色与其他顶点不同?_C#_Opengl_Opentk - Fatal编程技术网

C# 为什么一个顶点的着色与其他顶点不同?

C# 为什么一个顶点的着色与其他顶点不同?,c#,opengl,opentk,C#,Opengl,Opentk,我遇到的问题是,每个面的一个顶点总是以不同于其他顶点的方式进行着色(其他顶点亮起时为暗,其他顶点暗起时为亮)。我怀疑法线计算不正确,但我画了它们,它们都指向外垂直于曲面,就像在逐顶点照明中所假设的那样(或者我的假设是错误的) 以下是我绘制简单四边金字塔的代码: static float anglea = 0f; /// <summary>Creates a 800x600 window with the specified title.</summary>

我遇到的问题是,每个面的一个顶点总是以不同于其他顶点的方式进行着色(其他顶点亮起时为暗,其他顶点暗起时为亮)。我怀疑法线计算不正确,但我画了它们,它们都指向外垂直于曲面,就像在逐顶点照明中所假设的那样(或者我的假设是错误的)

以下是我绘制简单四边金字塔的代码:

    static float anglea = 0f;

    /// <summary>Creates a 800x600 window with the specified title.</summary>
    public Program() : base(800, 600, GraphicsMode.Default, "OpenTK Quick Start Sample")
    {
        VSync = VSyncMode.On;
    }

    /// <summary>Load resources here.</summary>
    /// <param name="e">Not used.</param>
    protected override void OnLoad(EventArgs e)
    {
        base.OnLoad(e);

        GL.ClearColor(0.1f, 0.2f, 0.5f, 0.0f);
        GL.Enable(EnableCap.DepthTest);

        GL.Enable(EnableCap.Lighting);
        GL.Enable(EnableCap.Light0);

        GL.Material(MaterialFace.FrontAndBack, MaterialParameter.Ambient, new float[] { 0f, 0f, 0f, 0f });
        GL.Material(MaterialFace.FrontAndBack, MaterialParameter.Diffuse, new float[] { 0.2f, 0.7f, 0.2f, 0f });
        GL.Material(MaterialFace.FrontAndBack, MaterialParameter.Specular, new float[] { 0.2f, 0.7f, 0.2f, 0f });
        GL.Material(MaterialFace.FrontAndBack, MaterialParameter.Shininess, 100f);

        GL.Light(LightName.Light0, LightParameter.Ambient, new float[] { 0f, 0f, 0f, 0f });
        GL.Light(LightName.Light0, LightParameter.Diffuse, new float[] { 1f, 1f, 1f, 0f });
        GL.Light(LightName.Light0, LightParameter.Specular, new float[] { 0.2f, 0.7f, 0.2f, 0f });
    }

    /// <summary>
    /// Called when your window is resized. Set your viewport here. It is also
    /// a good place to set up your projection matrix (which probably changes
    /// along when the aspect ratio of your window).
    /// </summary>
    /// <param name="e">Not used.</param>
    protected override void OnResize(EventArgs e)
    {
        base.OnResize(e);

        GL.Viewport(ClientRectangle.X, ClientRectangle.Y, ClientRectangle.Width, ClientRectangle.Height);

        Matrix4 projection = Matrix4.CreatePerspectiveFieldOfView((float)Math.PI / 4, Width / (float)Height, 1.0f, 64.0f);
        GL.MatrixMode(MatrixMode.Projection);
        GL.LoadMatrix(ref projection);
    }

    /// <summary>
    /// Called when it is time to setup the next frame. Add you game logic here.
    /// </summary>
    /// <param name="e">Contains timing information for framerate independent logic.</param>
    protected override void OnUpdateFrame(FrameEventArgs e)
    {
        base.OnUpdateFrame(e);

        if (Keyboard[Key.Escape])
            Exit();

        if (Keyboard[Key.Left])
            anglea -= 1.5f;

        if (Keyboard[Key.Right])
            anglea += 1.5f;
    }

    /// <summary>
    /// Called when it is time to render the next frame. Add your rendering code here.
    /// </summary>
    /// <param name="e">Contains timing information.</param>
    protected override void OnRenderFrame(FrameEventArgs e)
    {
        base.OnRenderFrame(e);

        GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit);

        //Matrix4 modelview = Matrix4.LookAt(Vector3.Zero, Vector3.UnitZ, Vector3.UnitY);
        Matrix4 modelview = Matrix4.LookAt(0f, 0f, 8f, 0f, 0f, 0f, 0f, 1f, 0f);
        GL.MatrixMode(MatrixMode.Modelview);
        GL.LoadMatrix(ref modelview);

        GL.Light(LightName.Light0, LightParameter.Position, new float[] { 0f, 0f, 1f, 0f });

        GL.Rotate(anglea, -Vector3.UnitY);

        Vector3 a, b, c, n;
        List<Vector3> tocke = new List<Vector3>();
        List<Vector3> normale = new List<Vector3>();

        //front face
        GL.Begin(BeginMode.Triangles);

        a = new Vector3(-1.0f, -1.0f, 1.0f);
        b = new Vector3(1.0f, -1.0f, 1.0f);
        c = new Vector3(0.0f, 1.0f, 0.0f);
        n = Vector3.Cross(new Vector3(a) - new Vector3(b), new Vector3(b) - new Vector3(c));
        n.Normalize();

        tocke.Add(a);
        tocke.Add(b);
        tocke.Add(c);
        normale.Add(n);
        normale.Add(n);
        normale.Add(n);

        GL.Vertex3(a);
        GL.Normal3(n);
        GL.Vertex3(b);
        GL.Normal3(n);
        GL.Vertex3(c);
        GL.Normal3(n);

        GL.End();

        //left face
        GL.Begin(BeginMode.Triangles);

        a = new Vector3(-1.0f, -1.0f, -1.0f);
        b = new Vector3(-1.0f, -1.0f, 1.0f);
        c = new Vector3(0.0f, 1.0f, 0.0f);
        n = Vector3.Cross(new Vector3(a) - new Vector3(b), new Vector3(b) - new Vector3(c));
        n.Normalize();

        tocke.Add(a);
        tocke.Add(b);
        tocke.Add(c);
        normale.Add(n);
        normale.Add(n);
        normale.Add(n);

        GL.Vertex3(a);
        GL.Normal3(n);
        GL.Vertex3(b);
        GL.Normal3(n);
        GL.Vertex3(c);
        GL.Normal3(n);

        GL.End();

        //back face
        GL.Begin(BeginMode.Triangles);

        a = new Vector3(1.0f, -1.0f, -1.0f);
        b = new Vector3(-1.0f, -1.0f, -1.0f);
        c = new Vector3(0.0f, 1.0f, 0.0f);
        n = Vector3.Cross(new Vector3(a) - new Vector3(b), new Vector3(b) - new Vector3(c));
        n.Normalize();

        tocke.Add(a);
        tocke.Add(b);
        tocke.Add(c);
        normale.Add(n);
        normale.Add(n);
        normale.Add(n);

        GL.Vertex3(a);
        GL.Normal3(n);
        GL.Vertex3(b);
        GL.Normal3(n);
        GL.Vertex3(c);
        GL.Normal3(n);

        GL.End();

        //right face
        GL.Begin(BeginMode.Triangles);

        a = new Vector3(1.0f, -1.0f, 1.0f);
        b = new Vector3(1.0f, -1.0f, -1.0f);
        c = new Vector3(0.0f, 1.0f, 0.0f);
        n = Vector3.Cross(new Vector3(a) - new Vector3(b), new Vector3(b) - new Vector3(c));
        n.Normalize();

        tocke.Add(a);
        tocke.Add(b);
        tocke.Add(c);
        normale.Add(n);
        normale.Add(n);
        normale.Add(n);

        GL.Vertex3(a);
        GL.Normal3(n);
        GL.Vertex3(b);
        GL.Normal3(n);
        GL.Vertex3(c);
        GL.Normal3(n);

        GL.End();

        //Drawing normals
        for (int i = 0; i < tocke.Count; i++)
        {
            GL.Begin(BeginMode.Lines);

            GL.Vertex3(tocke[i]);
            GL.Vertex3(tocke[i] + normale[i]);

            GL.End();
        }

        SwapBuffers();
    }

    /// <summary>
    /// The main entry point for the application.
    /// </summary>
    [STAThread]
    static void Main()
    {
        // The 'using' idiom guarantees proper resource cleanup.
        // We request 30 UpdateFrame events per second, and unlimited
        // RenderFrame events (as fast as the computer can handle).
        using (Program game = new Program())
        {
            game.Run(30.0);
        }
    }
静态浮动角度a=0f;
///创建具有指定标题的800x600窗口。
publicprogram():base(800600,GraphicsMode.Default,“OpenTK快速入门示例”)
{
VSync=VSyncMode.On;
}
///在这里加载资源。
///没有用。
受保护的覆盖无效加载(事件参数e)
{
基础荷载(e);
GL.ClearColor(0.1f、0.2f、0.5f、0.0f);
总账启用(启用CAP.深度测试);
总账启用(启用上限照明);
总账启用(启用CAP.Light0);
GL.材料(材料前、后、材料参数、环境、新浮子[]{0f、0f、0f、0f});
GL.Material(MaterialFace.FrontAndBack,MaterialParameter.Diffuse,new float[]{0.2f,0.7f,0.2f,0f});
GL.Material(MaterialFace.FrontAndBack,MaterialParameter.Specular,new float[]{0.2f,0.7f,0.2f,0f});
GL.材料(材料正面和背面,材料参数,光泽度,100f);
GL.Light(LightName.Light0,LightParameter.Ambient,新float[]{0f,0f,0f,0f});
GL.Light(LightName.Light0,LightParameter.Diffuse,新float[]{1f,1f,1f,0f});
GL.Light(LightName.Light0,LightParameter.Specular,新浮点[]{0.2f,0.7f,0.2f,0f});
}
/// 
///调整窗口大小时调用。在此处设置视口。也是
///设置投影矩阵的好地方(可能会发生变化
///当窗口的纵横比增大时)。
/// 
///没有用。
受保护的覆盖void OnResize(事件参数e)
{
基数(e);
GL.视口(ClientRectangle.X、ClientRectangle.Y、ClientRectangle.Width、ClientRectangle.Height);
Matrix4 projection=Matrix4.CreatePerspectiveFieldOfView((float)Math.PI/4,宽度/(float)高度,1.0f,64.0f);
GL.MatrixMode(MatrixMode.Projection);
总帐负荷矩阵(参考投影);
}
/// 
///在设置下一帧时调用。在这里添加游戏逻辑。
/// 
///包含与帧率无关的逻辑的计时信息。
受保护的覆盖无效OnUpdate帧(FrameEventArgs e)
{
基础.更新帧(e);
if(键盘[Key.Escape])
退出();
if(键盘[左键])
角度A-=1.5f;
如果(键盘[右键])
角度A+=1.5f;
}
/// 
///在渲染下一帧时调用。在此处添加渲染代码。
/// 
///包含计时信息。
RenderFrame上受保护的覆盖无效(FrameEventArgs e)
{
基于渲染帧(e);
GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit);
//Matrix4 modelview=Matrix4.LookAt(Vector3.Zero,Vector3.UnitZ,Vector3.UnitY);
Matrix4 modelview=Matrix4.LookAt(0f、0f、8f、0f、0f、0f、0f、1f、0f);
GL.MatrixMode(MatrixMode.Modelview);
总帐负荷矩阵(参考模型视图);
GL.Light(LightName.Light0,LightParameter.Position,新浮点[]{0f,0f,1f,0f});
GL.旋转(角度A,-矢量3.单位);
向量3 a,b,c,n;
List tocke=新列表();
列表法线=新列表();
//正面
GL.Begin(BeginMode.Triangles);
a=新矢量3(-1.0f,-1.0f,1.0f);
b=新矢量3(1.0f,-1.0f,1.0f);
c=新矢量3(0.0f,1.0f,0.0f);
n=向量3.交叉(新向量3(a)-新向量3(b),新向量3(b)-新向量3(c));
n、 规范化();
tocke.加入(a);
tocke.添加(b);
tocke.添加(c);
加(n);
加(n);
加(n);
GL.顶点3(a);
GL.Normal3(n);
GL.顶点3(b);
GL.Normal3(n);
GL.顶点3(c);
GL.Normal3(n);
GL.End();
//左脸
GL.Begin(BeginMode.Triangles);
a=新矢量3(-1.0f,-1.0f,-1.0f);
b=新矢量3(-1.0f,-1.0f,1.0f);
c=新矢量3(0.0f,1.0f,0.0f);
n=向量3.交叉(新向量3(a)-新向量3(b),新向量3(b)-新向量3(c));
n、 规范化();
tocke.加入(a);
tocke.添加(b);
tocke.添加(c);
加(n);
加(n);
加(n);
GL.顶点3(a);
GL.Normal3(n);
GL.顶点3(b);
GL.Normal3(n);
GL.顶点3(c);
GL.Normal3(n);
GL.End();
//背面
GL.Begin(BeginMode.Triangles);
a=新矢量3(1.0f,-1.0f,-1.0f);
b=新矢量3(-1.0f,-1.0f,-1.0f);
c=新矢量3(0.0f,1.0f,0.0f);
n=向量3.交叉(新向量3(a)-新向量3(b),新向量3(b)-新向量3(c));
n、 规范化();
tocke.加入(a);
tocke.添加(b);
tocke.添加(c);
加(n);
加(n);
加(n);
GL.顶点3(a);
GL.Normal3(n);
GL.顶点3(b);
GL.Normal3(n);
GL.顶点3(c);
GL.Normal3(n);
GL.End();
//右脸
GL.Begin(BeginMode.Triangles);
a=新矢量3(1.0f,-1.0f,1.0f);
b=新矢量3(1.0f,-1.0f,-1.0f);
c=新矢量3(0.0f,1.0f,0.0f);
n=向量3.交叉(新向量3(a)-新向量3(b),新向量3(b)-新向量3(c));
n、 规范化();
tocke.加入(a);
tocke.添加(b);
tocke.添加(c);
加(n);
加(n);
加(n);
GL.顶点3(a);
GL.Normal3(n);
GL.顶点3(b);
GL.Normal3(n);
GL.顶点3(c);
德国劳埃德船级社
    GL.Vertex3(a);
    GL.Normal3(n);
    GL.Vertex3(b);
    GL.Normal3(n);
    GL.Vertex3(c);
    GL.Normal3(n);