Renderer.cs
上传用户:gb3593
上传日期:2022-01-07
资源大小:3028k
文件大小:6k
源码类别:

游戏引擎

开发平台:

Visual C++

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using Tao.OpenGl;
  5. using Box2D.Net;
  6. using System.Windows.Forms;
  7. namespace TestBed.Net
  8. {
  9.     static class Renderer
  10.     {
  11.         public static void InitOpenGL(System.Drawing.Size WidthHeight, float viewZoom, Vector ViewOffset)
  12.         {
  13.             int Width = WidthHeight.Width;
  14.             int Height = WidthHeight.Height;
  15.             Height = Height > 0 ? Height : 1;
  16.             Gl.glDisable(Gl.GL_CULL_FACE);
  17.             Gl.glClearColor(.5f, .5f, .5f, 1);
  18.             float AspectRatio = (float)Width / (float)Height;
  19.             Gl.glViewport(0, 0, Width, Height);
  20.             Gl.glMatrixMode(Gl.GL_PROJECTION);
  21.             
  22.             Gl.glLoadIdentity();
  23.             Glu.gluOrtho2D(-AspectRatio, AspectRatio, -1, 1);
  24.             
  25.             Gl.glMatrixMode(Gl.GL_MODELVIEW);            
  26.         }
  27.         public static void OpenGLDraw(Test test)
  28.         {
  29.             Gl.glClear(Gl.GL_COLOR_BUFFER_BIT);
  30.             Gl.glMatrixMode(Gl.GL_MODELVIEW);
  31.             Gl.glLoadIdentity();
  32.             
  33.             Gl.glScalef(1.0f / test.Zoom, 1.0f / test.Zoom, 1.0f / test.Zoom);
  34.             Gl.glTranslatef(-test.ViewOffset.X, -test.ViewOffset.Y, 0);
  35.             Gl.glPushMatrix();
  36.             DrawBodies(test.world.Bodies);
  37.             DrawJoints(test.world.Joints);
  38.             Gl.glPopMatrix();
  39.         }
  40.         public static void DrawJoints(IList<Joint> joints)
  41.         {
  42.             foreach (Joint joint in joints)
  43.             {
  44.                 DrawJoint(joint, System.Drawing.Color.LawnGreen);
  45.             }
  46.         }
  47.         public static void DrawBodies(IList<Body> bodies)
  48.         {
  49.             foreach(Body body in bodies)
  50.                 foreach (Shape shape in body.Shapes)
  51.                 {
  52.                     System.Drawing.Color color = System.Drawing.Color.White;
  53.                     if (body.Static)
  54.                         color = System.Drawing.Color.LightGreen; //Color(0.5f, 0.9f, 0.5f)
  55.                     else if (body.Sleeping)
  56.                         color = System.Drawing.Color.LightBlue;
  57.                     //else if(body == bomb)
  58.                     DrawShape(body.GetXForm(), shape, System.Drawing.Color.White);
  59.                 }
  60.         }
  61.         public static void DrawShape(XForm xform, Shape shape, System.Drawing.Color c)
  62.         {
  63.         switch (shape.ShapeType)
  64.         {
  65.             case ShapeType.e_circleShape:
  66.         {
  67.                     Vector x = xform.Position;
  68.                     float r = (new CircleShape(shape)).Radius;
  69.                     float segments = 16;
  70.                     double increment = 2 * Math.PI / segments;
  71.         Gl.glColor3ub(c.R, c.G, c.B);
  72.         Gl.glBegin(Gl.GL_LINE_LOOP);
  73.         
  74.                     for (double i = 0, theta = 0; i < segments; ++i, theta += increment)
  75.         {
  76.                         Vector d = new Vector(r * (float)Math.Cos(theta), r * (float)Math.Sin(theta));
  77.         Vector v = x + d;
  78.                         Gl.glVertex2f(v.X, v.Y);
  79.         }
  80.                     Gl.glEnd();
  81.                     //Draw a line from the circle's center to it's right side
  82.                     //so we can visually inspect rotations.
  83.                     Gl.glBegin(Gl.GL_LINES);
  84.                     Gl.glVertex2f(x.X, x.Y);
  85.                     Vector ax = xform.Rotation.col1;
  86.                     Gl.glVertex2f(x.X + r * ax.X, x.Y + r * ax.Y);
  87.                     Gl.glEnd();
  88.         }
  89.         break;
  90.                 case ShapeType.e_polygonShape:
  91.         {
  92.                     Gl.glColor3ub(c.R, c.G, c.B);
  93.                     Gl.glBegin(Gl.GL_LINE_LOOP);
  94.                     foreach (Vector vertex in (new PolyShape(shape)).Vertices)
  95.                     {
  96.                         Vector vertprime = xform.Rotation * vertex + xform.Position;
  97.                         Gl.glVertex2f(vertprime.X, vertprime.Y);
  98.                     }
  99.         Gl.glEnd();
  100.         }
  101.         break;
  102.         }
  103.         }
  104.         public static void DrawAABB(AABB aabb, System.Drawing.Color c)
  105.         {
  106.             Gl.glColor3b(c.R, c.G, c.B);
  107.         Gl.glBegin(Gl.GL_LINE_LOOP);
  108.             Gl.glVertex2f(aabb.lowerBound.X, aabb.lowerBound.Y);
  109.             Gl.glVertex2f(aabb.upperBound.X, aabb.lowerBound.Y);
  110.             Gl.glVertex2f(aabb.upperBound.X, aabb.upperBound.Y);
  111.             Gl.glVertex2f(aabb.lowerBound.X, aabb.upperBound.Y);
  112.         Gl.glEnd();
  113.         }
  114.         public static void DrawJoint(Joint joint, System.Drawing.Color color)
  115.         {
  116.             Body b1 = joint.Body1;
  117.             Body b2 = joint.Body2;
  118.             Vector x1 = b1.GetXForm().Position;
  119.             Vector x2 = b2.GetXForm().Position;
  120.             Vector p1 = joint.Anchor2;
  121.             Vector p2 = joint.Anchor1;
  122.             Gl.glColor3ub(color.R, color.G, color.B);
  123.             Gl.glBegin(Gl.GL_LINES);
  124.             switch (joint.JointType)
  125.             {
  126.                 case JointType.e_mouseJoint:
  127.                 case JointType.e_distanceJoint:
  128.                     Gl.glVertex2f(p1.X, p1.Y);
  129.                     Gl.glVertex2f(p2.X, p2.Y);
  130.                     break;
  131.                 /*
  132.                  * case JointType.e_pulleyJoint:
  133.                     {
  134.                         b2PulleyJoint* pulley = (b2PulleyJoint*)joint;
  135.                         b2Vec2 s1 = pulley->GetGroundPoint1();
  136.                         b2Vec2 s2 = pulley->GetGroundPoint2();
  137.                         glVertex2f(s1.x, s1.y);
  138.                         glVertex2f(p1.x, p1.y);
  139.                         glVertex2f(s2.x, s2.y);
  140.                         glVertex2f(p2.x, p2.y);
  141.                     }
  142.                     break;
  143.                 */
  144.                 default:
  145.                     Gl.glVertex2f(x1.X, x1.Y);
  146.                     Gl.glVertex2f(p1.X, p1.Y);
  147.                     Gl.glVertex2f(x2.X, x2.Y);
  148.                     Gl.glVertex2f(p2.X, p2.Y);
  149.                     break;
  150.             }
  151.             Gl.glEnd();
  152.         }
  153.     }
  154. }