1afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg/*
2afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg * Mesa 3-D graphics library
3049e320f46f3a3daaa36ef67cc680dc504c124d5Brian Paul * Version:  6.3
45e3bc0c2a2bcdf59949410f94c9b705fc1281ce8Jouk Jansen *
5049e320f46f3a3daaa36ef67cc680dc504c124d5Brian Paul * Copyright (C) 1999-2005  Brian Paul   All Rights Reserved.
65e3bc0c2a2bcdf59949410f94c9b705fc1281ce8Jouk Jansen *
7afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg * Permission is hereby granted, free of charge, to any person obtaining a
8afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg * copy of this software and associated documentation files (the "Software"),
9afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg * to deal in the Software without restriction, including without limitation
10afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg * the rights to use, copy, modify, merge, publish, distribute, sublicense,
11afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg * and/or sell copies of the Software, and to permit persons to whom the
12afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg * Software is furnished to do so, subject to the following conditions:
135e3bc0c2a2bcdf59949410f94c9b705fc1281ce8Jouk Jansen *
14afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg * The above copyright notice and this permission notice shall be included
15afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg * in all copies or substantial portions of the Software.
165e3bc0c2a2bcdf59949410f94c9b705fc1281ce8Jouk Jansen *
17afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
18afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
20afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
21afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
22afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg */
24afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg
25afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg
26fbd8f212c3866ec98c1d8c9d3db3ddb7e7c479a5Brian Paul#include "glheader.h"
27afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg#include "clip.h"
28afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg#include "context.h"
29afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg#include "macros.h"
305e3bc0c2a2bcdf59949410f94c9b705fc1281ce8Jouk Jansen#include "mtypes.h"
31e828bc8f61736f6ba2eff7b2d3dd24056c8b86e0Keith Whitwell
3223caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell#include "math/m_matrix.h"
33e828bc8f61736f6ba2eff7b2d3dd24056c8b86e0Keith Whitwell
34e828bc8f61736f6ba2eff7b2d3dd24056c8b86e0Keith Whitwell
35ec4a21b0e21e8c706f26db5308fb267b6979b631Brian Paul/**
36ec4a21b0e21e8c706f26db5308fb267b6979b631Brian Paul * Update derived clip plane state.
37ec4a21b0e21e8c706f26db5308fb267b6979b631Brian Paul */
38ec4a21b0e21e8c706f26db5308fb267b6979b631Brian Paulvoid
39ec4a21b0e21e8c706f26db5308fb267b6979b631Brian Paul_mesa_update_clip_plane(struct gl_context *ctx, GLuint plane)
40ec4a21b0e21e8c706f26db5308fb267b6979b631Brian Paul{
41ec4a21b0e21e8c706f26db5308fb267b6979b631Brian Paul   if (_math_matrix_is_dirty(ctx->ProjectionMatrixStack.Top))
42ec4a21b0e21e8c706f26db5308fb267b6979b631Brian Paul      _math_matrix_analyse( ctx->ProjectionMatrixStack.Top );
43e828bc8f61736f6ba2eff7b2d3dd24056c8b86e0Keith Whitwell
44ec4a21b0e21e8c706f26db5308fb267b6979b631Brian Paul   /* Clip-Space Plane = Eye-Space Plane * Projection Matrix */
45ec4a21b0e21e8c706f26db5308fb267b6979b631Brian Paul   _mesa_transform_vector(ctx->Transform._ClipUserPlane[plane],
46ec4a21b0e21e8c706f26db5308fb267b6979b631Brian Paul                          ctx->Transform.EyeUserPlane[plane],
47ec4a21b0e21e8c706f26db5308fb267b6979b631Brian Paul                          ctx->ProjectionMatrixStack.Top->inv);
48ec4a21b0e21e8c706f26db5308fb267b6979b631Brian Paul}
49afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg
50afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg
51c40d1dd62dd9bcbb97128e37a75d991a8d3b2d8cKendall Bennettvoid GLAPIENTRY
52fbd8f212c3866ec98c1d8c9d3db3ddb7e7c479a5Brian Paul_mesa_ClipPlane( GLenum plane, const GLdouble *eq )
53afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg{
54fbd8f212c3866ec98c1d8c9d3db3ddb7e7c479a5Brian Paul   GET_CURRENT_CONTEXT(ctx);
55afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg   GLint p;
56fbd8f212c3866ec98c1d8c9d3db3ddb7e7c479a5Brian Paul   GLfloat equation[4];
57cab974cf6c2dbfbf5dd5d291e1aae0f8eeb34290Keith Whitwell   ASSERT_OUTSIDE_BEGIN_END(ctx);
58afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg
59afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg   p = (GLint) plane - (GLint) GL_CLIP_PLANE0;
60b51b0a847d7e7daaea69f77ab569086ef81c24a2Brian Paul   if (p < 0 || p >= (GLint) ctx->Const.MaxClipPlanes) {
6108836341788a9f9d638d9dc8328510ccd18ddeb5Brian Paul      _mesa_error( ctx, GL_INVALID_ENUM, "glClipPlane" );
62afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg      return;
63afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg   }
64afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg
65fde5e2c5f182759aff78bdd12e6c928f3f13bbdcBrian Paul   equation[0] = (GLfloat) eq[0];
66fde5e2c5f182759aff78bdd12e6c928f3f13bbdcBrian Paul   equation[1] = (GLfloat) eq[1];
67fde5e2c5f182759aff78bdd12e6c928f3f13bbdcBrian Paul   equation[2] = (GLfloat) eq[2];
68fde5e2c5f182759aff78bdd12e6c928f3f13bbdcBrian Paul   equation[3] = (GLfloat) eq[3];
6923caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell
70afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg   /*
71afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg    * The equation is transformed by the transpose of the inverse of the
72afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg    * current modelview matrix and stored in the resulting eye coordinates.
73afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg    *
74afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg    * KW: Eqn is then transformed to the current clip space, where user
75afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg    * clipping now takes place.  The clip-space equations are recalculated
76afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg    * whenever the projection matrix changes.
77afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg    */
78049e320f46f3a3daaa36ef67cc680dc504c124d5Brian Paul   if (_math_matrix_is_dirty(ctx->ModelviewMatrixStack.Top))
7930f51ae067379c2b3573c06b707d25a9704df7beBrian Paul      _math_matrix_analyse( ctx->ModelviewMatrixStack.Top );
8023caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell
8130f51ae067379c2b3573c06b707d25a9704df7beBrian Paul   _mesa_transform_vector( equation, equation,
8230f51ae067379c2b3573c06b707d25a9704df7beBrian Paul                           ctx->ModelviewMatrixStack.Top->inv );
83afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg
84cab974cf6c2dbfbf5dd5d291e1aae0f8eeb34290Keith Whitwell   if (TEST_EQ_4V(ctx->Transform.EyeUserPlane[p], equation))
85cab974cf6c2dbfbf5dd5d291e1aae0f8eeb34290Keith Whitwell      return;
8622144ab7552f0799bcfca506bf4ffa7f70a06649Gareth Hughes
87cab974cf6c2dbfbf5dd5d291e1aae0f8eeb34290Keith Whitwell   FLUSH_VERTICES(ctx, _NEW_TRANSFORM);
88cab974cf6c2dbfbf5dd5d291e1aae0f8eeb34290Keith Whitwell   COPY_4FV(ctx->Transform.EyeUserPlane[p], equation);
89afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg
90103bc0f75c00dfcf671dc50d8d9666f88a42a59dBrian Paul   if (ctx->Transform.ClipPlanesEnabled & (1 << p)) {
91ec4a21b0e21e8c706f26db5308fb267b6979b631Brian Paul      _mesa_update_clip_plane(ctx, p);
92afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg   }
93a96308c37db0bc0086a017d318bc3504aa5f0b1aKeith Whitwell
94ad2ac216fa0cbebc36530bf9e5256e902710b892Keith Whitwell   if (ctx->Driver.ClipPlane)
95ad2ac216fa0cbebc36530bf9e5256e902710b892Keith Whitwell      ctx->Driver.ClipPlane( ctx, plane, equation );
96afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg}
97afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg
98afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg
99c40d1dd62dd9bcbb97128e37a75d991a8d3b2d8cKendall Bennettvoid GLAPIENTRY
100fbd8f212c3866ec98c1d8c9d3db3ddb7e7c479a5Brian Paul_mesa_GetClipPlane( GLenum plane, GLdouble *equation )
101afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg{
102fbd8f212c3866ec98c1d8c9d3db3ddb7e7c479a5Brian Paul   GET_CURRENT_CONTEXT(ctx);
103afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg   GLint p;
104cab974cf6c2dbfbf5dd5d291e1aae0f8eeb34290Keith Whitwell   ASSERT_OUTSIDE_BEGIN_END(ctx);
105afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg
106afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg   p = (GLint) (plane - GL_CLIP_PLANE0);
107b51b0a847d7e7daaea69f77ab569086ef81c24a2Brian Paul   if (p < 0 || p >= (GLint) ctx->Const.MaxClipPlanes) {
10808836341788a9f9d638d9dc8328510ccd18ddeb5Brian Paul      _mesa_error( ctx, GL_INVALID_ENUM, "glGetClipPlane" );
109afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg      return;
110afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg   }
111afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg
112afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg   equation[0] = (GLdouble) ctx->Transform.EyeUserPlane[p][0];
113afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg   equation[1] = (GLdouble) ctx->Transform.EyeUserPlane[p][1];
114afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg   equation[2] = (GLdouble) ctx->Transform.EyeUserPlane[p][2];
115afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg   equation[3] = (GLdouble) ctx->Transform.EyeUserPlane[p][3];
116afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg}
117