public void Init(MainWindow mainWindow)
    {
        m_program = OpenGLHelper._CompilerShader(vShaderStr, fShaderStr);

        m_meshData = OpenGLHelper.GetCubeMesh();

        m_ptr = Marshal.AllocHGlobal(sizeof(float) * m_meshData.m_data.Length);
        Marshal.Copy(m_meshData.m_data, 0, m_ptr, m_meshData.m_data.Length);

        GL.Enable(EnableCap.DepthTest);
        GL.DepthFunc(All.Lequal);
        GL.Enable(EnableCap.CullFace);

        OpenGLHelper.ClearGLError();
        m_locAmbient = GL.GetUniformLocation(m_program, "ambientColor");
        Debug.Log("m_locAmbient" + m_locAmbient);
        OpenGLHelper.CheckGLError();
        m_locDiffuse = GL.GetUniformLocation(m_program, "diffuseColor");
        Debug.Log("m_locDiffuse" + m_locDiffuse);
        m_locSpecular = GL.GetUniformLocation(m_program, "specularColor");
        Debug.Log("m_locSpecular" + m_locSpecular);
        m_locLight = GL.GetUniformLocation(m_program, "vLightPosition");
        Debug.Log("m_locLight" + m_locLight);
        m_locMVP = GL.GetUniformLocation(m_program, "mvpMatrix");
        Debug.Log("m_locMVP" + m_locMVP);
        m_locMV = GL.GetUniformLocation(m_program, "mvMatrix");
        Debug.Log("m_locMV" + m_locMV);
        //m_locNM = GL.GetUniformLocation(m_program, "normalMatrix");

        m_width  = mainWindow.Width;
        m_height = mainWindow.Height;
    }
Beispiel #2
0
    void DrawQuad()
    {
        float[] black = new float[] { 0, 0, 0, 1 };
        GL.ClearBuffer(ClearBuffer.Color, 0, black);

        GL.Viewport(0, 0, m_width, m_height);
        //GL.DrawBuffer(All.Back);
        GL.UseProgram(m_lightPassProgram);

        OpenGLHelper.ClearGLError();
        GL.Uniform1(m_locTex0, 0);
        OpenGLHelper.CheckGLError();
        GL.Uniform1(m_locTex1, 1);

        GL.ActiveTexture(All.Texture0);
        GL.BindTexture(All.Texture2D, m_gbuffer_tex[0]);

        GL.ActiveTexture(All.Texture1);
        GL.BindTexture(All.Texture2D, m_gbuffer_tex[1]);


        float[] vAmbientColor = { 0.1f, 0.1f, 0.1f, 1.0f };
        float[] vDiffuseColor = { 0.0f, 0.0f, 1.0f, 1.0f };

        GL.Uniform4(m_locAmbient, 1, vAmbientColor);
        GL.Uniform4(m_locDiffuse, 1, vDiffuseColor);

        //GL.BindVertexArray(fs_quad_vao);
        //GL.DrawArrays(All.TriangleStrip, 0, 4);
        const int POS_INDEX = 0;
        const int UV_INDEX  = 1;
        const int POS_SIZE  = 3;
        const int UV_SIZE   = 2;

        GL.EnableVertexAttribArray(POS_INDEX);
        GL.EnableVertexAttribArray(UV_INDEX);

        GL.VertexAttribPointer(POS_INDEX, POS_SIZE, All.Float, false, (POS_SIZE + UV_SIZE) * sizeof(float), m_ptrQuad);
        GL.VertexAttribPointer(UV_INDEX, UV_SIZE, All.Float, false, (POS_SIZE + UV_SIZE) * sizeof(float), m_ptrQuad + sizeof(float) * POS_SIZE);



        //OpenGLHelper.ClearGLError();
        //GL.Uniform1(m_locTex1, m_gbuffer_tex[1]);
        //OpenGLHelper.CheckGLError();

        GL.DrawElements(PrimitiveType.Triangles, m_quadIndices.Length, DrawElementsType.UnsignedShort, m_quadIndices);

        GL.DisableVertexAttribArray(POS_INDEX);
        GL.DisableVertexAttribArray(UV_INDEX);
    }
    public void OnRenderFrame(OpenTK.FrameEventArgs e)
    {
        //ushort[] indices = { 0, 1, 2, 0, 2, 3 };

        float[] black = new float[] { 0, 0, 0, 1 };
        GL.ClearBuffer(ClearBuffer.Color, 0, black);
        float[] ones = new float[] { 1.0f };
        GL.ClearBuffer(ClearBuffer.Depth, 0, ones);

        GL.UseProgram(m_program);

        const int POS_INDEX    = 0;
        const int NORMAL_INDEX = 1;
        const int POS_SIZE     = 3;
        const int NORMAL_SIZE  = 3;

        GL.VertexAttribPointer(POS_INDEX, POS_SIZE, All.Float, false, (POS_SIZE + NORMAL_SIZE) * sizeof(float), m_ptr);
        GL.VertexAttribPointer(NORMAL_INDEX, NORMAL_SIZE, All.Float, false, (POS_SIZE + NORMAL_SIZE) * sizeof(float), m_ptr + sizeof(float) * POS_SIZE);

        GL.EnableVertexAttribArray(POS_INDEX);
        GL.EnableVertexAttribArray(NORMAL_INDEX);

        float[] vEyeLight      = { -100.0f, 100.0f, 100.0f };
        float[] vAmbientColor  = { 0.1f, 0.1f, 0.1f, 1.0f };
        float[] vDiffuseColor  = { 0.0f, 0.0f, 1.0f, 1.0f };
        float[] vSpecularColor = { 1.0f, 1.0f, 1.0f, 1.0f };

        GL.Uniform4(m_locAmbient, 1, vAmbientColor);
        GL.Uniform4(m_locDiffuse, 1, vDiffuseColor);
        GL.Uniform4(m_locSpecular, 1, vSpecularColor);
        GL.Uniform3(m_locLight, 1, vEyeLight);

        m_accTime += e.Time;

        Matrix4x4 modelUnity  = Matrix4x4.TRS(Vector3.one, Quaternion.identity, Vector3.one);
        Transform cameraTrans = new Transform();

        cameraTrans.position = new Vector3(10, 10, 10);
        cameraTrans.forward  = Vector3.zero - cameraTrans.position;
        Matrix4x4 cameraLocalToWorld = cameraTrans.localToWorldMatrix;
        Matrix4x4 viewUnity          = OpenGLHelper.UnityWorldToCameraMatrix(cameraLocalToWorld);
        Matrix4x4 projectionUnity    = Matrix4x4.Perspective(60, m_width / (float)m_height, 0.1f, 100f);
        Matrix4x4 mvUnity            = viewUnity * modelUnity;
        Matrix4x4 mvpUnity           = projectionUnity * viewUnity * modelUnity;
        Matrix4x4 mvUnity2           = viewUnity.transpose * modelUnity.transpose;
        Matrix4x4 mvpUnity2          = projectionUnity.transpose * viewUnity.transpose * modelUnity.transpose;

        //Vector3 testPoint = Vector3.one;
        Vector4 testPoint = mvpUnity.MultiplyPoint(Vector3.one);

        //Vector4 testPoint = mvpUnity * Vector3.one;
        //Matrix4x4 cameraLocalToWorldUnity = Matrix4x4.TRS(new Vector3(10, 10, 10), Quaternion.Euler(45, 0, 0), Vector3.one);
        //Matrix4x4 view = UnityWorldToCameraMatrix(cameraLocalToWorld);
        //Matrix4x4 projection = Matrix4x4.Perspective(60, m_width / (float)m_height, 0.1f, 100f);
        //Matrix4x4 mv = view * model;
        //Matrix4x4 mvp = projection * view * model;

        OpenTK.Matrix4 model = OpenGLHelper.GLSRT(OpenTK.Vector3.One, OpenTK.Quaternion.Identity, OpenTK.Vector3.One);
        //OpenTK.Matrix4 cameraLocaltoWorld = TRS(new OpenTK.Vector3(0, 0, 50), new OpenTK.Quaternion(0, 0, 0), OpenTK.Vector3.One);
        //OpenTK.Matrix4 view = worldToCameraMatrix(cameraLocaltoWorld);
        OpenTK.Matrix4 view       = OpenGLHelper.GLLookAt(new OpenTK.Vector3(10, 10, 10), OpenTK.Vector3.Zero, new OpenTK.Vector3(0, 1, 0));
        OpenTK.Matrix4 projection = OpenGLHelper.GLPerspective(60, m_width / (float)m_height, 0.1f, 100f);

        //  坑死。。opengl变换从左往右乘
        OpenTK.Matrix4 mv   = model * view;
        OpenTK.Matrix4 mv2  = OpenGLHelper.Multiply(model, view);
        OpenTK.Matrix4 mv3  = OpenGLHelper.GLVMathMultiply(view, model);
        OpenTK.Matrix4 mvp  = model * view * projection;
        OpenTK.Matrix4 mvp2 = OpenGLHelper.Multiply(mv2, projection);
        OpenTK.Matrix4 mvp3 = OpenGLHelper.GLVMathMultiply(projection, mv3);

        OpenTK.Vector4 testPoint2 = OpenGLHelper.LeftMultiply(OpenTK.Vector3.One, mvp);
        testPoint2 /= testPoint2.W;
        OpenTK.Vector4 testPoint3 = OpenGLHelper.RightMultiply(mvp, OpenTK.Vector3.One);
        //OpenTK.Vector4 testPoint4 = LeftMultiply(OpenTK.Vector3.One, mvp);

        //UnityEngine.Debug.Log(mvp.ToString());
        //OpenTK.Matrix4 mvp2 = ConverToFloat2(mvp);
        //OpenTK.Matrix4 mv2 = ConverToFloat2(mv);
        OpenGLHelper.ClearGLError();
        //GL.UniformMatrix4(m_locMVP, false, ref mvp);


        GL.UniformMatrix4(m_locMVP, 1, false, OpenGLHelper.ConverToFloat(mvp));


        OpenGLHelper.CheckGLError();
        GL.UniformMatrix4(m_locMV, false, ref mv);
        //GL.UniformMatrix4(m_locMVP, 1, false, ConverToFloat(mvp));
        //GL.UniformMatrix4(m_locMV, 1, false, ConverToFloat(mv));

        GL.DrawElements(PrimitiveType.Triangles, m_meshData.m_index.Length, DrawElementsType.UnsignedShort, m_meshData.m_index);
    }