⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 main_form.cs

📁 c#代码
💻 CS
📖 第 1 页 / 共 2 页
字号:
//+--------------------------------------------------------------------------+
//|                                                                          |
//|                                Planet3D                                  |
//|                              Version 1.01                                |
//|                                                                          |
//|      OpenGL project using the GDImage flat API to create on the fly      |
//|     planet texture from files with smooth shading and ambient light.     |
//|                                                                          |
//|          The GDImage.dll unregistered version has no limitation,         |
//|             and all features of the API are fully functional.            |
//|                                                                          |
//|       Buying a commercial license removes the copyright nag screen.      |
//|                                                                          |
//+--------------------------------------------------------------------------+
//|                                                                          |
//|                         Author Patrice TERRIER                           |
//|                           copyright (c) 2006                             |
//|                                                                          |
//|                        pterrier@zapsolution.com                          |
//|                                                                          |
//|                          www.zapsolution.com                             |
//|                                                                          |
//+--------------------------------------------------------------------------+
//|                  Project started on : 10-26-2006 (MM-DD-YYYY)            |
//|                        Last revised : 11-13-2006 (MM-DD-YYYY)            |
//+--------------------------------------------------------------------------+

using System;
//using System.Collections.Generic;
//using System.ComponentModel;
//using System.Data;
using System.Drawing;
//using System.Text;
using System.Windows.Forms;

using System.Runtime.InteropServices;
//using System.Runtime;
//using System.IO;
//using Microsoft.Win32;

using Win32;
using GDImage;
using OpenGL;
using SkinEngine;

namespace Planet3D
{
    //Version 1.01
    //___________________________________________________________
    public delegate int CallBack(IntPtr hWnd, int wMsg, uint wParam, uint lParam);
    //___________________________________________________________

    public partial class MAIN_Form : Form
    {
        const int GLOBE = 0;                           // GLOBE texture
        const int STARS = 1;                           // STARS texture
        const int ID_CTRL  = 100;                      // GDImage Zoom control
        const int ID_GLOBE = 101;
        const int ID_STARS = 102;
        const int TIMER_MAX = 2;                       // To perform smooth animation

        static GI.ZGLTEXTURE[] mt = new GI.ZGLTEXTURE[2];

        static public IntPtr hFORM_Main = IntPtr.Zero; // Default to Zero
        static public IntPtr hGLcontrol = IntPtr.Zero; // Default to Zero

        static GI.ZGLFONT UseFont = new GI.ZGLFONT();
        static bool Animate = false;

        static int WasZoom = 0;
        static bool Zooming = false;

        //Version 1.01
        //____________________________________________________________
        //static float grDistance, grSpinX, grSpinY, grYincr, grXincr;
        [DllImport("USER32", EntryPoint = "SetWindowLongA")]
        public static extern int SetCallBack(IntPtr hWnd, int nIndex, CallBack lNewLong);
        static CallBack UseCallBack = null;
        static int GLwasX, GLwasY;
        static bool bMousing;
        static int ptLastMousePositX, ptLastMousePositY, ptCurrentMousePositX, ptCurrentMousePositY;
        static int ghGLproc;                             // OpenGL subclassing
        //____________________________________________________________

        static float grSpinY, grSpinX;
        static float grYincr, grXincr;

        public MAIN_Form()
        {
            InitializeComponent();

            // Create the OpenGL control
            hGLcontrol = GI.CreateGLcontrol(GL_Template, ID_CTRL);

            hFORM_Main = this.Handle;
            StartAnimation();

        }

        private void FORM_Load(object sender, EventArgs e)
        {
            // We need this to retrieve bitmap from resource
            SK.UseNameSpace = this.GetType().Namespace;

            // Use GetPixel(0, 0) for transparent color
            SK.UseTransparencyColorTopLeft = true;

            // Create buttons region and init button (to show enabled/disabled mode)
            SK.CreateButtonRegion(BTN_Up);
            SK.CreateButtonRegion(BTN_Left); 
            SK.CreateButtonRegion(BTN_Right);
            SK.CreateButtonRegion(BTN_Down);
            SK.CreateButtonRegion(BTN_Reset);
            SK.CreateButtonRegion(BTN_Pause);
            SK.CreateButtonRegion(BTN_SelectFile);
            SK.CreateButtonRegion(BTN_Start);

            if (hGLcontrol != IntPtr.Zero)
            {
                //Version 1.01
                //______________________________________________________________
                UseCallBack = new CallBack(glWndProc);
                ghGLproc = SetCallBack(hGLcontrol, Api.GWL_WNDPROC, UseCallBack);
                //______________________________________________________________

                OnOff.Checked = true;

                // Do not fit the texture into a square shape
                GI.DoNotSquareTexture();

                // This is our background image texture
                mt[GLOBE].FullName = Api.ExePath() + "Earth.jpg"; mt[GLOBE].ID = ID_GLOBE;
                // This is our default moving sprite texture
                mt[STARS].FullName = Api.ExePath() + "StarsNight.jpg"; mt[STARS].ID = ID_STARS;

                // Assign textures to mt[]
                SetGenTextures(2);

                // I am using a temporary ZGLTEXTURE structure 
                // to protect mt[] from being erased during 
                // call to SetMutipleGLTextureFrom32Bit.
                GI.ZGLTEXTURE[] temp = new GI.ZGLTEXTURE[mt.Length];
                for (int k = 0; k < mt.Length; k++) { temp[k] = mt[k]; }
                if (GI.CreateMutipleGLtextures(ref temp, mt.Length) == 0)
                {
                    ResetGlobe();

                    float[] LightAmbient = { 0.05f, 0.05f, 0.05f, 1.0f };
                    float[] LightDiffuse = { 1.0f, 1.0f, 1.0f, 1.0f };
                    float[] LightPosition = { 20.0f, 0.0f, 30.0f, 1.0f };
                    GL.glLightfv(GL.GL_LIGHT0, GL.GL_AMBIENT, LightAmbient);   // Setup The Ambient Light
                    GL.glLightfv(GL.GL_LIGHT0, GL.GL_DIFFUSE, LightDiffuse);   // Setup The Diffuse Light
                    GL.glLightfv(GL.GL_LIGHT0, GL.GL_POSITION, LightPosition); // Position The Light

                    GL.glShadeModel(GL.GL_SMOOTH); // Enable Smooth Shading
                    GL.glHint(GL.GL_PERSPECTIVE_CORRECTION_HINT, GL.GL_NICEST); // Do nicest perspective
                    GL.glEnable(GL.GL_COLOR_MATERIAL); // Enable Coloring Of Material	

                    GL.glEnable(GL.GL_TEXTURE_2D);

                    UseFont.fontName = "Arial";       // A True Type font
                    UseFont.fontHeight = 10;          // Size of the font
                    UseFont.fontWeight = Api.FW_BOLD; // Font style
                    GI.BuildGLfont(GI.GetGLDC(hGLcontrol), ref UseFont);

                    // When using the GDImage file selector
                    // start looking for pictures into the EXE folder.
                    GI.LoadSavePath(Api.ExePath(), true);
                }
            }
        }

        private void SetGenTextures(int n)
        {
            int[] textures = new int[n];
            GL.glGenTextures(n, textures);
            for (int k = 0; k < mt.Length; k++) { mt[k].Texture = textures[k]; }
        }

        private void StartAnimation()
        {
            // Use API WM_TIMER to avoid aggressive garbage collection 
            // to occur before Main ends.
            for (int T = 0; T < TIMER_MAX; T++)
            {
                Api.SetTimer(hFORM_Main, T + 1, 0, null);
            }
        }

        private void SetZoom(int zoom)
        {
            GI.SetGLzoom(hGLcontrol, zoom);
            GI.ResizeGLWindow(hGLcontrol);
        }

        private void UpdateZoom(int z)
        {
            SetZoom(z); RenderOpenGL(hGLcontrol);
        }

        protected override void WndProc(ref Message m)
        {
            switch (m.Msg)
            {
                case Api.WM_TIMER:
                    RenderOpenGL(hGLcontrol);
                    m.Result = (IntPtr)0;
                    return;

                case Api.WM_COMMAND:
                    //  Zooming part
                    if (m.LParam == ZOOM_Track.Handle)
                    {// So much easier than trying to raise an event
                        // Warning, we are using polar coordinates there
                        // thus convert it to Degree
                        if (Zooming == false)
                        {
                            Zooming = true;
                            double range = ZOOM_Track.Maximum - ZOOM_Track.Minimum;
                            double increment = range / 90.0f;
                            int ZoomIs = (int)Math.Max(range - (ZOOM_Track.Value * increment), 1);

                            if (WasZoom < ZoomIs)
                            { for (int z = WasZoom; z < ZoomIs + 1; z++) UpdateZoom(z); }
                            else if (WasZoom > ZoomIs)
                            { for (int z = WasZoom; z > ZoomIs - 1; z--) UpdateZoom(z); }

                            WasZoom = ZoomIs;
                            Zooming = false;
                        }
                    }
                    break;

                case Api.WM_DESTROY:
                    DisposeGL(); // Free up OpenGL resources
                    break;
            }

            base.WndProc(ref m);

            if (m.Msg == Api.WM_COMMAND) Api.SetFocus(m.HWnd);            
        }

        //private void Form_KeyDown(object sender, KeyEventArgs e)
        //{
        //    bool Eat = false;
        //    switch ((int)e.KeyCode)
        //    {
        //        case Api.VK_LEFT:
        //        case Api.VK_RIGHT:
        //        case Api.VK_UP:
        //        case Api.VK_DOWN:
        //        case Api.VK_PGUP:
        //        case Api.VK_PGDN:
        //            Eat = true;
        //            break;
        //        default: break;
        //    }
        //    e.Handled = Eat;
        //}

        private void Form_PreviewKeydown(object sender, PreviewKeyDownEventArgs e)
        {
            int ZoomIs;
            switch ((int)e.KeyCode)
            {
                case Api.VK_LEFT: grXincr -= 0.01f; Animate = true; break;
                case Api.VK_RIGHT: grXincr += 0.01f; Animate = true; break;
                case Api.VK_UP: grYincr -= 0.01f; Animate = true; break;
                case Api.VK_DOWN: grYincr += 0.01f; Animate = true; break;
                case Api.VK_PGUP:
                    ZoomIs = GI.GetGLzoom(hGLcontrol);
                    if (ZoomIs > 1)
                    {
                        GI.SetGLzoom(hGLcontrol, ZoomIs - 1);
                        GI.ResizeGLWindow(hGLcontrol);

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -