17117f1b4Smrg/*
27117f1b4Smrg * Mesa 3-D graphics library
37117f1b4Smrg *
47117f1b4Smrg * Copyright (C) 1999-2005  Brian Paul   All Rights Reserved.
57117f1b4Smrg *
67117f1b4Smrg * Permission is hereby granted, free of charge, to any person obtaining a
77117f1b4Smrg * copy of this software and associated documentation files (the "Software"),
87117f1b4Smrg * to deal in the Software without restriction, including without limitation
97117f1b4Smrg * the rights to use, copy, modify, merge, publish, distribute, sublicense,
107117f1b4Smrg * and/or sell copies of the Software, and to permit persons to whom the
117117f1b4Smrg * Software is furnished to do so, subject to the following conditions:
127117f1b4Smrg *
137117f1b4Smrg * The above copyright notice and this permission notice shall be included
147117f1b4Smrg * in all copies or substantial portions of the Software.
157117f1b4Smrg *
167117f1b4Smrg * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
177117f1b4Smrg * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
187117f1b4Smrg * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
19af69d88dSmrg * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
20af69d88dSmrg * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
21af69d88dSmrg * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
22af69d88dSmrg * 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{
417ec681f3Smrg   /* make sure the inverse is up to date */
423464ebd5Sriastradh   if (_math_matrix_is_dirty(ctx->ProjectionMatrixStack.Top))
433464ebd5Sriastradh      _math_matrix_analyse( ctx->ProjectionMatrixStack.Top );
447117f1b4Smrg
453464ebd5Sriastradh   /* Clip-Space Plane = Eye-Space Plane * Projection Matrix */
463464ebd5Sriastradh   _mesa_transform_vector(ctx->Transform._ClipUserPlane[plane],
473464ebd5Sriastradh                          ctx->Transform.EyeUserPlane[plane],
483464ebd5Sriastradh                          ctx->ProjectionMatrixStack.Top->inv);
493464ebd5Sriastradh}
507117f1b4Smrg
517117f1b4Smrg
527117f1b4Smrgvoid GLAPIENTRY
537117f1b4Smrg_mesa_ClipPlane( GLenum plane, const GLdouble *eq )
547117f1b4Smrg{
557117f1b4Smrg   GET_CURRENT_CONTEXT(ctx);
567117f1b4Smrg   GLint p;
577117f1b4Smrg   GLfloat equation[4];
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
8701e04c3fSmrg   /* EyeUserPlane is used by program state constants. */
887ec681f3Smrg   FLUSH_VERTICES(ctx, _NEW_TRANSFORM, GL_TRANSFORM_BIT);
8901e04c3fSmrg   ctx->NewDriverState |= ctx->DriverFlags.NewClipPlane;
907117f1b4Smrg   COPY_4FV(ctx->Transform.EyeUserPlane[p], equation);
917117f1b4Smrg
927117f1b4Smrg   if (ctx->Transform.ClipPlanesEnabled & (1 << p)) {
933464ebd5Sriastradh      _mesa_update_clip_plane(ctx, p);
947117f1b4Smrg   }
957117f1b4Smrg
967117f1b4Smrg   if (ctx->Driver.ClipPlane)
977117f1b4Smrg      ctx->Driver.ClipPlane( ctx, plane, equation );
987117f1b4Smrg}
997117f1b4Smrg
1007117f1b4Smrg
1017117f1b4Smrgvoid GLAPIENTRY
1027117f1b4Smrg_mesa_GetClipPlane( GLenum plane, GLdouble *equation )
1037117f1b4Smrg{
1047117f1b4Smrg   GET_CURRENT_CONTEXT(ctx);
1057117f1b4Smrg   GLint p;
1067117f1b4Smrg
1077117f1b4Smrg   p = (GLint) (plane - GL_CLIP_PLANE0);
1087117f1b4Smrg   if (p < 0 || p >= (GLint) ctx->Const.MaxClipPlanes) {
1097117f1b4Smrg      _mesa_error( ctx, GL_INVALID_ENUM, "glGetClipPlane" );
1107117f1b4Smrg      return;
1117117f1b4Smrg   }
1127117f1b4Smrg
1137117f1b4Smrg   equation[0] = (GLdouble) ctx->Transform.EyeUserPlane[p][0];
1147117f1b4Smrg   equation[1] = (GLdouble) ctx->Transform.EyeUserPlane[p][1];
1157117f1b4Smrg   equation[2] = (GLdouble) ctx->Transform.EyeUserPlane[p][2];
1167117f1b4Smrg   equation[3] = (GLdouble) ctx->Transform.EyeUserPlane[p][3];
1177117f1b4Smrg}
118