Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- unsigned char globalsEndHere;
- #include "nstdlib.h"
- #include "olduino.h"
- #define SIMULATOR
- float abs(float x);
- float sqrt(float x);
- void raySphereIntersection(float posX, float posY, float posZ, float origX, float origY, float origZ, float dirX, float dirY, float dirZ, float radius, float* t0, float* t1);
- //const char brightnessSymbols[] = {176, 177, 178, 219};
- const char brightnessSymbols[] = {'.', '=', 'x', '#'};
- void main() {
- int i,j;
- char pRes;
- float rayX,rayY,rayZ,length,t0,t1,origX,origY,origZ,posX,posY,posZ;
- memcpy((void *)0xe000, (void *)0x6000, 0x1fff);
- t0 = t1 = 1.0;
- printf("Raytracing test 1802\r\n");
- printf("Pre-run test: Does abs function work?");
- rayX = 5.0;
- rayY = -7.0;
- if(abs(rayX) == 5.0 && abs(rayY) == 7.0) printf("\tYes\r\n");
- else {
- printf("\tNo\r\n");
- goto abort;
- }
- printf("Pre-run test: Does square-root function work?");
- rayX = 2.0;
- rayX = sqrt(rayX);
- if((int)(rayX * 1000.0) == 1414) printf("\tYes\r\n");
- else {
- printf("\tNo\r\n");
- goto abort;
- }
- printf("Pre-run test: Does ray-sphere-intersection function work?");
- raySphereIntersection(0.02, 0.02, 3.0, 0.01, 0.01, 0.01, -0.231, 0.033, 0.972, 1.0, &t0, &t1);
- if((long)(t0 * 1000.0) != 2200 || (long)(t1 * 1000.0) != 3607) {
- printf("\tNo\r\n");
- goto abort;
- }
- raySphereIntersection(0.02, 0.02, 3.0, 0.01, 0.01, 0.01, -0.231, -0.601, 0.765, 1.0, &t0, &t1);
- if(t0 == 10000.0 && t1 == 10000.0) printf("\tYes\r\n");
- else {
- printf("\tNo\r\n");
- goto abort;
- }
- printf("\r\nBEGIN RENDER\r\n");
- for(i = 0; i < 150; i++) putc('-');
- putc('\r');
- putc('\n');
- for(i = 0; i < 64; i++) {
- #ifndef SIMULATOR
- if(i % 2 == 0) asm(" seq\n");
- else asm(" req\n");
- #endif
- for(j = 0; j < 128; j++) {
- pRes = ' ';
- rayX = (float)i / 63.0 - 0.5;
- rayX *= 2.0;
- rayY = -(float)j / 127.0 - 0.5;
- rayY *= 2.0;
- rayZ = 1.0;
- origX = origY = origZ = 0.01;
- length = sqrt(rayX * rayX + rayY * rayY + rayZ * rayZ);
- rayX /= length;
- rayY /= length;
- rayZ /= length;
- raySphereIntersection(0.02, 0.02, 2.0, origX, origY, origZ, rayX, rayY, rayZ, 1.0, &t0, &t1);
- if(t0 < 9000) {
- posX = (origX + t0 * rayX) - 0.2;
- posY = (origY + t0 * rayY) - 0.2;
- posZ = (origZ + t0 * rayZ) - 3.0;
- length = sqrt(posX * posX + posY * posY + posZ * posZ);
- posX /= length;
- posY /= length;
- posZ /= length;
- length = -posX * posX + (10000.0 - posY) * posY + (-3200.0 - posZ) * posZ;
- length /= 1500.0;
- if(length >= 1.0) length = 0.995;
- if(length < 0) length = 0;
- pRes = brightnessSymbols[(char)(length * 4.0)];
- }
- putc(pRes);
- }
- putc('\r');
- putc('\n');
- }
- for(i = 0; i < 150; i++) putc('-');
- putc('\r');
- putc('\n');
- loop_forever:
- while(1){
- #ifndef SIMULATOR
- asm(" seq\n");
- for(i = 0; i < 16384; i++) {}
- asm(" req\n");
- for(i = 0; i < 16384; i++) {}
- #endif
- asm(" nop\n");
- }
- abort:
- printf("Abort!\r\n");
- goto loop_forever;
- }
- float abs(float x) {
- return x < 0 ? 0.0 - x : x;
- }
- float sqrt(float x) {
- float x2,y = 0;
- long i;
- if(x == 0.0) return 0.0;
- x2 = x * 0.5f;
- i = *(long *)&x;
- i = 0x5f3759dfL - (i >> 1);
- y = *(float *)&i;
- y *= 1.5f - (x2 * y * y);
- y *= 1.5f - (x2 * y * y);
- return 1.0 / y;
- }
- void raySphereIntersection(float posX, float posY, float posZ, float origX, float origY, float origZ, float dirX, float dirY, float dirZ, float radius, float* t0, float* t1) {
- float Lx,Ly,Lz,tca,L_2,d,thc;
- if(dirX == 0) dirX = 1e-3;
- if(dirY == 0) dirY = 1e-3;
- if(dirZ == 0) dirZ = 1e-3;
- *t0 = 10000.0;
- *t1 = 10000.0;
- Lx = posX - origX;
- Ly = posY - origY;
- Lz = posZ - origZ;
- tca = dirX * Lx;
- tca += dirY * Ly;
- tca += dirZ * Lz;
- L_2 = Lx * Lx + Ly * Ly + Lz * Lz;
- if(tca <= 0 && sqrt(L_2) >= radius) return;
- d = L_2 - tca * tca;
- if(sqrt(d) >= radius) return;
- thc = radius * radius - d;
- thc = sqrt(thc);
- *t0 = tca - thc;
- *t1 = tca + thc;
- }
- unsigned char globalsStartHere;
- #include "nstdlib.c"
- #include "olduino.c"
RAW Paste Data