Welcome to mirror list, hosted at ThFree Co, Russian Federation.

raskter_mt.c « raskter « intern - git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: c323a2eaa1222fc11b9860e6b06f2808a05af4a9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
/*
 * ***** BEGIN GPL LICENSE BLOCK *****
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 *
 * The Original Code is Copyright (C) 2012 Blender Foundation.
 * All rights reserved.
 *
 * The Original Code is: all of this file.
 *
 * Contributor(s): Peter Larabell.
 *
 * ***** END GPL LICENSE BLOCK *****
 */
/** \file raskter_mt.c
 *  \ingroup RASKTER
 */
#include <stdlib.h>

#include "raskter.h"
static int rast_scan_init(struct r_fill_context *ctx, struct poly_vert *verts, int num_verts) {
    int x_curr;                 /* current pixel position in X */
    int y_curr;                 /* current scan line being drawn */
    int yp;                     /* y-pixel's position in frame buffer */
    int swixd = 0;              /* whether or not edges switched position in X */
    int i=0;                    /* counter */
    float *cpxl;                /* pixel pointers... */
    float *mpxl;
    float *spxl;
    struct e_status *e_curr;    /* edge pointers... */
    struct e_status *e_temp;
    struct e_status *edgbuf;
    struct e_status **edgec;


    if(num_verts < 3) {
        return(1);
    }

    if((edgbuf = (struct e_status *)(malloc(sizeof(struct e_status) * num_verts))) == NULL) {
        return(0);
    }

    /* set initial bounds length to 0 */
    ctx->bounds_length=0;

    /* round 1, count up all the possible spans in the base buffer */
    preprocess_all_edges(ctx, verts, num_verts, edgbuf);

    /* can happen with a zero area mask */
    if (ctx->all_edges == NULL) {
        free(edgbuf);
        return(1);
    }
    ctx->possible_edges = NULL;

    for(y_curr = ctx->all_edges->ybeg; (ctx->all_edges || ctx->possible_edges); y_curr++) {

        for(edgec = &ctx->possible_edges; ctx->all_edges && (ctx->all_edges->ybeg == y_curr);) {
            x_curr = ctx->all_edges->x;                  /* Set current X position. */
            for(;;) {                                    /* Start looping edges. Will break when edges run out. */
                e_curr = *edgec;                         /* Set up a current edge pointer. */
                if(!e_curr || (e_curr->x >= x_curr)) {   /* If we have an no edge, or we need to skip some X-span, */
                    e_temp = ctx->all_edges->e_next;     /* set a temp "next" edge to test. */
                    *edgec = ctx->all_edges;             /* Add this edge to the list to be scanned. */
                    ctx->all_edges->e_next = e_curr;     /* Set up the next edge. */
                    edgec = &ctx->all_edges->e_next;     /* Set our list to the next edge's location in memory. */
                    ctx->all_edges = e_temp;             /* Skip the NULL or bad X edge, set pointer to next edge. */
                    break;                               /* Stop looping edges (since we ran out or hit empty X span. */
                } else {
                    edgec = &e_curr->e_next;             /* Set the pointer to the edge list the "next" edge. */
                }
            }
        }

        yp = y_curr * ctx->rb.sizex;
        spxl = ctx->rb.buf + (yp);

        for(e_curr = ctx->possible_edges; e_curr; e_curr = e_curr->e_next) {

            /* set up xmin and xmax bounds on this scan line */
            cpxl = spxl + MAX2(e_curr->x, 0);
            e_curr = e_curr->e_next;
            mpxl = spxl + MIN2(e_curr->x, ctx->rb.sizex) - 1;

            if((y_curr >= 0) && (y_curr < ctx->rb.sizey)) {
                ctx->bounds_length++;
            }
        }

        for(edgec = &ctx->possible_edges; (e_curr = *edgec);) {
            if(!(--(e_curr->num))) {
                *edgec = e_curr->e_next;
            } else {
                e_curr->x += e_curr->xshift;
                if((e_curr->drift += e_curr->drift_inc) > 0) {
                    e_curr->x += e_curr->xdir;
                    e_curr->drift -= e_curr->drift_dec;
                }
                edgec = &e_curr->e_next;
            }
        }
        if(ctx->possible_edges) {
            for(edgec = &ctx->possible_edges; (e_curr = *edgec)->e_next; edgec = &(*edgec)->e_next) {
                /* if the current edge hits scan line at greater X than the next edge, we need to exchange the edges */
                if(e_curr->x > e_curr->e_next->x) {
                    *edgec = e_curr->e_next;
                    /* exchange the pointers */
                    e_temp = e_curr->e_next->e_next;
                    e_curr->e_next->e_next = e_curr;
                    e_curr->e_next = e_temp;
                    /* set flag that we had at least one switch */
                    swixd = 1;
                }
            }
            /* if we did have a switch, look for more (there will more if there was one) */
            for(;;) {
                /* reset exchange flag so it's only set if we encounter another one */
                swixd = 0;
                for(edgec = &ctx->possible_edges; (e_curr = *edgec)->e_next; edgec = &(*edgec)->e_next) {
                    /* again, if current edge hits scan line at higher X than next edge, exchange the edges and set flag */
                    if(e_curr->x > e_curr->e_next->x) {
                        *edgec = e_curr->e_next;
                        /* exchange the pointers */
                        e_temp = e_curr->e_next->e_next;
                        e_curr->e_next->e_next = e_curr;
                        e_curr->e_next = e_temp;
                        /* flip the exchanged flag */
                        swixd = 1;
                    }
                }
                /* if we had no exchanges, we're done reshuffling the pointers */
                if(!swixd) {
                    break;
                }
            }
        }
    }


/*initialize index buffer and bounds buffers*/
    //gets the +1 for dummy at the end
    if((ctx->bound_indexes = (int *)(malloc(sizeof(int) * ctx->rb.sizey+1)))==NULL) {
        return(0);
    }
    //gets the +1 for dummy at the start
    if((ctx->bounds = (struct scan_line *)(malloc(sizeof(struct scan_line) * ctx->bounds_length+1)))==NULL){
        return(0);
    }
    //init all the indexes to zero (are they already zeroed from malloc???)
    for(i=0;i<ctx->rb.sizey+1;i++){
        ctx->bound_indexes[i]=0;
    }
    /* round 2, fill in the full list of bounds, and create indexes to the list... */
    preprocess_all_edges(ctx, verts, num_verts, edgbuf);

    /* can happen with a zero area mask */
    if (ctx->all_edges == NULL) {
        free(edgbuf);
        return(1);
    }
    ctx->possible_edges = NULL;

    /* restart i as a counter for total span placement in buffer */
    i=1;
    for(y_curr = ctx->all_edges->ybeg; (ctx->all_edges || ctx->possible_edges); y_curr++) {

        for(edgec = &ctx->possible_edges; ctx->all_edges && (ctx->all_edges->ybeg == y_curr);) {
            x_curr = ctx->all_edges->x;                  /* Set current X position. */
            for(;;) {                                    /* Start looping edges. Will break when edges run out. */
                e_curr = *edgec;                         /* Set up a current edge pointer. */
                if(!e_curr || (e_curr->x >= x_curr)) {   /* If we have an no edge, or we need to skip some X-span, */
                    e_temp = ctx->all_edges->e_next;     /* set a temp "next" edge to test. */
                    *edgec = ctx->all_edges;             /* Add this edge to the list to be scanned. */
                    ctx->all_edges->e_next = e_curr;     /* Set up the next edge. */
                    edgec = &ctx->all_edges->e_next;     /* Set our list to the next edge's location in memory. */
                    ctx->all_edges = e_temp;             /* Skip the NULL or bad X edge, set pointer to next edge. */
                    break;                               /* Stop looping edges (since we ran out or hit empty X span. */
                } else {
                    edgec = &e_curr->e_next;             /* Set the pointer to the edge list the "next" edge. */
                }
            }
        }

        yp = y_curr * ctx->rb.sizex;
        spxl = ctx->rb.buf + (yp);
        if((y_curr >=0) && (y_curr < ctx->rb.sizey)){
            ctx->bound_indexes[y_curr]=i;
        }
        for(e_curr = ctx->possible_edges; e_curr; e_curr = e_curr->e_next) {

            /* set up xmin and xmax bounds on this scan line */
            cpxl = spxl + MAX2(e_curr->x, 0);
            e_curr = e_curr->e_next;
            mpxl = spxl + MIN2(e_curr->x, ctx->rb.sizex) - 1;

            if((y_curr >= 0) && (y_curr < ctx->rb.sizey)) {
                ctx->bounds[i].xstart=cpxl-spxl;
                ctx->bounds[i].xend=mpxl-spxl;
                i++;
            }
        }

        for(edgec = &ctx->possible_edges; (e_curr = *edgec);) {
            if(!(--(e_curr->num))) {
                *edgec = e_curr->e_next;
            } else {
                e_curr->x += e_curr->xshift;
                if((e_curr->drift += e_curr->drift_inc) > 0) {
                    e_curr->x += e_curr->xdir;
                    e_curr->drift -= e_curr->drift_dec;
                }
                edgec = &e_curr->e_next;
            }
        }
        if(ctx->possible_edges) {
            for(edgec = &ctx->possible_edges; (e_curr = *edgec)->e_next; edgec = &(*edgec)->e_next) {
                /* if the current edge hits scan line at greater X than the next edge, we need to exchange the edges */
                if(e_curr->x > e_curr->e_next->x) {
                    *edgec = e_curr->e_next;
                    /* exchange the pointers */
                    e_temp = e_curr->e_next->e_next;
                    e_curr->e_next->e_next = e_curr;
                    e_curr->e_next = e_temp;
                    /* set flag that we had at least one switch */
                    swixd = 1;
                }
            }
            /* if we did have a switch, look for more (there will more if there was one) */
            for(;;) {
                /* reset exchange flag so it's only set if we encounter another one */
                swixd = 0;
                for(edgec = &ctx->possible_edges; (e_curr = *edgec)->e_next; edgec = &(*edgec)->e_next) {
                    /* again, if current edge hits scan line at higher X than next edge, exchange the edges and set flag */
                    if(e_curr->x > e_curr->e_next->x) {
                        *edgec = e_curr->e_next;
                        /* exchange the pointers */
                        e_temp = e_curr->e_next->e_next;
                        e_curr->e_next->e_next = e_curr;
                        e_curr->e_next = e_temp;
                        /* flip the exchanged flag */
                        swixd = 1;
                    }
                }
                /* if we had no exchanges, we're done reshuffling the pointers */
                if(!swixd) {
                    break;
                }
            }
        }
    }

    free(edgbuf);
    return 1;
}

/* static */ int init_base_data(float(*base_verts)[2], int num_base_verts,
                   float *buf, int buf_x, int buf_y) {
    int i;                                   /* i: Loop counter. */
    struct poly_vert *ply;                   /* ply: Pointer to a list of integer buffer-space vertex coordinates. */
    struct r_fill_context ctx = {0};
    const float buf_x_f = (float)(buf_x);
    const float buf_y_f = (float)(buf_y);
    if((ply = (struct poly_vert *)(malloc(sizeof(struct poly_vert) * num_base_verts))) == NULL) {
        return(0);
    }
    ctx.rb.buf = buf;                            /* Set the output buffer pointer. */
    ctx.rb.sizex = buf_x;                        /* Set the output buffer size in X. (width) */
    ctx.rb.sizey = buf_y;                        /* Set the output buffer size in Y. (height) */
    for(i = 0; i < num_base_verts; i++) {                           /* Loop over all base_verts. */
        ply[i].x = (int)((base_verts[i][0] * buf_x_f) + 0.5f);       /* Range expand normalized X to integer buffer-space X. */
        ply[i].y = (int)((base_verts[i][1] * buf_y_f) + 0.5f); /* Range expand normalized Y to integer buffer-space Y. */
    }
    i = rast_scan_init(&ctx, ply, num_base_verts);  /* Call our rasterizer, passing in the integer coords for each vert. */
    free(ply);                                      /* Free the memory allocated for the integer coordinate table. */
    return(i);                                      /* Return the value returned by the rasterizer. */
}