1 /**************************************************************************
3 * Copyright 2007 Tungsten Graphics, Inc., Cedar Park, Texas.
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the
8 * "Software"), to deal in the Software without restriction, including
9 * without limitation the rights to use, copy, modify, merge, publish,
10 * distribute, sub license, and/or sell copies of the Software, and to
11 * permit persons to whom the Software is furnished to do so, subject to
12 * the following conditions:
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
21 * IN NO EVENT SHALL TUNGSTEN GRAPHICS AND/OR ITS SUPPLIERS BE LIABLE FOR
22 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
23 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
24 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
26 **************************************************************************/
28 /* Authors: Keith Whitwell <keith@tungstengraphics.com>
31 #include "pipe/p_util.h"
32 #include "pipe/p_defines.h"
33 #include "pipe/p_shader_tokens.h"
34 #include "draw_private.h"
37 struct wideline_stage
{
38 struct draw_stage stage
;
40 float half_line_width
;
45 static INLINE
struct wideline_stage
*wideline_stage( struct draw_stage
*stage
)
47 return (struct wideline_stage
*)stage
;
51 static void wideline_point( struct draw_stage
*stage
,
52 struct prim_header
*header
)
54 stage
->next
->point( stage
->next
, header
);
58 static void wideline_tri( struct draw_stage
*stage
,
59 struct prim_header
*header
)
61 stage
->next
->tri(stage
->next
, header
);
66 * Draw a wide line by drawing a quad (two triangles).
67 * XXX need to disable polygon stipple.
69 static void wideline_line( struct draw_stage
*stage
,
70 struct prim_header
*header
)
72 /*const struct wideline_stage *wide = wideline_stage(stage);*/
73 const float half_width
= 0.5f
* stage
->draw
->rasterizer
->line_width
;
75 struct prim_header tri
;
77 struct vertex_header
*v0
= dup_vert(stage
, header
->v
[0], 0);
78 struct vertex_header
*v1
= dup_vert(stage
, header
->v
[0], 1);
79 struct vertex_header
*v2
= dup_vert(stage
, header
->v
[1], 2);
80 struct vertex_header
*v3
= dup_vert(stage
, header
->v
[1], 3);
82 float *pos0
= v0
->data
[0];
83 float *pos1
= v1
->data
[0];
84 float *pos2
= v2
->data
[0];
85 float *pos3
= v3
->data
[0];
87 const float dx
= FABSF(pos0
[0] - pos2
[0]);
88 const float dy
= FABSF(pos0
[1] - pos2
[1]);
90 /* small tweak to meet GL specification */
91 const float bias
= 0.125f
;
94 * Draw wide line as a quad (two tris) by "stretching" the line along
96 * We need to tweak coords in several ways to be conformant here.
101 pos0
[1] = pos0
[1] - half_width
- bias
;
102 pos1
[1] = pos1
[1] + half_width
- bias
;
103 pos2
[1] = pos2
[1] - half_width
- bias
;
104 pos3
[1] = pos3
[1] + half_width
- bias
;
105 if (pos0
[0] < pos2
[0]) {
106 /* left to right line */
113 /* right to left line */
122 pos0
[0] = pos0
[0] - half_width
+ bias
;
123 pos1
[0] = pos1
[0] + half_width
+ bias
;
124 pos2
[0] = pos2
[0] - half_width
+ bias
;
125 pos3
[0] = pos3
[0] + half_width
+ bias
;
126 if (pos0
[1] < pos2
[1]) {
127 /* top to bottom line */
134 /* bottom to top line */
142 tri
.det
= header
->det
; /* only the sign matters */
146 stage
->next
->tri( stage
->next
, &tri
);
151 stage
->next
->tri( stage
->next
, &tri
);
155 static void wideline_flush( struct draw_stage
*stage
, unsigned flags
)
157 stage
->next
->flush( stage
->next
, flags
);
161 static void wideline_reset_stipple_counter( struct draw_stage
*stage
)
163 stage
->next
->reset_stipple_counter( stage
->next
);
167 static void wideline_destroy( struct draw_stage
*stage
)
169 draw_free_temp_verts( stage
);
174 struct draw_stage
*draw_wide_line_stage( struct draw_context
*draw
)
176 struct wideline_stage
*wide
= CALLOC_STRUCT(wideline_stage
);
178 draw_alloc_temp_verts( &wide
->stage
, 4 );
180 wide
->stage
.draw
= draw
;
181 wide
->stage
.next
= NULL
;
182 wide
->stage
.point
= wideline_point
;
183 wide
->stage
.line
= wideline_line
;
184 wide
->stage
.tri
= wideline_tri
;
185 wide
->stage
.flush
= wideline_flush
;
186 wide
->stage
.reset_stipple_counter
= wideline_reset_stipple_counter
;
187 wide
->stage
.destroy
= wideline_destroy
;