Advertisement
Guest User

Untitled

a guest
May 24th, 2018
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.71 KB | None | 0 0
  1. //
  2. // to compile type... make
  3. // uses file named... Makefile
  4. //
  5. // a.out: fireGL.c
  6. // gcc fireGL.c -lGL -lGLU -lglut
  7. //
  8. // tab character '\t' before gcc
  9. //
  10. #include <stdio.h>
  11. #include <stdlib.h>
  12. #include <time.h>
  13. #include <math.h>
  14. //
  15. //#include <OpenGL/gl.h>
  16. //#include <OpenGL/glu.h>
  17. #include <GL/glut.h>
  18. //
  19. #define N 600
  20. #define TREE 1
  21. #define FIRE 2
  22. #define SPARK 3
  23. #define BURNT 8
  24. #define EMPTY 0
  25. //
  26. char t[N][N] ;
  27. int pause = 0 ;
  28. int step ;
  29. double prob = 0.60 ;
  30. int w = N;
  31. int h = N;
  32.  
  33.  
  34. double zoom = 1;
  35. double movex = -0.5;
  36. double movey = 0;
  37.  
  38. double center_x;
  39. double center_y;
  40.  
  41. double rgb[N][N][3];
  42. //
  43.  
  44. //
  45. void mandle()
  46. {
  47. int x , y;
  48.  
  49. //
  50. for(y = 0; y < h; y++)
  51. {
  52. for(x = 0; x < w; x++)
  53. {
  54.  
  55. double pr, pi; //real and imaginary part of the pixel p
  56. double newRe, newIm, oldRe, oldIm; //real and imaginary parts of new and old z
  57. //double zoom = 1, moveX = -0.5, moveY = 0; //you can change these to zoom and change position
  58. int maxIterations = 100;//after how much iterations the function should stop
  59.  
  60. //calculate the initial real and imaginary part of z, based on the pixel location and zoom and position values
  61. pr = 1.5 * (x - w / 2) / (0.5 * zoom * w) + movex;
  62. pi = (y - h / 2) / (0.5 * zoom * h) + movey;
  63. newRe = newIm = oldRe = oldIm = 0; //these should start at 0,0
  64. //"i" will represent the number of iterations
  65. int i;
  66. //start the iteration process
  67. for(i = 0; i < maxIterations; i++)
  68. {
  69. //remember value of previous iteration
  70. oldRe = newRe;
  71. oldIm = newIm;
  72. //the actual iteration, the real and imaginary part are calculated
  73. newRe = oldRe * oldRe - oldIm * oldIm + pr;
  74. newIm = 2 * oldRe * oldIm + pi;
  75. //if the point is outside the circle with radius 2: stop
  76. if((newRe * newRe + newIm * newIm) > 4) break;
  77. }
  78. if(i == maxIterations){
  79. rgb[y][x][0] = 0.0;
  80. rgb[y][x][1] = 0.0;
  81. rgb[y][x][2] = 0.0;
  82. }
  83. else{
  84. double val = sqrt(newRe * newRe + newIm * newIm);
  85. int colorval = 256. * log2(1.75 + i -log2(log2(val))) / log2((double)maxIterations);
  86. rgb[y][x][0] = (double) colorval/255;
  87. rgb[y][x][1] = (double) colorval/255;
  88. rgb[y][x][2] = 1.0;
  89.  
  90. }
  91.  
  92. }
  93. }
  94. }
  95. //
  96.  
  97.  
  98.  
  99. //
  100. // void idlefunc()
  101. // {
  102. // int x , y ;
  103. // int fire = 0 ;
  104. // //
  105. // if( pause ) return ;
  106. // //
  107. // // first...
  108. // //
  109. // for( x = 0 ; x < N ; x++ )
  110. // {
  111. // for( y = 0 ; y < N ; y++ )
  112. // {
  113. // if( t[y][x] == FIRE )
  114. // {
  115. // ck( y - 1 , x ) ;
  116. // ck( y + 1 , x ) ;
  117. // ck( y , x - 1 ) ;
  118. // ck( y , x + 1 ) ;
  119. // }
  120. // }
  121. // }
  122. // //
  123. // // second...
  124. // //
  125. // for( x = 0 ; x < N ; x++ )
  126. // {
  127. // for( y = 0 ; y < N ; y++ )
  128. // {
  129. // if( t[y][x] == FIRE )
  130. // {
  131. // t[y][x] = BURNT ;
  132. // }
  133. // else if( t[y][x] == SPARK )
  134. // {
  135. // fire = 1;
  136. // //
  137. // t[y][x] = FIRE ;
  138. // }
  139. // }
  140. // }
  141. // //
  142. // if( fire )
  143. // {
  144. // ++step ;
  145. // //
  146. // glutPostRedisplay(); // calls displayfunc
  147. // }
  148. // }
  149.  
  150. /*
  151. GLUT_BITMAP_8_BY_13
  152. GLUT_BITMAP_9_BY_15
  153. GLUT_BITMAP_TIMES_ROMAN_10
  154. GLUT_BITMAP_TIMES_ROMAN_24
  155. GLUT_BITMAP_HELVETICA_10
  156. GLUT_BITMAP_HELVETICA_12
  157. GLUT_BITMAP_HELVETICA_18
  158. */
  159. void displayfunc()
  160. {
  161. int x , y ;
  162. //
  163. //
  164. glClear(GL_COLOR_BUFFER_BIT); // white
  165. //
  166. for( y = 0 ; y < h ; y++ )
  167. {
  168. for( x = 0 ; x < w; x++ )
  169. {
  170. glColor3f(rgb[y][x][0], rgb[y][x][1], rgb[y][x][2]); // brown
  171.  
  172. //
  173. glBegin(GL_POINTS);
  174. glVertex2f(x,y);
  175. glEnd();
  176. }
  177. }
  178. glutSwapBuffers(); // single buffering... call glFlush();
  179. }
  180. void reshapefunc(int wscr,int hscr)
  181. {
  182. glViewport(0,0,(GLsizei)w,(GLsizei)h);
  183. glMatrixMode(GL_PROJECTION);
  184. glLoadIdentity();
  185. gluOrtho2D(0.0,1.0*w,1.0*h, 0.0); // always a square
  186. glMatrixMode(GL_MODELVIEW);
  187. }
  188. void mousefunc(int button,int state,int xscr,int yscr)
  189. {
  190. if(button==GLUT_LEFT_BUTTON)
  191. {
  192. if(state==GLUT_DOWN)
  193. {
  194. movex = 1.5 * (xscr -w/2) / (0.5 *zoom*w)+movex;
  195. movey = (yscr - h/2) / (0.5 * zoom * h) + movey;
  196. zoom *= 1.3;
  197. mandle();
  198. glutPostRedisplay();
  199. }
  200. }
  201. else if(button==GLUT_RIGHT_BUTTON)
  202. {
  203. if(state==GLUT_DOWN)
  204. {
  205. movex = 1.5 * (xscr -w/2) / (0.5 *zoom*w)+movex;
  206. movey = (yscr - h/2) / (0.5 * zoom * h) + movey;
  207. zoom *= 0.7;
  208. mandle();
  209. glutPostRedisplay();
  210. }
  211. }
  212. }
  213. void keyfunc(unsigned char key,int xscr,int yscr)
  214. {
  215. if( key == ' ' )
  216. {
  217. pause = !pause ;
  218. }
  219. else if( key == 'q' )
  220. {
  221. exit( 0 ) ;
  222. }
  223. }
  224. int main(int argc,char* argv[])
  225. {
  226. glutInit(&argc,argv);
  227. glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
  228. glutInitWindowSize(N,N);
  229. glutInitWindowPosition(200,150);
  230. glutCreateWindow("");
  231. glClearColor(1.0,1.0,1.0,0.0);
  232. glShadeModel(GL_SMOOTH);
  233. //
  234. //
  235. // init( prob ) ;
  236. // fire( ) ;
  237. mandle();
  238. //
  239. //glutIdleFunc(idlefunc);
  240. glutDisplayFunc(displayfunc);
  241. glutReshapeFunc(reshapefunc);
  242. glutMouseFunc(mousefunc);
  243. glutKeyboardFunc(keyfunc);
  244. //
  245. glutMainLoop();
  246. //
  247. return 0;
  248. }
  249. //
  250. // end of file
  251. //
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement