thomasfn

Untitled

Sep 21st, 2011
148
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 12.22 KB | None | 0 0
  1. using System;
  2. using System.Diagnostics;
  3. using System.Runtime.InteropServices;
  4. using System.Drawing;
  5.  
  6. using OpenTK;
  7. using OpenTK.Graphics;
  8. using OpenTK.Graphics.OpenGL;
  9.  
  10. using GLEx.Buffers;
  11. using GLEx.Misc;
  12. using GLEx.Shaders;
  13. using GLEx.Shapes;
  14. using GLEx.Views;
  15. using GLEx.Lights;
  16. using GLEx.Textures;
  17.  
  18. namespace TestProject
  19. {
  20. public class TestWindow : GameWindow
  21. {
  22. private Camera viewpoint;
  23. private Mesh mFloor, mBall;
  24. private ShaderProgram sNormal, sNormalT, sOverlay, sDebug, sLight, sWorld, sWorld2, sAmbient;
  25. private RenderTarget rtNormalMap, rtLightingPass;
  26. private Spotlight lTest1, lTest2, lTest3;
  27. private Scene scene;
  28. private Texture2D texBorder;
  29. private Texture2D texFloor, texFloorN;
  30. private Texture2D texBall, texBallN;
  31.  
  32. private Point pCenter;
  33. private Vector2 vMouseDelta;
  34.  
  35. private bool bForward, bBack;
  36.  
  37. private Stopwatch timer;
  38.  
  39. private static GraphicsMode Mode
  40. {
  41. get
  42. {
  43. return new GraphicsMode(new ColorFormat(32), 24, 8, 8);
  44. }
  45. }
  46.  
  47. public TestWindow() : base(1024, 600, Mode, "GLEx Test Project", GameWindowFlags.Default) { }
  48.  
  49. protected override void OnLoad(EventArgs e)
  50. {
  51. GL.Enable(EnableCap.DepthTest);
  52. GL.Disable(EnableCap.CullFace);
  53. GL.ClearColor(Color4.SkyBlue);
  54.  
  55. this.VSync = VSyncMode.On;
  56.  
  57. viewpoint = new Camera();
  58. viewpoint.UpdateProjection(this.Width, this.Height, (float)(Math.PI / 3d));
  59. viewpoint.Position = new Vector3(0f, 1.5f, 0f);
  60. viewpoint.Angle = new Vector3((float)(-Math.PI / 6d), (float)(-Math.PI / 4d), 0f);
  61.  
  62. #region Shaders
  63.  
  64. sNormal = ShaderFactory.CreateNormalMapShader(false);
  65. sNormalT = ShaderFactory.CreateNormalMapShader(true);
  66. sOverlay = ShaderFactory.CreateOverlayShader(OverlayType.Normal);
  67. sDebug = ShaderFactory.CreateDebugShader();
  68. sLight = ShaderFactory.CreateDeferredLightShader();
  69. sWorld = ShaderFactory.CreateSolidColourShader();
  70. sWorld2 = ShaderFactory.CreateSolidTextureShader();
  71. sAmbient = ShaderFactory.CreateSolidColourShader();
  72.  
  73. #endregion
  74.  
  75. #region Meshes
  76.  
  77. mFloor = new Mesh();
  78. mFloor.Generate();
  79.  
  80. mFloor.Usage = BufferUsageHint.StaticDraw;
  81. mFloor.Shader = sNormal;
  82.  
  83. mFloor.BufferData(new Vector3[] {
  84. new Vector3(0f, 0f, 0f),
  85. new Vector3(0f, 0f, -5f),
  86. new Vector3(5f, 0f, -5f),
  87. new Vector3(5f, 0f, 0f)
  88. }, "inPosition");
  89. mFloor.BufferData(new Vector3[] {
  90. new Vector3(0f, 1f, 0f),
  91. new Vector3(0f, 1f, 0f),
  92. new Vector3(0f, 1f, 0f),
  93. new Vector3(0f, 1f, 0f)
  94. }, "inNormal");
  95. mFloor.BufferData(new Vector2[] {
  96. new Vector2(2f, 0f),
  97. new Vector2(2f, 2f),
  98. new Vector2(0f, 2f),
  99. new Vector2(0f, 0f)
  100. }, "inUV");
  101.  
  102. mFloor.BufferIndices(new uint[] {
  103. 2, 1, 0,
  104. 3, 2, 0
  105. });
  106.  
  107. mBall = Sphere.Build(sNormal, "inPosition", "inNormal", "inUV");
  108. mBall.Usage = BufferUsageHint.StaticDraw;
  109.  
  110. FullscreenQuad.Viewpoint = viewpoint;
  111. FullscreenQuad.Initialise(this.Width, this.Height);
  112.  
  113. #endregion
  114.  
  115. #region Render Targets
  116.  
  117. rtNormalMap = new RenderTarget();
  118. rtNormalMap.Width = this.Width;
  119. rtNormalMap.Height = this.Height;
  120. rtNormalMap.UsePositionTexture = true;
  121. rtNormalMap.Setup();
  122.  
  123. rtLightingPass = new RenderTarget();
  124. rtLightingPass.Width = this.Width;
  125. rtLightingPass.Height = this.Height;
  126. rtLightingPass.Setup();
  127.  
  128. #endregion
  129.  
  130. #region Lights
  131.  
  132. lTest1 = new Spotlight();
  133. lTest1.Position = new Vector3(0.5f, 0.15f, -0.5f);
  134. lTest1.Angle = new Vector3(0f, MathHelper.PiOver2 + MathHelper.PiOver4, 0f);
  135. lTest1.DiffuseColour = Color4.Red;
  136. lTest1.CastShadows = false;
  137. lTest1.CameraView = viewpoint;
  138. lTest1.SetupView();
  139. lTest1.UpdateView();
  140.  
  141. lTest2 = new Spotlight();
  142. lTest2.Position = new Vector3(0.5f, 0.15f, -4.5f);
  143. lTest2.Angle = new Vector3(0f, MathHelper.PiOver4, 0f);
  144. lTest2.DiffuseColour = Color4.Green;
  145. lTest2.CastShadows = false;
  146. lTest2.CameraView = viewpoint;
  147. lTest2.SetupView();
  148. lTest2.UpdateView();
  149.  
  150. lTest3 = new Spotlight();
  151. lTest3.Position = new Vector3(4.5f, 0.15f, -2.5f);
  152. lTest3.Angle = new Vector3(0f, MathHelper.ThreePiOver2, 0f);
  153. lTest3.DiffuseColour = Color4.Blue;
  154. lTest3.CastShadows = false;
  155. lTest3.CameraView = viewpoint;
  156. lTest3.SetupView();
  157. lTest3.UpdateView();
  158.  
  159. #endregion
  160.  
  161. #region Input
  162.  
  163. System.Windows.Forms.Cursor.Hide();
  164.  
  165. this.Keyboard.KeyDown += (o, e2) =>
  166. {
  167. if (e2.Key == OpenTK.Input.Key.Escape)
  168. {
  169. this.Close();
  170. return;
  171. }
  172. if (e2.Key == OpenTK.Input.Key.W) bForward = true;
  173. if (e2.Key == OpenTK.Input.Key.S) bBack = true;
  174. };
  175. this.Keyboard.KeyUp += (o, e2) =>
  176. {
  177. if (e2.Key == OpenTK.Input.Key.W) bForward = false;
  178. if (e2.Key == OpenTK.Input.Key.S) bBack = false;
  179.  
  180. if (e2.Key == OpenTK.Input.Key.P)
  181. {
  182. System.Diagnostics.Debug.WriteLine("Camera Pos: " + viewpoint.Position.ToString());
  183. System.Diagnostics.Debug.WriteLine("Camera Ang: " + viewpoint.Angle.ToString());
  184. System.Diagnostics.Debug.WriteLine("Light1 Pos: " + lTest1.Position.ToString());
  185. System.Diagnostics.Debug.WriteLine("Light1 Ang: " + lTest1.Angle.ToString());
  186. }
  187. };
  188.  
  189. pCenter = new Point(Bounds.Left + (Bounds.Width / 2), Bounds.Top + (Bounds.Height / 2));
  190. vMouseDelta = new Vector2(0f, 0f);
  191.  
  192. #endregion
  193.  
  194. #region Textures
  195.  
  196. texBorder = TextureFactory.LoadTexture("border.png");
  197. Spotlight.BorderTexture = texBorder;
  198.  
  199. texFloor = TextureFactory.LoadTexture("CONCRETE.png");
  200. texFloorN = TextureFactory.LoadTexture("CONCRETE_NORM.png");
  201.  
  202. texBall = TextureFactory.LoadTexture("BRICK1.png");
  203. texBallN = TextureFactory.LoadTexture("BRICK1_NORM.png");
  204.  
  205. #endregion
  206.  
  207. scene = new Scene(DrawScene);
  208.  
  209. timer = new Stopwatch();
  210. timer.Start();
  211. }
  212.  
  213. private void UpdateMousePosition()
  214. {
  215. Point pos = System.Windows.Forms.Cursor.Position;
  216. System.Windows.Forms.Cursor.Position = pCenter;
  217. vMouseDelta.X = pos.X - pCenter.X;
  218. vMouseDelta.Y = pos.Y - pCenter.Y;
  219. }
  220.  
  221. protected override void OnUpdateFrame(FrameEventArgs e)
  222. {
  223. int forward = (bForward ? 1 : 0) - (bBack ? 1 : 0);
  224. viewpoint.Position += (viewpoint.Forward * forward * (float)e.Time);
  225. UpdateMousePosition();
  226. Vector3 oldang = viewpoint.Angle;
  227. oldang.Y -= vMouseDelta.X * 0.005f;
  228. oldang.X -= vMouseDelta.Y * 0.005f;
  229. oldang.X = (oldang.X < -MathHelper.PiOver2) ? (-MathHelper.PiOver2) : (oldang.X > MathHelper.PiOver2) ? MathHelper.PiOver2 : oldang.X;
  230. viewpoint.Angle = oldang;
  231. }
  232.  
  233. #region Scene
  234.  
  235. private void DrawFloor(RenderStage stage, View view)
  236. {
  237. ShaderProgram prog = null;
  238. if (stage == RenderStage.DiffuseMap) prog = sWorld2;
  239. if (stage == RenderStage.NormalMap) prog = sNormalT;
  240. if (stage == RenderStage.GeometryOnly) prog = sDebug;
  241. mFloor.Shader = prog;
  242. mFloor.BeginRender();
  243. view.PrepareShader(prog, ProjectionType.Perspective);
  244. prog.BindFragData("outColour", 0);
  245. if (stage == RenderStage.NormalMap)
  246. {
  247. texFloorN.Bind(TextureUnit.Texture0);
  248. prog.SetUniform("texNormal", 0);
  249. prog.BindFragData("outPosition", 1);
  250. }
  251. if (stage == RenderStage.DiffuseMap)
  252. {
  253. texFloor.Bind(TextureUnit.Texture0);
  254. prog.SetUniform("texDiffuse", 0);
  255. }
  256. mFloor.FinishRender();
  257. }
  258.  
  259. private void DrawBall(RenderStage stage, View view)
  260. {
  261. ShaderProgram prog = null;
  262. if (stage == RenderStage.DiffuseMap) prog = sWorld2;
  263. if (stage == RenderStage.NormalMap) prog = sNormalT;
  264. if (stage == RenderStage.GeometryOnly) prog = sDebug;
  265. view.PushMatrix();
  266. view.Translate(Matrix4.CreateTranslation(2.5f, 0f, -2.5f));
  267. mBall.Shader = prog;
  268. mBall.BeginRender();
  269. view.PrepareShader(prog, ProjectionType.Perspective);
  270. prog.BindFragData("outColour", 0);
  271. if (stage == RenderStage.NormalMap)
  272. {
  273. texBallN.Bind(TextureUnit.Texture0);
  274. prog.SetUniform("texNormal", 0);
  275. prog.BindFragData("outPosition", 1);
  276. }
  277. if (stage == RenderStage.DiffuseMap)
  278. {
  279. texBall.Bind(TextureUnit.Texture0);
  280. prog.SetUniform("texDiffuse", 0);
  281. }
  282. mBall.FinishRender();
  283. view.PopMatrix();
  284. }
  285.  
  286. private void DrawScene(RenderStage stage, View view)
  287. {
  288. DrawFloor(stage, view);
  289. DrawBall(stage, view);
  290. }
  291.  
  292. #endregion
  293.  
  294. protected override void OnRenderFrame(FrameEventArgs e)
  295. {
  296. GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit);
  297.  
  298. // Reset viewpoint
  299. viewpoint.ClearMatrixStack();
  300. viewpoint.ResetPerspective();
  301.  
  302. // Enable 3D settings
  303. GL.Enable(EnableCap.DepthTest);
  304. GL.Enable(EnableCap.CullFace);
  305. GL.CullFace(CullFaceMode.Back);
  306.  
  307. // Normal map pass
  308. rtNormalMap.Begin();
  309. scene.Render(RenderStage.NormalMap, viewpoint);
  310. rtNormalMap.Finish();
  311.  
  312. // Diffuse pass (the main scene)
  313. scene.Render(RenderStage.DiffuseMap, viewpoint);
  314.  
  315. // Prerender the lights (needed for shadow mapping)
  316. //lTest1.Prerender(scene);
  317. //lTest2.Prerender(scene);
  318. //lTest3.Prerender(scene);
  319.  
  320. // Enable deferred lighting pass settings
  321. GL.Disable(EnableCap.DepthTest);
  322. GL.Disable(EnableCap.CullFace);
  323. rtLightingPass.Begin();
  324.  
  325. // Ambient layer
  326. FullscreenQuad.Draw(sAmbient, (s) =>
  327. {
  328. s.SetUniform("colour", new Color4(25, 25, 25, 255));
  329. s.BindFragData("outColour", 0);
  330. }, BlendMode.Normal);
  331.  
  332. // Bind lighting textures
  333. Spotlight.BeginRender(rtNormalMap);
  334.  
  335. // Render the lights
  336. lTest1.Render();
  337. lTest2.Render();
  338. lTest3.Render();
  339.  
  340. rtLightingPass.Finish();
  341.  
  342. // Composite the scene
  343. FullscreenQuad.Draw(sOverlay, (s) =>
  344. {
  345. GL.ActiveTexture(TextureUnit.Texture0);
  346. rtLightingPass.BindColourTexture();
  347. s.SetUniform("texture0", 0);
  348. s.BindFragData("outColour", 0);
  349. }, BlendMode.Multiply);
  350.  
  351.  
  352. this.SwapBuffers();
  353. }
  354.  
  355. }
  356. }
Advertisement
Add Comment
Please, Sign In to add comment