//Main Methods protected override void OnRenderFrame(FrameEventArgs e) { GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit); uModelLocation = GL.GetUniformLocation(mShader.ShaderProgramID, "uModel"); float viewDistance = 200; //Resets matrix 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); //Renders the objects to the screen buffer Frame_Buffer.Unbind_Buffer(); Render_Objects(); GL.BindVertexArray(0); this.SwapBuffers(); projection = Matrix4.CreatePerspectiveFieldOfView(1, Bottom_Portal.mWidth / Bottom_Portal.mHeight, 0.5f, viewDistance); GL.UniformMatrix4(uProjectionLocation, true, ref projection); //Renders to Texture for the portal RenderToTexture_Bottom_Portal(); RenderToTexture_Top_Portal(); } //Draw
//Renders to Texture private void RenderToTexture_Bottom_Portal() { //Bind Bottom_Portal.Bind_Buffer(); GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit); Portal_ViewportUpdate(); GL.Enable(EnableCap.CullFace); int uView = GL.GetUniformLocation(mShader.ShaderProgramID, "uView"); Matrix4 previousView = mView; mView = mTopPortalView; GL.UniformMatrix4(uView, true, ref mView); camera.UpdateLightPositions(); Material.silver.Assign_Material(); BrickWall.MakeActive(); //Draws Cubes GL.BindVertexArray(mVAO[1]); DrawTopBox(mBoxMatrix * mGroundModel * Level.Level0); DrawBox(mBoxMatrix * mGroundModel * Level.Level1); DrawBox(mBoxMatrix * mGroundModel * Level.Level2); DrawBottomBox(mBoxMatrix * mGroundModel * Level.Level3); //Draws the Spheres GL.BindVertexArray(mVAO[0]); foreach (Sphere s in Sphere.DrawList) { s.Draw(); } //This stops the back of cylinders from not being drawn GL.Disable(EnableCap.CullFace); //Draws Cylinders GL.BindVertexArray(mVAO[2]); foreach (Cylinder c in Level.Level1_Cylinders) //LEVEL 1 { Level.Draw_Cylinders(c, Level.Level1); } mView = previousView; GL.UniformMatrix4(uView, true, ref mView); //Unbind Frame_Buffer.Unbind_Buffer(); }
private void RenderToTexture_Top_Portal() { //Bind Top_Portal.Bind_Buffer(); GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit); Portal_ViewportUpdate(); GL.Enable(EnableCap.CullFace); int uView = GL.GetUniformLocation(mShader.ShaderProgramID, "uView"); Matrix4 previousView = mView; mView = mBottomPortalView; GL.UniformMatrix4(uView, true, ref mView); camera.UpdateLightPositions(); //Draws the Spheres GL.BindVertexArray(mVAO[0]); foreach (Sphere s in Sphere.DrawList) { s.Draw(); } GL.BindVertexArray(mVAO[1]); DrawTopBox(mBoxMatrix * mGroundModel * Level.Level0); DrawBox(mBoxMatrix * mGroundModel * Level.Level1); DrawBox(mBoxMatrix * mGroundModel * Level.Level2); DrawBottomBox(mBoxMatrix * mGroundModel * Level.Level3); //SoD GL.BindVertexArray(mVAO[0]); Level.DoomSphere.Apply_MaterialValues(); Matrix4 DoomSphereLocation = Matrix4.CreateScale(Level.DoomSphere.mRadius) * Matrix4.CreateTranslation(Level.DoomSphere.mPosition) * Level.Level3 * mGroundModel; GL.UniformMatrix4(uModelLocation, true, ref DoomSphereLocation); GL.DrawElements(BeginMode.Triangles, mSphereModel.Indices.Length, DrawElementsType.UnsignedInt, 0); mView = previousView; GL.UniformMatrix4(uView, true, ref mView); //Unbind Frame_Buffer.Unbind_Buffer(); }
} //Update protected override void OnLoad(EventArgs e) { //Init GL.ClearColor(Color4.Black); GL.Enable(EnableCap.DepthTest); Camera.Type = CameraType.Static; mShader = new ShaderUtility(@"ACW/Shaders/s.vert", @"ACW/Shaders/Light.frag"); mSphereModel = ModelUtility.LoadModel(@"Utility/Models/sphere.bin"); mCubeModel = ModelUtility.LoadModel(@"Utility/Models/MainCube.sjg"); mCylinderModel = ModelUtility.LoadModel(@"Utility/Models/cylinder.bin"); mParticleCube = ModelUtility.LoadModel(@"Utility/Models/Particle_Cube.sjg"); 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 BrickWall = new Texture(@"ACW/Textures/BrickWall.jpg", TextureUnit.Texture0); //Frame buffers Bottom_Portal = new Frame_Buffer(TextureUnit.Texture2, FramebufferAttachment.ColorAttachment0); Top_Portal = new Frame_Buffer(TextureUnit.Texture3, FramebufferAttachment.ColorAttachment0); #region Sphere 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 #region Cube 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 #region Cylinder 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 #region General Init //Checks if 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); #endregion //---------------INIT-----------------// Light.Init(); Level.Init(); Emitter.Init(); SpotLight.Init(); float spawnRate = 0.01f; float lifetime = 2f; ActiveParticleSystems.Add(new Floating_CubeSystem(mParticleCube, new Vector3(-100, -100, 100), 200, 200, 200, lifetime, spawnRate, 100, Material.white)); //1 ActiveParticleSystems.Add(new Floating_CubeSystem(mParticleCube, new Vector3(-5, -15, 5), 10, 10, lifetime, 0.2f, 100, Material.crimson_Red)); //1 //------------------------------------// GL.BindVertexArray(0); base.OnLoad(e); }