CMPS-3480 Computer Graphics
Lab-7

Overview:
Step 1:
Log into Odin, using the -YC option.
   example: ssh -YC myname@odin

Do this:

   $ cd 3480
   $ ./lab-start.sh
   $ cd 7
   $ cp /home/fac/gordon/p/3480/code/7/* .
   $ make
   $ ./lab7

Press M

Step 1: render a scene similar to the one below



This scene is also possible




Step 2: create a short animation

   Follow along with Gordon to produce an animation

Step 3: make your own ray-traced animation

   1. Animation must loop forever.

   2. Animation must be seamless.

      Viewer does not know where it starts or ends.
      
      It can go up and down, left and right, or around.

      You may make the checkerboard floor texture move or change
	  to give the appearance of the camera following an object..

      Use the objects given.

   3. Create gif animation from program.

      Create your gif animation at the command-line something like this:

	     convert -delay  -loop 0 *.png mylab7.gif

      Your program will capture frames and create the gif animation
	  on a key press. Add the key to your menu.

   4. Make a web page at public_html/3480/lab7.html

      Display your animations.

//======================================================================= Screen shot function that produces .png files PNG is about 95% smaller than a PPM file. void takeScreenshot(const char *filename, int reset) { //This function will capture your current X11 window, //and save it to a PPM P6 image file. //File names are generated sequentially. static int picnum = 0; int x,y; int width, height; x11.getWindowAttributes(&width, &height); if (reset) picnum = 0; XImage *image = x11.getImage(width, height); // //If filename argument is empty, generate a sequential filename... char ts[200] = ""; char png[200] = ""; strcpy(ts, filename); strcpy(png, filename); if (ts[0] == '\0') { sprintf(ts,"./lab7%02i.ppm", picnum); sprintf(png,"./lab7%02i.png", picnum); picnum++; } FILE *fpo = fopen(ts, "w"); if (fpo) { fprintf(fpo, "P6\n%i %i\n255\n", width, height); for (y=0; y<height; y++) { for (x=0; x<width; x++) { unsigned long pixel = XGetPixel(image, x, y); fputc(((pixel & 0x00ff0000)>>16), fpo); fputc(((pixel & 0x0000ff00)>> 8), fpo); fputc(((pixel & 0x000000ff) ), fpo); } } fclose(fpo); } XFree(image); char str[450]; sprintf(str, "convert %s %s", ts, png); system(str); unlink(ts); } //=======================================================================

Ray tracer on a business card

1. vi card.cpp
2. Copy and paste the code below.
3. g++ card.cpp -lm -o card
4. ./card > image.ppm
5. display image.ppm

---------------------------------------
#include <stdlib.h>   // card > aek.ppm
#include <stdio.h>
#include <math.h>
typedef int i;typedef float f;struct v{
f x,y,z;v operator+(v r){return v(x+r.x
,y+r.y,z+r.z);}v operator*(f r){return
v(x*r,y*r,z*r);}f operator%(v r){return
x*r.x+y*r.y+z*r.z;}v(){}v operator^(v r
){return v(y*r.z-z*r.y,z*r.x-x*r.z,x*r.
y-y*r.x);}v(f a,f b,f c){x=a;y=b;z=c;}v
operator!(){return*this*(1/sqrt(*this%*
this));}};i G[]={247570,280596,280600,
249748,18578,18577,231184,16,16};f R(){
return(f)rand()/RAND_MAX;}i T(v o,v d,f
&t,v&n){t=1e9;i m=0;f p=-o.z/d.z;if(.01
<p)t=p,n=v(0,0,1),m=1;for(i k=19;k--;)
for(i j=9;j--;)if(G[j]&1<<k){v p=o+v(-k
,0,-j-4);f b=p%d,c=p%p-1,q=b*b-c;if(q>0
){f s=-b-sqrt(q);if(s<t&&s>.01)t=s,n=!(
p+d*t),m=2;}}return m;}v S(v o,v d){f t
;v n;i m=T(o,d,t,n);if(!m)return v(.7,
.6,1)*pow(1-d.z,4);v h=o+d*t,l=!(v(9+R(
),9+R(),16)+h*-1),r=d+n*(n%d*-2);f b=l%
n;if(b<0||T(h,l,t,n))b=0;f p=pow(l%r*(b
>0),99);if(m&1){h=h*.2;return((i)(ceil(
h.x)+ceil(h.y))&1?v(3,1,1):v(3,3,3))*(b
*.2+.1);}return v(p,p,p)+S(h,r)*.5;}i
main(){printf("P6 512 512 255 ");v g=!v
(-6,-16,0),a=!(v(0,0,1)^g)*.002,b=!(g^a
)*.002,c=(a+b)*-256+g;for(i y=512;y--;)
for(i x=512;x--;){v p(13,13,13);for(i r
=64;r--;){v t=a*(R()-.5)*99+b*(R()-.5)*
99;p=S(v(17,16,8)+t,!(t*-1+(a*(R()+x)+b
*(y+R())+c)*16))*3.5+p;}printf("%c%c%c"
,(i)p.x,(i)p.y,(i)p.z);}}
---------------------------------------

Reference
https://fabiensanglard.net/revisiting_the_businesscard_raytracer/