| 1 |
/* $Id: lines.c,v 1.2 2000/10/27 15:21:40 mholst Exp $ */ |
| 2 |
|
| 3 |
/* |
| 4 |
* Mesa 3-D graphics library |
| 5 |
* Version: 2.2 |
| 6 |
* Copyright (C) 1995-1997 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 "bresenhm.h" |
| 24 |
#include "context.h" |
| 25 |
#include "feedback.h" |
| 26 |
#include "interp.h" |
| 27 |
#include "lines.h" |
| 28 |
#include "dlist.h" |
| 29 |
#include "macros.h" |
| 30 |
#include "pb.h" |
| 31 |
#include "texture.h" |
| 32 |
#include "types.h" |
| 33 |
#include "vb.h" |
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
void gl_LineWidth( GLcontext *ctx, GLfloat width ) |
| 39 |
{ |
| 40 |
if (width<=0.0) { |
| 41 |
gl_error( ctx, GL_INVALID_VALUE, "glLineWidth" ); |
| 42 |
return; |
| 43 |
} |
| 44 |
if (INSIDE_BEGIN_END(ctx)) { |
| 45 |
gl_error( ctx, GL_INVALID_OPERATION, "glLineWidth" ); |
| 46 |
return; |
| 47 |
} |
| 48 |
ctx->Line.Width = width; |
| 49 |
ctx->NewState |= NEW_RASTER_OPS; |
| 50 |
} |
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
void gl_LineStipple( GLcontext *ctx, GLint factor, GLushort pattern ) |
| 55 |
{ |
| 56 |
if (INSIDE_BEGIN_END(ctx)) { |
| 57 |
gl_error( ctx, GL_INVALID_OPERATION, "glLineStipple" ); |
| 58 |
return; |
| 59 |
} |
| 60 |
ctx->Line.StippleFactor = CLAMP( factor, 1, 256 ); |
| 61 |
ctx->Line.StipplePattern = pattern; |
| 62 |
ctx->NewState |= NEW_RASTER_OPS; |
| 63 |
} |
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
/**********************************************************************/ |
| 68 |
/***** Rasterization *****/ |
| 69 |
/**********************************************************************/ |
| 70 |
|
| 71 |
|
| 72 |
/* |
| 73 |
* There are 4 pairs (RGBA, CI) of line drawing functions: |
| 74 |
* 1. simple: width=1 and no special rasterization functions (fastest) |
| 75 |
* 2. flat: width=1, non-stippled, flat-shaded, any raster operations |
| 76 |
* 3. smooth: width=1, non-stippled, smooth-shaded, any raster operations |
| 77 |
* 4. general: any other kind of line (slowest) |
| 78 |
*/ |
| 79 |
|
| 80 |
|
| 81 |
/* |
| 82 |
* All line drawing functions have the same arguments: |
| 83 |
* v1, v2 - indexes of first and second endpoints into vertex buffer arrays |
| 84 |
* pv - provoking vertex: which vertex color/index to use for flat shading. |
| 85 |
*/ |
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
static void feedback_line( GLcontext *ctx, GLuint v1, GLuint v2, GLuint pv ) |
| 90 |
{ |
| 91 |
struct vertex_buffer *VB = ctx->VB; |
| 92 |
GLfloat x1, y1, z1, w1; |
| 93 |
GLfloat x2, y2, z2, w2; |
| 94 |
GLfloat tex1[4], tex2[4], invq; |
| 95 |
GLfloat invRedScale = ctx->Visual->InvRedScale; |
| 96 |
GLfloat invGreenScale = ctx->Visual->InvGreenScale; |
| 97 |
GLfloat invBlueScale = ctx->Visual->InvBlueScale; |
| 98 |
GLfloat invAlphaScale = ctx->Visual->InvAlphaScale; |
| 99 |
|
| 100 |
x1 = VB->Win[v1][0]; |
| 101 |
y1 = VB->Win[v1][1]; |
| 102 |
z1 = VB->Win[v1][2] / DEPTH_SCALE; |
| 103 |
w1 = VB->Clip[v1][3]; |
| 104 |
|
| 105 |
x2 = VB->Win[v2][0]; |
| 106 |
y2 = VB->Win[v2][1]; |
| 107 |
z2 = VB->Win[v2][2] / DEPTH_SCALE; |
| 108 |
w2 = VB->Clip[v2][3]; |
| 109 |
|
| 110 |
invq = 1.0F / VB->TexCoord[v1][3]; |
| 111 |
tex1[0] = VB->TexCoord[v1][0] * invq; |
| 112 |
tex1[1] = VB->TexCoord[v1][1] * invq; |
| 113 |
tex1[2] = VB->TexCoord[v1][2] * invq; |
| 114 |
tex1[3] = VB->TexCoord[v1][3]; |
| 115 |
invq = 1.0F / VB->TexCoord[v2][3]; |
| 116 |
tex2[0] = VB->TexCoord[v2][0] * invq; |
| 117 |
tex2[1] = VB->TexCoord[v2][1] * invq; |
| 118 |
tex2[2] = VB->TexCoord[v2][2] * invq; |
| 119 |
tex2[3] = VB->TexCoord[v2][3]; |
| 120 |
|
| 121 |
if (ctx->StippleCounter==0) { |
| 122 |
FEEDBACK_TOKEN( ctx, (GLfloat) GL_LINE_RESET_TOKEN ); |
| 123 |
} |
| 124 |
else { |
| 125 |
FEEDBACK_TOKEN( ctx, (GLfloat) GL_LINE_TOKEN ); |
| 126 |
} |
| 127 |
if (ctx->Light.ShadeModel==GL_FLAT) { |
| 128 |
GLfloat color[4]; |
| 129 |
/* convert color from integer to a float in [0,1] */ |
| 130 |
color[0] = (GLfloat) VB->Color[pv][0] * invRedScale; |
| 131 |
color[1] = (GLfloat) VB->Color[pv][1] * invGreenScale; |
| 132 |
color[2] = (GLfloat) VB->Color[pv][2] * invBlueScale; |
| 133 |
color[3] = (GLfloat) VB->Color[pv][3] * invAlphaScale; |
| 134 |
gl_feedback_vertex( ctx, x1,y1,z1,w1, color, |
| 135 |
(GLfloat) VB->Index[pv], tex1 ); |
| 136 |
gl_feedback_vertex( ctx, x2,y2,z2,w2, color, |
| 137 |
(GLfloat) VB->Index[pv], tex2 ); |
| 138 |
} |
| 139 |
else { |
| 140 |
GLfloat color[4]; |
| 141 |
/* convert color from fixed point to a float in [0,1] */ |
| 142 |
color[0] = FixedToFloat(VB->Color[v1][0]) * invRedScale; |
| 143 |
color[1] = FixedToFloat(VB->Color[v1][1]) * invGreenScale; |
| 144 |
color[2] = FixedToFloat(VB->Color[v1][2]) * invBlueScale; |
| 145 |
color[3] = FixedToFloat(VB->Color[v1][3]) * invAlphaScale; |
| 146 |
gl_feedback_vertex( ctx, x1,y1,z1,w1, color, |
| 147 |
(GLfloat) VB->Index[v1], tex1 ); |
| 148 |
/* convert color from fixed point to a float in [0,1] */ |
| 149 |
color[0] = FixedToFloat(VB->Color[v2][0]) * invRedScale; |
| 150 |
color[1] = FixedToFloat(VB->Color[v2][1]) * invGreenScale; |
| 151 |
color[2] = FixedToFloat(VB->Color[v2][2]) * invBlueScale; |
| 152 |
color[3] = FixedToFloat(VB->Color[v2][3]) * invAlphaScale; |
| 153 |
gl_feedback_vertex( ctx, x2,y2,z2,w2, color, |
| 154 |
(GLfloat) VB->Index[v2], tex2 ); |
| 155 |
} |
| 156 |
ctx->StippleCounter++; |
| 157 |
} |
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
static void select_line( GLcontext *ctx, GLuint v1, GLuint v2, GLuint pv ) |
| 162 |
{ |
| 163 |
gl_update_hitflag( ctx, ctx->VB->Win[v1][2] / DEPTH_SCALE ); |
| 164 |
gl_update_hitflag( ctx, ctx->VB->Win[v2][2] / DEPTH_SCALE ); |
| 165 |
} |
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
#if MAX_WIDTH > MAX_HEIGHT |
| 170 |
# define MAXPOINTS MAX_WIDTH |
| 171 |
#else |
| 172 |
# define MAXPOINTS MAX_HEIGHT |
| 173 |
#endif |
| 174 |
|
| 175 |
|
| 176 |
/* |
| 177 |
* Flat shaded, width=1, non-stippled, color index line. |
| 178 |
*/ |
| 179 |
static void flat_ci_line( GLcontext *ctx, GLuint v1, GLuint v2, GLuint pv ) |
| 180 |
{ |
| 181 |
struct vertex_buffer *VB = ctx->VB; |
| 182 |
struct pixel_buffer *PB = ctx->PB; |
| 183 |
GLint x1 = (GLint) VB->Win[v1][0]; |
| 184 |
GLint y1 = (GLint) VB->Win[v1][1]; |
| 185 |
GLint x2 = (GLint) VB->Win[v2][0]; |
| 186 |
GLint y2 = (GLint) VB->Win[v2][1]; |
| 187 |
GLint n; |
| 188 |
|
| 189 |
PB_SET_INDEX( ctx, PB, VB->Index[pv] ); |
| 190 |
|
| 191 |
/* compute pixel locations */ |
| 192 |
n = gl_bresenham( ctx, x1, y1, x2, y2, PB->x+PB->count, PB->y+PB->count ); |
| 193 |
|
| 194 |
/* interpolate z values */ |
| 195 |
if (ctx->Depth.Test) { |
| 196 |
GLdepth *zptr = PB->z + PB->count; |
| 197 |
GLint z1 = (GLint) (VB->Win[v1][2] + ctx->LineZoffset); |
| 198 |
GLint z2 = (GLint) (VB->Win[v2][2] + ctx->LineZoffset); |
| 199 |
GL_INTERPOLATE_Z( n, z1, z2, zptr ); |
| 200 |
} |
| 201 |
|
| 202 |
PB->count += n; |
| 203 |
PB_CHECK_FLUSH( ctx, PB ); |
| 204 |
} |
| 205 |
|
| 206 |
|
| 207 |
/* |
| 208 |
* Flat-shaded, width=1, non-stippled, rgba line. |
| 209 |
*/ |
| 210 |
static void flat_rgba_line( GLcontext *ctx, GLuint v1, GLuint v2, GLuint pv ) |
| 211 |
{ |
| 212 |
struct vertex_buffer *VB = ctx->VB; |
| 213 |
struct pixel_buffer *PB = ctx->PB; |
| 214 |
GLint x1 = (GLint) VB->Win[v1][0]; |
| 215 |
GLint y1 = (GLint) VB->Win[v1][1]; |
| 216 |
GLint x2 = (GLint) VB->Win[v2][0]; |
| 217 |
GLint y2 = (GLint) VB->Win[v2][1]; |
| 218 |
GLint n; |
| 219 |
|
| 220 |
/* Note that color components are ints, not fixed point here */ |
| 221 |
PB_SET_COLOR( ctx, PB, VB->Color[pv][0], VB->Color[pv][1], |
| 222 |
VB->Color[pv][2], VB->Color[pv][3] ); |
| 223 |
|
| 224 |
/* compute pixel locations */ |
| 225 |
n = gl_bresenham( ctx, x1, y1, x2, y2, PB->x+PB->count, PB->y+PB->count ); |
| 226 |
|
| 227 |
/* interpolate z values */ |
| 228 |
if (ctx->Depth.Test) { |
| 229 |
GLdepth *zptr = PB->z + PB->count; |
| 230 |
GLint z1 = (GLint) (VB->Win[v1][2] + ctx->LineZoffset); |
| 231 |
GLint z2 = (GLint) (VB->Win[v2][2] + ctx->LineZoffset); |
| 232 |
GL_INTERPOLATE_Z( n, z1, z2, zptr ); |
| 233 |
} |
| 234 |
|
| 235 |
PB->count += n; |
| 236 |
PB_CHECK_FLUSH( ctx, PB ); |
| 237 |
} |
| 238 |
|
| 239 |
|
| 240 |
|
| 241 |
/* |
| 242 |
* Smooth-shaded, width=1, non-stippled, color index line. |
| 243 |
*/ |
| 244 |
static void smooth_ci_line( GLcontext *ctx, GLuint v1, GLuint v2, GLuint pv ) |
| 245 |
{ |
| 246 |
struct vertex_buffer *VB = ctx->VB; |
| 247 |
struct pixel_buffer *PB = ctx->PB; |
| 248 |
GLint x1 = (GLint) VB->Win[v1][0]; |
| 249 |
GLint y1 = (GLint) VB->Win[v1][1]; |
| 250 |
GLint x2 = (GLint) VB->Win[v2][0]; |
| 251 |
GLint y2 = (GLint) VB->Win[v2][1]; |
| 252 |
GLint n; |
| 253 |
|
| 254 |
/* compute pixel locations */ |
| 255 |
n = gl_bresenham( ctx, x1, y1, x2, y2, PB->x+PB->count, PB->y+PB->count ); |
| 256 |
|
| 257 |
/* interpolate z values */ |
| 258 |
if (ctx->Depth.Test) { |
| 259 |
GLdepth *zptr = PB->z + PB->count; |
| 260 |
GLint z1 = (GLint) (VB->Win[v1][2] + ctx->LineZoffset); |
| 261 |
GLint z2 = (GLint) (VB->Win[v2][2] + ctx->LineZoffset); |
| 262 |
GL_INTERPOLATE_Z( n, z1, z2, zptr ); |
| 263 |
} |
| 264 |
|
| 265 |
/* interpolate index */ |
| 266 |
gl_interpolate_i( n, (GLint) VB->Index[v1], (GLint) VB->Index[v2], |
| 267 |
(GLint *) PB->i+PB->count ); |
| 268 |
|
| 269 |
PB->count += n; |
| 270 |
PB_CHECK_FLUSH( ctx, PB ); |
| 271 |
} |
| 272 |
|
| 273 |
|
| 274 |
/* |
| 275 |
* Smooth-shaded, width=1, non-stippled, RGBA line. |
| 276 |
*/ |
| 277 |
static void smooth_rgba_line( GLcontext *ctx, GLuint v1, GLuint v2, GLuint pv ) |
| 278 |
{ |
| 279 |
struct vertex_buffer *VB = ctx->VB; |
| 280 |
struct pixel_buffer *PB = ctx->PB; |
| 281 |
GLint x1 = (GLint) VB->Win[v1][0]; |
| 282 |
GLint y1 = (GLint) VB->Win[v1][1]; |
| 283 |
GLint x2 = (GLint) VB->Win[v2][0]; |
| 284 |
GLint y2 = (GLint) VB->Win[v2][1]; |
| 285 |
GLint n; |
| 286 |
|
| 287 |
/* compute pixel locations */ |
| 288 |
n = gl_bresenham( ctx, x1, y1, x2, y2, PB->x+PB->count, PB->y+PB->count ); |
| 289 |
|
| 290 |
/* interpolate z values */ |
| 291 |
if (ctx->Depth.Test) { |
| 292 |
GLdepth *zptr = PB->z + PB->count; |
| 293 |
GLint z1 = (GLint) (VB->Win[v1][2] + ctx->LineZoffset); |
| 294 |
GLint z2 = (GLint) (VB->Win[v2][2] + ctx->LineZoffset); |
| 295 |
GL_INTERPOLATE_Z( n, z1, z2, zptr ); |
| 296 |
} |
| 297 |
|
| 298 |
/* interpolate color, VB->Colors are in fixed point */ |
| 299 |
gl_interpolate_rgba( n, |
| 300 |
VB->Color[v1][0], VB->Color[v2][0], PB->r+PB->count, |
| 301 |
VB->Color[v1][1], VB->Color[v2][1], PB->g+PB->count, |
| 302 |
VB->Color[v1][2], VB->Color[v2][2], PB->b+PB->count, |
| 303 |
VB->Color[v1][3], VB->Color[v2][3], PB->a+PB->count ); |
| 304 |
|
| 305 |
PB->count += n; |
| 306 |
PB_CHECK_FLUSH( ctx, PB ); |
| 307 |
} |
| 308 |
|
| 309 |
|
| 310 |
|
| 311 |
/* |
| 312 |
* General CI line: any width, smooth or flat, stippled, any raster ops. |
| 313 |
*/ |
| 314 |
static void general_ci_line( GLcontext *ctx, GLuint v1, GLuint v2, GLuint pv ) |
| 315 |
{ |
| 316 |
struct vertex_buffer *VB = ctx->VB; |
| 317 |
struct pixel_buffer *PB = ctx->PB; |
| 318 |
GLint x1, y1, x2, y2; |
| 319 |
GLint x[MAXPOINTS], y[MAXPOINTS]; |
| 320 |
GLdepth z[MAXPOINTS]; |
| 321 |
GLubyte mask[MAXPOINTS]; |
| 322 |
GLuint index[MAXPOINTS]; |
| 323 |
GLint i, n; |
| 324 |
GLint dx, dy; |
| 325 |
|
| 326 |
x1 = (GLint) VB->Win[v1][0]; |
| 327 |
y1 = (GLint) VB->Win[v1][1]; |
| 328 |
x2 = (GLint) VB->Win[v2][0]; |
| 329 |
y2 = (GLint) VB->Win[v2][1]; |
| 330 |
|
| 331 |
/* compute pixel locations */ |
| 332 |
if (ctx->Line.StippleFlag) { |
| 333 |
n = gl_stippled_bresenham( ctx, x1, y1, x2, y2, x, y, mask ); |
| 334 |
} |
| 335 |
else { |
| 336 |
n = gl_bresenham( ctx, x1, y1, x2, y2, x, y ); |
| 337 |
for (i=0;i<n;i++) { |
| 338 |
mask[i] = 1; |
| 339 |
} |
| 340 |
} |
| 341 |
|
| 342 |
if (ctx->Depth.Test) { |
| 343 |
/* interpolate z */ |
| 344 |
GLint z1 = (GLint) (VB->Win[v1][2] + ctx->LineZoffset); |
| 345 |
GLint z2 = (GLint) (VB->Win[v2][2] + ctx->LineZoffset); |
| 346 |
GL_INTERPOLATE_Z( n, z1, z2, z ); |
| 347 |
} |
| 348 |
|
| 349 |
if (ctx->Light.ShadeModel==GL_FLAT) { |
| 350 |
GLuint indx = VB->Index[pv]; |
| 351 |
for (i=0;i<n;i++) { |
| 352 |
index[i] = indx; |
| 353 |
} |
| 354 |
} |
| 355 |
else { |
| 356 |
/* interpolate index */ |
| 357 |
gl_interpolate_i( n, (GLint) VB->Index[v1], (GLint) VB->Index[v2], |
| 358 |
(GLint *) index ); |
| 359 |
} |
| 360 |
|
| 361 |
/* compute delta x and delta y */ |
| 362 |
if (x1>x2) { |
| 363 |
dx = x1 - x2; |
| 364 |
} |
| 365 |
else { |
| 366 |
dx = x2 - x1; |
| 367 |
} |
| 368 |
if (y1>y2) { |
| 369 |
dy = y1 - y2; |
| 370 |
} |
| 371 |
else { |
| 372 |
dy = y2 - y1; |
| 373 |
} |
| 374 |
|
| 375 |
|
| 376 |
/* render */ |
| 377 |
if (ctx->Line.Width==2.0F) { |
| 378 |
/* special case, easy to optimize */ |
| 379 |
if (dx>dy) { |
| 380 |
/* X-major: duplicate pixels in Y direction */ |
| 381 |
for (i=0;i<n;i++) { |
| 382 |
if (mask[i]) { |
| 383 |
PB_WRITE_CI_PIXEL( PB, x[i], y[i]-1, z[i], index[i] ); |
| 384 |
PB_WRITE_CI_PIXEL( PB, x[i], y[i], z[i], index[i] ); |
| 385 |
} |
| 386 |
} |
| 387 |
} |
| 388 |
else { |
| 389 |
/* Y-major: duplicate pixels in X direction */ |
| 390 |
for (i=0;i<n;i++) { |
| 391 |
if (mask[i]) { |
| 392 |
PB_WRITE_CI_PIXEL( PB, x[i]-1, y[i], z[i], index[i] ); |
| 393 |
PB_WRITE_CI_PIXEL( PB, x[i], y[i], z[i], index[i] ); |
| 394 |
} |
| 395 |
} |
| 396 |
} |
| 397 |
PB_CHECK_FLUSH( ctx, PB ); |
| 398 |
} |
| 399 |
else { |
| 400 |
GLint width, w0, w1; |
| 401 |
width = (GLint) CLAMP( ctx->Line.Width, MIN_LINE_WIDTH, MAX_LINE_WIDTH ); |
| 402 |
w0 = -width / 2; |
| 403 |
w1 = w0 + width - 1; |
| 404 |
|
| 405 |
if (dx>dy) { |
| 406 |
/* X-major: duplicate pixels in Y direction */ |
| 407 |
for (i=0;i<n;i++) { |
| 408 |
if (mask[i]) { |
| 409 |
GLint yy; |
| 410 |
GLint y0 = y[i] + w0; |
| 411 |
GLint y1 = y[i] + w1; |
| 412 |
for (yy=y0;yy<=y1;yy++) { |
| 413 |
PB_WRITE_CI_PIXEL( PB, x[i], yy, z[i], index[i] ); |
| 414 |
} |
| 415 |
PB_CHECK_FLUSH( ctx, PB ); |
| 416 |
} |
| 417 |
} |
| 418 |
} |
| 419 |
else { |
| 420 |
/* Y-major: duplicate pixels in X direction */ |
| 421 |
for (i=0;i<n;i++) { |
| 422 |
if (mask[i]) { |
| 423 |
GLint xx; |
| 424 |
GLint x0 = x[i] + w0; |
| 425 |
GLint x1 = x[i] + w1; |
| 426 |
for (xx=x0;xx<=x1;xx++) { |
| 427 |
PB_WRITE_CI_PIXEL( PB, xx, y[i], z[i], index[i] ); |
| 428 |
} |
| 429 |
PB_CHECK_FLUSH( ctx, PB ); |
| 430 |
} |
| 431 |
} |
| 432 |
} |
| 433 |
} |
| 434 |
} |
| 435 |
|
| 436 |
|
| 437 |
/* |
| 438 |
* General RGBA line: any width, smooth or flat, stippled, any raster ops. |
| 439 |
*/ |
| 440 |
static void general_rgba_line( GLcontext *ctx, |
| 441 |
GLuint v1, GLuint v2, GLuint pv ) |
| 442 |
{ |
| 443 |
struct vertex_buffer *VB = ctx->VB; |
| 444 |
struct pixel_buffer *PB = ctx->PB; |
| 445 |
GLint x1, y1, x2, y2; |
| 446 |
GLint x[MAXPOINTS], y[MAXPOINTS]; |
| 447 |
GLdepth z[MAXPOINTS]; |
| 448 |
GLubyte mask[MAXPOINTS]; |
| 449 |
GLubyte red[MAXPOINTS], green[MAXPOINTS], blue[MAXPOINTS], alpha[MAXPOINTS]; |
| 450 |
GLint i, n; |
| 451 |
GLint dx, dy; |
| 452 |
|
| 453 |
x1 = (GLint) VB->Win[v1][0]; |
| 454 |
y1 = (GLint) VB->Win[v1][1]; |
| 455 |
x2 = (GLint) VB->Win[v2][0]; |
| 456 |
y2 = (GLint) VB->Win[v2][1]; |
| 457 |
|
| 458 |
/* compute the line */ |
| 459 |
if (ctx->Line.StippleFlag) { |
| 460 |
n = gl_stippled_bresenham( ctx, x1, y1, x2, y2, x, y, mask ); |
| 461 |
} |
| 462 |
else { |
| 463 |
n = gl_bresenham( ctx, x1, y1, x2, y2, x, y ); |
| 464 |
for (i=0;i<n;i++) { |
| 465 |
mask[i] = 1; |
| 466 |
} |
| 467 |
} |
| 468 |
|
| 469 |
if (ctx->Depth.Test) { |
| 470 |
GLint z1 = (GLint) (VB->Win[v1][2] + ctx->LineZoffset); |
| 471 |
GLint z2 = (GLint) (VB->Win[v2][2] + ctx->LineZoffset); |
| 472 |
GL_INTERPOLATE_Z( n, z1, z2, z ); |
| 473 |
} |
| 474 |
|
| 475 |
if (ctx->Light.ShadeModel==GL_FLAT) { |
| 476 |
GLint r, g, b, a; |
| 477 |
r = VB->Color[pv][0]; /* colors are ints, not fixed point */ |
| 478 |
g = VB->Color[pv][1]; |
| 479 |
b = VB->Color[pv][2]; |
| 480 |
a = VB->Color[pv][3]; |
| 481 |
for (i=0;i<n;i++) { |
| 482 |
red[i] = r; |
| 483 |
green[i] = g; |
| 484 |
blue[i] = b; |
| 485 |
alpha[i] = a; |
| 486 |
} |
| 487 |
} |
| 488 |
else { |
| 489 |
/* interpolate color, VB->Colors are in fixed point */ |
| 490 |
gl_interpolate_rgba( n, |
| 491 |
VB->Color[v1][0], VB->Color[v2][0], red, |
| 492 |
VB->Color[v1][1], VB->Color[v2][1], green, |
| 493 |
VB->Color[v1][2], VB->Color[v2][2], blue, |
| 494 |
VB->Color[v1][3], VB->Color[v2][3], alpha ); |
| 495 |
} |
| 496 |
|
| 497 |
/* compute delta x and delta y */ |
| 498 |
if (x1>x2) { |
| 499 |
dx = x1 - x2; |
| 500 |
} |
| 501 |
else { |
| 502 |
dx = x2 - x1; |
| 503 |
} |
| 504 |
if (y1>y2) { |
| 505 |
dy = y1 - y2; |
| 506 |
} |
| 507 |
else { |
| 508 |
dy = y2 - y1; |
| 509 |
} |
| 510 |
|
| 511 |
/* render */ |
| 512 |
if (ctx->Line.Width==2.0F) { |
| 513 |
/* special case, easy to optimize */ |
| 514 |
if (dx>dy) { |
| 515 |
/* X-major: duplicate pixels in Y direction */ |
| 516 |
for (i=0;i<n;i++) { |
| 517 |
if (mask[i]) { |
| 518 |
PB_WRITE_RGBA_PIXEL( PB, x[i], y[i]-1, z[i], |
| 519 |
red[i], green[i], blue[i], alpha[i] ); |
| 520 |
PB_WRITE_RGBA_PIXEL( PB, x[i], y[i], z[i], |
| 521 |
red[i], green[i], blue[i], alpha[i] ); |
| 522 |
} |
| 523 |
} |
| 524 |
} |
| 525 |
else { |
| 526 |
/* Y-major: duplicate pixels in X direction */ |
| 527 |
for (i=0;i<n;i++) { |
| 528 |
if (mask[i]) { |
| 529 |
PB_WRITE_RGBA_PIXEL( PB, x[i]-1, y[i], z[i], |
| 530 |
red[i], green[i], blue[i], alpha[i] ); |
| 531 |
PB_WRITE_RGBA_PIXEL( PB, x[i], y[i], z[i], |
| 532 |
red[i], green[i], blue[i], alpha[i] ); |
| 533 |
} |
| 534 |
} |
| 535 |
} |
| 536 |
PB_CHECK_FLUSH( ctx, PB ); |
| 537 |
} |
| 538 |
else { |
| 539 |
GLint width, w0, w1; |
| 540 |
width = (GLint) CLAMP( ctx->Line.Width, MIN_LINE_WIDTH, MAX_LINE_WIDTH ); |
| 541 |
w0 = -width / 2; |
| 542 |
w1 = w0 + width - 1; |
| 543 |
|
| 544 |
if (dx>dy) { |
| 545 |
/* X-major: duplicate pixels in Y direction */ |
| 546 |
for (i=0;i<n;i++) { |
| 547 |
if (mask[i]) { |
| 548 |
GLint yy; |
| 549 |
GLint y0 = y[i] + w0; |
| 550 |
GLint y1 = y[i] + w1; |
| 551 |
for (yy=y0;yy<=y1;yy++) { |
| 552 |
PB_WRITE_RGBA_PIXEL( PB, x[i], yy, z[i], |
| 553 |
red[i], green[i], blue[i], alpha[i] ); |
| 554 |
} |
| 555 |
PB_CHECK_FLUSH( ctx, PB ); |
| 556 |
} |
| 557 |
} |
| 558 |
} |
| 559 |
else { |
| 560 |
/* Y-major: duplicate pixels in X direction */ |
| 561 |
for (i=0;i<n;i++) { |
| 562 |
if (mask[i]) { |
| 563 |
GLint xx; |
| 564 |
GLint x0 = x[i] + w0; |
| 565 |
GLint x1 = x[i] + w1; |
| 566 |
for (xx=x0;xx<=x1;xx++) { |
| 567 |
PB_WRITE_RGBA_PIXEL( PB, xx, y[i], z[i], |
| 568 |
red[i], green[i], blue[i], alpha[i] ); |
| 569 |
} |
| 570 |
PB_CHECK_FLUSH( ctx, PB ); |
| 571 |
} |
| 572 |
} |
| 573 |
} |
| 574 |
} |
| 575 |
} |
| 576 |
|
| 577 |
|
| 578 |
|
| 579 |
/* |
| 580 |
* Textured RGBA line: any width, smooth or flat, stippled, any raster ops |
| 581 |
* with texturing. |
| 582 |
*/ |
| 583 |
static void textured_rgba_line( GLcontext *ctx, |
| 584 |
GLuint v1, GLuint v2, GLuint pv ) |
| 585 |
{ |
| 586 |
struct vertex_buffer *VB = ctx->VB; |
| 587 |
struct pixel_buffer *PB = ctx->PB; |
| 588 |
GLint x1, y1, x2, y2; |
| 589 |
GLint x[MAXPOINTS], y[MAXPOINTS]; |
| 590 |
GLdepth z[MAXPOINTS]; |
| 591 |
GLubyte mask[MAXPOINTS]; |
| 592 |
/* Allocate arrays dynamically on Mac */ |
| 593 |
DEFARRAY(GLubyte,red,MAXPOINTS); |
| 594 |
DEFARRAY(GLubyte,green,MAXPOINTS); |
| 595 |
DEFARRAY(GLubyte,blue,MAXPOINTS); |
| 596 |
DEFARRAY(GLubyte,alpha,MAXPOINTS); |
| 597 |
DEFARRAY(GLfloat,s,MAXPOINTS); |
| 598 |
DEFARRAY(GLfloat,t,MAXPOINTS); |
| 599 |
DEFARRAY(GLfloat,u,MAXPOINTS); |
| 600 |
#if 0 |
| 601 |
DEFARRAY(GLfloat,v,MAXPOINTS); |
| 602 |
#endif |
| 603 |
GLint i, n; |
| 604 |
GLint dx, dy; |
| 605 |
|
| 606 |
x1 = (GLint) VB->Win[v1][0]; |
| 607 |
y1 = (GLint) VB->Win[v1][1]; |
| 608 |
x2 = (GLint) VB->Win[v2][0]; |
| 609 |
y2 = (GLint) VB->Win[v2][1]; |
| 610 |
|
| 611 |
/* compute the line */ |
| 612 |
if (ctx->Line.StippleFlag) { |
| 613 |
n = gl_stippled_bresenham( ctx, x1, y1, x2, y2, x, y, mask ); |
| 614 |
} |
| 615 |
else { |
| 616 |
n = gl_bresenham( ctx, x1, y1, x2, y2, x, y ); |
| 617 |
for (i=0;i<n;i++) { |
| 618 |
mask[i] = 1; |
| 619 |
} |
| 620 |
} |
| 621 |
|
| 622 |
if (ctx->Depth.Test) { |
| 623 |
GLint z1 = (GLint) (VB->Win[v1][2] + ctx->LineZoffset); |
| 624 |
GLint z2 = (GLint) (VB->Win[v2][2] + ctx->LineZoffset); |
| 625 |
GL_INTERPOLATE_Z( n, z1, z2, z ); |
| 626 |
} |
| 627 |
|
| 628 |
if (ctx->Light.ShadeModel==GL_FLAT) { |
| 629 |
GLint r, g, b, a; |
| 630 |
r = VB->Color[pv][0]; /* colors are ints, not in fixed point */ |
| 631 |
g = VB->Color[pv][1]; |
| 632 |
b = VB->Color[pv][2]; |
| 633 |
a = VB->Color[pv][3]; |
| 634 |
for (i=0;i<n;i++) { |
| 635 |
red[i] = r; |
| 636 |
green[i] = g; |
| 637 |
blue[i] = b; |
| 638 |
alpha[i] = a; |
| 639 |
} |
| 640 |
} |
| 641 |
else { |
| 642 |
/* interpolate color, VB->Colors are in fixed point */ |
| 643 |
gl_interpolate_rgba( n, |
| 644 |
VB->Color[v1][0], VB->Color[v2][0], red, |
| 645 |
VB->Color[v1][1], VB->Color[v2][1], green, |
| 646 |
VB->Color[v1][2], VB->Color[v2][2], blue, |
| 647 |
VB->Color[v1][3], VB->Color[v2][3], alpha ); |
| 648 |
} |
| 649 |
|
| 650 |
/* interpolate texture coordinates */ |
| 651 |
{ |
| 652 |
GLfloat w1 = 1.0F / VB->Clip[v1][3]; |
| 653 |
GLfloat w2 = 1.0F / VB->Clip[v2][3]; |
| 654 |
GLfloat s1 = VB->TexCoord[v1][0] * w1; |
| 655 |
GLfloat s2 = VB->TexCoord[v2][0] * w2; |
| 656 |
GLfloat t1 = VB->TexCoord[v1][1] * w1; |
| 657 |
GLfloat t2 = VB->TexCoord[v2][1] * w2; |
| 658 |
GLfloat u1 = VB->TexCoord[v1][2] * w1; |
| 659 |
GLfloat u2 = VB->TexCoord[v2][2] * w2; |
| 660 |
/* don't interpolate r since we don't do 3-D textures, yet */ |
| 661 |
GLfloat q1 = VB->TexCoord[v1][3] * w1; |
| 662 |
GLfloat q2 = VB->TexCoord[v2][3] * w2; |
| 663 |
GLfloat inv_n = 1.0F / (GLfloat) n; |
| 664 |
GLfloat ds = (s2-s1) * inv_n; |
| 665 |
GLfloat dt = (t2-t1) * inv_n; |
| 666 |
GLfloat du = (u2-u1) * inv_n; |
| 667 |
GLfloat dq = (q2-q1) * inv_n; |
| 668 |
for (i=0;i<n;i++) { |
| 669 |
s[i] = s1 / q1; |
| 670 |
t[i] = t1 / q1; |
| 671 |
u[i] = u1 / q1; |
| 672 |
s1 += ds; |
| 673 |
t1 += dt; |
| 674 |
u1 += du; |
| 675 |
q1 += dq; |
| 676 |
} |
| 677 |
} |
| 678 |
|
| 679 |
/* compute delta x and delta y */ |
| 680 |
if (x1>x2) { |
| 681 |
dx = x1 - x2; |
| 682 |
} |
| 683 |
else { |
| 684 |
dx = x2 - x1; |
| 685 |
} |
| 686 |
if (y1>y2) { |
| 687 |
dy = y1 - y2; |
| 688 |
} |
| 689 |
else { |
| 690 |
dy = y2 - y1; |
| 691 |
} |
| 692 |
|
| 693 |
/* render */ |
| 694 |
if (ctx->Line.Width==2.0F) { |
| 695 |
/* special case, easy to optimize */ |
| 696 |
if (dx>dy) { |
| 697 |
/* X-major: duplicate pixels in Y direction */ |
| 698 |
for (i=0;i<n;i++) { |
| 699 |
if (mask[i]) { |
| 700 |
PB_WRITE_TEX_PIXEL( PB, x[i], y[i]-1, z[i], |
| 701 |
red[i], green[i], blue[i], alpha[i], s[i], t[i], u[i] ); |
| 702 |
PB_WRITE_TEX_PIXEL( PB, x[i], y[i], z[i], |
| 703 |
red[i], green[i], blue[i], alpha[i], s[i], t[i], u[i] ); |
| 704 |
} |
| 705 |
} |
| 706 |
} |
| 707 |
else { |
| 708 |
/* Y-major: duplicate pixels in X direction */ |
| 709 |
for (i=0;i<n;i++) { |
| 710 |
if (mask[i]) { |
| 711 |
PB_WRITE_TEX_PIXEL( PB, x[i]-1, y[i], z[i], |
| 712 |
red[i], green[i], blue[i], alpha[i], s[i], t[i], u[i] ); |
| 713 |
PB_WRITE_TEX_PIXEL( PB, x[i], y[i], z[i], |
| 714 |
red[i], green[i], blue[i], alpha[i], s[i], t[i], u[i] ); |
| 715 |
} |
| 716 |
} |
| 717 |
} |
| 718 |
PB_CHECK_FLUSH( ctx, PB ); |
| 719 |
} |
| 720 |
else { |
| 721 |
GLint width, w0, w1; |
| 722 |
width = (GLint) CLAMP( ctx->Line.Width, MIN_LINE_WIDTH, MAX_LINE_WIDTH ); |
| 723 |
w0 = -width / 2; |
| 724 |
w1 = w0 + width - 1; |
| 725 |
|
| 726 |
if (dx>dy) { |
| 727 |
/* X-major: duplicate pixels in Y direction */ |
| 728 |
for (i=0;i<n;i++) { |
| 729 |
if (mask[i]) { |
| 730 |
GLint yy; |
| 731 |
GLint y0 = y[i] + w0; |
| 732 |
GLint y1 = y[i] + w1; |
| 733 |
for (yy=y0;yy<=y1;yy++) { |
| 734 |
PB_WRITE_TEX_PIXEL( PB, x[i], yy, z[i], |
| 735 |
red[i], green[i], blue[i], alpha[i], s[i], t[i], u[i] ); |
| 736 |
} |
| 737 |
PB_CHECK_FLUSH( ctx, PB ); |
| 738 |
} |
| 739 |
} |
| 740 |
} |
| 741 |
else { |
| 742 |
/* Y-major: duplicate pixels in X direction */ |
| 743 |
for (i=0;i<n;i++) { |
| 744 |
if (mask[i]) { |
| 745 |
GLint xx; |
| 746 |
GLint x0 = x[i] + w0; |
| 747 |
GLint x1 = x[i] + w1; |
| 748 |
for (xx=x0;xx<=x1;xx++) { |
| 749 |
PB_WRITE_TEX_PIXEL( PB, xx, y[i], z[i], |
| 750 |
red[i], green[i], blue[i], alpha[i], s[i], t[i], u[i] ); |
| 751 |
} |
| 752 |
PB_CHECK_FLUSH( ctx, PB ); |
| 753 |
} |
| 754 |
} |
| 755 |
} |
| 756 |
} |
| 757 |
|
| 758 |
/* Deallocate dynamic arrays on Mac */ |
| 759 |
UNDEFARRAY(red); |
| 760 |
UNDEFARRAY(green); |
| 761 |
UNDEFARRAY(blue); |
| 762 |
UNDEFARRAY(alpha); |
| 763 |
UNDEFARRAY(s); |
| 764 |
UNDEFARRAY(t); |
| 765 |
UNDEFARRAY(u); |
| 766 |
#if 0 |
| 767 |
UNDEFARRAY(v); |
| 768 |
#endif |
| 769 |
} |
| 770 |
|
| 771 |
|
| 772 |
|
| 773 |
/* |
| 774 |
* Null rasterizer for measuring transformation speed. |
| 775 |
*/ |
| 776 |
static void null_line( GLcontext *ctx, GLuint v1, GLuint v2, GLuint pv ) |
| 777 |
{ |
| 778 |
} |
| 779 |
|
| 780 |
|
| 781 |
|
| 782 |
/* |
| 783 |
* Determine which line drawing function to use given the current |
| 784 |
* rendering context. |
| 785 |
*/ |
| 786 |
void gl_set_line_function( GLcontext *ctx ) |
| 787 |
{ |
| 788 |
GLboolean rgbmode = ctx->Visual->RGBAflag; |
| 789 |
/* TODO: antialiased lines */ |
| 790 |
|
| 791 |
if (ctx->RenderMode==GL_RENDER) { |
| 792 |
if (ctx->NoRaster) { |
| 793 |
ctx->LineFunc = null_line; |
| 794 |
return; |
| 795 |
} |
| 796 |
if (ctx->Driver.LineFunc) { |
| 797 |
/* Device driver will draw lines. */ |
| 798 |
ctx->LineFunc = ctx->Driver.LineFunc; |
| 799 |
} |
| 800 |
else if (gl_texturing_enabled(ctx)) { |
| 801 |
ctx->LineFunc = textured_rgba_line; |
| 802 |
} |
| 803 |
else if (ctx->Line.Width!=1.0 || ctx->Line.StippleFlag |
| 804 |
|| ctx->Line.SmoothFlag || ctx->Texture.Enabled) { |
| 805 |
ctx->LineFunc = rgbmode ? general_rgba_line : general_ci_line; |
| 806 |
} |
| 807 |
else { |
| 808 |
if (ctx->Light.ShadeModel==GL_SMOOTH) { |
| 809 |
/* Width==1, non-stippled, smooth-shaded, any raster ops */ |
| 810 |
ctx->LineFunc = rgbmode ? smooth_rgba_line : smooth_ci_line; |
| 811 |
} |
| 812 |
else { |
| 813 |
/* Width==1, non-stippled, flat-shaded, any raster ops */ |
| 814 |
ctx->LineFunc = rgbmode ? flat_rgba_line : flat_ci_line; |
| 815 |
} |
| 816 |
} |
| 817 |
} |
| 818 |
else if (ctx->RenderMode==GL_FEEDBACK) { |
| 819 |
ctx->LineFunc = feedback_line; |
| 820 |
} |
| 821 |
else { |
| 822 |
/* GL_SELECT mode */ |
| 823 |
ctx->LineFunc = select_line; |
| 824 |
} |
| 825 |
} |
| 826 |
|
| 827 |
|