Hanaaa_

egc

Jan 15th, 2020
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.48 KB | None | 0 0
  1. using OpenTK;
  2. using OpenTK.Graphics.OpenGL;
  3. using System;
  4. using System.Drawing;
  5. using System.Windows.Forms;
  6. using Template._3DObj;
  7.  
  8. using System.Drawing.Imaging;
  9.  
  10. namespace Template
  11. {
  12. public partial class MainForm : Form
  13. { int[] textures = new int[2];
  14. bool brick;
  15. int colorTex=1;
  16.  
  17. float camDepth;
  18. static bool iluminare=true;
  19. int eyePosX, eyePosY, eyePosZ;
  20. private Point mousePos;
  21.  
  22. Color backColor = Color.Gray;
  23. Random rnd = new Random();
  24.  
  25. Coords v1 = new Coords(10, 0, 10);
  26. Coords v2 = new Coords(40, 0, 10);
  27. Coords v3 = new Coords(40, 0, 40);
  28. Coords v4 = new Coords(10, 0, 40);
  29. Coords v5 = new Coords(10, 30, 10);
  30. Coords v6 = new Coords(40, 30, 10);
  31. Coords v7 = new Coords(40, 30, 40);
  32. Coords v8 = new Coords(10, 30, 40);
  33.  
  34. Axes mainAx;
  35. Cube cb;
  36.  
  37.  
  38.  
  39.  
  40.  
  41. public MainForm()
  42. {
  43. InitializeComponent();
  44. }
  45.  
  46. private void MainForm_Load(object sender, EventArgs e)
  47. {
  48. SetupDefaultValues();
  49. SetupWindowGUI();
  50. SetupSceneObjects();
  51. }
  52.  
  53. private void SetupDefaultValues()
  54. {
  55. camDepth = 1.04f;
  56. eyePosX = 100;
  57. eyePosY = 100;
  58. eyePosZ = 50;
  59. }
  60.  
  61. private void SetupWindowGUI()
  62. {
  63.  
  64. }
  65.  
  66. private void SetupSceneObjects()
  67. {
  68. GL.Enable(EnableCap.DepthTest);
  69. GL.DepthFunc(DepthFunction.Less);
  70. GL.Enable(EnableCap.Texture2D);
  71.  
  72. mainAx = new Axes(chkAxesVisibility.Checked);
  73. cb = new Cube(v1, v2, v3, v4, v5, v6, v7, v8, Color.LightBlue);
  74. }
  75.  
  76. private void MainViewport_Load(object sender, EventArgs e)
  77. {
  78. GL.ClearColor(backColor);
  79. }
  80.  
  81. private void chkAxesVisibility_CheckedChanged(object sender, EventArgs e)
  82. {
  83. mainAx.SetVisibility(chkAxesVisibility.Checked);
  84. MainViewport.Invalidate();
  85. }
  86.  
  87. private void chkPolygonalStyle_CheckedChanged(object sender, EventArgs e)
  88. {
  89. if (chkPolygonalStyle.Checked == true)
  90. {
  91. cb.PolygonDrawingStyle("line");
  92. } else
  93. {
  94. cb.PolygonDrawingStyle("surface");
  95. }
  96. MainViewport.Invalidate();
  97. }
  98.  
  99. private void btnChangeBack_Click(object sender, EventArgs e)
  100. {
  101. backColor = Color.FromArgb(rnd.Next(255), rnd.Next(255), rnd.Next(255));
  102. MainViewport.Invalidate();
  103. }
  104.  
  105. private void MainViewport_MouseMove(object sender, MouseEventArgs e)
  106. {
  107. mousePos = new Point(e.X, e.Y);
  108. MainViewport.Invalidate();
  109. }
  110.  
  111.  
  112.  
  113. private void button1_Click(object sender, EventArgs e)
  114. {
  115. if (iluminare == true)
  116. {
  117. iluminare = false;
  118. button1.Text = "iluminare off";
  119. }
  120. else
  121. {
  122. iluminare = true;
  123. button1.Text = "iluminare on";
  124. }
  125.  
  126. MainViewport.Invalidate();
  127. }
  128.  
  129.  
  130. private void LoadTexture(int textureId, string filename)
  131. {
  132. Bitmap bmp = new Bitmap(filename);
  133.  
  134. BitmapData data = bmp.LockBits(new Rectangle(0, 0, bmp.Width, bmp.Height),
  135. System.Drawing.Imaging.ImageLockMode.ReadOnly,
  136. System.Drawing.Imaging.PixelFormat.Format32bppArgb);
  137.  
  138. GL.BindTexture(TextureTarget.Texture2D, textureId);
  139. GL.TexImage2D(TextureTarget.Texture2D, 0, PixelInternalFormat.Rgba,
  140. bmp.Width, bmp.Height, 0, OpenTK.Graphics.OpenGL.PixelFormat.Bgra,
  141. PixelType.UnsignedByte, data.Scan0);
  142.  
  143. bmp.UnlockBits(data);
  144.  
  145. GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMinFilter, (float)TextureMinFilter.Linear);
  146. GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMagFilter, (float)TextureMagFilter.Linear);
  147. }
  148.  
  149. private void button2_Click(object sender, EventArgs e)
  150. {
  151. GL.Enable(EnableCap.Texture2D);
  152. GL.Hint(HintTarget.PerspectiveCorrectionHint, HintMode.Nicest);
  153. GL.GenTextures(textures.Length, textures);
  154.  
  155. LoadTexture(textures[0], "C:/Users/popov/OneDrive/Desktop/EGC/labs/OpenTK_WinForms_StandardTemplate/ReTester/Content/Textures/wooden_crate.jpg");
  156. LoadTexture(textures[1], "C:/Users/popov/OneDrive/Desktop/EGC/labs/OpenTK_WinForms_StandardTemplate/ReTester/Content/Textures/grates.png");
  157. brick = true;
  158. MainViewport.Invalidate();
  159.  
  160. }
  161.  
  162. private void button3_Click(object sender, EventArgs e)
  163. {
  164. GL.Enable(EnableCap.Texture2D);
  165. GL.Hint(HintTarget.PerspectiveCorrectionHint, HintMode.Nicest);
  166. GL.GenTextures(textures.Length, textures);
  167.  
  168. LoadTexture(textures[0], "C:/Users/popov/OneDrive/Desktop/EGC/labs/OpenTK_WinForms_StandardTemplate/ReTester/Content/Textures/wooden_crate.jpg");
  169. LoadTexture(textures[1], "C:/Users/popov/OneDrive/Desktop/EGC/labs/OpenTK_WinForms_StandardTemplate/ReTester/Content/Textures/grates.png");
  170.  
  171. brick = false;
  172. MainViewport.Invalidate();
  173. }
  174.  
  175. private void radioButton1_CheckedChanged(object sender, EventArgs e)
  176. {
  177. colorTex = 1;
  178. }
  179.  
  180. private void radioButton2_CheckedChanged(object sender, EventArgs e)
  181. {
  182. colorTex = 2;
  183. }
  184. private void radioButton3_CheckedChanged(object sender, EventArgs e)
  185. {
  186. colorTex = 3;
  187. }
  188. private void MainViewport_Paint(object sender, PaintEventArgs e)
  189. {
  190. GL.Clear(ClearBufferMask.ColorBufferBit);
  191. GL.Clear(ClearBufferMask.DepthBufferBit);
  192. GL.ClearColor(backColor);
  193.  
  194. SetView();
  195.  
  196. if (chkRotationOn.Checked == true)
  197. {
  198. // Doar după Ox.
  199. GL.Rotate(Math.Max(mousePos.X, mousePos.Y), 1, 1, 1);
  200. }
  201.  
  202. // GRAPHICS PAYLOAD
  203. mainAx.DrawMe();
  204. if (brick == true)
  205. {
  206. GL.BindTexture(TextureTarget.Texture2D, textures[0]);
  207. }
  208. else
  209. {
  210. GL.BindTexture(TextureTarget.Texture2D, textures[1]);
  211. }
  212. if(colorTex==1)
  213. GL.Color3(Color.White);
  214. if (colorTex == 2)
  215. GL.Color3(Color.Red);
  216. if (colorTex == 3)
  217. GL.Color3(Color.Blue);
  218. cb.DrawMe();
  219.  
  220. if (iluminare == true)
  221. {
  222. GL.Enable(EnableCap.Lighting);
  223. }
  224. else GL.Disable(EnableCap.Lighting);
  225.  
  226. MainViewport.SwapBuffers();
  227. }
  228.  
  229. private void SetView() {
  230. Matrix4 perspective = Matrix4.CreatePerspectiveFieldOfView(camDepth, 4 / 3, 1, 256);
  231. Matrix4 lookat = Matrix4.LookAt(eyePosX, eyePosY, eyePosZ, 0, 0, 0, 0, 1, 0);
  232. GL.MatrixMode(MatrixMode.Projection);
  233. GL.LoadIdentity();
  234. GL.LoadMatrix(ref perspective);
  235. GL.MatrixMode(MatrixMode.Modelview);
  236. GL.LoadIdentity();
  237. GL.LoadMatrix(ref lookat);
  238. GL.Viewport(0, 0, MainViewport.Width, MainViewport.Height);
  239. }
  240.  
  241. }
  242. }
  243. //in cube.cs
  244.  
  245. /*public void DrawMe()
  246. {
  247.  
  248. // GL.PolygonMode(MaterialFace.FrontAndBack, currentPolygonState);
  249.  
  250. GL.Begin(PrimitiveType.Quads);
  251.  
  252. int R = color.R;
  253. int G = color.G;
  254. int B = color.B;
  255. int offset = 20;
  256.  
  257. //GL.Color3(Color.FromArgb(R,G,B - offset));
  258. // baza
  259. GL.TexCoord2(0.0, 1.0);
  260. GL.Vertex3(v1.x, v1.y, v1.z);
  261. GL.TexCoord2(1.0, 1.0);
  262. GL.Vertex3(v2.x, v2.y, v2.z);
  263. GL.TexCoord2(1.0, 0.0);
  264. GL.Vertex3(v3.x, v3.y, v3.z);
  265. GL.TexCoord2(0.0, 0.0);
  266. GL.Vertex3(v4.x, v4.y, v4.z);
  267.  
  268. //GL.Color3(Color.FromArgb(R, G- offset, B));
  269. //perete dreapta
  270. GL.TexCoord2(0.0, 1.0);
  271. GL.Vertex3(v2.x, v2.y, v2.z); GL.TexCoord2(1.0, 1.0);
  272. GL.Vertex3(v3.x, v3.y, v3.z); GL.TexCoord2(1.0, 0.0);
  273. GL.Vertex3(v7.x, v7.y, v7.z); GL.TexCoord2(0.0, 0.0);
  274. GL.Vertex3(v6.x, v6.y, v6.z);
  275.  
  276. //GL.Color3(Color.FromArgb(R - offset, G, B));
  277. // perete fata
  278. GL.TexCoord2(0.0, 1.0); GL.Vertex3(v3.x, v3.y, v3.z); GL.TexCoord2(1.0, 1.0);
  279. GL.Vertex3(v4.x, v4.y, v4.z); GL.TexCoord2(1.0, 0.0);
  280. GL.Vertex3(v8.x, v8.y, v8.z); GL.TexCoord2(0.0, 0.0);
  281. GL.Vertex3(v7.x, v7.y, v7.z);
  282.  
  283. //GL.Color3(Color.FromArgb(R - offset, G, B - offset));
  284. // perete stanga
  285. GL.TexCoord2(0.0, 1.0); GL.Vertex3(v1.x, v1.y, v1.z); GL.TexCoord2(1.0, 1.0);
  286. GL.Vertex3(v4.x, v4.y, v4.z); GL.TexCoord2(1.0, 0.0);
  287. GL.Vertex3(v8.x, v8.y, v8.z); GL.TexCoord2(0.0, 0.0);
  288. GL.Vertex3(v5.x, v5.y, v5.z);
  289.  
  290. //GL.Color3(Color.FromArgb(R - offset, G - offset, B));
  291. // perete spate
  292. GL.TexCoord2(0.0, 1.0); GL.Vertex3(v1.x, v1.y, v1.z); GL.TexCoord2(1.0, 1.0);
  293. GL.Vertex3(v2.x, v2.y, v2.z); GL.TexCoord2(1.0, 0.0);
  294. GL.Vertex3(v6.x, v6.y, v6.z); GL.TexCoord2(0.0, 0.0);
  295. GL.Vertex3(v5.x, v5.y, v5.z);
  296.  
  297. //GL.Color3(Color.FromArgb(R - offset, G - offset, B - offset));
  298. // capac
  299. GL.TexCoord2(0.0, 1.0); GL.Vertex3(v5.x, v5.y, v5.z); GL.TexCoord2(1.0, 1.0);
  300. GL.Vertex3(v6.x, v6.y, v6.z); GL.TexCoord2(1.0, 0.0);
  301. GL.Vertex3(v7.x, v7.y, v7.z); GL.TexCoord2(0.0, 0.0);
  302. GL.Vertex3(v8.x, v8.y, v8.z);
  303. GL.End();
  304. }*/
  305.  
  306. /*GL.TexCoord2(0.0, 1.0);
  307.  
  308. GL.TexCoord2(1.0, 1.0);
  309.  
  310. GL.TexCoord2(1.0, 0.0);
  311.  
  312. GL.TexCoord2(0.0, 0.0);*/
Advertisement
Add Comment
Please, Sign In to add comment