home *** CD-ROM | disk | FTP | other *** search
/ The Utilities Experience / The Utilities Experience - Volume 1.iso / software / misc / o-z / x-windows / mesa-amiwin / src / context.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-11-30  |  29.8 KB  |  1,065 lines

  1. /* context.c */
  2.  
  3. /*
  4.  * Mesa 3-D graphics library
  5.  * Version:  1.2
  6.  * Copyright (C) 1995  Brian Paul  (brianp@ssec.wisc.edu)
  7.  *
  8.  * This library is free software; you can redistribute it and/or
  9.  * modify it under the terms of the GNU Library General Public
  10.  * License as published by the Free Software Foundation; either
  11.  * version 2 of the License, or (at your option) any later version.
  12.  *
  13.  * This library is distributed in the hope that it will be useful,
  14.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  16.  * Library General Public License for more details.
  17.  *
  18.  * You should have received a copy of the GNU Library General Public
  19.  * License along with this library; if not, write to the Free
  20.  * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  21.  */
  22.  
  23.  
  24. /*
  25. $Id: context.c,v 1.32 1995/11/22 13:35:00 brianp Exp $
  26.  
  27. $Log: context.c,v $
  28.  * Revision 1.32  1995/11/22  13:35:00  brianp
  29.  * added MutableColors flag, test for RGBA mode before setting GAMMA_BIT
  30.  *
  31.  * Revision 1.31  1995/11/08  22:09:05  brianp
  32.  * changed GL<type> assertions from == to >=
  33.  *
  34.  * Revision 1.30  1995/11/03  17:41:05  brianp
  35.  * removed unused variables
  36.  *
  37.  * Revision 1.29  1995/11/01  21:44:15  brianp
  38.  * added CC.Light.LastEnabled
  39.  *
  40.  * Revision 1.28  1995/10/27  20:28:27  brianp
  41.  * added glPolygonOffsetEXT() support
  42.  *
  43.  * Revision 1.27  1995/10/19  15:45:20  brianp
  44.  * added gamma support
  45.  * new arguments to gl_new_context()
  46.  *
  47.  * Revision 1.26  1995/10/14  16:26:13  brianp
  48.  * enable dithering by default
  49.  * added SWmasking code
  50.  *
  51.  * Revision 1.25  1995/09/25  16:31:54  brianp
  52.  * reorganized front and back material indexing
  53.  *
  54.  * Revision 1.24  1995/09/15  18:47:32  brianp
  55.  * introduced CC.NewState convention
  56.  * use bitmask flag for CC.Texture.TexGenEnabled
  57.  *
  58.  * Revision 1.23  1995/07/25  16:41:54  brianp
  59.  * made changes for using CC.VertexFunc pointer
  60.  *
  61.  * Revision 1.22  1995/07/24  20:34:16  brianp
  62.  * replaced memset() with MEMSET() and memcpy() with MEMCPY()
  63.  *
  64.  * Revision 1.21  1995/06/19  14:52:37  brianp
  65.  * initialize current texture coordinate, per Asif Khan
  66.  *
  67.  * Revision 1.20  1995/06/05  20:26:24  brianp
  68.  * added Unfilled field to gl_polygon struct
  69.  *
  70.  * Revision 1.19  1995/05/22  21:02:41  brianp
  71.  * Release 1.2
  72.  *
  73.  * Revision 1.18  1995/05/17  13:52:37  brianp
  74.  * implemented glIndexMask(0) and glColorMask(0,0,0,0)
  75.  *
  76.  * Revision 1.17  1995/05/17  13:17:22  brianp
  77.  * changed default CC.Mode value to allow use of real OpenGL headers
  78.  * removed need for CC.MajorMode variable
  79.  *
  80.  * Revision 1.16  1995/05/15  16:07:01  brianp
  81.  * implemented shared/nonshared display lists
  82.  *
  83.  * Revision 1.15  1995/05/12  16:30:14  brianp
  84.  * Texture images stored as bytes, not floats
  85.  *
  86.  * Revision 1.14  1995/04/17  13:51:19  brianp
  87.  * added gl_copy_context() function
  88.  *
  89.  * Revision 1.13  1995/03/27  20:31:26  brianp
  90.  * new Texture.Enabled scheme
  91.  *
  92.  * Revision 1.12  1995/03/24  16:59:56  brianp
  93.  * added gl_update_pixel_logic
  94.  *
  95.  * Revision 1.11  1995/03/24  16:11:41  brianp
  96.  * fixed logicop bug in gl_update_rasterflags
  97.  *
  98.  * Revision 1.10  1995/03/16  20:36:38  brianp
  99.  * added call to gl_update_rasterflags in gl_set_context
  100.  *
  101.  * Revision 1.9  1995/03/10  16:26:43  brianp
  102.  * updated for bleding extensions
  103.  *
  104.  * Revision 1.8  1995/03/09  21:40:14  brianp
  105.  * added ModelViewInvValid initializer
  106.  * added ComputePlane test to gl_update_rasterflags
  107.  *
  108.  * Revision 1.7  1995/03/09  19:07:16  brianp
  109.  * added MESA_DEBUG env var support
  110.  *
  111.  * Revision 1.6  1995/03/08  15:10:02  brianp
  112.  * added support for dd_logicop
  113.  *
  114.  * Revision 1.5  1995/03/04  19:29:44  brianp
  115.  * 1.1 beta revision
  116.  *
  117.  * Revision 1.4  1995/03/02  19:17:54  brianp
  118.  * new RasterMask logic, fixed some comments
  119.  *
  120.  * Revision 1.3  1995/02/27  22:48:28  brianp
  121.  * modified for PB
  122.  *
  123.  * Revision 1.2  1995/02/24  15:19:23  brianp
  124.  * *** empty log message ***
  125.  *
  126.  * Revision 1.1  1995/02/24  14:18:45  brianp
  127.  * Initial revision
  128.  *
  129.  */
  130.  
  131.  
  132. /*
  133.  * The gl_context structure holds the current state of the library.
  134.  * Typically, there will be a gl_context structure associated with each
  135.  * window into which we're rendering:
  136.  *    When we open a new rendering window we need a new gl_context.
  137.  *    When we close a rendering window we destroy its gl_context.
  138.  *    When we switch rendering to a different window we change gl_context.
  139.  *
  140.  * Throughout this implementation, references are made to CC which is
  141.  * the Current Context.  Some information is shared among all contexts:
  142.  *     Texture Images         (incorrect?)
  143.  *     Display Lists          (incorrect)
  144.  *     Evaluator definitions  (incorrect?)
  145.  */
  146.  
  147.  
  148.  
  149. #include <assert.h>
  150. #include <math.h>
  151. #include <stdio.h>
  152. #include <stdlib.h>
  153. #include <string.h>
  154. #include "context.h"
  155. #include "dd.h"
  156. #include "draw.h"
  157. #include "eval.h"
  158. #include "lines.h"
  159. #include "list.h"
  160. #include "macros.h"
  161. #include "pb.h"
  162. #include "points.h"
  163. #include "polygons.h"
  164. #include "vb.h"
  165.  
  166.  
  167.  
  168. /* Copy of Current Context (PUBLIC) */
  169. struct gl_context CC;
  170.  
  171. /* Pointer to Current Context (PRIVATE) */
  172. static struct gl_context *CCptr = NULL;
  173.  
  174.  
  175.  
  176.  
  177. /*
  178.  * Allocate and initialize a display list group.
  179.  */
  180. static struct gl_list_group *alloc_display_list_group( void )
  181. {
  182.    struct gl_list_group *lg;
  183.    GLuint i;
  184.  
  185.    lg = (struct gl_list_group*) malloc( sizeof(struct gl_list_group) );
  186.    for (i=0;i<MAX_DISPLAYLISTS;i++) {
  187.       lg->List[i] = NULL;
  188.       lg->Reserved[i] = GL_FALSE;
  189.    }
  190.    lg->RefCount = 0;
  191.    return lg;
  192. }
  193.  
  194.  
  195.  
  196. /*
  197.  * Initialize the nth light.  Note that the defaults for light 0 are
  198.  * different than the other lights.
  199.  */
  200. static void init_light( struct gl_light *l, GLuint n )
  201. {
  202.    ASSIGN_4V( l->Ambient, 0.0, 0.0, 0.0, 1.0 );
  203.    if (n==0) {
  204.       ASSIGN_4V( l->Diffuse, 1.0, 1.0, 1.0, 1.0 );
  205.       ASSIGN_4V( l->Specular, 1.0, 1.0, 1.0, 1.0 );
  206.    }
  207.    else {
  208.       ASSIGN_4V( l->Diffuse, 0.0, 0.0, 0.0, 1.0 );
  209.       ASSIGN_4V( l->Specular, 0.0, 0.0, 0.0, 1.0 );
  210.    }
  211.    ASSIGN_4V( l->Position, 0.0, 0.0, 1.0, 0.0 );
  212.    ASSIGN_3V( l->NormPosition, 0.0, 0.0, 1.0 );
  213.    ASSIGN_3V( l->Direction, 0.0, 0.0, -1.0 );
  214.    l->SpotExponent = 0.0;
  215.    l->SpotCutoff = 180.0;
  216.    l->ConstantAttenuation = 1.0;
  217.    l->LinearAttenuation = 0.0;
  218.    l->QuadraticAttenuation = 0.0;
  219.    l->Enabled = GL_FALSE;
  220. }
  221.  
  222.  
  223.  
  224. static void init_lightmodel( struct gl_lightmodel *lm )
  225. {
  226.    ASSIGN_4V( lm->Ambient, 0.2, 0.2, 0.2, 1.0 );
  227.    lm->LocalViewer = GL_FALSE;
  228.    lm->TwoSide = GL_FALSE;
  229. }
  230.  
  231.  
  232. static void init_material( struct gl_material *m )
  233. {
  234.    ASSIGN_4V( m->Ambient,  0.2, 0.2, 0.2, 1.0 );
  235.    ASSIGN_4V( m->Diffuse,  0.8, 0.8, 0.8, 1.0 );
  236.    ASSIGN_4V( m->Specular, 0.0, 0.0, 0.0, 1.0 );
  237.    ASSIGN_4V( m->Emission, 0.0, 0.0, 0.0, 1.0 );
  238.    m->Shininess = 0.0;
  239.    m->AmbientIndex = 0;
  240.    m->DiffuseIndex = 1;
  241.    m->SpecularIndex = 1;
  242. }
  243.  
  244.  
  245.  
  246. /*
  247.  * Setup the gamma lookup tables for the given context by examining the
  248.  * MESA_GAMMA environment variable.
  249.  */
  250. static void init_gamma_tables( struct gl_context *c )
  251. {
  252.    char *gamma;
  253.    GLint i, n;
  254.    GLdouble g;
  255.  
  256.    if (!c->RGBAflag) {
  257.       return;
  258.    }
  259.  
  260.    gamma = getenv("MESA_GAMMA");
  261.    if (gamma) {
  262.       sscanf( gamma, "%f %f %f", &c->RedGamma, &c->GreenGamma, &c->BlueGamma );
  263.       if (c->RedGamma==0.0)    c->RedGamma = 1.0;
  264.       if (c->GreenGamma==0.0)  c->GreenGamma = c->RedGamma;
  265.       if (c->BlueGamma==0.0)   c->BlueGamma = c->RedGamma;
  266.    }
  267.    else {
  268.       c->RedGamma = c->GreenGamma = c->BlueGamma = 1.0;
  269.    }
  270.  
  271.    /* red */
  272.    n = (GLint) c->RedScale;
  273.    g = 1.0 / c->RedGamma;
  274.    for (i=0;i<=n;i++) {
  275.       double x = (double) i / c->RedScale;
  276.       c->RedRamp[i] = c->RedScale * pow(x,g) + 0.5;
  277.    }
  278.  
  279.    /* green */
  280.    n = (GLint) c->GreenScale;
  281.    g = 1.0 / c->GreenGamma;
  282.    for (i=0;i<=n;i++) {
  283.       double x = (double) i / c->GreenScale;
  284.       c->GreenRamp[i] = c->GreenScale * pow(x,g) + 0.5;
  285.    }
  286.  
  287.    /* blue */
  288.    n = (GLint) c->BlueScale;
  289.    g = 1.0 / c->BlueGamma;
  290.    for (i=0;i<=n;i++) {
  291.       double x = (double) i / c->BlueScale;
  292.       c->BlueRamp[i] = c->BlueScale * pow(x,g) + 0.5;
  293.    }
  294. }
  295.  
  296.  
  297.  
  298. /*
  299.  * Initialize a gl_context structure to default values.
  300.  */
  301. void gl_initialize_context( struct gl_context *c )
  302. {
  303.    static GLfloat identity[16] = {
  304.     1.0, 0.0, 0.0, 0.0,
  305.     0.0, 1.0, 0.0, 0.0,
  306.     0.0, 0.0, 1.0, 0.0,
  307.     0.0, 0.0, 0.0, 1.0
  308.    };
  309.    GLuint i;
  310.  
  311.    if (c) {
  312.       ASSIGN_4V( c->Accum.ClearColor, 0.0, 0.0, 0.0, 0.0 );
  313.  
  314.       c->Color.IndexMask = 0xffffffff;
  315.       c->Color.ColorMask = 0xf;
  316.       c->Color.SWmasking = GL_FALSE;
  317.       c->Color.ClearIndex = 0;
  318.       ASSIGN_4V( c->Color.ClearColor, 0.0, 0.0, 0.0, 0.0 );
  319.       c->Color.DrawBuffer = GL_FRONT;
  320.       c->Color.AlphaEnabled = GL_FALSE;
  321.       c->Color.AlphaFunc = GL_ALWAYS;
  322.       c->Color.AlphaRef = 0.0;
  323.       c->Color.BlendEnabled = GL_FALSE;
  324.       c->Color.BlendSrc = GL_ONE;
  325.       c->Color.BlendDst = GL_ZERO;
  326.       c->Color.BlendEquation = GL_FUNC_ADD_EXT;
  327.       ASSIGN_4V( c->Color.BlendColor, 0.0, 0.0, 0.0, 0.0 );
  328.       c->Color.LogicOpEnabled = GL_FALSE;
  329.       c->Color.SWLogicOpEnabled = GL_FALSE;
  330.       c->Color.LogicOp = GL_COPY;
  331.       c->Color.DitherFlag = GL_TRUE;
  332.  
  333.       c->Current.Index = 1;
  334.       ASSIGN_3V( c->Current.Normal, 0.0, 0.0, 1.0 );
  335.       ASSIGN_4V( c->Current.Color, 1.0, 1.0, 1.0, 1.0 );
  336.       ASSIGN_4V( c->Current.RasterPos, 0.0, 0.0, 0.0, 1.0 );
  337.       c->Current.RasterPosValid = GL_TRUE;
  338.       c->Current.RasterIndex = 1;
  339.       ASSIGN_4V( c->Current.TexCoord, 0.0, 0.0, 0.0, 1.0 );
  340.       ASSIGN_4V( c->Current.RasterColor, 1.0, 1.0, 1.0, 1.0 );
  341.       c->Current.EdgeFlag = GL_TRUE;
  342.  
  343.       c->Depth.Test = GL_FALSE;
  344.       c->Depth.Clear = 1.0;
  345.       c->Depth.Func = GL_LESS;
  346.       c->Depth.Mask = GL_TRUE;
  347.  
  348.       c->Eval.Map1Color4 = GL_FALSE;
  349.       c->Eval.Map1Index = GL_FALSE;
  350.       c->Eval.Map1Normal = GL_FALSE;
  351.       c->Eval.Map1TextureCoord1 = GL_FALSE;
  352.       c->Eval.Map1TextureCoord2 = GL_FALSE;
  353.       c->Eval.Map1TextureCoord3 = GL_FALSE;
  354.       c->Eval.Map1TextureCoord4 = GL_FALSE;
  355.       c->Eval.Map1Vertex3 = GL_FALSE;
  356.       c->Eval.Map1Vertex4 = GL_FALSE;
  357.       c->Eval.Map2Color4 = GL_FALSE;
  358.       c->Eval.Map2Index = GL_FALSE;
  359.       c->Eval.Map2Normal = GL_FALSE;
  360.       c->Eval.Map2TextureCoord1 = GL_FALSE;
  361.       c->Eval.Map2TextureCoord2 = GL_FALSE;
  362.       c->Eval.Map2TextureCoord3 = GL_FALSE;
  363.       c->Eval.Map2TextureCoord4 = GL_FALSE;
  364.       c->Eval.Map2Vertex3 = GL_FALSE;
  365.       c->Eval.Map2Vertex4 = GL_FALSE;
  366.       c->Eval.AutoNormal = GL_FALSE;
  367.       c->Eval.MapGrid1un = 1;
  368.       c->Eval.MapGrid1u1 = 0.0;
  369.       c->Eval.MapGrid1u2 = 1.0;
  370.       c->Eval.MapGrid2un = 1;
  371.       c->Eval.MapGrid2vn = 1;
  372.       c->Eval.MapGrid2u1 = 0.0;
  373.       c->Eval.MapGrid2u2 = 1.0;
  374.       c->Eval.MapGrid2v1 = 0.0;
  375.       c->Eval.MapGrid2v2 = 1.0;
  376.  
  377.       c->Fog.Enabled = GL_FALSE;
  378.       c->Fog.Mode = GL_EXP;
  379.       ASSIGN_4V( c->Fog.Color, 0.0, 0.0, 0.0, 0.0 );
  380.       c->Fog.Index = 0.0;
  381.       c->Fog.Density = 1.0;
  382.       c->Fog.Start = 0.0;
  383.       c->Fog.End = 1.0;
  384.  
  385.       c->Hint.PerspectiveCorrection = GL_DONT_CARE;
  386.       c->Hint.PointSmooth = GL_DONT_CARE;
  387.       c->Hint.LineSmooth = GL_DONT_CARE;
  388.       c->Hint.PolygonSmooth = GL_DONT_CARE;
  389.       c->Hint.Fog = GL_DONT_CARE;
  390.  
  391.       for (i=0;i<MAX_LIGHTS;i++) {
  392.      init_light( &c->Light.Light[i], i );
  393.       }
  394.       init_lightmodel( &c->Light.Model );
  395.       init_material( &c->Light.Material[0] );
  396.       init_material( &c->Light.Material[1] );
  397.       c->Light.ShadeModel = GL_SMOOTH;
  398.       c->Light.Enabled = GL_FALSE;
  399.       c->Light.ColorMaterialFace = GL_FRONT_AND_BACK;
  400.       c->Light.ColorMaterialMode = GL_AMBIENT_AND_DIFFUSE;
  401.       c->Light.ColorMaterialEnabled = GL_FALSE;
  402.       c->Light.LastEnabled = -1;
  403.  
  404.       c->Line.SmoothFlag = GL_FALSE;
  405.       c->Line.StippleFlag = GL_FALSE;
  406.       c->Line.Width = 1.0;
  407.       c->Line.StipplePattern = 0xffff;
  408.       c->Line.StippleFactor = 1;
  409.  
  410.       c->List.ListBase = 0;
  411.  
  412.       c->Pixel.RedBias = 0.0;
  413.       c->Pixel.RedScale = 1.0;
  414.       c->Pixel.GreenBias = 0.0;
  415.       c->Pixel.GreenScale = 1.0;
  416.       c->Pixel.BlueBias = 0.0;
  417.       c->Pixel.BlueScale = 1.0;
  418.       c->Pixel.AlphaBias = 0.0;
  419.       c->Pixel.AlphaScale = 1.0;
  420.       c->Pixel.DepthBias = 0.0;
  421.       c->Pixel.DepthScale = 1.0;
  422.       c->Pixel.IndexOffset = 0;
  423.       c->Pixel.IndexShift = 0;
  424.       c->Pixel.ZoomX = 1.0;
  425.       c->Pixel.ZoomY = 1.0;
  426.       c->Pixel.MapColorFlag = GL_FALSE;
  427.       c->Pixel.MapStencilFlag = GL_FALSE;
  428.       c->Pixel.MapStoSsize = 1;
  429.       c->Pixel.MapItoIsize = 1;
  430.       c->Pixel.MapItoRsize = 1;
  431.       c->Pixel.MapItoGsize = 1;
  432.       c->Pixel.MapItoBsize = 1;
  433.       c->Pixel.MapItoAsize = 1;
  434.       c->Pixel.MapRtoRsize = 1;
  435.       c->Pixel.MapGtoGsize = 1;
  436.       c->Pixel.MapBtoBsize = 1;
  437.       c->Pixel.MapAtoAsize = 1;
  438.       c->Pixel.MapStoS[0] = 0;
  439.       c->Pixel.MapItoI[0] = 0;
  440.       c->Pixel.MapItoR[0] = 0.0;
  441.       c->Pixel.MapItoG[0] = 0.0;
  442.       c->Pixel.MapItoB[0] = 0.0;
  443.       c->Pixel.MapItoA[0] = 0.0;
  444.       c->Pixel.MapRtoR[0] = 0.0;
  445.       c->Pixel.MapGtoG[0] = 0.0;
  446.       c->Pixel.MapBtoB[0] = 0.0;
  447.       c->Pixel.MapAtoA[0] = 0.0;
  448.  
  449.       c->Point.SmoothFlag = GL_FALSE;
  450.       c->Point.Size = 1.0;
  451.  
  452.       c->Polygon.CullFlag = GL_FALSE;
  453.       c->Polygon.CullFaceMode = GL_BACK;
  454.       c->Polygon.FrontFace = GL_CCW;
  455.       c->Polygon.FrontMode = GL_FILL;
  456.       c->Polygon.BackMode = GL_FILL;
  457.       c->Polygon.Unfilled = GL_FALSE;
  458.       c->Polygon.SmoothFlag = GL_FALSE;
  459.       c->Polygon.StippleFlag = GL_FALSE;
  460.       c->Polygon.OffsetFactor = 0.0;
  461.       c->Polygon.OffsetBias = 0.0;
  462.       c->Polygon.OffsetEnabled = GL_FALSE;
  463.  
  464.       MEMSET( c->PolygonStipple, 0xff, 32*sizeof(GLuint) );
  465.  
  466.       c->Scissor.Enabled = GL_FALSE;
  467.       c->Scissor.X = 0;
  468.       c->Scissor.Y = 0;
  469.       c->Scissor.Width = 0;
  470.       c->Scissor.Height = 0;
  471.  
  472.       c->Stencil.Enabled = GL_FALSE;
  473.       c->Stencil.Function = GL_ALWAYS;
  474.       c->Stencil.Ref = 0;
  475.       c->Stencil.ValueMask = 0xffffffff;
  476.       c->Stencil.FailFunc = GL_KEEP;
  477.       c->Stencil.ZPassFunc = GL_KEEP;
  478.       c->Stencil.ZFailFunc = GL_KEEP;
  479.       c->Stencil.Clear = 0;
  480.       c->Stencil.WriteMask = 0xffffffff;
  481.  
  482.       c->Texture.Enabled = 0;
  483.       c->Texture.EnvMode = GL_MODULATE;
  484.       ASSIGN_4V( c->Texture.EnvColor, 0.0, 0.0, 0.0, 0.0 );
  485.       ASSIGN_4V( c->Texture.BorderColor, 0.0, 0.0, 0.0, 0.0 );
  486.       c->Texture.TexGenEnabled = 0;
  487.       c->Texture.GenModeS = GL_EYE_LINEAR;
  488.       c->Texture.GenModeT = GL_EYE_LINEAR;
  489.       c->Texture.GenModeR = GL_EYE_LINEAR;
  490.       c->Texture.GenModeQ = GL_EYE_LINEAR;
  491.       ASSIGN_4V( c->Texture.ObjectPlaneS, 1.0, 0.0, 0.0, 0.0 );
  492.       ASSIGN_4V( c->Texture.ObjectPlaneT, 0.0, 1.0, 0.0, 0.0 );
  493.       ASSIGN_4V( c->Texture.ObjectPlaneR, 0.0, 0.0, 0.0, 0.0 );
  494.       ASSIGN_4V( c->Texture.ObjectPlaneQ, 0.0, 0.0, 0.0, 0.0 );
  495.       ASSIGN_4V( c->Texture.EyePlaneS, 1.0, 0.0, 0.0, 0.0 );
  496.       ASSIGN_4V( c->Texture.EyePlaneT, 0.0, 1.0, 0.0, 0.0 );
  497.       ASSIGN_4V( c->Texture.EyePlaneR, 0.0, 0.0, 0.0, 0.0 );
  498.       ASSIGN_4V( c->Texture.EyePlaneQ, 0.0, 0.0, 0.0, 0.0 );
  499.       c->Texture.WrapS1D = GL_REPEAT;
  500.       c->Texture.WrapT1D = GL_REPEAT;
  501.       c->Texture.WrapS2D = GL_REPEAT;
  502.       c->Texture.WrapT2D = GL_REPEAT;
  503.       c->Texture.MinFilter1D = GL_NEAREST_MIPMAP_LINEAR;
  504.       c->Texture.MagFilter1D = GL_LINEAR;
  505.       c->Texture.MinFilter2D = GL_NEAREST_MIPMAP_LINEAR;
  506.       c->Texture.MagFilter2D = GL_LINEAR;
  507.  
  508.       c->Transform.MatrixMode = GL_MODELVIEW;
  509.       c->Transform.Normalize = GL_TRUE;
  510.       for (i=0;i<MAX_CLIP_PLANES;i++) {
  511.      c->Transform.ClipEnabled[i] = GL_FALSE;
  512.          ASSIGN_4V( c->Transform.ClipEquation[i], 0.0, 0.0, 0.0, 0.0 );
  513.       }
  514.       c->Transform.AnyClip = GL_FALSE;
  515.  
  516.       c->Viewport.X = 0;
  517.       c->Viewport.Y = 0;
  518.       c->Viewport.Width = 0;
  519.       c->Viewport.Height = 0;   
  520.       c->Viewport.Near = 0.0;
  521.       c->Viewport.Far = 1.0;
  522.       c->Viewport.Sx = 0.0;  /* Sx, Tx, Sy, Ty are computed later */
  523.       c->Viewport.Tx = 0.0;
  524.       c->Viewport.Sy = 0.0;
  525.       c->Viewport.Ty = 0.0;
  526.       c->Viewport.Sz = 0.5;
  527.       c->Viewport.Tz = 0.5;
  528.  
  529.       c->PackAlignment = 4;
  530.       c->PackRowLength = 0;
  531.       c->PackSkipPixels = 0;
  532.       c->PackSkipRows = 0;
  533.       c->PackSwapBytes = GL_FALSE;
  534.       c->PackLSBFirst = GL_FALSE;
  535.       c->UnpackAlignment = 4;
  536.       c->UnpackRowLength = 0;
  537.       c->UnpackSkipPixels = 0;
  538.       c->UnpackSkipRows = 0;
  539.       c->UnpackSwapBytes = GL_FALSE;
  540.       c->UnpackLSBFirst = GL_FALSE;
  541.       c->RenderMode = GL_RENDER;
  542.  
  543.       c->FeedbackType = GL_2D;   /* TODO: verify */
  544.       c->FeedbackBuffer = NULL;
  545.       c->FeedbackBufferSize = 0;
  546.       c->FeedbackCount = 0;
  547.  
  548.       c->SelectBuffer = NULL;
  549.       c->SelectBufferSize = 0;
  550.       c->SelectBufferCount = 0;
  551.       c->SelectHits = 0;
  552.       c->NameStackDepth = 0;
  553.  
  554.       c->AttribStackDepth = 0;
  555.  
  556.       c->NewState = GL_TRUE;
  557.       c->Mode = GL_BITMAP;
  558.  
  559.       c->NeedNormals = GL_FALSE;
  560.       c->FastDrawPixels = GL_TRUE;
  561.  
  562.       c->ModelViewStackDepth = 0;
  563.       MEMCPY( c->ModelViewMatrix, identity, 16*sizeof(GLfloat) );
  564.       MEMCPY( c->ModelViewInv, identity, 16*sizeof(GLfloat) );
  565.       c->ModelViewInvValid = GL_TRUE;
  566.  
  567.       c->ProjectionStackDepth = 0;
  568.       MEMCPY( c->ProjectionMatrix, identity, 16*sizeof(GLfloat) );
  569.  
  570.       c->TextureStackDepth = 0;
  571.       MEMCPY( c->TextureMatrix, identity, 16*sizeof(GLfloat) );
  572.       c->IdentityTexMat = GL_TRUE;
  573.  
  574.       for (i=0;i<MAX_TEXTURE_LEVELS;i++) {
  575.      c->TextureComponents1D[i] = 0;
  576.      c->TextureWidth1D[i] = 0;
  577.      c->TextureBorder1D[i] = 0;
  578.      c->TextureImage1D[i] = NULL;
  579.      c->TextureImage1DDeleteFlag[i] = GL_FALSE;
  580.      c->TextureComponents2D[i] = 0;
  581.      c->TextureWidth2D[i] = 0;
  582.      c->TextureHeight2D[i] = 0;
  583.      c->TextureBorder2D[i] = 0;
  584.      c->TextureImage2D[i] = NULL;
  585.      c->TextureImage2DDeleteFlag[i] = GL_FALSE;
  586.       }
  587.  
  588.       c->VertexFunc = gl_execute_vertex;
  589.       c->PointsFunc = NULL;
  590.       c->LineFunc = NULL;
  591.       c->PolygonFunc = NULL;
  592.       c->AuxPolygonFunc = NULL;
  593.  
  594.       c->CallDepth = 0;
  595.       c->ExecuteFlag = GL_TRUE;
  596.       c->CompileFlag = GL_FALSE;
  597.  
  598.       c->BufferWidth = 0;
  599.       c->BufferHeight = 0;
  600.       c->DepthBuffer = NULL;
  601.       c->AccumBuffer = NULL;
  602.       c->StencilBuffer = NULL;
  603.  
  604.       c->ErrorValue = GL_NO_ERROR;
  605.    }
  606. }
  607.  
  608.  
  609.  
  610. /*
  611.  * Allocate and initialize a gl_context structure.
  612.  * Input:  rgb_flag - GL_TRUE or GL_FALSE to indicate if using RGB mode
  613.  *         redscale, greenscale, bluescale, alphascale - if in RGB mode
  614.  *               these values are used to scale floating point color values
  615.  *               in [0,1] to integers in [0,scale]
  616.  *         db_flag - GL_TRUE or GL_FALSE to indicate if using double buffering
  617.  *         sharelist - another context to share display lists with or NULL
  618.  */
  619. struct gl_context *gl_new_context( GLboolean rgb_flag,
  620.                                    GLfloat redscale,
  621.                                    GLfloat greenscale,
  622.                                    GLfloat bluescale,
  623.                                    GLfloat alphascale,
  624.                                    GLboolean db_flag,
  625.                                    struct gl_context *shareList )
  626. {
  627.    struct gl_context *c;
  628.  
  629.    /* do some implementation tests */
  630.    assert( sizeof(GLbyte) >= 1 );
  631.    assert( sizeof(GLshort) >= 2 );
  632.    assert( sizeof(GLint) >= 4 );
  633.    assert( sizeof(GLubyte) >= 1 );
  634.    assert( sizeof(GLushort) >= 2 );
  635.    assert( sizeof(GLuint) >= 4 );
  636.  
  637.    gl_init_lists();
  638.    gl_init_eval();
  639.    gl_init_vb();
  640.  
  641.    c = (struct gl_context *) malloc( sizeof(struct gl_context) );
  642.    if (c) {
  643.       gl_initialize_context( c );
  644.       c->RGBAflag = rgb_flag;
  645.       c->RedScale = redscale;
  646.       c->GreenScale = greenscale;
  647.       c->BlueScale = bluescale;
  648.       c->AlphaScale = alphascale;
  649.       if (db_flag) {
  650.          c->DBflag = GL_TRUE;
  651.          c->Color.DrawBuffer = GL_BACK;
  652.          c->Pixel.ReadBuffer = GL_BACK;
  653.       }
  654.       else {
  655.          c->DBflag = GL_FALSE;
  656.          c->Color.DrawBuffer = GL_FRONT;
  657.          c->Pixel.ReadBuffer = GL_FRONT;
  658.       }
  659.       init_gamma_tables( c );
  660.  
  661.       if (shareList) {
  662.      /* share the group of display lists of another context */
  663.      c->ListGroup = shareList->ListGroup;
  664.      c->ListGroup->RefCount++;
  665.       }
  666.       else {
  667.      /* allocate new group of display lists */
  668.      c->ListGroup = alloc_display_list_group();
  669.       }
  670.    }
  671.    return c;
  672. }
  673.  
  674.  
  675.  
  676. /*
  677.  * Destroy a gl_context structure.
  678.  */
  679. void gl_destroy_context( struct gl_context *c )
  680. {
  681.    if (c) {
  682.       if (c->DepthBuffer)
  683.      free(c->DepthBuffer);
  684.       if (c->AccumBuffer)
  685.      free(c->AccumBuffer);
  686.       c->ListGroup->RefCount--;
  687.       if (c->ListGroup->RefCount==0) {
  688.      /* free display list group */
  689.      free( c->ListGroup );
  690.       }
  691.       free( (void *) c );
  692.       if (c==CCptr) {
  693.          CCptr = NULL;
  694.       }
  695.    }
  696. }
  697.  
  698.  
  699.  
  700. /*
  701.  * Set the current context.
  702.  */
  703. void gl_set_context( struct gl_context *c )
  704. {
  705.    if (c) {
  706.       /* "write back" current context */
  707.       if (CCptr) {
  708.          MEMCPY( CCptr, &CC, sizeof(struct gl_context) );
  709.       }
  710.       /* "load" new context */
  711.       CCptr = c;
  712.       MEMCPY( &CC, c, sizeof(struct gl_context) );
  713.    }
  714.    gl_init_pb( GL_BITMAP );
  715.    CC.NewState = GL_TRUE;
  716. }
  717.  
  718.  
  719.  
  720. /*
  721.  * Copy attribute groups from one context to another.
  722.  * Input:  src - source context
  723.  *         dst - destination context
  724.  *         mask - bitwise OR of GL_*_BIT flags
  725.  */
  726. void gl_copy_context( struct gl_context *src, struct gl_context *dst,
  727.               GLuint mask )
  728. {
  729.    if (src==CCptr) {
  730.       src = &CC;
  731.    }
  732.    else if (dst==CCptr) {
  733.       dst = &CC;
  734.    }
  735.  
  736.    if (mask & GL_ACCUM_BUFFER_BIT) {
  737.       MEMCPY( &dst->Accum, &src->Accum, sizeof(struct gl_accum_attrib) );
  738.    }
  739.    if (mask & GL_COLOR_BUFFER_BIT) {
  740.       MEMCPY( &dst->Color, &src->Color, sizeof(struct gl_colorbuffer_attrib) );
  741.    }
  742.    if (mask & GL_CURRENT_BIT) {
  743.       MEMCPY( &dst->Current, &src->Current, sizeof(struct gl_current_attrib) );
  744.    }
  745.    if (mask & GL_DEPTH_BUFFER_BIT) {
  746.       MEMCPY( &dst->Depth, &src->Depth, sizeof(struct gl_depthbuffer_attrib) );
  747.    }
  748.    if (mask & GL_ENABLE_BIT) {
  749.       /* no op */
  750.    }
  751.    if (mask & GL_EVAL_BIT) {
  752.       MEMCPY( &dst->Eval, &src->Eval, sizeof(struct gl_eval_attrib) );
  753.    }
  754.    if (mask & GL_FOG_BIT) {
  755.       MEMCPY( &dst->Fog, &src->Fog, sizeof(struct gl_fog_attrib) );
  756.    }
  757.    if (mask & GL_HINT_BIT) {
  758.       MEMCPY( &dst->Hint, &src->Hint, sizeof(struct gl_hint_attrib) );
  759.    }
  760.    if (mask & GL_LIGHTING_BIT) {
  761.       MEMCPY( &dst->Light, &src->Light, sizeof(struct gl_light_attrib) );
  762.    }
  763.    if (mask & GL_LINE_BIT) {
  764.       MEMCPY( &dst->Line, &src->Line, sizeof(struct gl_line_attrib) );
  765.    }
  766.    if (mask & GL_LIST_BIT) {
  767.       MEMCPY( &dst->List, &src->List, sizeof(struct gl_list_attrib) );
  768.    }
  769.    if (mask & GL_PIXEL_MODE_BIT) {
  770.       MEMCPY( &dst->Pixel, &src->Pixel, sizeof(struct gl_pixel_attrib) );
  771.    }
  772.    if (mask & GL_POINT_BIT) {
  773.       MEMCPY( &dst->Point, &src->Point, sizeof(struct gl_point_attrib) );
  774.    }
  775.    if (mask & GL_POLYGON_BIT) {
  776.       MEMCPY( &dst->Polygon, &src->Polygon, sizeof(struct gl_polygon_attrib) );
  777.    }
  778.    if (mask & GL_POLYGON_STIPPLE_BIT) {
  779.       MEMCPY( &dst->PolygonStipple, &src->PolygonStipple, 32*sizeof(GLuint) );
  780.    }
  781.    if (mask & GL_SCISSOR_BIT) {
  782.       MEMCPY( &dst->Scissor, &src->Scissor, sizeof(struct gl_scissor_attrib) );
  783.    }
  784.    if (mask & GL_STENCIL_BUFFER_BIT) {
  785.       MEMCPY( &dst->Stencil, &src->Stencil, sizeof(struct gl_stencil_attrib) );
  786.    }
  787.    if (mask & GL_TEXTURE_BIT) {
  788.       MEMCPY( &dst->Texture, &src->Texture, sizeof(struct gl_texture_attrib) );
  789.    }
  790.    if (mask & GL_TRANSFORM_BIT) {
  791.       MEMCPY( &dst->Transform, &src->Transform, sizeof(struct gl_transform_attrib) );
  792.    }
  793.    if (mask & GL_VIEWPORT_BIT) {
  794.       MEMCPY( &dst->Viewport, &src->Viewport, sizeof(struct gl_viewport_attrib) );
  795.    }
  796. }
  797.  
  798.  
  799.  
  800. /*
  801.  * This is Mesa's error handler.  Normally, all that's done is the updating
  802.  * of the current error value.  If Mesa is compiled with -DDEBUG or if the
  803.  * environment variable "MESA_DEBUG" is defined then a real error message
  804.  * is printed to stderr.
  805.  * Input:  error - the error value
  806.  *         s - a diagnostic string
  807.  */
  808. void gl_error( GLenum error, char *s )
  809. {
  810.    GLboolean debug;
  811.  
  812. #ifdef DEBUG
  813.    debug = GL_TRUE;
  814. #else
  815.    if (getenv("MESA_DEBUG")) {
  816.       debug = GL_TRUE;
  817.    }
  818.    else {
  819.       debug = GL_FALSE;
  820.    }
  821. #endif
  822.  
  823.    if (debug) {
  824.       char errstr[1000];
  825.  
  826.       switch (error) {
  827.      case GL_NO_ERROR:
  828.         strcpy( errstr, "GL_NO_ERROR" );
  829.         break;
  830.      case GL_INVALID_VALUE:
  831.         strcpy( errstr, "GL_INVALID_VALUE" );
  832.         break;
  833.      case GL_INVALID_ENUM:
  834.         strcpy( errstr, "GL_INVALID_ENUM" );
  835.         break;
  836.      case GL_INVALID_OPERATION:
  837.         strcpy( errstr, "GL_INVALID_OPERATION" );
  838.         break;
  839.      case GL_STACK_OVERFLOW:
  840.         strcpy( errstr, "GL_STACK_OVERFLOW" );
  841.         break;
  842.      case GL_STACK_UNDERFLOW:
  843.         strcpy( errstr, "GL_STACK_UNDERFLOW" );
  844.         break;
  845.      case GL_OUT_OF_MEMORY:
  846.         strcpy( errstr, "GL_OUT_OF_MEMORY" );
  847.         break;
  848.      default:
  849.         strcpy( errstr, "unknown" );
  850.         break;
  851.       }
  852.       fprintf( stderr, "Mesa Error (%s): %s\n", errstr, s );
  853.    }
  854.  
  855.    if (CC.ErrorValue==GL_NO_ERROR) {
  856.       CC.ErrorValue = error;
  857.    }
  858. }
  859.  
  860.  
  861.  
  862. GLenum glGetError( void )
  863. {
  864.    GLenum e;
  865.  
  866.    if (!CCptr) {
  867.       /* No current context */
  868.       return GL_NO_ERROR;
  869.    }
  870.  
  871.    if (INSIDE_BEGIN_END) {
  872.       gl_error( GL_INVALID_OPERATION, "glGetError" );
  873.       return GL_INVALID_OPERATION;
  874.    }
  875.  
  876.    e = CC.ErrorValue;
  877.    CC.ErrorValue = GL_NO_ERROR;
  878.    return e;
  879. }
  880.  
  881.  
  882.  
  883. /*
  884.  * Since the device driver may or may not support pixel logic ops we
  885.  * have to make some extensive tests whenever glLogicOp, glBlendFunc,
  886.  * glBlendEquation, glEn/Disable( GL_LOGIC_OP ), glEn/Disable( GL_BLEND ),
  887.  * or glPopAttrib is called.
  888.  */
  889. static void update_pixel_logic( void )
  890. {
  891.    if (CC.RGBAflag) {
  892.       /* RGBA mode blending w/ Logic Op */
  893.       if (CC.Color.BlendEnabled && CC.Color.BlendEquation==GL_LOGIC_OP) {
  894.      if ((*DD.logicop)( CC.Color.LogicOp )) {
  895.         /* Device driver can do logic, don't have to do it in software */
  896.         CC.Color.SWLogicOpEnabled = GL_FALSE;
  897.      }
  898.      else {
  899.         /* Device driver can't do logic op so we do it in software */
  900.         CC.Color.SWLogicOpEnabled = GL_TRUE;
  901.      }
  902.       }
  903.       else {
  904.      /* no logic op */
  905.      (void) (*DD.logicop)( GL_COPY );
  906.      CC.Color.SWLogicOpEnabled = GL_FALSE;
  907.       }
  908.    }
  909.    else {
  910.       /* CI mode Logic Op */
  911.       if (CC.Color.LogicOpEnabled) {
  912.      if ((*DD.logicop)( CC.Color.LogicOp )) {
  913.         /* Device driver can do logic, don't have to do it in software */
  914.         CC.Color.SWLogicOpEnabled = GL_FALSE;
  915.      }
  916.      else {
  917.         /* Device driver can't do logic op so we do it in software */
  918.         CC.Color.SWLogicOpEnabled = GL_TRUE;
  919.      }
  920.       }
  921.       else {
  922.      /* no logic op */
  923.      (void) (*DD.logicop)( GL_COPY );
  924.      CC.Color.SWLogicOpEnabled = GL_FALSE;
  925.       }
  926.    }
  927. }
  928.  
  929.  
  930.  
  931. /*
  932.  * Check if software implemented RGBA or Color Index masking is needed.
  933.  */
  934. static void update_pixel_masking( void )
  935. {
  936.    if (CC.RGBAflag) {
  937.       if (CC.Color.ColorMask==0xf) {
  938.          /* disable masking */
  939.          (void) (*DD.color_mask)( GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE );
  940.          CC.Color.SWmasking = GL_FALSE;
  941.       }
  942.       else {
  943.          /* Ask DD to do color masking, if it can't we'll do it in software */
  944.          GLboolean red   = (CC.Color.ColorMask & 8) ? GL_TRUE : GL_FALSE;
  945.          GLboolean green = (CC.Color.ColorMask & 4) ? GL_TRUE : GL_FALSE;
  946.          GLboolean blue  = (CC.Color.ColorMask & 2) ? GL_TRUE : GL_FALSE;
  947.          GLboolean alpha = (CC.Color.ColorMask & 1) ? GL_TRUE : GL_FALSE;
  948.          if ((*DD.color_mask)( red, green, blue, alpha )) {
  949.             CC.Color.SWmasking = GL_FALSE;
  950.          }
  951.          else {
  952.             CC.Color.SWmasking = GL_TRUE;
  953.          }
  954.       }
  955.    }
  956.    else {
  957.       if (CC.Color.IndexMask==0xffffffff) {
  958.          /* disable masking */
  959.          (void) (*DD.index_mask)( 0xffffffff );
  960.          CC.Color.SWmasking = GL_FALSE;
  961.       }
  962.       else {
  963.          /* Ask DD to do index masking, if it can't we'll do it in software */
  964.          if ((*DD.index_mask)( CC.Color.IndexMask )) {
  965.             CC.Color.SWmasking = GL_FALSE;
  966.          }
  967.          else {
  968.             CC.Color.SWmasking = GL_TRUE;
  969.          }
  970.       }
  971.    }
  972. }
  973.  
  974.  
  975.  
  976. /*
  977.  * Recompute the value of CC.RasterMask, CC.ClipMask, etc. according to
  978.  * the current context.
  979.  */
  980. static void update_rasterflags( void )
  981. {
  982.    CC.RasterMask = 0;
  983.  
  984.    if (CC.Color.AlphaEnabled)        CC.RasterMask |= ALPHA_BIT;
  985.    if (CC.Color.BlendEnabled)        CC.RasterMask |= BLEND_BIT;
  986.    if (CC.Depth.Test)            CC.RasterMask |= DEPTH_BIT;
  987.    if (CC.Fog.Enabled)            CC.RasterMask |= FOG_BIT;
  988.    if (CC.Color.SWLogicOpEnabled)    CC.RasterMask |= LOGIC_OP_BIT;
  989.    if (CC.Scissor.Enabled)        CC.RasterMask |= SCISSOR_BIT;
  990.    if (CC.Stencil.Enabled)        CC.RasterMask |= STENCIL_BIT;
  991.    if (CC.Color.SWmasking)        CC.RasterMask |= MASKING_BIT;
  992.    if (CC.RGBAflag &&
  993.        (CC.RedGamma!=1.0 ||
  994.         CC.GreenGamma!=1.0 ||
  995.         CC.BlueGamma!=1.0))        CC.RasterMask |= GAMMA_BIT;
  996.  
  997.    /* Recompute ClipMask (what has to be interpolated when clipping) */
  998.    CC.ClipMask = 0;
  999.    if (CC.Texture.Enabled) {
  1000.       CC.ClipMask |= CLIP_TEXTURE_BIT;
  1001.    }
  1002.    if (CC.Light.ShadeModel==GL_SMOOTH) {
  1003.       if (CC.RGBAflag) {
  1004.      CC.ClipMask |= CLIP_FCOLOR_BIT;
  1005.      if (CC.Light.Model.TwoSide) {
  1006.         CC.ClipMask |= CLIP_BCOLOR_BIT;
  1007.      }
  1008.       }
  1009.       else {
  1010.      CC.ClipMask |= CLIP_FINDEX_BIT;
  1011.      if (CC.Light.Model.TwoSide) {
  1012.         CC.ClipMask |= CLIP_BINDEX_BIT;
  1013.      }
  1014.       }
  1015.    }
  1016.  
  1017.  
  1018.    /* Check if the equation of the plane for polygons has to be computed. */
  1019.    CC.ComputePlane = CC.Depth.Test || CC.Polygon.CullFlag
  1020.                   || CC.Light.Model.TwoSide || CC.Texture.Enabled
  1021.                   || CC.Polygon.OffsetEnabled;
  1022. }
  1023.  
  1024.  
  1025.  
  1026. /*
  1027.  * If CC.NewState==GL_TRUE then this function MUST be called before
  1028.  * rendering any primitive.  Basically, function pointers and miscellaneous
  1029.  * flags are updated to reflect the current state of the state machine.
  1030.  */
  1031. void gl_update_state( void )
  1032. {
  1033.    update_pixel_logic();
  1034.    update_pixel_masking();
  1035.    update_rasterflags();
  1036.  
  1037.    /* Check if normal vectors are needed */
  1038.    if (CC.Light.Enabled
  1039.        || (CC.Texture.GenModeS==GL_SPHERE_MAP
  1040.            && (CC.Texture.TexGenEnabled & S_BIT))
  1041.        || (CC.Texture.GenModeT==GL_SPHERE_MAP
  1042.            && (CC.Texture.TexGenEnabled & T_BIT))) {
  1043.       CC.NeedNormals = GL_TRUE;
  1044.    }
  1045.    else {
  1046.       CC.NeedNormals = GL_FALSE;
  1047.    }
  1048.  
  1049.    /* Check if incoming colors can be modified by rasterization */
  1050.    if ( CC.Color.BlendEnabled || (CC.RasterMask & GAMMA_BIT)
  1051.        || CC.Color.SWmasking) {
  1052.       CC.MutableColors = GL_TRUE;
  1053.    }
  1054.    else {
  1055.       CC.MutableColors = GL_FALSE;
  1056.    }
  1057.  
  1058.    gl_set_point_function();
  1059.    gl_set_line_function();
  1060.    gl_set_polygon_function();
  1061.  
  1062.    CC.NewState = GL_FALSE;
  1063. }
  1064.  
  1065.