Advertisement
juyt66

Untitled

Mar 21st, 2025
249
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.00 KB | Software | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4.  
  5. using OpenGL;
  6. using System.Runtime.InteropServices;
  7.  
  8. namespace objview
  9. {
  10.     public static class GlMain
  11.     {
  12.         private static IModel _Model;
  13.  
  14.         public static IModel Model
  15.         {
  16.             get { return _Model; }
  17.             set
  18.             {
  19.                 _Model?.Dispose();
  20.                 _Model = value;
  21.                 ViewportIsDirty = true;
  22.                 Roller.Reset();
  23.             }
  24.         }
  25.  
  26.         public static bool Animating { get { return Roller.IsAnimating; } }
  27.  
  28.         private static int ViewportWidth;
  29.  
  30.         private static int ViewportHeight;
  31.  
  32.         private static bool ViewportIsDirty;
  33.  
  34.         private static float ViewportCanonicalScale;
  35.  
  36.         private static float CameraDistance;
  37.  
  38.         public static void Initialize()
  39.         {
  40.  
  41.         }
  42.  
  43.         public static void Destroy()
  44.         {
  45.             _Model = null;
  46.         }
  47.  
  48.         public static void Resize(int width, int height)
  49.         {
  50.             ViewportWidth = width;
  51.             ViewportHeight = height;
  52.             ViewportIsDirty = true;
  53.         }
  54.  
  55.         private const float CAMERA_DISTANCE_RATIO = 4.0f;
  56.  
  57.         private static DateTime LastDraw = DateTime.UtcNow;
  58.  
  59.         public static void Draw()
  60.         {
  61.             if (Model == null) return;
  62.  
  63.             Roller.Update();
  64.  
  65.             if (ViewportIsDirty)
  66.             {
  67.                 Gl.Viewport(0, 0, ViewportWidth, ViewportHeight);
  68.  
  69.                 CameraDistance = CAMERA_DISTANCE_RATIO * Model.BoundingRadius;
  70.  
  71.                 ViewportCanonicalScale = 2f / Math.Min(ViewportWidth, ViewportHeight);
  72.                 var s = (double)Model.BoundingRadius * ViewportCanonicalScale / 2;
  73.  
  74.                 Gl.MatrixMode(MatrixMode.Projection);
  75.                 Gl.LoadIdentity();
  76.                 Gl.Frustum(-ViewportWidth * s, ViewportWidth * s, -ViewportHeight * s, ViewportHeight * s, CameraDistance - Model.BoundingRadius, CameraDistance + Model.BoundingRadius);
  77.                 //Gl.Ortho(-ViewportWidth * s, ViewportWidth * s, -ViewportHeight * s, ViewportHeight * s, distance - MeshRadious, distance + MeshRadious);
  78.                 Gl.Translate(0f, 0f, -CameraDistance);
  79.             }
  80.  
  81.             Gl.ClearColor(0.0f, 0.0f, 0.0f, 1.0f);
  82.             Gl.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit);
  83.  
  84.             Gl.MatrixMode(MatrixMode.Modelview);
  85.             Gl.LoadIdentity();
  86.  
  87.             Gl.Disable(EnableCap.Normalize);
  88.  
  89.             Gl.Enable(EnableCap.Multisample);
  90.  
  91.             Gl.Disable(EnableCap.Blend);
  92.             Gl.Disable(EnableCap.Dither);
  93.             Gl.BlendFunc(BlendingFactor.SrcAlpha, BlendingFactor.OneMinusSrcAlpha);
  94.  
  95.             Gl.Enable(EnableCap.Lighting);
  96.             Gl.Enable(EnableCap.Light0);
  97.             Gl.Light(LightName.Light0, LightParameter.Ambient, new[] { 0.8f, 0.8f, 0.8f, 1.0f });
  98.             Gl.Light(LightName.Light0, LightParameter.Specular, new[] { 0.5f, 0.5f, 0.5f, 1.0f });
  99.             Gl.Light(LightName.Light0, LightParameter.Diffuse, new[] { 1.0f, 1.0f, 1.0f, 1.0f });
  100.             Gl.Light(LightName.Light0, LightParameter.Position, new[] { 0.3f, 0.5f, 1.0f, 0.0f });
  101.  
  102.             Gl.MultMatrix(Roller.getMatrix().ToArray());
  103.             Model.Draw();
  104.         }
  105.  
  106.         private static readonly Roller Roller = new Roller();
  107.  
  108.         private static int LastRotateX, LastRotateY;
  109.  
  110.         public static void StartRotating(int x, int y)
  111.         {
  112.             LastRotateX = x;
  113.             LastRotateY = y;
  114.             Roller.Pin();
  115.         }
  116.  
  117.         public static void Rotating(int x, int y)
  118.         {
  119.             Roller.Rotate((x - LastRotateX) * ViewportCanonicalScale, (y - LastRotateY) * ViewportCanonicalScale);
  120.         }
  121.  
  122.         public static void EndRotating(int x, int y)
  123.         {
  124.             // Do nothing special.
  125.         }
  126.  
  127.         public static void ResetRotation()
  128.         {
  129.             Roller.StartReset();
  130.         }
  131.     }
  132. }
  133.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement