|
|
Just a Game of Life. I wanted to see the transitions in state instead of just
flipping cells.
ingo
---%<---%<---%<---
// Persistence of Vision Ray Tracer Scene Description File
// File: conway.pov
// Vers: 3.7
// Desc: Game of Life, "slow version" to show the transistions
// Date: 2022/06/16
// Auth: Ingo Janssen
//
// cmd: +am2 +a0.5 +w640 +h360 +kff3000
// ffmpeg -r 30 -f image2 -s 640x360 -i conway%04d.png -vcodec libx265 -crf 30
-preset veryslow -pix_fmt yuv420p conway.mp4
#version 3.7;
#include "rand.inc"
#include "math.inc"
global_settings{ assumed_gamma 1.0 }
#default{ finish{ ambient 0.1 diffuse 0.9 }}
#declare Stream = seed(777);
#declare _CAstate_current = 0;
#declare _CAstate_last = 1;
// variables
#declare _CAdead = 0;
#declare _CAlive = 1;
/*====
CAinit(SizeX, SizeY): Initialize the two dimensional grid for the Game of life.
It will contain te current state and the last state of each cell in the grid.
SizeX, SizeY: the grid dimensions
*/
#macro CAinit(SizeX, SizeY)
#local CAgrid = array[SizeX][SizeY];
#for(i,0,SizeY-1)
#for(j,0,SizeX-1)
#local CAgrid[j][i] = array[2]{0,0};
#end
#end
CAgrid
#end
/*====
CArndState(CAgrid): Randomize the grid's current state, fill with 1 & 0's
CAgrid: Two dimensional array that is the grid.
*/
#macro CArndState(CAgrid)
#for(i, 0, dimension_size(CAgrid, 2) - 1)
#for(j, 0, dimension_size(CAgrid, 1) - 1)
#local rndVal = Prob(0.5, Stream);
#local CAgrid[j][i][_CAstate_current] = rndVal;
#end
#end
#end
/*====
CAwrite(CAgrid, Path): writes the current and last state of the grid to file
CAgrid: Two dimensional array that is the grid.
Path: path/file name to write the file to.
*/
#macro CAwrite(CAgrid, Path)
#local SizeX = dimension_size(CAgrid,1);
#local SizeY = dimension_size(CAgrid,2);
#local Vals = "";
#for(i, 0, SizeY - 1)
#for(j, 0, SizeX - 1)
#local Vals = concat(Vals,
str(CAgrid[j][i][_CAstate_current],0,0),",",
str(CAgrid[j][i][_CAstate_last],0,0),","
);
#end
#local Vals = concat(Vals, "\n");
#end
#fopen CAFile Path write
#write (CAFile SizeX "," SizeY "\n")
#write (CAFile Vals)
#fclose CAFile
#end
/*====
CAread(CAgrid, Path): reads the current and last state of the grid from file
into the grid
CAgrid: Two dimensional array that is the grid and holds the states
Path: path/file name to write the file to.
*/
#macro CAread(CAgrid, Path)
#local SizeX = 0;
#local SizeY = 0;
#fopen CAFile Path read
#read(CAFile SizeX, SizeY)
#for(i, 0, SizeY - 1)
#for(j, 0, SizeX - 1)
#read(CAFile
CAgrid[j][i][_CAstate_current],
CAgrid[j][i][_CAstate_last],
)
#end
#end
#fclose CAFile
#end
#macro CAdebug(CAgrid)
#debug concat("Frame number: ",str(frame_number,0,0),"\n")
#debug "Current state:\n"
#for(i, 0, dimension_size(CAgrid, 2) - 1)
#for(j, 0, dimension_size(CAgrid, 1) - 1)
#debug concat(str(CAgrid[j][i][_CAstate_current],0,0)," ")
#end
#debug "\n"
#end
#debug "Last state:\n"
#for(i, 0, dimension_size(CAgrid, 2) - 1)
#for(j, 0, dimension_size(CAgrid, 1) - 1)
#debug concat(str(CAgrid[j][i][_CAstate_last],0,0)," ")
#end
#debug "\n"
#end
#debug "\n"
#end
#declare RollOver = function(ModAB, B){
select(ModAB, B + ModAB, ModAB)
}//#debug concat(str(RollOver(mod(-0.1, 3), 3),0,-1),"\n")
#macro CAgetNeighbours(CAgrid, CurrentCell)
#local SizeX = dimension_size(CAgrid, 1);
#local SizeY = dimension_size(CAgrid, 2);
#local Sum = 0;
#for(i, CurrentCell.y-1, CurrentCell.y+1)
#local I = RollOver(mod(i,SizeY), SizeY);
#for(j, CurrentCell.x-1, CurrentCell.x+1)
#local J = RollOver(mod(j,SizeX), SizeX);
#local Sum = CAgrid[J][I][_CAstate_last] + Sum;
#end
#end
#local Sum = Sum - CAgrid[CurrentCell.x][CurrentCell.y][_CAstate_last];
Sum
#end
#macro CArulerun(CAgrid)
#local SizeX = dimension_size(CAgrid,1);
#local SizeY = dimension_size(CAgrid,2);
#for(i, 0, SizeY - 1)
#for(j, 0, SizeX - 1)
#local CAgrid[j][i][_CAstate_last] = CAgrid[j][i][_CAstate_current];
#end
#end
#for(i, 0, SizeY - 1)
#for(j, 0, SizeX - 1)
#local CurrentCell = <j,i>;
#local CurrentValue = CAgrid[j][i][_CAstate_last];
#local Neighbour = CAgetNeighbours(CAgrid, CurrentCell);
#if(CurrentValue = 1 & Neighbour < 2)
#local CAgrid[j][i][_CAstate_current] = 0;
#elseif(CurrentValue = 1 & Neighbour > 3)
#local CAgrid[j][i][_CAstate_current] = 0;
#elseif(CurrentValue = 0 & Neighbour = 3)
#local CAgrid[j][i][_CAstate_current] = 1;
#end
#end
#end
#end
#macro CAobject(CAgrid, Frames)
#local Frame = mod(frame_number, Frames);
#local Half = Frames/2;
#debug concat(str(Frame,0,-1)," , ",str(Half,0,-1) ,"\n")
union {
#if(Frame>Half)
#for(i, 0, dimension_size(CAgrid, 2) - 1)
#for(j, 0, dimension_size(CAgrid, 1) - 1)
#if(CAgrid[j][i][_CAstate_current] = 1)
sphere{<j,i,0> 0.5 pigment {rgb 1}}
#end
#end
#end
#elseif(Frame <= Half)
#for(i, 0, dimension_size(CAgrid, 2) - 1)
#for(j, 0, dimension_size(CAgrid, 1) - 1)
#if(CAgrid[j][i][_CAstate_current] != CAgrid[j][i][_CAstate_last])
#if (CAgrid[j][i][_CAstate_current] = 1)
sphere{<j,i,0> adj_range2(Frame,0,Half,0,0.5) pigment {rgb 1}}
#else
sphere{<j,i,0> adj_range2(Frame,0,Half,0.5,0) pigment {rgb 1}}
#end
#elseif(
CAgrid[j][i][_CAstate_current] = CAgrid[j][i][_CAstate_last]
& CAgrid[j][i][_CAstate_current] = 1
)
sphere{<j,i,0> 0.5 pigment {rgb 1}}
#end
#end
#end
#end
}
#end
#declare TempFn = "conway.arr" //ramdisc
#declare Frames = 20;
#declare SizeX = 48;
#declare SizeY = 27;
#declare Conway = CAinit(SizeX, SizeY);
#if(file_exists(TempFn))
CAread(Conway, TempFn)
#else
CArndState(Conway)
#end
#if(mod(frame_number, Frames) = 0 & frame_number > 1)
CArulerun(Conway)
CAwrite(Conway, TempFn)
#end
//CAdebug(Conway)
object{CAobject(Conway, Frames)}
camera {
angle 55
location <(SizeX/2)-0.5,(SizeY/2)-0.5, -50>
look_at <(SizeX/2)-0.5,(SizeY/2)-0.5, 0>
right x*image_width/image_height
}
light_source{
<SizeX, SizeY, -2000>, rgb 1
}
Post a reply to this message
Attachments:
Download 'conway.mp4.dat' (2243 KB)
|
|