fork(1) download
  1.  
  2. #include <iostream>
  3. #include <math.h>
  4. #include <string>
  5. #include <time.h>
  6. #include <stdio.h>
  7. #include <sstream>
  8. #include <vector>
  9. #include <cmath>
  10.  
  11. #ifdef __APPLE__
  12. #include <OpenGL/OpenGL.h>
  13. #include <GLUT/glut.h>
  14. #else
  15. //#include <GL/glut.h>
  16. #include <GL/glew.h>
  17. #include <GL/freeglut.h>
  18.  
  19. #endif
  20.  
  21. using namespace std;
  22.  
  23. //// ***** TODO *****
  24. // Choose a better method for storing points RGB values.
  25. // A. use 256-scale unsigned char
  26. // B. figure out a pointer system.
  27.  
  28. struct point {
  29. unsigned char pointR;
  30. unsigned char pointG;
  31. unsigned char pointB;
  32. float pointX;
  33. float pointY;
  34. float pointZ;
  35. };
  36.  
  37. point * buff;
  38. point * tmp;
  39.  
  40. float x = 0.3, y = 0, z = 0,
  41. xOld = 0,
  42. yOld = 0,
  43. zOld = 0,
  44. xOld2 = 0,
  45. yOld2 = 0,
  46. zOld2 = 0,
  47. dOld = 0,
  48. h = 0.083,
  49. a = 14.633,
  50. b = 9.502,
  51. c = 7.827; // starting point
  52.  
  53. int initialIterations = 200, // initial number of iterations to allow the attractor to settle
  54. iterations = 5000000, // number of times to iterate through the functions and draw a point
  55. cols = 3;
  56.  
  57.  
  58.  
  59. void merge(point*, int, int, int);
  60. void mergesort(point *p, int low, int high)
  61. {
  62. int pivot;
  63. if(low<high)
  64. {
  65. pivot = (low + high)/2;
  66. mergesort(p, low, pivot);
  67. mergesort(p, pivot+1,high);
  68. merge(p,low,pivot,high);
  69. }
  70. }
  71.  
  72. void merge(point *p, int low, int pivot, int high) {
  73. int mh, mi, mj, mk;
  74. mh = low;
  75. mi = low;
  76. mj = pivot+1;
  77.  
  78. while((mh <= pivot) && (mj <= high)) {
  79. if(p[mh].pointZ <= p[mj].pointZ) {
  80. tmp[mi] = p[mh];
  81. mh++;
  82. } else {
  83. tmp[mi] = p[mj];
  84. mj++;
  85. }
  86. mi++;
  87. }
  88.  
  89. if(mh > pivot) {
  90. for(mk = mj; mk <= high; mk++) {
  91. tmp[mi] = p[mk];
  92. mi++;
  93. }
  94. } else {
  95. for(mk = mh; mk <= pivot; mk++) {
  96. tmp[mi] = p[mk];
  97. mi++;
  98. }
  99. }
  100. for(mk = low; mk <= high; mk++) p[mk]=tmp[mk];
  101. }
  102.  
  103.  
  104. void myinit() {
  105. // set the background color
  106. glClearColor(0.0, 0.0, 0.0, 1.0);
  107.  
  108. // set the foreground (pen) color
  109. glColor4f(1.0, 1.0, 1.0, 0.0005);
  110.  
  111. // set up the viewport
  112. glViewport(0,0,1366,768);
  113.  
  114. // set up the projection matrix (the camera)
  115. glMatrixMode(GL_PROJECTION);
  116. glLoadIdentity();
  117. gluOrtho2D(-20.0, 20.0, -11.24451, 11.24451);
  118.  
  119. // set up the modelview matrix (the objects)
  120. glMatrixMode(GL_MODELVIEW);
  121. glLoadIdentity();
  122.  
  123. GLint iMultiSample = 0;
  124. GLint iNumSamples = 0;
  125. glGetIntegerv(GL_SAMPLE_BUFFERS, &iMultiSample);
  126. glGetIntegerv(GL_SAMPLES, &iNumSamples);
  127.  
  128. // compute some initial iterations to settle into the orbit of the attractor
  129. for (int i = 0; i < initialIterations; i++) {
  130.  
  131. float xnew = x + h * a * (y - x);
  132. float ynew = y + h * (x * (b-z) - y);
  133. float znew = z + h * ( x * y - c * z);
  134.  
  135. float xdist = abs(xnew - x);
  136. float ydist = abs(ynew - y);
  137. float zdist = abs(znew - z);
  138. float xydist = sqrt(xdist*xdist + ydist*ydist);
  139. float dist = sqrt (xydist*xydist + zdist*zdist);
  140.  
  141. dOld = dist;
  142. xOld = x;
  143. yOld = y;
  144. zOld = z;
  145.  
  146. // save the new point
  147. x = xnew;
  148. y = ynew;
  149. z = znew;
  150.  
  151.  
  152.  
  153. }
  154.  
  155. // enable blending
  156. glEnable(GL_BLEND);
  157. glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
  158.  
  159. // enable point smoothing
  160. glEnable(GL_POINT_SMOOTH);
  161. glEnable(GL_MULTISAMPLE);
  162. glEnable(GL_LINE_SMOOTH);
  163. glHint(GL_LINE_SMOOTH, GL_NICEST);
  164. glHint(GL_LINE_SMOOTH_HINT, GL_NICEST);
  165. glHint(GL_POINT_SMOOTH_HINT, GL_NICEST);
  166. glHint(GL_POINT_SMOOTH, GL_NICEST);
  167. glPointSize(1.0f);
  168.  
  169. }
  170.  
  171. void mydisplay() {
  172.  
  173. // clear the screen
  174. glClear(GL_COLOR_BUFFER_BIT);
  175.  
  176. // draw some points
  177. glBegin(GL_POINTS);
  178.  
  179. // iterate through the equations many times, drawing one point for each iteration
  180. for (int i = 0; i < iterations; i++) {
  181.  
  182. // compute a new point using the strange attractor equations
  183.  
  184. float xnew = x + h * a * (y - x);
  185. float ynew = y + h * (x * (b-z) - y);
  186. float znew = z + h * ( x * y - c * z);
  187.  
  188. int mod = static_cast<int> ( i / 1000000 );
  189. float res = i - static_cast<float>( mod ) * 1000000;
  190. if (res == 0.0) {
  191. cout << i / 1000000 << " million rendered, res = " << res << endl;
  192. }
  193.  
  194. float xdist = abs(xnew - x);
  195. float ydist = abs(ynew - y);
  196. float zdist = abs(znew - z);
  197. float xydist = sqrt(xdist*xdist + ydist*ydist);
  198. float dist = sqrt (xydist*xydist + zdist*zdist);
  199. float dAvg = (dist + dOld) / 2;
  200. dOld = dist;
  201. dist = dAvg;
  202.  
  203. float ypos = (1 / (dist / 27));
  204. float angle = atan2(ypos, dist) * 4.0f;
  205. angle = angle + (3.1415926535)/3.0f;
  206. float r = 0.5 * ( sin(angle) ) + 0.5;
  207. float g = 0.5 * ( sin(angle + (2 * 3.1415926535)/3) ) + 0.5;
  208. float b = 0.5 * ( sin(angle + (4 * 3.1415926535)/3) ) + 0.5;
  209.  
  210. if (r >= 1.0f || g >= 1.0f || b >= 1.0f) {
  211. // cout << "R: " << r << "G: " << g << "B: " << b << endl;
  212. }
  213.  
  214. //vector<float> vx1, vy1, vz1, vx2, vy2, vz2;
  215.  
  216. xOld2 = xOld;
  217. yOld2 = yOld;
  218. zOld2 = zOld;
  219.  
  220. xOld = x;
  221. yOld = y;
  222. zOld = z;
  223.  
  224. x = xnew;
  225. y = ynew;
  226. z = znew;
  227.  
  228. // draw the new point
  229. //glVertex4f(xOld, yOld, 0, 1.0f);
  230.  
  231.  
  232. buff[i].pointX = xOld;
  233. buff[i].pointY = yOld;
  234. buff[i].pointZ = zOld;
  235. buff[i].pointR = r * 256;
  236. buff[i].pointG = g * 256;
  237. buff[i].pointB = b * 256;
  238.  
  239. }
  240.  
  241. //glBegin(GL_POINTS);
  242. for(int i = 0; i < iterations; i++) {
  243. //cout << buff[i].pointR << " " << buff[i].pointG << " " << buff[i].pointB << endl;
  244. glColor4f ((float)buff[i].pointR / 256.0f, (float)buff[i].pointG / 256.0f, (float)buff[i].pointB / 256.0f, 0.1f);
  245. //cout << buff[i].pointX << " " << buff[i].pointY << " " << buff[i].pointZ << endl;
  246. glVertex4f(buff[i].pointX, buff[i].pointY, /*buff[i].pointZ*/ 0.0, 1.0f);
  247. }
  248.  
  249. glEnd();
  250. // swap the buffers
  251. glutSwapBuffers();
  252.  
  253. for(int li = 0; li < 20; li ++) {
  254. cout << buff[li].pointZ << " ";
  255. }
  256. cout << endl;
  257.  
  258. mergesort(buff, 0, iterations);
  259.  
  260. for(int li = 0; li < 20; li ++) {
  261. cout << buff[li].pointZ << " ";
  262. }
  263. cout << endl;
  264.  
  265. cout << "Finished!" << endl;
  266.  
  267. }
  268.  
  269. void mykey(unsigned char mychar, int x, int y) {
  270.  
  271. // exit the program when the Esc key is pressed
  272. if (mychar == 27) {
  273. exit(0);
  274. }
  275.  
  276. }
  277.  
  278. int main (int argc, char **argv) {
  279.  
  280. buff = new point[iterations];
  281. tmp = new point[iterations];
  282.  
  283. // initialize GLUT
  284. glutInit(&argc, argv);
  285.  
  286. // set up our display mode for color with alpha and float buffering
  287. glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE | GLUT_DEPTH | GLUT_MULTISAMPLE);
  288.  
  289. // create a 400px x 400px window
  290. glutInitWindowSize(1366, 768);
  291. glutCreateWindow("Strange Attractors in C++ and OpenGL Tutorial");
  292.  
  293. // register our callback functions
  294. glutDisplayFunc(mydisplay);
  295. glutKeyboardFunc(mykey);
  296.  
  297. // call our initialization function
  298. myinit();
  299.  
  300. // start the program
  301. glutMainLoop();
  302.  
  303. return 0;
  304. }
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
prog.cpp:16:21: error: GL/glew.h: No such file or directory
prog.cpp:17:25: error: GL/freeglut.h: No such file or directory
prog.cpp: In function ‘void myinit()’:
prog.cpp:106: error: ‘glClearColor’ was not declared in this scope
prog.cpp:109: error: ‘glColor4f’ was not declared in this scope
prog.cpp:112: error: ‘glViewport’ was not declared in this scope
prog.cpp:115: error: ‘GL_PROJECTION’ was not declared in this scope
prog.cpp:115: error: ‘glMatrixMode’ was not declared in this scope
prog.cpp:116: error: ‘glLoadIdentity’ was not declared in this scope
prog.cpp:117: error: ‘gluOrtho2D’ was not declared in this scope
prog.cpp:120: error: ‘GL_MODELVIEW’ was not declared in this scope
prog.cpp:123: error: ‘GLint’ was not declared in this scope
prog.cpp:123: error: expected `;' before ‘iMultiSample’
prog.cpp:124: error: expected `;' before ‘iNumSamples’
prog.cpp:125: error: ‘GL_SAMPLE_BUFFERS’ was not declared in this scope
prog.cpp:125: error: ‘iMultiSample’ was not declared in this scope
prog.cpp:125: error: ‘glGetIntegerv’ was not declared in this scope
prog.cpp:126: error: ‘GL_SAMPLES’ was not declared in this scope
prog.cpp:126: error: ‘iNumSamples’ was not declared in this scope
prog.cpp:156: error: ‘GL_BLEND’ was not declared in this scope
prog.cpp:156: error: ‘glEnable’ was not declared in this scope
prog.cpp:157: error: ‘GL_SRC_ALPHA’ was not declared in this scope
prog.cpp:157: error: ‘GL_ONE_MINUS_SRC_ALPHA’ was not declared in this scope
prog.cpp:157: error: ‘glBlendFunc’ was not declared in this scope
prog.cpp:160: error: ‘GL_POINT_SMOOTH’ was not declared in this scope
prog.cpp:161: error: ‘GL_MULTISAMPLE’ was not declared in this scope
prog.cpp:162: error: ‘GL_LINE_SMOOTH’ was not declared in this scope
prog.cpp:163: error: ‘GL_NICEST’ was not declared in this scope
prog.cpp:163: error: ‘glHint’ was not declared in this scope
prog.cpp:164: error: ‘GL_LINE_SMOOTH_HINT’ was not declared in this scope
prog.cpp:165: error: ‘GL_POINT_SMOOTH_HINT’ was not declared in this scope
prog.cpp:167: error: ‘glPointSize’ was not declared in this scope
prog.cpp: In function ‘void mydisplay()’:
prog.cpp:174: error: ‘GL_COLOR_BUFFER_BIT’ was not declared in this scope
prog.cpp:174: error: ‘glClear’ was not declared in this scope
prog.cpp:177: error: ‘GL_POINTS’ was not declared in this scope
prog.cpp:177: error: ‘glBegin’ was not declared in this scope
prog.cpp:244: error: ‘glColor4f’ was not declared in this scope
prog.cpp:246: error: ‘glVertex4f’ was not declared in this scope
prog.cpp:249: error: ‘glEnd’ was not declared in this scope
prog.cpp:251: error: ‘glutSwapBuffers’ was not declared in this scope
prog.cpp: In function ‘void mykey(unsigned char, int, int)’:
prog.cpp:273: error: ‘exit’ was not declared in this scope
prog.cpp: In function ‘int main(int, char**)’:
prog.cpp:284: error: ‘glutInit’ was not declared in this scope
prog.cpp:287: error: ‘GLUT_RGBA’ was not declared in this scope
prog.cpp:287: error: ‘GLUT_DOUBLE’ was not declared in this scope
prog.cpp:287: error: ‘GLUT_DEPTH’ was not declared in this scope
prog.cpp:287: error: ‘GLUT_MULTISAMPLE’ was not declared in this scope
prog.cpp:287: error: ‘glutInitDisplayMode’ was not declared in this scope
prog.cpp:290: error: ‘glutInitWindowSize’ was not declared in this scope
prog.cpp:291: error: ‘glutCreateWindow’ was not declared in this scope
prog.cpp:294: error: ‘glutDisplayFunc’ was not declared in this scope
prog.cpp:295: error: ‘glutKeyboardFunc’ was not declared in this scope
prog.cpp:301: error: ‘glutMainLoop’ was not declared in this scope
stdout
Standard output is empty