Clone of mesa.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

draw_pipe_wide_line.c 5.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. /**************************************************************************
  2. *
  3. * Copyright 2007 Tungsten Graphics, Inc., Cedar Park, Texas.
  4. * All Rights Reserved.
  5. *
  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:
  13. *
  14. * The above copyright notice and this permission notice (including the
  15. * next paragraph) shall be included in all copies or substantial portions
  16. * of the Software.
  17. *
  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.
  25. *
  26. **************************************************************************/
  27. /* Authors: Keith Whitwell <keith@tungstengraphics.com>
  28. */
  29. #include "pipe/p_util.h"
  30. #include "pipe/p_defines.h"
  31. #include "pipe/p_shader_tokens.h"
  32. #include "draw_private.h"
  33. #include "draw_pipe.h"
  34. struct wideline_stage {
  35. struct draw_stage stage;
  36. float half_line_width;
  37. };
  38. static INLINE struct wideline_stage *wideline_stage( struct draw_stage *stage )
  39. {
  40. return (struct wideline_stage *)stage;
  41. }
  42. /**
  43. * Draw a wide line by drawing a quad (two triangles).
  44. * XXX need to disable polygon stipple.
  45. */
  46. static void wideline_line( struct draw_stage *stage,
  47. struct prim_header *header )
  48. {
  49. /*const struct wideline_stage *wide = wideline_stage(stage);*/
  50. const float half_width = 0.5f * stage->draw->rasterizer->line_width;
  51. struct prim_header tri;
  52. struct vertex_header *v0 = dup_vert(stage, header->v[0], 0);
  53. struct vertex_header *v1 = dup_vert(stage, header->v[0], 1);
  54. struct vertex_header *v2 = dup_vert(stage, header->v[1], 2);
  55. struct vertex_header *v3 = dup_vert(stage, header->v[1], 3);
  56. float *pos0 = v0->data[0];
  57. float *pos1 = v1->data[0];
  58. float *pos2 = v2->data[0];
  59. float *pos3 = v3->data[0];
  60. const float dx = FABSF(pos0[0] - pos2[0]);
  61. const float dy = FABSF(pos0[1] - pos2[1]);
  62. /* small tweak to meet GL specification */
  63. const float bias = 0.125f;
  64. /*
  65. * Draw wide line as a quad (two tris) by "stretching" the line along
  66. * X or Y.
  67. * We need to tweak coords in several ways to be conformant here.
  68. */
  69. if (dx > dy) {
  70. /* x-major line */
  71. pos0[1] = pos0[1] - half_width - bias;
  72. pos1[1] = pos1[1] + half_width - bias;
  73. pos2[1] = pos2[1] - half_width - bias;
  74. pos3[1] = pos3[1] + half_width - bias;
  75. if (pos0[0] < pos2[0]) {
  76. /* left to right line */
  77. pos0[0] -= 0.5f;
  78. pos1[0] -= 0.5f;
  79. pos2[0] -= 0.5f;
  80. pos3[0] -= 0.5f;
  81. }
  82. else {
  83. /* right to left line */
  84. pos0[0] += 0.5f;
  85. pos1[0] += 0.5f;
  86. pos2[0] += 0.5f;
  87. pos3[0] += 0.5f;
  88. }
  89. }
  90. else {
  91. /* y-major line */
  92. pos0[0] = pos0[0] - half_width + bias;
  93. pos1[0] = pos1[0] + half_width + bias;
  94. pos2[0] = pos2[0] - half_width + bias;
  95. pos3[0] = pos3[0] + half_width + bias;
  96. if (pos0[1] < pos2[1]) {
  97. /* top to bottom line */
  98. pos0[1] -= 0.5f;
  99. pos1[1] -= 0.5f;
  100. pos2[1] -= 0.5f;
  101. pos3[1] -= 0.5f;
  102. }
  103. else {
  104. /* bottom to top line */
  105. pos0[1] += 0.5f;
  106. pos1[1] += 0.5f;
  107. pos2[1] += 0.5f;
  108. pos3[1] += 0.5f;
  109. }
  110. }
  111. tri.det = header->det; /* only the sign matters */
  112. tri.v[0] = v0;
  113. tri.v[1] = v2;
  114. tri.v[2] = v3;
  115. stage->next->tri( stage->next, &tri );
  116. tri.v[0] = v0;
  117. tri.v[1] = v3;
  118. tri.v[2] = v1;
  119. stage->next->tri( stage->next, &tri );
  120. }
  121. static void wideline_flush( struct draw_stage *stage, unsigned flags )
  122. {
  123. stage->next->flush( stage->next, flags );
  124. }
  125. static void wideline_reset_stipple_counter( struct draw_stage *stage )
  126. {
  127. stage->next->reset_stipple_counter( stage->next );
  128. }
  129. static void wideline_destroy( struct draw_stage *stage )
  130. {
  131. draw_free_temp_verts( stage );
  132. FREE( stage );
  133. }
  134. struct draw_stage *draw_wide_line_stage( struct draw_context *draw )
  135. {
  136. struct wideline_stage *wide = CALLOC_STRUCT(wideline_stage);
  137. draw_alloc_temp_verts( &wide->stage, 4 );
  138. wide->stage.draw = draw;
  139. wide->stage.next = NULL;
  140. wide->stage.point = draw_pipe_passthrough_point;
  141. wide->stage.line = wideline_line;
  142. wide->stage.tri = draw_pipe_passthrough_point;
  143. wide->stage.flush = wideline_flush;
  144. wide->stage.reset_stipple_counter = wideline_reset_stipple_counter;
  145. wide->stage.destroy = wideline_destroy;
  146. return &wide->stage;
  147. }