1bf215546Sopenharmony_ci/*
2bf215546Sopenharmony_ci * Mesa 3-D graphics library
3bf215546Sopenharmony_ci *
4bf215546Sopenharmony_ci * Copyright (C) 1999-2005  Brian Paul   All Rights Reserved.
5bf215546Sopenharmony_ci *
6bf215546Sopenharmony_ci * Permission is hereby granted, free of charge, to any person obtaining a
7bf215546Sopenharmony_ci * copy of this software and associated documentation files (the "Software"),
8bf215546Sopenharmony_ci * to deal in the Software without restriction, including without limitation
9bf215546Sopenharmony_ci * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10bf215546Sopenharmony_ci * and/or sell copies of the Software, and to permit persons to whom the
11bf215546Sopenharmony_ci * Software is furnished to do so, subject to the following conditions:
12bf215546Sopenharmony_ci *
13bf215546Sopenharmony_ci * The above copyright notice and this permission notice shall be included
14bf215546Sopenharmony_ci * in all copies or substantial portions of the Software.
15bf215546Sopenharmony_ci *
16bf215546Sopenharmony_ci * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
17bf215546Sopenharmony_ci * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18bf215546Sopenharmony_ci * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
19bf215546Sopenharmony_ci * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
20bf215546Sopenharmony_ci * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
21bf215546Sopenharmony_ci * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
22bf215546Sopenharmony_ci * OTHER DEALINGS IN THE SOFTWARE.
23bf215546Sopenharmony_ci */
24bf215546Sopenharmony_ci
25bf215546Sopenharmony_ci
26bf215546Sopenharmony_ci#include "glheader.h"
27bf215546Sopenharmony_ci#include "clip.h"
28bf215546Sopenharmony_ci#include "context.h"
29bf215546Sopenharmony_ci#include "macros.h"
30bf215546Sopenharmony_ci#include "mtypes.h"
31bf215546Sopenharmony_ci
32bf215546Sopenharmony_ci#include "math/m_matrix.h"
33bf215546Sopenharmony_ci#include "api_exec_decl.h"
34bf215546Sopenharmony_ci
35bf215546Sopenharmony_ci#include "state_tracker/st_context.h"
36bf215546Sopenharmony_ci
37bf215546Sopenharmony_ci/**
38bf215546Sopenharmony_ci * Update derived clip plane state.
39bf215546Sopenharmony_ci */
40bf215546Sopenharmony_civoid
41bf215546Sopenharmony_ci_mesa_update_clip_plane(struct gl_context *ctx, GLuint plane)
42bf215546Sopenharmony_ci{
43bf215546Sopenharmony_ci   /* make sure the inverse is up to date */
44bf215546Sopenharmony_ci   if (_math_matrix_is_dirty(ctx->ProjectionMatrixStack.Top))
45bf215546Sopenharmony_ci      _math_matrix_analyse( ctx->ProjectionMatrixStack.Top );
46bf215546Sopenharmony_ci
47bf215546Sopenharmony_ci   /* Clip-Space Plane = Eye-Space Plane * Projection Matrix */
48bf215546Sopenharmony_ci   _mesa_transform_vector(ctx->Transform._ClipUserPlane[plane],
49bf215546Sopenharmony_ci                          ctx->Transform.EyeUserPlane[plane],
50bf215546Sopenharmony_ci                          ctx->ProjectionMatrixStack.Top->inv);
51bf215546Sopenharmony_ci}
52bf215546Sopenharmony_ci
53bf215546Sopenharmony_ci
54bf215546Sopenharmony_civoid GLAPIENTRY
55bf215546Sopenharmony_ci_mesa_ClipPlane( GLenum plane, const GLdouble *eq )
56bf215546Sopenharmony_ci{
57bf215546Sopenharmony_ci   GET_CURRENT_CONTEXT(ctx);
58bf215546Sopenharmony_ci   GLint p;
59bf215546Sopenharmony_ci   GLfloat equation[4];
60bf215546Sopenharmony_ci
61bf215546Sopenharmony_ci   p = (GLint) plane - (GLint) GL_CLIP_PLANE0;
62bf215546Sopenharmony_ci   if (p < 0 || p >= (GLint) ctx->Const.MaxClipPlanes) {
63bf215546Sopenharmony_ci      _mesa_error( ctx, GL_INVALID_ENUM, "glClipPlane" );
64bf215546Sopenharmony_ci      return;
65bf215546Sopenharmony_ci   }
66bf215546Sopenharmony_ci
67bf215546Sopenharmony_ci   equation[0] = (GLfloat) eq[0];
68bf215546Sopenharmony_ci   equation[1] = (GLfloat) eq[1];
69bf215546Sopenharmony_ci   equation[2] = (GLfloat) eq[2];
70bf215546Sopenharmony_ci   equation[3] = (GLfloat) eq[3];
71bf215546Sopenharmony_ci
72bf215546Sopenharmony_ci   /*
73bf215546Sopenharmony_ci    * The equation is transformed by the transpose of the inverse of the
74bf215546Sopenharmony_ci    * current modelview matrix and stored in the resulting eye coordinates.
75bf215546Sopenharmony_ci    *
76bf215546Sopenharmony_ci    * KW: Eqn is then transformed to the current clip space, where user
77bf215546Sopenharmony_ci    * clipping now takes place.  The clip-space equations are recalculated
78bf215546Sopenharmony_ci    * whenever the projection matrix changes.
79bf215546Sopenharmony_ci    */
80bf215546Sopenharmony_ci   if (_math_matrix_is_dirty(ctx->ModelviewMatrixStack.Top))
81bf215546Sopenharmony_ci      _math_matrix_analyse( ctx->ModelviewMatrixStack.Top );
82bf215546Sopenharmony_ci
83bf215546Sopenharmony_ci   _mesa_transform_vector( equation, equation,
84bf215546Sopenharmony_ci                           ctx->ModelviewMatrixStack.Top->inv );
85bf215546Sopenharmony_ci
86bf215546Sopenharmony_ci   if (TEST_EQ_4V(ctx->Transform.EyeUserPlane[p], equation))
87bf215546Sopenharmony_ci      return;
88bf215546Sopenharmony_ci
89bf215546Sopenharmony_ci   /* EyeUserPlane is used by program state constants. */
90bf215546Sopenharmony_ci   FLUSH_VERTICES(ctx, _NEW_TRANSFORM, GL_TRANSFORM_BIT);
91bf215546Sopenharmony_ci   ctx->NewDriverState |= ST_NEW_CLIP_STATE;
92bf215546Sopenharmony_ci   COPY_4FV(ctx->Transform.EyeUserPlane[p], equation);
93bf215546Sopenharmony_ci
94bf215546Sopenharmony_ci   if (ctx->Transform.ClipPlanesEnabled & (1 << p)) {
95bf215546Sopenharmony_ci      _mesa_update_clip_plane(ctx, p);
96bf215546Sopenharmony_ci   }
97bf215546Sopenharmony_ci}
98bf215546Sopenharmony_ci
99bf215546Sopenharmony_ci
100bf215546Sopenharmony_civoid GLAPIENTRY
101bf215546Sopenharmony_ci_mesa_GetClipPlane( GLenum plane, GLdouble *equation )
102bf215546Sopenharmony_ci{
103bf215546Sopenharmony_ci   GET_CURRENT_CONTEXT(ctx);
104bf215546Sopenharmony_ci   GLint p;
105bf215546Sopenharmony_ci
106bf215546Sopenharmony_ci   p = (GLint) (plane - GL_CLIP_PLANE0);
107bf215546Sopenharmony_ci   if (p < 0 || p >= (GLint) ctx->Const.MaxClipPlanes) {
108bf215546Sopenharmony_ci      _mesa_error( ctx, GL_INVALID_ENUM, "glGetClipPlane" );
109bf215546Sopenharmony_ci      return;
110bf215546Sopenharmony_ci   }
111bf215546Sopenharmony_ci
112bf215546Sopenharmony_ci   equation[0] = (GLdouble) ctx->Transform.EyeUserPlane[p][0];
113bf215546Sopenharmony_ci   equation[1] = (GLdouble) ctx->Transform.EyeUserPlane[p][1];
114bf215546Sopenharmony_ci   equation[2] = (GLdouble) ctx->Transform.EyeUserPlane[p][2];
115bf215546Sopenharmony_ci   equation[3] = (GLdouble) ctx->Transform.EyeUserPlane[p][3];
116bf215546Sopenharmony_ci}
117