Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029 #include "graphics/tinygl/zgl.h"
00030
00031 namespace TinyGL {
00032
00033 static void calc_buf(GLSpecBuf *buf, const float shininess) {
00034 float val, inc;
00035 val = 0.0f;
00036 inc = 1.0f / SPECULAR_BUFFER_SIZE;
00037 for (int i = 0; i <= SPECULAR_BUFFER_SIZE; i++) {
00038 buf->buf[i] = pow(val, shininess);
00039 val += inc;
00040 }
00041 }
00042
00043 GLSpecBuf *specbuf_get_buffer(GLContext *c, const int shininess_i, const float shininess) {
00044 GLSpecBuf *found, *oldest;
00045 found = oldest = c->specbuf_first;
00046 while (found && found->shininess_i != shininess_i) {
00047 if (found->last_used < oldest->last_used) {
00048 oldest = found;
00049 }
00050 found = found->next;
00051 }
00052 if (found) {
00053 found->last_used = c->specbuf_used_counter++;
00054 return found;
00055 }
00056 if (!oldest || c->specbuf_num_buffers < MAX_SPECULAR_BUFFERS) {
00057
00058 GLSpecBuf *buf = (GLSpecBuf *)gl_malloc(sizeof(GLSpecBuf));
00059 if (!buf)
00060 error("could not allocate specular buffer");
00061 c->specbuf_num_buffers++;
00062 buf->next = c->specbuf_first;
00063 c->specbuf_first = buf;
00064 buf->last_used = c->specbuf_used_counter++;
00065 buf->shininess_i = shininess_i;
00066 calc_buf(buf, shininess);
00067 return buf;
00068 }
00069
00070 oldest->shininess_i = shininess_i;
00071 oldest->last_used = c->specbuf_used_counter++;
00072 calc_buf(oldest, shininess);
00073 return oldest;
00074 }
00075
00076 void specbuf_cleanup(GLContext *c) {
00077 GLSpecBuf *buf, *next;
00078 buf = c->specbuf_first;
00079 for (int i = 0; i < c->specbuf_num_buffers; ++i) {
00080 next = buf->next;
00081 gl_free(buf);
00082 buf = next;
00083 }
00084 }
00085
00086 }