SurfacePlotWindow.cs 48 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997
  1. using System;
  2. using System.ComponentModel;
  3. using System.Windows.Forms;
  4. using System.Diagnostics;
  5. using System.Runtime.InteropServices;
  6. using Tao.OpenGl;
  7. using Tao.Platform.Windows;
  8. using OpenCvSharp;
  9. using System.Collections.Generic;
  10. using System.Drawing;
  11. namespace PaintDotNet.Instrument
  12. {
  13. public partial class SurfacePlotWindow : PdnBaseForm
  14. {
  15. private GroupBox groupBox1;
  16. // --- Fields ---
  17. private static IntPtr hDC; // Private GDI Device Context
  18. private static IntPtr hRC; // Permanent Rendering Context
  19. public static SurfacePlotWindow form; // Our Current Windows Form
  20. private static bool active = true; // Window Active Flag, Set To True By Default
  21. private static bool done = false; // Bool Variable To Exit Main Loop
  22. private AppWorkspace appWorkspace;
  23. /// <summary>
  24. /// 三维数据
  25. /// </summary>
  26. private Base.SettingModel.ThreeDataDemoModel threeDataDemo;
  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 static/* const*/ int LINE_PNTS = 100;
  45. //Glfw.GLFWkeyfun m_funKeyCallBak;
  46. //Glfw.GLFWmousebuttonfun m_funMsBtnCallBak;
  47. //Glfw.GLFWmouseposfun m_funMsPosCallBak;
  48. int m_iMusMod = 1;//0;
  49. int m_iLastmouseX, m_iLastmouseY;
  50. int m_iLastMusDown = 0;
  51. float[] m_fR_xyz = new float[3] { 0.0F, 0.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_afLineStep = new float[2] { 0.0F, 0.0F };
  58. float[,] m_afLineXY = new float[/*LINE_PNTS - 2*/98, 2];
  59. double[] m_adLineZV = new double[/*LINE_PNTS*/100];
  60. double[] m_adLineOverTurnZV = new double[/*LINE_PNTS*/100];
  61. //SRLinesForm m_fmLine;
  62. float[, ,] m_afThisClr;
  63. double[,] m_adThisZV;
  64. double[] m_adThisLineZV;
  65. internal SurfacePlotWindow(AppWorkspace appWorkspace)
  66. {
  67. this.appWorkspace = appWorkspace;
  68. InitializeComponent();
  69. InitializeLanguageText();
  70. this.CreateParams.ClassStyle = this.CreateParams.ClassStyle | // Redraw On Size, And Own DC For Window.
  71. User.CS_HREDRAW | User.CS_VREDRAW | User.CS_OWNDC;
  72. this.SetStyle(ControlStyles.AllPaintingInWmPaint, true); // No Need To Erase Form Background
  73. this.SetStyle(ControlStyles.DoubleBuffer, true); // Buffer Control
  74. //zzz this.SetStyle(ControlStyles.Opaque, true); // No Need To Draw Form Background
  75. this.SetStyle(ControlStyles.ResizeRedraw, true); // Redraw On Resize
  76. this.SetStyle(ControlStyles.UserPaint, true); // We'll Handle Painting Ourselves
  77. this.Activated += new EventHandler(this.Form_Activated); // On Activate Event Call Form_Activated
  78. this.Closing += new CancelEventHandler(this.Form_Closing); // On Closing Event Call Form_Closing
  79. this.Deactivate += new EventHandler(this.Form_Deactivate); // On Deactivate Event Call Form_Deactivate
  80. //this.KeyUp += new KeyEventHandler(this.Form_KeyUp); // On KeyUp Event Call Form_KeyUp
  81. this.Resize += new EventHandler(this.Form_Resize);
  82. //this.Close();
  83. ReadPar();
  84. }
  85. private void InitializeLanguageText()
  86. {
  87. this.groupBox1.Text = "";// "三维图像";
  88. this.Text = PdnResources.GetString("Menu.Tools.SurfacePlot.Text");
  89. }
  90. /// <summary>
  91. /// 初始化基础控件
  92. /// </summary>
  93. public void InitializeComponent()
  94. {
  95. this.groupBox1 = new System.Windows.Forms.GroupBox();
  96. this.SuspendLayout();
  97. //
  98. // groupBox1
  99. //
  100. this.groupBox1.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
  101. | System.Windows.Forms.AnchorStyles.Left)));
  102. this.groupBox1.Location = new System.Drawing.Point(20, 20);
  103. this.groupBox1.Margin = new System.Windows.Forms.Padding(4);
  104. this.groupBox1.Name = "groupBox1";
  105. this.groupBox1.Padding = new System.Windows.Forms.Padding(4);
  106. this.groupBox1.Size = new System.Drawing.Size(588, 424);
  107. this.groupBox1.TabIndex = 1;
  108. this.groupBox1.TabStop = false;
  109. //
  110. // SurfacePlotWindow
  111. //
  112. this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
  113. this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
  114. this.ClientSize = new System.Drawing.Size(612, 425);
  115. this.Controls.Add(this.groupBox1);
  116. this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle;
  117. this.Margin = new System.Windows.Forms.Padding(4);
  118. this.MaximizeBox = false;
  119. this.MaximumSize = new System.Drawing.Size(648, 504);
  120. this.MinimizeBox = false;
  121. this.MinimumSize = new System.Drawing.Size(648, 504);
  122. this.Name = "SurfacePlotWindow";
  123. this.Text = "SurfacePlotWindow";
  124. this.Controls.SetChildIndex(this.groupBox1, 0);
  125. this.ResumeLayout(false);
  126. }
  127. //private Mat ImMer;
  128. public Base.SettingModel.ThreeDataDemoModel CreateThreeDataDemoModel()
  129. {
  130. Mat origMat = this.appWorkspace.ActiveDocumentWorkspace.CompositionSurface.CreatedAliasedMat();
  131. string mode = "H";
  132. int maxSize = 640;
  133. if (origMat.Width > origMat.Height)
  134. mode = "W";
  135. Mat oriMat;
  136. if (maxSize >= origMat.Height && maxSize >= origMat.Width)
  137. oriMat = origMat;// PaintDotNet.Camera.Tools.ToMat(origin);
  138. else
  139. oriMat = Surface.MakeThumbnailMat(origMat, maxSize, maxSize, mode);//待测试
  140. ////string fileName = System.Windows.Forms.Application.StartupPath + "\\ThreeDataDemo_temp.xml";
  141. //this.ImMer = new Mat(oriMat.Size(), MatType.CV_8UC1/*4/*, oriMat.Type()*/);//Scalar.All(255))
  142. Mat matGray = oriMat.CvtColor(ColorConversionCodes.BGR2GRAY);
  143. int dstWidth = oriMat.Width;
  144. int matHeight = oriMat.Height;
  145. //dataValueList = new List<double>();
  146. //threedDataList = new List<List<double>>();
  147. threeDataDemo = new Base.SettingModel.ThreeDataDemoModel();// Base.CommTool.XmlSerializeHelper.DESerializer<Base.SettingModel.ThreeDataDemoModel>(Base.CommTool.FileOperationHelper.ReadStringFromFile(System.Windows.Forms.Application.StartupPath + "\\Config\\" + "Default"/*SettingPrefix*/ + "\\ThreeDataDemo.xml", System.IO.FileMode.Open));
  148. threeDataDemo.size = new Base.SettingModel.ThreeDataDemoModel.Size();//.Height = coll[0].Height;
  149. threeDataDemo.size.Height = matHeight;
  150. threeDataDemo.size.Width = dstWidth;
  151. threeDataDemo.Z_value = new List<string>();
  152. for (int threeD = 0; threeD < dstWidth; threeD++)
  153. {
  154. List<double> threedData = new List<double>();
  155. for (int indexY = 0; indexY < matHeight; indexY++)
  156. threedData.Add(matGray.At<byte>(indexY, threeD));
  157. //threedDataList.Add(threedData);
  158. threeDataDemo.Z_value.Add(string.Join(",", threedData.ToArray()));
  159. }
  160. //coll[0].ForEachAsDouble(DefineProfileForEachAsVec4bForHeight);
  161. //if (this.ImMer != null)
  162. //{
  163. // Mat matGray = this.ImMer.CvtColor(ColorConversionCodes.BGR2GRAY);
  164. // for (int index_x = 0; index_x < threedDataList.Count; index_x++)
  165. // {
  166. // List<double> threedData = threedDataList[index_x];
  167. // for (int index_y = 0; index_y < matHeight; index_y++)
  168. // {//高度与颜色相乘
  169. // byte vec = matGray.At<byte>(index_y, index_x);
  170. // threedData[index_y] = vec;// threedData[index_y] * vec / 255.0;
  171. // }
  172. // threeDataDemo.Z_value.Add(string.Join(",", threedData.ToArray()));
  173. // }
  174. //}
  175. threeDataDemo.Z_level = "16.000000,0.000000";
  176. if (threeDataDemo.Dyeing == null || threeDataDemo.Dyeing.Count == 0)
  177. threeDataDemo.Dyeing = new List<string>();
  178. //threeDataDemo.Dyeing.Add("100.00,0.00,100.00");
  179. threeDataDemo.Dyeing.Add("255.00,0.00,0.00");
  180. threeDataDemo.Dyeing.Add("0.00,255.00,0.00");
  181. return threeDataDemo;
  182. }
  183. public void ReadPar()
  184. {
  185. threeDataDemo = CreateThreeDataDemoModel();// Base.CommTool.XmlSerializeHelper.DESerializer<Base.SettingModel.ThreeDataDemoModel>(Base.CommTool.FileOperationHelper.ReadStringFromFile(fileName, System.IO.FileMode.Open));
  186. //XmlDocument xd = new XmlDocument();
  187. //string fileName = @"C:\Users\win10SSD\Desktop\工作目录\ThreeDDemo\ThreeDDemo20201029\ThreeDataDemo_00.xml";
  188. ////string fileName = @"C:\Users\win10SSD\Desktop\工作目录\ThreeDDemo\ThreeDSR20201108\ThreeDSR\ThreeDData.xml";
  189. //xd.Load(fileName);
  190. string str;
  191. string[] strarr;
  192. if (threeDataDemo.size.Width < 1)
  193. //if (xmlNoteList.Count < 1)
  194. {
  195. MessageBox.Show("XML文件错");
  196. return;
  197. }
  198. str = threeDataDemo.size.Width.ToString();
  199. m_iWidth = int.Parse(str);
  200. str = threeDataDemo.size.Height.ToString();
  201. m_iHeight = int.Parse(str);
  202. if (threeDataDemo.Z_level == null)
  203. //if (xmlNoteList.Count < 1)
  204. {
  205. MessageBox.Show("XML文件错");
  206. //return;
  207. }
  208. else
  209. {
  210. str = threeDataDemo.Z_level.ToString();
  211. strarr = str.Split(',');
  212. m_iDLevel = strarr.Length;
  213. m_adDZ = new double[m_iDLevel];
  214. m_adDC = new double[m_iDLevel, 3];
  215. for (int i = 0; i < m_iDLevel; i++)
  216. {
  217. m_adDZ[i] = double.Parse(strarr[i]);
  218. }
  219. }
  220. //xmlNoteList = xd.GetElementsByTagName("colour");
  221. if (threeDataDemo.Dyeing.Count < m_iDLevel)
  222. //if (xmlNoteList.Count < m_iDLevel)
  223. {
  224. MessageBox.Show("XML文件错");
  225. return;
  226. }
  227. int iX = 0;
  228. for (int dyeingIndex = 0; dyeingIndex < threeDataDemo.Dyeing.Count; dyeingIndex++)
  229. //foreach (XmlElement item in xmlNoteList)
  230. {
  231. str = threeDataDemo.Dyeing[dyeingIndex].ToString();
  232. //str = item.InnerXml;
  233. strarr = str.Split(',');
  234. if (strarr.Length < 3)
  235. {
  236. MessageBox.Show("XML文件错");
  237. return;
  238. }
  239. m_adDC[iX, 0] = double.Parse(strarr[0]);
  240. m_adDC[iX, 1] = double.Parse(strarr[1]);
  241. m_adDC[iX, 2] = double.Parse(strarr[2]);
  242. iX++;
  243. }
  244. //xmlNoteList = xd.GetElementsByTagName("Column");
  245. if (threeDataDemo.Z_value.Count < m_iWidth)
  246. //if (xmlNoteList.Count < m_iWidth)
  247. {
  248. MessageBox.Show("XML文件错");
  249. return;
  250. }
  251. m_adZV = new double[m_iWidth, m_iHeight];
  252. bool bFirst = true;
  253. iX = 0;
  254. for (int Z_valueIndex = 0; Z_valueIndex < threeDataDemo.Z_value.Count; Z_valueIndex++)
  255. //foreach (XmlElement item in xmlNoteList)
  256. {
  257. str = threeDataDemo.Z_value[Z_valueIndex].ToString();
  258. //str = item.InnerXml;
  259. strarr = str.Split(',');
  260. if (strarr.Length < m_iHeight)
  261. {
  262. MessageBox.Show("XML文件错");
  263. return;
  264. }
  265. for (int iY = 0; iY < m_iHeight; iY++)
  266. {
  267. m_adZV[iX, m_iHeight - 1 - iY] = double.Parse(strarr[iY]);
  268. if ((bFirst) || (m_adZV[iX, m_iHeight - 1 - iY] > m_dMaxZV))
  269. {
  270. m_dMaxZV = m_adZV[iX, m_iHeight - 1 - iY];
  271. }
  272. if ((bFirst) || (m_adZV[iX, m_iHeight - 1 - iY] < m_dMinZV))
  273. {
  274. m_dMinZV = m_adZV[iX, m_iHeight - 1 - iY];
  275. bFirst = false;
  276. }
  277. }
  278. iX++;
  279. }
  280. //处理剖面线参数
  281. m_afPonitB[0] = m_iWidth - 1;
  282. m_afPonitB[1] = m_iHeight - 1;
  283. CalculationIne();
  284. //处理颜色和翻转参数
  285. m_afNewClr = new float[m_iWidth, m_iHeight, 3];
  286. m_adOverTurnZV = new double[m_iWidth, m_iHeight];
  287. //for (iX = 0; iX < m_iWidth; iX++)
  288. //{
  289. // for (int iY = 0; iY < m_iHeight; iY++)
  290. // {
  291. // float[] fColor = CalculationColorf(m_adZV[iX, iY]);
  292. // m_afNewClr[iX, iY, 0] = fColor[0];
  293. // m_afNewClr[iX, iY, 1] = fColor[1];
  294. // m_afNewClr[iX, iY, 2] = fColor[2];
  295. // m_adOverTurnZV[iX, iY] = m_dMinZV + (m_dMaxZV - m_adZV[iX, iY]);
  296. // }
  297. //}
  298. //fileName = System.Windows.Forms.Application.StartupPath/* + "\\Config\\" + "Default"*//*SettingPrefix*/ + "\\ThreeDataDemo_temp.bmp";
  299. ////fileName = @"C:\Users\win10SSD\Desktop\工作目录\ThreeDDemo\ThreeDDemo20201029\d2.bmp";
  300. ////fileName = @"C:\Users\win10SSD\Desktop\工作目录\ThreeDDemo\ThreeDSR20201108\ThreeDSR\d1.bmp";
  301. Mat origMat = this.appWorkspace.ActiveDocumentWorkspace.CompositionSurface.CreatedAliasedMat();
  302. string mode = "H";
  303. int maxSize = 640;
  304. if (origMat.Width > origMat.Height)
  305. mode = "W";
  306. Mat OrgImg;
  307. if (maxSize >= origMat.Height && maxSize >= origMat.Width)
  308. OrgImg = origMat;// PaintDotNet.Camera.Tools.ToMat(origin);
  309. else
  310. OrgImg = Surface.MakeThumbnailMat(origMat, maxSize, maxSize, mode);//待测试
  311. if ((m_iWidth != OrgImg.Cols) || (m_iHeight != OrgImg.Rows))//行数Rows是Y,列数是X
  312. {
  313. MessageBox.Show("图像尺寸和XML文件矛盾");
  314. return;
  315. }
  316. m_afOrgClr = new float[m_iWidth, m_iHeight, 3];
  317. int[] point = new int[2];
  318. Vec3b OrgClr;
  319. for (point[0] = 0; point[0] < m_iHeight; point[0]++)
  320. {
  321. for (point[1] = 0; point[1] < m_iWidth; point[1]++)
  322. {
  323. OrgClr = OrgImg.At<Vec3b>(point[0], point[1]);
  324. m_afOrgClr[point[1], m_iHeight - 1 - point[0], 0] = OrgClr.Item2 / 255.0F;
  325. m_afOrgClr[point[1], m_iHeight - 1 - point[0], 1] = OrgClr.Item1 / 255.0F;
  326. m_afOrgClr[point[1], m_iHeight - 1 - point[0], 2] = OrgClr.Item0 / 255.0F;
  327. }
  328. }
  329. m_bRulerColor = false;
  330. m_afThisClr = m_afOrgClr;
  331. m_bOverTurn = false;
  332. m_adThisZV = m_adZV;
  333. m_adThisLineZV = m_adLineZV;
  334. ChangeColor();
  335. }
  336. public float[] CalculationColorf(double dZ)
  337. {
  338. float[] fColor = new float[3];
  339. if (dZ < m_adDZ[m_iDLevel - 1])
  340. {
  341. fColor[0] = (float)m_adDC[m_iDLevel - 1, 0];
  342. fColor[1] = (float)m_adDC[m_iDLevel - 1, 1];
  343. fColor[2] = (float)m_adDC[m_iDLevel - 1, 2];
  344. return fColor;
  345. }
  346. if (dZ > m_adDZ[0])
  347. {
  348. fColor[0] = (float)m_adDC[0, 0];
  349. fColor[1] = (float)m_adDC[0, 1];
  350. fColor[2] = (float)m_adDC[0, 2];
  351. return fColor;
  352. }
  353. int i, j;
  354. for (j = 1; j < m_iDLevel; j++)
  355. {
  356. if (dZ > m_adDZ[j])
  357. {
  358. break;
  359. }
  360. }
  361. i = j - 1;
  362. double dIJ = Math.Abs(m_adDZ[i] - m_adDZ[j]);
  363. double dWI = Math.Abs(dZ - m_adDZ[j]) / dIJ;
  364. double dWJ = Math.Abs(m_adDZ[i] - dZ) / dIJ;
  365. fColor[0] = (float)((m_adDC[i, 0] * dWI + m_adDC[j, 0] * dWJ) / 255.0);
  366. fColor[1] = (float)((m_adDC[i, 1] * dWI + m_adDC[j, 1] * dWJ) / 255.0);
  367. fColor[2] = (float)((m_adDC[i, 2] * dWI + m_adDC[j, 2] * dWJ) / 255.0);
  368. return fColor;
  369. }
  370. private void CalculationIne()
  371. {
  372. LINE_PNTS = (int)Math.Sqrt((m_afPonitB[0] - m_afPonitA[0]) * (m_afPonitB[0] - m_afPonitA[0]) + (m_afPonitB[1] - m_afPonitA[1]) * (m_afPonitB[1] - m_afPonitA[1]));
  373. if (LINE_PNTS < 3)
  374. LINE_PNTS = 3;
  375. m_afLineStep[0] = (m_afPonitB[0] - m_afPonitA[0]) / (LINE_PNTS - 1);
  376. m_afLineStep[1] = (m_afPonitB[1] - m_afPonitA[1]) / (LINE_PNTS - 1);
  377. m_afLineXY = new float[LINE_PNTS - 2, 2];
  378. m_adLineZV = new double[LINE_PNTS];
  379. m_adLineOverTurnZV = new double[LINE_PNTS];
  380. if (!m_bOverTurn)
  381. m_adThisLineZV = m_adLineZV;
  382. else
  383. m_adThisLineZV = m_adLineOverTurnZV;
  384. m_adLineZV[0] = CalculationInterpolation(m_afPonitA[0], m_afPonitA[1]);
  385. m_adLineOverTurnZV[0] = m_dMinZV + (m_dMaxZV - m_adLineZV[0]);
  386. for (int i = 0; i < (LINE_PNTS - 2); i++)
  387. {
  388. m_afLineXY[i, 0] = m_afPonitA[0] + (i + 1) * m_afLineStep[0];
  389. m_afLineXY[i, 1] = m_afPonitA[1] + (i + 1) * m_afLineStep[1];
  390. m_adLineZV[i + 1] = CalculationInterpolation(m_afLineXY[i, 0], m_afLineXY[i, 1]);
  391. m_adLineOverTurnZV[i + 1] = m_dMinZV + (m_dMaxZV - m_adLineZV[i + 1]);
  392. }
  393. m_adLineZV[LINE_PNTS - 1] = CalculationInterpolation(m_afPonitB[0], m_afPonitB[1]);
  394. m_adLineOverTurnZV[LINE_PNTS - 1] = m_dMinZV + (m_dMaxZV - m_adLineZV[LINE_PNTS - 1]);
  395. }
  396. private double CalculationInterpolation(float fX, float fY)
  397. {
  398. int iXS = Math.Min(Math.Max((int)(fX), 0), m_iWidth - 1);
  399. int iYS = Math.Min(Math.Max((int)(fY), 0), m_iHeight - 1);
  400. int iXB = Math.Min(Math.Max((int)(fX + 1.0), 0), m_iWidth - 1);
  401. int iYB = Math.Min(Math.Max((int)(fY + 1.0), 0), m_iHeight - 1);
  402. double dWXB = Math.Min(Math.Max((fX - (double)iXS), 0.0), 1.0);
  403. double dWXS = 1.0 - dWXB;
  404. double dWYB = Math.Min(Math.Max((fY - (double)iYS), 0.0), 1.0);
  405. double dWYS = 1.0 - dWYB;
  406. double dTempA = m_adZV[iXS, iYS];
  407. double dTempB = m_adZV[iXS, iYB];
  408. double dTempC = m_adZV[iXB, iYS];
  409. double dTempD = m_adZV[iXB, iYB];
  410. double dTempAB = dTempA * dWYS + dTempB * dWYB;
  411. double dTempCD = dTempC * dWYS + dTempD * dWYB;
  412. double dRtn = dTempAB * dWXS + dTempCD * dWXB;
  413. return dRtn;// dTempA;//
  414. }
  415. private static void PictureBox1_Paint(object sender, PaintEventArgs e)
  416. {
  417. //Application.DoEvents(); // Process Events
  418. //// Draw The Scene. Watch For ESC Key And Quit Messages From DrawGLScene()
  419. //if ((active && (form != null) && !DrawGLScene()) /*|| keys[(int) Keys.Escape]*/)
  420. //{ // Active? Was There A Quit Received?
  421. // done = true; // ESC Or DrawGLScene Signalled A Quit
  422. //}
  423. //else
  424. //{ // Not Time To Quit, Update Screen
  425. // Gdi.SwapBuffers(hDC); // Swap Buffers (Double Buffering)
  426. //}
  427. }
  428. internal static void Run(AppWorkspace appWorkspace) {
  429. if (!done && (form != null))
  430. {
  431. form.ReadPar();
  432. form.Focus();
  433. return;
  434. }
  435. done = false;
  436. // Create Our OpenGL Window
  437. if (!CreateGLWindow(appWorkspace, PdnResources.GetString("Menu.Tools.SurfacePlot.Text"), 990, 690, 16)) {
  438. return; // Quit If Window Was Not Created
  439. }
  440. while (!done)
  441. { // Loop That Runs While done = false
  442. Application.DoEvents(); // Process Events
  443. // Draw The Scene. Watch For ESC Key And Quit Messages From DrawGLScene()
  444. if ((active && (form != null) && !DrawGLScene()) /*|| keys[(int) Keys.Escape]*/)
  445. { // Active? Was There A Quit Received?
  446. done = true; // ESC Or DrawGLScene Signalled A Quit
  447. }
  448. else
  449. { // Not Time To Quit, Update Screen
  450. Gdi.SwapBuffers(hDC); // Swap Buffers (Double Buffering)
  451. }
  452. /*
  453. if(keys[(int) Keys.F1]) { // Is F1 Being Pressed?
  454. keys[(int) Keys.F1] = false; // If So Make Key false
  455. KillGLWindow(); // Kill Our Current Window
  456. // Recreate Our OpenGL Window
  457. if(!CreateGLWindow("三维面绘制", 640, 480, 16)) {
  458. return; // Quit If Window Was Not Created
  459. }
  460. done = false; // We're Not Done Yet
  461. }
  462. */
  463. }
  464. // Shutdown
  465. KillGLWindow(); // Kill The Window
  466. return; // Exit The Program
  467. }
  468. private static bool CreateGLWindow(AppWorkspace appWorkspace, string title, int width, int height, int bits)
  469. {
  470. int pixelFormat; // Holds The Results After Searching For A Match
  471. form = null; // Null The Form
  472. GC.Collect(); // Request A Collection
  473. // This Forces A Swap
  474. Kernel.SetProcessWorkingSetSize(Process.GetCurrentProcess().Handle, -1, -1);
  475. form = new SurfacePlotWindow(appWorkspace); // Create The Window
  476. form.FormBorderStyle = FormBorderStyle.Sizable; // Sizable
  477. Cursor.Show(); // Show Mouse Pointer
  478. form.Width = width; // Set Window Width
  479. form.Height = height; // Set Window Height
  480. form.Text = title; // Set Window Title
  481. Gdi.PIXELFORMATDESCRIPTOR pfd = new Gdi.PIXELFORMATDESCRIPTOR(); // pfd Tells Windows How We Want Things To Be
  482. pfd.nSize = (short) Marshal.SizeOf(pfd); // Size Of This Pixel Format Descriptor
  483. pfd.nVersion = 1; // Version Number
  484. pfd.dwFlags = Gdi.PFD_DRAW_TO_WINDOW | // Format Must Support Window
  485. Gdi.PFD_SUPPORT_OPENGL | // Format Must Support OpenGL
  486. Gdi.PFD_DOUBLEBUFFER; // Format Must Support Double Buffering
  487. pfd.iPixelType = (byte) Gdi.PFD_TYPE_RGBA; // Request An RGBA Format
  488. pfd.cColorBits = (byte) bits; // Select Our Color Depth
  489. pfd.cRedBits = 0; // Color Bits Ignored
  490. pfd.cRedShift = 0;
  491. pfd.cGreenBits = 0;
  492. pfd.cGreenShift = 0;
  493. pfd.cBlueBits = 0;
  494. pfd.cBlueShift = 0;
  495. pfd.cAlphaBits = 0; // No Alpha Buffer
  496. pfd.cAlphaShift = 0; // Shift Bit Ignored
  497. pfd.cAccumBits = 0; // No Accumulation Buffer
  498. pfd.cAccumRedBits = 0; // Accumulation Bits Ignored
  499. pfd.cAccumGreenBits = 0;
  500. pfd.cAccumBlueBits = 0;
  501. pfd.cAccumAlphaBits = 0;
  502. pfd.cDepthBits = 16; // 16Bit Z-Buffer (Depth Buffer)
  503. pfd.cStencilBits = 0; // No Stencil Buffer
  504. pfd.cAuxBuffers = 0; // No Auxiliary Buffer
  505. pfd.iLayerType = (byte) Gdi.PFD_MAIN_PLANE; // Main Drawing Layer
  506. pfd.bReserved = 0; // Reserved
  507. pfd.dwLayerMask = 0; // Layer Masks Ignored
  508. pfd.dwVisibleMask = 0;
  509. pfd.dwDamageMask = 0;
  510. //hDC = User.GetDC(form.Handle); // Attempt To Get A Device Context
  511. form.pictureBox1 = new PictureBox();
  512. ((System.ComponentModel.ISupportInitialize)(form.pictureBox1)).BeginInit();
  513. //form.pictureBox1.Location = new System.Drawing.Point(22, 40);
  514. //form.pictureBox1.Location = new System.Drawing.Point(20, 180);
  515. form.pictureBox1.Location = new System.Drawing.Point(15, 18);
  516. form.pictureBox1.Name = "pictureBox1";
  517. form.pictureBox1.Size = new System.Drawing.Size(560, 400);
  518. //form.pictureBox1.Size = new System.Drawing.Size(1272, 651);
  519. form.pictureBox1.TabIndex = 0;
  520. form.pictureBox1.TabStop = false;
  521. /*form*/form.groupBox1.Controls.Add(form.pictureBox1);
  522. ((System.ComponentModel.ISupportInitialize)(form.pictureBox1)).EndInit();
  523. form.pictureBox1.Paint += PictureBox1_Paint;
  524. form.pictureBox1.MouseMove += new MouseEventHandler(form.pictureBox1_MouseMove);
  525. form.pictureBox1.MouseDown += new MouseEventHandler(form.pictureBox1_MouseDown);
  526. form.pictureBox1.MouseUp += new MouseEventHandler(form.pictureBox1_MouseUp);
  527. form.pictureBox1.MouseLeave += new EventHandler(form.pictureBox1_MouseLeave);
  528. hDC = User.GetDC(form.pictureBox1.Handle);
  529. if(hDC == IntPtr.Zero) { // Did We Get A Device Context?
  530. KillGLWindow(); // Reset The Display
  531. MessageBox.Show("Can't Create A GL Device Context.", "ERROR",
  532. MessageBoxButtons.OK, MessageBoxIcon.Error);
  533. return false;
  534. }
  535. pixelFormat = Gdi.ChoosePixelFormat(hDC, ref pfd); // Attempt To Find An Appropriate Pixel Format
  536. if(pixelFormat == 0) { // Did Windows Find A Matching Pixel Format?
  537. KillGLWindow(); // Reset The Display
  538. MessageBox.Show("Can't Find A Suitable PixelFormat.", "ERROR",
  539. MessageBoxButtons.OK, MessageBoxIcon.Error);
  540. return false;
  541. }
  542. if(!Gdi.SetPixelFormat(hDC, pixelFormat, ref pfd)) { // Are We Able To Set The Pixel Format?
  543. KillGLWindow(); // Reset The Display
  544. MessageBox.Show("Can't Set The PixelFormat.", "ERROR",
  545. MessageBoxButtons.OK, MessageBoxIcon.Error);
  546. return false;
  547. }
  548. hRC = Wgl.wglCreateContext(hDC); // Attempt To Get The Rendering Context
  549. if(hRC == IntPtr.Zero) { // Are We Able To Get A Rendering Context?
  550. KillGLWindow(); // Reset The Display
  551. MessageBox.Show("Can't Create A GL Rendering Context.", "ERROR",
  552. MessageBoxButtons.OK, MessageBoxIcon.Error);
  553. return false;
  554. }
  555. if(!Wgl.wglMakeCurrent(hDC, hRC)) { // Try To Activate The Rendering Context
  556. KillGLWindow(); // Reset The Display
  557. MessageBox.Show("Can't Activate The GL Rendering Context.", "ERROR",
  558. MessageBoxButtons.OK, MessageBoxIcon.Error);
  559. return false;
  560. }
  561. form.Show(appWorkspace); // Show The Window
  562. //zzz form.TopMost = true; // Topmost Window
  563. form.Focus(); // Focus The Window
  564. //form.BackColor = Color.Gray;
  565. ////form.WindowState = FormWindowState.Maximized;
  566. //ReSizeGLScene(width, height); // Set Up Our Perspective GL Screen
  567. if (!InitGL())
  568. { // Initialize Our Newly Created GL Window
  569. KillGLWindow(); // Reset The Display
  570. MessageBox.Show("Initialization Failed.", "ERROR",
  571. MessageBoxButtons.OK, MessageBoxIcon.Error);
  572. return false;
  573. }
  574. return true; // Success
  575. }
  576. private static bool DrawGLScene()
  577. {
  578. //new platform
  579. iniView();
  580. Gl.glTranslatef(0, 0, 0);
  581. myRotate();
  582. //错了 Gl.glTranslatef(m_fT_xyz[0],m_fT_xyz[1],0);
  583. for (int i = 0; i < form.m_iWidth - 1; i++)
  584. {
  585. Gl.glBegin(Gl.GL_TRIANGLE_STRIP);
  586. for (int j = 0; j < form.m_iHeight; j++)
  587. {
  588. DrowOnePnt(i, j);
  589. DrowOnePnt(i + 1, j);
  590. }
  591. Gl.glEnd();
  592. }
  593. //if ((form.m_iMusMod >= 5) && (form.m_iMusMod <= 7) || true)
  594. //{
  595. // Gl.glBegin(Gl.GL_QUADS);
  596. // Gl.glColor3f(1.0F, 1.0F, 0.0F);
  597. // Gl.glVertex3d(form.m_afPonitA[0] - 10 - form.m_iWidth / 2.0, form.m_afPonitA[1] - 10 - form.m_iHeight / 2.0, 1.0 + form.m_adThisLineZV[0] - (form.m_dMaxZV + form.m_dMinZV) / 2.0);
  598. // Gl.glVertex3d(form.m_afPonitA[0] + 10 - form.m_iWidth / 2.0, form.m_afPonitA[1] - 10 - form.m_iHeight / 2.0, 1.0 + form.m_adThisLineZV[0] - (form.m_dMaxZV + form.m_dMinZV) / 2.0);
  599. // Gl.glVertex3d(form.m_afPonitA[0] + 10 - form.m_iWidth / 2.0, form.m_afPonitA[1] + 10 - form.m_iHeight / 2.0, 1.0 + form.m_adThisLineZV[0] - (form.m_dMaxZV + form.m_dMinZV) / 2.0);
  600. // Gl.glVertex3d(form.m_afPonitA[0] - 10 - form.m_iWidth / 2.0, form.m_afPonitA[1] + 10 - form.m_iHeight / 2.0, 1.0 + form.m_adThisLineZV[0] - (form.m_dMaxZV + form.m_dMinZV) / 2.0);
  601. // Gl.glEnd();
  602. // Gl.glBegin(Gl.GL_LINE_STRIP);
  603. // Gl.glColor3f(0.0F, 1.0F, 0.0F);
  604. // Gl.glVertex3d(form.m_afPonitA[0] - form.m_iWidth / 2.0, form.m_afPonitA[1] - form.m_iHeight / 2.0, 1.0 + form.m_adThisLineZV[0] - (form.m_dMaxZV + form.m_dMinZV) / 2.0);
  605. // for (int i = 0; i < (LINE_PNTS - 2); i++)
  606. // {
  607. // Gl.glVertex3d(form.m_afLineXY[i, 0] - form.m_iWidth / 2.0, form.m_afLineXY[i, 1] - form.m_iHeight / 2.0, 1.0 + /*form.m_adThisZV[(int)form.m_afLineXY[i, 0], (int)form.m_afLineXY[i, 1]]*/form.m_adThisLineZV[i + 1] - (form.m_dMaxZV + form.m_dMinZV) / 2.0);
  608. // }
  609. // Gl.glVertex3d(form.m_afPonitB[0] - form.m_iWidth / 2.0, form.m_afPonitB[1] - form.m_iHeight / 2.0, 1.0 + form.m_adThisLineZV[LINE_PNTS - 1] - (form.m_dMaxZV + form.m_dMinZV) / 2.0);
  610. // Gl.glEnd();
  611. // Gl.glBegin(Gl.GL_QUADS);
  612. // Gl.glColor3f(0.0F, 1.0F, 01.0F);
  613. // Gl.glVertex3d(form.m_afPonitB[0] - 10 - form.m_iWidth / 2.0, form.m_afPonitB[1] - 10 - form.m_iHeight / 2.0, 1.0 + form.m_adThisLineZV[LINE_PNTS - 1] - (form.m_dMaxZV + form.m_dMinZV) / 2.0);
  614. // Gl.glVertex3d(form.m_afPonitB[0] + 10 - form.m_iWidth / 2.0, form.m_afPonitB[1] - 10 - form.m_iHeight / 2.0, 1.0 + form.m_adThisLineZV[LINE_PNTS - 1] - (form.m_dMaxZV + form.m_dMinZV) / 2.0);
  615. // Gl.glVertex3d(form.m_afPonitB[0] + 10 - form.m_iWidth / 2.0, form.m_afPonitB[1] + 10 - form.m_iHeight / 2.0, 1.0 + form.m_adThisLineZV[LINE_PNTS - 1] - (form.m_dMaxZV + form.m_dMinZV) / 2.0);
  616. // Gl.glVertex3d(form.m_afPonitB[0] - 10 - form.m_iWidth / 2.0, form.m_afPonitB[1] + 10 - form.m_iHeight / 2.0, 1.0 + form.m_adThisLineZV[LINE_PNTS - 1] - (form.m_dMaxZV + form.m_dMinZV) / 2.0);
  617. // Gl.glEnd();
  618. //}
  619. Gl.glLoadIdentity();
  620. return true;
  621. }
  622. private static void myRotate()
  623. {
  624. int[,] vec = new int[3, 3] { { 1, 0, 0 }, { 0, 1, 0 }, { 0, 0, 1 } };
  625. for (int i = 0; i <= 2; i++)
  626. {
  627. Gl.glRotatef(form.m_fR_xyz[i], vec[i, 0], vec[i, 1], vec[i, 2]);
  628. }
  629. }
  630. public static void DrowOnePnt(int i, int j)
  631. {
  632. Gl.glColor3f(form.m_afThisClr[i, j, 0], form.m_afThisClr[i, j, 1], form.m_afThisClr[i, j, 2]);
  633. 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);
  634. }
  635. private static bool InitGL()
  636. {
  637. //Gl.glShadeModel(Gl.GL_SMOOTH); // Enable Smooth Shading
  638. //Gl.glClearColor(0, 0, 0, 0.5f); // Black Background
  639. //Gl.glClearDepth(1); // Depth Buffer Setup
  640. Gl.glEnable(Gl.GL_DEPTH_TEST); // Enables Depth Testing
  641. //Gl.glDepthFunc(Gl.GL_LEQUAL); // The Type Of Depth Testing To Do
  642. //Gl.glHint(Gl.GL_PERSPECTIVE_CORRECTION_HINT, Gl.GL_NICEST); // Really Nice Perspective Calculations
  643. // 设置视口 viewport
  644. Gl.glViewport(0, 0, form.pictureBox1.Width, form.pictureBox1.Height);
  645. //启用阴影平滑
  646. Gl.glShadeModel(Gl.GL_SMOOTH);
  647. //启用反走样
  648. Gl.glHint(Gl.GL_PERSPECTIVE_CORRECTION_HINT, Gl.GL_NICEST);
  649. // 设置投影模式 projection matrix
  650. Gl.glMatrixMode(Gl.GL_PROJECTION);
  651. Gl.glLoadIdentity();
  652. iniView();
  653. return true;
  654. }
  655. public static void iniView(bool clearColor = true)
  656. {
  657. // 重置黑色背景
  658. if (clearColor)
  659. {
  660. Gl.glClearColor((float)(0 / 255.0), (float)(0 / 255.0), (float)(0 / 255.0), (float)(255 / 255.0));
  661. Gl.glClear(Gl.GL_COLOR_BUFFER_BIT | Gl.GL_DEPTH_BUFFER_BIT);
  662. }
  663. else
  664. return;
  665. form.m_fT_xyz[2] = (float)Math.Max(form.m_dMaxZV, Math.Abs(form.m_dMinZV));
  666. form.m_fT_xyz[2] = (float)(-1.0 * Math.Max(Math.Max(form.m_iWidth * 0.98, form.m_iHeight * 0.98), form.m_fT_xyz[2]));
  667. ////form.m_fT_xyz[2] = (float)(1.2 * form.m_fT_xyz[2]);
  668. ////Glu.gluPerspective(85, form.pictureBox1.Width / (double)form.pictureBox1.Height, 1, -4.0/*-2.0*/ * form.m_fT_xyz[2]);//距离裁减
  669. //Gl.glOrtho(0, form.pictureBox1.Width, form.pictureBox1.Height, 0, 1, -4.0/*-2.0*/ * form.m_fT_xyz[2]);
  670. double size1 = form.m_iHeight / 1.0;
  671. Gl.glOrtho(-size1 * form.pictureBox1.Width / (double)form.pictureBox1.Height, size1 * form.pictureBox1.Width / (double)form.pictureBox1.Height, -size1, size1, 2.4 * form.m_fT_xyz[2], -2.4 * form.m_fT_xyz[2]);
  672. // 选择模型观察矩阵 modelview matrix
  673. Gl.glMatrixMode(Gl.GL_MODELVIEW);
  674. //重置模型观察矩阵
  675. Gl.glLoadIdentity();
  676. Glu.gluLookAt(-1.0 * form.m_fT_xyz[0], -1.0 * form.m_fT_xyz[1], -1.0/*-1.0*/ * form.m_fT_xyz[2], // 眼睛位置
  677. -1.0 * form.m_fT_xyz[0], -1.0 * form.m_fT_xyz[1], 0, // 观察点
  678. 0, 1, 0);
  679. }
  680. private static void KillGLWindow() {
  681. if(hRC != IntPtr.Zero) { // Do We Have A Rendering Context?
  682. if(!Wgl.wglMakeCurrent(IntPtr.Zero, IntPtr.Zero)) { // Are We Able To Release The DC and RC Contexts?
  683. MessageBox.Show("Release Of DC And RC Failed.", "SHUTDOWN ERROR",
  684. MessageBoxButtons.OK, MessageBoxIcon.Error);
  685. }
  686. if(!Wgl.wglDeleteContext(hRC)) { // Are We Able To Delete The RC?
  687. MessageBox.Show("Release Rendering Context Failed.", "SHUTDOWN ERROR",
  688. MessageBoxButtons.OK, MessageBoxIcon.Error);
  689. }
  690. hRC = IntPtr.Zero; // Set RC To Null
  691. }
  692. if(hDC != IntPtr.Zero) { // Do We Have A Device Context?
  693. if(form != null && !form.IsDisposed) { // Do We Have A Window?
  694. if(form.Handle != IntPtr.Zero) { // Do We Have A Window Handle?
  695. if(!User.ReleaseDC(form.Handle, hDC)) { // Are We Able To Release The DC?
  696. MessageBox.Show("Release Device Context Failed.", "SHUTDOWN ERROR",
  697. MessageBoxButtons.OK, MessageBoxIcon.Error);
  698. }
  699. }
  700. }
  701. hDC = IntPtr.Zero; // Set DC To Null
  702. }
  703. if(form != null) { // Do We Have A Windows Form?
  704. form.Hide(); // Hide The Window
  705. form.Close(); // Close The Form
  706. form = null; // Set form To Null
  707. }
  708. }
  709. private static void ReSizeGLScene(int width, int height) {
  710. if(height == 0) { // Prevent A Divide By Zero...
  711. height = 1; // By Making Height Equal To One
  712. }
  713. Gl.glViewport(0, 0, width, height); // Reset The Current Viewport
  714. Gl.glMatrixMode(Gl.GL_PROJECTION); // Select The Projection Matrix
  715. Gl.glLoadIdentity(); // Reset The Projection Matrix
  716. Glu.gluPerspective(45, width / (double) height, 0.1, 100); // Calculate The Aspect Ratio Of The Window
  717. Gl.glMatrixMode(Gl.GL_MODELVIEW); // Select The Modelview Matrix
  718. Gl.glLoadIdentity(); // Reset The Modelview Matrix
  719. }
  720. private void Form_Activated(object sender, EventArgs e)
  721. {
  722. active = true; // Program Is Active
  723. }
  724. private void Form_Closing(object sender, CancelEventArgs e)
  725. {
  726. done = true; // Send A Quit Message
  727. }
  728. private void Form_Deactivate(object sender, EventArgs e)
  729. {
  730. active = false; // Program Is No Longer Active
  731. }
  732. //zzz
  733. public int m_iYVCount = 10;
  734. public double m_dYVMax = 51;
  735. public double m_dYVMin = -51;
  736. /// <summary>
  737. /// Z轴高度集合,从A到B按照均匀取到了100个点
  738. /// </summary>
  739. private double[] m_adValue = new double[LINE_PNTS];
  740. public int m_iYPixelMax = 350;
  741. public int m_iYPixelMin = 40;
  742. public int m_iXPixelMax = 1100;
  743. public int m_iXPixelMin = 60;
  744. private void ChangeColor()
  745. {
  746. if (m_bRulerColor)
  747. m_bRulerColor = false;
  748. else
  749. m_bRulerColor = true;
  750. if (m_bRulerColor)
  751. RefreshColorAction();
  752. else
  753. m_afThisClr = m_afOrgClr;
  754. }
  755. private void RefreshColorAction()
  756. {
  757. if (m_bRulerColor)
  758. {
  759. style1Color1 = Color.Green;
  760. style1Color2 = Color.Red;
  761. double adZVRange = m_dMaxZV - m_dMinZV;
  762. //处理颜色和翻转参数
  763. m_afNewClr = new float[m_iWidth, m_iHeight, 3];
  764. //m_adOverTurnZV = new double[m_iWidth, m_iHeight];
  765. for (int iX = 0; iX < m_iWidth; iX++)
  766. {
  767. for (int iY = 0; iY < m_iHeight; iY++)
  768. {
  769. Color fColor = /*Color.FromArgb(*/CalculationColorfRange(((m_bOverTurn ? m_adOverTurnZV[iX, iY] : m_adZV[iX, iY]) - m_dMinZV) / adZVRange)/*)*/;
  770. m_afNewClr[iX, iY, 0] = (float)(fColor.R / 255.0);
  771. m_afNewClr[iX, iY, 1] = (float)(fColor.G / 255.0);
  772. m_afNewClr[iX, iY, 2] = (float)(fColor.B / 255.0);
  773. //m_adOverTurnZV[iX, iY] = m_dMinZV + (m_dMaxZV - m_adZV[iX, iY]);
  774. }
  775. }
  776. m_afThisClr = m_afNewClr;
  777. }
  778. }
  779. private Color style1Color1;
  780. private Color style1Color2;
  781. /// <summary>
  782. /// 根据设置计算彩虹色的变化
  783. /// </summary>
  784. /// <param name="dZ"></param>
  785. /// <returns></returns>
  786. public Color CalculationColorfRange(double dZ)
  787. {
  788. if (dZ < 0)
  789. dZ = 0;
  790. if (dZ > 1)
  791. dZ = 1;
  792. return Color.FromArgb(255, (int)(style1Color1.R + (style1Color2.R - style1Color1.R) * dZ), (int)(style1Color1.G + (style1Color2.G - style1Color1.G) * dZ), (int)(style1Color1.B + (style1Color2.B - style1Color1.B) * dZ));// (int)(style1Color1 + (style1Color2 - style1Color1) * dZ);
  793. }
  794. /*
  795. private void Form_KeyUp(object sender, KeyEventArgs e) {
  796. keys[e.KeyValue] = false; // Key Has Been Released, Mark It As false
  797. }
  798. */
  799. private void Form_Resize(object sender, EventArgs e)
  800. {
  801. //ReSizeGLScene(form.Width, form.Height); // Resize The OpenGL Window
  802. }
  803. private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
  804. {
  805. if ((m_iMusMod <= 0) || (m_iMusMod >= 7))
  806. {
  807. return;
  808. }
  809. //int iBtnDwn = Glfw.glfwGetMouseButton(Glfw.GLFW_MOUSE_BUTTON_LEFT);
  810. if (0 == m_iMusDown)
  811. {
  812. m_iLastMusDown = 0;
  813. return;
  814. }
  815. else if (0 == m_iLastMusDown)
  816. {
  817. m_iLastMusDown = 1;
  818. m_iLastmouseX = e.X;
  819. m_iLastmouseY = e.Y;
  820. return;
  821. }
  822. float fXChange = e.X - m_iLastmouseX;
  823. float fYChange = e.Y - m_iLastmouseY;
  824. m_fR_xyz[0] = AngleNormalization(m_fR_xyz[0]);
  825. m_fR_xyz[1] = AngleNormalization(m_fR_xyz[1]);
  826. if (1 == m_iMusMod)
  827. {
  828. if ((m_fR_xyz[0] > 90.0) && (m_fR_xyz[0] < 270.0))//先绕X轴转完了
  829. {
  830. fXChange *= -1.0F;
  831. }
  832. m_fR_xyz[0] += fYChange;
  833. m_fR_xyz[1] += fXChange;
  834. //m_fR_xyz[2] = 0.0F;
  835. }
  836. else if (2 == m_iMusMod)
  837. {
  838. m_fT_xyz[0] += fXChange * 0.91F;
  839. m_fT_xyz[1] += fYChange * -0.91F;
  840. }
  841. else if (5 == m_iMusMod)
  842. {
  843. if ((m_fR_xyz[0] > 90.0) && (m_fR_xyz[0] < 270.0))//先绕X轴转完了
  844. {
  845. fYChange *= -1.0F;
  846. }
  847. if ((m_fR_xyz[1] > 90.0) && (m_fR_xyz[1] < 270.0))//先绕X轴转完了
  848. {
  849. fXChange *= -1.0F;
  850. }
  851. m_afPonitA[0] += fXChange * 0.95F;
  852. m_afPonitA[0] = (float)Math.Min(Math.Max(m_afPonitA[0], 1.0), m_iWidth - 1);
  853. m_afPonitA[1] += fYChange * -0.95F;
  854. m_afPonitA[1] = (float)Math.Min(Math.Max(m_afPonitA[1], 1.0), m_iHeight - 1);
  855. CalculationIne();
  856. }
  857. else if (6 == m_iMusMod)
  858. {
  859. if ((m_fR_xyz[0] > 90.0) && (m_fR_xyz[0] < 270.0))//先绕X轴转完了
  860. {
  861. fYChange *= -1.0F;
  862. }
  863. if ((m_fR_xyz[1] > 90.0) && (m_fR_xyz[1] < 270.0))//先绕X轴转完了
  864. {
  865. fXChange *= -1.0F;
  866. }
  867. m_afPonitB[0] += fXChange * 0.95F;
  868. m_afPonitB[0] = (float)Math.Min(Math.Max(m_afPonitB[0], 1.0), m_iWidth - 1);
  869. m_afPonitB[1] += fYChange * -0.95F;
  870. m_afPonitB[1] = (float)Math.Min(Math.Max(m_afPonitB[1], 1.0), m_iHeight - 1);
  871. CalculationIne();
  872. }
  873. m_iLastmouseX = e.X;
  874. m_iLastmouseY = e.Y;
  875. }
  876. private float AngleNormalization(float fOldAngle)
  877. {
  878. float fNewAngle = fOldAngle % 360.0F;
  879. if (fNewAngle < 0)
  880. {
  881. fNewAngle += 360.0F;
  882. }
  883. return fNewAngle;
  884. }
  885. private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
  886. {
  887. if (e.Button == MouseButtons.Left)
  888. {
  889. m_iMusDown = 1;
  890. }
  891. }
  892. private void pictureBox1_MouseUp(object sender, MouseEventArgs e)
  893. {
  894. if (e.Button == MouseButtons.Left)
  895. {
  896. m_iMusDown = 0;
  897. }
  898. }
  899. private void pictureBox1_MouseLeave(object sender, EventArgs e)
  900. {
  901. m_iMusDown = 0;
  902. }
  903. }
  904. }