a028262b51ae11f65960aa076709e9a823912fa5
[bootcensus] / src / census / 3dgfx.c
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
4 #include <math.h>
5 #include <assert.h>
6 #if defined(__WATCOMC__) || defined(_MSC_VER) || defined(__DJGPP__)
7 #include <malloc.h>
8 #else
9 #include <alloca.h>
10 #endif
11 #include "3dgfx.h"
12 #include "gfxutil.h"
13 #include "polyfill.h"
14 #include "polyclip.h"
15 #include "inttypes.h"
16 #include "census.h"
17 #include "util.h"
18
19 #define STACK_SIZE      8
20 typedef float g3d_matrix[16];
21
22 #define MAX_LIGHTS              4
23
24 #define IMM_VBUF_SIZE   256
25
26 struct light {
27         float x, y, z;
28         float r, g, b;
29 };
30
31 struct material {
32         float kd[3];
33         float ks[3];
34         float shin;
35 };
36
37 struct g3d_state {
38         unsigned int opt;
39         int frontface;
40         int polymode;
41
42         g3d_matrix mat[G3D_NUM_MATRICES][STACK_SIZE];
43         int mtop[G3D_NUM_MATRICES];
44         int mmode;
45
46         g3d_matrix norm_mat;
47
48         float ambient[3];
49         struct light lt[MAX_LIGHTS];
50         struct material mtl;
51
52         int width, height;
53         uint32_t *pixels;
54
55         int vport[4];
56
57         /* immediate mode */
58         int imm_prim;
59         int imm_numv, imm_pcount;
60         struct g3d_vertex imm_curv;
61         struct g3d_vertex imm_vbuf[IMM_VBUF_SIZE];
62 };
63
64 static void imm_flush(void);
65 static void xform4_vec3(const float *mat, float *vec);
66 static void xform3_vec3(const float *mat, float *vec);
67 static void shade(struct g3d_vertex *v);
68
69 static struct g3d_state *st;
70 static const float idmat[] = {
71         1, 0, 0, 0,
72         0, 1, 0, 0,
73         0, 0, 1, 0,
74         0, 0, 0, 1
75 };
76
77 int g3d_init(void)
78 {
79         int i;
80
81         if(!(st = calloc(1, sizeof *st))) {
82                 fprintf(stderr, "failed to allocate G3D context\n");
83                 return -1;
84         }
85         st->opt = G3D_CLIP_FRUSTUM;
86         st->polymode = POLYFILL_FLAT;
87
88         for(i=0; i<G3D_NUM_MATRICES; i++) {
89                 g3d_matrix_mode(i);
90                 g3d_load_identity();
91         }
92
93         for(i=0; i<MAX_LIGHTS; i++) {
94                 g3d_light_color(i, 1, 1, 1);
95         }
96         g3d_light_ambient(0.1, 0.1, 0.1);
97
98         g3d_mtl_diffuse(1, 1, 1);
99         return 0;
100 }
101
102 void g3d_destroy(void)
103 {
104         free(st);
105 }
106
107 void g3d_framebuffer(int width, int height, void *pixels)
108 {
109         st->width = width;
110         st->height = height;
111         st->pixels = pixels;
112
113         pfill_fb.pixels = pixels;
114         pfill_fb.width = width;
115         pfill_fb.height = height;
116
117         g3d_viewport(0, 0, width, height);
118 }
119
120 /* set the framebuffer pointer, without resetting the size */
121 void g3d_framebuffer_addr(void *pixels)
122 {
123         st->pixels = pixels;
124         pfill_fb.pixels = pixels;
125 }
126
127 void g3d_viewport(int x, int y, int w, int h)
128 {
129         st->vport[0] = x;
130         st->vport[1] = y;
131         st->vport[2] = w;
132         st->vport[3] = h;
133 }
134
135 void g3d_enable(unsigned int opt)
136 {
137         st->opt |= opt;
138 }
139
140 void g3d_disable(unsigned int opt)
141 {
142         st->opt &= ~opt;
143 }
144
145 void g3d_setopt(unsigned int opt, unsigned int mask)
146 {
147         st->opt = (st->opt & ~mask) | (opt & mask);
148 }
149
150 unsigned int g3d_getopt(unsigned int mask)
151 {
152         return st->opt & mask;
153 }
154
155 void g3d_front_face(unsigned int order)
156 {
157         st->frontface = order;
158 }
159
160 void g3d_polygon_mode(int pmode)
161 {
162         st->polymode = pmode;
163 }
164
165 void g3d_matrix_mode(int mmode)
166 {
167         st->mmode = mmode;
168 }
169
170 void g3d_load_identity(void)
171 {
172         int top = st->mtop[st->mmode];
173         memcpy(st->mat[st->mmode][top], idmat, 16 * sizeof(float));
174 }
175
176 void g3d_load_matrix(const float *m)
177 {
178         int top = st->mtop[st->mmode];
179         memcpy(st->mat[st->mmode][top], m, 16 * sizeof(float));
180 }
181
182 #define M(i,j)  (((i) << 2) + (j))
183 void g3d_mult_matrix(const float *m2)
184 {
185         int i, j, top = st->mtop[st->mmode];
186         float m1[16];
187         float *dest = st->mat[st->mmode][top];
188
189         memcpy(m1, dest, sizeof m1);
190
191         for(i=0; i<4; i++) {
192                 for(j=0; j<4; j++) {
193                         *dest++ = m1[M(0,j)] * m2[M(i,0)] +
194                                 m1[M(1,j)] * m2[M(i,1)] +
195                                 m1[M(2,j)] * m2[M(i,2)] +
196                                 m1[M(3,j)] * m2[M(i,3)];
197                 }
198         }
199 }
200
201 void g3d_push_matrix(void)
202 {
203         int top = st->mtop[st->mmode];
204         if(top >= G3D_NUM_MATRICES) {
205                 fprintf(stderr, "g3d_push_matrix overflow\n");
206                 return;
207         }
208         memcpy(st->mat[st->mmode][top + 1], st->mat[st->mmode][top], 16 * sizeof(float));
209         st->mtop[st->mmode] = top + 1;
210 }
211
212 void g3d_pop_matrix(void)
213 {
214         if(st->mtop[st->mmode] <= 0) {
215                 fprintf(stderr, "g3d_pop_matrix underflow\n");
216                 return;
217         }
218         --st->mtop[st->mmode];
219 }
220
221 void g3d_translate(float x, float y, float z)
222 {
223         float m[] = {1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1};
224         m[12] = x;
225         m[13] = y;
226         m[14] = z;
227         g3d_mult_matrix(m);
228 }
229
230 void g3d_rotate(float deg, float x, float y, float z)
231 {
232         float m[] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
233
234         float angle = M_PI * deg / 180.0f;
235         float sina = sin(angle);
236         float cosa = cos(angle);
237         float one_minus_cosa = 1.0f - cosa;
238         float nxsq = x * x;
239         float nysq = y * y;
240         float nzsq = z * z;
241
242         m[0] = nxsq + (1.0f - nxsq) * cosa;
243         m[4] = x * y * one_minus_cosa - z * sina;
244         m[8] = x * z * one_minus_cosa + y * sina;
245         m[1] = x * y * one_minus_cosa + z * sina;
246         m[5] = nysq + (1.0 - nysq) * cosa;
247         m[9] = y * z * one_minus_cosa - x * sina;
248         m[2] = x * z * one_minus_cosa - y * sina;
249         m[6] = y * z * one_minus_cosa + x * sina;
250         m[10] = nzsq + (1.0 - nzsq) * cosa;
251         m[15] = 1.0f;
252
253         g3d_mult_matrix(m);
254 }
255
256 void g3d_scale(float x, float y, float z)
257 {
258         float m[] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
259         m[0] = x;
260         m[5] = y;
261         m[10] = z;
262         m[15] = 1.0f;
263         g3d_mult_matrix(m);
264 }
265
266 void g3d_ortho(float left, float right, float bottom, float top, float znear, float zfar)
267 {
268         float m[] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
269
270         float dx = right - left;
271         float dy = top - bottom;
272         float dz = zfar - znear;
273
274         m[0] = 2.0 / dx;
275         m[5] = 2.0 / dy;
276         m[10] = -2.0 / dz;
277         m[12] = -(right + left) / dx;
278         m[13] = -(top + bottom) / dy;
279         m[14] = -(zfar + znear) / dz;
280
281         g3d_mult_matrix(m);
282 }
283
284 void g3d_frustum(float left, float right, float bottom, float top, float nr, float fr)
285 {
286         float m[] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
287
288         float dx = right - left;
289         float dy = top - bottom;
290         float dz = fr - nr;
291
292         float a = (right + left) / dx;
293         float b = (top + bottom) / dy;
294         float c = -(fr + nr) / dz;
295         float d = -2.0 * fr * nr / dz;
296
297         m[0] = 2.0 * nr / dx;
298         m[5] = 2.0 * nr / dy;
299         m[8] = a;
300         m[9] = b;
301         m[10] = c;
302         m[11] = -1.0f;
303         m[14] = d;
304
305         g3d_mult_matrix(m);
306 }
307
308 void g3d_perspective(float vfov_deg, float aspect, float znear, float zfar)
309 {
310         float m[] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
311
312         float vfov = M_PI * vfov_deg / 180.0f;
313         float s = 1.0f / tan(vfov * 0.5f);
314         float range = znear - zfar;
315
316         m[0] = s / aspect;
317         m[5] = s;
318         m[10] = (znear + zfar) / range;
319         m[11] = -1.0f;
320         m[14] = 2.0f * znear * zfar / range;
321
322         g3d_mult_matrix(m);
323 }
324
325 const float *g3d_get_matrix(int which, float *m)
326 {
327         int top = st->mtop[which];
328
329         if(m) {
330                 memcpy(m, st->mat[which][top], 16 * sizeof(float));
331         }
332         return st->mat[which][top];
333 }
334
335 void g3d_light_pos(int idx, float x, float y, float z)
336 {
337         int mvtop = st->mtop[G3D_MODELVIEW];
338
339         st->lt[idx].x = x;
340         st->lt[idx].y = y;
341         st->lt[idx].z = z;
342
343         xform4_vec3(st->mat[G3D_MODELVIEW][mvtop], &st->lt[idx].x);
344 }
345
346 void g3d_light_color(int idx, float r, float g, float b)
347 {
348         st->lt[idx].r = r;
349         st->lt[idx].g = g;
350         st->lt[idx].b = b;
351 }
352
353 void g3d_light_ambient(float r, float g, float b)
354 {
355         st->ambient[0] = r;
356         st->ambient[1] = g;
357         st->ambient[2] = b;
358 }
359
360 void g3d_mtl_diffuse(float r, float g, float b)
361 {
362         st->mtl.kd[0] = r;
363         st->mtl.kd[1] = g;
364         st->mtl.kd[2] = b;
365 }
366
367 void g3d_mtl_specular(float r, float g, float b)
368 {
369         st->mtl.ks[0] = r;
370         st->mtl.ks[1] = g;
371         st->mtl.ks[2] = b;
372 }
373
374 void g3d_mtl_shininess(float shin)
375 {
376         st->mtl.shin = shin;
377 }
378
379 static INLINE int calc_shift(unsigned int x)
380 {
381         int res = -1;
382         while(x) {
383                 x >>= 1;
384                 ++res;
385         }
386         return res;
387 }
388
389 static INLINE int calc_mask(unsigned int x)
390 {
391         return x - 1;
392 }
393
394 void g3d_set_texture(int xsz, int ysz, void *pixels)
395 {
396         pfill_tex.pixels = pixels;
397         pfill_tex.width = xsz;
398         pfill_tex.height = ysz;
399
400         pfill_tex.xshift = calc_shift(xsz);
401         pfill_tex.yshift = calc_shift(ysz);
402         pfill_tex.xmask = calc_mask(xsz);
403         pfill_tex.ymask = calc_mask(ysz);
404 }
405
406 void g3d_draw(int prim, const struct g3d_vertex *varr, int varr_size)
407 {
408         g3d_draw_indexed(prim, varr, varr_size, 0, 0);
409 }
410
411 void g3d_draw_indexed(int prim, const struct g3d_vertex *varr, int varr_size,
412                 const uint16_t *iarr, int iarr_size)
413 {
414         int i, j, vnum, nfaces, fill_mode;
415         struct pvertex pv[16];
416         struct g3d_vertex v[16];
417         int mvtop = st->mtop[G3D_MODELVIEW];
418         int ptop = st->mtop[G3D_PROJECTION];
419         struct g3d_vertex *tmpv;
420
421         tmpv = alloca(prim * 6 * sizeof *tmpv);
422
423         /* calc the normal matrix */
424         memcpy(st->norm_mat, st->mat[G3D_MODELVIEW][mvtop], 16 * sizeof(float));
425         st->norm_mat[12] = st->norm_mat[13] = st->norm_mat[14] = 0.0f;
426
427         nfaces = (iarr ? iarr_size : varr_size) / prim;
428
429         for(j=0; j<nfaces; j++) {
430                 vnum = prim;    /* reset vnum for each iteration */
431
432                 for(i=0; i<vnum; i++) {
433                         v[i] = iarr ? varr[*iarr++] : *varr++;
434
435                         xform4_vec3(st->mat[G3D_MODELVIEW][mvtop], &v[i].x);
436                         xform3_vec3(st->norm_mat, &v[i].nx);
437
438                         if(st->opt & G3D_LIGHTING) {
439                                 shade(v + i);
440                         }
441                         if(st->opt & G3D_TEXTURE_GEN) {
442                                 v[i].u = v[i].nx * 0.5 + 0.5;
443                                 v[i].v = v[i].ny * 0.5 + 0.5;
444                         }
445                         if(st->opt & G3D_TEXTURE_MAT) {
446                                 float *mat = st->mat[G3D_TEXTURE][st->mtop[G3D_TEXTURE]];
447                                 float x = mat[0] * v[i].u + mat[4] * v[i].v + mat[12];
448                                 float y = mat[1] * v[i].u + mat[5] * v[i].v + mat[13];
449                                 float w = mat[3] * v[i].u + mat[7] * v[i].v + mat[15];
450                                 v[i].u = x / w;
451                                 v[i].v = y / w;
452                         }
453                         xform4_vec3(st->mat[G3D_PROJECTION][ptop], &v[i].x);
454                 }
455
456                 /* clipping */
457                 if(st->opt & G3D_CLIP_FRUSTUM) {
458                         for(i=0; i<6; i++) {
459                                 memcpy(tmpv, v, vnum * sizeof *v);
460
461                                 if(clip_frustum(v, &vnum, tmpv, vnum, i) < 0) {
462                                         /* polygon completely outside of view volume. discard */
463                                         vnum = 0;
464                                         break;
465                                 }
466                         }
467
468                         if(!vnum) continue;
469                 }
470
471                 for(i=0; i<vnum; i++) {
472                         if(v[i].w != 0.0f) {
473                                 v[i].x /= v[i].w;
474                                 v[i].y /= v[i].w;
475                                 /*v[i].z /= v[i].w;*/
476                         }
477
478                         /* viewport transformation */
479                         v[i].x = (v[i].x * 0.5f + 0.5f) * (float)st->vport[2] + st->vport[0];
480                         v[i].y = (0.5f - v[i].y * 0.5f) * (float)st->vport[3] + st->vport[1];
481
482                         /* convert pos to 24.8 fixed point */
483                         pv[i].x = cround64(v[i].x * 256.0f);
484                         pv[i].y = cround64(v[i].y * 256.0f);
485                         /* convert tex coords to 16.16 fixed point */
486                         pv[i].u = cround64(v[i].u * 65536.0f);
487                         pv[i].v = cround64(v[i].v * 65536.0f);
488                         /* pass the color through as is */
489                         pv[i].r = v[i].r;
490                         pv[i].g = v[i].g;
491                         pv[i].b = v[i].b;
492                         pv[i].a = v[i].a;
493                 }
494
495                 /* backface culling */
496                 if(vnum > 2 && st->opt & G3D_CULL_FACE) {
497                         int32_t ax = pv[1].x - pv[0].x;
498                         int32_t ay = pv[1].y - pv[0].y;
499                         int32_t bx = pv[2].x - pv[0].x;
500                         int32_t by = pv[2].y - pv[0].y;
501                         int32_t cross_z = (ax >> 4) * (by >> 4) - (ay >> 4) * (bx >> 4);
502                         int sign = (cross_z >> 31) & 1;
503
504                         if(!(sign ^ st->frontface)) {
505                                 continue;       /* back-facing */
506                         }
507                 }
508
509                 switch(vnum) {
510                 case 1:
511                         if(st->opt & G3D_BLEND) {
512                                 int r, g, b;
513                                 int inv_alpha = 255 - pv[0].a;
514                                 uint32_t *dest = st->pixels + (pv[0].y >> 8) * st->width + (pv[0].x >> 8);
515                                 r = ((int)pv[0].r * pv[0].a + UNPACK_R32(*dest) * inv_alpha) >> 8;
516                                 g = ((int)pv[0].g * pv[0].a + UNPACK_G32(*dest) * inv_alpha) >> 8;
517                                 b = ((int)pv[0].b * pv[0].a + UNPACK_B32(*dest) * inv_alpha) >> 8;
518                                 *dest++ = PACK_RGB16(r, g, b);
519                         } else {
520                                 uint32_t *dest = st->pixels + (pv[0].y >> 8) * st->width + (pv[0].x >> 8);
521                                 *dest = PACK_RGB32(pv[0].r, pv[0].g, pv[0].b);
522                         }
523                         break;
524
525                 case 2:
526                         {
527                                 uint32_t col = PACK_RGB32(pv[0].r, pv[0].g, pv[0].b);
528                                 draw_line(pv[0].x >> 8, pv[0].y >> 8, pv[1].x >> 8, pv[1].y >> 8, col);
529                         }
530                         break;
531
532                 default:
533                         fill_mode = st->polymode;
534                         if(st->opt & G3D_TEXTURE_2D) {
535                                 fill_mode |= POLYFILL_TEX_BIT;
536                         }
537                         if(st->opt & G3D_BLEND) {
538                                 fill_mode |= POLYFILL_BLEND_BIT;
539                         }
540                         polyfill(fill_mode, pv, vnum);
541                 }
542         }
543 }
544
545 void g3d_begin(int prim)
546 {
547         st->imm_prim = prim;
548         st->imm_pcount = prim;
549         st->imm_numv = 0;
550 }
551
552 void g3d_end(void)
553 {
554         imm_flush();
555 }
556
557 static void imm_flush(void)
558 {
559         int numv = st->imm_numv;
560         st->imm_numv = 0;
561         g3d_draw_indexed(st->imm_prim, st->imm_vbuf, numv, 0, 0);
562 }
563
564 void g3d_vertex(float x, float y, float z)
565 {
566         struct g3d_vertex *vptr = st->imm_vbuf + st->imm_numv++;
567         *vptr = st->imm_curv;
568         vptr->x = x;
569         vptr->y = y;
570         vptr->z = z;
571         vptr->w = 1.0f;
572
573         if(!--st->imm_pcount) {
574                 if(st->imm_numv >= IMM_VBUF_SIZE - st->imm_prim) {
575                         imm_flush();
576                 }
577                 st->imm_pcount = st->imm_prim;
578         }
579 }
580
581 void g3d_normal(float x, float y, float z)
582 {
583         st->imm_curv.nx = x;
584         st->imm_curv.ny = y;
585         st->imm_curv.nz = z;
586 }
587
588 #define CLAMP(x, a, b)  ((x) < (a) ? (a) : ((x) > (b) ? (b) : (x)))
589
590 void g3d_color3b(unsigned char r, unsigned char g, unsigned char b)
591 {
592         st->imm_curv.r = CLAMP(r, 0, 255);
593         st->imm_curv.g = CLAMP(g, 0, 255);
594         st->imm_curv.b = CLAMP(b, 0, 255);
595         st->imm_curv.a = 255;
596 }
597
598 void g3d_color4b(unsigned char r, unsigned char g, unsigned char b, unsigned char a)
599 {
600         st->imm_curv.r = CLAMP(r, 0, 255);
601         st->imm_curv.g = CLAMP(g, 0, 255);
602         st->imm_curv.b = CLAMP(b, 0, 255);
603         st->imm_curv.a = CLAMP(a, 0, 255);
604 }
605
606 void g3d_color3f(float r, float g, float b)
607 {
608         int ir = r * 255.0f;
609         int ig = g * 255.0f;
610         int ib = b * 255.0f;
611         st->imm_curv.r = CLAMP(ir, 0, 255);
612         st->imm_curv.g = CLAMP(ig, 0, 255);
613         st->imm_curv.b = CLAMP(ib, 0, 255);
614         st->imm_curv.a = 255;
615 }
616
617 void g3d_color4f(float r, float g, float b, float a)
618 {
619         int ir = r * 255.0f;
620         int ig = g * 255.0f;
621         int ib = b * 255.0f;
622         int ia = a * 255.0f;
623         st->imm_curv.r = CLAMP(ir, 0, 255);
624         st->imm_curv.g = CLAMP(ig, 0, 255);
625         st->imm_curv.b = CLAMP(ib, 0, 255);
626         st->imm_curv.a = CLAMP(ia, 0, 255);
627 }
628
629 void g3d_texcoord(float u, float v)
630 {
631         st->imm_curv.u = u;
632         st->imm_curv.v = v;
633 }
634
635 static void xform4_vec3(const float *mat, float *vec)
636 {
637         float x = mat[0] * vec[0] + mat[4] * vec[1] + mat[8] * vec[2] + mat[12];
638         float y = mat[1] * vec[0] + mat[5] * vec[1] + mat[9] * vec[2] + mat[13];
639         float z = mat[2] * vec[0] + mat[6] * vec[1] + mat[10] * vec[2] + mat[14];
640         float w = mat[3] * vec[0] + mat[7] * vec[1] + mat[11] * vec[2] + mat[15];
641
642         vec[0] = x;
643         vec[1] = y;
644         vec[2] = z;
645         vec[3] = w;
646 }
647
648 static void xform3_vec3(const float *mat, float *vec)
649 {
650         float x = mat[0] * vec[0] + mat[4] * vec[1] + mat[8] * vec[2];
651         float y = mat[1] * vec[0] + mat[5] * vec[1] + mat[9] * vec[2];
652         float z = mat[2] * vec[0] + mat[6] * vec[1] + mat[10] * vec[2];
653
654         vec[0] = x;
655         vec[1] = y;
656         vec[2] = z;
657 }
658
659 #define NORMALIZE(v) \
660         do { \
661                 float len = sqrt(v[0] * v[0] + v[1] * v[1] + v[2] * v[2]); \
662                 if(len != 0.0) { \
663                         float s = 1.0 / len; \
664                         v[0] *= s; \
665                         v[1] *= s; \
666                         v[2] *= s; \
667                 } \
668         } while(0)
669
670 static void shade(struct g3d_vertex *v)
671 {
672         int i, r, g, b;
673         float color[3];
674
675         color[0] = st->ambient[0] * st->mtl.kd[0];
676         color[1] = st->ambient[1] * st->mtl.kd[1];
677         color[2] = st->ambient[2] * st->mtl.kd[2];
678
679         for(i=0; i<MAX_LIGHTS; i++) {
680                 float ldir[3];
681                 float ndotl;
682
683                 if(!(st->opt & (G3D_LIGHT0 << i))) {
684                         continue;
685                 }
686
687                 ldir[0] = st->lt[i].x - v->x;
688                 ldir[1] = st->lt[i].y - v->y;
689                 ldir[2] = st->lt[i].z - v->z;
690                 NORMALIZE(ldir);
691
692                 if((ndotl = v->nx * ldir[0] + v->ny * ldir[1] + v->nz * ldir[2]) < 0.0f) {
693                         ndotl = 0.0f;
694                 }
695
696                 color[0] += st->mtl.kd[0] * st->lt[i].r * ndotl;
697                 color[1] += st->mtl.kd[1] * st->lt[i].g * ndotl;
698                 color[2] += st->mtl.kd[2] * st->lt[i].b * ndotl;
699         }
700
701         r = cround64(color[0] * 255.0);
702         g = cround64(color[1] * 255.0);
703         b = cround64(color[2] * 255.0);
704
705         v->r = r > 255 ? 255 : r;
706         v->g = g > 255 ? 255 : g;
707         v->b = b > 255 ? 255 : b;
708 }