Advertisement
Guest User

Untitled

a guest
Apr 22nd, 2018
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.63 KB | None | 0 0
  1. GK_07_KPP.CPP
  2.  
  3. #include <iostream>
  4.  
  5. #define GLUT_DISABLE_ATEXIT_HACK
  6.  
  7. #include "GL/glut.h"
  8.  
  9. using namespace std;
  10.  
  11. GLfloat glfActualAngle;
  12. GLfloat glfDelta;
  13. GLfloat tt, uu, vv;
  14. bool bTryb;
  15. GLfloat ptab[4][3]; //tablica PUNKCIOROW
  16. GLfloat ptab2[4][3]; //tablica PUNKCIOROW 2
  17.  
  18. // Drawing (display) routine.
  19. void drawScene(void)
  20. {
  21. // Clear screen to background color.
  22. glClear(GL_COLOR_BUFFER_BIT);
  23.  
  24.  
  25.  
  26. if (bTryb) {
  27. // ToDo - Ustawienie punktow kontrolnych krzywej Beziera(typ GLfloat)
  28.  
  29.  
  30. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  31. glDisable(GL_LIGHTING);
  32. glLoadIdentity();
  33. glRotatef(glfActualAngle, 0.0f, 1.0f, 1.0f);
  34.  
  35. // TODO - rysowanie punktow kontrolnych
  36. glPointSize(5.0);
  37.  
  38. /* należy wywolac funkcje, ktora definiuje wierzcholek w trzech wymiarach
  39. glVertex3f(GLfloat x, GLfloat y, GLfloat z)
  40. */
  41.  
  42. //PUNKCIORY 1
  43. ptab[0][0] = -4; ptab[0][1] = -4; ptab[0][2] = 0;
  44. ptab[1][0] = 8; ptab[1][1] = 8; ptab[1][2] = 0;
  45. ptab[2][0] = -8; ptab[2][1] = 8; ptab[2][2] = 0;
  46. ptab[3][0] = 4; ptab[3][1] = -4; ptab[3][2] = 0;
  47.  
  48. //PUNKCIORY 2
  49. ptab2[0][0] = 4; ptab2[0][1] = -4; ptab2[0][2] = 0;
  50. ptab2[1][0] = 16; ptab2[1][1] = -16; ptab2[1][2] = 0;
  51. ptab2[2][0] = 16; ptab2[2][1] = 0; ptab2[2][2] = 0;
  52. ptab2[3][0] = 4; ptab2[3][1] = -12; ptab2[3][2] = 0;
  53.  
  54. //RYSOWANIE PUNKCIOROW 1
  55. glColor3f(0.0, 0.0, 1.0);
  56. glBegin(GL_POINTS);
  57. for(int i =0; i < 4; ++i)
  58. glVertex3f(ptab[i][0], ptab[i][1], ptab[i][2]);
  59. glEnd();
  60.  
  61. //RYSOWANIE PUNKCIOROW 2
  62. glColor3f(1.0, 1.0, 0.0);
  63. glBegin(GL_POINTS);
  64. for(int i =0; i < 4; ++i)
  65. glVertex3f(ptab[i][0], ptab[i][1], ptab[i][2]);
  66. glEnd();
  67.  
  68. glPointSize(1.0);
  69. glColor3f(1.0, 0.0, 0.0);
  70.  
  71. glBegin(GL_LINE_STRIP);
  72. // TODO - rysowanie krzywej
  73. /*
  74. - krzywą można narysowac laczyc punkty liniami -
  75. glBegin(GL_LINE_STRIP) i po wyznaczeniu punktu wywolac
  76. glVertex3f(GLfloat x, GLfloat y, GLfloat z)
  77. kazdy nastepny podany wierzcholek okresla punkt do ktorego przedluzana jest linia
  78. */
  79.  
  80. for(double i =0; i <= 1.01; i += 0.01)
  81. {
  82. glVertex3f(pow(1-i, 3)*ptab[0][0] + 3.0*i*pow(1-i, 2)*ptab[1][0] + 3.0*pow(i, 2)*(1-i)*ptab[2][0] + pow(i, 3)*ptab[3][0],
  83. pow(1-i, 3)*ptab[0][1] + 3.0*i*pow(1-i, 2)*ptab[1][1] + 3.0*pow(i, 2)*(1-i)*ptab[2][1] + pow(i, 3)*ptab[3][1],
  84. pow(1-i, 3)*ptab[0][2] + 3.0*i*pow(1-i, 2)*ptab[1][2] + 3.0*pow(i, 2)*(1-i)*ptab[2][2] + pow(i, 3)*ptab[3][2]);
  85.  
  86. }
  87. for(double i =0; i <= 1.01; i += 0.01)
  88. {
  89. glVertex3f(pow(1-i, 3)*ptab2[0][0] + 3.0*i*pow(1-i, 2)*ptab2[1][0] + 3.0*pow(i, 2)*(1-i)*ptab2[2][0] + pow(i, 3)*ptab2[3][0],
  90. pow(1-i, 3)*ptab2[0][1] + 3.0*i*pow(1-i, 2)*ptab2[1][1] + 3.0*pow(i, 2)*(1-i)*ptab2[2][1] + pow(i, 3)*ptab2[3][1],
  91. pow(1-i, 3)*ptab2[0][2] + 3.0*i*pow(1-i, 2)*ptab2[1][2] + 3.0*pow(i, 2)*(1-i)*ptab2[2][2] + pow(i, 3)*ptab2[3][2]);
  92.  
  93. }
  94.  
  95.  
  96. glEnd();
  97.  
  98.  
  99.  
  100. //Animacja punktu na krzywej. Nalezy wyznaczyc wspolrzedne punktu na krzywej za pomoca algorytmu
  101. //deCasteliau
  102. glPointSize(5.0);
  103. glColor3f(0.0, 1.0, 0.0);
  104.  
  105. //TODO - Wyznaczenie i narysowanie zielonego punktu dla parametru tt
  106. static bool drugi = false;
  107. glBegin(GL_POINTS);
  108. tt += 0.005f;
  109.  
  110.  
  111. GLfloat ptabb[4][3];
  112. GLfloat ptabb2[4][3];
  113.  
  114. //SEGMENT 1
  115. if(!drugi)
  116. {
  117. for(int i = 0; i < 4; ++i)
  118. for(int j = 0; j < 3; ++j)
  119. ptabb[i][j] = ptab[i][j];
  120.  
  121. for(int i = 1; i < 4; ++i)
  122. for(int j = 0; j < 4 - i; ++j)
  123. for(int k = 0; k < 3; ++k)
  124. ptabb[j][k] = (1-tt)*ptabb[j][k] + tt*ptabb[j+1][k];
  125.  
  126. glVertex3f(ptabb[0][0], ptabb[0][1], ptabb[0][2]);
  127. }
  128. //SEGMENT 2
  129. if(drugi)
  130. {
  131. for(int i = 0; i < 4; ++i)
  132. for(int j = 0; j < 3; ++j)
  133. ptabb2[i][j] = ptab2[i][j];
  134.  
  135. for(int i = 1; i < 4; ++i)
  136. for(int j = 0; j < 4 - i; ++j)
  137. for(int k = 0; k < 3; ++k)
  138. ptabb2[j][k] = (1-tt)*ptabb2[j][k] + tt*ptabb2[j+1][k];
  139.  
  140. glVertex3f(ptabb2[0][0], ptabb2[0][1], ptabb2[0][2]);
  141. }
  142.  
  143.  
  144. if (tt > 1.0f)
  145. {
  146. drugi = !drugi;
  147. tt = 0.0f;
  148. }
  149. glEnd();
  150. }
  151. else //POWIERZCHNIA
  152. {
  153. // TODO - Ustawienie punktow kontrolnych dla powierzchni Beziera (typ GLfloat)
  154.  
  155.  
  156. glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
  157. glDisable(GL_LIGHTING);
  158. glLoadIdentity();
  159. glRotatef(glfActualAngle, 0.0, 1.0, 1.0);
  160.  
  161. glPointSize(5.0);
  162. glColor3f(1.0, 1.0, 0.0);
  163. // TODO - rysowanie punktow kontrolnych
  164.  
  165. glBegin(GL_POINTS);
  166. // należy wywolac funkcje, ktora definiuje wierzcholek w trzech wymiarach
  167. // glVertex3f(GLfloat x, GLfloat y, GLfloat z)
  168.  
  169. // rysowanie powierzchni
  170. glPointSize(1.0);
  171. glColor3f(1.0, 0.0, 0.0);
  172. // TODO - rysowanie powierzchni
  173. // Rysowanie jednej lini lamanej nalezy umiescic miedzy
  174. //glBegin(GL_LINE_STRIP);
  175. // kolejne wyznaczone punkty glVertex3f(GLfloat x, GLfloat y, GLfloat z)
  176. //glEnd();
  177.  
  178.  
  179.  
  180.  
  181. //Animacja punktu na krzywej. Nalezy wyznaczyc wspolrzedne punktu na krzywej za pomoca algorytmu
  182. //deCasteliau
  183. glPointSize(5.0);
  184. glColor3f(0.0, 1.0, 0.0);
  185.  
  186. //TODO - Wyznaczenie i narysowanie zielonego punktu dla parametru uu, vv
  187. glBegin(GL_POINTS);
  188. uu += 0.005f;
  189.  
  190.  
  191. if (uu > 1.0f)
  192. {
  193. uu = 0.0f;
  194. vv += 0.5f;
  195. if (vv > 1.0f)
  196. vv = 0.0f;
  197. }
  198. glEnd();
  199.  
  200.  
  201. }
  202.  
  203.  
  204.  
  205. glPopMatrix();
  206.  
  207. // Flush created objects to the screen, i.e., force rendering.
  208. glFlush();
  209. }
  210.  
  211. // Initialization routine.
  212. void setup(void)
  213. {
  214. // Set background (or clearing) color.
  215. glClearColor(1.0, 1.0, 1.0, 0.0);
  216.  
  217. glfActualAngle = 0.0f;
  218. glfDelta = 0.2f;
  219. tt = 0.0f;
  220. uu = 0.0f;
  221. vv = 0.0f;
  222. bTryb = true;
  223.  
  224. }
  225.  
  226. // OpenGL window reshape routine.
  227. void resize(int w, int h)
  228. {
  229. // Set viewport size to be entire OpenGL window.
  230. glViewport(0, 0, (GLsizei)w, (GLsizei)h);
  231.  
  232. // Set matrix mode to projection.
  233. glMatrixMode(GL_PROJECTION);
  234.  
  235. // Clear current projection matrix to identity.
  236. glLoadIdentity();
  237.  
  238. // Specify the orthographic (or perpendicular) projection,
  239. // i.e., define the viewing box.
  240. glOrtho(-10.0, 10.0, -10.0, 10.0,-10.0, 10.0);
  241. //glFrustum(-10.0, 10.0, -10.0, 10.0, 5.0, 100.0);
  242.  
  243. // Set matrix mode to modelview.
  244. glMatrixMode(GL_MODELVIEW);
  245.  
  246. // Clear current modelview matrix to identity.
  247. glLoadIdentity();
  248. }
  249.  
  250. void animate(int value) {
  251.  
  252. glfActualAngle += glfDelta;
  253. if(glfActualAngle > 360.0f)
  254. glfActualAngle = 0.0f;
  255. glutTimerFunc(5, animate, 1);
  256. glutPostRedisplay();
  257. }
  258.  
  259. // Keyboard input processing routine.
  260. void keyInput(unsigned char key, int x, int y)
  261. {
  262. bTryb = !bTryb;
  263. }
  264.  
  265. // Mouse callback routine.
  266. void mouseControl(int button, int state, int x, int y)
  267. {
  268.  
  269. //ToDo
  270. }
  271.  
  272. // Main routine: defines window properties, creates window,
  273. // registers callback routines and begins processing.
  274. int main(int argc, char **argv)
  275. {
  276. // Initialize GLUT.
  277. glutInit(&argc, argv);
  278.  
  279. // Set display mode as single-buffered and RGB color.
  280. glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
  281.  
  282. // Set OpenGL window size.
  283. glutInitWindowSize(500, 500);
  284.  
  285. // Set position of OpenGL window upper-left corner.
  286. glutInitWindowPosition(100, 100);
  287.  
  288. // Create OpenGL window with title.
  289. glutCreateWindow("Laboratorium GK: 07_KPP");
  290.  
  291. // Initialize.
  292. setup();
  293.  
  294. // Register display routine.
  295. glutDisplayFunc(drawScene);
  296.  
  297. // Register reshape routine.
  298. glutReshapeFunc(resize);
  299.  
  300.  
  301. // Register the mouse and keyboard callback function.
  302. glutMouseFunc(mouseControl);
  303. glutKeyboardFunc(keyInput);
  304.  
  305. //Animation
  306. glutTimerFunc(5, animate, 1);
  307.  
  308. // Begin processing.
  309. glutMainLoop();
  310.  
  311. return 0;
  312. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement