| 1 | #include "zgl.h" | 
|---|
| 2 | #include "msghandling.h" | 
|---|
| 3 | #include <math.h> | 
|---|
| 4 | #include <stdlib.h> | 
|---|
| 5 |  | 
|---|
| 6 | static void calc_buf(GLSpecBuf *buf, const float shininess) | 
|---|
| 7 | { | 
|---|
| 8 |   int i; | 
|---|
| 9 |   float val, inc; | 
|---|
| 10 |   val = 0.0f; | 
|---|
| 11 |   inc = 1.0f/SPECULAR_BUFFER_SIZE; | 
|---|
| 12 |   for (i = 0; i <= SPECULAR_BUFFER_SIZE; i++) { | 
|---|
| 13 |     buf->buf[i] = pow(val, shininess); | 
|---|
| 14 |     val += inc; | 
|---|
| 15 |   } | 
|---|
| 16 | } | 
|---|
| 17 |  | 
|---|
| 18 | GLSpecBuf * | 
|---|
| 19 | specbuf_get_buffer(GLContext *c, const int shininess_i,  | 
|---|
| 20 |                    const float shininess) | 
|---|
| 21 | { | 
|---|
| 22 |   GLSpecBuf *found, *oldest; | 
|---|
| 23 |   found = oldest = c->specbuf_first; | 
|---|
| 24 |   while (found && found->shininess_i != shininess_i) { | 
|---|
| 25 |     if (found->last_used < oldest->last_used) { | 
|---|
| 26 |       oldest = found; | 
|---|
| 27 |     } | 
|---|
| 28 |     found = found->next;  | 
|---|
| 29 |   } | 
|---|
| 30 |   if (found) { /* hey, found one! */ | 
|---|
| 31 |     found->last_used = c->specbuf_used_counter++; | 
|---|
| 32 |     return found; | 
|---|
| 33 |   } | 
|---|
| 34 |   if (oldest == NULL || c->specbuf_num_buffers < MAX_SPECULAR_BUFFERS) { | 
|---|
| 35 |     /* create new buffer */ | 
|---|
| 36 |     GLSpecBuf *buf = gl_malloc(sizeof(GLSpecBuf)); | 
|---|
| 37 |     if (!buf) gl_fatal_error("could not allocate specular buffer"); | 
|---|
| 38 |     c->specbuf_num_buffers++; | 
|---|
| 39 |     buf->next = c->specbuf_first; | 
|---|
| 40 |     c->specbuf_first = buf; | 
|---|
| 41 |     buf->last_used = c->specbuf_used_counter++; | 
|---|
| 42 |     buf->shininess_i = shininess_i; | 
|---|
| 43 |     calc_buf(buf, shininess); | 
|---|
| 44 |     return buf;      | 
|---|
| 45 |   } | 
|---|
| 46 |   /* overwrite the lru buffer */ | 
|---|
| 47 |   /*tgl_trace("overwriting spec buffer :(\n");*/ | 
|---|
| 48 |   oldest->shininess_i = shininess_i; | 
|---|
| 49 |   oldest->last_used = c->specbuf_used_counter++; | 
|---|
| 50 |   calc_buf(oldest, shininess); | 
|---|
| 51 |   return oldest; | 
|---|
| 52 | } | 
|---|