Subversion Repositories Kolibri OS

Rev

Blame | Last modification | View Log | RSS feed

  1. /*
  2.  * Mesa 3-D graphics library
  3.  *
  4.  * Copyright (C) 1999-2007  Brian Paul   All Rights Reserved.
  5.  *
  6.  * Permission is hereby granted, free of charge, to any person obtaining a
  7.  * copy of this software and associated documentation files (the "Software"),
  8.  * to deal in the Software without restriction, including without limitation
  9.  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  10.  * and/or sell copies of the Software, and to permit persons to whom the
  11.  * Software is furnished to do so, subject to the following conditions:
  12.  *
  13.  * The above copyright notice and this permission notice shall be included
  14.  * in all copies or substantial portions of the Software.
  15.  *
  16.  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
  17.  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18.  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
  19.  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
  20.  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
  21.  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  22.  * OTHER DEALINGS IN THE SOFTWARE.
  23.  */
  24.  
  25.  
  26. #include "c99_math.h"
  27. #include "main/glheader.h"
  28. #include "main/feedback.h"
  29. #include "main/light.h"
  30. #include "main/macros.h"
  31. #include "util/simple_list.h"
  32. #include "main/mtypes.h"
  33. #include "main/viewport.h"
  34.  
  35. #include "math/m_matrix.h"
  36. #include "tnl/tnl.h"
  37.  
  38.  
  39.  
  40. /**
  41.  * Clip a point against the view volume.
  42.  *
  43.  * \param v vertex vector describing the point to clip.
  44.  *
  45.  * \return zero if outside view volume, or one if inside.
  46.  */
  47. static GLuint
  48. viewclip_point_xy( const GLfloat v[] )
  49. {
  50.    if (   v[0] > v[3] || v[0] < -v[3]
  51.        || v[1] > v[3] || v[1] < -v[3] ) {
  52.       return 0;
  53.    }
  54.    else {
  55.       return 1;
  56.    }
  57. }
  58.  
  59.  
  60. /**
  61.  * Clip a point against the far/near Z clipping planes.
  62.  *
  63.  * \param v vertex vector describing the point to clip.
  64.  *
  65.  * \return zero if outside view volume, or one if inside.
  66.  */
  67. static GLuint
  68. viewclip_point_z( const GLfloat v[] )
  69. {
  70.    if (v[2] > v[3] || v[2] < -v[3] ) {
  71.       return 0;
  72.    }
  73.    else {
  74.       return 1;
  75.    }
  76. }
  77.  
  78.  
  79. /**
  80.  * Clip a point against the user clipping planes.
  81.  *
  82.  * \param ctx GL context.
  83.  * \param v vertex vector describing the point to clip.
  84.  *
  85.  * \return zero if the point was clipped, or one otherwise.
  86.  */
  87. static GLuint
  88. userclip_point( struct gl_context *ctx, const GLfloat v[] )
  89. {
  90.    GLuint p;
  91.  
  92.    for (p = 0; p < ctx->Const.MaxClipPlanes; p++) {
  93.       if (ctx->Transform.ClipPlanesEnabled & (1 << p)) {
  94.          GLfloat dot = v[0] * ctx->Transform._ClipUserPlane[p][0]
  95.                      + v[1] * ctx->Transform._ClipUserPlane[p][1]
  96.                      + v[2] * ctx->Transform._ClipUserPlane[p][2]
  97.                      + v[3] * ctx->Transform._ClipUserPlane[p][3];
  98.          if (dot < 0.0F) {
  99.             return 0;
  100.          }
  101.       }
  102.    }
  103.  
  104.    return 1;
  105. }
  106.  
  107.  
  108. /**
  109.  * Compute lighting for the raster position.  RGB modes computed.
  110.  * \param ctx the context
  111.  * \param vertex vertex location
  112.  * \param normal normal vector
  113.  * \param Rcolor returned color
  114.  * \param Rspec returned specular color (if separate specular enabled)
  115.  */
  116. static void
  117. shade_rastpos(struct gl_context *ctx,
  118.               const GLfloat vertex[4],
  119.               const GLfloat normal[3],
  120.               GLfloat Rcolor[4],
  121.               GLfloat Rspec[4])
  122. {
  123.    /*const*/ GLfloat (*base)[3] = ctx->Light._BaseColor;
  124.    const struct gl_light *light;
  125.    GLfloat diffuseColor[4], specularColor[4];  /* for RGB mode only */
  126.  
  127.    COPY_3V(diffuseColor, base[0]);
  128.    diffuseColor[3] = CLAMP(
  129.       ctx->Light.Material.Attrib[MAT_ATTRIB_FRONT_DIFFUSE][3], 0.0F, 1.0F );
  130.    ASSIGN_4V(specularColor, 0.0, 0.0, 0.0, 1.0);
  131.  
  132.    foreach (light, &ctx->Light.EnabledList) {
  133.       GLfloat attenuation = 1.0;
  134.       GLfloat VP[3]; /* vector from vertex to light pos */
  135.       GLfloat n_dot_VP;
  136.       GLfloat diffuseContrib[3], specularContrib[3];
  137.  
  138.       if (!(light->_Flags & LIGHT_POSITIONAL)) {
  139.          /* light at infinity */
  140.          COPY_3V(VP, light->_VP_inf_norm);
  141.          attenuation = light->_VP_inf_spot_attenuation;
  142.       }
  143.       else {
  144.          /* local/positional light */
  145.          GLfloat d;
  146.  
  147.          /* VP = vector from vertex pos to light[i].pos */
  148.          SUB_3V(VP, light->_Position, vertex);
  149.          /* d = length(VP) */
  150.          d = (GLfloat) LEN_3FV( VP );
  151.          if (d > 1.0e-6) {
  152.             /* normalize VP */
  153.             GLfloat invd = 1.0F / d;
  154.             SELF_SCALE_SCALAR_3V(VP, invd);
  155.          }
  156.  
  157.          /* atti */
  158.          attenuation = 1.0F / (light->ConstantAttenuation + d *
  159.                                (light->LinearAttenuation + d *
  160.                                 light->QuadraticAttenuation));
  161.  
  162.          if (light->_Flags & LIGHT_SPOT) {
  163.             GLfloat PV_dot_dir = - DOT3(VP, light->_NormSpotDirection);
  164.  
  165.             if (PV_dot_dir<light->_CosCutoff) {
  166.                continue;
  167.             }
  168.             else {
  169.                GLfloat spot = powf(PV_dot_dir, light->SpotExponent);
  170.                attenuation *= spot;
  171.             }
  172.          }
  173.       }
  174.  
  175.       if (attenuation < 1e-3)
  176.          continue;
  177.  
  178.       n_dot_VP = DOT3( normal, VP );
  179.  
  180.       if (n_dot_VP < 0.0F) {
  181.          ACC_SCALE_SCALAR_3V(diffuseColor, attenuation, light->_MatAmbient[0]);
  182.          continue;
  183.       }
  184.  
  185.       /* Ambient + diffuse */
  186.       COPY_3V(diffuseContrib, light->_MatAmbient[0]);
  187.       ACC_SCALE_SCALAR_3V(diffuseContrib, n_dot_VP, light->_MatDiffuse[0]);
  188.  
  189.       /* Specular */
  190.       {
  191.          const GLfloat *h;
  192.          GLfloat n_dot_h;
  193.  
  194.          ASSIGN_3V(specularContrib, 0.0, 0.0, 0.0);
  195.  
  196.          if (ctx->Light.Model.LocalViewer) {
  197.             GLfloat v[3];
  198.             COPY_3V(v, vertex);
  199.             NORMALIZE_3FV(v);
  200.             SUB_3V(VP, VP, v);
  201.             NORMALIZE_3FV(VP);
  202.             h = VP;
  203.          }
  204.          else if (light->_Flags & LIGHT_POSITIONAL) {
  205.             ACC_3V(VP, ctx->_EyeZDir);
  206.             NORMALIZE_3FV(VP);
  207.             h = VP;
  208.          }
  209.          else {
  210.             h = light->_h_inf_norm;
  211.          }
  212.  
  213.          n_dot_h = DOT3(normal, h);
  214.  
  215.          if (n_dot_h > 0.0F) {
  216.             GLfloat shine;
  217.             GLfloat spec_coef;
  218.  
  219.             shine = ctx->Light.Material.Attrib[MAT_ATTRIB_FRONT_SHININESS][0];
  220.             spec_coef = powf(n_dot_h, shine);
  221.  
  222.             if (spec_coef > 1.0e-10) {
  223.                if (ctx->Light.Model.ColorControl==GL_SEPARATE_SPECULAR_COLOR) {
  224.                   ACC_SCALE_SCALAR_3V( specularContrib, spec_coef,
  225.                                        light->_MatSpecular[0]);
  226.                }
  227.                else {
  228.                   ACC_SCALE_SCALAR_3V( diffuseContrib, spec_coef,
  229.                                        light->_MatSpecular[0]);
  230.                }
  231.             }
  232.          }
  233.       }
  234.  
  235.       ACC_SCALE_SCALAR_3V( diffuseColor, attenuation, diffuseContrib );
  236.       ACC_SCALE_SCALAR_3V( specularColor, attenuation, specularContrib );
  237.    }
  238.  
  239.    Rcolor[0] = CLAMP(diffuseColor[0], 0.0F, 1.0F);
  240.    Rcolor[1] = CLAMP(diffuseColor[1], 0.0F, 1.0F);
  241.    Rcolor[2] = CLAMP(diffuseColor[2], 0.0F, 1.0F);
  242.    Rcolor[3] = CLAMP(diffuseColor[3], 0.0F, 1.0F);
  243.    Rspec[0] = CLAMP(specularColor[0], 0.0F, 1.0F);
  244.    Rspec[1] = CLAMP(specularColor[1], 0.0F, 1.0F);
  245.    Rspec[2] = CLAMP(specularColor[2], 0.0F, 1.0F);
  246.    Rspec[3] = CLAMP(specularColor[3], 0.0F, 1.0F);
  247. }
  248.  
  249.  
  250. /**
  251.  * Do texgen needed for glRasterPos.
  252.  * \param ctx  rendering context
  253.  * \param vObj  object-space vertex coordinate
  254.  * \param vEye  eye-space vertex coordinate
  255.  * \param normal  vertex normal
  256.  * \param unit  texture unit number
  257.  * \param texcoord  incoming texcoord and resulting texcoord
  258.  */
  259. static void
  260. compute_texgen(struct gl_context *ctx, const GLfloat vObj[4], const GLfloat vEye[4],
  261.                const GLfloat normal[3], GLuint unit, GLfloat texcoord[4])
  262. {
  263.    const struct gl_texture_unit *texUnit = &ctx->Texture.Unit[unit];
  264.  
  265.    /* always compute sphere map terms, just in case */
  266.    GLfloat u[3], two_nu, rx, ry, rz, m, mInv;
  267.    COPY_3V(u, vEye);
  268.    NORMALIZE_3FV(u);
  269.    two_nu = 2.0F * DOT3(normal, u);
  270.    rx = u[0] - normal[0] * two_nu;
  271.    ry = u[1] - normal[1] * two_nu;
  272.    rz = u[2] - normal[2] * two_nu;
  273.    m = rx * rx + ry * ry + (rz + 1.0F) * (rz + 1.0F);
  274.    if (m > 0.0F)
  275.       mInv = 0.5F * (1.0f / sqrtf(m));
  276.    else
  277.       mInv = 0.0F;
  278.  
  279.    if (texUnit->TexGenEnabled & S_BIT) {
  280.       switch (texUnit->GenS.Mode) {
  281.          case GL_OBJECT_LINEAR:
  282.             texcoord[0] = DOT4(vObj, texUnit->GenS.ObjectPlane);
  283.             break;
  284.          case GL_EYE_LINEAR:
  285.             texcoord[0] = DOT4(vEye, texUnit->GenS.EyePlane);
  286.             break;
  287.          case GL_SPHERE_MAP:
  288.             texcoord[0] = rx * mInv + 0.5F;
  289.             break;
  290.          case GL_REFLECTION_MAP:
  291.             texcoord[0] = rx;
  292.             break;
  293.          case GL_NORMAL_MAP:
  294.             texcoord[0] = normal[0];
  295.             break;
  296.          default:
  297.             _mesa_problem(ctx, "Bad S texgen in compute_texgen()");
  298.             return;
  299.       }
  300.    }
  301.  
  302.    if (texUnit->TexGenEnabled & T_BIT) {
  303.       switch (texUnit->GenT.Mode) {
  304.          case GL_OBJECT_LINEAR:
  305.             texcoord[1] = DOT4(vObj, texUnit->GenT.ObjectPlane);
  306.             break;
  307.          case GL_EYE_LINEAR:
  308.             texcoord[1] = DOT4(vEye, texUnit->GenT.EyePlane);
  309.             break;
  310.          case GL_SPHERE_MAP:
  311.             texcoord[1] = ry * mInv + 0.5F;
  312.             break;
  313.          case GL_REFLECTION_MAP:
  314.             texcoord[1] = ry;
  315.             break;
  316.          case GL_NORMAL_MAP:
  317.             texcoord[1] = normal[1];
  318.             break;
  319.          default:
  320.             _mesa_problem(ctx, "Bad T texgen in compute_texgen()");
  321.             return;
  322.       }
  323.    }
  324.  
  325.    if (texUnit->TexGenEnabled & R_BIT) {
  326.       switch (texUnit->GenR.Mode) {
  327.          case GL_OBJECT_LINEAR:
  328.             texcoord[2] = DOT4(vObj, texUnit->GenR.ObjectPlane);
  329.             break;
  330.          case GL_EYE_LINEAR:
  331.             texcoord[2] = DOT4(vEye, texUnit->GenR.EyePlane);
  332.             break;
  333.          case GL_REFLECTION_MAP:
  334.             texcoord[2] = rz;
  335.             break;
  336.          case GL_NORMAL_MAP:
  337.             texcoord[2] = normal[2];
  338.             break;
  339.          default:
  340.             _mesa_problem(ctx, "Bad R texgen in compute_texgen()");
  341.             return;
  342.       }
  343.    }
  344.  
  345.    if (texUnit->TexGenEnabled & Q_BIT) {
  346.       switch (texUnit->GenQ.Mode) {
  347.          case GL_OBJECT_LINEAR:
  348.             texcoord[3] = DOT4(vObj, texUnit->GenQ.ObjectPlane);
  349.             break;
  350.          case GL_EYE_LINEAR:
  351.             texcoord[3] = DOT4(vEye, texUnit->GenQ.EyePlane);
  352.             break;
  353.          default:
  354.             _mesa_problem(ctx, "Bad Q texgen in compute_texgen()");
  355.             return;
  356.       }
  357.    }
  358. }
  359.  
  360.  
  361. /**
  362.  * glRasterPos transformation.  Typically called via ctx->Driver.RasterPos().
  363.  * XXX some of this code (such as viewport xform, clip testing and setting
  364.  * of ctx->Current.Raster* fields) could get lifted up into the
  365.  * main/rasterpos.c code.
  366.  *
  367.  * \param vObj  vertex position in object space
  368.  */
  369. void
  370. _tnl_RasterPos(struct gl_context *ctx, const GLfloat vObj[4])
  371. {
  372.    if (ctx->VertexProgram._Enabled) {
  373.       /* XXX implement this */
  374.       _mesa_problem(ctx, "Vertex programs not implemented for glRasterPos");
  375.       return;
  376.    }
  377.    else {
  378.       GLfloat eye[4], clip[4], ndc[3], d;
  379.       GLfloat *norm, eyenorm[3];
  380.       GLfloat *objnorm = ctx->Current.Attrib[VERT_ATTRIB_NORMAL];
  381.       double scale[3], translate[3];
  382.  
  383.       /* apply modelview matrix:  eye = MV * obj */
  384.       TRANSFORM_POINT( eye, ctx->ModelviewMatrixStack.Top->m, vObj );
  385.       /* apply projection matrix:  clip = Proj * eye */
  386.       TRANSFORM_POINT( clip, ctx->ProjectionMatrixStack.Top->m, eye );
  387.  
  388.       /* clip to view volume. */
  389.       if (!ctx->Transform.DepthClamp) {
  390.          if (viewclip_point_z(clip) == 0) {
  391.             ctx->Current.RasterPosValid = GL_FALSE;
  392.             return;
  393.          }
  394.       }
  395.       if (!ctx->Transform.RasterPositionUnclipped) {
  396.          if (viewclip_point_xy(clip) == 0) {
  397.             ctx->Current.RasterPosValid = GL_FALSE;
  398.             return;
  399.          }
  400.       }
  401.  
  402.       /* clip to user clipping planes */
  403.       if (ctx->Transform.ClipPlanesEnabled && !userclip_point(ctx, clip)) {
  404.          ctx->Current.RasterPosValid = GL_FALSE;
  405.          return;
  406.       }
  407.  
  408.       /* ndc = clip / W */
  409.       d = (clip[3] == 0.0F) ? 1.0F : 1.0F / clip[3];
  410.       ndc[0] = clip[0] * d;
  411.       ndc[1] = clip[1] * d;
  412.       ndc[2] = clip[2] * d;
  413.       /* wincoord = viewport_mapping(ndc) */
  414.       _mesa_get_viewport_xform(ctx, 0, scale, translate);
  415.       ctx->Current.RasterPos[0] = ndc[0] * scale[0] + translate[0];
  416.       ctx->Current.RasterPos[1] = ndc[1] * scale[1] + translate[1];
  417.       ctx->Current.RasterPos[2] = ndc[2] * scale[2] + translate[2];
  418.       ctx->Current.RasterPos[3] = clip[3];
  419.  
  420.       if (ctx->Transform.DepthClamp) {
  421.          ctx->Current.RasterPos[3] = CLAMP(ctx->Current.RasterPos[3],
  422.                                            ctx->ViewportArray[0].Near,
  423.                                            ctx->ViewportArray[0].Far);
  424.       }
  425.  
  426.       /* compute raster distance */
  427.       if (ctx->Fog.FogCoordinateSource == GL_FOG_COORDINATE_EXT)
  428.          ctx->Current.RasterDistance = ctx->Current.Attrib[VERT_ATTRIB_FOG][0];
  429.       else
  430.          ctx->Current.RasterDistance =
  431.                         sqrtf( eye[0]*eye[0] + eye[1]*eye[1] + eye[2]*eye[2] );
  432.  
  433.       /* compute transformed normal vector (for lighting or texgen) */
  434.       if (ctx->_NeedEyeCoords) {
  435.          const GLfloat *inv = ctx->ModelviewMatrixStack.Top->inv;
  436.          TRANSFORM_NORMAL( eyenorm, objnorm, inv );
  437.          norm = eyenorm;
  438.       }
  439.       else {
  440.          norm = objnorm;
  441.       }
  442.  
  443.       /* update raster color */
  444.       if (ctx->Light.Enabled) {
  445.          /* lighting */
  446.          shade_rastpos( ctx, vObj, norm,
  447.                         ctx->Current.RasterColor,
  448.                         ctx->Current.RasterSecondaryColor );
  449.       }
  450.       else {
  451.          /* use current color */
  452.          COPY_4FV(ctx->Current.RasterColor,
  453.                   ctx->Current.Attrib[VERT_ATTRIB_COLOR0]);
  454.          COPY_4FV(ctx->Current.RasterSecondaryColor,
  455.                   ctx->Current.Attrib[VERT_ATTRIB_COLOR1]);
  456.       }
  457.  
  458.       /* texture coords */
  459.       {
  460.          GLuint u;
  461.          for (u = 0; u < ctx->Const.MaxTextureCoordUnits; u++) {
  462.             GLfloat tc[4];
  463.             COPY_4V(tc, ctx->Current.Attrib[VERT_ATTRIB_TEX0 + u]);
  464.             if (ctx->Texture.Unit[u].TexGenEnabled) {
  465.                compute_texgen(ctx, vObj, eye, norm, u, tc);
  466.             }
  467.             TRANSFORM_POINT(ctx->Current.RasterTexCoords[u],
  468.                             ctx->TextureMatrixStack[u].Top->m, tc);
  469.          }
  470.       }
  471.  
  472.       ctx->Current.RasterPosValid = GL_TRUE;
  473.    }
  474.  
  475.    if (ctx->RenderMode == GL_SELECT) {
  476.       _mesa_update_hitflag( ctx, ctx->Current.RasterPos[2] );
  477.    }
  478. }
  479.