14 static int init(void);
15 static void destroy(void);
16 static void start(long trans_time);
17 static void draw(void);
18 static int gen_phong_tex(struct pimage *img, int xsz, int ysz, float sexp,
19 float offx, float offy, int dr, int dg, int db, int sr, int sg, int sb);
21 static struct screen scr = {
29 static float cam_theta = -29, cam_phi = 35;
30 static float cam_dist = 6;
31 static struct pimage tex_inner, tex_outer;
32 static struct g3d_mesh mesh_cube, mesh_cube2;
34 struct screen *infcubes_screen(void)
39 #define PHONG_TEX_SZ 128
43 static const float scalemat[16] = {-6, 0, 0, 0, 0, -6, 0, 0, 0, 0, -6, 0, 0, 0, 0, 1};
45 if(!(tex_inner.pixels = img_load_pixels("data/crate.jpg", &tex_inner.width,
46 &tex_inner.height, IMG_FMT_RGB24))) {
47 fprintf(stderr, "infcubes: failed to load crate texture\n");
50 convimg_rgb24_rgb16(tex_inner.pixels, (unsigned char*)tex_inner.pixels, tex_inner.width, tex_inner.height);
52 gen_phong_tex(&tex_inner, PHONG_TEX_SZ, PHONG_TEX_SZ, 5.0f, 0, 0, 10, 50, 92, 192, 192, 192);
54 if(!(tex_outer.pixels = img_load_pixels("data/refmap1.jpg", &tex_outer.width,
55 &tex_outer.height, IMG_FMT_RGB24))) {
56 fprintf(stderr, "infcubes: failed to load outer texture\n");
59 convimg_rgb24_rgb16(tex_outer.pixels, (unsigned char*)tex_outer.pixels, tex_outer.width, tex_outer.height);
60 /*gen_phong_tex(&tex_outer, PHONG_TEX_SZ, PHONG_TEX_SZ, 5.0f, 50, 50, 50, 255, 255, 255);*/
63 if(gen_cube_mesh(&mesh_cube, 1.0f, 3) == -1) {
67 if(load_mesh(&mesh_cube, "data/bevelbox.obj") == -1) {
70 if(copy_mesh(&mesh_cube2, &mesh_cube) == -1) {
73 apply_mesh_xform(&mesh_cube2, scalemat);
74 normalize_mesh_normals(&mesh_cube2);
78 static void destroy(void)
80 img_free_pixels(tex_inner.pixels);
83 static void start(long trans_time)
85 g3d_matrix_mode(G3D_PROJECTION);
87 g3d_perspective(70.0, 1.3333333, 0.5, 100.0);
89 g3d_enable(G3D_CULL_FACE);
90 g3d_disable(G3D_LIGHTING);
91 g3d_enable(G3D_LIGHT0);
94 static void update(void)
96 mouse_orbit_update(&cam_theta, &cam_phi, &cam_dist);
99 static void draw(void)
101 float t = (float)time_msec / 16.0f;
104 g3d_matrix_mode(G3D_MODELVIEW);
106 g3d_translate(0, 0, -cam_dist);
107 g3d_rotate(cam_phi, 1, 0, 0);
108 g3d_rotate(cam_theta, 0, 1, 0);
110 g3d_mult_matrix(sball_matrix);
113 /*memset(fb_pixels, 0, FB_WIDTH * FB_HEIGHT * 2);*/
115 g3d_polygon_mode(G3D_FLAT);
116 g3d_enable(G3D_TEXTURE_2D);
117 g3d_enable(G3D_TEXTURE_GEN);
120 g3d_rotate(t, 1, 0, 0);
121 g3d_rotate(t, 0, 1, 0);
122 g3d_set_texture(tex_outer.width, tex_outer.height, tex_outer.pixels);
123 draw_mesh(&mesh_cube2);
126 g3d_set_texture(tex_inner.width, tex_inner.height, tex_inner.pixels);
127 draw_mesh(&mesh_cube);
128 g3d_disable(G3D_TEXTURE_GEN);
130 swap_buffers(fb_pixels);
133 static int gen_phong_tex(struct pimage *img, int xsz, int ysz, float sexp,
134 float offx, float offy, int dr, int dg, int db, int sr, int sg, int sb)
140 if(!(img->pixels = malloc(xsz * ysz * sizeof *pix))) {
145 du = 2.0f / (float)(xsz - 1);
146 dv = 2.0f / (float)(ysz - 1);
149 for(i=0; i<ysz; i++) {
151 for(j=0; j<xsz; j++) {
152 float d = sqrt(u * u + v * v);
153 float val = pow(cos(d * M_PI / 2.0f), sexp);
154 int ival = abs(val * 255.0f);
156 int r = dr + ival * sr / 256;
157 int g = dg + ival * sg / 256;
158 int b = db + ival * sb / 256;
164 *pix++ = PACK_RGB16(r, g, b);