mesa: for ARB_fbo, lift restriction that all FBO attachments are same size and color...
[mesa.git] / src / mesa / main / clip.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
26 #include "glheader.h"
27 #include "clip.h"
28 #include "context.h"
29 #include "macros.h"
30 #include "mtypes.h"
31
32 #include "math/m_xform.h"
33 #include "math/m_matrix.h"
34
35
36
37 /**********************************************************************/
38 /* Get/Set User clip-planes. */
39 /**********************************************************************/
40
41
42
43 void GLAPIENTRY
44 _mesa_ClipPlane( GLenum plane, const GLdouble *eq )
45 {
46 GET_CURRENT_CONTEXT(ctx);
47 GLint p;
48 GLfloat equation[4];
49 ASSERT_OUTSIDE_BEGIN_END(ctx);
50
51 p = (GLint) plane - (GLint) GL_CLIP_PLANE0;
52 if (p < 0 || p >= (GLint) ctx->Const.MaxClipPlanes) {
53 _mesa_error( ctx, GL_INVALID_ENUM, "glClipPlane" );
54 return;
55 }
56
57 equation[0] = (GLfloat) eq[0];
58 equation[1] = (GLfloat) eq[1];
59 equation[2] = (GLfloat) eq[2];
60 equation[3] = (GLfloat) eq[3];
61
62 /*
63 * The equation is transformed by the transpose of the inverse of the
64 * current modelview matrix and stored in the resulting eye coordinates.
65 *
66 * KW: Eqn is then transformed to the current clip space, where user
67 * clipping now takes place. The clip-space equations are recalculated
68 * whenever the projection matrix changes.
69 */
70 if (_math_matrix_is_dirty(ctx->ModelviewMatrixStack.Top))
71 _math_matrix_analyse( ctx->ModelviewMatrixStack.Top );
72
73 _mesa_transform_vector( equation, equation,
74 ctx->ModelviewMatrixStack.Top->inv );
75
76 if (TEST_EQ_4V(ctx->Transform.EyeUserPlane[p], equation))
77 return;
78
79 FLUSH_VERTICES(ctx, _NEW_TRANSFORM);
80 COPY_4FV(ctx->Transform.EyeUserPlane[p], equation);
81
82 /* Update derived state. This state also depends on the projection
83 * matrix, and is recalculated on changes to the projection matrix by
84 * code in _mesa_update_state().
85 */
86 if (ctx->Transform.ClipPlanesEnabled & (1 << p)) {
87 if (_math_matrix_is_dirty(ctx->ProjectionMatrixStack.Top))
88 _math_matrix_analyse( ctx->ProjectionMatrixStack.Top );
89
90 _mesa_transform_vector( ctx->Transform._ClipUserPlane[p],
91 ctx->Transform.EyeUserPlane[p],
92 ctx->ProjectionMatrixStack.Top->inv );
93 }
94
95 if (ctx->Driver.ClipPlane)
96 ctx->Driver.ClipPlane( ctx, plane, equation );
97 }
98
99
100 void GLAPIENTRY
101 _mesa_GetClipPlane( GLenum plane, GLdouble *equation )
102 {
103 GET_CURRENT_CONTEXT(ctx);
104 GLint p;
105 ASSERT_OUTSIDE_BEGIN_END(ctx);
106
107 p = (GLint) (plane - GL_CLIP_PLANE0);
108 if (p < 0 || p >= (GLint) ctx->Const.MaxClipPlanes) {
109 _mesa_error( ctx, GL_INVALID_ENUM, "glGetClipPlane" );
110 return;
111 }
112
113 equation[0] = (GLdouble) ctx->Transform.EyeUserPlane[p][0];
114 equation[1] = (GLdouble) ctx->Transform.EyeUserPlane[p][1];
115 equation[2] = (GLdouble) ctx->Transform.EyeUserPlane[p][2];
116 equation[3] = (GLdouble) ctx->Transform.EyeUserPlane[p][3];
117 }
118
119 void GLAPIENTRY
120 _mesa_CullParameterfvEXT (GLenum cap, GLfloat *v)
121 {
122 GET_CURRENT_CONTEXT(ctx);
123 ASSERT_OUTSIDE_BEGIN_END(ctx);
124
125 switch (cap) {
126 case GL_CULL_VERTEX_EYE_POSITION_EXT:
127 FLUSH_VERTICES(ctx, _NEW_TRANSFORM);
128 COPY_4FV(ctx->Transform.CullEyePos, v);
129
130 _mesa_transform_vector( ctx->Transform.CullObjPos,
131 ctx->Transform.CullEyePos,
132 ctx->ModelviewMatrixStack.Top->inv );
133 break;
134
135 case GL_CULL_VERTEX_OBJECT_POSITION_EXT:
136 FLUSH_VERTICES(ctx, _NEW_TRANSFORM);
137 COPY_4FV(ctx->Transform.CullObjPos, v);
138
139 _mesa_transform_vector( ctx->Transform.CullEyePos,
140 ctx->Transform.CullObjPos,
141 ctx->ModelviewMatrixStack.Top->m );
142 break;
143 default:
144 _mesa_error( ctx, GL_INVALID_ENUM, "glCullParameterfvEXT" );
145 }
146 }
147
148 void GLAPIENTRY
149 _mesa_CullParameterdvEXT (GLenum cap, GLdouble *v)
150 {
151 GLfloat f[4];
152
153 f[0] = (GLfloat)v[0];
154 f[1] = (GLfloat)v[1];
155 f[2] = (GLfloat)v[2];
156 f[3] = (GLfloat)v[3];
157
158 _mesa_CullParameterfvEXT(cap, f);
159 }
160