1 | #include <stdio.h> |
---|
2 | #include <stdlib.h> |
---|
3 | #include <memory.h> |
---|
4 | |
---|
5 | #include "../include/color.h" |
---|
6 | |
---|
7 | using namespace VMI; |
---|
8 | |
---|
9 | Color *VMI::initColors(GLuint numColors) { |
---|
10 | Color *colors; |
---|
11 | |
---|
12 | colors = (Color *)malloc(sizeof(Color) * (numColors + 1)); |
---|
13 | /* The 0 position is reserved for the BACKGROUND, thus color of triangle 0 |
---|
14 | is at 1 position and so on */ |
---|
15 | |
---|
16 | if (colors == NULL) { |
---|
17 | fprintf(stderr, "Error allocating memory\n"); |
---|
18 | exit(1); |
---|
19 | } |
---|
20 | |
---|
21 | /* Fill the color buffer with the BACKGROUND color */ |
---|
22 | memset(colors, BACKGROUND, sizeof(Color) * (numColors + 1)); |
---|
23 | |
---|
24 | return colors; |
---|
25 | } |
---|
26 | |
---|
27 | void VMI::fillAllColors(Color *colors, GLuint numColors, GLuint begin, GLuint end, GLubyte color) { |
---|
28 | GLuint i; |
---|
29 | |
---|
30 | if (end > numColors) return; |
---|
31 | |
---|
32 | for (i=begin; i<end; i++) { |
---|
33 | |
---|
34 | colors[i].r = color; |
---|
35 | colors[i].g = color; |
---|
36 | colors[i].b = color; |
---|
37 | colors[i].a = color; |
---|
38 | } |
---|
39 | |
---|
40 | } |
---|
41 | |
---|
42 | void VMI::setColors(Color *colors, GLuint numPasses, GLuint begin, GLuint end) { |
---|
43 | GLuint i, step = numPasses * MAX_NUM_COLORS; |
---|
44 | GLubyte r = 1, |
---|
45 | g = 1, |
---|
46 | b = 1, |
---|
47 | a = 1; |
---|
48 | |
---|
49 | for (i = begin; i < end; i++) { |
---|
50 | if (i < (255 + step)) { |
---|
51 | colors[i].r = r; |
---|
52 | r++; |
---|
53 | colors[i].g = 0; |
---|
54 | colors[i].b = 0; |
---|
55 | colors[i].a = 0; |
---|
56 | |
---|
57 | } |
---|
58 | if ((i >= (255 + step)) && (i < (510 + step))) { |
---|
59 | colors[i].r = 0; |
---|
60 | colors[i].g = g; |
---|
61 | g++; |
---|
62 | colors[i].b = 0; |
---|
63 | colors[i].a = 0; |
---|
64 | |
---|
65 | } |
---|
66 | |
---|
67 | if ((i >= (510 + step)) && (i < (765 + step))) { |
---|
68 | colors[i].r = 0; |
---|
69 | colors[i].g = 0; |
---|
70 | colors[i].b = b; |
---|
71 | b++; |
---|
72 | colors[i].a = 0; |
---|
73 | |
---|
74 | } |
---|
75 | if ((i >= (765 + step)) && (i < (1020 + step))) { |
---|
76 | colors[i].r = 0; |
---|
77 | colors[i].g = 0; |
---|
78 | colors[i].b = 0; |
---|
79 | colors[i].a = a; |
---|
80 | a++; |
---|
81 | |
---|
82 | } |
---|
83 | //printf("c:%d (%d,%d,%d,%d)\n",i, colors[i].r, colors[i].g, colors[i].b, colors[i].a); |
---|
84 | //getchar(); |
---|
85 | } |
---|
86 | } |
---|
87 | |
---|
88 | void VMI::setColors2(Color *colors, GLuint numColors, GLuint begin, GLuint end) { |
---|
89 | GLuint i, |
---|
90 | channel = 0, |
---|
91 | step = (numColors < MAX_NUM_COLORS) ? (MAX_NUM_COLORS / numColors) : 1; |
---|
92 | GLubyte r = 1; |
---|
93 | |
---|
94 | if (end > numColors) return; |
---|
95 | |
---|
96 | for (i = begin; i < end; i++) { |
---|
97 | |
---|
98 | if (channel == 0) { |
---|
99 | colors[i].r = r; |
---|
100 | colors[i].g = 0; |
---|
101 | colors[i].b = 0; |
---|
102 | colors[i].a = 0; |
---|
103 | } |
---|
104 | if (channel == 1) { |
---|
105 | colors[i].r = 0; |
---|
106 | colors[i].g = r; |
---|
107 | colors[i].b = 0; |
---|
108 | colors[i].a = 0; |
---|
109 | } |
---|
110 | if (channel == 2) { |
---|
111 | colors[i].r = 0; |
---|
112 | colors[i].g = 0; |
---|
113 | colors[i].b = r; |
---|
114 | colors[i].a = 0; |
---|
115 | } |
---|
116 | if (channel == 3) { |
---|
117 | colors[i].r = 0; |
---|
118 | colors[i].g = 0; |
---|
119 | colors[i].b = 0; |
---|
120 | colors[i].a = r; |
---|
121 | } |
---|
122 | |
---|
123 | channel++; |
---|
124 | |
---|
125 | if (channel > 3) { |
---|
126 | if (r == 255) r = 1; |
---|
127 | else r += step; |
---|
128 | channel = 0; |
---|
129 | } |
---|
130 | //printf("c:%d (%d,%d,%d,%d)\n", i, colors[i].r, colors[i].g, colors[i].b, colors[i].a); |
---|
131 | //getchar(); |
---|
132 | } |
---|
133 | } |
---|
134 | |
---|
135 | void VMI::setColors3(Color *colors, GLuint numColors, GLubyte color) { |
---|
136 | GLuint i; |
---|
137 | |
---|
138 | for (i = 0; i < numColors; i++) { |
---|
139 | |
---|
140 | colors[i].r = color; |
---|
141 | colors[i].g = 0; |
---|
142 | colors[i].b = 0; |
---|
143 | colors[i].a = 0; |
---|
144 | |
---|
145 | //printf("c:%d (%d,%d,%d,%d)\n", i, colors[i].r, colors[i].g, colors[i].b, colors[i].a); |
---|
146 | //getchar(); |
---|
147 | } |
---|
148 | } |
---|
149 | |
---|
150 | void VMI::setColors4(Color *colors, GLuint numColors) { |
---|
151 | GLuint i, c = 1; |
---|
152 | |
---|
153 | for (i = 0; i < numColors; i++) { |
---|
154 | |
---|
155 | colors[i].r = c & 0xFF; |
---|
156 | colors[i].g = (c & 0xFF00) >> 8; |
---|
157 | colors[i].b = (c & 0xFF0000) >> 16; |
---|
158 | colors[i].a = (c & 0xFF000000) >> 24; |
---|
159 | |
---|
160 | //printf("c:%d (%d,%d,%d,%d)\n", i, colors[i].r, colors[i].g, colors[i].b, colors[i].a); |
---|
161 | c++; |
---|
162 | } |
---|
163 | //getchar(); |
---|
164 | } |
---|
165 | |
---|
166 | void VMI::viewColors(Color *colors, GLuint numColors) { |
---|
167 | GLuint i; |
---|
168 | |
---|
169 | printf("\n"); |
---|
170 | for (i=0; i<numColors; i++) |
---|
171 | printf("c:%d (%d,%d,%d,%d)\n", i, colors[i].r, colors[i].g, colors[i].b, colors[i].a); |
---|
172 | } |
---|