opengl中实现MIF文件按某一字段3维显示

来源:互联网 发布:网络招生渠道 编辑:程序博客网 时间:2024/05/21 12:46

1读取mif文件到内存,可以用System.Collections.Generic.List<Vertex>集合来保存一个多边形的顶点,.

 struct Vertex
{
    
   public float  x, y, z;
}

            并调用   glInterleavedArrays(GL_V3F, 0, Marshal.UnsafeAddrOfPinnedArrayElement(vertex, 0));
              glColor3f(_rect[0].z, 0f, 0.0f);
               glDrawArrays(GL_POLYGON, 0, vertex.Length);

              显示出该多边形。

2.在每2个顶点之间画一个4边形,(x1,0,y1)(x2,0,y2)(x2,z,y2)(x1,z,y1).注意地理的Y,对应opengl的Z.所以y,z的坐标要交换,还有Z的坐标要×(-1),否则该点可能不能看不见。

代码

#region BSD License
/*
 BSD License
Copyright (c) 2002, Randy Ridge, The CsGL Development Team
http://csgl.sourceforge.net/
All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:

1. Redistributions of source code must retain the above copyright notice,
   this list of conditions and the following disclaimer.

2. Redistributions in binary form must reproduce the above copyright notice,
   this list of conditions and the following disclaimer in the documentation
   and/or other materials provided with the distribution.

3. Neither the name of The CsGL Development Team nor the names of its
   contributors may be used to endorse or promote products derived from this
   software without specific prior written permission.

   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
   FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
   COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
   INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
   BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
   LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
   CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
   LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
   ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
   POSSIBILITY OF SUCH DAMAGE.
 */
#endregion BSD License

#region Original Credits / License
/*
 *  This Code Was Created By Jeff Molofee 2000
 *  A HUGE Thanks To Fredric Echols For Cleaning Up
 *  And Optimizing The Base Code, Making It More Flexible!
 *  If You've Found This Code Useful, Please Let Me Know.
 *  Visit My Site At nehe.gamedev.net
 */
#endregion Original Credits / License

using CsGL.Basecode;
using System.Reflection;
using CSGL;
using System;
using System.Windows.Forms;
using System.Runtime.InteropServices;

#region AssemblyInfo
[assembly: AssemblyCompany("The CsGL Development Team (http://csgl.sourceforge.net)")]
[assembly: AssemblyCopyright("2002 The CsGL Development Team (http://csgl.sourceforge.net)")]
[assembly: AssemblyDescription("NeHe Lesson 03")]
[assembly: AssemblyProduct("NeHe Lesson 03")]
[assembly: AssemblyTitle("NeHe Lesson 03")]
[assembly: AssemblyVersion("1.0.0.0")]
#endregion AssemblyInfo

namespace NeHeLessons
{
  
 /// <summary>
 /// NeHe Lesson 03 -- Adding Color (http://nehe.gamedev.net)
 /// Implemented In C# By The CsGL Development Team (http://csgl.sourceforge.net)
 /// </summary>
 public sealed class NeHeLesson03 : Model {
  // --- Fields ---
  #region Public Properties
        
        float rquad = 0f;

  /// <summary>
  /// Lesson title.
  /// </summary>
  public override string Title {
   get {
    return "NeHe Lesson 03 -- Adding Color";
   }
  }

  /// <summary>
  /// Lesson description.
  /// </summary>
  public override string Description {
   get {
    return "Expanding on lesson 02, you will now add color to your primitives.  The triangle on the left uses smooth coloring, notice how the colors on the triangle blend together.  The square on the right uses flat coloring, covering the entire primitive in a solid color.  Coloring adds greatly to your OpenGL projects.";
   }
  }
        progon[] pros = new progon[8];
        float[] height ={ 0.3f, 0.5f, 0.2f, 0.2f, 0.144f,0.3f,0.45f ,0.33f};
        public NeHeLesson03()
        {
            System.IO.StreamReader reader = new System.IO.StreamReader(@"e:/县界.MIF");
            String line;
            int num = 0;
            while ((line = reader.ReadLine()) != null)
            {
                if (line.Contains("Region"))
                {
                   int linenum = int.Parse(reader.ReadLine());
                    line="";
                   for (int i = 0; i < linenum; i++)
                   {
                       line += reader.ReadLine();
                       if (i != linenum - 1)
                                line +=" ";
                     
                   }
                   
               
                   pros[num] = new progon(line, height[num]);
                   num++;

                }
            }
       
           
        }
      
 
  /// <summary>
  /// Lesson URL.
  /// </summary>
  public override string Url {
   get {
    return "http://nehe.gamedev.net/tutorials/lesson.asp?l=03";
   }
  }
  #endregion Public Properties

  // --- Entry Point ---
  #region Main()
  /// <summary>
  /// Application's entry point, runs this NeHe lesson.
  /// </summary>
  public static void Main() {              // Entry Point
   App.Run(new NeHeLesson03());            // Run Our NeHe Lesson As A Windows Forms Application
  }
  #endregion Main()
        float xrotspeed, yrotspeed;

  // --- Basecode Methods ---
         public override void ProcessInput() {
   base.ProcessInput();              // Handle The Default Basecode Keys

   if(KeyState[(int) Keys.Up]) {            // Is Up Arrow Key Being Pressed?
    xrotspeed -= 0.08f;              // Decrease xrotspeed
   }

   if(KeyState[(int) Keys.Down]) {            // Is Down Arrow Key Being Pressed?
    xrotspeed += 0.08f;              // Increase xrotspeed
   }

   if(KeyState[(int) Keys.Left]) {            // Is Left Arrow Key Being Pressed?
    yrotspeed -= 0.08f;              // Decrease yrotspeed
   }

   if(KeyState[(int) Keys.Right]) {           // Is Right Arrow Key Being Pressed?
    yrotspeed += 0.08f;              // Increase yrotspeed
   }

   
  }
  #region Draw()
  /// <summary>
  /// Draws NeHe Lesson 03 scene.
  /// </summary>
        public override void Draw()
        {
            glEnableClientState(GL_TEXTURE_COORD_ARRAY);
            glEnableClientState(GL_NORMAL_ARRAY);
            glEnableClientState(GL_VERTEX_ARRAY);             // Here's Where We Do All The Drawing
            glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);       // Clear Screen And Depth Buffer
            glLoadIdentity();               // Reset The Current Modelview Matrix
          // gluLookAt(0f, 0f, this.xrotspeed, 0f, 0f, 0f, 0f, 2f, 0f);
           //Console.WriteLine(this.xrotspeed);
               // Move Left 1.5 Units And Into The Screen 6.0
            // Finished Drawing The Triangles
         // gluPerspective(60.0, 1.0 * 120 / 30, 30, 200.0);
                        // Increase The Rotation Variable For The Triangle
         glTranslatef(0f, 0f, -2f);
         glRotatef(rquad, 1.0f, 0.0f, 0.0f); 
       //   glScalef(f, 10f,1f);

      // Move Right 3 Units

         
      
            glColor3f(1f, 0.5f, 1.0f);
         
            for (int k = 0; k < 8; k++)
            {
              progon pro = pros[k];
              Vertex[] vertex=   pro.cores.ToArray();
              rect[] _rect = pro.rects.ToArray();
              glInterleavedArrays(GL_V3F, 0, Marshal.UnsafeAddrOfPinnedArrayElement(vertex, 0));
              glColor3f(_rect[0].z, 0f, 0.0f);
               glDrawArrays(GL_POLYGON, 0, vertex.Length);

                for (int i = 0; i < _rect.Length &&_rect[i] != null; i++)
                {
                  
                        glBegin(GL_QUADS);               // Draw A Quad
                       glColor3f(0.5f, 0.5f, 1.0f);
                        glVertex3f(_rect[i].x1, 0.0f, _rect[i].y1);           // Top Left
                        glVertex3f(_rect[i].x2, 0.0f, _rect[i].y2);
                        glVertex3f(_rect[i].x2, _rect[i].z, _rect[i].y2);
                        //glColor3f(0.5f, 1.5f, 1.5f); // Top Right
                        glVertex3f(_rect[i].x1, _rect[i].z, _rect[i].y1); 
         // Bottom Right
                        //  Console.WriteLine(pro.rects[i].x1 + "  " + pro.rects[i].y1 + "  " + pro.rects[i].x2 + "  " + pro.rects[i].y2);         // Bottom Left
                        glEnd();
                   
                  

                }
             
            }
           
                       // Increase The Rotation Variable For The Triangle
            rquad += 0.2f;
         
        }
  #endregion Draw()
          public override void InputHelp() {
   base.InputHelp();               // Set Up The Default Input Help

   System.Data.DataRow dataRow;               // Row To Add

   dataRow = InputHelpDataTable.NewRow();          // Up Arrow - Decrease X Rotation
   dataRow["Input"] = "Up Arrow";
   dataRow["Effect"] = "Decrease X Rotation";
   dataRow["Current State"] = "";
   InputHelpDataTable.Rows.Add(dataRow);

   dataRow = InputHelpDataTable.NewRow();          // Down Arrow - Increase X Rotation
   dataRow["Input"] = "Down Arrow";
   dataRow["Effect"] = "Increase X Rotation";
   dataRow["Current State"] = "";
   InputHelpDataTable.Rows.Add(dataRow);

   dataRow = InputHelpDataTable.NewRow();          // Left Arrow - Decrease Y Rotation
   dataRow["Input"] = "Left Arrow";
   dataRow["Effect"] = "Decrease Y Rotation";
   dataRow["Current State"] = "";
   InputHelpDataTable.Rows.Add(dataRow);

   dataRow = InputHelpDataTable.NewRow();          // Right Arrow - Increase Y Rotation
   dataRow["Input"] = "Right Arrow";
   dataRow["Effect"] = "Increase Y Rotation";
   dataRow["Current State"] = "";
   InputHelpDataTable.Rows.Add(dataRow);

   dataRow = InputHelpDataTable.NewRow();          // Page Up - Move Ball Up
   dataRow["Input"] = "Page Up";
   dataRow["Effect"] = "Move Ball Up";
   dataRow["Current State"] = "";
   InputHelpDataTable.Rows.Add(dataRow);

   dataRow = InputHelpDataTable.NewRow();          // Page Down - Move Ball Down
   dataRow["Input"] = "Page Down";
   dataRow["Effect"] = "Move Ball Down";
   dataRow["Current State"] = "";
   InputHelpDataTable.Rows.Add(dataRow);

   dataRow = InputHelpDataTable.NewRow();          // Left Arrow - Decrease Y Rotation
   dataRow["Input"] = "Left Arrow";
   dataRow["Effect"] = "Decrease Y Rotation";
   dataRow["Current State"] = "";
   InputHelpDataTable.Rows.Add(dataRow);

   dataRow = InputHelpDataTable.NewRow();          // A - Zoom In
   dataRow["Input"] = "A";
   dataRow["Effect"] = "Zoom In";
   dataRow["Current State"] = "";
   InputHelpDataTable.Rows.Add(dataRow);

   dataRow = InputHelpDataTable.NewRow();          // Z - Zoom Out
   dataRow["Input"] = "Z";
   dataRow["Effect"] = "Zoom Out";
   dataRow["Current State"] = "";
   InputHelpDataTable.Rows.Add(dataRow);
  }
 }
}

using System;
using System.Collections.Generic;
using System.Text;

namespace CSGL
{
    struct Vertex
{
    
   public float  x, y, z;
}
    class progon
    {
        public System.Collections.Generic.List<Vertex> cores = new System.Collections.Generic.List<Vertex>();
     
    
        public progon(string data,float z)
        {
           // string data="0.078726 -0.705331 0.080502 -0.659189 0.10527 -0.485897 0.27863 0.244407 0.476758 0.665255 0.696396 0.727002 0.789428 0.664981 1.0309 0.48233 1.086624 0.38636 1.244508 0.113931 1.272372 0.036537 1.290944 -0.204934 1.2507 -0.328766 0.749184 -0.625961 0.476752 -0.650727 0.23528 -0.718834 0.078726 -0.705331 -1.024708 0.934314 -1.061858 0.949794 -0.600584 1.042666 -0.058449 1.017645 -0.099058 0.931385 -0.272418 0.201081 -0.297186 0.027789 -0.298349 -0.002426 -0.972082 0.064919 -1.368338 -0.046481 -1.554347 -0.441586 -1.609813 -0.071816 -1.594334 0.22538 -1.588142 0.395648 -1.554088 0.652598 -1.024708 0.934314";
            //z = 0f;
            char[] spit={' '};
            string[] datas = data.Split(spit);
            for (int i = 0; i < datas.Length ; i = i + 2)
            {
             datas[i]=  "" + (float.Parse(datas[i]) -119.5);
             datas[i+1]=  "" + ((-1)*(float.Parse(datas[i+1]) -29.8));
            }
            for (int i = 0; i < datas.Length-2 && datas[i + 2]!=""; i = i + 2)
            {
                float x1 = float.Parse(datas[i]);
                float y1 = float.Parse(datas[i + 1]);
                float x2 =float.Parse(datas[i + 2]);
                float y2 =float.Parse(datas[i + 3]);
              //  Console.WriteLine(x1 + "  " + y1);
               rects.Add(new rect(x1, y1, x2, y2,z));
             
            }
           // Console.WriteLine("12333333333333333333");
            for (int i = 0; i < datas.Length-1; i += 2)
            {
               Vertex core = new Vertex();

                core.x = float.Parse(datas[i]);
                core.y =z;
                core.z = float.Parse(datas[i + 1]) ;
                //Console.WriteLine(core.x + "  " + core.z + "  "+core.y);
                cores.Add(core);
            }
        }
       // public rect[] rects = new rect[36];
        public System.Collections.Generic.List<rect> rects = new System.Collections.Generic.List<rect>();
      //  public rect[] cores = new rect[36];

    }
}
using System;
using System.Collections.Generic;
using System.Text;

namespace CSGL
{
    class rect
    {
        public float x1, y1, x2, y2;
        public float x, y, z;
        public rect(float x1, float y1, float x2, float y2,float z)
        {
            this.x1 = x1;
            this.y1 = y1;
            this.x2 = x2;
            this.y2 = y2;
            this.z = z;
        }

        public rect(float x, float y, float z)
        {
            this.x = x;
            this.y = y;
            this.z = z;
        }

    }
}