clip.c revision 3464ebd5
17117f1b4Smrg/*
27117f1b4Smrg * Mesa 3-D graphics library
37117f1b4Smrg * Version:  6.3
47117f1b4Smrg *
57117f1b4Smrg * Copyright (C) 1999-2005  Brian Paul   All Rights Reserved.
67117f1b4Smrg *
77117f1b4Smrg * Permission is hereby granted, free of charge, to any person obtaining a
87117f1b4Smrg * copy of this software and associated documentation files (the "Software"),
97117f1b4Smrg * to deal in the Software without restriction, including without limitation
107117f1b4Smrg * the rights to use, copy, modify, merge, publish, distribute, sublicense,
117117f1b4Smrg * and/or sell copies of the Software, and to permit persons to whom the
127117f1b4Smrg * Software is furnished to do so, subject to the following conditions:
137117f1b4Smrg *
147117f1b4Smrg * The above copyright notice and this permission notice shall be included
157117f1b4Smrg * in all copies or substantial portions of the Software.
167117f1b4Smrg *
177117f1b4Smrg * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
187117f1b4Smrg * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
197117f1b4Smrg * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
207117f1b4Smrg * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
217117f1b4Smrg * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
227117f1b4Smrg * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
237117f1b4Smrg */
247117f1b4Smrg
257117f1b4Smrg
267117f1b4Smrg#include "glheader.h"
277117f1b4Smrg#include "clip.h"
287117f1b4Smrg#include "context.h"
297117f1b4Smrg#include "macros.h"
307117f1b4Smrg#include "mtypes.h"
317117f1b4Smrg
327117f1b4Smrg#include "math/m_matrix.h"
337117f1b4Smrg
347117f1b4Smrg
353464ebd5Sriastradh/**
363464ebd5Sriastradh * Update derived clip plane state.
373464ebd5Sriastradh */
383464ebd5Sriastradhvoid
393464ebd5Sriastradh_mesa_update_clip_plane(struct gl_context *ctx, GLuint plane)
403464ebd5Sriastradh{
413464ebd5Sriastradh   if (_math_matrix_is_dirty(ctx->ProjectionMatrixStack.Top))
423464ebd5Sriastradh      _math_matrix_analyse( ctx->ProjectionMatrixStack.Top );
437117f1b4Smrg
443464ebd5Sriastradh   /* Clip-Space Plane = Eye-Space Plane * Projection Matrix */
453464ebd5Sriastradh   _mesa_transform_vector(ctx->Transform._ClipUserPlane[plane],
463464ebd5Sriastradh                          ctx->Transform.EyeUserPlane[plane],
473464ebd5Sriastradh                          ctx->ProjectionMatrixStack.Top->inv);
483464ebd5Sriastradh}
497117f1b4Smrg
507117f1b4Smrg
517117f1b4Smrgvoid GLAPIENTRY
527117f1b4Smrg_mesa_ClipPlane( GLenum plane, const GLdouble *eq )
537117f1b4Smrg{
547117f1b4Smrg   GET_CURRENT_CONTEXT(ctx);
557117f1b4Smrg   GLint p;
567117f1b4Smrg   GLfloat equation[4];
577117f1b4Smrg   ASSERT_OUTSIDE_BEGIN_END(ctx);
587117f1b4Smrg
597117f1b4Smrg   p = (GLint) plane - (GLint) GL_CLIP_PLANE0;
607117f1b4Smrg   if (p < 0 || p >= (GLint) ctx->Const.MaxClipPlanes) {
617117f1b4Smrg      _mesa_error( ctx, GL_INVALID_ENUM, "glClipPlane" );
627117f1b4Smrg      return;
637117f1b4Smrg   }
647117f1b4Smrg
657117f1b4Smrg   equation[0] = (GLfloat) eq[0];
667117f1b4Smrg   equation[1] = (GLfloat) eq[1];
677117f1b4Smrg   equation[2] = (GLfloat) eq[2];
687117f1b4Smrg   equation[3] = (GLfloat) eq[3];
697117f1b4Smrg
707117f1b4Smrg   /*
717117f1b4Smrg    * The equation is transformed by the transpose of the inverse of the
727117f1b4Smrg    * current modelview matrix and stored in the resulting eye coordinates.
737117f1b4Smrg    *
747117f1b4Smrg    * KW: Eqn is then transformed to the current clip space, where user
757117f1b4Smrg    * clipping now takes place.  The clip-space equations are recalculated
767117f1b4Smrg    * whenever the projection matrix changes.
777117f1b4Smrg    */
787117f1b4Smrg   if (_math_matrix_is_dirty(ctx->ModelviewMatrixStack.Top))
797117f1b4Smrg      _math_matrix_analyse( ctx->ModelviewMatrixStack.Top );
807117f1b4Smrg
817117f1b4Smrg   _mesa_transform_vector( equation, equation,
827117f1b4Smrg                           ctx->ModelviewMatrixStack.Top->inv );
837117f1b4Smrg
847117f1b4Smrg   if (TEST_EQ_4V(ctx->Transform.EyeUserPlane[p], equation))
857117f1b4Smrg      return;
867117f1b4Smrg
877117f1b4Smrg   FLUSH_VERTICES(ctx, _NEW_TRANSFORM);
887117f1b4Smrg   COPY_4FV(ctx->Transform.EyeUserPlane[p], equation);
897117f1b4Smrg
907117f1b4Smrg   if (ctx->Transform.ClipPlanesEnabled & (1 << p)) {
913464ebd5Sriastradh      _mesa_update_clip_plane(ctx, p);
927117f1b4Smrg   }
937117f1b4Smrg
947117f1b4Smrg   if (ctx->Driver.ClipPlane)
957117f1b4Smrg      ctx->Driver.ClipPlane( ctx, plane, equation );
967117f1b4Smrg}
977117f1b4Smrg
987117f1b4Smrg
997117f1b4Smrgvoid GLAPIENTRY
1007117f1b4Smrg_mesa_GetClipPlane( GLenum plane, GLdouble *equation )
1017117f1b4Smrg{
1027117f1b4Smrg   GET_CURRENT_CONTEXT(ctx);
1037117f1b4Smrg   GLint p;
1047117f1b4Smrg   ASSERT_OUTSIDE_BEGIN_END(ctx);
1057117f1b4Smrg
1067117f1b4Smrg   p = (GLint) (plane - GL_CLIP_PLANE0);
1077117f1b4Smrg   if (p < 0 || p >= (GLint) ctx->Const.MaxClipPlanes) {
1087117f1b4Smrg      _mesa_error( ctx, GL_INVALID_ENUM, "glGetClipPlane" );
1097117f1b4Smrg      return;
1107117f1b4Smrg   }
1117117f1b4Smrg
1127117f1b4Smrg   equation[0] = (GLdouble) ctx->Transform.EyeUserPlane[p][0];
1137117f1b4Smrg   equation[1] = (GLdouble) ctx->Transform.EyeUserPlane[p][1];
1147117f1b4Smrg   equation[2] = (GLdouble) ctx->Transform.EyeUserPlane[p][2];
1157117f1b4Smrg   equation[3] = (GLdouble) ctx->Transform.EyeUserPlane[p][3];
1167117f1b4Smrg}
117