merge from master
[mesa.git] / src / mesa / swrast / s_feedback.c
1 /*
2 * Mesa 3-D graphics library
3 * Version: 6.3
4 *
5 * Copyright (C) 1999-2005 Brian Paul All Rights Reserved.
6 *
7 * Permission is hereby granted, free of charge, to any person obtaining a
8 * copy of this software and associated documentation files (the "Software"),
9 * to deal in the Software without restriction, including without limitation
10 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
11 * and/or sell copies of the Software, and to permit persons to whom the
12 * Software is furnished to do so, subject to the following conditions:
13 *
14 * The above copyright notice and this permission notice shall be included
15 * in all copies or substantial portions of the Software.
16 *
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
18 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20 * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
21 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
22 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23 */
24
25 #include "glheader.h"
26 #include "colormac.h"
27 #include "context.h"
28 #include "enums.h"
29 #include "feedback.h"
30 #include "macros.h"
31
32 #include "s_context.h"
33 #include "s_feedback.h"
34 #include "s_triangle.h"
35
36
37 #define FB_3D 0x01
38 #define FB_4D 0x02
39 #define FB_INDEX 0x04
40 #define FB_COLOR 0x08
41 #define FB_TEXTURE 0X10
42
43
44
45
46 static void feedback_vertex( GLcontext *ctx,
47 const SWvertex *v, const SWvertex *pv )
48 {
49 GLfloat win[4];
50 GLfloat color[4];
51 GLfloat tc[4];
52 const GLfloat *vtc = v->attrib[FRAG_ATTRIB_TEX0];
53
54 win[0] = v->win[0];
55 win[1] = v->win[1];
56 win[2] = v->win[2] / ctx->DrawBuffer->_DepthMaxF;
57 win[3] = 1.0F / v->win[3];
58
59 color[0] = CHAN_TO_FLOAT(pv->color[0]);
60 color[1] = CHAN_TO_FLOAT(pv->color[1]);
61 color[2] = CHAN_TO_FLOAT(pv->color[2]);
62 color[3] = CHAN_TO_FLOAT(pv->color[3]);
63
64 if (vtc[3] != 1.0 && vtc[3] != 0.0) {
65 GLfloat invq = 1.0F / vtc[3];
66 tc[0] = vtc[0] * invq;
67 tc[1] = vtc[1] * invq;
68 tc[2] = vtc[2] * invq;
69 tc[3] = vtc[3];
70 }
71 else {
72 COPY_4V(tc, vtc);
73 }
74
75 _mesa_feedback_vertex( ctx, win, color, (GLfloat) v->index, tc );
76 }
77
78
79 /*
80 * Put triangle in feedback buffer.
81 */
82 void _swrast_feedback_triangle( GLcontext *ctx,
83 const SWvertex *v0,
84 const SWvertex *v1,
85 const SWvertex *v2)
86 {
87 if (_swrast_culltriangle( ctx, v0, v1, v2 )) {
88 FEEDBACK_TOKEN( ctx, (GLfloat) (GLint) GL_POLYGON_TOKEN );
89 FEEDBACK_TOKEN( ctx, (GLfloat) 3 ); /* three vertices */
90
91 if (ctx->Light.ShadeModel == GL_SMOOTH) {
92 feedback_vertex( ctx, v0, v0 );
93 feedback_vertex( ctx, v1, v1 );
94 feedback_vertex( ctx, v2, v2 );
95 } else {
96 feedback_vertex( ctx, v0, v2 );
97 feedback_vertex( ctx, v1, v2 );
98 feedback_vertex( ctx, v2, v2 );
99 }
100 }
101 }
102
103
104 void _swrast_feedback_line( GLcontext *ctx, const SWvertex *v0, const SWvertex *v1 )
105 {
106 GLenum token = GL_LINE_TOKEN;
107 SWcontext *swrast = SWRAST_CONTEXT(ctx);
108
109 if (swrast->StippleCounter==0)
110 token = GL_LINE_RESET_TOKEN;
111
112 FEEDBACK_TOKEN( ctx, (GLfloat) (GLint) token );
113
114 if (ctx->Light.ShadeModel == GL_SMOOTH) {
115 feedback_vertex( ctx, v0, v0 );
116 feedback_vertex( ctx, v1, v1 );
117 } else {
118 feedback_vertex( ctx, v0, v1 );
119 feedback_vertex( ctx, v1, v1 );
120 }
121
122 swrast->StippleCounter++;
123 }
124
125
126 void _swrast_feedback_point( GLcontext *ctx, const SWvertex *v )
127 {
128 FEEDBACK_TOKEN( ctx, (GLfloat) (GLint) GL_POINT_TOKEN );
129 feedback_vertex( ctx, v, v );
130 }
131
132
133 void _swrast_select_triangle( GLcontext *ctx,
134 const SWvertex *v0,
135 const SWvertex *v1,
136 const SWvertex *v2)
137 {
138 if (_swrast_culltriangle( ctx, v0, v1, v2 )) {
139 const GLfloat zs = 1.0F / ctx->DrawBuffer->_DepthMaxF;
140
141 _mesa_update_hitflag( ctx, v0->win[2] * zs );
142 _mesa_update_hitflag( ctx, v1->win[2] * zs );
143 _mesa_update_hitflag( ctx, v2->win[2] * zs );
144 }
145 }
146
147
148 void _swrast_select_line( GLcontext *ctx, const SWvertex *v0, const SWvertex *v1 )
149 {
150 const GLfloat zs = 1.0F / ctx->DrawBuffer->_DepthMaxF;
151 _mesa_update_hitflag( ctx, v0->win[2] * zs );
152 _mesa_update_hitflag( ctx, v1->win[2] * zs );
153 }
154
155
156 void _swrast_select_point( GLcontext *ctx, const SWvertex *v )
157 {
158 const GLfloat zs = 1.0F / ctx->DrawBuffer->_DepthMaxF;
159 _mesa_update_hitflag( ctx, v->win[2] * zs );
160 }