int vnum = prim; /* primitive vertex counts correspond to enum values */
int mvtop = st->mtop[G3D_MODELVIEW];
int ptop = st->mtop[G3D_PROJECTION];
+ struct g3d_vertex *tmpv;
+
+ tmpv = alloca(prim * 6 * sizeof *tmpv);
/* calc the normal matrix */
memcpy(st->norm_mat, st->mat[G3D_MODELVIEW][mvtop], 16 * sizeof(float));
/* clipping */
for(i=0; i<6; i++) {
- struct g3d_vertex tmpv[16];
memcpy(tmpv, v, vnum * sizeof *v);
if(clip_frustum(v, &vnum, tmpv, vnum, i) < 0) {
void g3d_frustum(float left, float right, float bottom, float top, float znear, float zfar);
void g3d_perspective(float vfov, float aspect, float znear, float zfar);
+/* returns pointer to the *internal* matrix, and if argument m is not null,
+ * also copies the internal matrix there. */
const float *g3d_get_matrix(int which, float *m);
void g3d_light_pos(int idx, float x, float y, float z);
static struct bspnode *new_node(struct g3d_vertex *v, int vnum);
static struct bspnode *add_poly_tree(struct bspnode *n, struct g3d_vertex *v, int vnum);
+static void draw_bsp_tree(struct bspnode *n, const vec3_t *vdir);
static void save_bsp_tree(struct bspnode *n, FILE *fp);
static struct bspnode *load_bsp_tree(FILE *fp);
void draw_bsp(struct bsptree *bsp, float view_x, float view_y, float view_z)
{
+ vec3_t vdir;
+ vdir.x = view_x;
+ vdir.y = view_y;
+ vdir.z = view_z;
+ draw_bsp_tree(bsp->root, &vdir);
}
static int count_nodes(struct bspnode *n)
return n;
}
+static void draw_bsp_tree(struct bspnode *n, const vec3_t *vdir)
+{
+ float dot;
+
+ if(!n) return;
+
+ dot = vdir->x * n->plane.nx + vdir->y * n->plane.ny + vdir->z * n->plane.nz;
+ if(dot >= 0.0f) {
+ draw_bsp_tree(n->front, vdir);
+ g3d_draw_indexed(n->vcount, n->verts, n->vcount, 0, 0);
+ draw_bsp_tree(n->back, vdir);
+ } else {
+ draw_bsp_tree(n->back, vdir);
+ g3d_draw_indexed(n->vcount, n->verts, n->vcount, 0, 0);
+ draw_bsp_tree(n->front, vdir);
+ }
+}
+
static void save_bsp_tree(struct bspnode *n, FILE *fp)
{
/* TODO */
int i;
struct g3d_mesh *m;
struct g3d_mesh tmpmesh;
- float xform[16];
static float rotface[][4] = {
{0, 0, 1, 0},
{90, 0, 1, 0},
g3d_load_identity();
g3d_rotate(rotface[i][0], rotface[i][1], rotface[i][2], rotface[i][3]);
g3d_translate(0, 0, sz / 2.0f);
- g3d_get_matrix(G3D_MODELVIEW, xform);
- apply_mesh_xform(m, xform);
+ apply_mesh_xform(m, g3d_get_matrix(G3D_MODELVIEW, 0));
if(i > 0) {
if(append_mesh(mesh, m) == -1) {
return -1;
static void draw(void)
{
+ float vdir[3];
+ float mat[16];
+
update();
memset(fb_pixels, 0, fb_width * fb_height * 2);
g3d_rotate(cam_theta, 0, 1, 0);
}
+ /* calc world-space view direction */
+ g3d_get_matrix(G3D_MODELVIEW, mat);
+ /* transform (0, 0, -1) with transpose(mat3x3) */
+ vdir[0] = -mat[2];
+ vdir[1] = -mat[6];
+ vdir[2] = -mat[10];
+
+
g3d_light_pos(0, -10, 10, 20);
zsort_mesh(&torus);
g3d_mtl_diffuse(0.4, 0.7, 1.0);
g3d_set_texture(tex.width, tex.height, tex.pixels);
- draw_mesh(&torus);
- /*draw_bsp(&torus_bsp);*/
+ /*draw_mesh(&torus);*/
+ draw_bsp(&torus_bsp, vdir[0], vdir[1], vdir[2]);
/*draw_mesh(&cube);*/
swap_buffers(fb_pixels);
return quat_mul(q, rq);
}
+static INLINE void mat4_transpose(float *mat)
+{
+ int i, j;
+
+ for(i=0; i<4; i++) {
+ for(j=0; j<i; j++) {
+ int rowidx = i * 4 + j;
+ int colidx = j * 4 + i;
+ float tmp = mat[rowidx];
+ mat[rowidx] = mat[colidx];
+ mat[colidx] = tmp;
+ }
+ }
+}
+
#endif /* VMATH_H_ */