3dtoys/commonobj.c

138 lines
4.7 KiB
C

#include "haloo3d/haloo3d.h"
#include "haloo3d/haloo3dex_gen.h"
#include "haloo3d/haloo3dex_obj.h"
#ifndef TREECOL
#define TREECOL 0xF070
#endif
#ifndef TREETRUNKCOL
#define TREETRUNKCOL 0xF950
#endif
#ifndef TREEBOTTOM
#define TREEBOTTOM -0.333
#endif
#ifndef TREETOPWIDTH
#define TREETOPWIDTH 1.0
#endif
#ifndef TREETRUNKWIDTH
#define TREETRUNKWIDTH 0.2
#endif
#define TOWERTHICK 0.65
#define COMVERT(m, _x, _y, _z) \
haloo3d_obj_addvertex(m, (struct vec4){.x = _x, .y = _y, .z = _z, .w = 1})
#define COMCOL(m, col) haloo3d_obj_addvtexture(m, haloo3d_gen_paletteuv(col))
// Fill a face with a solid color given by the tex
void fastface(haloo3d_facei face, uint16_t tex, uint16_t v0, uint16_t v1,
uint16_t v2) {
face[0].texi = tex;
face[1].texi = tex;
face[2].texi = tex;
face[0].posi = v0;
face[1].posi = v1;
face[2].posi = v2;
}
static inline uint16_t fastface2(haloo3d_obj *model, uint16_t tex, uint16_t v0,
uint16_t v1, uint16_t v2) {
haloo3d_facei face;
fastface(face, tex, v0, v1, v2);
return haloo3d_obj_addface(model, face);
}
// Generate a basic tree model
void gen_tree_model(haloo3d_obj *tree) {
haloo3d_obj_resetfixed(tree, 12, 24);
int treeuv = COMCOL(tree, TREECOL);
int trunkuv = COMCOL(tree, TREETRUNKCOL);
uint16_t top = COMVERT(tree, 0, 1, 0);
uint16_t tree1 = COMVERT(tree, -TREETOPWIDTH, TREEBOTTOM, -TREETOPWIDTH);
uint16_t tree2 = COMVERT(tree, TREETOPWIDTH, TREEBOTTOM, -TREETOPWIDTH);
uint16_t tree3 = COMVERT(tree, 0, TREEBOTTOM, TREETOPWIDTH);
uint16_t trunk1 = COMVERT(tree, -TREETRUNKWIDTH, -1, -TREETRUNKWIDTH);
uint16_t trunk2 = COMVERT(tree, TREETRUNKWIDTH, -1, -TREETRUNKWIDTH);
uint16_t trunk3 = COMVERT(tree, 0, -1, TREETRUNKWIDTH);
fastface2(tree, treeuv, top, tree1, tree3);
fastface2(tree, treeuv, top, tree3, tree2);
fastface2(tree, treeuv, top, tree2, tree1);
fastface2(tree, trunkuv, top, trunk1, trunk3);
fastface2(tree, trunkuv, top, trunk3, trunk2);
fastface2(tree, trunkuv, top, trunk2, trunk1);
haloo3d_obj_shrinktofit(tree);
}
void gen_circle_model(haloo3d_obj *model, uint16_t color, int vertices) {
haloo3d_obj_resetfixed(model, vertices, vertices + 1);
int circleuv = COMCOL(model, color);
// Add the center
uint16_t middle = COMVERT(model, 0, 0, 0);
// Go in a circle of course
float angle = 0;
float angle_inc = MPI * 2 / vertices;
uint16_t lastv = 0;
uint16_t firstv = 0;
for (int i = 0; i < vertices; i++) {
uint16_t thisv = COMVERT(model, cos(angle), sin(angle), 0);
if (i == 0) {
firstv = thisv;
} else {
fastface2(model, circleuv, middle, lastv, thisv);
}
angle += angle_inc;
lastv = thisv;
}
fastface2(model, circleuv, middle, lastv, firstv);
}
void gen_flower_model(haloo3d_obj *model, uint16_t color) {
haloo3d_obj_resetfixed(model, 2, 5);
uint16_t vs[5];
uint16_t col = COMCOL(model, color);
vs[0] = COMVERT(model, 0, -1, 0); // The shared middle
vs[1] = COMVERT(model, 1, 1, 0);
vs[2] = COMVERT(model, -1, 1, 0);
vs[3] = COMVERT(model, 0, 1, 1);
vs[4] = COMVERT(model, 0, 1, -1);
fastface2(model, col, vs[0], vs[1], vs[2]);
fastface2(model, col, vs[0], vs[3], vs[4]);
}
void gen_horibox(haloo3d_obj *model, uint16_t col, mfloat_t wid, mfloat_t bot,
mfloat_t hei) {
uint16_t box[8];
box[0] = COMVERT(model, -wid, bot, wid);
box[1] = COMVERT(model, -wid, bot + hei, wid);
box[2] = COMVERT(model, wid, bot, wid);
box[3] = COMVERT(model, wid, bot + hei, wid);
box[4] = COMVERT(model, wid, bot, -wid);
box[5] = COMVERT(model, wid, bot + hei, -wid);
box[6] = COMVERT(model, -wid, bot, -wid);
box[7] = COMVERT(model, -wid, bot + hei, -wid);
for (int i = 0; i < 8; i += 2) {
fastface2(model, col, box[i], box[(i + 3) & 7], box[(i + 1) & 7]);
fastface2(model, col, box[i], box[(i + 2) & 7], box[(i + 3) & 7]);
}
}
void gen_tower_model(haloo3d_obj *model, uint16_t color) {
haloo3d_obj_resetfixed(model, 100, 100);
uint16_t col = COMCOL(model, color);
uint16_t top = COMVERT(model, 0, 1, 0);
fastface2(model, col, top, COMVERT(model, -1, -1, 1),
COMVERT(model, -TOWERTHICK, -1, TOWERTHICK));
fastface2(model, col, top, COMVERT(model, 1, -1, 1),
COMVERT(model, TOWERTHICK, -1, TOWERTHICK));
fastface2(model, col, top, COMVERT(model, -1, -1, -1),
COMVERT(model, -TOWERTHICK, -1, -TOWERTHICK));
fastface2(model, col, top, COMVERT(model, 1, -1, -1),
COMVERT(model, TOWERTHICK, -1, -TOWERTHICK));
gen_horibox(model, col, 0.2, 0.8, 0.03);
gen_horibox(model, col, 0.8, -0.4, 0.05);
for (mfloat_t f = 0.7; f >= -0.4; f -= 0.2) {
gen_horibox(model, col, (1 - f) * 0.5, f, 0.01);
}
haloo3d_obj_shrinktofit(model);
}