diff --git a/Makefile b/Makefile index 993fd3b..d7e8e49 100644 --- a/Makefile +++ b/Makefile @@ -21,7 +21,7 @@ $(BUILDD)/%.o: %.c %.h # Rule to build any sample. We ALWAYS need math so... link it %.exe: %.o $(HALOOLIB) - $(CC) $(CFLAGS) $< $(HALOOLIB) -o $@ -lm + $(CC) $(CFLAGS) $< $(HALOOLIB) -o $@ -lm -lSDL # Rule to clean the build files clean: diff --git a/camera.h b/camera.h new file mode 100644 index 0000000..9d225d2 --- /dev/null +++ b/camera.h @@ -0,0 +1,31 @@ +#ifndef __3DTOYS_CAMERA +#define __3DTOYS_CAMERA +#include "haloo3d/haloo3d.h" + +typedef struct { + mfloat_t xofs; + mfloat_t yofs; + mfloat_t zofs; + mfloat_t yaw; + mfloat_t pitch; +} camset; + +static inline int readcam(camset *set, int max, char *filename) { + FILE *f = fopen(filename, "r"); + if (f == NULL) { + dieerr("Can't open %s for reading cam\n", filename); + } + int num = 0; + while (5 == fscanf(f, "%f %f %f %f %f", &set[num].xofs, &set[num].yofs, + &set[num].zofs, &set[num].yaw, &set[num].pitch)) { + num++; + if (num >= max) { + eprintf("Camera file too big! Ignoring rest\n"); + break; + } + } + fclose(f); + printf("Read %d camlines from %s\n", num, filename); + return num; +} +#endif diff --git a/scene.c b/scene.c index 9e829ff..43187c0 100644 --- a/scene.c +++ b/scene.c @@ -4,6 +4,11 @@ #include "haloo3d/haloo3dex_obj.h" #include "haloo3d/haloo3dex_print.h" +#include "unigi/unigi.headers/src/main.h" +#include "unigi/unigi.platform.sdl1/src/main.c" +// #include "unigi/unigi.ext/src/main.c" + +#include "camera.h" #include "resources/flower.h" #include @@ -45,44 +50,14 @@ sum = thistime; \ sum = AVGWEIGHT * sum + (1 - AVGWEIGHT) * thistime; -typedef struct { - mfloat_t xofs; - mfloat_t yofs; - mfloat_t zofs; - mfloat_t yaw; - mfloat_t pitch; -} camset; - uint16_t redflower[64] = H3D_FLOWER(0xFE55, 0xF6C4, 0xFFE0, 0xFD44, 0xF492); -int readcam(camset *set, int max, char *filename) { - FILE *f = fopen(filename, "r"); - if (f == NULL) { - dieerr("Can't open %s for reading cam\n", filename); - } - int num = 0; - while (5 == fscanf(f, "%f %f %f %f %f", &set[num].xofs, &set[num].yofs, - &set[num].zofs, &set[num].yaw, &set[num].pitch)) { - // char linebuf[1024]; - // fgets(linebuf, sizeof(linebuf), f)) { - num++; - if (num >= max) { - eprintf("Camera file too big! Ignoring rest\n"); - break; - } - } - fclose(f); - printf("Read %d camlines from %s\n", num, filename); - return num; -} - int main(int argc, char **argv) { if (argc != 4) { eprintf("WARN: THIS PROGRAM GENERATES A LOT OF FILES!\n"); dieerr("You must pass in the following:\n- obj file .obj\n- texture file " ".ppm\n- camera file (xofs yofs zofs yawdeg pitchdeg)\n"); - //- obj xofs\n- obj yofs\n- obj zofs\n- obj rotation deg\n"); } // Load the junk + generate stuff @@ -97,7 +72,6 @@ int main(int argc, char **argv) { haloo3d_gen_sloped(models + 2, PLANESIZE, 1.0, 1.25); haloo3d_fb_init_tex(textures + 3, 8, 8); memcpy(textures[3].buffer, redflower, sizeof(uint16_t) * 64); - // memset(textures[3].buffer, 0xFF, sizeof(uint16_t) * 64); haloo3d_gen_crossquad(models + 3, textures + 3); camset cams[MAXCAM]; @@ -141,9 +115,6 @@ int main(int argc, char **argv) { objects[0].lighting = &light; objects[2].lighting = &light; #endif - // objects[0].pos.z = 0; - // objects[0].pos.y = -10; - // vec3(objects[2].pos.v, 0.5, 0.8, 0.5); objects[0].pos.y = 1; objects[1].scale = SKYSCALE; @@ -151,12 +122,18 @@ int main(int argc, char **argv) { haloo3d_fb fb; haloo3d_fb_init(&fb, WIDTH, HEIGHT); + unigi_type_event event; + unigi_type_resolution res; + res.width = WIDTH; + res.height = HEIGHT; + res.depth = 32; // Not actually 32 bit + // Printing to screen needs tracking haloo3d_print_tracker t; char printbuf[8192]; haloo3d_print_initdefault(&t, printbuf, sizeof(printbuf)); t.fb = &fb; - t.logprints = 1; + // t.logprints = 1; // Storage stuff mfloat_t matrix3d[MAT4_SIZE], matrixcam[MAT4_SIZE], matrixscreen[MAT4_SIZE], @@ -166,26 +143,30 @@ int main(int argc, char **argv) { haloo3d_facef face, baseface; struct vec4 *vert_precalc; mallocordie(vert_precalc, sizeof(struct vec4) * H3D_OBJ_MAXVERTICES); - char fname[1024]; - // float sumverts = 0, sumdraw = 0, sumframe = 0, suminit = 0, sumclip = 0; - // clock_t begin, end, vertend, drawend, initend, clipend, tempstart, tempend; clock_t begin, end; float sumframe = 0; - // float sumframe = 0; - // clock_t begin, end; int totaldrawn = 0; eprintf("Scene has %d tris, %d verts\n", totalfaces, totalverts); + // Init unigi system + sprintf(printbuf, "scene.exe - %s %s %s", argv[1], argv[2], argv[3]); + unigi_graphics_init(); + unigi_window_create(res, printbuf); + // ----------------------------------- // Actual rendering // ----------------------------------- - for (int cami = 0; cami < numcams; cami++) { + int cami = 0; + + while (1) { + unigi_event_get(&event); + if (event.type == unigi_enum_event_input_keyboard) { + exit(0); + } + begin = clock(); - // vertend = begin; - // drawend = begin; - // clipend = begin; totaldrawn = 0; haloo3d_print_refresh(&t); @@ -197,36 +178,26 @@ int main(int argc, char **argv) { // REMEMBER TO CLEAR DEPTH BUFFER haloo3d_fb_cleardepth(&fb, WBUFCLEAR); - // memset(fb.buffer, 0xFF, sizeof(uint16_t) * fb.width * fb.height); // Screen matrix calc. We multiply the modelview matrix with this later haloo3d_camera_calclook(&camera, matrixcam); mat4_inverse(matrixcam, matrixcam); mat4_multiply(matrixscreen, perspective, matrixcam); - // initend = clock(); - // Iterate over objects for (int i = 0; i < NUMINSTANCES; i++) { - // tempstart = clock(); // Setup final model matrix and the precalced vertices vec3_add(tmp1.v, objects[i].pos.v, objects[i].lookvec.v); haloo3d_my_lookat(matrixmodel, objects[i].pos.v, tmp1.v, camera.up.v); haloo3d_mat4_scale(matrixmodel, objects[i].scale); mat4_multiply(matrix3d, matrixscreen, matrixmodel); haloo3d_precalc_verts(objects[i].model, matrix3d, vert_precalc); - // tempend = clock(); - // vertend += (tempend - tempstart); // Iterate over object faces for (int fi = 0; fi < objects[i].model->numfaces; fi++) { // Copy face values out of precalc array and clip them haloo3d_make_facef(objects[i].model->faces[fi], vert_precalc, objects[i].model->vtexture, face); - // tempstart = clock(); int tris = haloo3d_facef_clip(face, outfaces); - // tempend = clock(); - // clipend += (tempend - tempstart); - // tempstart = tempend; for (int ti = 0; ti < tris; ti++) { int backface = !haloo3d_facef_finalize(outfaces[ti]); if (objects[i].cullbackface && backface) { @@ -244,32 +215,22 @@ int main(int argc, char **argv) { haloo3d_facef_viewport_into(outfaces[ti], WIDTH, HEIGHT); TRIFUNC(&fb, objects[i].texture, intensity, outfaces[ti]); } - // tempend = clock(); - // drawend += (tempend - tempstart); } } end = clock(); - // CALCTIME(thisinittime, begin, initend, suminit); - // CALCTIME(thisverttime, begin, vertend, sumverts); - // CALCTIME(thiscliptime, begin, clipend, sumclip); - // CALCTIME(thisdrawtime, begin, drawend, sumdraw); CALCTIME(thisframetime, begin, end, sumframe); - // haloo3d_print(&t, - // "Init work: %.2f (%.2f)\nVert calc: %.2f (%.2f)\n Clip: - // " - // "%.2f (%.2f)\n Draw: %.2f" - // " (%.2f)\n Frame: %.2f (%.2f)\nTris: %d / %d\nVerts: - // %d\n", thisinittime, suminit, thisverttime, sumverts, - // thiscliptime, sumclip, thisdrawtime, sumdraw, - // thisframetime, sumframe, totaldrawn, totalfaces, - // totalverts); haloo3d_print(&t, "Frame: %.2f (%.2f)\nTris: %d / %d\nVerts: %d\n", thisframetime, sumframe, totaldrawn, totalfaces, totalverts); - sprintf(fname, "scene_%04d.ppm", cami); - haloo3d_img_writeppmfile(&fb, fname); + // sprintf(fname, "scene_%04d.ppm", cami); + // haloo3d_img_writeppmfile(&fb, fname); + unigi_graphics_blit(0, (unigi_type_color *)fb.buffer, + res.width * res.height); + unigi_graphics_flush(); + + cami = (cami + 1) % numcams; } for (int i = 0; i < NUMOBJECTS; i++) {