OpenTK。镜面反射的麻烦

时间:2015-03-01 20:25:50

标签: reflection

我在OpenTK中渲染镜像反射时遇到了一些问题。我需要从镜子的一侧看到物体的反射,但是我的程序也从另一侧反射并隐藏了第一个。你能帮帮我吗?

        private void glControl1_Load(object sender, EventArgs e)
        {
            loaded = true;

            GL.ClearColor(Color.SkyBlue);
            initRendering();

            Matrix4 p = Matrix4.CreatePerspectiveFieldOfView((float)(80 * Math.PI / 180), 1, 20, 500);
            GL.MatrixMode(MatrixMode.Projection);
            GL.LoadMatrix(ref p);

            Matrix4 modelview = Matrix4.LookAt(70, 70, 70, 0, 0, 0, 0, 1, 0);
            GL.MatrixMode(MatrixMode.Modelview);
            GL.LoadMatrix(ref modelview);
        }

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

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

            drawScene();

            /*axes*/
            GL.Color3(Color.Black);
            GL.Begin(BeginMode.Lines);
            GL.Vertex3(0, 0, 0);
            GL.Vertex3(70, 0, 0);
            GL.Vertex3(0, 0, 0);
            GL.Vertex3(0, 70, 0);
            GL.Vertex3(0, 0, 0);
            GL.Vertex3(0, 0, 70);
            GL.End();

            glControl1.SwapBuffers();
        }

        void initRendering()
        {
            GL.ShadeModel(ShadingModel.Flat);

            GL.ClearDepth(1.0f);
            GL.ClearStencil(0);

            GL.Enable(EnableCap.DepthTest);
            GL.DepthFunc(DepthFunction.Lequal);
            GL.Hint(HintTarget.PerspectiveCorrectionHint, HintMode.Nicest);

            GL.Enable(EnableCap.AutoNormal);
            // Setup the drawing area and shading mode
            GL.MatrixMode(MatrixMode.Projection);
            GL.LoadIdentity();


            GL.Enable(EnableCap.PointSmooth);
            GL.MatrixMode(MatrixMode.Modelview);
            GL.LoadIdentity();

            GL.Enable(EnableCap.Lighting);
            GL.LightModel(LightModelParameter.LightModelAmbient, ambientLight);
            GL.Enable(EnableCap.Normalize);
            GL.Enable(EnableCap.ColorMaterial);
        }

        public class CustomOpenGLControl : GLControl
        {
            public CustomOpenGLControl()
                : base(new OpenTK.Graphics.GraphicsMode(32, 24, 8))
            {

            }
        }

        private void drawFrame()
        {
            GL.Color3(Color.Red);
            GL.Begin(BeginMode.Quads);
            GL.Normal3(0.0f, -1.0f, 0.0f);
            for (int i = 0; i < upWall.Length; i++)
            {
                GL.Vertex3(upWall[i].x, upWall[i].y, upWall[i].z);
            }
            GL.End();

            GL.Color3(Color.Yellow);
            GL.Begin(BeginMode.Quads);
            GL.Normal3(0.0f, -1.0f, 0.0f);
            for (int i = 0; i < downWall.Length; i++)
            {
                GL.Vertex3(downWall[i].x, downWall[i].y, downWall[i].z);
            }
            GL.End();

            GL.Color3(Color.Green);
            GL.Begin(BeginMode.Quads);
            GL.Normal3(0.0f, 0.0f, -1.0f);
            for (int i = 0; i < backWall.Length; i++)
            {
                GL.Vertex3(backWall[i].x, backWall[i].y, backWall[i].z);
            }
            GL.End();

            GL.Color3(Color.Blue);
            GL.Begin(BeginMode.Quads);
            GL.Normal3(-1.0f, 0.0f, 0.0f);
            for (int i = 0; i < rightWall.Length; i++)
            {
                GL.Vertex3(rightWall[i].x, rightWall[i].y, rightWall[i].z);
            }
            GL.End();

            GL.Color3(Color.Orange);
            GL.Begin(BeginMode.Quads);
            GL.Normal3(1.0f, 0.0f, 0.0f);
            for (int i = 0; i < leftWall.Length; i++)
            {
                GL.Vertex3(leftWall[i].x, leftWall[i].y, leftWall[i].z);
            }
            GL.End();
        }

        private void drawMirror()
        {
            GL.PushMatrix();

            GL.Begin(BeginMode.Quads);
            GL.Normal3(0.0f, -1.0f, 0.0f);
            for (int i = 0; i < mirror.Length; i++)
            {
                GL.Vertex3(mirror[i].x, mirror[i].y, mirror[i].z);
            }
            GL.End();

            GL.PopMatrix();
        }

        private void drawScene()
        {
            GL.Clear(ClearBufferMask.AccumBufferBit | ClearBufferMask.DepthBufferBit | ClearBufferMask.StencilBufferBit | ClearBufferMask.ColorBufferBit);
            double[] eqr = { 0f, y0 + height / 6f, 0f, 0.0f };

            GL.PushMatrix();

            GL.ColorMask(false, false, false, false);
            GL.Enable(EnableCap.StencilTest);
            GL.StencilFunc(StencilFunction.Always, 1, 1);
            GL.StencilOp(StencilOp.Keep, StencilOp.Keep, StencilOp.Replace);
            GL.Disable(EnableCap.DepthTest);
            drawMirror();
            GL.Enable(EnableCap.DepthTest);
            GL.ColorMask(true, true, true, true);
            GL.StencilFunc(StencilFunction.Equal, 1, 1);
            GL.StencilOp(StencilOp.Keep, StencilOp.Keep, StencilOp.Keep);

            GL.Enable(EnableCap.ClipPlane0);
            GL.ClipPlane(ClipPlaneName.ClipPlane0, eqr);
            GL.PushMatrix();
            GL.Scale(1, -1, 1);
            drawFrame();
            GL.PopMatrix();
            GL.Disable(EnableCap.ClipPlane0);
            GL.Disable(EnableCap.StencilTest);

            GL.Enable(EnableCap.Blend);
            GL.Disable(EnableCap.Lighting);
            GL.Color4(1, 1, 1, 0.3f);
            GL.BlendFunc(BlendingFactorSrc.SrcAlpha, BlendingFactorDest.OneMinusSrcAlpha);
            drawMirror();
            GL.Enable(EnableCap.Lighting);
            GL.Disable(EnableCap.Blend);
            drawFrame();
            GL.PopMatrix();
        }
    }

1 个答案:

答案 0 :(得分:0)

抱歉,我对此无能为力。我会运行你的代码,但不幸的是我现在不在我的编程计算机上,而且我对opentk没有任何经验。