Lightning generator for IRIX

From Higher Intellect Wiki
Jump to: navigation, search
/*****************************************
* Lightning generator!!!!
*
* Ludo Texier
*
*
*****************************************/

#include <stdio.h>
#include <gl/gl.h>
#include <gl/device.h>
#include <stdlib.h>
#include <math.h>


/*----------------------------------------
| open a window function                 |
-----------------------------------------*/

openWin(
int winX,		/* x size of window */
int winY,		/* y size of window */
char winName[] )	/* window name	*/

{

	float blackCol[3] = {
		0.0, 0.0, 0.0 								};
	short val;

	prefsize(winX,winY);
	winopen(winName);
	RGBmode();
	gconfig();
	c3f(blackCol);
	clear();

	qdevice(ESCKEY);
	qdevice(KEYBD);

	while(1){
		while (qtest() ) {
			switch(qread(&val)) {

			case ESCKEY:
				exit(0);

			case KEYBD:
				if (val=='d') {

					GenerateIt(winX,winY);
				}
			}
		}
	}



}

/*---------------------------------------
| draw in the window function           |
---------------------------------------*/
GenerateIt( winX, winY){

	int scanX,scanY;	/* pixel coordinate to be drawn */
	int RevScanY;		/* reverse of scanY	*/
	int filter;		/* number of time to filter	*/
	float   vert[2] = {
		0.0, 0.0 							};
	float   col[3] = {
		0.0, 0.0, 0.0 							};
	float red, green, blue;
	float randNum;		/* a random number */
	float randNum2;          /* anther random number */
	float tmp ;
	float tmp2,tmp4;
	int tmp3,tmp5;
	float data[360][243];	/*stores all the lightning*/
	float data2[360][243];	/* store the main branch of the lighning */
	int i,j;
	int dist;		/* dist between pixels in X */
	float forkLim=.98; 	/* chances that the lightning will fork */
	float noFork=.002;	/* stop the lightning there */
	float intensity;
	float baseIntens;
	float contrast=.50;		/* color contrast */
	int bozo,bozo2;
	int filtSize=2;


	randNum=drand48();
	randNum2=(drand48());

	/*initialization*/
	for (i=0;i<winX;i++){
		for (j=0;j<winY;j++){
			data[i][j]=0;
			data2[i][j]=0;

		}
	}

	data[180][0]=1;

	intensity=6.0;
	baseIntens=intensity;





	/*-------------------------------------------*/
	/* loop that generate the  lightning  strike */
	/*-------------------------------------------*/

	for (scanY=1;scanY<winY;scanY++){
		for (scanX=1;scanX<winX;scanX++){
			randNum=drand48();
			randNum2=(drand48())*5;	/* distance to move away */
			dist=randNum2;
			if (data[scanX][scanY-1]){
				if (randNum<noFork){
					(data[scanX-dist][scanY])=0;
				}
				if ((randNum<.33)&& (randNum>=noFork)) {
					(data[scanX-dist][scanY])=1;
				}
				if ((randNum>.66)&&(randNum<forkLim)) {
					(data[scanX][scanY])=data[scanX][scanY-1];
				}
				if ((randNum>.33)&&(randNum<.66)) {
					(data[scanX+dist][scanY])=data[scanX][scanY-1];
				}
				if (randNum>forkLim){
					(data[scanX-dist][scanY])=data[scanX][scanY-1];
					(data[scanX+dist][scanY])=data[scanX][scanY-1];
				}
			}
		}
	}



	/* filter loop */
	/* add pixels to make a constant line */

	for (scanY=1;scanY<winY;scanY++){
		for(scanX=1;scanX<winX;scanX++){
			if ((data[scanX][scanY])==1){
				for (i=(scanX-5);i<scanX;i++){	/*scan left*/
					if ((data[i][scanY-1])==1){
						for(j=i;j<scanX;j++){
							data[j][scanY-1]=1;
						}
					}
				}
				for (i=(scanX+5);i>scanX;i--){  /*scan right*/
					if ((data[i][scanY-1])==1){
						for(j=i;j>scanX;j--){
							data[j][scanY-1]=1;
						}
					}
				}
			}
		}
	}




	/*-------------*/
	/* strong glow */
	/*-------------*/

	/* determine where the main lightning is */

	scanY=winY-1;
	scanX=0;
	while (data[scanX][scanY]!=1){
		if (scanX==winX) break;
		scanX++;
	}
	for (scanY=(winY-1);scanY>=1;scanY--){
		for (i=(scanX-4);i<=(scanX+4);i++){
			if (data[i][scanY]==1){
				tmp5=i;
				data2[i][scanY]=1;
			}

		}
		scanX=tmp5;
	}





	/* makes the glow on the main lightning */

	for (scanY=0;scanY<winY;scanY++){
		for (scanX=0;scanX<winX;scanX++){
			if (data2[scanX][scanY]==1){
				randNum2=(drand48())*20;
				for (i=-(filtSize);i<(filtSize+1);i++){
					for (j=-(filtSize);j<(filtSize+1);j++){
						data[scanX+i][scanY+j]+=
						    (6.0-(abs(i)+abs(j)))*intensity/90.0;

					}
				}
			}
		}
	}



	/*-------------*/
	/* glow effect */
	/*-------------*/

	/* add a smaller glow everywhere */

	for (scanY=0;scanY<winY;scanY++){
		for (scanX=0;scanX<winX;scanX++){
			if (data[scanX][scanY]==1){
				randNum2=(drand48())*20;
				for (i=-(filtSize);i<(filtSize+1);i++){
					for (j=-(filtSize);j<(filtSize+1);j++){
						data[scanX+i][scanY+j]+=
						    (6.0-(abs(i)+abs(j)))*intensity/40.0;
					}
				}
			}
		}
	}



	/*------------------*/
	/* print pixel loop */
	/*------------------*/

	for (scanX=0;scanX<winX;scanX++){
		for (scanY=0;scanY<winY;scanY++){
			RevScanY=winY-scanY;		/* reverse the window !!! */
			blue=(data[scanX][scanY])*contrast;
			green=blue/1.5;
			red=green/1.5;
			vert[0] = (float)scanX;
			vert[1] = (float)RevScanY;
			col[0] = red;
			col[1] = green;
			col[2] = blue;

			bgnpoint();
			c3f(col);
			v2f(vert);
			endpoint();

		}
	}
	system("playaiff ./ligh_str.aiff");
}

/*---------------------------------------
-----------------------------------------
||   Begin of Main 				       ||
-----------------------------------------
---------------------------------------*/

main()
{
	char winName [100];
	printf(">>> Eclair v1 3/28/96 . Lightning generator. <<<\n");
	printf("Hit 'D' to get a new strike.\nEsc to quit.\n");
	openWin(360,243,"Eclair");
}

Also uses https://web.archive.org/web/19970617090049fw_/http://reality.sgi.com:80/ludo_sb/cstuff/ligh_str.aiff



Share your opinion