clip.c revision 6dc85575000127630489b407c50a4b3ea87c9acb
1
2/*
3 * Mesa 3-D graphics library
4 * Version:  5.1
5 *
6 * Copyright (C) 1999-2001  Brian Paul   All Rights Reserved.
7 *
8 * Permission is hereby granted, free of charge, to any person obtaining a
9 * copy of this software and associated documentation files (the "Software"),
10 * to deal in the Software without restriction, including without limitation
11 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
12 * and/or sell copies of the Software, and to permit persons to whom the
13 * Software is furnished to do so, subject to the following conditions:
14 *
15 * The above copyright notice and this permission notice shall be included
16 * in all copies or substantial portions 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 MERCHANTABILITY,
20 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
21 * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
22 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
23 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24 */
25
26
27#include "glheader.h"
28#include "clip.h"
29#include "context.h"
30#include "macros.h"
31#include "mtypes.h"
32
33#include "math/m_xform.h"
34#include "math/m_matrix.h"
35
36
37
38/**********************************************************************/
39/*                     Get/Set User clip-planes.                      */
40/**********************************************************************/
41
42
43
44void
45_mesa_ClipPlane( GLenum plane, const GLdouble *eq )
46{
47   GET_CURRENT_CONTEXT(ctx);
48   GLint p;
49   GLfloat equation[4];
50   ASSERT_OUTSIDE_BEGIN_END(ctx);
51
52   p = (GLint) plane - (GLint) GL_CLIP_PLANE0;
53   if (p < 0 || p >= (GLint) ctx->Const.MaxClipPlanes) {
54      _mesa_error( ctx, GL_INVALID_ENUM, "glClipPlane" );
55      return;
56   }
57
58   equation[0] = (GLfloat) eq[0];
59   equation[1] = (GLfloat) eq[1];
60   equation[2] = (GLfloat) eq[2];
61   equation[3] = (GLfloat) eq[3];
62
63   /*
64    * The equation is transformed by the transpose of the inverse of the
65    * current modelview matrix and stored in the resulting eye coordinates.
66    *
67    * KW: Eqn is then transformed to the current clip space, where user
68    * clipping now takes place.  The clip-space equations are recalculated
69    * whenever the projection matrix changes.
70    */
71   if (ctx->ModelviewMatrixStack.Top->flags & MAT_DIRTY)
72      _math_matrix_analyse( ctx->ModelviewMatrixStack.Top );
73
74   _mesa_transform_vector( equation, equation,
75                           ctx->ModelviewMatrixStack.Top->inv );
76
77   if (TEST_EQ_4V(ctx->Transform.EyeUserPlane[p], equation))
78      return;
79
80   FLUSH_VERTICES(ctx, _NEW_TRANSFORM);
81   COPY_4FV(ctx->Transform.EyeUserPlane[p], equation);
82
83   /* Update derived state.  This state also depends on the projection
84    * matrix, and is recalculated on changes to the projection matrix by
85    * code in _mesa_update_state().
86    */
87   if (ctx->Transform.ClipPlanesEnabled & (1 << p)) {
88      if (ctx->ProjectionMatrixStack.Top->flags & MAT_DIRTY)
89         _math_matrix_analyse( ctx->ProjectionMatrixStack.Top );
90
91      _mesa_transform_vector( ctx->Transform._ClipUserPlane[p],
92			   ctx->Transform.EyeUserPlane[p],
93			   ctx->ProjectionMatrixStack.Top->inv );
94   }
95
96   if (ctx->Driver.ClipPlane)
97      ctx->Driver.ClipPlane( ctx, plane, equation );
98}
99
100
101void
102_mesa_GetClipPlane( GLenum plane, GLdouble *equation )
103{
104   GET_CURRENT_CONTEXT(ctx);
105   GLint p;
106   ASSERT_OUTSIDE_BEGIN_END(ctx);
107
108   p = (GLint) (plane - GL_CLIP_PLANE0);
109   if (p < 0 || p >= (GLint) ctx->Const.MaxClipPlanes) {
110      _mesa_error( ctx, GL_INVALID_ENUM, "glGetClipPlane" );
111      return;
112   }
113
114   equation[0] = (GLdouble) ctx->Transform.EyeUserPlane[p][0];
115   equation[1] = (GLdouble) ctx->Transform.EyeUserPlane[p][1];
116   equation[2] = (GLdouble) ctx->Transform.EyeUserPlane[p][2];
117   equation[3] = (GLdouble) ctx->Transform.EyeUserPlane[p][3];
118}
119
120