SRWindow.cs 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. using System.Windows.Forms;
  10. using System.Diagnostics;
  11. using System.Runtime.InteropServices;
  12. using Tao.OpenGl;
  13. using Tao.Platform.Windows;
  14. using OpenCvSharp;
  15. using System.Xml;
  16. namespace ThreeDSR
  17. {
  18. public partial class SRWindow : Form
  19. {
  20. // --- Fields ---
  21. private static IntPtr hDC; // Private GDI Device Context
  22. private static IntPtr hRC; // Permanent Rendering Context
  23. private static SRWindow form; // Our Current Windows Form
  24. //private static bool[] keys = new bool[256]; // Array Used For The Keyboard Routine
  25. private static bool active = true; // Window Active Flag, Set To True By Default
  26. private static bool done = false; // Bool Variable To Exit Main Loop
  27. //for XML
  28. int m_iWidth;
  29. int m_iHeight;
  30. int m_iDLevel;
  31. double[] m_adDZ;
  32. double[,] m_adDC;
  33. double[,] m_adZV;
  34. double[,] m_adOverTurnZV;
  35. double m_dMaxZV;
  36. double m_dMinZV;
  37. float[, ,] m_afNewClr;
  38. //bmp
  39. float[, ,] m_afOrgClr;
  40. //new platform
  41. private System.Windows.Forms.PictureBox pictureBox1;
  42. int m_iMusDown = 0;
  43. //zzz
  44. public const int LINE_PNTS = 100;
  45. //Glfw.GLFWkeyfun m_funKeyCallBak;
  46. //Glfw.GLFWmousebuttonfun m_funMsBtnCallBak;
  47. //Glfw.GLFWmouseposfun m_funMsPosCallBak;
  48. int m_iMusMod = 0;
  49. int m_iLastmouseX, m_iLastmouseY;
  50. int m_iLastMusDown = 0;
  51. float[] m_fR_xyz = new float[3] { 30.0F, 30.0F, 0.0F };
  52. float[] m_fT_xyz = new float[3] { 0.0F, 0.0F, 0.0F };
  53. bool m_bOverTurn = false;
  54. bool m_bRulerColor = true;
  55. float[] m_afPonitA = new float[2] { 1.0F, 1.0F };
  56. float[] m_afPonitB = new float[2] { 1.0F, 1.0F };
  57. float[,] m_afLineXY = new float[LINE_PNTS - 2, 2];
  58. float[] m_afLineStep = new float[2] { 0.0F, 0.0F };
  59. double[] m_adLineZV = new double[LINE_PNTS];
  60. double[] m_adLineOverTurnZV = new double[LINE_PNTS];
  61. Form1 m_fmLine;
  62. float[, ,] m_afThisClr;
  63. double[,] m_adThisZV;
  64. double[] m_adThisLineZV;
  65. public SRWindow() {
  66. this.CreateParams.ClassStyle = this.CreateParams.ClassStyle | // Redraw On Size, And Own DC For Window.
  67. User.CS_HREDRAW | User.CS_VREDRAW | User.CS_OWNDC;
  68. this.SetStyle(ControlStyles.AllPaintingInWmPaint, true); // No Need To Erase Form Background
  69. this.SetStyle(ControlStyles.DoubleBuffer, true); // Buffer Control
  70. //zzz this.SetStyle(ControlStyles.Opaque, true); // No Need To Draw Form Background
  71. this.SetStyle(ControlStyles.ResizeRedraw, true); // Redraw On Resize
  72. this.SetStyle(ControlStyles.UserPaint, true); // We'll Handle Painting Ourselves
  73. this.Activated += new EventHandler(this.Form_Activated); // On Activate Event Call Form_Activated
  74. this.Closing += new CancelEventHandler(this.Form_Closing); // On Closing Event Call Form_Closing
  75. this.Deactivate += new EventHandler(this.Form_Deactivate); // On Deactivate Event Call Form_Deactivate
  76. this.KeyDown += new KeyEventHandler(this.Form_KeyDown); // On KeyDown Event Call Form_KeyDown
  77. //this.KeyUp += new KeyEventHandler(this.Form_KeyUp); // On KeyUp Event Call Form_KeyUp
  78. this.Resize += new EventHandler(this.Form_Resize);
  79. ReadPar();
  80. }
  81. public void ReadPar()
  82. {
  83. XmlDocument xd = new XmlDocument();
  84. string fileName = @"D:\ThreeDData.xml";
  85. xd.Load(fileName);
  86. string str;
  87. string[] strarr;
  88. //XmlElement oneitem = (XmlElement)(xd.SelectSingleNode("Size"));
  89. //str = oneitem.GetAttribute("width");
  90. //m_iWidth = int.Parse(str);
  91. //str = oneitem.GetAttribute("height");
  92. //m_iHeight = int.Parse(str);
  93. XmlNodeList xmlNoteList = xd.GetElementsByTagName("Size");
  94. if (xmlNoteList.Count < 1)
  95. {
  96. MessageBox.Show("XML文件错");
  97. return;
  98. }
  99. foreach (XmlElement item in xmlNoteList)
  100. {
  101. str = item.GetAttribute("width");
  102. m_iWidth = int.Parse(str);
  103. str = item.GetAttribute("height");
  104. m_iHeight = int.Parse(str);
  105. break;
  106. }
  107. /*
  108. XmlNode onenode = xd.SelectSingleNode("Z_level");
  109. str = onenode.InnerXml;
  110. string[] strarr = str.Split(',');
  111. m_iDLevel = strarr.Length;
  112. m_adDZ = new double[m_iDLevel];
  113. m_adDC = new double[m_iDLevel,3];
  114. for(int i = 0; i < m_iDLevel; i++)
  115.             {
  116. m_adDZ[i] = double.Parse(strarr[i]);
  117.             }
  118. */
  119. xmlNoteList = xd.GetElementsByTagName("Z_level");
  120. if (xmlNoteList.Count < 1)
  121. {
  122. MessageBox.Show("XML文件错");
  123. return;
  124. }
  125. foreach (XmlElement item in xmlNoteList)
  126. {
  127. str = item.InnerXml;
  128. strarr = str.Split(',');
  129. m_iDLevel = strarr.Length;
  130. m_adDZ = new double[m_iDLevel];
  131. m_adDC = new double[m_iDLevel, 3];
  132. for (int i = 0; i < m_iDLevel; i++)
  133. {
  134. m_adDZ[i] = double.Parse(strarr[i]);
  135. }
  136. break;
  137. }
  138. xmlNoteList = xd.GetElementsByTagName("colour");
  139. if (xmlNoteList.Count < m_iDLevel)
  140. {
  141. MessageBox.Show("XML文件错");
  142. return;
  143. }
  144. int iX = 0;
  145. foreach (XmlElement item in xmlNoteList)
  146. {
  147. str = item.InnerXml;
  148. strarr = str.Split(',');
  149. if (strarr.Length < 3)
  150. {
  151. MessageBox.Show("XML文件错");
  152. return;
  153. }
  154. m_adDC[iX, 0] = double.Parse(strarr[0]);
  155. m_adDC[iX, 1] = double.Parse(strarr[1]);
  156. m_adDC[iX, 2] = double.Parse(strarr[2]);
  157. iX++;
  158. }
  159. xmlNoteList = xd.GetElementsByTagName("Column");
  160. if (xmlNoteList.Count < m_iWidth)
  161. {
  162. MessageBox.Show("XML文件错");
  163. return;
  164. }
  165. m_adZV = new double[m_iWidth, m_iHeight];
  166. bool bFirst = true;
  167. iX = 0;
  168. foreach (XmlElement item in xmlNoteList)
  169. {
  170. str = item.InnerXml;
  171. strarr = str.Split(',');
  172. if (strarr.Length < m_iHeight)
  173. {
  174. MessageBox.Show("XML文件错");
  175. return;
  176. }
  177. for (int iY = 0; iY < m_iHeight; iY++)
  178. {
  179. m_adZV[iX, iY] = double.Parse(strarr[iY]);
  180. if ((bFirst) || (m_adZV[iX, iY] > m_dMaxZV))
  181. {
  182. m_dMaxZV = m_adZV[iX, iY];
  183. }
  184. if ((bFirst) || (m_adZV[iX, iY] < m_dMinZV))
  185. {
  186. m_dMinZV = m_adZV[iX, iY];
  187. bFirst = false;
  188. }
  189. }
  190. iX++;
  191. }
  192. //处理剖面线参数
  193. m_afPonitB[0] = m_iWidth - 1;
  194. m_afPonitB[1] = m_iHeight - 1;
  195. CalculationIne();
  196. //处理颜色和翻转参数
  197. m_afNewClr = new float[m_iWidth, m_iHeight, 3];
  198. m_adOverTurnZV = new double[m_iWidth, m_iHeight];
  199. for (iX = 0; iX < m_iWidth; iX++)
  200. {
  201. for (int iY = 0; iY < m_iHeight; iY++)
  202. {
  203. float[] fColor = CalculationColorf(m_adZV[iX, iY]);
  204. m_afNewClr[iX, iY, 0] = fColor[0];
  205. m_afNewClr[iX, iY, 1] = fColor[1];
  206. m_afNewClr[iX, iY, 2] = fColor[2];
  207. m_adOverTurnZV[iX, iY] = m_dMinZV + (m_dMaxZV - m_adZV[iX, iY]);
  208. }
  209. }
  210. fileName = @"D:\d1.bmp";
  211. Mat OrgImg = OpenCvSharp.Cv2.ImRead(fileName);
  212. if ((m_iWidth != OrgImg.Cols) || (m_iHeight != OrgImg.Rows))//行数Rows是Y,列数是X
  213. {
  214. MessageBox.Show("图像尺寸和XML文件矛盾");
  215. return;
  216. }
  217. m_afOrgClr = new float[m_iWidth, m_iHeight, 3];
  218. int[] point = new int[2];
  219. Vec3b OrgClr;
  220. for (point[0] = 0; point[0] < m_iHeight; point[0]++)
  221. {
  222. for (point[1] = 0; point[1] < m_iWidth; point[1]++)
  223. {
  224. OrgClr = OrgImg.At<Vec3b>(point[0], point[1]);
  225. m_afOrgClr[point[1], point[0], 0] = OrgClr.Item2 / 255.0F;
  226. m_afOrgClr[point[1], point[0], 1] = OrgClr.Item1 / 255.0F;
  227. m_afOrgClr[point[1], point[0], 2] = OrgClr.Item0 / 255.0F;
  228. }
  229. }
  230. m_bRulerColor = false;
  231. m_afThisClr = m_afOrgClr;
  232. m_bOverTurn = false;
  233. m_adThisZV = m_adZV;
  234. m_adThisLineZV = m_adLineZV;
  235. }
  236. public float[] CalculationColorf(double dZ)
  237. {
  238. float[] fColor = new float[3];
  239. if (dZ < m_adDZ[m_iDLevel - 1])
  240. {
  241. fColor[0] = (float)m_adDC[m_iDLevel - 1, 0];
  242. fColor[1] = (float)m_adDC[m_iDLevel - 1, 1];
  243. fColor[2] = (float)m_adDC[m_iDLevel - 1, 2];
  244. return fColor;
  245. }
  246. if (dZ > m_adDZ[0])
  247. {
  248. fColor[0] = (float)m_adDC[0, 0];
  249. fColor[1] = (float)m_adDC[0, 1];
  250. fColor[2] = (float)m_adDC[0, 2];
  251. return fColor;
  252. }
  253. int i, j;
  254. for (j = 1; j < m_iDLevel; j++)
  255. {
  256. if (dZ > m_adDZ[j])
  257. {
  258. break;
  259. }
  260. }
  261. i = j - 1;
  262. double dIJ = Math.Abs(m_adDZ[i] - m_adDZ[j]);
  263. double dWI = Math.Abs(dZ - m_adDZ[j]) / dIJ;
  264. double dWJ = Math.Abs(m_adDZ[i] - dZ) / dIJ;
  265. fColor[0] = (float)((m_adDC[i, 0] * dWI + m_adDC[j, 0] * dWJ) / 255.0);
  266. fColor[1] = (float)((m_adDC[i, 1] * dWI + m_adDC[j, 1] * dWJ) / 255.0);
  267. fColor[2] = (float)((m_adDC[i, 2] * dWI + m_adDC[j, 2] * dWJ) / 255.0);
  268. return fColor;
  269. }
  270. private void CalculationIne()
  271. {
  272. m_adLineZV[0] = CalculationInterpolation(m_afPonitA[0], m_afPonitA[1]);
  273. m_adLineOverTurnZV[0] = m_dMinZV + (m_dMaxZV - m_adLineZV[0]);
  274. m_afLineStep[0] = (m_afPonitB[0] - m_afPonitA[0]) / (LINE_PNTS - 1);
  275. m_afLineStep[1] = (m_afPonitB[1] - m_afPonitA[1]) / (LINE_PNTS - 1);
  276. for (int i = 0; i < (LINE_PNTS - 2); i++)
  277. {
  278. m_afLineXY[i, 0] = m_afPonitA[0] + (i + 1) * m_afLineStep[0];
  279. m_afLineXY[i, 1] = m_afPonitA[1] + (i + 1) * m_afLineStep[1];
  280. m_adLineZV[i + 1] = CalculationInterpolation(m_afLineXY[i, 0], m_afLineXY[i, 1]);
  281. m_adLineOverTurnZV[i + 1] = m_dMinZV + (m_dMaxZV - m_adLineZV[i + 1]);
  282. }
  283. m_adLineZV[LINE_PNTS - 1] = CalculationInterpolation(m_afPonitB[0], m_afPonitB[1]);
  284. m_adLineOverTurnZV[LINE_PNTS - 1] = m_dMinZV + (m_dMaxZV - m_adLineZV[LINE_PNTS - 1]);
  285. }
  286. private double CalculationInterpolation(float fX, float fY)
  287. {
  288. int iXS = Math.Min(Math.Max((int)(fX), 0), m_iWidth - 1);
  289. int iYS = Math.Min(Math.Max((int)(fY), 0), m_iHeight - 1);
  290. int iXB = Math.Min(Math.Max((int)(fX + 1.0), 0), m_iWidth - 1);
  291. int iYB = Math.Min(Math.Max((int)(fY + 1.0), 0), m_iHeight - 1);
  292. double dWXB = Math.Min(Math.Max((fX - (double)iXS), 0.0), 1.0);
  293. double dWXS = 1.0 - dWXB;
  294. double dWYB = Math.Min(Math.Max((fY - (double)iYS), 0.0), 1.0);
  295. double dWYS = 1.0 - dWYB;
  296. double dTempA = m_adZV[iXS, iYS];
  297. double dTempB = m_adZV[iXS, iYB];
  298. double dTempC = m_adZV[iXB, iYS];
  299. double dTempD = m_adZV[iXB, iYB];
  300. double dTempAB = dTempA * dWYS + dTempB * dWYB;
  301. double dTempCD = dTempC * dWYS + dTempD * dWYB;
  302. double dRtn = dTempAB * dWXS + dTempCD * dWXB;
  303. return dRtn;
  304. }
  305. public static void Run() {
  306. // Create Our OpenGL Window
  307. if(!CreateGLWindow("三维面绘制", 640, 480, 16)) {
  308. return; // Quit If Window Was Not Created
  309. }
  310. while(!done) { // Loop That Runs While done = false
  311. Application.DoEvents(); // Process Events
  312. // Draw The Scene. Watch For ESC Key And Quit Messages From DrawGLScene()
  313. if((active && (form != null) && !DrawGLScene()) /*|| keys[(int) Keys.Escape]*/) { // Active? Was There A Quit Received?
  314. done = true; // ESC Or DrawGLScene Signalled A Quit
  315. }
  316. else { // Not Time To Quit, Update Screen
  317. Gdi.SwapBuffers(hDC); // Swap Buffers (Double Buffering)
  318. }
  319. /*
  320. if(keys[(int) Keys.F1]) { // Is F1 Being Pressed?
  321. keys[(int) Keys.F1] = false; // If So Make Key false
  322. KillGLWindow(); // Kill Our Current Window
  323. // Recreate Our OpenGL Window
  324. if(!CreateGLWindow("三维面绘制", 640, 480, 16)) {
  325. return; // Quit If Window Was Not Created
  326. }
  327. done = false; // We're Not Done Yet
  328. }
  329. */
  330. }
  331. // Shutdown
  332. KillGLWindow(); // Kill The Window
  333. return; // Exit The Program
  334. }
  335. private static bool CreateGLWindow(string title, int width, int height, int bits)
  336. {
  337. int pixelFormat; // Holds The Results After Searching For A Match
  338. form = null; // Null The Form
  339. GC.Collect(); // Request A Collection
  340. // This Forces A Swap
  341. Kernel.SetProcessWorkingSetSize(Process.GetCurrentProcess().Handle, -1, -1);
  342. form = new SRWindow(); // Create The Window
  343. form.FormBorderStyle = FormBorderStyle.Sizable; // Sizable
  344. Cursor.Show(); // Show Mouse Pointer
  345. form.Width = width; // Set Window Width
  346. form.Height = height; // Set Window Height
  347. form.Text = title; // Set Window Title
  348. Gdi.PIXELFORMATDESCRIPTOR pfd = new Gdi.PIXELFORMATDESCRIPTOR(); // pfd Tells Windows How We Want Things To Be
  349. pfd.nSize = (short) Marshal.SizeOf(pfd); // Size Of This Pixel Format Descriptor
  350. pfd.nVersion = 1; // Version Number
  351. pfd.dwFlags = Gdi.PFD_DRAW_TO_WINDOW | // Format Must Support Window
  352. Gdi.PFD_SUPPORT_OPENGL | // Format Must Support OpenGL
  353. Gdi.PFD_DOUBLEBUFFER; // Format Must Support Double Buffering
  354. pfd.iPixelType = (byte) Gdi.PFD_TYPE_RGBA; // Request An RGBA Format
  355. pfd.cColorBits = (byte) bits; // Select Our Color Depth
  356. pfd.cRedBits = 0; // Color Bits Ignored
  357. pfd.cRedShift = 0;
  358. pfd.cGreenBits = 0;
  359. pfd.cGreenShift = 0;
  360. pfd.cBlueBits = 0;
  361. pfd.cBlueShift = 0;
  362. pfd.cAlphaBits = 0; // No Alpha Buffer
  363. pfd.cAlphaShift = 0; // Shift Bit Ignored
  364. pfd.cAccumBits = 0; // No Accumulation Buffer
  365. pfd.cAccumRedBits = 0; // Accumulation Bits Ignored
  366. pfd.cAccumGreenBits = 0;
  367. pfd.cAccumBlueBits = 0;
  368. pfd.cAccumAlphaBits = 0;
  369. pfd.cDepthBits = 16; // 16Bit Z-Buffer (Depth Buffer)
  370. pfd.cStencilBits = 0; // No Stencil Buffer
  371. pfd.cAuxBuffers = 0; // No Auxiliary Buffer
  372. pfd.iLayerType = (byte) Gdi.PFD_MAIN_PLANE; // Main Drawing Layer
  373. pfd.bReserved = 0; // Reserved
  374. pfd.dwLayerMask = 0; // Layer Masks Ignored
  375. pfd.dwVisibleMask = 0;
  376. pfd.dwDamageMask = 0;
  377. //hDC = User.GetDC(form.Handle); // Attempt To Get A Device Context
  378. form.pictureBox1 = new PictureBox();
  379. ((System.ComponentModel.ISupportInitialize)(form.pictureBox1)).BeginInit();
  380. form.pictureBox1.Location = new System.Drawing.Point(22, 40);
  381. form.pictureBox1.Name = "pictureBox1";
  382. form.pictureBox1.Size = new System.Drawing.Size(1272, 651);
  383. form.pictureBox1.TabIndex = 0;
  384. form.pictureBox1.TabStop = false;
  385. form.Controls.Add(form.pictureBox1);
  386. ((System.ComponentModel.ISupportInitialize)(form.pictureBox1)).EndInit();
  387. form.pictureBox1.MouseMove += new MouseEventHandler(form.pictureBox1_MouseMove);
  388. form.pictureBox1.MouseDown += new MouseEventHandler(form.pictureBox1_MouseDown);
  389. form.pictureBox1.MouseUp += new MouseEventHandler(form.pictureBox1_MouseUp);
  390. form.pictureBox1.MouseLeave += new EventHandler(form.pictureBox1_MouseLeave);
  391. hDC = User.GetDC(form.pictureBox1.Handle);
  392. if(hDC == IntPtr.Zero) { // Did We Get A Device Context?
  393. KillGLWindow(); // Reset The Display
  394. MessageBox.Show("Can't Create A GL Device Context.", "ERROR",
  395. MessageBoxButtons.OK, MessageBoxIcon.Error);
  396. return false;
  397. }
  398. pixelFormat = Gdi.ChoosePixelFormat(hDC, ref pfd); // Attempt To Find An Appropriate Pixel Format
  399. if(pixelFormat == 0) { // Did Windows Find A Matching Pixel Format?
  400. KillGLWindow(); // Reset The Display
  401. MessageBox.Show("Can't Find A Suitable PixelFormat.", "ERROR",
  402. MessageBoxButtons.OK, MessageBoxIcon.Error);
  403. return false;
  404. }
  405. if(!Gdi.SetPixelFormat(hDC, pixelFormat, ref pfd)) { // Are We Able To Set The Pixel Format?
  406. KillGLWindow(); // Reset The Display
  407. MessageBox.Show("Can't Set The PixelFormat.", "ERROR",
  408. MessageBoxButtons.OK, MessageBoxIcon.Error);
  409. return false;
  410. }
  411. hRC = Wgl.wglCreateContext(hDC); // Attempt To Get The Rendering Context
  412. if(hRC == IntPtr.Zero) { // Are We Able To Get A Rendering Context?
  413. KillGLWindow(); // Reset The Display
  414. MessageBox.Show("Can't Create A GL Rendering Context.", "ERROR",
  415. MessageBoxButtons.OK, MessageBoxIcon.Error);
  416. return false;
  417. }
  418. if(!Wgl.wglMakeCurrent(hDC, hRC)) { // Try To Activate The Rendering Context
  419. KillGLWindow(); // Reset The Display
  420. MessageBox.Show("Can't Activate The GL Rendering Context.", "ERROR",
  421. MessageBoxButtons.OK, MessageBoxIcon.Error);
  422. return false;
  423. }
  424. form.Show(); // Show The Window
  425. //zzz form.TopMost = true; // Topmost Window
  426. form.Focus(); // Focus The Window
  427. form.BackColor = Color.Gray;
  428. form.WindowState = FormWindowState.Maximized;
  429. ReSizeGLScene(width, height); // Set Up Our Perspective GL Screen
  430. if (!InitGL())
  431. { // Initialize Our Newly Created GL Window
  432. KillGLWindow(); // Reset The Display
  433. MessageBox.Show("Initialization Failed.", "ERROR",
  434. MessageBoxButtons.OK, MessageBoxIcon.Error);
  435. return false;
  436. }
  437. return true; // Success
  438. }
  439. private static bool DrawGLScene()
  440. {
  441. //new platform
  442. iniView();
  443. Gl.glTranslatef(0, 0, 0);
  444. myRotate();
  445. //错了 Gl.glTranslatef(m_fT_xyz[0],m_fT_xyz[1],0);
  446. for (int i = 0; i < form.m_iWidth - 1; i++)
  447. {
  448. Gl.glBegin(Gl.GL_TRIANGLE_STRIP);
  449. for (int j = 0; j < form.m_iHeight; j++)
  450. {
  451. DrowOnePnt(i, j);
  452. DrowOnePnt(i + 1, j);
  453. }
  454. Gl.glEnd();
  455. }
  456. if ((form.m_iMusMod >= 5) && (form.m_iMusMod <= 7))
  457. {
  458. Gl.glBegin(Gl.GL_QUADS);
  459. Gl.glColor3f(1.0F, 1.0F, 0.0F);
  460. Gl.glVertex3d(form.m_afPonitA[0] - 10 - form.m_iWidth / 2.0, form.m_afPonitA[1] - 10 - form.m_iHeight / 2.0, form.m_adThisLineZV[0] - (form.m_dMaxZV + form.m_dMinZV) / 2.0);
  461. Gl.glVertex3d(form.m_afPonitA[0] + 10 - form.m_iWidth / 2.0, form.m_afPonitA[1] - 10 - form.m_iHeight / 2.0, form.m_adThisLineZV[0] - (form.m_dMaxZV + form.m_dMinZV) / 2.0);
  462. Gl.glVertex3d(form.m_afPonitA[0] + 10 - form.m_iWidth / 2.0, form.m_afPonitA[1] + 10 - form.m_iHeight / 2.0, form.m_adThisLineZV[0] - (form.m_dMaxZV + form.m_dMinZV) / 2.0);
  463. Gl.glVertex3d(form.m_afPonitA[0] - 10 - form.m_iWidth / 2.0, form.m_afPonitA[1] + 10 - form.m_iHeight / 2.0, form.m_adThisLineZV[0] - (form.m_dMaxZV + form.m_dMinZV) / 2.0);
  464. Gl.glEnd();
  465. Gl.glBegin(Gl.GL_LINE_STRIP);
  466. Gl.glColor3f(0.0F, 1.0F, 0.0F);
  467. Gl.glVertex3d(form.m_afPonitA[0] - form.m_iWidth / 2.0, form.m_afPonitA[1] - form.m_iHeight / 2.0, form.m_adThisLineZV[0] - (form.m_dMaxZV + form.m_dMinZV) / 2.0);
  468. for (int i = 0; i < (LINE_PNTS - 2); i++)
  469. {
  470. Gl.glVertex3d(form.m_afLineXY[i, 0] - form.m_iWidth / 2.0, form.m_afLineXY[i, 1] - form.m_iHeight / 2.0, form.m_adThisLineZV[i + 1] - (form.m_dMaxZV + form.m_dMinZV) / 2.0);
  471. }
  472. Gl.glVertex3d(form.m_afPonitB[0] - form.m_iWidth / 2.0, form.m_afPonitB[1] - form.m_iHeight / 2.0, form.m_adThisLineZV[LINE_PNTS - 1] - (form.m_dMaxZV + form.m_dMinZV) / 2.0);
  473. Gl.glEnd();
  474. Gl.glBegin(Gl.GL_QUADS);
  475. Gl.glColor3f(0.0F, 1.0F, 01.0F);
  476. Gl.glVertex3d(form.m_afPonitB[0] - 10 - form.m_iWidth / 2.0, form.m_afPonitB[1] - 10 - form.m_iHeight / 2.0, form.m_adThisLineZV[0] - (form.m_dMaxZV + form.m_dMinZV) / 2.0);
  477. Gl.glVertex3d(form.m_afPonitB[0] + 10 - form.m_iWidth / 2.0, form.m_afPonitB[1] - 10 - form.m_iHeight / 2.0, form.m_adThisLineZV[0] - (form.m_dMaxZV + form.m_dMinZV) / 2.0);
  478. Gl.glVertex3d(form.m_afPonitB[0] + 10 - form.m_iWidth / 2.0, form.m_afPonitB[1] + 10 - form.m_iHeight / 2.0, form.m_adThisLineZV[0] - (form.m_dMaxZV + form.m_dMinZV) / 2.0);
  479. Gl.glVertex3d(form.m_afPonitB[0] - 10 - form.m_iWidth / 2.0, form.m_afPonitB[1] + 10 - form.m_iHeight / 2.0, form.m_adThisLineZV[0] - (form.m_dMaxZV + form.m_dMinZV) / 2.0);
  480. Gl.glEnd();
  481. }
  482. Gl.glLoadIdentity();
  483. return true;
  484. }
  485. private static void myRotate()
  486. {
  487. int[,] vec = new int[3, 3] { { 1, 0, 0 }, { 0, 1, 0 }, { 0, 0, 1 } };
  488. for (int i = 0; i <= 2; i++)
  489. {
  490. Gl.glRotatef(form.m_fR_xyz[i], vec[i, 0], vec[i, 1], vec[i, 2]);
  491. }
  492. }
  493. public static void DrowOnePnt(int i, int j)
  494. {
  495. Gl.glColor3f(form.m_afThisClr[i, j, 0], form.m_afThisClr[i, j, 1], form.m_afThisClr[i, j, 2]);
  496. Gl.glVertex3d(i - form.m_iWidth / 2.0, j - form.m_iHeight / 2.0, form.m_adThisZV[i, j] - (form.m_dMaxZV + form.m_dMinZV) / 2.0);
  497. }
  498. private static bool InitGL()
  499. {
  500. //Gl.glShadeModel(Gl.GL_SMOOTH); // Enable Smooth Shading
  501. //Gl.glClearColor(0, 0, 0, 0.5f); // Black Background
  502. //Gl.glClearDepth(1); // Depth Buffer Setup
  503. //Gl.glEnable(Gl.GL_DEPTH_TEST); // Enables Depth Testing
  504. //Gl.glDepthFunc(Gl.GL_LEQUAL); // The Type Of Depth Testing To Do
  505. //Gl.glHint(Gl.GL_PERSPECTIVE_CORRECTION_HINT, Gl.GL_NICEST); // Really Nice Perspective Calculations
  506. // 设置视口 viewport
  507. Gl.glViewport(0, 0, form.pictureBox1.Width, form.pictureBox1.Height);
  508. //启用阴影平滑
  509. Gl.glShadeModel(Gl.GL_SMOOTH);
  510. //启用反走样
  511. Gl.glHint(Gl.GL_PERSPECTIVE_CORRECTION_HINT, Gl.GL_NICEST);
  512. // 设置投影模式 projection matrix
  513. Gl.glMatrixMode(Gl.GL_PROJECTION);
  514. Gl.glLoadIdentity();
  515. iniView();
  516. return true;
  517. }
  518. public static void iniView()
  519. {
  520. // 重置黑色背景
  521. Gl.glClearColor(0, 0, 0, 0);
  522. Gl.glClear(Gl.GL_COLOR_BUFFER_BIT);
  523. form.m_fT_xyz[2] = (float)Math.Max(form.m_dMaxZV, Math.Abs(form.m_dMinZV));
  524. form.m_fT_xyz[2] = (float)(-1.0 * Math.Max(Math.Max(form.m_iWidth * 0.8, form.m_iHeight * 0.8), form.m_fT_xyz[2]));
  525. Glu.gluPerspective(65, form.pictureBox1.Width / (double)form.pictureBox1.Height, 1, -2.0 * form.m_fT_xyz[2]);//距离裁减
  526. // 选择模型观察矩阵 modelview matrix
  527. Gl.glMatrixMode(Gl.GL_MODELVIEW);
  528. //重置模型观察矩阵
  529. Gl.glLoadIdentity();
  530. Glu.gluLookAt(-1.0 * form.m_fT_xyz[0], -1.0 * form.m_fT_xyz[1], -1.0 * form.m_fT_xyz[2], // 眼睛位置
  531. -1.0 * form.m_fT_xyz[0], -1.0 * form.m_fT_xyz[1], 0, // 观察点
  532. 0, 1, 0);
  533. }
  534. private static void KillGLWindow() {
  535. if(hRC != IntPtr.Zero) { // Do We Have A Rendering Context?
  536. if(!Wgl.wglMakeCurrent(IntPtr.Zero, IntPtr.Zero)) { // Are We Able To Release The DC and RC Contexts?
  537. MessageBox.Show("Release Of DC And RC Failed.", "SHUTDOWN ERROR",
  538. MessageBoxButtons.OK, MessageBoxIcon.Error);
  539. }
  540. if(!Wgl.wglDeleteContext(hRC)) { // Are We Able To Delete The RC?
  541. MessageBox.Show("Release Rendering Context Failed.", "SHUTDOWN ERROR",
  542. MessageBoxButtons.OK, MessageBoxIcon.Error);
  543. }
  544. hRC = IntPtr.Zero; // Set RC To Null
  545. }
  546. if(hDC != IntPtr.Zero) { // Do We Have A Device Context?
  547. if(form != null && !form.IsDisposed) { // Do We Have A Window?
  548. if(form.Handle != IntPtr.Zero) { // Do We Have A Window Handle?
  549. if(!User.ReleaseDC(form.Handle, hDC)) { // Are We Able To Release The DC?
  550. MessageBox.Show("Release Device Context Failed.", "SHUTDOWN ERROR",
  551. MessageBoxButtons.OK, MessageBoxIcon.Error);
  552. }
  553. }
  554. }
  555. hDC = IntPtr.Zero; // Set DC To Null
  556. }
  557. if(form != null) { // Do We Have A Windows Form?
  558. form.Hide(); // Hide The Window
  559. form.Close(); // Close The Form
  560. form = null; // Set form To Null
  561. }
  562. }
  563. private static void ReSizeGLScene(int width, int height) {
  564. if(height == 0) { // Prevent A Divide By Zero...
  565. height = 1; // By Making Height Equal To One
  566. }
  567. Gl.glViewport(0, 0, width, height); // Reset The Current Viewport
  568. Gl.glMatrixMode(Gl.GL_PROJECTION); // Select The Projection Matrix
  569. Gl.glLoadIdentity(); // Reset The Projection Matrix
  570. Glu.gluPerspective(45, width / (double) height, 0.1, 100); // Calculate The Aspect Ratio Of The Window
  571. Gl.glMatrixMode(Gl.GL_MODELVIEW); // Select The Modelview Matrix
  572. Gl.glLoadIdentity(); // Reset The Modelview Matrix
  573. }
  574. private void Form_Activated(object sender, EventArgs e)
  575. {
  576. active = true; // Program Is Active
  577. }
  578. private void Form_Closing(object sender, CancelEventArgs e)
  579. {
  580. done = true; // Send A Quit Message
  581. }
  582. private void Form_Deactivate(object sender, EventArgs e)
  583. {
  584. active = false; // Program Is No Longer Active
  585. }
  586. private void Form_KeyDown(object sender, KeyEventArgs e)
  587. {
  588. //keys[e.KeyValue] = true; // Key Has Been Pressed, Mark It As true
  589. switch (e.KeyValue)
  590. {
  591. case (int) Keys.Escape://退出
  592. done = true;
  593. break;
  594. case 49://'1'
  595. m_iMusMod = 1;//旋转图像
  596. break;
  597. case 50://'2'
  598. m_iMusMod = 2;//平移图像
  599. break;
  600. case 53://'5'
  601. m_iMusMod = 5;//操作点A
  602. break;
  603. case 54://'6'
  604. m_iMusMod = 6;//操作点B
  605. break;
  606. case 55://'7'
  607. m_iMusMod = 7;//看剖面线
  608. ShowSectionLine();
  609. break;
  610. case 56://'8'
  611. overturn();
  612. break;
  613. case 57://'9'
  614. ChangeColor();
  615. break;
  616. default:
  617. m_iMusMod = 0;//no meaning
  618. break;
  619. }
  620. }
  621. private void ShowSectionLine()
  622. {
  623. m_fmLine = new Form1();
  624. m_fmLine.m_dYVMax = m_dMaxZV + 1.0;
  625. m_fmLine.m_dYVMin = m_dMinZV - 1.0;
  626. for (int i = 0; i < LINE_PNTS; i++)
  627. {
  628. m_fmLine.m_adValue[i] = m_adLineZV[i];
  629. }
  630. m_fmLine.Show();
  631. }
  632. private void overturn()
  633. {
  634. if (m_bOverTurn)
  635. {
  636. m_bOverTurn = false;
  637. m_adThisZV = m_adZV;
  638. m_adThisLineZV = m_adLineZV;
  639. }
  640. else
  641. {
  642. m_bOverTurn = true;
  643. m_adThisZV = m_adOverTurnZV;
  644. m_adThisLineZV = m_adLineOverTurnZV;
  645. }
  646. }
  647. private void ChangeColor()
  648. {
  649. if (m_bRulerColor)
  650. {
  651. m_bRulerColor = false;
  652. m_afThisClr = m_afOrgClr;
  653. }
  654. else
  655. {
  656. m_bRulerColor = true;
  657. m_afThisClr = m_afNewClr;
  658. }
  659. }
  660. /*
  661. private void Form_KeyUp(object sender, KeyEventArgs e) {
  662. keys[e.KeyValue] = false; // Key Has Been Released, Mark It As false
  663. }
  664. */
  665. private void Form_Resize(object sender, EventArgs e)
  666. {
  667. ReSizeGLScene(form.Width, form.Height); // Resize The OpenGL Window
  668. }
  669. private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
  670. {
  671. if ((m_iMusMod <= 0) || (m_iMusMod >= 7))
  672. {
  673. return;
  674. }
  675. //int iBtnDwn = Glfw.glfwGetMouseButton(Glfw.GLFW_MOUSE_BUTTON_LEFT);
  676. if (0 == m_iMusDown)
  677. {
  678. m_iLastMusDown = 0;
  679. return;
  680. }
  681. else if (0 == m_iLastMusDown)
  682. {
  683. m_iLastMusDown = 1;
  684. m_iLastmouseX = e.X;
  685. m_iLastmouseY = e.Y;
  686. return;
  687. }
  688. float fXChange = e.X - m_iLastmouseX;
  689. float fYChange = e.Y - m_iLastmouseY;
  690. m_fR_xyz[0] = AngleNormalization(m_fR_xyz[0]);
  691. m_fR_xyz[1] = AngleNormalization(m_fR_xyz[1]);
  692. if (1 == m_iMusMod)
  693. {
  694. if ((m_fR_xyz[0] > 90.0) && (m_fR_xyz[0] < 270.0))//先绕X轴转完了
  695. {
  696. fXChange *= -1.0F;
  697. }
  698. m_fR_xyz[0] += fYChange;
  699. m_fR_xyz[1] += fXChange;
  700. //m_fR_xyz[2] = 0.0F;
  701. }
  702. else if (2 == m_iMusMod)
  703. {
  704. m_fT_xyz[0] += fXChange * 0.1F;
  705. m_fT_xyz[1] += fYChange * -0.1F;
  706. }
  707. else if (5 == m_iMusMod)
  708. {
  709. if ((m_fR_xyz[0] > 90.0) && (m_fR_xyz[0] < 270.0))//先绕X轴转完了
  710. {
  711. fYChange *= -1.0F;
  712. }
  713. if ((m_fR_xyz[1] > 90.0) && (m_fR_xyz[1] < 270.0))//先绕X轴转完了
  714. {
  715. fXChange *= -1.0F;
  716. }
  717. m_afPonitA[0] += fXChange * 0.5F;
  718. m_afPonitA[0] = (float)Math.Min(Math.Max(m_afPonitA[0], 1.0), m_iWidth - 1);
  719. m_afPonitA[1] += fYChange * -0.5F;
  720. m_afPonitA[1] = (float)Math.Min(Math.Max(m_afPonitA[1], 1.0), m_iHeight - 1);
  721. CalculationIne();
  722. }
  723. else if (6 == m_iMusMod)
  724. {
  725. if ((m_fR_xyz[0] > 90.0) && (m_fR_xyz[0] < 270.0))//先绕X轴转完了
  726. {
  727. fYChange *= -1.0F;
  728. }
  729. if ((m_fR_xyz[1] > 90.0) && (m_fR_xyz[1] < 270.0))//先绕X轴转完了
  730. {
  731. fXChange *= -1.0F;
  732. }
  733. m_afPonitB[0] += fXChange * 0.5F;
  734. m_afPonitB[0] = (float)Math.Min(Math.Max(m_afPonitB[0], 1.0), m_iWidth - 1);
  735. m_afPonitB[1] += fYChange * -0.5F;
  736. m_afPonitB[1] = (float)Math.Min(Math.Max(m_afPonitB[1], 1.0), m_iHeight - 1);
  737. CalculationIne();
  738. }
  739. m_iLastmouseX = e.X;
  740. m_iLastmouseY = e.Y;
  741. }
  742. private float AngleNormalization(float fOldAngle)
  743. {
  744. float fNewAngle = fOldAngle % 360.0F;
  745. if (fNewAngle < 0)
  746. {
  747. fNewAngle += 360.0F;
  748. }
  749. return fNewAngle;
  750. }
  751. private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
  752. {
  753. if (e.Button == MouseButtons.Left)
  754. {
  755. m_iMusDown = 1;
  756. }
  757. }
  758. private void pictureBox1_MouseUp(object sender, MouseEventArgs e)
  759. {
  760. if (e.Button == MouseButtons.Left)
  761. {
  762. m_iMusDown = 0;
  763. }
  764. }
  765. private void pictureBox1_MouseLeave(object sender, EventArgs e)
  766. {
  767. m_iMusDown = 0;
  768. }
  769. }
  770. }