Posted By

autofasurer on 10/27/10


Tagged

opengl


Versions (?)

Spirograph


 / Published in: C++
 

URL: http://www.autofasurer.net/wp/

GLUT / OpenGL / C++ (although not much happening there). The finished app is available at the above link. Haven't tested on anything but my own Macbook Pro, so use at your own risk :)

  1. /*
  2.  * harmonograph.cpp
  3.  *
  4.  * Created by Brecht Debackere on 02/04/10.
  5.  * Copyright 2010 Autofasurer. All rights reserved.
  6.  *
  7.  */
  8.  
  9. #include <GLUT/glut.h>
  10. #include <iostream>
  11. using namespace std;
  12.  
  13. //initialising variables
  14. float fRed = 1.0, fGreen = 1.0, fBlue =1.0, fXpos = 0.0, fYpos = 0.0, fFrequency = 0.0, fAmplitude = 0.0, fPhase = 0.0, fTime = 0.0, fPointsize = 1.0;
  15.  
  16.  
  17. //initialising states
  18. void init()
  19. {
  20. glClearColor(0.0,0.0,0.0,0.7);
  21. glEnable(GL_BLEND);
  22. glBlendFunc(GL_ONE, GL_ONE);
  23. }
  24.  
  25. //reshaping the viewport and projection
  26. void reshape(int width, int height)
  27. {
  28. glViewport(0, 0, 1440, 900);
  29. glMatrixMode(GL_PROJECTION);
  30. glLoadIdentity();
  31. gluPerspective(45.,(GLfloat) width/(GLfloat) height,0.1, 800.0);
  32. glMatrixMode(GL_MODELVIEW);
  33. gluLookAt(0.0,0.0,10.0, 0.0, 0.0, 0.0, 0,1,0);
  34.  
  35. }
  36.  
  37. //reading the keyboard input
  38. void key(unsigned char key, int x, int y){
  39. switch (key){
  40. case 32:
  41. fFrequency = (rand()%500-250)/5.0;
  42. fPhase = (rand()%500-250)/5.0;
  43. fAmplitude = (rand()%500-250)/5.0;
  44. fTime = 0.0;
  45. break;
  46. }
  47. }
  48.  
  49.  
  50. //drawing and displaying
  51. void display()
  52. {
  53. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  54. glBegin(GL_LINE_STRIP);
  55. while (fTime < 400.0) {
  56. glColor3d(fRed, fGreen, fBlue);
  57. fXpos = 4*(cos(fFrequency * fTime) + cos(fPhase * fTime)*0.5 + cos(fAmplitude * fTime)*0.33);
  58. fYpos = 4*(sin(fFrequency * fTime) + sin(fPhase * fTime)*0.5 + sin(fAmplitude * fTime)*0.33);
  59. glVertex3f(fXpos, fYpos, 0);
  60. fTime += 0.002;
  61. }
  62. glEnd();
  63. fFrequency -= 0.0001;
  64. fAmplitude += 0.0001;
  65. fPhase += 0.0001;
  66. fTime = 0.0;
  67. glutSwapBuffers();
  68. fRed = (rand()%100)/400.0;
  69. fGreen = (rand()%100)/400.0;
  70. fBlue = (rand()%100)/400.0;
  71. }
  72.  
  73. //main loop with initialising of window and glut, assigning functions
  74. int main(int argc, char** argv)
  75. {
  76. srand(time(NULL));
  77. glutInit(&argc, argv);
  78. glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
  79. glutGameModeString("1440x900:32");
  80. glutEnterGameMode();
  81. init();
  82. glutDisplayFunc(display);
  83. glutIdleFunc(display);
  84. glutReshapeFunc(reshape);
  85. glutKeyboardFunc(key);
  86. glutMainLoop();
  87. return 0;
  88. }

Report this snippet  

You need to login to post a comment.