Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Drawing;
- using OpenTK;
- using OpenTK.Graphics.OpenGL;
- using OpenTK.Input;
- namespace OpenGLSwapTest
- {
- public class TestingWindow : GameWindow
- {
- const float RotationSpeed = 180.0f;
- float _angle;
- protected override void OnLoad(EventArgs e)
- {
- base.OnLoad(e);
- GL.ClearColor(Color.MidnightBlue);
- GL.Enable(EnableCap.DepthTest);
- }
- protected override void OnResize(EventArgs e)
- {
- base.OnResize(e);
- SwitchTo3D();
- }
- protected override void OnUpdateFrame(FrameEventArgs e)
- {
- base.OnUpdateFrame(e);
- KeyboardState keyboard = OpenTK.Input.Keyboard.GetState();
- if (keyboard[Key.Escape])
- {
- this.Exit();
- }
- }
- private void SwitchTo3D()
- {
- GL.Viewport(0, 0, Width, Height);
- double aspectRatio = Width / (double)Height;
- Matrix4 perspective = Matrix4.CreatePerspectiveFieldOfView(MathHelper.PiOver4, (float)aspectRatio, 1, 64);
- GL.MatrixMode(MatrixMode.Projection);
- GL.LoadMatrix(ref perspective);
- }
- protected override void OnRenderFrame(FrameEventArgs e)
- {
- base.OnRenderFrame(e);
- GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit);
- //SwitchTo3D();
- Matrix4 lookat = Matrix4.LookAt(0, 5, 5, 0, 0, 0, 0, 1, 0);
- GL.MatrixMode(MatrixMode.Modelview);
- GL.LoadMatrix(ref lookat);
- _angle += RotationSpeed * (float)e.Time;
- GL.Rotate(_angle, 0.0f, 1.0f, 0.0f);
- DrawCube();
- GL.DepthMask(false);
- Draw2DOver3D();
- GL.DepthMask(true);
- this.SwapBuffers();
- }
- private void Draw2DOver3D()
- {
- //
- GL.PushMatrix();
- GL.LoadIdentity();
- Matrix4 orthoProjection = Matrix4.CreateOrthographicOffCenter(0, Width, Height, 0, -1, 1);
- GL.MatrixMode(MatrixMode.Projection);
- GL.PushMatrix();//
- GL.LoadMatrix(ref orthoProjection);
- //
- int width = 400;
- int height = 400;
- GL.Begin(PrimitiveType.Quads);
- GL.TexCoord2(0, 0); GL.Vertex2(0, 0);
- GL.TexCoord2(1, 0); GL.Vertex2(width, 0);
- GL.TexCoord2(1, 1); GL.Vertex2(width, height);
- GL.TexCoord2(0, 1); GL.Vertex2(0, height);
- GL.End();
- //
- GL.PopMatrix();
- GL.MatrixMode(MatrixMode.Modelview);
- GL.PopMatrix();
- }
- private void DrawCube()
- {
- GL.Begin(PrimitiveType.Quads);
- GL.Color3(Color.Silver);
- GL.Vertex3(-1.0f, -1.0f, -1.0f);
- GL.Vertex3(-1.0f, 1.0f, -1.0f);
- GL.Vertex3(1.0f, 1.0f, -1.0f);
- GL.Vertex3(1.0f, -1.0f, -1.0f);
- GL.Color3(Color.Honeydew);
- GL.Vertex3(-1.0f, -1.0f, -1.0f);
- GL.Vertex3(1.0f, -1.0f, -1.0f);
- GL.Vertex3(1.0f, -1.0f, 1.0f);
- GL.Vertex3(-1.0f, -1.0f, 1.0f);
- GL.Color3(Color.Moccasin);
- GL.Vertex3(-1.0f, -1.0f, -1.0f);
- GL.Vertex3(-1.0f, -1.0f, 1.0f);
- GL.Vertex3(-1.0f, 1.0f, 1.0f);
- GL.Vertex3(-1.0f, 1.0f, -1.0f);
- GL.Color3(Color.IndianRed);
- GL.Vertex3(-1.0f, -1.0f, 1.0f);
- GL.Vertex3(1.0f, -1.0f, 1.0f);
- GL.Vertex3(1.0f, 1.0f, 1.0f);
- GL.Vertex3(-1.0f, 1.0f, 1.0f);
- GL.Color3(Color.PaleVioletRed);
- GL.Vertex3(-1.0f, 1.0f, -1.0f);
- GL.Vertex3(-1.0f, 1.0f, 1.0f);
- GL.Vertex3(1.0f, 1.0f, 1.0f);
- GL.Vertex3(1.0f, 1.0f, -1.0f);
- GL.Color3(Color.ForestGreen);
- GL.Vertex3(1.0f, -1.0f, -1.0f);
- GL.Vertex3(1.0f, 1.0f, -1.0f);
- GL.Vertex3(1.0f, 1.0f, 1.0f);
- GL.Vertex3(1.0f, -1.0f, 1.0f);
- GL.End();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement