struct g3d_state {
unsigned int opt;
int frontface;
+ int fill_mode;
g3d_matrix mat[G3D_NUM_MATRICES][STACK_SIZE];
int mtop[G3D_NUM_MATRICES];
fprintf(stderr, "failed to allocate G3D context\n");
return -1;
}
+ st->fill_mode = POLYFILL_FLAT;
for(i=0; i<G3D_NUM_MATRICES; i++) {
g3d_matrix_mode(i);
st->width = width;
st->height = height;
st->pixels = pixels;
+
+ pimg_fb.pixels = pixels;
+ pimg_fb.width = width;
+ pimg_fb.height = height;
}
void g3d_enable(unsigned int opt)
st->frontface = order;
}
+void g3d_polygon_mode(int pmode)
+{
+ st->fill_mode = pmode;
+}
+
void g3d_matrix_mode(int mmode)
{
st->mmode = mmode;
}
}
- polyfill_flat(pv, vnum);
+ polyfill(st->fill_mode, pv, vnum);
}
}
/* arg to g3d_front_face */
enum { G3D_CCW, G3D_CW };
+/* arg to g3d_polygon_mode */
+enum {
+ G3D_WIRE,
+ G3D_FLAT
+};
+
/* matrix stacks */
enum {
G3D_MODELVIEW,
unsigned int g3d_getopt(unsigned int mask);
void g3d_front_face(unsigned int order);
+void g3d_polygon_mode(int pmode);
void g3d_matrix_mode(int mmode);
#ifndef GFXUTIL_H_
#define GFXUTIL_H_
+#define PACK_RGB16(r, g, b) \
+ (((((r) >> 3) & 0x1f) << 11) | ((((g) >> 2) & 0x3f) << 5) | (((b) >> 3) & 0x1f))
+
+#define PACK_RGB32(r, g, b) \
+ ((((r) & 0xff) << 16) | (((g) & 0xff) << 8) | ((b) & 0xff))
+
int clip_line(int *x0, int *y0, int *x1, int *y1, int xmin, int ymin, int xmax, int ymax);
void draw_line(int x0, int y0, int x1, int y1, unsigned short color);
0, 0, 0
};
+struct pimage pimg_fb, pimg_texture;
+
void polyfill(int mode, struct pvertex *verts, int nverts)
{
#ifndef NDEBUG
++v;
x1 = v->x >> 8;
y1 = v->y >> 8;
- if(clip_line(&x0, &y0, &x1, &y1, 0, 0, fb_width, fb_height)) {
+ if(clip_line(&x0, &y0, &x1, &y1, 0, 0, pimg_fb.width, pimg_fb.height)) {
draw_line(x0, y0, x1, y1, color);
}
}
x0 = verts[0].x >> 8;
y0 = verts[0].y >> 8;
- if(clip_line(&x1, &y1, &x0, &y0, 0, 0, fb_width, fb_height)) {
+ if(clip_line(&x1, &y1, &x0, &y0, 0, 0, pimg_fb.width, pimg_fb.height)) {
draw_line(x1, y1, x0, y0, color);
}
}
x = left_x >> 8;
slen = (right_x >> 8) - (left_x >> 8);
- pixptr = (uint16_t*)fb_pixels + sline * fb_width + x;
+ pixptr = pimg_fb.pixels + sline * pimg_fb.width + x;
for(i=0; i<slen; i++) {
- *pixptr++ += 10;
+ *pixptr++ += 15;
}
++sline;
unsigned char r, g, b;
};
+struct pimage {
+ uint16_t *pixels;
+ int width, height;
+};
+
+extern struct pimage pimg_fb;
+extern struct pimage pimg_texture;
+
void polyfill(int mode, struct pvertex *verts, int nverts);
void polyfill_wire(struct pvertex *verts, int nverts);
void polyfill_flat(struct pvertex *verts, int nverts);
#include "screen.h"
#include "demo.h"
#include "3dgfx.h"
+#include "gfxutil.h"
struct mesh {
int prim;
static int gen_cube(struct mesh *mesh, float sz);
static int gen_torus(struct mesh *mesh, float rad, float ringrad, int usub, int vsub);
static void zsort(struct mesh *m);
+static void draw_lowres_raster(void);
static struct screen scr = {
"polytest",
draw
};
+static float theta, phi = 25;
static struct mesh cube, torus;
+#define LOWRES_SCALE 16
+static uint16_t *lowres_pixels;
+static int lowres_width, lowres_height;
+
struct screen *polytest_screen(void)
{
return &scr;
{
gen_cube(&cube, 1.0);
gen_torus(&torus, 1.0, 0.25, 24, 12);
+
+ lowres_width = fb_width / LOWRES_SCALE;
+ lowres_height = fb_height / LOWRES_SCALE;
+ lowres_pixels = malloc(lowres_width * lowres_height * 2);
+
return 0;
}
static void destroy(void)
{
+ free(lowres_pixels);
free(cube.varr);
free(torus.varr);
free(torus.iarr);
g3d_enable(G3D_CULL_FACE);
}
-static void draw(void)
+static void update(void)
{
static int prev_mx, prev_my;
- static float theta, phi = 25;
+ static unsigned int prev_bmask;
- int dx = mouse_x - prev_mx;
- int dy = mouse_y - prev_my;
- prev_mx = mouse_x;
- prev_my = mouse_y;
+ if(mouse_bmask) {
+ if(mouse_bmask ^ prev_bmask == 0) {
+ int dx = mouse_x - prev_mx;
+ int dy = mouse_y - prev_my;
- if(dx || dy) {
- theta += dx * 2.0;
- phi += dy * 2.0;
+ if(dx || dy) {
+ theta += dx * 2.0;
+ phi += dy * 2.0;
- if(phi < -90) phi = -90;
- if(phi > 90) phi = 90;
+ if(phi < -90) phi = -90;
+ if(phi > 90) phi = 90;
+ }
+ }
}
+ prev_mx = mouse_x;
+ prev_my = mouse_y;
+ prev_bmask = mouse_bmask;
+}
- /*float angle = (float)time_msec / 50.0;*/
+static void draw(void)
+{
+ update();
- memset(fb_pixels, 0, fb_width * fb_height * 2);
+ memset(lowres_pixels, 0, lowres_width * lowres_height * 2);
g3d_matrix_mode(G3D_MODELVIEW);
g3d_load_identity();
g3d_rotate(phi, 1, 0, 0);
g3d_rotate(theta, 0, 1, 0);
+ g3d_framebuffer(lowres_width, lowres_height, lowres_pixels);
/*zsort(&torus);*/
draw_mesh(&cube);
+ draw_lowres_raster();
+
+
+ g3d_framebuffer(fb_width, fb_height, fb_pixels);
+
+ g3d_polygon_mode(G3D_WIRE);
+ draw_mesh(&cube);
+ g3d_polygon_mode(G3D_FLAT);
+
swap_buffers(fb_pixels);
}
qsort(m->iarr, nfaces, m->prim * sizeof *m->iarr, zsort_cmp);
}
+
+static void draw_huge_pixel(uint16_t *dest, int dest_width, uint16_t color)
+{
+ int i, j;
+ uint16_t grid_color = PACK_RGB16(127, 127, 127);
+
+ for(i=0; i<LOWRES_SCALE; i++) {
+ for(j=0; j<LOWRES_SCALE; j++) {
+ dest[j] = i == 0 || j == 0 ? grid_color : color;
+ }
+ dest += dest_width;
+ }
+}
+
+static void draw_lowres_raster(void)
+{
+ int i, j;
+ uint16_t *sptr = lowres_pixels;
+ uint16_t *dptr = fb_pixels;
+
+ for(i=0; i<lowres_height; i++) {
+ for(j=0; j<lowres_width; j++) {
+ draw_huge_pixel(dptr, fb_width, *sptr++);
+ dptr += LOWRES_SCALE;
+ }
+ dptr += fb_width * LOWRES_SCALE - fb_width;
+ }
+}
#include "imago2.h"
#include "demo.h"
#include "screen.h"
+#include "gfxutil.h"
#ifndef M_PI
#define M_PI 3.1415926535
*bp = (b * fog) >> 8;
}
-#define PACK_RGB16(r, g, b) \
- (((((r) >> 3) & 0x1f) << 11) | ((((g) >> 2) & 0x3f) << 5) | (((b) >> 3) & 0x1f))
-#define PACK_RGB32(r, g, b) \
- ((((r) & 0xff) << 16) | (((g) & 0xff) << 8) | ((b) & 0xff))
-
static void draw_tunnel_range(unsigned short *pix, int xoffs, int yoffs, int starty, int num_lines, long tm)
{
int i, j;