Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Diagnostics;
- using System.Runtime.InteropServices;
- using System.Drawing;
- using OpenTK;
- using OpenTK.Graphics;
- using OpenTK.Graphics.OpenGL;
- using GLEx.Buffers;
- using GLEx.Misc;
- using GLEx.Shaders;
- using GLEx.Shapes;
- using GLEx.Views;
- using GLEx.Lights;
- using GLEx.Textures;
- namespace TestProject
- {
- public class TestWindow : GameWindow
- {
- private Camera viewpoint;
- private Mesh mFloor, mBall;
- private ShaderProgram sNormal, sNormalT, sOverlay, sDebug, sLight, sWorld, sWorld2, sAmbient;
- private RenderTarget rtNormalMap, rtLightingPass;
- private Spotlight lTest1, lTest2, lTest3;
- private Scene scene;
- private Texture2D texBorder;
- private Texture2D texFloor, texFloorN;
- private Texture2D texBall, texBallN;
- private Point pCenter;
- private Vector2 vMouseDelta;
- private bool bForward, bBack;
- private Stopwatch timer;
- private static GraphicsMode Mode
- {
- get
- {
- return new GraphicsMode(new ColorFormat(32), 24, 8, 8);
- }
- }
- public TestWindow() : base(1024, 600, Mode, "GLEx Test Project", GameWindowFlags.Default) { }
- protected override void OnLoad(EventArgs e)
- {
- GL.Enable(EnableCap.DepthTest);
- GL.Disable(EnableCap.CullFace);
- GL.ClearColor(Color4.SkyBlue);
- this.VSync = VSyncMode.On;
- viewpoint = new Camera();
- viewpoint.UpdateProjection(this.Width, this.Height, (float)(Math.PI / 3d));
- viewpoint.Position = new Vector3(0f, 1.5f, 0f);
- viewpoint.Angle = new Vector3((float)(-Math.PI / 6d), (float)(-Math.PI / 4d), 0f);
- #region Shaders
- sNormal = ShaderFactory.CreateNormalMapShader(false);
- sNormalT = ShaderFactory.CreateNormalMapShader(true);
- sOverlay = ShaderFactory.CreateOverlayShader(OverlayType.Normal);
- sDebug = ShaderFactory.CreateDebugShader();
- sLight = ShaderFactory.CreateDeferredLightShader();
- sWorld = ShaderFactory.CreateSolidColourShader();
- sWorld2 = ShaderFactory.CreateSolidTextureShader();
- sAmbient = ShaderFactory.CreateSolidColourShader();
- #endregion
- #region Meshes
- mFloor = new Mesh();
- mFloor.Generate();
- mFloor.Usage = BufferUsageHint.StaticDraw;
- mFloor.Shader = sNormal;
- mFloor.BufferData(new Vector3[] {
- new Vector3(0f, 0f, 0f),
- new Vector3(0f, 0f, -5f),
- new Vector3(5f, 0f, -5f),
- new Vector3(5f, 0f, 0f)
- }, "inPosition");
- mFloor.BufferData(new Vector3[] {
- new Vector3(0f, 1f, 0f),
- new Vector3(0f, 1f, 0f),
- new Vector3(0f, 1f, 0f),
- new Vector3(0f, 1f, 0f)
- }, "inNormal");
- mFloor.BufferData(new Vector2[] {
- new Vector2(2f, 0f),
- new Vector2(2f, 2f),
- new Vector2(0f, 2f),
- new Vector2(0f, 0f)
- }, "inUV");
- mFloor.BufferIndices(new uint[] {
- 2, 1, 0,
- 3, 2, 0
- });
- mBall = Sphere.Build(sNormal, "inPosition", "inNormal", "inUV");
- mBall.Usage = BufferUsageHint.StaticDraw;
- FullscreenQuad.Viewpoint = viewpoint;
- FullscreenQuad.Initialise(this.Width, this.Height);
- #endregion
- #region Render Targets
- rtNormalMap = new RenderTarget();
- rtNormalMap.Width = this.Width;
- rtNormalMap.Height = this.Height;
- rtNormalMap.UsePositionTexture = true;
- rtNormalMap.Setup();
- rtLightingPass = new RenderTarget();
- rtLightingPass.Width = this.Width;
- rtLightingPass.Height = this.Height;
- rtLightingPass.Setup();
- #endregion
- #region Lights
- lTest1 = new Spotlight();
- lTest1.Position = new Vector3(0.5f, 0.15f, -0.5f);
- lTest1.Angle = new Vector3(0f, MathHelper.PiOver2 + MathHelper.PiOver4, 0f);
- lTest1.DiffuseColour = Color4.Red;
- lTest1.CastShadows = false;
- lTest1.CameraView = viewpoint;
- lTest1.SetupView();
- lTest1.UpdateView();
- lTest2 = new Spotlight();
- lTest2.Position = new Vector3(0.5f, 0.15f, -4.5f);
- lTest2.Angle = new Vector3(0f, MathHelper.PiOver4, 0f);
- lTest2.DiffuseColour = Color4.Green;
- lTest2.CastShadows = false;
- lTest2.CameraView = viewpoint;
- lTest2.SetupView();
- lTest2.UpdateView();
- lTest3 = new Spotlight();
- lTest3.Position = new Vector3(4.5f, 0.15f, -2.5f);
- lTest3.Angle = new Vector3(0f, MathHelper.ThreePiOver2, 0f);
- lTest3.DiffuseColour = Color4.Blue;
- lTest3.CastShadows = false;
- lTest3.CameraView = viewpoint;
- lTest3.SetupView();
- lTest3.UpdateView();
- #endregion
- #region Input
- System.Windows.Forms.Cursor.Hide();
- this.Keyboard.KeyDown += (o, e2) =>
- {
- if (e2.Key == OpenTK.Input.Key.Escape)
- {
- this.Close();
- return;
- }
- if (e2.Key == OpenTK.Input.Key.W) bForward = true;
- if (e2.Key == OpenTK.Input.Key.S) bBack = true;
- };
- this.Keyboard.KeyUp += (o, e2) =>
- {
- if (e2.Key == OpenTK.Input.Key.W) bForward = false;
- if (e2.Key == OpenTK.Input.Key.S) bBack = false;
- if (e2.Key == OpenTK.Input.Key.P)
- {
- System.Diagnostics.Debug.WriteLine("Camera Pos: " + viewpoint.Position.ToString());
- System.Diagnostics.Debug.WriteLine("Camera Ang: " + viewpoint.Angle.ToString());
- System.Diagnostics.Debug.WriteLine("Light1 Pos: " + lTest1.Position.ToString());
- System.Diagnostics.Debug.WriteLine("Light1 Ang: " + lTest1.Angle.ToString());
- }
- };
- pCenter = new Point(Bounds.Left + (Bounds.Width / 2), Bounds.Top + (Bounds.Height / 2));
- vMouseDelta = new Vector2(0f, 0f);
- #endregion
- #region Textures
- texBorder = TextureFactory.LoadTexture("border.png");
- Spotlight.BorderTexture = texBorder;
- texFloor = TextureFactory.LoadTexture("CONCRETE.png");
- texFloorN = TextureFactory.LoadTexture("CONCRETE_NORM.png");
- texBall = TextureFactory.LoadTexture("BRICK1.png");
- texBallN = TextureFactory.LoadTexture("BRICK1_NORM.png");
- #endregion
- scene = new Scene(DrawScene);
- timer = new Stopwatch();
- timer.Start();
- }
- private void UpdateMousePosition()
- {
- Point pos = System.Windows.Forms.Cursor.Position;
- System.Windows.Forms.Cursor.Position = pCenter;
- vMouseDelta.X = pos.X - pCenter.X;
- vMouseDelta.Y = pos.Y - pCenter.Y;
- }
- protected override void OnUpdateFrame(FrameEventArgs e)
- {
- int forward = (bForward ? 1 : 0) - (bBack ? 1 : 0);
- viewpoint.Position += (viewpoint.Forward * forward * (float)e.Time);
- UpdateMousePosition();
- Vector3 oldang = viewpoint.Angle;
- oldang.Y -= vMouseDelta.X * 0.005f;
- oldang.X -= vMouseDelta.Y * 0.005f;
- oldang.X = (oldang.X < -MathHelper.PiOver2) ? (-MathHelper.PiOver2) : (oldang.X > MathHelper.PiOver2) ? MathHelper.PiOver2 : oldang.X;
- viewpoint.Angle = oldang;
- }
- #region Scene
- private void DrawFloor(RenderStage stage, View view)
- {
- ShaderProgram prog = null;
- if (stage == RenderStage.DiffuseMap) prog = sWorld2;
- if (stage == RenderStage.NormalMap) prog = sNormalT;
- if (stage == RenderStage.GeometryOnly) prog = sDebug;
- mFloor.Shader = prog;
- mFloor.BeginRender();
- view.PrepareShader(prog, ProjectionType.Perspective);
- prog.BindFragData("outColour", 0);
- if (stage == RenderStage.NormalMap)
- {
- texFloorN.Bind(TextureUnit.Texture0);
- prog.SetUniform("texNormal", 0);
- prog.BindFragData("outPosition", 1);
- }
- if (stage == RenderStage.DiffuseMap)
- {
- texFloor.Bind(TextureUnit.Texture0);
- prog.SetUniform("texDiffuse", 0);
- }
- mFloor.FinishRender();
- }
- private void DrawBall(RenderStage stage, View view)
- {
- ShaderProgram prog = null;
- if (stage == RenderStage.DiffuseMap) prog = sWorld2;
- if (stage == RenderStage.NormalMap) prog = sNormalT;
- if (stage == RenderStage.GeometryOnly) prog = sDebug;
- view.PushMatrix();
- view.Translate(Matrix4.CreateTranslation(2.5f, 0f, -2.5f));
- mBall.Shader = prog;
- mBall.BeginRender();
- view.PrepareShader(prog, ProjectionType.Perspective);
- prog.BindFragData("outColour", 0);
- if (stage == RenderStage.NormalMap)
- {
- texBallN.Bind(TextureUnit.Texture0);
- prog.SetUniform("texNormal", 0);
- prog.BindFragData("outPosition", 1);
- }
- if (stage == RenderStage.DiffuseMap)
- {
- texBall.Bind(TextureUnit.Texture0);
- prog.SetUniform("texDiffuse", 0);
- }
- mBall.FinishRender();
- view.PopMatrix();
- }
- private void DrawScene(RenderStage stage, View view)
- {
- DrawFloor(stage, view);
- DrawBall(stage, view);
- }
- #endregion
- protected override void OnRenderFrame(FrameEventArgs e)
- {
- GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit);
- // Reset viewpoint
- viewpoint.ClearMatrixStack();
- viewpoint.ResetPerspective();
- // Enable 3D settings
- GL.Enable(EnableCap.DepthTest);
- GL.Enable(EnableCap.CullFace);
- GL.CullFace(CullFaceMode.Back);
- // Normal map pass
- rtNormalMap.Begin();
- scene.Render(RenderStage.NormalMap, viewpoint);
- rtNormalMap.Finish();
- // Diffuse pass (the main scene)
- scene.Render(RenderStage.DiffuseMap, viewpoint);
- // Prerender the lights (needed for shadow mapping)
- //lTest1.Prerender(scene);
- //lTest2.Prerender(scene);
- //lTest3.Prerender(scene);
- // Enable deferred lighting pass settings
- GL.Disable(EnableCap.DepthTest);
- GL.Disable(EnableCap.CullFace);
- rtLightingPass.Begin();
- // Ambient layer
- FullscreenQuad.Draw(sAmbient, (s) =>
- {
- s.SetUniform("colour", new Color4(25, 25, 25, 255));
- s.BindFragData("outColour", 0);
- }, BlendMode.Normal);
- // Bind lighting textures
- Spotlight.BeginRender(rtNormalMap);
- // Render the lights
- lTest1.Render();
- lTest2.Render();
- lTest3.Render();
- rtLightingPass.Finish();
- // Composite the scene
- FullscreenQuad.Draw(sOverlay, (s) =>
- {
- GL.ActiveTexture(TextureUnit.Texture0);
- rtLightingPass.BindColourTexture();
- s.SetUniform("texture0", 0);
- s.BindFragData("outColour", 0);
- }, BlendMode.Multiply);
- this.SwapBuffers();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment