Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //
- // to compile type... make
- // uses file named... Makefile
- //
- // a.out: fireGL.c
- // gcc fireGL.c -lGL -lGLU -lglut
- //
- // tab character '\t' before gcc
- //
- #include <stdio.h>
- #include <stdlib.h>
- #include <time.h>
- #include <math.h>
- //
- //#include <OpenGL/gl.h>
- //#include <OpenGL/glu.h>
- #include <GL/glut.h>
- //
- #define N 600
- #define TREE 1
- #define FIRE 2
- #define SPARK 3
- #define BURNT 8
- #define EMPTY 0
- //
- char t[N][N] ;
- int pause = 0 ;
- int step ;
- double prob = 0.60 ;
- int w = N;
- int h = N;
- double zoom = 1;
- double movex = -0.5;
- double movey = 0;
- double center_x;
- double center_y;
- double rgb[N][N][3];
- //
- //
- void mandle()
- {
- int x , y;
- //
- for(y = 0; y < h; y++)
- {
- for(x = 0; x < w; x++)
- {
- double pr, pi; //real and imaginary part of the pixel p
- double newRe, newIm, oldRe, oldIm; //real and imaginary parts of new and old z
- //double zoom = 1, moveX = -0.5, moveY = 0; //you can change these to zoom and change position
- int maxIterations = 100;//after how much iterations the function should stop
- //calculate the initial real and imaginary part of z, based on the pixel location and zoom and position values
- pr = 1.5 * (x - w / 2) / (0.5 * zoom * w) + movex;
- pi = (y - h / 2) / (0.5 * zoom * h) + movey;
- newRe = newIm = oldRe = oldIm = 0; //these should start at 0,0
- //"i" will represent the number of iterations
- int i;
- //start the iteration process
- for(i = 0; i < maxIterations; i++)
- {
- //remember value of previous iteration
- oldRe = newRe;
- oldIm = newIm;
- //the actual iteration, the real and imaginary part are calculated
- newRe = oldRe * oldRe - oldIm * oldIm + pr;
- newIm = 2 * oldRe * oldIm + pi;
- //if the point is outside the circle with radius 2: stop
- if((newRe * newRe + newIm * newIm) > 4) break;
- }
- if(i == maxIterations){
- rgb[y][x][0] = 0.0;
- rgb[y][x][1] = 0.0;
- rgb[y][x][2] = 0.0;
- }
- else{
- double val = sqrt(newRe * newRe + newIm * newIm);
- int colorval = 256. * log2(1.75 + i -log2(log2(val))) / log2((double)maxIterations);
- rgb[y][x][0] = (double) colorval/255;
- rgb[y][x][1] = (double) colorval/255;
- rgb[y][x][2] = 1.0;
- }
- }
- }
- }
- //
- //
- // void idlefunc()
- // {
- // int x , y ;
- // int fire = 0 ;
- // //
- // if( pause ) return ;
- // //
- // // first...
- // //
- // for( x = 0 ; x < N ; x++ )
- // {
- // for( y = 0 ; y < N ; y++ )
- // {
- // if( t[y][x] == FIRE )
- // {
- // ck( y - 1 , x ) ;
- // ck( y + 1 , x ) ;
- // ck( y , x - 1 ) ;
- // ck( y , x + 1 ) ;
- // }
- // }
- // }
- // //
- // // second...
- // //
- // for( x = 0 ; x < N ; x++ )
- // {
- // for( y = 0 ; y < N ; y++ )
- // {
- // if( t[y][x] == FIRE )
- // {
- // t[y][x] = BURNT ;
- // }
- // else if( t[y][x] == SPARK )
- // {
- // fire = 1;
- // //
- // t[y][x] = FIRE ;
- // }
- // }
- // }
- // //
- // if( fire )
- // {
- // ++step ;
- // //
- // glutPostRedisplay(); // calls displayfunc
- // }
- // }
- /*
- GLUT_BITMAP_8_BY_13
- GLUT_BITMAP_9_BY_15
- GLUT_BITMAP_TIMES_ROMAN_10
- GLUT_BITMAP_TIMES_ROMAN_24
- GLUT_BITMAP_HELVETICA_10
- GLUT_BITMAP_HELVETICA_12
- GLUT_BITMAP_HELVETICA_18
- */
- void displayfunc()
- {
- int x , y ;
- //
- //
- glClear(GL_COLOR_BUFFER_BIT); // white
- //
- for( y = 0 ; y < h ; y++ )
- {
- for( x = 0 ; x < w; x++ )
- {
- glColor3f(rgb[y][x][0], rgb[y][x][1], rgb[y][x][2]); // brown
- //
- glBegin(GL_POINTS);
- glVertex2f(x,y);
- glEnd();
- }
- }
- glutSwapBuffers(); // single buffering... call glFlush();
- }
- void reshapefunc(int wscr,int hscr)
- {
- glViewport(0,0,(GLsizei)w,(GLsizei)h);
- glMatrixMode(GL_PROJECTION);
- glLoadIdentity();
- gluOrtho2D(0.0,1.0*w,1.0*h, 0.0); // always a square
- glMatrixMode(GL_MODELVIEW);
- }
- void mousefunc(int button,int state,int xscr,int yscr)
- {
- if(button==GLUT_LEFT_BUTTON)
- {
- if(state==GLUT_DOWN)
- {
- movex = 1.5 * (xscr -w/2) / (0.5 *zoom*w)+movex;
- movey = (yscr - h/2) / (0.5 * zoom * h) + movey;
- zoom *= 1.3;
- mandle();
- glutPostRedisplay();
- }
- }
- else if(button==GLUT_RIGHT_BUTTON)
- {
- if(state==GLUT_DOWN)
- {
- movex = 1.5 * (xscr -w/2) / (0.5 *zoom*w)+movex;
- movey = (yscr - h/2) / (0.5 * zoom * h) + movey;
- zoom *= 0.7;
- mandle();
- glutPostRedisplay();
- }
- }
- }
- void keyfunc(unsigned char key,int xscr,int yscr)
- {
- if( key == ' ' )
- {
- pause = !pause ;
- }
- else if( key == 'q' )
- {
- exit( 0 ) ;
- }
- }
- int main(int argc,char* argv[])
- {
- glutInit(&argc,argv);
- glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
- glutInitWindowSize(N,N);
- glutInitWindowPosition(200,150);
- glutCreateWindow("");
- glClearColor(1.0,1.0,1.0,0.0);
- glShadeModel(GL_SMOOTH);
- //
- //
- // init( prob ) ;
- // fire( ) ;
- mandle();
- //
- //glutIdleFunc(idlefunc);
- glutDisplayFunc(displayfunc);
- glutReshapeFunc(reshapefunc);
- glutMouseFunc(mousefunc);
- glutKeyboardFunc(keyfunc);
- //
- glutMainLoop();
- //
- return 0;
- }
- //
- // end of file
- //
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement