main.cpp 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. #include <iostream>
  2. #include <math.h>
  3. #include <GL/freeglut.h>
  4. using std::cout;
  5. using std::endl;
  6. void initFunc();
  7. void displayFunc();
  8. void keyboardFunc(unsigned char key, int x, int y);
  9. // strange attractor
  10. float x = 0.1;
  11. float y = 0.1;
  12. float a = -0.966918; // ?
  13. float b = 2.879879; // ?!
  14. float c = 0.765145; // ??
  15. float d = 0.744728; // ??!
  16. int initialIterations = 100; // ???
  17. int iterations = 1000000;
  18. void attractor_iteration() {
  19. float xnew = sin(y*b) + c*sin(x*b);
  20. float ynew = sin(x*a) + d*sin(y*a);
  21. x = xnew;
  22. y = ynew;
  23. }
  24. int main(int argc, char const *argv[])
  25. {
  26. cout << "main" << endl;
  27. glutInit(&argc, (char**)argv);
  28. // set up our display mode for color with alpha and double buffering
  29. glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE);
  30. // create a 400px x 400px window
  31. glutInitWindowSize(400, 400);
  32. glutCreateWindow("Strange Attractors in C++ and OpenGL");
  33. glutDisplayFunc(displayFunc);
  34. glutKeyboardFunc(keyboardFunc);
  35. initFunc();
  36. glutMainLoop();
  37. return 0;
  38. }
  39. void initFunc() {
  40. glClearColor(0.0f, 0.0f, 0.0f, 0.0f); // background
  41. glColor4f(1.0f, 1.0f, 1.0f, 0.02f); // foreground (pen) color
  42. glViewport(0, 0, 400, 400);
  43. // blending (transparency)
  44. glEnable(GL_BLEND);
  45. glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
  46. // point smoothing
  47. glEnable(GL_POINT_SMOOTH);
  48. glPointSize(1.0f);
  49. // setup projection matrix (camera)
  50. glMatrixMode(GL_PROJECTION);
  51. glLoadIdentity();
  52. gluOrtho2D(-2.0f, 2.0f, -2.0f, 2.0f);
  53. // setup model matrix (objects)
  54. glMatrixMode(GL_MODELVIEW);
  55. glLoadIdentity();
  56. // strange attractor initial iterations to settle into an orbit (?)
  57. for (int i = 0; i < initialIterations; i++) {
  58. attractor_iteration();
  59. }
  60. }
  61. void displayFunc() {
  62. glClear(GL_COLOR_BUFFER_BIT); // clear screen
  63. glBegin(GL_POINTS);
  64. for(int i = 0; i < iterations; i++) {
  65. attractor_iteration();
  66. glVertex2f(x, y);
  67. }
  68. glEnd();
  69. glutSwapBuffers();
  70. }
  71. void keyboardFunc(unsigned char key, int x, int y) {
  72. cout << "pressed '" << key << "'" << endl;
  73. if(key == 27) { //esc
  74. exit(0);
  75. }
  76. }