clip.c revision 01e04c3f
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{
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
587117f1b4Smrg   p = (GLint) plane - (GLint) GL_CLIP_PLANE0;
597117f1b4Smrg   if (p < 0 || p >= (GLint) ctx->Const.MaxClipPlanes) {
607117f1b4Smrg      _mesa_error( ctx, GL_INVALID_ENUM, "glClipPlane" );
617117f1b4Smrg      return;
627117f1b4Smrg   }
637117f1b4Smrg
647117f1b4Smrg   equation[0] = (GLfloat) eq[0];
657117f1b4Smrg   equation[1] = (GLfloat) eq[1];
667117f1b4Smrg   equation[2] = (GLfloat) eq[2];
677117f1b4Smrg   equation[3] = (GLfloat) eq[3];
687117f1b4Smrg
697117f1b4Smrg   /*
707117f1b4Smrg    * The equation is transformed by the transpose of the inverse of the
717117f1b4Smrg    * current modelview matrix and stored in the resulting eye coordinates.
727117f1b4Smrg    *
737117f1b4Smrg    * KW: Eqn is then transformed to the current clip space, where user
747117f1b4Smrg    * clipping now takes place.  The clip-space equations are recalculated
757117f1b4Smrg    * whenever the projection matrix changes.
767117f1b4Smrg    */
777117f1b4Smrg   if (_math_matrix_is_dirty(ctx->ModelviewMatrixStack.Top))
787117f1b4Smrg      _math_matrix_analyse( ctx->ModelviewMatrixStack.Top );
797117f1b4Smrg
807117f1b4Smrg   _mesa_transform_vector( equation, equation,
817117f1b4Smrg                           ctx->ModelviewMatrixStack.Top->inv );
827117f1b4Smrg
837117f1b4Smrg   if (TEST_EQ_4V(ctx->Transform.EyeUserPlane[p], equation))
847117f1b4Smrg      return;
857117f1b4Smrg
8601e04c3fSmrg   /* EyeUserPlane is used by program state constants. */
877117f1b4Smrg   FLUSH_VERTICES(ctx, _NEW_TRANSFORM);
8801e04c3fSmrg   ctx->NewDriverState |= ctx->DriverFlags.NewClipPlane;
897117f1b4Smrg   COPY_4FV(ctx->Transform.EyeUserPlane[p], equation);
907117f1b4Smrg
917117f1b4Smrg   if (ctx->Transform.ClipPlanesEnabled & (1 << p)) {
923464ebd5Sriastradh      _mesa_update_clip_plane(ctx, p);
937117f1b4Smrg   }
947117f1b4Smrg
957117f1b4Smrg   if (ctx->Driver.ClipPlane)
967117f1b4Smrg      ctx->Driver.ClipPlane( ctx, plane, equation );
977117f1b4Smrg}
987117f1b4Smrg
997117f1b4Smrg
1007117f1b4Smrgvoid GLAPIENTRY
1017117f1b4Smrg_mesa_GetClipPlane( GLenum plane, GLdouble *equation )
1027117f1b4Smrg{
1037117f1b4Smrg   GET_CURRENT_CONTEXT(ctx);
1047117f1b4Smrg   GLint p;
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