Advertisement
Guest User

Untitled

a guest
Oct 18th, 2018
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.81 KB | None | 0 0
  1. VJEŽBA 2 Rješenje
  2. Računalna grafika - OpenGL 2
  3.  
  4.  
  5. using System.Drawing;
  6. using System.Windows.Forms;
  7. using CsGL.OpenGL;
  8.  
  9. namespace Vjezba2
  10. {
  11. public class OurView : OpenGLControl
  12. {
  13. private float angleY = 0;
  14. private float angleX = 0;
  15. private float angleZ = 0;
  16. private float angleR = 0;
  17. private float angleT = 0;
  18. private float lengthZ = 0;
  19.  
  20. public OurView()
  21. {
  22. this.KeyDown += new KeyEventHandler(OurView_OnKeyDown);
  23. }
  24.  
  25. protected void OurView_OnKeyDown(object Sender, KeyEventArgs kea)
  26. {
  27. if (kea.KeyCode == Keys.Escape)
  28. {
  29. Application.Exit();
  30. }
  31. if (kea.KeyCode == Keys.Y)
  32. {
  33. angleY += 5;
  34. Refresh();
  35. }
  36. if (kea.KeyCode == Keys.X)
  37. {
  38. angleX += 5;
  39. Refresh();
  40. }
  41. if (kea.KeyCode == Keys.Z)
  42. {
  43. angleZ += 5;
  44. Refresh();
  45. }
  46. if (kea.KeyCode == Keys.P)
  47. {
  48. angleR += 10;
  49. Refresh();
  50. }
  51. if (kea.KeyCode == Keys.T)
  52. {
  53. angleT += 10;
  54. Refresh();
  55. }
  56. if (kea.KeyCode == Keys.Q)
  57. {
  58. lengthZ += 1;
  59. Refresh();
  60. }
  61. if (kea.KeyCode == Keys.W)
  62. {
  63. lengthZ -= 1;
  64. Refresh();
  65. }
  66. }
  67.  
  68. public override void glDraw()
  69. {
  70. GL.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT);
  71. GL.glLoadIdentity();
  72. GL.glTranslatef(0, 0, -6 + lengthZ);
  73.  
  74. GL.glRotatef(angleY, 0, 1, 0);
  75. GL.glRotatef(angleX, 1, 0, 0);
  76. GL.glRotatef(angleZ, 0, 0, 1);
  77.  
  78. GL.glPushMatrix();
  79. GL.glRotatef(angleT, 0, 0, 1);
  80. GL.glScalef(1.4f, 1.4f, 1);
  81. GL.glBegin(GL.GL_POLYGON);
  82. GL.glColor3f(255, 0, 0);
  83. GL.glVertex3f(0, 1, 0);
  84. GL.glVertex3f(-1, -1, 0);
  85. GL.glVertex3f(1, -1, 0);
  86. GL.glEnd();
  87. GL.glPopMatrix();
  88.  
  89. GL.glTranslatef(0.4f, -0.7f, 1);
  90. GL.glRotatef(angleR, 0, 1, 0);
  91. GL.glBegin(GL.GL_POLYGON);
  92. GL.glColor3f(0, 255, 0);
  93. GL.glVertex3f(0.3f, 0.3f, 0);
  94. GL.glVertex3f(0.3f, -0.3f, 0);
  95. GL.glVertex3f(-0.3f, -0.3f, 0);
  96. GL.glVertex3f(-0.3f, 0.3f, 0);
  97. GL.glEnd();
  98. }
  99.  
  100. protected override void InitGLContext()
  101. {
  102. GL.glShadeModel(GL.GL_SMOOTH);
  103. GL.glClearColor(0.0f, 0.0f, 0.0f, 0.5f);
  104. GL.glClearDepth(1.0f);
  105. GL.glEnable(GL.GL_DEPTH_TEST);
  106. GL.glDepthFunc(GL.GL_LEQUAL);
  107. GL.glHint(GL.GL_PERSPECTIVE_CORRECTION_HINT, GL.GL_NICEST);
  108.  
  109. Size s = this.Size;
  110. GL.glMatrixMode(GL.GL_PROJECTION);
  111. GL.glLoadIdentity();
  112. GL.gluPerspective(45f, (double)s.Width / (double)s.Height, 0.1f, 100.0f);
  113. GL.glMatrixMode(GL.GL_MODELVIEW);
  114. }
  115. }
  116.  
  117. public class MainForm : Form
  118. {
  119. private OurView view;
  120.  
  121. public MainForm()
  122. {
  123. this.AutoScaleBaseSize = new Size(5, 13);
  124. this.ClientSize = new Size(740, 580);
  125. this.Name = "MainForm";
  126. this.Text = "Vjezba2";
  127. this.view = new OurView();
  128. this.view.Parent = this;
  129. this.view.Dock = DockStyle.Fill;
  130. }
  131.  
  132. static void Main()
  133. {
  134. Application.Run(new MainForm());
  135. }
  136. }
  137. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement