Advertisement
Krythic

3D and 2D Mixed

Jan 3rd, 2016
241
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.23 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Drawing;
  4. using OpenTK;
  5. using OpenTK.Graphics.OpenGL;
  6. using OpenTK.Input;
  7.  
  8. namespace OpenGLSwapTest
  9. {
  10.     public class TestingWindow : GameWindow
  11.     {
  12.  
  13.         const float RotationSpeed = 180.0f;
  14.         float _angle;
  15.  
  16.  
  17.         protected override void OnLoad(EventArgs e)
  18.         {
  19.             base.OnLoad(e);
  20.  
  21.             GL.ClearColor(Color.MidnightBlue);
  22.             GL.Enable(EnableCap.DepthTest);
  23.         }
  24.  
  25.  
  26.         protected override void OnResize(EventArgs e)
  27.         {
  28.             base.OnResize(e);
  29.             SwitchTo3D();
  30.         }
  31.  
  32.         protected override void OnUpdateFrame(FrameEventArgs e)
  33.         {
  34.             base.OnUpdateFrame(e);
  35.             KeyboardState keyboard = OpenTK.Input.Keyboard.GetState();
  36.             if (keyboard[Key.Escape])
  37.             {
  38.                 this.Exit();
  39.             }
  40.         }
  41.  
  42.         private void SwitchTo3D()
  43.         {
  44.             GL.Viewport(0, 0, Width, Height);
  45.             double aspectRatio = Width / (double)Height;
  46.             Matrix4 perspective = Matrix4.CreatePerspectiveFieldOfView(MathHelper.PiOver4, (float)aspectRatio, 1, 64);
  47.             GL.MatrixMode(MatrixMode.Projection);
  48.             GL.LoadMatrix(ref perspective);
  49.         }
  50.  
  51.  
  52.         protected override void OnRenderFrame(FrameEventArgs e)
  53.         {
  54.             base.OnRenderFrame(e);
  55.             GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit);
  56.            
  57.             //SwitchTo3D();
  58.             Matrix4 lookat = Matrix4.LookAt(0, 5, 5, 0, 0, 0, 0, 1, 0);
  59.             GL.MatrixMode(MatrixMode.Modelview);
  60.             GL.LoadMatrix(ref lookat);
  61.             _angle += RotationSpeed * (float)e.Time;
  62.             GL.Rotate(_angle, 0.0f, 1.0f, 0.0f);
  63.             DrawCube();
  64.             GL.DepthMask(false);
  65.             Draw2DOver3D();
  66.             GL.DepthMask(true);
  67.             this.SwapBuffers();
  68.         }
  69.  
  70.         private void Draw2DOver3D()
  71.         {
  72.             //
  73.             GL.PushMatrix();
  74.             GL.LoadIdentity();
  75.             Matrix4 orthoProjection = Matrix4.CreateOrthographicOffCenter(0, Width, Height, 0, -1, 1);
  76.             GL.MatrixMode(MatrixMode.Projection);
  77.             GL.PushMatrix();//
  78.             GL.LoadMatrix(ref orthoProjection);
  79.             //
  80.             int width = 400;
  81.             int height = 400;
  82.             GL.Begin(PrimitiveType.Quads);
  83.  
  84.             GL.TexCoord2(0, 0); GL.Vertex2(0, 0);
  85.             GL.TexCoord2(1, 0); GL.Vertex2(width, 0);
  86.             GL.TexCoord2(1, 1); GL.Vertex2(width, height);
  87.             GL.TexCoord2(0, 1); GL.Vertex2(0, height);
  88.  
  89.             GL.End();
  90.  
  91.             //
  92.             GL.PopMatrix();
  93.             GL.MatrixMode(MatrixMode.Modelview);
  94.             GL.PopMatrix();
  95.         }
  96.  
  97.         private void DrawCube()
  98.         {
  99.             GL.Begin(PrimitiveType.Quads);
  100.  
  101.             GL.Color3(Color.Silver);
  102.             GL.Vertex3(-1.0f, -1.0f, -1.0f);
  103.             GL.Vertex3(-1.0f, 1.0f, -1.0f);
  104.             GL.Vertex3(1.0f, 1.0f, -1.0f);
  105.             GL.Vertex3(1.0f, -1.0f, -1.0f);
  106.  
  107.             GL.Color3(Color.Honeydew);
  108.             GL.Vertex3(-1.0f, -1.0f, -1.0f);
  109.             GL.Vertex3(1.0f, -1.0f, -1.0f);
  110.             GL.Vertex3(1.0f, -1.0f, 1.0f);
  111.             GL.Vertex3(-1.0f, -1.0f, 1.0f);
  112.  
  113.             GL.Color3(Color.Moccasin);
  114.  
  115.             GL.Vertex3(-1.0f, -1.0f, -1.0f);
  116.             GL.Vertex3(-1.0f, -1.0f, 1.0f);
  117.             GL.Vertex3(-1.0f, 1.0f, 1.0f);
  118.             GL.Vertex3(-1.0f, 1.0f, -1.0f);
  119.  
  120.             GL.Color3(Color.IndianRed);
  121.             GL.Vertex3(-1.0f, -1.0f, 1.0f);
  122.             GL.Vertex3(1.0f, -1.0f, 1.0f);
  123.             GL.Vertex3(1.0f, 1.0f, 1.0f);
  124.             GL.Vertex3(-1.0f, 1.0f, 1.0f);
  125.  
  126.             GL.Color3(Color.PaleVioletRed);
  127.             GL.Vertex3(-1.0f, 1.0f, -1.0f);
  128.             GL.Vertex3(-1.0f, 1.0f, 1.0f);
  129.             GL.Vertex3(1.0f, 1.0f, 1.0f);
  130.             GL.Vertex3(1.0f, 1.0f, -1.0f);
  131.  
  132.             GL.Color3(Color.ForestGreen);
  133.             GL.Vertex3(1.0f, -1.0f, -1.0f);
  134.             GL.Vertex3(1.0f, 1.0f, -1.0f);
  135.             GL.Vertex3(1.0f, 1.0f, 1.0f);
  136.             GL.Vertex3(1.0f, -1.0f, 1.0f);
  137.  
  138.             GL.End();
  139.         }
  140.  
  141.  
  142.  
  143.     }
  144. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement