Sam Spilsbury | 619859c | 2013-09-13 10:01:21 +0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright © 2012 Intel Corporation |
| 3 | * |
| 4 | * Permission to use, copy, modify, distribute, and sell this software and |
| 5 | * its documentation for any purpose is hereby granted without fee, provided |
| 6 | * that the above copyright notice appear in all copies and that both that |
| 7 | * copyright notice and this permission notice appear in supporting |
| 8 | * documentation, and that the name of the copyright holders not be used in |
| 9 | * advertising or publicity pertaining to distribution of the software |
| 10 | * without specific, written prior permission. The copyright holders make |
| 11 | * no representations about the suitability of this software for any |
| 12 | * purpose. It is provided "as is" without express or implied warranty. |
| 13 | * |
| 14 | * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS |
| 15 | * SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND |
| 16 | * FITNESS, IN NO EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY |
| 17 | * SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER |
| 18 | * RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF |
| 19 | * CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN |
| 20 | * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |
| 21 | */ |
| 22 | #include <assert.h> |
| 23 | #include <float.h> |
| 24 | #include <math.h> |
| 25 | |
| 26 | #include <GLES2/gl2.h> |
| 27 | |
| 28 | #include "vertex-clipping.h" |
| 29 | |
| 30 | GLfloat |
| 31 | float_difference(GLfloat a, GLfloat b) |
| 32 | { |
| 33 | /* http://www.altdevblogaday.com/2012/02/22/comparing-floating-point-numbers-2012-edition/ */ |
| 34 | static const GLfloat max_diff = 4.0f * FLT_MIN; |
| 35 | static const GLfloat max_rel_diff = 4.0e-5; |
| 36 | GLfloat diff = a - b; |
| 37 | GLfloat adiff = fabsf(diff); |
| 38 | |
| 39 | if (adiff <= max_diff) |
| 40 | return 0.0f; |
| 41 | |
| 42 | a = fabsf(a); |
| 43 | b = fabsf(b); |
| 44 | if (adiff <= (a > b ? a : b) * max_rel_diff) |
| 45 | return 0.0f; |
| 46 | |
| 47 | return diff; |
| 48 | } |
| 49 | |
| 50 | /* A line segment (p1x, p1y)-(p2x, p2y) intersects the line x = x_arg. |
| 51 | * Compute the y coordinate of the intersection. |
| 52 | */ |
| 53 | static GLfloat |
| 54 | clip_intersect_y(GLfloat p1x, GLfloat p1y, GLfloat p2x, GLfloat p2y, |
| 55 | GLfloat x_arg) |
| 56 | { |
| 57 | GLfloat a; |
| 58 | GLfloat diff = float_difference(p1x, p2x); |
| 59 | |
| 60 | /* Practically vertical line segment, yet the end points have already |
| 61 | * been determined to be on different sides of the line. Therefore |
| 62 | * the line segment is part of the line and intersects everywhere. |
| 63 | * Return the end point, so we use the whole line segment. |
| 64 | */ |
| 65 | if (diff == 0.0f) |
| 66 | return p2y; |
| 67 | |
| 68 | a = (x_arg - p2x) / diff; |
| 69 | return p2y + (p1y - p2y) * a; |
| 70 | } |
| 71 | |
| 72 | /* A line segment (p1x, p1y)-(p2x, p2y) intersects the line y = y_arg. |
| 73 | * Compute the x coordinate of the intersection. |
| 74 | */ |
| 75 | static GLfloat |
| 76 | clip_intersect_x(GLfloat p1x, GLfloat p1y, GLfloat p2x, GLfloat p2y, |
| 77 | GLfloat y_arg) |
| 78 | { |
| 79 | GLfloat a; |
| 80 | GLfloat diff = float_difference(p1y, p2y); |
| 81 | |
| 82 | /* Practically horizontal line segment, yet the end points have already |
| 83 | * been determined to be on different sides of the line. Therefore |
| 84 | * the line segment is part of the line and intersects everywhere. |
| 85 | * Return the end point, so we use the whole line segment. |
| 86 | */ |
| 87 | if (diff == 0.0f) |
| 88 | return p2x; |
| 89 | |
| 90 | a = (y_arg - p2y) / diff; |
| 91 | return p2x + (p1x - p2x) * a; |
| 92 | } |
| 93 | |
| 94 | enum path_transition { |
| 95 | PATH_TRANSITION_OUT_TO_OUT = 0, |
| 96 | PATH_TRANSITION_OUT_TO_IN = 1, |
| 97 | PATH_TRANSITION_IN_TO_OUT = 2, |
| 98 | PATH_TRANSITION_IN_TO_IN = 3, |
| 99 | }; |
| 100 | |
| 101 | static void |
| 102 | clip_append_vertex(struct clip_context *ctx, GLfloat x, GLfloat y) |
| 103 | { |
| 104 | *ctx->vertices.x++ = x; |
| 105 | *ctx->vertices.y++ = y; |
| 106 | } |
| 107 | |
| 108 | static enum path_transition |
| 109 | path_transition_left_edge(struct clip_context *ctx, GLfloat x, GLfloat y) |
| 110 | { |
| 111 | return ((ctx->prev.x >= ctx->clip.x1) << 1) | (x >= ctx->clip.x1); |
| 112 | } |
| 113 | |
| 114 | static enum path_transition |
| 115 | path_transition_right_edge(struct clip_context *ctx, GLfloat x, GLfloat y) |
| 116 | { |
| 117 | return ((ctx->prev.x < ctx->clip.x2) << 1) | (x < ctx->clip.x2); |
| 118 | } |
| 119 | |
| 120 | static enum path_transition |
| 121 | path_transition_top_edge(struct clip_context *ctx, GLfloat x, GLfloat y) |
| 122 | { |
| 123 | return ((ctx->prev.y >= ctx->clip.y1) << 1) | (y >= ctx->clip.y1); |
| 124 | } |
| 125 | |
| 126 | static enum path_transition |
| 127 | path_transition_bottom_edge(struct clip_context *ctx, GLfloat x, GLfloat y) |
| 128 | { |
| 129 | return ((ctx->prev.y < ctx->clip.y2) << 1) | (y < ctx->clip.y2); |
| 130 | } |
| 131 | |
| 132 | static void |
| 133 | clip_polygon_leftright(struct clip_context *ctx, |
| 134 | enum path_transition transition, |
| 135 | GLfloat x, GLfloat y, GLfloat clip_x) |
| 136 | { |
| 137 | GLfloat yi; |
| 138 | |
| 139 | switch (transition) { |
| 140 | case PATH_TRANSITION_IN_TO_IN: |
| 141 | clip_append_vertex(ctx, x, y); |
| 142 | break; |
| 143 | case PATH_TRANSITION_IN_TO_OUT: |
| 144 | yi = clip_intersect_y(ctx->prev.x, ctx->prev.y, x, y, clip_x); |
| 145 | clip_append_vertex(ctx, clip_x, yi); |
| 146 | break; |
| 147 | case PATH_TRANSITION_OUT_TO_IN: |
| 148 | yi = clip_intersect_y(ctx->prev.x, ctx->prev.y, x, y, clip_x); |
| 149 | clip_append_vertex(ctx, clip_x, yi); |
| 150 | clip_append_vertex(ctx, x, y); |
| 151 | break; |
| 152 | case PATH_TRANSITION_OUT_TO_OUT: |
| 153 | /* nothing */ |
| 154 | break; |
| 155 | default: |
| 156 | assert(0 && "bad enum path_transition"); |
| 157 | } |
| 158 | |
| 159 | ctx->prev.x = x; |
| 160 | ctx->prev.y = y; |
| 161 | } |
| 162 | |
| 163 | static void |
| 164 | clip_polygon_topbottom(struct clip_context *ctx, |
| 165 | enum path_transition transition, |
| 166 | GLfloat x, GLfloat y, GLfloat clip_y) |
| 167 | { |
| 168 | GLfloat xi; |
| 169 | |
| 170 | switch (transition) { |
| 171 | case PATH_TRANSITION_IN_TO_IN: |
| 172 | clip_append_vertex(ctx, x, y); |
| 173 | break; |
| 174 | case PATH_TRANSITION_IN_TO_OUT: |
| 175 | xi = clip_intersect_x(ctx->prev.x, ctx->prev.y, x, y, clip_y); |
| 176 | clip_append_vertex(ctx, xi, clip_y); |
| 177 | break; |
| 178 | case PATH_TRANSITION_OUT_TO_IN: |
| 179 | xi = clip_intersect_x(ctx->prev.x, ctx->prev.y, x, y, clip_y); |
| 180 | clip_append_vertex(ctx, xi, clip_y); |
| 181 | clip_append_vertex(ctx, x, y); |
| 182 | break; |
| 183 | case PATH_TRANSITION_OUT_TO_OUT: |
| 184 | /* nothing */ |
| 185 | break; |
| 186 | default: |
| 187 | assert(0 && "bad enum path_transition"); |
| 188 | } |
| 189 | |
| 190 | ctx->prev.x = x; |
| 191 | ctx->prev.y = y; |
| 192 | } |
| 193 | |
| 194 | static void |
| 195 | clip_context_prepare(struct clip_context *ctx, const struct polygon8 *src, |
| 196 | GLfloat *dst_x, GLfloat *dst_y) |
| 197 | { |
| 198 | ctx->prev.x = src->x[src->n - 1]; |
| 199 | ctx->prev.y = src->y[src->n - 1]; |
| 200 | ctx->vertices.x = dst_x; |
| 201 | ctx->vertices.y = dst_y; |
| 202 | } |
| 203 | |
| 204 | static int |
| 205 | clip_polygon_left(struct clip_context *ctx, const struct polygon8 *src, |
| 206 | GLfloat *dst_x, GLfloat *dst_y) |
| 207 | { |
| 208 | enum path_transition trans; |
| 209 | int i; |
| 210 | |
| 211 | clip_context_prepare(ctx, src, dst_x, dst_y); |
| 212 | for (i = 0; i < src->n; i++) { |
| 213 | trans = path_transition_left_edge(ctx, src->x[i], src->y[i]); |
| 214 | clip_polygon_leftright(ctx, trans, src->x[i], src->y[i], |
| 215 | ctx->clip.x1); |
| 216 | } |
| 217 | return ctx->vertices.x - dst_x; |
| 218 | } |
| 219 | |
| 220 | static int |
| 221 | clip_polygon_right(struct clip_context *ctx, const struct polygon8 *src, |
| 222 | GLfloat *dst_x, GLfloat *dst_y) |
| 223 | { |
| 224 | enum path_transition trans; |
| 225 | int i; |
| 226 | |
| 227 | clip_context_prepare(ctx, src, dst_x, dst_y); |
| 228 | for (i = 0; i < src->n; i++) { |
| 229 | trans = path_transition_right_edge(ctx, src->x[i], src->y[i]); |
| 230 | clip_polygon_leftright(ctx, trans, src->x[i], src->y[i], |
| 231 | ctx->clip.x2); |
| 232 | } |
| 233 | return ctx->vertices.x - dst_x; |
| 234 | } |
| 235 | |
| 236 | static int |
| 237 | clip_polygon_top(struct clip_context *ctx, const struct polygon8 *src, |
| 238 | GLfloat *dst_x, GLfloat *dst_y) |
| 239 | { |
| 240 | enum path_transition trans; |
| 241 | int i; |
| 242 | |
| 243 | clip_context_prepare(ctx, src, dst_x, dst_y); |
| 244 | for (i = 0; i < src->n; i++) { |
| 245 | trans = path_transition_top_edge(ctx, src->x[i], src->y[i]); |
| 246 | clip_polygon_topbottom(ctx, trans, src->x[i], src->y[i], |
| 247 | ctx->clip.y1); |
| 248 | } |
| 249 | return ctx->vertices.x - dst_x; |
| 250 | } |
| 251 | |
| 252 | static int |
| 253 | clip_polygon_bottom(struct clip_context *ctx, const struct polygon8 *src, |
| 254 | GLfloat *dst_x, GLfloat *dst_y) |
| 255 | { |
| 256 | enum path_transition trans; |
| 257 | int i; |
| 258 | |
| 259 | clip_context_prepare(ctx, src, dst_x, dst_y); |
| 260 | for (i = 0; i < src->n; i++) { |
| 261 | trans = path_transition_bottom_edge(ctx, src->x[i], src->y[i]); |
| 262 | clip_polygon_topbottom(ctx, trans, src->x[i], src->y[i], |
| 263 | ctx->clip.y2); |
| 264 | } |
| 265 | return ctx->vertices.x - dst_x; |
| 266 | } |
| 267 | |
| 268 | #define max(a, b) (((a) > (b)) ? (a) : (b)) |
| 269 | #define min(a, b) (((a) > (b)) ? (b) : (a)) |
| 270 | #define clip(x, a, b) min(max(x, a), b) |
| 271 | |
| 272 | int |
| 273 | clip_simple(struct clip_context *ctx, |
| 274 | struct polygon8 *surf, |
| 275 | GLfloat *ex, |
| 276 | GLfloat *ey) |
| 277 | { |
| 278 | int i; |
| 279 | for (i = 0; i < surf->n; i++) { |
| 280 | ex[i] = clip(surf->x[i], ctx->clip.x1, ctx->clip.x2); |
| 281 | ey[i] = clip(surf->y[i], ctx->clip.y1, ctx->clip.y2); |
| 282 | } |
| 283 | return surf->n; |
| 284 | } |
| 285 | |
| 286 | int |
| 287 | clip_transformed(struct clip_context *ctx, |
| 288 | struct polygon8 *surf, |
| 289 | GLfloat *ex, |
| 290 | GLfloat *ey) |
| 291 | { |
| 292 | struct polygon8 polygon; |
| 293 | int i, n; |
| 294 | |
| 295 | polygon.n = clip_polygon_left(ctx, surf, polygon.x, polygon.y); |
| 296 | surf->n = clip_polygon_right(ctx, &polygon, surf->x, surf->y); |
| 297 | polygon.n = clip_polygon_top(ctx, surf, polygon.x, polygon.y); |
| 298 | surf->n = clip_polygon_bottom(ctx, &polygon, surf->x, surf->y); |
| 299 | |
| 300 | /* Get rid of duplicate vertices */ |
| 301 | ex[0] = surf->x[0]; |
| 302 | ey[0] = surf->y[0]; |
| 303 | n = 1; |
| 304 | for (i = 1; i < surf->n; i++) { |
| 305 | if (float_difference(ex[n - 1], surf->x[i]) == 0.0f && |
| 306 | float_difference(ey[n - 1], surf->y[i]) == 0.0f) |
| 307 | continue; |
| 308 | ex[n] = surf->x[i]; |
| 309 | ey[n] = surf->y[i]; |
| 310 | n++; |
| 311 | } |
| 312 | if (float_difference(ex[n - 1], surf->x[0]) == 0.0f && |
| 313 | float_difference(ey[n - 1], surf->y[0]) == 0.0f) |
| 314 | n--; |
| 315 | |
| 316 | return n; |
| 317 | } |