Debian Codegraph @

debian/external_deps/fetk/sg/src/gl/interp.c

raw
1 /* $Id: interp.c,v 1.2 2000/10/27 15:21:40 mholst Exp $ */
2
3 /*
4 * Mesa 3-D graphics library
5 * Version: 2.0
6 * Copyright (C) 1995-1996 Brian Paul
7 *
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Library General Public
10 * License as published by the Free Software Foundation; either
11 * version 2 of the License, or (at your option) any later version.
12 *
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Library General Public License for more details.
17 *
18 * You should have received a copy of the GNU Library General Public
19 * License along with this library; if not, write to the Free
20 * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 */
22
23 #include "interp.h"
24 #include "macros.h"
25 #include "types.h"
26
27
28
29 /*
30 * Linear integer interpolation of Z (depth) values:
31 * Iterpolate n integer values between z0 and z1 and put into zspan.
32 * When finished, zspan[0] = z0, zspan[n-1] = z1, and the rest of
33 * zspan[] is filled with interpolated values.
34 * We have to be careful to avoid integer overflow!
35 */
36 void gl_interpolate_z( GLint n, GLint z0, GLint z1, GLdepth zspan[] )
37 {
38 GLint i, dz;
39
40 switch (n) {
41 case 1:
42 zspan[0] = z0;
43 return;
44 case 2:
45 zspan[0] = z0;
46 zspan[1] = z1;
47 return;
48 case 3:
49 zspan[0] = z0;
50 zspan[1] = (z0 + z1) >> 1;
51 zspan[2] = z1;
52 return;
53 default:
54 z0 = z0 << 7;
55 z1 = z1 << 7;
56 dz = (z1-z0) / (n-1);
57 for (i=0;i<n;i++) {
58 zspan[i] = z0 >> 7;
59 z0 += dz;
60 }
61 return;
62 }
63 }
64
65
66
67 /*
68 * Linear integer interpolation:
69 * Iterpolate n integer values between y0 and y1 and put into yspan.
70 * When finished, yspan[0] = y0, yspan[n-1] = y1, and the rest of
71 * yspan[] is filled with interpolated values.
72 */
73 void gl_interpolate_i( GLint n, GLint y0, GLint y1, GLint yspan[] )
74 {
75 switch (n) {
76 case 1:
77 yspan[0] = y0;
78 return;
79 case 2:
80 yspan[0] = y0;
81 yspan[1] = y1;
82 return;
83 case 3:
84 yspan[0] = y0;
85 yspan[1] = (y0+y1) >> 1;
86 yspan[2] = y1;
87 return;
88 default:
89 if (y0==y1) {
90 register GLint i;
91 for (i=0;i<n;i++) {
92 yspan[i] = y0;
93 }
94 }
95 else {
96 register GLint i;
97 register GLint dx, dy;
98 register GLint a, b, d;
99 register GLint y;
100 register GLint qa, qb;
101 dx = n-1;
102 dy = y1 - y0;
103 qa = dy / dx;
104 dy = dy % dx;
105 if (dy<0) {
106 dy = -dy;
107 qb = qa - 1;
108 }
109 else {
110 qb = qa + 1;
111 }
112 a = dy+dy; d = a-dx; b = d-dx;
113 y = y0;
114 for (i=0;i<n;i++) {
115 yspan[i] = y;
116 if (d<0) {
117 d += a;
118 y += qa;
119 }
120 else {
121 d += b;
122 y += qb;
123 }
124 }
125 }
126 }
127 }
128
129
130
131 /*
132 * Interpolate RGBA values.
133 * Input: n - number of values to generate
134 * r0, r1 - first and last alpha values
135 * g0, g1 - first and last alpha values
136 * b0, b1 - first and last alpha values
137 * a0, a1 - first and last alpha values
138 * Output: rspan, gspan, bspan, aspan - interpolated color values in
139 * the range [0,CC.RedScale], [0,CC.GreenScale], [0,CC.BlueScale],
140 * and [0,CC.AlphaScale].
141 */
142 void gl_interpolate_rgba( GLint n,
143 GLfixed r0, GLfixed r1, GLubyte rspan[],
144 GLfixed g0, GLfixed g1, GLubyte gspan[],
145 GLfixed b0, GLfixed b1, GLubyte bspan[],
146 GLfixed a0, GLfixed a1, GLubyte aspan[] )
147 {
148 GLint i, m;
149 GLfixed dr, dg, db, da;
150
151 switch (n) {
152 case 1:
153 rspan[0] = FixedToInt(r0);
154 gspan[0] = FixedToInt(g0);
155 bspan[0] = FixedToInt(b0);
156 aspan[0] = FixedToInt(a0);
157 return;
158 case 2:
159 rspan[0] = FixedToInt(r0); rspan[1] = FixedToInt(r1);
160 gspan[0] = FixedToInt(g0); gspan[1] = FixedToInt(g1);
161 bspan[0] = FixedToInt(b0); bspan[1] = FixedToInt(b1);
162 aspan[0] = FixedToInt(a0); aspan[1] = FixedToInt(a1);
163 return;
164 default:
165 m = n-1;
166 dr = (r1-r0) / m;
167 dg = (g1-g0) / m;
168 db = (b1-b0) / m;
169 da = (a1-a0) / m;
170 for (i=0;i<n;i++) {
171 rspan[i] = FixedToInt(r0); r0 += dr;
172 gspan[i] = FixedToInt(g0); g0 += dg;
173 bspan[i] = FixedToInt(b0); b0 += db;
174 aspan[i] = FixedToInt(a0); a0 += da;
175 }
176 return;
177 }
178 }
179