Exemplo n.º 1
0
        protected override void OnLoad(EventArgs e)
        {
            // On  load methods to  reading shaders ,models , textures ...
            GL.ClearColor(Color4.Black);
            GL.Enable(EnableCap.DepthTest);
            Camera.Type = CameraType.FreeCam;

            mShader = new ShaderUtility(@"ACW/Shaders/vSdr.vert", @"ACW/Shaders/fLight.frag");

            mSphereModel   = ModelUtility.LoadModel(@"Utility/Models/sphere.bin");
            mCubeModel     = ModelUtility.LoadModel(@"Utility/Models/MainCube.sjg");
            mCylinderModel = ModelUtility.LoadModel(@"Utility/Models/cylinder.bin");


            int vPositionLocation = GL.GetAttribLocation(mShader.ShaderProgramID, "vPosition");
            int vNormalLocation   = GL.GetAttribLocation(mShader.ShaderProgramID, "vNormal");
            int vTexCoords        = GL.GetAttribLocation(mShader.ShaderProgramID, "vTexCoords");

            int size;

            GL.UseProgram(mShader.ShaderProgramID);

            GL.GenVertexArrays(mVAO.Length, mVAO);
            GL.GenBuffers(mVBO.Length, mVBO);

            //Textures
            Txt1 = new Texture(@"ACW/Shaders/tx2.jpg", TextureUnit.Texture0);
            Txt2 = new Texture(@"ACW/Shaders/tx.jpg", TextureUnit.Texture1);

            #region Spheres
            GL.BindVertexArray(mVAO[0]);
            GL.BindBuffer(BufferTarget.ArrayBuffer, mVBO[0]);
            GL.BufferData(BufferTarget.ArrayBuffer, (IntPtr)(mSphereModel.Vertices.Length * sizeof(float)), mSphereModel.Vertices, BufferUsageHint.StaticDraw);
            GL.BindBuffer(BufferTarget.ElementArrayBuffer, mVBO[1]);
            GL.BufferData(BufferTarget.ElementArrayBuffer, (IntPtr)(mSphereModel.Indices.Length * sizeof(float)), mSphereModel.Indices, BufferUsageHint.StaticDraw);

            GL.GetBufferParameter(BufferTarget.ArrayBuffer, BufferParameterName.BufferSize, out size);
            if (mSphereModel.Vertices.Length * sizeof(float) != size)
            {
                throw new ApplicationException("Vertex data not loaded onto graphics card correctly");
            }

            GL.GetBufferParameter(BufferTarget.ElementArrayBuffer, BufferParameterName.BufferSize, out size);
            if (mSphereModel.Indices.Length * sizeof(float) != size)
            {
                throw new ApplicationException("Index data not loaded onto graphics card correctly");
            }

            GL.EnableVertexAttribArray(vPositionLocation);
            GL.VertexAttribPointer(vPositionLocation, 3, VertexAttribPointerType.Float, false, 6 * sizeof(float), 0);

            GL.EnableVertexAttribArray(vNormalLocation);
            GL.VertexAttribPointer(vNormalLocation, 3, VertexAttribPointerType.Float, true, 6 * sizeof(float), 3 * sizeof(float));
            #endregion // Binds Spheres

            #region Cubes
            GL.BindVertexArray(mVAO[1]);
            GL.BindBuffer(BufferTarget.ArrayBuffer, mVBO[2]);
            GL.BufferData(BufferTarget.ArrayBuffer, (IntPtr)(mCubeModel.Vertices.Length * sizeof(float)), mCubeModel.Vertices, BufferUsageHint.StaticDraw);
            GL.BindBuffer(BufferTarget.ElementArrayBuffer, mVBO[3]);
            GL.BufferData(BufferTarget.ElementArrayBuffer, (IntPtr)(mCubeModel.Indices.Length * sizeof(float)), mCubeModel.Indices, BufferUsageHint.StaticDraw);

            GL.GetBufferParameter(BufferTarget.ArrayBuffer, BufferParameterName.BufferSize, out size);
            if (mCubeModel.Vertices.Length * sizeof(float) != size)
            {
                throw new ApplicationException("Vertex data not loaded onto graphics card correctly");
            }

            GL.GetBufferParameter(BufferTarget.ElementArrayBuffer, BufferParameterName.BufferSize, out size);
            if (mCubeModel.Indices.Length * sizeof(float) != size)
            {
                throw new ApplicationException("Index data not loaded onto graphics card correctly");
            }

            GL.EnableVertexAttribArray(vPositionLocation);
            GL.VertexAttribPointer(vPositionLocation, 3, VertexAttribPointerType.Float, false, 8 * sizeof(float), 0);

            GL.EnableVertexAttribArray(vNormalLocation);
            GL.VertexAttribPointer(vNormalLocation, 3, VertexAttribPointerType.Float, true, 8 * sizeof(float), 3 * sizeof(float));

            GL.EnableVertexAttribArray(vTexCoords);
            GL.VertexAttribPointer(vTexCoords, 2, VertexAttribPointerType.Float, true, 8 * sizeof(float), 6 * sizeof(float));
            #endregion  // Bind Cubes

            #region Cylinders
            GL.BindVertexArray(mVAO[2]);
            GL.BindBuffer(BufferTarget.ArrayBuffer, mVBO[4]);
            GL.BufferData(BufferTarget.ArrayBuffer, (IntPtr)(mCylinderModel.Vertices.Length * sizeof(float)), mCylinderModel.Vertices, BufferUsageHint.StaticDraw);
            GL.BindBuffer(BufferTarget.ElementArrayBuffer, mVBO[5]);
            GL.BufferData(BufferTarget.ElementArrayBuffer, (IntPtr)(mCylinderModel.Indices.Length * sizeof(float)), mCylinderModel.Indices, BufferUsageHint.StaticDraw);

            GL.GetBufferParameter(BufferTarget.ArrayBuffer, BufferParameterName.BufferSize, out size);
            if (mCylinderModel.Vertices.Length * sizeof(float) != size)
            {
                throw new ApplicationException("Vertex data not loaded onto graphics card correctly");
            }

            GL.GetBufferParameter(BufferTarget.ElementArrayBuffer, BufferParameterName.BufferSize, out size);
            if (mCylinderModel.Indices.Length * sizeof(float) != size)
            {
                throw new ApplicationException("Index data not loaded onto graphics card correctly");
            }

            GL.EnableVertexAttribArray(vPositionLocation);
            GL.VertexAttribPointer(vPositionLocation, 3, VertexAttribPointerType.Float, false, 6 * sizeof(float), 0);

            GL.EnableVertexAttribArray(vNormalLocation);
            GL.VertexAttribPointer(vNormalLocation, 3, VertexAttribPointerType.Float, true, 6 * sizeof(float), 3 * sizeof(float));
            #endregion


            //Checking acctivity o gravity is on
            if (gravityAcceleration.Y == 0)
            {
                gravityOn = false;
            }
            else
            {
                gravityOn = true;
            }

            int uView = GL.GetUniformLocation(mShader.ShaderProgramID, "uView");
            GL.UniformMatrix4(uView, true, ref mView);

            //Manages the projection for resizing the window
            int     uProjectionLocation = GL.GetUniformLocation(mShader.ShaderProgramID, "uProjection");
            Matrix4 projection          = Matrix4.CreatePerspectiveFieldOfView(1, (float)ClientRectangle.Width / ClientRectangle.Height, 0.5f, viewDistance);
            GL.UniformMatrix4(uProjectionLocation, true, ref projection);


            Light.Inside();
            Level.Inside();
            EmitterBox.Inside();
            SpotLight.Inside();



            GL.BindVertexArray(0);

            base.OnLoad(e);
        }