瀏覽代碼

restructuring

Tomas Dougan 1 年之前
父節點
當前提交
ef3ab255b5
共有 11 個文件被更改,包括 298 次插入468 次删除
  1. 7 2
      clip.c
  2. 0 29
      clip.h
  3. 37 19
      mat.c
  4. 0 91
      mat.h
  5. 6 1
      pipe.c
  6. 11 3
      rast.c
  7. 0 43
      rast.h
  8. 25 29
      shad.c
  9. 26 53
      sr.c
  10. 56 198
      sr.h
  11. 130 0
      sr_priv.h

+ 7 - 2
clip.c

@@ -26,6 +26,7 @@
  ********/
 
 /* swaps what the pointers point to */
+
 static void
 swap(float** a, float** b)
 {
@@ -39,6 +40,7 @@ swap(float** a, float** b)
  ********/
 
 /* interpolates a point some length up an edge */
+
 static void
 lerp(float* dest, float* from, float* to, float a, int n_attr)
 {
@@ -55,6 +57,7 @@ lerp(float* dest, float* from, float* to, float a, int n_attr)
  * a variant of cohen-sutherland for one plane
  * as determined by an axis and a sign to indicate direction
  */
+
 static void
 clip_routine(float* dest, float* src, 
              int* n_pts, int n_attr, 
@@ -107,7 +110,8 @@ clip_routine(float* dest, float* src,
  * matches a one-hot clip flag to its clip routine
  * updates source points to the clipped ones in place
  */
-extern void
+
+void
 clip_poly(float* src, int* n_pts, 
           int n_attr, uint8_t clip_flags)
 {
@@ -150,7 +154,8 @@ clip_poly(float* src, int* n_pts,
  *************/
 
 /* assigns proper clip flags to a point */
-extern void
+
+void
 clip_test(float* pt, uint8_t* flags)
 {
     uint8_t left = (pt[3] + pt[0] < 0) << 0;

+ 0 - 29
clip.h

@@ -1,29 +0,0 @@
-
-
-#ifndef SR_CLIP_H
-#define SR_CLIP_H
-
-/**
- * sr_clip.h
- * --------
- * defines functions to clip a polygon of vectors (x, y, z, w) in
- * homogeneous space
- * 
- */
-
-#include "sr.h"
-
-/*********************************************************************
- *                                                                   *
- *                        public declarations                        *
- *                                                                   *
- *********************************************************************/
-
-void
-clip_poly(float* src, int* n_pts, 
-          int n_attr, uint8_t clip_flags);
-
-void
-clip_test(float* pt, uint8_t* flags);
-
-#endif /* SR_CLIP_H */

+ 37 - 19
mat.c

@@ -21,7 +21,8 @@
  **********/
 
 /* multiplies two 4x4 matrices, 'a' and 'b', stores the result in 'a' */
-extern void 
+
+void 
 matmul(struct mat4* a, struct mat4* b)
 {
     struct mat4 tmp;
@@ -49,7 +50,8 @@ matmul(struct mat4* a, struct mat4* b)
  **********/
 
 /* inverts a 4x4 matrix, returns 0 if non invertible */
-extern int
+
+int
 invert(struct mat4* a)
 {
     struct mat4 tmp;
@@ -201,7 +203,8 @@ invert(struct mat4* a)
  *************/
 
 /* transpose of a 4x4 matrix */
-extern void
+
+void
 transpose(struct mat4* a) {
     struct mat4 tmp;
     tmp.e00 = a->e00;
@@ -227,8 +230,9 @@ transpose(struct mat4* a) {
  * upper_3x3 *
  *************/
 
-/* converts 4x4 matrix to its upper 3x3 matrix by filling 0s*/
-extern void
+/* converts 4x4 matrix to its upper 3x3 matrix by filling 0s */
+
+void
 upper_3x3(struct mat4* a) {
     a->e03 = 0;
     a->e13 = 0;
@@ -251,7 +255,8 @@ upper_3x3(struct mat4* a) {
  ***************/
 
 /* applys the matrix 'b' to vector 'c', stores result in vector 'a' */ 
-extern void
+
+void
 vec4_matmul(float* a, struct mat4* b, float* c)
 {
     a[0] = c[0] * b->e00 + c[1] * b->e01 + c[2] * b->e02 + c[3] * b->e03;
@@ -265,7 +270,8 @@ vec4_matmul(float* a, struct mat4* b, float* c)
  ************/
 
 /* multiplies vec4 'b' and 'c' componentwise, result in 'a' */
-extern void
+
+void
 vec4_mul(float* a, float* b, float* c)
 {
     a[0] = b[0] * c[0];
@@ -279,7 +285,8 @@ vec4_mul(float* a, float* b, float* c)
  ***********/
 
 /* adds vec4 'b' to 'c' and stores result in 'a' */
-extern void
+
+void
 vec4_add(float* a, float* b, float* c)
 {
     a[0] = b[0] + c[0];
@@ -293,7 +300,8 @@ vec4_add(float* a, float* b, float* c)
  **************/
 
 /* multiplies vec4 'b' by scalar 'c' and stores result in 'a' */
-extern void
+
+void
 vec4_scale(float* a, float* b, float c)
 {
     a[0] = b[0] * c;
@@ -307,7 +315,8 @@ vec4_scale(float* a, float* b, float c)
  *************/
 
 /* linear interpolation for vec4s, result in 'a' */
-extern void
+
+void
 lerp(float* a, float* b, float* c, float alpha)
 {
     a[0] = b[0] + (c[0] - b[0]) * alpha;
@@ -327,7 +336,8 @@ lerp(float* a, float* b, float* c, float alpha)
  ************/
 
 /* subtracts vec3 'c' from 'b' and stores result in 'a' */
-extern void
+
+void
 vec3_sub(float* a, float* b, float* c)
 {
     a[0] = b[0] - c[0];
@@ -340,7 +350,8 @@ vec3_sub(float* a, float* b, float* c)
  ***********/
 
 /* adds vec3 'b' to 'c' and stores result in 'a' */
-extern void
+
+void
 vec3_add(float* a, float* b, float* c)
 {
     a[0] = b[0] + c[0];
@@ -353,7 +364,8 @@ vec3_add(float* a, float* b, float* c)
  **************/
 
 /* multiplies vec3 'b' by scalar 'c' and stores result in 'a' */
-extern void
+
+void
 vec3_scale(float* a, float* b, float c)
 {
     a[0] = b[0] * c;
@@ -372,7 +384,8 @@ vec3_scale(float* a, float* b, float c)
  ***********/
 
 /* reflects a vec3 'l' across a normal 'n', result in 'r' */
-extern void
+
+void
 reflect(float* r, float* l, float* n)
 {
     float tmp[3];
@@ -385,7 +398,8 @@ reflect(float* r, float* l, float* n)
  *******/
 
 /* dot product of two vec3s */
-extern float
+
+float
 dot(float* a, float* b)
 {
     return a[0] * b[0] + a[1] * b[1] + a[2] * b[2];
@@ -396,7 +410,8 @@ dot(float* a, float* b)
  *********/
 
 /* applys the cross product 'b' x 'c', stores result in 'a' */
-extern void
+
+void
 cross(float* a, float* b, float* c)
 {
     a[0] = (b[1] * c[2]) - (b[2] * c[1]);
@@ -409,7 +424,8 @@ cross(float* a, float* b, float* c)
  *************/
 
 /* returns the magnitude of a vec3 */
-extern float
+
+float
 magnitude(float* a)
 {
     return sqrt(pow(a[0], 2) + pow(a[1], 2) + pow(a[2], 2));
@@ -420,7 +436,8 @@ magnitude(float* a)
  *************/
 
 /* normalizes vec3 in place */
-extern void
+
+void
 normalize(float* a)
 {
     float m = magnitude(a);
@@ -440,7 +457,8 @@ normalize(float* a)
  ***********/
 
 /* converts degrees to radians */
-extern float 
+
+float 
 radians(float deg) 
 {
     return deg * (M_PI / 180);

+ 0 - 91
mat.h

@@ -1,91 +0,0 @@
-
-#ifndef SR_MATH_H
-#define SR_MATH_H
-
-#include "sr.h"
-
-#include <string.h>
-#include <math.h>
-
-/**
- * sr_math.h
- * --------
- * provides an internal matrix representation (mat4)
- * and associated operations for the sr lib
- * 
- */
-
-/*********************************************************************
- *                                                                   *
- *                        4 by 4 matrix struct                       *
- *                                                                   *
- *********************************************************************/
-
-struct mat4 {
-    float e00, e01, e02, e03;
-    float e10, e11, e12, e13;
-    float e20, e21, e22, e23;
-    float e30, e31, e32, e33;
-};
-
-/*********************************************************************
- *                                                                   *
- *                        public declarations                        *
- *                                                                   *
- *********************************************************************/
-
-extern void 
-matmul(struct mat4* a, struct mat4* b);
-
-extern int
-invert(struct mat4* a);
-
-extern void
-transpose(struct mat4* a);
-
-extern void
-upper_3x3(struct mat4* a);
-
-extern void
-vec4_matmul(float* a, struct mat4* b, float* c);
-
-extern void
-vec4_mul(float* a, float* b, float* c);
-
-extern void
-vec4_add(float* a, float* b, float* c);
-
-extern void
-vec4_scale(float* a, float* b, float c);
-
-extern void
-lerp(float* a, float* b, float* c, float alpha);
-
-extern void
-vec3_sub(float* a, float* b, float* c);
-
-extern void
-vec3_add(float* a, float* b, float* c);
-
-extern void
-vec3_scale(float* a, float* b, float c);
-
-extern void
-reflect(float* r, float* n, float* v);
-
-extern float
-dot(float* a, float* b);
-
-extern void
-cross(float* a, float* b, float* c);
-
-extern float
-magnitude(float* a);
-
-extern void
-normalize(float* a);
-
-extern float
-radians(float deg);
-
-#endif   /* SR_MATH_H */

+ 6 - 1
pipe.c

@@ -25,6 +25,7 @@
  *****************/
 
 /* false for triangles against the winding order */
+
 static int
 winding_order(int winding, float* v0, float* v1, float* v2)
 {
@@ -40,6 +41,7 @@ winding_order(int winding, float* v0, float* v1, float* v2)
  *************/
 
 /* matches the correct drawing routine with the primitive type */
+
 static void 
 draw_prim(struct raster_context* rast, float* pts, 
           int n_pts, enum sr_primitive prim_type)
@@ -79,6 +81,7 @@ draw_prim(struct raster_context* rast, float* pts,
  **************/
 
 /* fills relevant traversal data about a primitive type */
+
 static void
 split_prim(enum sr_primitive prim_type, int* prim_size)
 {
@@ -104,6 +107,7 @@ split_prim(enum sr_primitive prim_type, int* prim_size)
  ****************/
 
 /* moves coordinates from clip space to screen space */
+
 static void
 screen_space(struct sr_framebuffer* fbuf, float* pt)
 {
@@ -134,7 +138,8 @@ screen_space(struct sr_framebuffer* fbuf, float* pt)
  * entry point of the sr pipeline, 
  * refines indexed vertex data to be sent to rasterizer
  */
-extern void
+
+void
 sr_render(struct sr_pipeline* pipe, int* indices, 
           int n_indices, enum sr_primitive prim_type)
 {

+ 11 - 3
rast.c

@@ -2,7 +2,8 @@
 #include <math.h>
 #include <string.h>
 #include <stdlib.h>
-#include "rast.h"
+
+#include "sr_priv.h"
 
 /**
  * sr_rast.c
@@ -22,6 +23,7 @@
  ***************/
 
 /* holds the steps sizes / data for barycentric weight for an edge */
+
 struct edge {
     float step_x;    /* steps to increment to get the det at new pt */
     float step_y;    
@@ -33,6 +35,7 @@ struct edge {
  ***************/
 
 /* holds the upper and lower bounds of the triangle */
+
 struct bbox {
     float min_x; 
     float min_y;
@@ -54,6 +57,7 @@ struct bbox {
  * determines whether an edge is top or left 
  * where the edge is directed, starting at v0 pointing to v1
  */
+
 static int 
 is_tl(float* v0, float* v1) 
 {
@@ -73,6 +77,7 @@ is_tl(float* v0, float* v1)
  *************/
 
 /* make an edge struct instance, return the initial determinant */
+
 static float 
 edge_init(struct edge* edge, int winding, 
           float* v0, float* v1, float* pt)
@@ -100,6 +105,7 @@ edge_init(struct edge* edge, int winding,
  *************/
 
 /* define a pixel-aligned bounding box for triangle rasterization */
+
 static void
 bbox_init(struct bbox* bbox, float* v0, float* v1, float* v2)
 {    
@@ -129,7 +135,8 @@ bbox_init(struct bbox* bbox, float* v0, float* v1, float* v2)
  ***********/
 
 /* render point to framebuffer */
-extern void 
+
+void 
 draw_pt(struct raster_context* rast, float* pt)
 {
     uint32_t color = 0; /* color dest */
@@ -147,7 +154,8 @@ draw_pt(struct raster_context* rast, float* pt)
  ***********/
 
 /* rasterize a triangle to framebuffer */
-extern void 
+
+void 
 draw_tr(struct raster_context* rast, float* v0, float* v1, float* v2)
 {   
     /* find bounding box */

+ 0 - 43
rast.h

@@ -1,43 +0,0 @@
-
-#ifndef SR_RASTER_H
-#define SR_RASTER_H
-
-#include "sr.h"
-
-/**
- * sr_rast.h
- * --------
- * rasterizes triangles, lines, and points
- * 
- */
-
-/*********************************************************************
- *                                                                   *
- *                     public data structures                        *
- *                                                                   *
- *********************************************************************/
-
-struct raster_context {
-    struct sr_framebuffer* fbuf;
-    void* uniform;
-    fs_f fs;
-    int n_attr;
-    int winding;
-};
-
-/*********************************************************************
- *                                                                   *
- *                      public declarations                          *
- *                                                                   *
- *********************************************************************/
-
-extern void 
-draw_pt(struct raster_context* rast, float* pt);
-
-extern void 
-draw_ln(struct raster_context* rast, float* v0, float* v1);
-
-extern void 
-draw_tr(struct raster_context* rast, float* v0, float* v1, float* v2);
-
-#endif /* SR_RASTER_H */

+ 25 - 29
shad.c

@@ -1,12 +1,10 @@
 
-
-#include "sr.h"
-#include "state.h"
-#include "mat.h"
-
 #include <stdio.h>
 #include <stdlib.h>
 
+#include "sr.h"
+#include "sr_priv.h"
+
 /**
  * sr_shaders.c
  * --------
@@ -157,25 +155,23 @@ falloff(float x, float inner, float outer) {
 static void
 phong(float* color, float* pos, float* uv, 
       float* normal, struct sr_uniform* uniform)
-{   
+{       
+    float fatt, intensity, dist;
+    float I[4], L[3], R[3], V[3], tmp[4];
+    float *Oa, *Od, *Os;
+    float n, ka, kd, ks;
+
     memset(color, 0, 4 * sizeof(float));
-    
-    float tmp[4];
-    float fatt = 1;
-    float intensity = 1;
-    float I[4];
-    float L[3];
-    float R[3];
-    float V[3];
-    float dist;
-
-    float* Oa = uniform->material->ambient;
-    float* Od = uniform->material->diffuse;
-    float* Os = uniform->material->specular;
-    float n = uniform->material->shininess;
-    float ka = uniform->ka;
-    float kd = uniform->kd;
-    float ks = uniform->ks;
+    fatt = 1;
+    intensity = 1;
+
+    Oa = uniform->material->ambient;
+    Od = uniform->material->diffuse;
+    Os = uniform->material->specular;
+    n = uniform->material->shininess;
+    ka = uniform->ka;
+    kd = uniform->kd;
+    ks = uniform->ks;
 
     /* ambient */
     vec4_scale(tmp, Oa, ka);
@@ -402,13 +398,13 @@ phong_fs(uint32_t* out, float* in, void* uniform)
  * color *
  *********/
 
-extern void
+void
 sr_bind_color_vs()
 {
     sr_bind_vs(color_vs, 7);
 }
 
-extern void
+void
 sr_bind_color_fs()
 {
     sr_bind_fs(color_fs);
@@ -418,13 +414,13 @@ sr_bind_color_fs()
  * texture *
  ***********/
 
-extern void
+void
 sr_bind_texture_vs()
 {
     sr_bind_vs(texture_vs, 6);
 }
 
-extern void
+void
 sr_bind_texture_fs()
 {
     sr_bind_fs(texture_fs);
@@ -434,13 +430,13 @@ sr_bind_texture_fs()
  * phong *
  *********/
 
-extern void
+void
 sr_bind_std_vs()
 {
     sr_bind_vs(std_vs, 12);
 }
 
-extern void
+void
 sr_bind_phong_fs()
 {
     sr_bind_fs(phong_fs);

+ 26 - 53
api.c → sr.c

@@ -253,71 +253,40 @@ sr_bind_texture(uint32_t* colors, int width, int height)
  *                                                                   *
  *********************************************************************/
 
-/***************
- * split_light *
- ***************/
-
-static int
-split_light(enum sr_light slot) {
-        switch(slot) {
-        case SR_LIGHT_1:
-            return 0;
-        case SR_LIGHT_2:
-            return 1;
-        case SR_LIGHT_3:
-            return 2;
-        case SR_LIGHT_4:
-            return 3;
-        case SR_LIGHT_5:
-            return 4;
-        case SR_LIGHT_6:
-            return 5;
-        case SR_LIGHT_7:
-            return 6;
-        case SR_LIGHT_8:
-            return 7;
-        default:
-            return -1;
-    }
-    
-}
-
 /************
  * sr_light *
  ************/
 
 /* binds a light to pipeline */
+
 extern void 
 sr_light(enum sr_light slot, enum sr_light_attr attr, float* data)
 {
-    /* split index */
-    int idx = split_light(slot);
-
     /* split attribute data */
     switch(attr) {
         case SR_POSITION:
-            memcpy(g_lights[idx].pos, data, 3 * sizeof(float));
+            memcpy(g_lights[slot].pos, data, 3 * sizeof(float));
             break;
         case SR_DIRECTION:
-            memcpy(g_lights[idx].dir, data, 3 * sizeof(float));
+            memcpy(g_lights[slot].dir, data, 3 * sizeof(float));
             break;
         case SR_COLOR:
-            memcpy(g_lights[idx].color, data, 4 * sizeof(float));
+            memcpy(g_lights[slot].color, data, 4 * sizeof(float));
             break;
         case SR_SPOT_ANGLE:
-            g_lights[idx].spot_angle = *data;
+            g_lights[slot].spot_angle = *data;
             break;
         case SR_SPOT_PENUMBRA:
-            g_lights[idx].spot_penumbra = *data;
+            g_lights[slot].spot_penumbra = *data;
             break;
         case SR_CONSTANT_ATTENUATION:
-            g_lights[idx].attn_const = *data;
+            g_lights[slot].attn_const = *data;
             break;
         case SR_LINEAR_ATTENUATION:
-            g_lights[idx].attn_lin = *data;
+            g_lights[slot].attn_lin = *data;
             break;
         case SR_QUADRATIC_ATTENUATION:
-            g_lights[idx].attn_quad = *data;
+            g_lights[slot].attn_quad = *data;
             break;
         default:
             return;
@@ -353,6 +322,7 @@ sr_glight(enum sr_light_attr attr, float* data)
  *****************/
 
 /* binds light type to slot */
+
 extern void 
 sr_light_type(enum sr_light slot, enum sr_light_type type)
 {
@@ -370,17 +340,16 @@ sr_light_type(enum sr_light slot, enum sr_light_type type)
     }
 }
 
-
 /*******************
  * sr_light_enable *
  *******************/
 
 /* enables light at specified slot */
+
 extern void 
 sr_light_enable(enum sr_light slot)
 {
-    int idx = split_light(slot);
-    g_uniform.light_state |= 1 << idx;
+    g_uniform.light_state |= 1 << slot;
 }
 
 /********************
@@ -391,8 +360,7 @@ sr_light_enable(enum sr_light slot)
 extern void 
 sr_light_disable(enum sr_light slot)
 {
-    int idx = split_light(slot);
-    g_uniform.light_state &= ~(1 << idx);
+    g_uniform.light_state &= ~(1 << slot);
 }
 
 /***************
@@ -407,7 +375,7 @@ sr_material(enum sr_light_attr attr, float* data)
         case SR_AMBIENT:
             memcpy(g_material.ambient, data, 4 * sizeof(float));
             break;
-        case SR_DIFFUSE:
+       case SR_DIFFUSE:
             memcpy(g_material.diffuse, data, 4 * sizeof(float));
             break;
         case SR_SPECULAR:
@@ -567,7 +535,8 @@ sr_rotate_x(float t)
  ***************/
 
 /* pushes a matrix rotating about the y axis by t radians */
-extern void
+
+void
 sr_rotate_y(float t)
 {
     float c = cos(t);
@@ -588,7 +557,8 @@ sr_rotate_y(float t)
  ***************/
 
 /* pushes a matrix rotating about the z axis by t radians */
-extern void
+
+void
 sr_rotate_z(float t)
 {
     float c = cos(t);
@@ -609,7 +579,7 @@ sr_rotate_z(float t)
  ************/
 
 /* pushes a scale matrix by sx, sy, sz */
-extern void
+void
 sr_scale(float sx, float sy, float sz)
 {
     struct mat4 s = {
@@ -632,13 +602,14 @@ sr_scale(float sx, float sy, float sz)
  * sr_look_at *
  **************/
 
-/* 
+/** 
  * constructs a view matrix from three vectors :
  * the eye vector, camera position in world space
  * the target vector, the position of view target in world space
  * the up vector, pointing to whats generally above the camera
  */
-extern void
+
+void
 sr_look_at(float ex, float ey, float ez, 
            float lx, float ly, float lz, 
            float ux, float uy, float uz)
@@ -697,7 +668,8 @@ sr_look_at(float ex, float ey, float ez,
  ******************/
 
 /* pushes a perspective matrix specified by fov */
-extern void
+
+void
 sr_perspective(float fovy, float aspect, float near, float far)
 {
     float f = 1 / (tan(fovy / 2));
@@ -720,7 +692,8 @@ sr_perspective(float fovy, float aspect, float near, float far)
  **************/
 
 /* pushes a projection matrix based on frustum */
-extern void
+
+void
 sr_frustum(float left, float right, float bottom, 
            float top, float near, float far)
 {

+ 56 - 198
sr.h

@@ -8,15 +8,17 @@
 #define SR_MAX_ATTRIBUTE_COUNT 32
 #define SR_MAX_LIGHT_COUNT 8
 
-#define SR_CLIP_LEFT_PLANE 1 << 0
-#define SR_CLIP_BOTTOM_PLANE 1 << 1
-#define SR_CLIP_NEAR_PLANE 1 << 2
-#define SR_CLIP_RIGHT_PLANE 1 << 3
-#define SR_CLIP_TOP_PLANE 1 << 4
-
 #define SR_WINDING_ORDER_CCW 1
 #define SR_WINDING_ORDER_CW -1
 
+enum sr_clip_plane {
+    SR_CLIP_LEFT_PLANE = 1 << 0,
+    SR_CLIP_BOTTOM_PLANE = 1 << 1,
+    SR_CLIP_NEAR_PLANE = 1 << 2,
+    SR_CLIP_RIGHT_PLANE = 1 << 3,
+    SR_CLIP_TOP_PLANE = 1 << 4
+};
+
 enum sr_primitive {
     SR_POINT_LIST,
     SR_LINE_LIST,
@@ -33,14 +35,14 @@ enum sr_matrix_mode {
 };
 
 enum sr_light {
-    SR_LIGHT_1,
-    SR_LIGHT_2,
-    SR_LIGHT_3,
-    SR_LIGHT_4,
-    SR_LIGHT_5,
-    SR_LIGHT_6,
-    SR_LIGHT_7,
-    SR_LIGHT_8,
+    SR_LIGHT_1 = 0,
+    SR_LIGHT_2 = 1,
+    SR_LIGHT_3 = 2,
+    SR_LIGHT_4 = 3,
+    SR_LIGHT_5 = 4,
+    SR_LIGHT_6 = 5,
+    SR_LIGHT_7 = 6,
+    SR_LIGHT_8 = 7,
 };
 
 enum sr_light_attr {
@@ -75,75 +77,12 @@ enum sr_light_type {
 typedef void (*vs_f)(float* out, float* in, void* uniform);
 typedef void (*fs_f)(uint32_t* out, float* in, void* uniform);
 
-/*********************************************************************
- *                                                                   *
- *                      public data structures                       *
- *                                                                   *
- *********************************************************************/
-
-/*********
- * light *
- *********/
-
-/* holds light data */
-struct light {
-    uint8_t type;
-    float pos[3];
-    float color[4];
-
-    float dir[3];
-    float spot_angle;
-    float spot_penumbra;
-
-    float attn_const;
-    float attn_lin;
-    float attn_quad;
-};
-
-/************
- * material *
- ************/
-
-struct material {
-    float ambient[4];
-    float diffuse[4];
-    float specular[4];
-    float blend;
-    float shininess;
-};
-
-/**************
- * sr_uniform *
- **************/
-
-/* the uniform variables for the fixed lib shaders */
-struct sr_uniform {
-
-    /* geometry */
-    struct mat4* model;
-    struct mat4* normal_transform;
-    struct mat4* mvp;
-    float cam_pos[3];
-
-    /* material */
-    int has_texture;
-    struct material* material;
-    struct sr_texture* texture;
-
-    /* light */
-    uint8_t light_state;
-    struct light* lights;
-    float ka;
-    float kd;
-    float ks;
-};
-
-
 /******************
  * sr_framebuffer *
  ******************/
 
 /* interface to whatever writes to the screen */
+
 struct sr_framebuffer {
     uint32_t* colors;
     float* depths;           
@@ -160,34 +99,19 @@ struct sr_framebuffer {
  * memory required to render an indexed list of
  * arbitrary length
  */
+
 struct sr_pipeline {
     struct sr_framebuffer* fbuf;
     void* uniform;        
     vs_f vs;
     fs_f fs;
-
     float* pts_in;
     int n_pts;
     int n_attr_in;
     int n_attr_out;
-
     int winding;
 };
 
-/*********************************************************************
- *                                                                   *
- *                             file input                            *
- *                                                                   *
- *********************************************************************/
-
-
-/*********************************************************************
- *                                                                   *
- *                           struct helpers                          *
- *                                                                   *
- *********************************************************************/
-
-
 /*********************************************************************
  *                                                                   *
  *                           render pipeline                         *
@@ -196,38 +120,15 @@ struct sr_pipeline {
 
 /* render interface */
 
-extern void
-sr_bind_pts(float* pts, int n_pts, int n_attr);
-
-extern void
-sr_bind_framebuffer(int width, int height, uint32_t* colors, float* depths);
-
-extern void
-sr_bind_vs(vs_f vs, int n_attr_out);
-
-/* WARNING can't use matrix stack, default shaders, or lights if called */
-extern void
-sr_bind_uniform(void* uniform);
-
-/* restores default uniform if sr_bind_uniform was called */
-extern void
-sr_restore_uniform();
-
-extern void
-sr_bind_fs(fs_f fs);
-
-extern void
-sr_bind_texture(uint32_t* colors, int width, int height);
-
-extern void
-sr_bind_base_color(float r, float g, float b);
-
-extern void
-sr_renderl(int* indices, int n_indices, enum sr_primitive prim_type);
-
-extern void
-sr_render(struct sr_pipeline* pipe, int* indices, 
-          int n_indices, enum sr_primitive prim_type);
+void sr_bind_vertices(float* pts, int n_pts, int n_attr);
+void sr_bind_framebuffer(int width, int height, uint32_t* colors, float* depths);
+void sr_bind_uniform(void* uniform);
+void sr_restore_uniform();
+void sr_bind_texture(uint32_t* colors, int width, int height);
+void sr_bind_base_color(float r, float g, float b);
+void sr_renderl(int* indices, int n_indices, enum sr_primitive prim_type);
+void sr_render(struct sr_pipeline* pipe, int* indices, 
+               int n_indices, enum sr_primitive prim_type);
 
 /*********************************************************************
  *                                                                   *
@@ -235,23 +136,12 @@ sr_render(struct sr_pipeline* pipe, int* indices,
  *                                                                   *
  *********************************************************************/
 
-extern void
-sr_light(enum sr_light slot, enum sr_light_attr attr, float* data);
-
-extern void 
-sr_glight(enum sr_light_attr attr, float* data);
-
-extern void 
-sr_light_type(enum sr_light slot, enum sr_light_type type);
-
-extern void
-sr_light_enable(enum sr_light slot);
-
-extern void
-sr_light_disable(enum sr_light slot);
-
-extern void
-sr_material(enum sr_light_attr attr, float* data);
+void sr_light(enum sr_light slot, enum sr_light_attr attr, float* data);
+void sr_glight(enum sr_light_attr attr, float* data);
+void sr_light_type(enum sr_light slot, enum sr_light_type type);
+void sr_light_enable(enum sr_light slot);
+void sr_light_disable(enum sr_light slot);
+void sr_material(enum sr_light_attr attr, float* data);
 
 /*********************************************************************
  *                                                                   *
@@ -259,44 +149,21 @@ sr_material(enum sr_light_attr attr, float* data);
  *                                                                   *
  *********************************************************************/
 
-extern void
-sr_matrix_mode(enum sr_matrix_mode mode);
-
-extern void
-sr_dump_matrix(float* dest);
-
-extern void
-sr_load_matrix(float* src);
-
-extern void
-sr_load_identity();
-
-extern void
-sr_perspective(float fovy, float aspect, float near, float far);
-
-extern void
-sr_frustum(float left, float right, float bottom, 
-           float top, float near, float far);
-
-extern void
-sr_translate(float x, float y, float z);
-
-extern void
-sr_rotate_x(float t);
-
-extern void
-sr_rotate_y(float t);
-
-extern void
-sr_rotate_z(float t);
-
-extern void
-sr_scale(float sx, float sy, float sz);
-
-extern void
-sr_look_at(float ex, float ey, float ez, 
-           float cx, float cy, float cz, 
-           float ux, float uy, float uz);
+void sr_matrix_mode(enum sr_matrix_mode mode);
+void sr_dump_matrix(float* dest);
+void sr_load_matrix(float* src);
+void sr_load_identity();
+void sr_perspective(float fovy, float aspect, float near, float far);
+void sr_frustum(float left, float right, float bottom, 
+                float top, float near, float far);
+void sr_translate(float x, float y, float z);
+void sr_rotate_x(float t);
+void sr_rotate_y(float t);
+void sr_rotate_z(float t);
+void sr_scale(float sx, float sy, float sz);
+void sr_look_at(float ex, float ey, float ez, 
+                float cx, float cy, float cz, 
+                float ux, float uy, float uz);
 
 /*********************************************************************
  *                                                                   *
@@ -304,22 +171,13 @@ sr_look_at(float ex, float ey, float ez,
  *                                                                   *
  *********************************************************************/
 
-extern void 
-sr_bind_color_vs();
-
-extern void
-sr_bind_color_fs();
-
-extern void
-sr_bind_texture_vs();
-
-extern void
-sr_bind_texture_fs();
-
-extern void
-sr_bind_std_vs();
-
-extern void
-sr_bind_phong_fs();
+void sr_bind_custom_vs(vs_f vs, int n_attr_out);
+void sr_bind_custom_fs(fs_f fs);
+void sr_bind_color_vs();
+void sr_bind_color_fs();
+void sr_bind_texture_vs();
+void sr_bind_texture_fs();
+void sr_bind_std_vs();
+void sr_bind_phong_fs();
 
 #endif /* SR_H */

+ 130 - 0
sr_priv.h

@@ -0,0 +1,130 @@
+
+/*********************************************************************
+ *                                                                   *
+ *                               uniform                             *
+ *                                                                   *
+ *********************************************************************/
+
+/***********
+ * texture *
+ ***********/
+
+struct texture {
+    uint32_t* data;
+    int n_pixels;
+};
+
+/*********
+ * light *
+ *********/
+
+/* holds light data */
+
+struct light {
+    enum sr_light_type type;
+    float pos[3];
+    float color[4];
+    float dir[3];
+    float spot_angle;
+    float spot_penumbra;
+    float attn_const;
+    float attn_lin;
+    float attn_quad;
+};
+
+/************
+ * material *
+ ************/
+
+struct material {
+    struct texture texture;
+    float ambient[4];
+    float diffuse[4];
+    float specular[4];
+    float blend;
+    float shininess;
+};
+
+/***********
+ * uniform *
+ ***********/
+
+/* the uniform variables for the fixed lib shaders */
+
+struct uniform {
+    struct mat4* model;             /* geometry */
+    struct mat4* normal;
+    struct mat4* mvp;
+    float cam_pos[3];
+    int has_texture;                /* material */
+    struct material* material;
+    uint8_t light_state;            /* light */
+    struct light* lights;
+    float ka;
+    float kd;
+    float ks;
+};
+
+/*********************************************************************
+ *                                                                   *
+ *                             rasterizer                            *
+ *                                                                   *
+ *********************************************************************/
+
+/**********
+ * raster *
+ **********/
+
+struct raster {
+    struct sr_framebuffer* fbuf;
+    void* uniform;
+    fs_f fs;
+    int n_attr;
+    int winding;
+};
+
+void draw_pt(struct raster* rast, float* pt);
+void draw_ln(struct raster* rast, float* v0, float* v1);
+void draw_tr(struct raster* rast, float* v0, float* v1, float* v2);
+
+/*********************************************************************
+ *                                                                   *
+ *                        clipping functions                         *
+ *                                                                   *
+ *********************************************************************/
+
+void clip_poly(float* src, int* n_pts, 
+               int n_attr, uint8_t clip_flags);
+void clip_test(float* pt, uint8_t* flags);
+
+/*********************************************************************
+ *                                                                   *
+ *                               math                                *
+ *                                                                   *
+ *********************************************************************/
+
+struct mat4 {
+    float e00, e01, e02, e03;
+    float e10, e11, e12, e13;
+    float e20, e21, e22, e23;
+    float e30, e31, e32, e33;
+};
+
+void matmul(struct mat4* a, struct mat4* b);
+int invert(struct mat4* a);
+void transpose(struct mat4* a);
+void upper_3x3(struct mat4* a);
+void vec4_matmul(float* a, struct mat4* b, float* c);
+void vec4_mul(float* a, float* b, float* c);
+void vec4_add(float* a, float* b, float* c);
+void vec4_scale(float* a, float* b, float c);
+void lerp(float* a, float* b, float* c, float alpha);
+void vec3_sub(float* a, float* b, float* c);
+void vec3_add(float* a, float* b, float* c);
+void vec3_scale(float* a, float* b, float c);
+void reflect(float* r, float* n, float* v);
+float dot(float* a, float* b);
+void cross(float* a, float* b, float* c);
+float magnitude(float* a);
+void normalize(float* a);
+float radians(float deg);