Debian Codegraph @

generator/pamtris/framebuffer.c

raw
1 /*=============================================================================
2 framebuffer.c
3 ===============================================================================
4 Frame buffer functions
5
6 Every drawing operation is applied on an internal "frame buffer", which is
7 simply an "image buffer" which represents the picture currently being drawn,
8 along with a "Z-Buffer" which contains the depth values for every pixel in
9 the image buffer. Once all desired drawing operations for a particular
10 picture are effected, a function is provided to print the current contents
11 of the image buffer as a PAM image on standard output. Another function is
12 provided to clear the contents of the frame buffer (i. e. set all image
13 samples and Z-Buffer entries to 0), with the option of only clearing either
14 the image buffer or the Z-Buffer individually.
15
16 The Z-Buffer works as follows: Every pixel in the image buffer has a
17 corresponding entry in the Z-Buffer. Initially, every entry in the Z-Buffer
18 is set to 0. Every time we desire to plot a pixel at some particular
19 position in the frame buffer, the current value of the corresponding entry
20 in the Z-Buffer is compared against the the Z component of the incoming
21 pixel. If MAX_Z minus the value of the Z component of the incoming pixel is
22 equal to or greater than the current value of the corresponding entry in the
23 Z-Buffer, the frame buffer is changed as follows:
24
25 1. All the samples but the last of the corresponding position in the
26 image buffer are set to equal those of the incoming pixel.
27
28 2. The last sample, that is, the A-component of the corresponding position
29 in the image buffer is set to equal the maxval.
30
31 3. The corresponding entry in the Z-Buffer is set to equal MAX_Z minus the
32 value of the Z component of the incoming pixel.
33
34 Otherwise, no changes are made on the frame buffer.
35 =============================================================================*/
36 #include <stdbool.h>
37 #include <stdint.h>
38 #include <stdlib.h>
39 #include <string.h>
40 #include <math.h>
41
42 #include "utils.h"
43 #include "varying.h"
44 #include "limits_pamtris.h"
45
46 #include "framebuffer.h"
47
48
49
50 int
51 set_tupletype(const char * const str,
52 char * const tupletype) {
53 /*----------------------------------------------------------------------------
54 Set the tuple type for the output PAM images given a string ("str") of 255
55 characters or less. If the string has more than 255 characters, the function
56 returns 0. Otherwise, it returns 1. If NULL is given for the "str" argument,
57 the tuple type is set to a null string. This function is called during
58 program initialization and whenever a "r" command is executed. The second
59 argument must point to the tuple_type member of the "outpam" field in the
60 framebuffer_info struct.
61 -----------------------------------------------------------------------------*/
62 if (str == NULL) {
63 memset(tupletype, 0, 256);
64 } else {
65 size_t len;
66
67 len = strlen(str); /* initial value */
68
69 if (len > 255) {
70 return 0;
71 }
72
73 if (len > 0) {
74 memcpy(tupletype, str, len);
75 }
76
77 tupletype[len--] = '\0';
78
79 while(len > 0 && isspace(tupletype[len])) {
80 tupletype[len--] = '\0';
81 }
82 }
83
84 return 1;
85 }
86
87
88
89 int
90 init_framebuffer(framebuffer_info * const fbi) {
91
92 uint8_t const num_planes = fbi->num_attribs + 1;
93
94 uint32_t const elements = fbi->width * fbi->height;
95
96 fbi->img.bytes = elements * (num_planes * sizeof(uint16_t));
97 fbi->z.bytes = elements * sizeof(uint32_t);
98
99 fbi->img.buffer =
100 calloc(fbi->img.bytes / sizeof(uint16_t), sizeof(uint16_t));
101 fbi->z.buffer =
102 calloc(fbi->z.bytes / sizeof(uint32_t), sizeof(uint32_t));
103
104 if(fbi->img.buffer == NULL || fbi->z.buffer == NULL) {
105 free(fbi->img.buffer);
106 free(fbi->z.buffer);
107
108 return 0;
109 }
110
111 fbi->outpam.size = sizeof(struct pam);
112 fbi->outpam.len = sizeof(struct pam);
113 fbi->outpam.file = stdout;
114 fbi->outpam.format = PAM_FORMAT;
115 fbi->outpam.plainformat = 0;
116 fbi->outpam.height = fbi->height;
117 fbi->outpam.width = fbi->width;
118 fbi->outpam.depth = num_planes;
119 fbi->outpam.maxval = fbi->maxval;
120 fbi->outpam.allocation_depth = 0;
121 fbi->outpam.comment_p = NULL;
122
123 fbi->pamrow = NULL;
124 fbi->pamrow = pnm_allocpamrow(&fbi->outpam);
125
126 if (fbi->pamrow == NULL) {
127 free(fbi->img.buffer);
128 free(fbi->z.buffer);
129
130 return 0;
131 }
132
133 return 1;
134 }
135
136
137
138 void
139 free_framebuffer(framebuffer_info * const fbi) {
140
141 free(fbi->img.buffer);
142 free(fbi->z.buffer);
143
144 pnm_freepamrow(fbi->pamrow);
145 }
146
147
148
149 int
150 realloc_image_buffer(int32_t const new_maxval,
151 int32_t const new_num_attribs,
152 framebuffer_info * const fbi) {
153 /*----------------------------------------------------------------------------
154 Reallocate the image buffer with a new maxval and depth, given the struct
155 with information about the framebuffer. The fields variables "maxval" and
156 "num_attribs".
157
158 From the point this function is called onwards, new PAM images printed on
159 standard output will have the new maxval for the maxval and num_attribs + 1
160 for the depth.
161
162 This function does *not* check whether the new maxval and num_attribs are
163 within the proper allowed limits. That is done inside the input processing
164 function "process_next_command", which is the only function that calls this
165 one.
166
167 If the function succeeds, the image buffer is left in cleared state. The
168 Z-Buffer, however, is not touched at all.
169
170 If the new depth is equal to the previous one, no actual reallocation is
171 performed: only the global variable "maxval" is changed. But the image
172 buffer is nonetheless left in cleared state regardless.
173 -----------------------------------------------------------------------------*/
174 uint8_t num_planes;
175
176 pnm_freepamrow(fbi->pamrow);
177 fbi->pamrow = NULL;
178
179 num_planes = fbi->num_attribs + 1; /* initial value */
180
181 if (new_num_attribs != fbi->num_attribs) {
182 fbi->num_attribs = new_num_attribs;
183 num_planes = fbi->num_attribs + 1;
184
185 fbi->img.bytes =
186 fbi->width * fbi->height * (num_planes * sizeof(uint16_t));
187
188 {
189 uint16_t * const new_ptr =
190 realloc(fbi->img.buffer, fbi->img.bytes);
191
192 if (new_ptr == NULL) {
193 free(fbi->img.buffer);
194 fbi->img.buffer = NULL;
195
196 return 0;
197 }
198 fbi->img.buffer = new_ptr;
199 }
200 }
201
202 fbi->maxval = new_maxval;
203
204 fbi->outpam.size = sizeof(struct pam);
205 fbi->outpam.len = sizeof(struct pam);
206 fbi->outpam.file = stdout;
207 fbi->outpam.format = PAM_FORMAT;
208 fbi->outpam.plainformat = 0;
209 fbi->outpam.height = fbi->height;
210 fbi->outpam.width = fbi->width;
211 fbi->outpam.depth = num_planes;
212 fbi->outpam.maxval = fbi->maxval;
213 fbi->outpam.allocation_depth = 0;
214 fbi->outpam.comment_p = NULL;
215
216 fbi->pamrow = pnm_allocpamrow(&fbi->outpam);
217
218 if (fbi->pamrow == NULL) {
219 free(fbi->img.buffer);
220 fbi->img.buffer = NULL;
221
222 return 0;
223 }
224
225 memset(fbi->img.buffer, 0, fbi->img.bytes);
226
227 return 1;
228 }
229
230
231
232 void
233 print_framebuffer(framebuffer_info * const fbi) {
234
235 uint8_t const num_planes = fbi->num_attribs + 1;
236 uint32_t const end = fbi->width * fbi->height;
237
238 uint32_t i;
239
240 pnm_writepaminit(&fbi->outpam);
241
242 for (i = 0; i != end; ) {
243 int j;
244 for (j = 0; j < fbi->width; j++) {
245 uint32_t const k = (i + j) * num_planes;
246
247 unsigned int l;
248
249 for (l = 0; l < num_planes; l++) {
250 fbi->pamrow[j][l] = fbi->img.buffer[k + l];
251 }
252 }
253
254 pnm_writepamrow(&fbi->outpam, fbi->pamrow);
255
256 i += fbi->width;
257 }
258 }
259
260
261
262 void
263 clear_framebuffer(bool const clear_image_buffer,
264 bool const clear_z_buffer,
265 framebuffer_info * const fbi) {
266
267 if (clear_image_buffer) {
268 memset(fbi->img.buffer, 0, fbi->img.bytes);
269 }
270
271 if (clear_z_buffer) {
272 memset(fbi->z.buffer, 0, fbi->z.bytes);
273 }
274 }
275
276
277
278 void
279 draw_span(uint32_t const base,
280 uint16_t const length,
281 varying * const attribs,
282 framebuffer_info * const fbi) {
283 /*----------------------------------------------------------------------------
284 Draw a horizontal span of "length" pixels into the frame buffer, performing
285 the appropriate depth tests. "base" must equal the row of the frame buffer
286 where one desires to draw the span *times* the image width, plus the column
287 of the first pixel in the span.
288
289 This function does not perform any kind of bounds checking.
290 -----------------------------------------------------------------------------*/
291 static double const depth_range = MAX_Z;
292
293 uint16_t const maxval = fbi->maxval;
294 uint8_t const z = fbi->num_attribs;
295 uint8_t const w = z + 1;
296 uint8_t const n = w + 1;
297
298 uint8_t const num_planes = w;
299
300 unsigned int i;
301
302 /* Process each pixel in the span: */
303
304 for (i = 0; i < length; i++) {
305 int32_t const d = round(depth_range * attribs[z].v);
306 uint32_t const d_mask = geq_mask64(d, fbi->z.buffer[base + i]);
307
308 uint32_t const j = base + i;
309 uint32_t const k = j * num_planes;
310
311 varying const inverse_w = inverse_varying(attribs[w]);
312
313 unsigned int l;
314
315 /* The following statements will only have any effect if the depth
316 test, performed above, has succeeded. I. e. if the depth test fails,
317 no changes will be made on the frame buffer; otherwise, the
318 frame buffer will be updated with the new values.
319 */
320 fbi->z.buffer[j] = (fbi->z.buffer[j] & ~d_mask) | (d & d_mask);
321
322 for (l = 0; l < z; l++) {
323 varying const newval = multiply_varyings(attribs[l], inverse_w);
324
325 fbi->img.buffer[k + l] =
326 (fbi->img.buffer[k + l] & ~d_mask) |
327 (round_varying(newval) & d_mask);
328 }
329
330 fbi->img.buffer[k + z] |= (maxval & d_mask);
331
332 /* Compute the attribute values for the next pixel: */
333
334 step_up(attribs, n);
335 }
336 }
337
338
339