#include #include "utils.h" #include "rayhdr.h" #define CAM_Z 2 asm(".string \"PRG\"\n"); int main(void) { int x, y, i; float a, b; //eye point float eye[] = {0, 0, 3}; //list of centers in (x, y, z) float ctr[] = {0, 0, 0, 1, .3, 0}; //list of radii float r[] = {1, .5}; //list of colors float clr[] = {12, 8}; //number of spheres float num = 2; //z-coordinate of the floor float floorz = -1; //coordinates of lamp float lamp[] = {1, 1, 1}; clearScreen(); for (x = 0; x < 320; x++) { for (y = 0; y < 240; y++) { a = (x - 160) / 80.0; b = (y - 120) / 80.0; float vd[] = {a - eye[0], b - eye[1], -1}; float t = 1000; int sphr_idx = -1; for (i = 0; i < num; i++) { float thisctr[] = {ctr[3 * i], ctr[3 * i + 1], ctr[3 * i + 2]}; float this_inter = raysphere(eye, vd, thisctr, r[i]); if (this_inter > 0 && this_inter < t) { t = this_inter; sphr_idx = i; } } float this_inter = rayxyplane(eye, vd, floorz); if (this_inter > 0 && this_inter < t) { t = this_inter; sphr_idx = -2; } float mlt = 1; if (sphr_idx < -2) goto black; float pt[] = {eye[0] + t * vd[0], eye[1] + t * vd[1], eye[2] + t * vd[2]}; float ldir[] = {pt[0] - lamp[0], pt[1] - lamp[1], pt[2] - lamp[2]}; int blocked = 0; for (i = 0; i < num; i++) { float thisctr[] = {ctr[3 * i], ctr[3 * i + 1], ctr[3 * i + 2]}; float this_inter = raysphere(lamp, ldir, thisctr, r[i]); if (this_inter > 0 && i != sphr_idx) { blocked = 1; break; } } if (blocked) mlt = 0.3; if (sphr_idx >= 0) { float thectr[] = {ctr[3 * sphr_idx], ctr[3 * sphr_idx + 1], ctr[3 * sphr_idx + 2]}; float nrml[] = {thectr[0] - pt[0], thectr[1] - pt[1], thectr[2] - pt[2]}; float cos = angle(nrml, ldir); if (cos < 0) { cos = 0; } setPixel(x, y, (int) (cos * clr[sphr_idx] * mlt)); } else if (sphr_idx == -2) { float color = 15; //checkers on floor if (pt[0] * pt[1] >= 0 && ((int) fabs(pt[0]) % 2 == 0 && (int) fabs(pt[1]) % 2 == 1 || (int) fabs(pt[0]) % 2 == 1 && (int) fabs(pt[1]) % 2 == 0)) color = 0; if (pt[0] * pt[1] < 0 && ((int) fabs(pt[0]) % 2 == 1 && (int) fabs(pt[1]) % 2 == 1 || (int) fabs(pt[0]) % 2 == 0 && (int) fabs(pt[1]) % 2 == 0)) color = 0; //end checkers float nrml[] = {0, 0, -1}; float cos = angle(nrml, ldir); if (cos < 0) { cos = 0; } setPixel(x, y, (int) (cos * color * mlt)); } else { black: setPixel(x, y, 0); } } } while(!isKeyPressed(KEY_NSPIRE_ESC)); return 0; }