texture mapping and shading LUTs
[metatoy] / src / colormgr.c
1 #include <stdio.h>
2 #include "colormgr.h"
3 #include "vga.h"
4 #include "util.h"
5
6 unsigned char *colormap;
7 int *shade_lut;
8
9 void init_colormgr(void)
10 {
11         unsigned int i, r, g, b;
12         unsigned char *cptr;
13
14         cptr = colormap = malloc_nf(256 * 3);
15
16         for(i=0; i<256; i++) {
17                 r = i & 0xe0;
18                 g = (i << 3) & 0xe0;
19                 b = (i << 5) & 0xc0;
20
21                 r |= r >> 3;
22                 g |= g >> 3;
23                 b |= (b >> 2) | (b >> 4);
24
25                 cptr[0] = r | (r >> 3);
26                 cptr[1] = g | (g >> 3);
27                 cptr[2] = b | (b >> 2) | (b >> 4);
28                 vga_setpalent(i, cptr[0], cptr[1], cptr[2]);
29                 cptr += 3;
30         }
31
32         shade_lut = calloc_nf(256 * SHADE_LEVELS, sizeof *shade_lut);
33 }
34
35 void load_colormap(int offs, int sz, unsigned char *col, unsigned char *slut)
36 {
37         int i, j;
38         unsigned char *cptr;
39         int *sptr;
40
41         if(sz + offs > 256) sz = 256 - offs;
42
43         cptr = colormap + offs * 3;
44         sptr = shade_lut + (offs << SHADE_SHIFT);
45         for(i=0; i<sz; i++) {
46                 cptr[0] = col[0];
47                 cptr[1] = col[1];
48                 cptr[2] = col[2];
49                 vga_setpalent(offs + i, col[0], col[1], col[2]);
50                 cptr += 3;
51                 col += 3;
52
53                 for(j=0; j<SHADE_LEVELS; j++) {
54                         sptr[j] = (int)slut[j];
55                 }
56                 sptr += SHADE_LEVELS;
57                 slut += SHADE_LEVELS;
58         }
59 }
60
61 int find_color(int r, int g, int b)
62 {
63         return 0;       /* TODO */
64 }
65
66 int shade_color(int col, int shade)
67 {
68         return LOOKUP_SHADE(col, shade);
69 }