public override void display()
        {
            ClearDisplay();

            if((g_pPlaneMesh != null) && (g_pCylinderMesh != null))
            {
                MatrixStack modelMatrix = new MatrixStack();
                modelMatrix.SetMatrix(g_viewPole.CalcMatrix());

                Vector4 lightDirCameraSpace = Vector4.Transform(g_lightDirection, modelMatrix.Top());
                ProgramData whiteDiffuse = g_bShowAmbient ? g_WhiteAmbDiffuseColor : g_WhiteDiffuseColor;
                ProgramData vertexDiffuse = g_bShowAmbient ? g_VertexAmbDiffuseColor : g_VertexDiffuseColor;

                if(g_bShowAmbient)
                {
                    GL.UseProgram(whiteDiffuse.theProgram);
                    GL.Uniform4(whiteDiffuse.lightIntensityUnif, 0.8f, 0.8f, 0.8f, 1.0f);
                    GL.Uniform4(whiteDiffuse.ambientIntensityUnif, 0.2f, 0.2f, 0.2f, 1.0f);
                    GL.UseProgram(0);
                    GL.UseProgram(vertexDiffuse.theProgram);
                    GL.Uniform4(vertexDiffuse.lightIntensityUnif, 0.8f, 0.8f, 0.8f, 1.0f);
                    GL.Uniform4(vertexDiffuse.ambientIntensityUnif, 0.2f, 0.2f, 0.2f, 1.0f);
                    GL.UseProgram(0);
                }
                else
                {
                    GL.UseProgram(whiteDiffuse.theProgram);
                    GL.Uniform4(whiteDiffuse.lightIntensityUnif, 0.5f, 0.5f, 0.5f, 0.5f);
                    GL.UseProgram(0);
                    GL.UseProgram(vertexDiffuse.theProgram);
                    GL.Uniform4(vertexDiffuse.lightIntensityUnif, 0.5f, 0.5f, 0.5f, 0.5f);
                    GL.UseProgram(0);
                }

                GL.UseProgram(whiteDiffuse.theProgram);
                Vector3 dirToLight = new Vector3(lightDirCameraSpace.X, lightDirCameraSpace.Y, lightDirCameraSpace.Z);

                GL.Uniform3(whiteDiffuse.dirToLightUnif, dirToLight);
                GL.UseProgram(0);
                GL.UseProgram(vertexDiffuse.theProgram);
                GL.Uniform3(vertexDiffuse.dirToLightUnif, dirToLight);
                GL.UseProgram(0);

                //Render the ground plane.
                using ( PushStack pushstack = new PushStack(modelMatrix))
                {
                    GL.UseProgram(whiteDiffuse.theProgram);
                    //modelMatrix.Scale(new Vector3(10f, 10f, 1f));
                    //modelMatrix.ApplyMatrix(g_objtPole.CalcMatrix());

                    //modelMatrix.Translate(new Vector3(0f, 0f, -10f));

                    Matrix4 mm =  modelMatrix.Top();
                    planeModelmatrix = mm;
                    //mm = Matrix4.Identity; // TEST
                    GL.UniformMatrix4(whiteDiffuse.modelToCameraMatrixUnif, false, ref mm);
                    //projData.cameraToClipMatrix = Matrix4.Identity; // Test
                    GL.UniformMatrix4(whiteDiffuse.cameraToClipMatrixUnif, false, ref projData.cameraToClipMatrix);
                    Matrix3 normMatrix = new Matrix3(modelMatrix.Top());
                    //normMatrix.Normalize();
                    //normMatrix = Matrix3.Identity; // TEST
                    GL.UniformMatrix3(whiteDiffuse.normalModelToCameraMatrixUnif, false, ref normMatrix);
                    g_pPlaneMesh.Render();
                    GL.UseProgram(0);
                }

                //Render the Cylinder
                using (PushStack pushstack = new PushStack(modelMatrix))
                {
                    modelMatrix.ApplyMatrix(g_objtPole.CalcMatrix());
                    //modelMatrix.Scale(0.05f);
                    modelMatrix.Translate(new Vector3(0f, 0.0f, -10f));
                    coloredCylinderModelmatrix = modelMatrix.Top ();
                    if(g_bDrawColoredCyl)
                    {
                        GL.UseProgram(vertexDiffuse.theProgram);
                        Matrix4 mm = modelMatrix.Top();
                        //mm = Matrix4.Identity; // TEST
                        GL.UniformMatrix4(vertexDiffuse.modelToCameraMatrixUnif, false, ref mm);
                        //projData.cameraToClipMatrix = Matrix4.Identity; // TEST
                        GL.UniformMatrix4(vertexDiffuse.cameraToClipMatrixUnif, false, ref projData.cameraToClipMatrix);
                        Matrix3 normMatrix = new Matrix3(modelMatrix.Top());
                        //normMatrix = Matrix3.Identity; // TEST
                        GL.UniformMatrix3(vertexDiffuse.normalModelToCameraMatrixUnif, false, ref normMatrix);
                        g_pCylinderMesh.Render("lit-color");
                    }
                    else
                    {
                        GL.UseProgram(whiteDiffuse.theProgram);
                        Matrix4 mm = modelMatrix.Top();
                        //mm = Matrix4.Identity; // TEST
                        GL.UniformMatrix4(whiteDiffuse.modelToCameraMatrixUnif, false, ref mm);
                        //projData.cameraToClipMatrix = Matrix4.Identity; // TEST
                        GL.UniformMatrix4(whiteDiffuse.cameraToClipMatrixUnif, false, ref projData.cameraToClipMatrix);
                        Matrix3  normMatrix = new Matrix3(modelMatrix.Top());
                        //normMatrix = Matrix3.Identity; // TEST
                        GL.UniformMatrix3(whiteDiffuse.normalModelToCameraMatrixUnif, false, ref normMatrix);
                        g_pCylinderMesh.Render("lit");
                    }

                    GL.UseProgram(0);
                }

            }
        }
        //Called whenever the window is resized. The new window size is given, in pixels.
        //This is an opportunity to call glViewport or glScissor to keep up with the change in size.
        public override void reshape()
        {
            MatrixStack camMatrix = new MatrixStack();
            camMatrix.SetMatrix(Camera.GetLookAtMatrix());

            cm = camMatrix.Top();

            MatrixStack persMatrix = new MatrixStack();
            persMatrix.Perspective(perspectiveAngle, (width / (float)height), g_fzNear, g_fzFar);
            pm = persMatrix.Top();

            SetGlobalMatrices(currentProgram);

            GL.Viewport(0, 0, width, height);
        }
        public override void reshape()
        {
            MatrixStack persMatrix = new MatrixStack();
            persMatrix.Perspective(45.0f, (width / (float)height), g_fzNear, g_fzFar);

            projData.cameraToClipMatrix = persMatrix.Top();

            MatrixStack camMatrix = new MatrixStack();
            camMatrix.SetMatrix(Camera.GetLookAtMatrix());

            GL.Viewport(0, 0, width, height);
        }
        public override void display()
        {
            g_lightTimer.Update();

            GL.ClearColor(0.75f, 0.75f, 1.0f, 1.0f);
            GL.ClearDepth(1.0f);
            GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit);

            if((g_pObjectMesh != null) && (g_pCubeMesh != null))
            {
                MatrixStack modelMatrix = new MatrixStack();
                modelMatrix.SetMatrix(g_viewPole.CalcMatrix());
                Matrix4 worldToCamMat = modelMatrix.Top();

                LightBlock lightData = new LightBlock(NUMBER_OF_LIGHTS);

                lightData.ambientIntensity = new Vector4(0.2f, 0.2f, 0.2f, 1.0f);
                lightData.lightAttenuation = g_fLightAttenuation;

                Vector3 globalLightDirection = new Vector3(0.707f, 0.707f, 0.0f);

                lightData.lights[0].cameraSpaceLightPos =
                    Vector4.Transform(new Vector4(globalLightDirection, 0.0f), worldToCamMat);
                lightData.lights[0].lightIntensity = new Vector4(0.6f, 0.6f, 0.6f, 1.0f);

                lightData.lights[1].cameraSpaceLightPos = Vector4.Transform(CalcLightPosition(), worldToCamMat);
                lightData.lights[1].lightIntensity = new Vector4(0.4f, 0.4f, 0.4f, 1.0f);

                g_litShaderProg.lightBlock.Update(lightData);
                g_litTextureProg.lightBlock.Update(lightData);

                using ( PushStack pushstack = new PushStack(modelMatrix))
                {
                    modelMatrix.ApplyMatrix(g_objtPole.CalcMatrix());
                    modelMatrix.Scale(2.0f);

                    Matrix3 normMatrix = new Matrix3(modelMatrix.Top());
                    normMatrix.Transpose();
                    //TEST
                    normMatrix = Matrix3.Identity;
                    //normMatrix = glm::transpose(glm::inverse(normMatrix));

                    ProgramData prog = g_bUseTexture ? g_litTextureProg : g_litShaderProg;

                    GL.UseProgram(prog.theProgram);
                    Matrix4 mm = modelMatrix.Top();
                    GL.UniformMatrix4(prog.modelToCameraMatrixUnif, false, ref mm);
                    GL.UniformMatrix3(prog.normalModelToCameraMatrixUnif, false, ref normMatrix);

                    GL.ActiveTexture(TextureUnit.Texture0 + g_gaussTexUnit);
                    GL.BindTexture(TextureTarget.Texture1D, g_gaussTextures[g_currTexture]);
                    GL.BindSampler(g_gaussTexUnit, g_gaussSampler);

                    g_pObjectMesh.Render("lit");

                    GL.BindSampler(g_gaussTexUnit, 0);
                    GL.BindTexture(TextureTarget.Texture1D, 0);

                    GL.UseProgram(0);
                }

                if(g_bDrawLights)
                {
                    using (PushStack pushstack = new PushStack(modelMatrix))
                    {
                        modelMatrix.Translate(new Vector3(CalcLightPosition()));
                        modelMatrix.Scale(0.25f);

                        GL.UseProgram(g_Unlit.theProgram);
                        Matrix4 mm = modelMatrix.Top();
                        GL.UniformMatrix4(g_Unlit.modelToCameraMatrixUnif, false, ref mm);

                        Vector4 lightColor = new Vector4(1f, 1f, 1f, 1f);
                        GL.Uniform4(g_Unlit.objectColorUnif, ref lightColor);
                        g_pCubeMesh.Render("flat");
                    }

                    modelMatrix.Translate(globalLightDirection * 100.0f);
                    modelMatrix.Scale(5.0f);

                    Matrix4 mm2 = modelMatrix.Top();
                    GL.UniformMatrix4(g_Unlit.modelToCameraMatrixUnif, false, ref mm2);
                    g_pCubeMesh.Render("flat");

                    GL.UseProgram(0);
                }

                if(g_bDrawCameraPos)
                {
                    using (PushStack pushstack = new PushStack(modelMatrix))
                    {
                        modelMatrix.SetIdentity();
                        modelMatrix.Translate(new Vector3(0.0f, 0.0f, -g_viewPole.GetView().radius));
                        modelMatrix.Scale(0.25f);

                        GL.Disable(EnableCap.DepthTest);
                        GL.DepthMask(false);
                        GL.UseProgram(g_Unlit.theProgram);
                        Matrix4 mm = modelMatrix.Top();
                        GL.UniformMatrix4(g_Unlit.modelToCameraMatrixUnif, false, ref mm);
                        GL.Uniform4(g_Unlit.objectColorUnif, 0.25f, 0.25f, 0.25f, 1.0f);
                        g_pCubeMesh.Render("flat");
                        GL.DepthMask(true);
                        GL.Enable(EnableCap.DepthTest);
                        GL.Uniform4(g_Unlit.objectColorUnif, 1.0f, 1.0f, 1.0f, 1.0f);
                        g_pCubeMesh.Render("flat");
                    }
                }
            }
        }
        public override void display()
        {
            g_LightTimer.Update();
            ClearDisplay();
            if((g_pPlaneMesh != null) && (g_pCylinderMesh != null) && ( g_pCubeMesh != null))
            {
                MatrixStack modelMatrix = new MatrixStack();
                modelMatrix.SetMatrix(g_viewPole.CalcMatrix());

                Vector4 worldLightPos = CalcLightPosition();

                Vector4 lightPosCameraSpace = Vector4.Transform(worldLightPos, modelMatrix.Top());

                ProgramData pWhiteProgram;
                ProgramData pVertColorProgram;

                if(g_bUseFragmentLighting)
                {
                    pWhiteProgram = g_FragWhiteDiffuseColor;
                    pVertColorProgram = g_FragVertexDiffuseColor;
                }
                else
                {
                    pWhiteProgram = g_WhiteDiffuseColor;
                    pVertColorProgram = g_VertexDiffuseColor;
                }

                GL.UseProgram(pWhiteProgram.theProgram);
                GL.Uniform4(pWhiteProgram.lightIntensityUnif, 0.8f, 0.8f, 0.8f, 1.0f);
                GL.Uniform4(pWhiteProgram.ambientIntensityUnif, 0.2f, 0.2f, 0.2f, 1.0f);
             	GL.UniformMatrix4(pWhiteProgram.cameraToClipMatrixUnif, false, ref projData.cameraToClipMatrix);
                GL.UseProgram(pVertColorProgram.theProgram);
                GL.Uniform4(pVertColorProgram.lightIntensityUnif, 0.8f, 0.8f, 0.8f, 1.0f);
                GL.Uniform4(pVertColorProgram.ambientIntensityUnif, 0.2f, 0.2f, 0.2f, 1.0f);
                GL.UniformMatrix4(pVertColorProgram.cameraToClipMatrixUnif, false, ref projData.cameraToClipMatrix);

                GL.UseProgram(0);

                //Render the ground plane.
                using (PushStack pushstack = new PushStack(modelMatrix))
                {
                    GL.UseProgram(pWhiteProgram.theProgram);
                    Matrix4 mm = modelMatrix.Top();
                    GL.UniformMatrix4(pWhiteProgram.modelToCameraMatrixUnif, false, ref mm);

                    Matrix4 invTransform = modelMatrix.Top().Inverted();
                    Vector4 lightPosModelSpace = Vector4.Transform(lightPosCameraSpace, invTransform);
                    Vector3 lightPos = new Vector3(lightPosModelSpace.X, lightPosModelSpace.Y, lightPosModelSpace.Z);
                    GL.Uniform3(pWhiteProgram.modelSpaceLightPosUnif, ref lightPos);

                    g_pPlaneMesh.Render();
                    GL.UseProgram(0);
                }

                //Render the Cylinder
                using (PushStack pushstack = new PushStack(modelMatrix))
                {
                    modelMatrix.ApplyMatrix(g_objtPole.CalcMatrix());
                    //modelMatrix.Translate(new Vector3(0f, 0f, 10f));
                    coloredCylinderModelmatrix = modelMatrix.Top();
                    if(g_bScaleCyl)
                        modelMatrix.Scale(1.0f, 1.0f, 0.2f);
                    Matrix4 mm = modelMatrix.Top();
                    Matrix4 invTransform = modelMatrix.Top().Inverted();
                    Vector4 lightPosModelSpace = Vector4.Transform(lightPosCameraSpace,  invTransform);

                    if(g_bDrawColoredCyl)
                    {
                        GL.UseProgram(pVertColorProgram.theProgram);

                        GL.UniformMatrix4(pVertColorProgram.modelToCameraMatrixUnif, false, ref mm);
                        Vector3 lightPos = new Vector3(lightPosModelSpace.X, lightPosModelSpace.Y, lightPosModelSpace.Z);
                        GL.Uniform3(pVertColorProgram.modelSpaceLightPosUnif, ref lightPos);

                        GL.UniformMatrix4(pVertColorProgram.cameraToClipMatrixUnif, false, ref projData.cameraToClipMatrix);

                        g_pCylinderMesh.Render("lit-color");
                    }
                    else
                    {
                        GL.UseProgram(pWhiteProgram.theProgram);
                        GL.UniformMatrix4(pWhiteProgram.modelToCameraMatrixUnif, false, ref mm);
                        Vector3 lightPos = new Vector3(lightPosModelSpace.X, lightPosModelSpace.Y, lightPosModelSpace.Z);
                        GL.Uniform3(pWhiteProgram.modelSpaceLightPosUnif, ref lightPos);

                        GL.UniformMatrix4(pWhiteProgram.cameraToClipMatrixUnif, false, ref projData.cameraToClipMatrix);

                        g_pCylinderMesh.Render("lit");
                    }
                    GL.UseProgram(0);
                }

                //Render the light
                if(g_bDrawLight)
                {
                    using (PushStack pushstack = new PushStack(modelMatrix))
                    {
                        modelMatrix.Translate(new Vector3(worldLightPos));
                        modelMatrix.Scale(0.1f, 0.1f, 0.1f);

                        GL.UseProgram(g_Unlit.theProgram);
                        Matrix4 mm = modelMatrix.Top();
                        GL.UniformMatrix4(g_Unlit.modelToCameraMatrixUnif, false, ref mm);
                        GL.Uniform4(g_Unlit.baseColorUnif, 0.8078f, 0.8706f, 0.9922f, 1.0f);

                        GL.UniformMatrix4(g_Unlit.cameraToClipMatrixUnif, false, ref projData.cameraToClipMatrix);

                        g_pCubeMesh.Render("flat");
                    }
                }
            }
        }
        public override void display()
        {
            if(g_pScene == null)
                return;

            g_lights.UpdateTime();

            float gamma = g_isGammaCorrect ? g_gammaValue : 1.0f;

            Vector4 bkg = g_lights.GetBackgroundColor();
            GL.ClearColor(bkg[0], bkg[1], bkg[2], bkg[3]);
            GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit);

            MatrixStack modelMatrix = new MatrixStack();
            modelMatrix.SetMatrix(g_viewPole.CalcMatrix());

            Matrix4 worldToCamMat = modelMatrix.Top();
            LightBlock lightData = g_lights.GetLightInformationGamma(worldToCamMat);

            lightData.gamma = gamma;

            foreach (SceneProgramData spd in g_Programs)
            {
                spd.lightBlock.Update(lightData);
            }

            if(g_pScene != null)
            {
                using ( PushStack pushstack = new PushStack(modelMatrix))
                {
                    g_pScene.Draw(modelMatrix, g_materialBlockIndex, g_lights.GetTimerValue("tetra"));
                }
            }

            //Render the sun
            if (renderSun)
            {
                using ( PushStack pushstack = new PushStack(modelMatrix))
                {
                    Vector3 sunlightDir = new Vector3(g_lights.GetSunlightDirection());
                    modelMatrix.Translate(sunlightDir * 500.0f);
                    modelMatrix.Scale(30.0f, 30.0f, 30.0f);

                    GL.UseProgram(g_Unlit.theProgram);
                    Matrix4 mm = modelMatrix.Top();
                    GL.UniformMatrix4(g_Unlit.modelToCameraMatrixUnif, false, ref mm);

                    Vector4 lightColor = GammaCorrect(g_lights.GetSunlightIntensity(), gamma);
                    GL.Uniform4(g_Unlit.objectColorUnif, lightColor);
                    g_pScene.GetSphereMesh().Render("flat");
                }
            }

            //Render the lights
            if(g_bDrawLights)
            {
                for(int light = 0; light < g_lights.GetNumberOfPointLights(); light++)
                {
                    using (PushStack pushstack = new PushStack(modelMatrix))
                    {
                        modelMatrix.Translate(g_lights.GetWorldLightPosition(light));

                        GL.UseProgram(g_Unlit.theProgram);
                        Matrix4 mm = modelMatrix.Top();
                        GL.UniformMatrix4(g_Unlit.modelToCameraMatrixUnif, false, ref mm);

                        Vector4 lightColor = GammaCorrect(g_lights.GetPointLightIntensity(light), gamma);
                        GL.Uniform4(g_Unlit.objectColorUnif, lightColor);
                        g_pScene.GetCubeMesh().Render("flat");
                    }
                }
            }

            if(g_bDrawCameraPos)
            {
                using ( PushStack pushstack = new PushStack(modelMatrix))
                {
                    modelMatrix.SetIdentity();
                    modelMatrix.Translate(new Vector3(0.0f, 0.0f, -g_viewPole.GetView().radius));

                    GL.Disable(EnableCap.DepthTest);
                    GL.DepthMask(false);
                    GL.UseProgram(g_Unlit.theProgram);
                    Matrix4 mm = modelMatrix.Top();
                    GL.UniformMatrix4(g_Unlit.modelToCameraMatrixUnif, false, ref mm);
                    GL.Uniform4(g_Unlit.objectColorUnif, 0.25f, 0.25f, 0.25f, 1.0f);
                    g_pScene.GetCubeMesh().Render("flat");
                    GL.DepthMask(true);
                    GL.Enable(EnableCap.DepthTest);
                    GL.Uniform4(g_Unlit.objectColorUnif, 1.0f, 1.0f, 1.0f, 1.0f);
                    g_pScene.GetCubeMesh().Render("flat");
                }
            }
        }
Ejemplo n.º 7
0
 private void ChangePlayerView()
 {
     MatrixStack camMatrix = new MatrixStack();
     camMatrix.SetMatrix(Camera.GetLookAtMatrix());
     worldToCameraMatrix = camMatrix.Top();
     Matrix4 rotX = Matrix4.CreateRotationX(playerRotations[playerNumber].X * (float)Math.PI / 180f);
     Matrix4 rotY = Matrix4.CreateRotationY(playerRotations[playerNumber].Y * (float)Math.PI / 180f);
     Matrix4 rotZ = Matrix4.CreateRotationZ(playerRotations[playerNumber].Z * (float)Math.PI / 180f);
     //worldToCameraMatrix = Matrix4.Mult(worldToCameraMatrix, rot);
     worldToCameraMatrix = Matrix4.Mult(rotX, worldToCameraMatrix);
     worldToCameraMatrix = Matrix4.Mult(rotY, worldToCameraMatrix);
     worldToCameraMatrix = Matrix4.Mult(rotZ, worldToCameraMatrix);
     //cameraToClipMatrix = Matrix4.Mult(cameraToClipMatrix, rot);
     //cameraToClipMatrix = Matrix4.Mult(rot, cameraToClipMatrix);
     SetGlobalMatrices(currentProgram);
 }
        //Called whenever the window is resized. The new window size is given, in pixels.
        //This is an opportunity to call glViewport or glScissor to keep up with the change in size.
        public override void reshape()
        {
            MatrixStack camMatrix = new MatrixStack();
            camMatrix.SetMatrix(Camera.GetLookAtMatrix());

            cm = camMatrix.Top();

            MatrixStack persMatrix = new MatrixStack();
            persMatrix.Perspective(45.0f, (width / (float)height), g_fzNear, g_fzFar);
            pm = persMatrix.Top();

            SetGlobalMatrices(UniformColor);
            SetGlobalMatrices(ObjectColor);
            SetGlobalMatrices(UniformColorTint);

            GL.Viewport(0, 0, width, height);
        }