X-Git-Url: http://git.mutantstargoat.com/user/nuclear/?a=blobdiff_plain;f=src%2F3dgfx.c;h=b1dea9f086bee0cd850fc6cd35bd933f2017f342;hb=HEAD;hp=5cf3c42645df36c9364daa8bbbbce4fba757ae91;hpb=d1a2007e191d68b104e436d1817eafcc99ae21dd;p=dosdemo diff --git a/src/3dgfx.c b/src/3dgfx.c deleted file mode 100644 index 5cf3c42..0000000 --- a/src/3dgfx.c +++ /dev/null @@ -1,321 +0,0 @@ -#include -#include -#include -#include "3dgfx.h" -#include "polyfill.h" - -#define STACK_SIZE 8 -typedef float g3d_matrix[16]; - -struct g3d_state { - unsigned int opt; - - g3d_matrix mat[G3D_NUM_MATRICES][STACK_SIZE]; - int mtop[G3D_NUM_MATRICES]; - - int width, height; - void *pixels; -}; - -static void xform4_vec3(const float *mat, float *vec); -static void xform3_vec3(const float *mat, float *vec); -static void proc_vertex(struct pvertex *res, const struct g3d_vertex *vert, int space); - -static struct g3d_state st; -static const float idmat[] = { - 1, 0, 0, 0, - 0, 1, 0, 0, - 0, 0, 1, 0, - 0, 0, 0, 1 -}; - -void g3d_init(void) -{ - int i; - - memset(&st, 0, sizeof st); - - for(i=0; i= G3D_NUM_MATRICES) { - fprintf(stderr, "g3d_push_matrix overflow\n"); - return; - } - memcpy(st.mat[which][top + 1], st.mat[which][top], 16 * sizeof(float)); - st.mtop[which] = top + 1;; -} - -void g3d_pop_matrix(int which) -{ - if(st.mtop[which] <= 0) { - fprintf(stderr, "g3d_pop_matrix underflow\n"); - return; - } - --st.mtop[which]; -} - -void g3d_translate(int which, float x, float y, float z) -{ - float m[] = {1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1}; - m[12] = x; - m[13] = y; - m[14] = z; - g3d_mult_matrix(which, m); -} - -void g3d_rotate(int which, float deg, float x, float y, float z) -{ - float m[] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}; - - float angle = M_PI * deg / 180.0f; - float sina = sin(angle); - float cosa = cos(angle); - float one_minus_cosa = 1.0f - cosa; - float nxsq = x * x; - float nysq = y * y; - float nzsq = z * z; - - m[0] = nxsq + (1.0f - nxsq) * cosa; - m[4] = x * y * one_minus_cosa - z * sina; - m[8] = x * z * one_minus_cosa + y * sina; - m[1] = x * y * one_minus_cosa + z * sina; - m[5] = nysq + (1.0 - nysq) * cosa; - m[9] = y * z * one_minus_cosa - x * sina; - m[2] = x * z * one_minus_cosa - y * sina; - m[6] = y * z * one_minus_cosa + x * sina; - m[10] = nzsq + (1.0 - nzsq) * cosa; - - g3d_mult_matrix(which, m); -} - -void g3d_scale(int which, float x, float y, float z) -{ - float m[] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}; - m[0] = x; - m[5] = y; - m[10] = z; - g3d_mult_matrix(which, m); -} - -void g3d_ortho(int which, float left, float right, float bottom, float top, float znear, float zfar) -{ - float m[] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}; - - float dx = right - left; - float dy = top - bottom; - float dz = zfar - znear; - - m[0] = 2.0 / dx; - m[5] = 2.0 / dy; - m[10] = -2.0 / dz; - m[12] = -(right + left) / dx; - m[13] = -(top + bottom) / dy; - m[14] = -(zfar + znear) / dz; - - g3d_mult_matrix(which, m); -} - -void g3d_frustum(int which, float left, float right, float bottom, float top, float nr, float fr) -{ - float m[] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}; - - float dx = right - left; - float dy = top - bottom; - float dz = fr - nr; - - float a = (right + left) / dx; - float b = (top + bottom) / dy; - float c = -(fr + nr) / dz; - float d = -2.0 * fr * nr / dz; - - m[0] = 2.0 * nr / dx; - m[5] = 2.0 * nr / dy; - m[8] = a; - m[9] = b; - m[10] = c; - m[11] = -1.0f; - m[14] = d; - - g3d_mult_matrix(which, m); -} - -void g3d_perspective(int which, float vfov_deg, float aspect, float znear, float zfar) -{ - float m[] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}; - - float vfov = M_PI * vfov_deg / 180.0f; - float s = 1.0f / tan(vfov * 0.5f); - float range = znear - zfar; - - m[0] = s / aspect; - m[5] = s; - m[10] = (znear + zfar) / range; - m[11] = -1.0f; - m[14] = 2.0f * znear * zfar / range; - - g3d_mult_matrix(which, m); -} - -void g3d_draw(int prim, int space, const struct g3d_vertex *varr, int varr_size) -{ - int i; - int vnum = prim; /* primitive vertex counts correspond to the enum values */ - - while(varr_size >= vnum) { - struct pvertex pv[4]; - - for(i=0; ix, vert->y, vert->z, 1.0f); - VEC3(norm, vert->nx, vert->ny, vert->nz); - - switch(space) { - case G3D_LOCAL_SPACE: - memcpy(norm_mat, st.mat[G3D_MODELVIEW][mvtop], 16 * sizeof(float)); - norm_mat[12] = norm_mat[13] = norm_mat[14] = 0.0f; - - xform4_vec3(pos, st.mat[G3D_MODELVIEW][mvtop]); - xform3_vec3(norm, norm_mat); - - case G3D_VIEW_SPACE: - if(st.opt & G3D_LIGHTING) { - /* TODO lighting */ - color[0] = vert->r; - color[1] = vert->g; - color[2] = vert->b; - } - xform4_vec3(pos, st.mat[G3D_PROJECTION][ptop]); - - case G3D_CLIP_SPACE: - /* TODO clipping */ - if(pos[3] != 0.0f) { - pos[0] /= pos[3]; - pos[1] /= pos[3]; - pos[2] /= pos[3]; - } - - case G3D_SCREEN_SPACE: - pos[0] = (pos[0] * 0.5 + 0.5) * (float)st.width; - pos[1] = (0.5 - pos[0] * 0.5) * (float)st.height; - - case G3D_PIXEL_SPACE: - break; - } - - /* convert pos to 24.8 fixed point */ - res->x = (int32_t)(pos[0] * 256.0f); - res->y = (int32_t)(pos[1] * 256.0f); - - /* convert tex coords to 16.16 fixed point */ - res->u = (int32_t)(vert->u * 65536.0f); - res->v = (int32_t)(vert->v * 65536.0f); - - /* pass color through as is */ - res->r = color[0]; - res->g = color[1]; - res->b = color[2]; -}