home *** CD-ROM | disk | FTP | other *** search
/ Amiga MA Magazine 1998 #6 / amigamamagazinepolishissue1998.iso / coders / mesa-1.2.8 / nt / context.c next >
C/C++ Source or Header  |  1996-05-27  |  50KB  |  1,610 lines

  1. /* $Id: context.c,v 1.49 1996/02/26 15:14:32 brianp Exp $ */
  2.  
  3. /*
  4.  * Mesa 3-D graphics library
  5.  * Version:  1.2
  6.  * Copyright (C) 1995-1996  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. $Log: context.c,v $
  26.  * Revision 1.49  1996/02/26  15:14:32  brianp
  27.  * removed CC.Current.Color, replaced by CC.Current.IntColor
  28.  *
  29.  * Revision 1.48  1996/02/26  15:13:24  brianp
  30.  * added lookup tables and code for optimized lighting
  31.  *
  32.  * Revision 1.47  1996/02/19  21:49:26  brianp
  33.  * added support for software alpha buffering
  34.  *
  35.  * Revision 1.46  1996/02/13  17:46:49  brianp
  36.  * call gl_update_lighting() in gl_udpate_state()
  37.  *
  38.  * Revision 1.45  1996/02/06  04:13:02  brianp
  39.  * added CC.Polygon.CullBits code
  40.  *
  41.  * Revision 1.44  1996/02/06  03:24:36  brianp
  42.  * removed gamma correction code
  43.  *
  44.  * Revision 1.43  1996/01/29  19:04:54  brianp
  45.  * test for CC.Polygon.Unfilled for CC.ComputePlane
  46.  *
  47.  * Revision 1.42  1996/01/23  16:54:29  brianp
  48.  * CC.FastDrawPixels wasn't initialized correctly, added more comments
  49.  *
  50.  * Revision 1.41  1996/01/22  15:36:14  brianp
  51.  * new logic in gl_update_state() for CC.MutablePixels and CC.MonoPixels
  52.  *
  53.  * Revision 1.40  1996/01/12  22:31:10  brianp
  54.  * changed default stencil masks to 0xff (1 byte)
  55.  *
  56.  * Revision 1.39  1996/01/11  15:46:56  brianp
  57.  * free stencil buffer, if any, in gl_destroy_context()
  58.  *
  59.  * Revision 1.38  1996/01/09  19:53:04  brianp
  60.  * fixed a memory leak from failing to incr display list reference count
  61.  *
  62.  * Revision 1.37  1996/01/07  22:49:41  brianp
  63.  * call gl_init_lighting() when creating a context
  64.  *
  65.  * Revision 1.36  1996/01/05  01:21:20  brianp
  66.  * added profiling
  67.  *
  68.  * Revision 1.35  1995/12/30  17:14:23  brianp
  69.  * initialize CC.Current.IntColor
  70.  *
  71.  * Revision 1.34  1995/12/30  00:48:26  brianp
  72.  * check for EightBitColor
  73.  *
  74.  * Revision 1.33  1995/12/12  21:43:18  brianp
  75.  * default state of normalization changed to disabled
  76.  *
  77.  * Revision 1.32  1995/11/22  13:35:00  brianp
  78.  * added MutableColors flag, test for RGBA mode before setting GAMMA_BIT
  79.  *
  80.  * Revision 1.31  1995/11/08  22:09:05  brianp
  81.  * changed GL<type> assertions from == to >=
  82.  *
  83.  * Revision 1.30  1995/11/03  17:41:05  brianp
  84.  * removed unused variables
  85.  *
  86.  * Revision 1.29  1995/11/01  21:44:15  brianp
  87.  * added CC.Light.LastEnabled
  88.  *
  89.  * Revision 1.28  1995/10/27  20:28:27  brianp
  90.  * added glPolygonOffsetEXT() support
  91.  *
  92.  * Revision 1.27  1995/10/19  15:45:20  brianp
  93.  * added gamma support
  94.  * new arguments to gl_new_context()
  95.  *
  96.  * Revision 1.26  1995/10/14  16:26:13  brianp
  97.  * enable dithering by default
  98.  * added SWmasking code
  99.  *
  100.  * Revision 1.25  1995/09/25  16:31:54  brianp
  101.  * reorganized front and back material indexing
  102.  *
  103.  * Revision 1.24  1995/09/15  18:47:32  brianp
  104.  * introduced CC.NewState convention
  105.  * use bitmask flag for CC.Texture.TexGenEnabled
  106.  *
  107.  * Revision 1.23  1995/07/25  16:41:54  brianp
  108.  * made changes for using CC.VertexFunc pointer
  109.  *
  110.  * Revision 1.22  1995/07/24  20:34:16  brianp
  111.  * replaced memset() with MEMSET() and memcpy() with MEMCPY()
  112.  *
  113.  * Revision 1.21  1995/06/19  14:52:37  brianp
  114.  * initialize current texture coordinate, per Asif Khan
  115.  *
  116.  * Revision 1.20  1995/06/05  20:26:24  brianp
  117.  * added Unfilled field to gl_polygon struct
  118.  *
  119.  * Revision 1.19  1995/05/22  21:02:41  brianp
  120.  * Release 1.2
  121.  *
  122.  * Revision 1.18  1995/05/17  13:52:37  brianp
  123.  * implemented glIndexMask(0) and glColorMask(0,0,0,0)
  124.  *
  125.  * Revision 1.17  1995/05/17  13:17:22  brianp
  126.  * changed default CC.Mode value to allow use of real OpenGL headers
  127.  * removed need for CC.MajorMode variable
  128.  *
  129.  * Revision 1.16  1995/05/15  16:07:01  brianp
  130.  * implemented shared/nonshared display lists
  131.  *
  132.  * Revision 1.15  1995/05/12  16:30:14  brianp
  133.  * Texture images stored as bytes, not floats
  134.  *
  135.  * Revision 1.14  1995/04/17  13:51:19  brianp
  136.  * added gl_copy_context() function
  137.  *
  138.  * Revision 1.13  1995/03/27  20:31:26  brianp
  139.  * new Texture.Enabled scheme
  140.  *
  141.  * Revision 1.12  1995/03/24  16:59:56  brianp
  142.  * added gl_update_pixel_logic
  143.  *
  144.  * Revision 1.11  1995/03/24  16:11:41  brianp
  145.  * fixed logicop bug in gl_update_rasterflags
  146.  *
  147.  * Revision 1.10  1995/03/16  20:36:38  brianp
  148.  * added call to gl_update_rasterflags in gl_set_context
  149.  *
  150.  * Revision 1.9  1995/03/10  16:26:43  brianp
  151.  * updated for bleding extensions
  152.  *
  153.  * Revision 1.8  1995/03/09  21:40:14  brianp
  154.  * added ModelViewInvValid initializer
  155.  * added ComputePlane test to gl_update_rasterflags
  156.  *
  157.  * Revision 1.7  1995/03/09  19:07:16  brianp
  158.  * added MESA_DEBUG env var support
  159.  *
  160.  * Revision 1.6  1995/03/08  15:10:02  brianp
  161.  * added support for dd_logicop
  162.  *
  163.  * Revision 1.5  1995/03/04  19:29:44  brianp
  164.  * 1.1 beta revision
  165.  *
  166.  * Revision 1.4  1995/03/02  19:17:54  brianp
  167.  * new RasterMask logic, fixed some comments
  168.  *
  169.  * Revision 1.3  1995/02/27  22:48:28  brianp
  170.  * modified for PB
  171.  *
  172.  * Revision 1.2  1995/02/24  15:19:23  brianp
  173.  * *** empty log message ***
  174.  *
  175.  * Revision 1.1  1995/02/24  14:18:45  brianp
  176.  * Initial revision
  177.  *
  178.  */
  179.  
  180.  
  181. /*
  182.  * The gl_context structure holds the current state of the library.
  183.  * Typically, there will be a gl_context structure associated with each
  184.  * window into which we're rendering:
  185.  *    When we open a new rendering window we need a new gl_context.
  186.  *    When we close a rendering window we destroy its gl_context.
  187.  *    When we switch rendering to a different window we change gl_context.
  188.  *
  189.  * Throughout this implementation, references are made to CC which is
  190.  * the Current Context.
  191.  */
  192.  
  193.  
  194. #pragma warning( disable : 4244 )
  195.  
  196. #include <assert.h>
  197. #include <math.h>
  198. #include <stdio.h>
  199. #include <stdlib.h>
  200. #include <string.h>
  201. #include "context.h"
  202. #include "dd.h"
  203. #include "draw.h"
  204. #include "eval.h"
  205. #include "light.h"
  206. #include "lines.h"
  207. #include "list.h"
  208. #include "macros.h"
  209. #include "pb.h"
  210. #include "points.h"
  211. #include "polygons.h"
  212. /*#include "triangles.h"*/
  213. #include "vb.h"
  214.  
  215.  
  216.  
  217. /* Copy of Current Context (PUBLIC) */
  218. struct gl_context CC;
  219.  
  220. /* Pointer to Current Context (PRIVATE) */
  221. static struct gl_context *CCptr = NULL;
  222.  
  223.  
  224.  
  225.  
  226. /**********************************************************************/
  227. /*****                   Profiling functions                      *****/
  228. /**********************************************************************/
  229.  
  230. #ifdef PROFILE
  231.  
  232. #ifndef WIN32
  233. #include <sys/times.h>
  234. #include <sys/param.h>
  235. #else
  236. #include <windows.h>
  237. #include <mesagl/mesagl32.h>
  238. #include "../../src/profile.h"
  239. #include "../../src/wmesadef.h"
  240.  
  241. extern WMesaContext WC;
  242. #endif
  243.  
  244.  
  245. /*
  246.  * Return system time in seconds.
  247.  * NOTE:  this implementation may not be very portable!
  248.  */
  249. GLdouble gl_time( void )
  250. {
  251. #ifndef WIN32
  252.    static GLdouble prev_time = 0.0;
  253.    static GLdouble time;
  254.    struct tms tm;
  255.    clock_t clk;
  256.  
  257.    clk = times(&tm);
  258.  
  259. #ifdef CLK_TCK
  260.    time = (double)clk / (double)CLK_TCK;
  261. #else
  262.    time = (double)clk / (double)HZ;
  263. #endif
  264.  
  265. #else
  266.    static DWORD    prev_time = 0;
  267.    static DWORD    time;
  268.    time = GetTickCount();
  269. #endif /* WIN32 */
  270.  
  271.    if (time>prev_time) {
  272.       prev_time = time;
  273.       return (GLdouble)time;
  274.    }
  275.    else {
  276.       return (GLdouble)prev_time;
  277.    }
  278. }
  279.  
  280.  
  281.  
  282. /*
  283.  * Reset the timing/profiling counters
  284.  */
  285. static void init_timings( struct gl_context *c )
  286. {
  287.    c->BeginEndCount = 0;
  288.    c->BeginEndTime = 0.0;
  289.    c->VertexCount = 0;
  290.    c->VertexTime = 0.0;
  291.    c->PointCount = 0;
  292.    c->PointTime = 0.0;
  293.    c->LineCount = 0;
  294.    c->LineTime = 0.0;
  295.    c->PolygonCount = 0;
  296.    c->PolygonTime = 0.0;
  297.    c->ClearCount = 0;
  298.    c->ClearTime = 0.0;
  299.    c->SwapCount = 0;
  300.    c->SwapTime = 0.0;
  301. }
  302.  
  303.  
  304.  
  305. /*
  306.  * Print the accumulated timing/profiling data.
  307.  */
  308. static void print_timings( struct gl_context *c )
  309. {
  310.    GLdouble beginendrate;
  311.    GLdouble vertexrate;
  312.    GLdouble pointrate;
  313.    GLdouble linerate;
  314.    GLdouble polygonrate;
  315.    GLdouble overhead;
  316.    GLdouble clearrate;
  317.    GLdouble swaprate;
  318. #ifdef WIN32
  319.     GLdouble buffer_sizeRate = 0.0;
  320.     GLdouble choose_line_functionRate = 0.0;
  321.     GLdouble choose_points_functionRate = 0.0;
  322.     GLdouble choose_polygon_functionRate = 0.0;
  323.     GLdouble clearRate = 0.0;
  324.     GLdouble clear_colorRate = 0.0;
  325.     GLdouble clear_indexRate = 0.0;
  326.     GLdouble fast_flat_rgb_lineRate = 0.0;
  327.     GLdouble fast_flat_rgb_polygonRate = 0.0;
  328.     GLdouble fast_rgb_pointsRate = 0.0;
  329.     GLdouble flushRate = 0.0;
  330.     GLdouble GetPaletteRate = 0.0;
  331.     GLdouble read_color_pixelsRate = 0.0;
  332.     GLdouble read_color_spanRate = 0.0;
  333.     GLdouble read_index_pixelsRate = 0.0;
  334.     GLdouble read_index_spanRate = 0.0;
  335.     GLdouble set_bufferRate = 0.0;
  336.     GLdouble set_colorRate = 0.0;
  337.     GLdouble set_indexRate = 0.0;
  338.     GLdouble write_color_pixelsRate = 0.0;
  339.     GLdouble write_color_spanRate = 0.0;
  340.     GLdouble write_index_pixelsRate = 0.0;
  341.     GLdouble write_index_spanRate = 0.0;
  342.     GLdouble write_monocolor_pixelsRate = 0.0;
  343.     GLdouble write_monocolor_spanRate = 0.0;
  344.     GLdouble write_monoindex_pixelsRate = 0.0;
  345.     GLdouble write_monoindex_spanRate = 0.0;
  346.    char        szDebug[4096];
  347.    char        szTmp[256];
  348.  
  349.    if ( WC->profile.buffer_sizeTime > 0.0 ) {
  350.        buffer_sizeRate = WC->profile.buffer_sizeCount / WC->profile.buffer_sizeTime;
  351.    }
  352.    if ( WC->profile.choose_line_functionTime > 0.0 ) {
  353.        choose_line_functionRate = WC->profile.choose_line_functionCount / WC->profile.choose_line_functionTime;
  354.    }
  355.    if ( WC->profile.choose_points_functionTime > 0.0 ) {
  356.        choose_points_functionRate = WC->profile.choose_points_functionCount / WC->profile.choose_points_functionTime;
  357.    }
  358.    if ( WC->profile.choose_polygon_functionTime > 0.0 ) {
  359.        choose_polygon_functionRate = WC->profile.choose_polygon_functionCount / WC->profile.choose_polygon_functionTime;
  360.    }
  361.    if ( WC->profile.clearTime > 0.0 ) {
  362.        clearRate = WC->profile.clearCount / WC->profile.clearTime;
  363.    }
  364.    if ( WC->profile.clear_colorTime > 0.0 ) {
  365.        clear_colorRate = WC->profile.clear_colorCount / WC->profile.clear_colorTime;
  366.    }
  367.    if ( WC->profile.clear_indexTime > 0.0 ) {
  368.        clear_indexRate = WC->profile.clear_indexCount / WC->profile.clear_indexTime;
  369.    }
  370.    if ( WC->profile.fast_flat_rgb_lineTime > 0.0 ) {
  371.        fast_flat_rgb_lineRate = WC->profile.fast_flat_rgb_lineCount / WC->profile.fast_flat_rgb_lineTime;
  372.    }
  373.    if ( WC->profile.fast_flat_rgb_polygonTime > 0.0 ) {
  374.        fast_flat_rgb_polygonRate = WC->profile.fast_flat_rgb_polygonCount / WC->profile.fast_flat_rgb_polygonTime;
  375.    }
  376.    if ( WC->profile.fast_rgb_pointsTime > 0.0 ) {
  377.        fast_rgb_pointsRate = WC->profile.fast_rgb_pointsCount / WC->profile.fast_rgb_pointsTime;
  378.    }
  379.    if ( WC->profile.flushTime > 0.0 ) {
  380.        flushRate = WC->profile.flushCount / WC->profile.flushTime;
  381.    }
  382.    if ( WC->profile.GetPaletteTime > 0.0 ) {
  383.        GetPaletteRate = WC->profile.GetPaletteCount / WC->profile.GetPaletteTime;
  384.    }
  385.    if ( WC->profile.read_color_pixelsTime > 0.0 ) {
  386.        read_color_pixelsRate = WC->profile.read_color_pixelsCount / WC->profile.read_color_pixelsTime;
  387.    }
  388.    if ( WC->profile.read_color_spanTime > 0.0 ) {
  389.        read_color_spanRate = WC->profile.read_color_spanCount / WC->profile.read_color_spanTime;
  390.    }
  391.    if ( WC->profile.read_index_pixelsTime > 0.0 ) {
  392.        read_index_pixelsRate = WC->profile.read_index_pixelsCount / WC->profile.read_index_pixelsTime;
  393.    }
  394.    if ( WC->profile.read_index_spanTime > 0.0 ) {
  395.        read_index_spanRate = WC->profile.read_index_spanCount / WC->profile.read_index_spanTime;
  396.    }
  397.    if ( WC->profile.set_bufferTime > 0.0 ) {
  398.        set_bufferRate = WC->profile.set_bufferCount / WC->profile.set_bufferTime;
  399.    }
  400.    if ( WC->profile.set_colorTime > 0.0 ) {
  401.        set_colorRate = WC->profile.set_colorCount / WC->profile.set_colorTime;
  402.    }
  403.    if ( WC->profile.set_indexTime > 0.0 ) {
  404.        set_indexRate = WC->profile.set_indexCount / WC->profile.set_indexTime;
  405.    }
  406.    if ( WC->profile.write_color_pixelsTime > 0.0 ) {
  407.        write_color_pixelsRate = WC->profile.write_color_pixelsCount / WC->profile.write_color_pixelsTime;
  408.    }
  409.    if ( WC->profile.write_color_spanTime > 0.0 ) {
  410.        write_color_spanRate = WC->profile.write_color_spanCount / WC->profile.write_color_spanTime;
  411.    }
  412.    if ( WC->profile.write_index_pixelsTime > 0.0 ) {
  413.        write_index_pixelsRate = WC->profile.write_index_pixelsCount / WC->profile.write_index_pixelsTime;
  414.    }
  415.    if ( WC->profile.write_index_spanTime > 0.0 ) {
  416.        write_index_spanRate = WC->profile.write_index_spanCount / WC->profile.write_index_spanTime;
  417.    }
  418.    if ( WC->profile.write_monocolor_pixelsTime > 0.0 ) {
  419.        write_monocolor_pixelsRate = WC->profile.write_monocolor_pixelsCount / WC->profile.write_monocolor_pixelsTime;
  420.    }
  421.    if ( WC->profile.write_monocolor_spanTime > 0.0 ) {
  422.        write_monocolor_spanRate = WC->profile.write_monocolor_spanCount / WC->profile.write_monocolor_spanTime;
  423.    }
  424.    if ( WC->profile.write_monoindex_pixelsTime > 0.0 ) {
  425.        write_monoindex_pixelsRate = WC->profile.write_monoindex_pixelsCount / WC->profile.write_monoindex_pixelsTime;
  426.    }
  427.    if ( WC->profile.write_monoindex_spanTime > 0.0 ) {
  428.        write_monoindex_spanRate = WC->profile.write_monoindex_spanCount / WC->profile.write_monoindex_spanTime;
  429.    }
  430.  
  431. #endif
  432.  
  433.    if (c->BeginEndTime>0.0) {
  434.       beginendrate = c->BeginEndCount / c->BeginEndTime;
  435.    }
  436.    else {
  437.       beginendrate = 0.0;
  438.    }
  439.    if (c->VertexTime>0.0) {
  440.       vertexrate = c->VertexCount / c->VertexTime;
  441.    }
  442.    else {
  443.       vertexrate = 0.0;
  444.    }
  445.    if (c->PointTime>0.0) {
  446.       pointrate = c->PointCount / c->PointTime;
  447.    }
  448.    else {
  449.       pointrate = 0.0;
  450.    }
  451.    if (c->LineTime>0.0) {
  452.       linerate = c->LineCount / c->LineTime;
  453.    }
  454.    else {
  455.       linerate = 0.0;
  456.    }
  457.    if (c->PolygonTime>0.0) {
  458.       polygonrate = c->PolygonCount / c->PolygonTime;
  459.    }
  460.    else {
  461.       polygonrate = 0.0;
  462.    }
  463.    if (c->ClearTime>0.0) {
  464.       clearrate = c->ClearCount / c->ClearTime;
  465.    }
  466.    else {
  467.       clearrate = 0.0;
  468.    }
  469.    if (c->SwapTime>0.0) {
  470.       swaprate = c->SwapCount / c->SwapTime;
  471.    }
  472.    else {
  473.       swaprate = 0.0;
  474.    }
  475.  
  476.  
  477.    overhead = c->BeginEndTime - c->VertexTime - c->PointTime
  478.               - c->LineTime - c->PolygonTime;
  479.  
  480. #ifndef WIN32
  481.    printf("                          Count   Time (s)    Rate (/s) \n");
  482.    printf("--------------------------------------------------------\n");
  483.    printf("glBegin/glEnd           %7d  %8.3f   %10.3f\n",
  484.           c->BeginEndCount, c->BeginEndTime, beginendrate);
  485.    printf("  vertexes transformed  %7d  %8.3f   %10.3f\n",
  486.           c->VertexCount, c->VertexTime, vertexrate );
  487.    printf("  points rasterized     %7d  %8.3f   %10.3f\n",
  488.           c->PointCount, c->PointTime, pointrate );
  489.    printf("  lines rasterized      %7d  %8.3f   %10.3f\n",
  490.           c->LineCount, c->LineTime, linerate );
  491.    printf("  polygons rasterized   %7d  %8.3f   %10.3f\n",
  492.           c->PolygonCount, c->PolygonTime, polygonrate );
  493.    printf("  overhead                       %8.3f\n", overhead );
  494.    printf( "glClear                 %7d  %8.3f   %10.3f\n",
  495.           c->ClearCount, c->ClearTime, clearrate );
  496.    printf( "SwapBuffers             %7d  %8.3f   %10.3f\n",
  497.           c->SwapCount, c->SwapTime, swaprate );
  498. #else
  499.    sprintf(szTmp, "                          Count   Time (ms)    Rate (/ms) \n");
  500.    strcpy(szDebug, szTmp);
  501.    sprintf(szTmp, "--------------------------------------------------------\n");
  502.    strcat(szDebug, szTmp);
  503.    sprintf(szTmp, "%-23s %7d  %8.6g   %10.6g\n",
  504.           "glBegin/glEnd", c->BeginEndCount, c->BeginEndTime, beginendrate);
  505.    strcat(szDebug, szTmp);
  506.    sprintf(szTmp, "%-23s %7d  %8.6g   %10.6g\n",
  507.           "  vertexes transformed", c->VertexCount, c->VertexTime, vertexrate );
  508.    strcat(szDebug, szTmp);
  509.    sprintf(szTmp, "%-23s %7d  %8.6g   %10.6g\n",
  510.           "  points rasterized", c->PointCount, c->PointTime, pointrate );
  511.    strcat(szDebug, szTmp);
  512.    sprintf(szTmp, "%-23s %7d  %8.6g   %10.6g\n",
  513.           "  lines rasterized", c->LineCount, c->LineTime, linerate );
  514.    strcat(szDebug, szTmp);
  515.    sprintf(szTmp, "%-23s %7d  %8.6g   %10.6g\n",
  516.           "  polygons rasterized", c->PolygonCount, c->PolygonTime, polygonrate );
  517.    strcat(szDebug, szTmp);
  518.    sprintf(szTmp, "  overhead                       %8.6g\n", overhead );
  519.    strcat(szDebug, szTmp);
  520.    sprintf(szTmp, "%-23s %7d  %8.6g   %10.6g\n",
  521.           "glClear", c->ClearCount, c->ClearTime, clearrate );
  522.    strcat(szDebug, szTmp);
  523.    sprintf(szTmp, "%-23s %7d  %8.6g   %10.6g\n",
  524.           "SwapBuffers", c->SwapCount, c->SwapTime, swaprate );
  525.    strcat(szDebug, szTmp);
  526.    OutputDebugString(szDebug);
  527.  
  528.    sprintf(szTmp, "%-23s %7d  %8.6g   %10.6g\n",
  529.        "buffer_size", WC->profile.buffer_sizeCount, 
  530.        WC->profile.buffer_sizeTime, buffer_sizeRate);
  531.    strcpy(szDebug, szTmp);
  532.    sprintf(szTmp, "%-23s %7d  %8.6g   %10.6g\n",
  533.        "choose_line_function", WC->profile.choose_line_functionCount, 
  534.        WC->profile.choose_line_functionTime, choose_line_functionRate);
  535.    strcat(szDebug, szTmp);
  536.    sprintf(szTmp, "%-23s %7d  %8.6g   %10.6g\n",
  537.        "choose_points_function", WC->profile.choose_points_functionCount, 
  538.        WC->profile.choose_points_functionTime, choose_points_functionRate);
  539.    strcat(szDebug, szTmp);
  540.    sprintf(szTmp, "%-23s %7d  %8.6g   %10.6g\n",
  541.        "choose_polygon_function", WC->profile.choose_polygon_functionCount, 
  542.        WC->profile.choose_polygon_functionTime, choose_polygon_functionRate);
  543.    strcat(szDebug, szTmp);
  544.    sprintf(szTmp, "%-23s %7d  %8.6g   %10.6g\n",
  545.        "clear", WC->profile.clearCount, 
  546.        WC->profile.clearTime, clearRate);
  547.    strcat(szDebug, szTmp);
  548.    sprintf(szTmp, "%-23s %7d  %8.6g   %10.6g\n",
  549.        "clear_color", WC->profile.clear_colorCount, 
  550.        WC->profile.clear_colorTime, clear_colorRate);
  551.    strcat(szDebug, szTmp);
  552.    sprintf(szTmp, "%-23s %7d  %8.6g   %10.6g\n",
  553.        "clear_index", WC->profile.clear_indexCount, 
  554.        WC->profile.clear_indexTime, clear_indexRate);
  555.    strcat(szDebug, szTmp);
  556.    OutputDebugString(szDebug);
  557.  
  558.    sprintf(szTmp, "%-23s %7d  %8.6g   %10.6g\n",
  559.        "fast_flat_rgb_line", WC->profile.fast_flat_rgb_lineCount, 
  560.        WC->profile.fast_flat_rgb_lineTime, fast_flat_rgb_lineRate);
  561.    strcpy(szDebug, szTmp);
  562.    sprintf(szTmp, "%-23s %7d  %8.6g   %10.6g\n",
  563.        "fast_flat_rgb_polygon", WC->profile.fast_flat_rgb_polygonCount, 
  564.        WC->profile.fast_flat_rgb_polygonTime, fast_flat_rgb_polygonRate);
  565.    strcat(szDebug, szTmp);
  566.    sprintf(szTmp, "%-23s %7d  %8.6g   %10.6g\n",
  567.        "fast_rgb_points", WC->profile.fast_rgb_pointsCount, 
  568.        WC->profile.fast_rgb_pointsTime, fast_rgb_pointsRate);
  569.    strcat(szDebug, szTmp);
  570.    sprintf(szTmp, "%-23s %7d  %8.6g   %10.6g\n",
  571.        "flush", WC->profile.flushCount, 
  572.        WC->profile.flushTime, flushRate);
  573.    strcat(szDebug, szTmp);
  574.    sprintf(szTmp, "%-23s %7d  %8.6g   %10.6g\n",
  575.        "GetPalette", WC->profile.GetPaletteCount, 
  576.        WC->profile.GetPaletteTime, GetPaletteRate);
  577.    strcat(szDebug, szTmp);
  578.    sprintf(szTmp, "%-23s %7d  %8.6g   %10.6g\n",
  579.        "read_color_pixels", WC->profile.read_color_pixelsCount, 
  580.        WC->profile.read_color_pixelsTime, read_color_pixelsRate);
  581.    strcat(szDebug, szTmp);
  582.    OutputDebugString(szDebug);
  583.  
  584.    sprintf(szTmp, "%-23s %7d  %8.6g   %10.6g\n",
  585.        "read_color_span", WC->profile.read_color_spanCount, 
  586.        WC->profile.read_color_spanTime, read_color_spanRate);
  587.    strcpy(szDebug, szTmp);
  588.    sprintf(szTmp, "%-23s %7d  %8.6g   %10.6g\n",
  589.        "read_index_pixels", WC->profile.read_index_pixelsCount, 
  590.        WC->profile.read_index_pixelsTime, read_index_pixelsRate);
  591.    strcat(szDebug, szTmp);
  592.    sprintf(szTmp, "%-23s %7d  %8.6g   %10.6g\n",
  593.        "read_index_span", WC->profile.read_index_spanCount, 
  594.        WC->profile.read_index_spanTime, read_index_spanRate);
  595.    strcat(szDebug, szTmp);
  596.    sprintf(szTmp, "%-23s %7d  %8.6g   %10.6g\n",
  597.        "set_buffer", WC->profile.set_bufferCount, 
  598.        WC->profile.set_bufferTime, set_bufferRate);
  599.    strcat(szDebug, szTmp);
  600.    sprintf(szTmp, "%-23s %7d  %8.6g   %10.6g\n",
  601.        "set_color", WC->profile.set_colorCount, 
  602.        WC->profile.set_colorTime, set_colorRate);
  603.    strcat(szDebug, szTmp);
  604.    OutputDebugString(szDebug);
  605.  
  606.    sprintf(szTmp, "%-23s %7d  %8.6g   %10.6g\n",
  607.        "set_index", WC->profile.set_indexCount, 
  608.        WC->profile.set_indexTime, set_indexRate);
  609.    strcpy(szDebug, szTmp);
  610.    sprintf(szTmp, "%-23s %7d  %8.6g   %10.6g\n",
  611.        "write_color_pixels", WC->profile.write_color_pixelsCount, 
  612.        WC->profile.write_color_pixelsTime, write_color_pixelsRate);
  613.    strcat(szDebug, szTmp);
  614.    sprintf(szTmp, "%-23s %7d  %8.6g   %10.6g\n",
  615.        "write_color_span", WC->profile.write_color_spanCount, 
  616.        WC->profile.write_color_spanTime, write_color_spanRate);
  617.    strcat(szDebug, szTmp);
  618.    sprintf(szTmp, "%-23s %7d  %8.6g   %10.6g\n",
  619.        "write_index_pixels", WC->profile.write_index_pixelsCount, 
  620.        WC->profile.write_index_pixelsTime, write_index_pixelsRate);
  621.    strcat(szDebug, szTmp);
  622.    sprintf(szTmp, "%-23s %7d  %8.6g   %10.6g\n",
  623.        "write_index_span", WC->profile.write_index_spanCount, 
  624.        WC->profile.write_index_spanTime, write_index_spanRate);
  625.    strcat(szDebug, szTmp);
  626.    sprintf(szTmp, "%-23s %7d  %8.6g   %10.6g\n",
  627.        "write_monocolor_pixels", WC->profile.write_monocolor_pixelsCount, 
  628.        WC->profile.write_monocolor_pixelsTime, write_monocolor_pixelsRate);
  629.    strcat(szDebug, szTmp);
  630.    sprintf(szTmp, "%-23s %7d  %8.6g   %10.6g\n",
  631.        "write_monocolor_span", WC->profile.write_monocolor_spanCount, 
  632.        WC->profile.write_monocolor_spanTime, write_monocolor_spanRate);
  633.    strcat(szDebug, szTmp);
  634.    sprintf(szTmp, "%-23s %7d  %8.6g   %10.6g\n",
  635.        "write_monoindex_pixels", WC->profile.write_monoindex_pixelsCount, 
  636.        WC->profile.write_monoindex_pixelsTime, write_monoindex_pixelsRate);
  637.    strcat(szDebug, szTmp);
  638.    sprintf(szTmp, "%-23s %7d  %8.6g   %10.6g\n",
  639.        "write_monoindex_span", WC->profile.write_monoindex_spanCount, 
  640.        WC->profile.write_monoindex_spanTime, write_monoindex_spanRate);
  641.    strcat(szDebug, szTmp);
  642.  
  643.    OutputDebugString(szDebug);
  644. #endif
  645. }
  646. #endif
  647.  
  648.  
  649.  
  650.  
  651.  
  652. /**********************************************************************/
  653. /*****       Context allocation, initialization, destroying       *****/
  654. /**********************************************************************/
  655.  
  656.  
  657. /*
  658.  * Allocate and initialize a display list group.
  659.  */
  660. static struct gl_list_group *alloc_display_list_group( void )
  661. {
  662.    struct gl_list_group *lg;
  663.    GLuint i;
  664.  
  665.    lg = (struct gl_list_group*) malloc( sizeof(struct gl_list_group) );
  666.    for (i=0;i<MAX_DISPLAYLISTS;i++) {
  667.       lg->List[i] = NULL;
  668.       lg->Reserved[i] = GL_FALSE;
  669.    }
  670.    lg->RefCount = 0;
  671.    return lg;
  672. }
  673.  
  674.  
  675.  
  676. /*
  677.  * Initialize the nth light.  Note that the defaults for light 0 are
  678.  * different than the other lights.
  679.  */
  680. static void init_light( struct gl_light *l, GLuint n )
  681. {
  682.    ASSIGN_4V( l->Ambient, 0.0, 0.0, 0.0, 1.0 );
  683.    if (n==0) {
  684.       ASSIGN_4V( l->Diffuse, 1.0, 1.0, 1.0, 1.0 );
  685.       ASSIGN_4V( l->Specular, 1.0, 1.0, 1.0, 1.0 );
  686.    }
  687.    else {
  688.       ASSIGN_4V( l->Diffuse, 0.0, 0.0, 0.0, 1.0 );
  689.       ASSIGN_4V( l->Specular, 0.0, 0.0, 0.0, 1.0 );
  690.    }
  691.    ASSIGN_4V( l->Position, 0.0, 0.0, 1.0, 0.0 );
  692.    ASSIGN_3V( l->Direction, 0.0, 0.0, -1.0 );
  693.    l->SpotExponent = 0.0;
  694.    gl_compute_spot_exp_table( l );
  695.    l->SpotCutoff = 180.0;
  696.    l->CosCutoff = -1.0;
  697.    l->ConstantAttenuation = 1.0;
  698.    l->LinearAttenuation = 0.0;
  699.    l->QuadraticAttenuation = 0.0;
  700.    l->Enabled = GL_FALSE;
  701. }
  702.  
  703.  
  704.  
  705. static void init_lightmodel( struct gl_lightmodel *lm )
  706. {
  707.    ASSIGN_4V( lm->Ambient, 0.2, 0.2, 0.2, 1.0 );
  708.    lm->LocalViewer = GL_FALSE;
  709.    lm->TwoSide = GL_FALSE;
  710. }
  711.  
  712.  
  713. static void init_material( struct gl_material *m )
  714. {
  715.    ASSIGN_4V( m->Ambient,  0.2, 0.2, 0.2, 1.0 );
  716.    ASSIGN_4V( m->Diffuse,  0.8, 0.8, 0.8, 1.0 );
  717.    ASSIGN_4V( m->Specular, 0.0, 0.0, 0.0, 1.0 );
  718.    ASSIGN_4V( m->Emission, 0.0, 0.0, 0.0, 1.0 );
  719.    m->Shininess = 0.0;
  720.    m->AmbientIndex = 0;
  721.    m->DiffuseIndex = 1;
  722.    m->SpecularIndex = 1;
  723.    gl_compute_material_shine_table( m );
  724. }
  725.  
  726.  
  727.  
  728. /*
  729.  * Initialize a gl_context structure to default values.
  730.  */
  731. void gl_initialize_context( struct gl_context *c )
  732. {
  733.    static GLfloat identity[16] = {
  734.     1.0, 0.0, 0.0, 0.0,
  735.     0.0, 1.0, 0.0, 0.0,
  736.     0.0, 0.0, 1.0, 0.0,
  737.     0.0, 0.0, 0.0, 1.0
  738.    };
  739.    GLuint i;
  740.  
  741.    if (c) {
  742.       /* Transformation matrices and stacks */
  743.       c->ModelViewStackDepth = 0;
  744.       MEMCPY( c->ModelViewMatrix, identity, 16*sizeof(GLfloat) );
  745.       MEMCPY( c->ModelViewInv, identity, 16*sizeof(GLfloat) );
  746.       c->ModelViewInvValid = GL_TRUE;
  747.  
  748.       c->ProjectionStackDepth = 0;
  749.       MEMCPY( c->ProjectionMatrix, identity, 16*sizeof(GLfloat) );
  750.  
  751.       c->TextureStackDepth = 0;
  752.       MEMCPY( c->TextureMatrix, identity, 16*sizeof(GLfloat) );
  753.       c->IdentityTexMat = GL_TRUE;
  754.  
  755.       /* Accumulate buffer group */
  756.       ASSIGN_4V( c->Accum.ClearColor, 0.0, 0.0, 0.0, 0.0 );
  757.  
  758.       /* Color buffer group */
  759.       c->Color.IndexMask = 0xffffffff;
  760.       c->Color.ColorMask = 0xf;
  761.       c->Color.SWmasking = GL_FALSE;
  762.       c->Color.ClearIndex = 0;
  763.       ASSIGN_4V( c->Color.ClearColor, 0.0, 0.0, 0.0, 0.0 );
  764.       c->Color.DrawBuffer = GL_FRONT;
  765.       c->Color.AlphaEnabled = GL_FALSE;
  766.       c->Color.AlphaFunc = GL_ALWAYS;
  767.       c->Color.AlphaRef = 0.0;
  768.       c->Color.BlendEnabled = GL_FALSE;
  769.       c->Color.BlendSrc = GL_ONE;
  770.       c->Color.BlendDst = GL_ZERO;
  771.       c->Color.BlendEquation = GL_FUNC_ADD_EXT;
  772.       ASSIGN_4V( c->Color.BlendColor, 0.0, 0.0, 0.0, 0.0 );
  773.       c->Color.LogicOpEnabled = GL_FALSE;
  774.       c->Color.SWLogicOpEnabled = GL_FALSE;
  775.       c->Color.LogicOp = GL_COPY;
  776.       c->Color.DitherFlag = GL_TRUE;
  777.  
  778.       /* Current group */
  779.       c->Current.Index = 1;
  780.       ASSIGN_3V( c->Current.Normal, 0.0, 0.0, 1.0 );
  781.       c->Current.IntColor[0] = (GLint) c->RedScale;
  782.       c->Current.IntColor[1] = (GLint) c->GreenScale;
  783.       c->Current.IntColor[2] = (GLint) c->BlueScale;
  784.       c->Current.IntColor[3] = (GLint) c->AlphaScale;
  785.       ASSIGN_4V( c->Current.RasterPos, 0.0, 0.0, 0.0, 1.0 );
  786.       c->Current.RasterPosValid = GL_TRUE;
  787.       c->Current.RasterIndex = 1;
  788.       ASSIGN_4V( c->Current.TexCoord, 0.0, 0.0, 0.0, 1.0 );
  789.       ASSIGN_4V( c->Current.RasterColor, 1.0, 1.0, 1.0, 1.0 );
  790.       c->Current.EdgeFlag = GL_TRUE;
  791.  
  792.       /* Depth buffer group */
  793.       c->Depth.Test = GL_FALSE;
  794.       c->Depth.Clear = 1.0;
  795.       c->Depth.Func = GL_LESS;
  796.       c->Depth.Mask = GL_TRUE;
  797.  
  798.       /* Evaluators group */
  799.       c->Eval.Map1Color4 = GL_FALSE;
  800.       c->Eval.Map1Index = GL_FALSE;
  801.       c->Eval.Map1Normal = GL_FALSE;
  802.       c->Eval.Map1TextureCoord1 = GL_FALSE;
  803.       c->Eval.Map1TextureCoord2 = GL_FALSE;
  804.       c->Eval.Map1TextureCoord3 = GL_FALSE;
  805.       c->Eval.Map1TextureCoord4 = GL_FALSE;
  806.       c->Eval.Map1Vertex3 = GL_FALSE;
  807.       c->Eval.Map1Vertex4 = GL_FALSE;
  808.       c->Eval.Map2Color4 = GL_FALSE;
  809.       c->Eval.Map2Index = GL_FALSE;
  810.       c->Eval.Map2Normal = GL_FALSE;
  811.       c->Eval.Map2TextureCoord1 = GL_FALSE;
  812.       c->Eval.Map2TextureCoord2 = GL_FALSE;
  813.       c->Eval.Map2TextureCoord3 = GL_FALSE;
  814.       c->Eval.Map2TextureCoord4 = GL_FALSE;
  815.       c->Eval.Map2Vertex3 = GL_FALSE;
  816.       c->Eval.Map2Vertex4 = GL_FALSE;
  817.       c->Eval.AutoNormal = GL_FALSE;
  818.       c->Eval.MapGrid1un = 1;
  819.       c->Eval.MapGrid1u1 = 0.0;
  820.       c->Eval.MapGrid1u2 = 1.0;
  821.       c->Eval.MapGrid2un = 1;
  822.       c->Eval.MapGrid2vn = 1;
  823.       c->Eval.MapGrid2u1 = 0.0;
  824.       c->Eval.MapGrid2u2 = 1.0;
  825.       c->Eval.MapGrid2v1 = 0.0;
  826.       c->Eval.MapGrid2v2 = 1.0;
  827.  
  828.       /* Fog group */
  829.       c->Fog.Enabled = GL_FALSE;
  830.       c->Fog.Mode = GL_EXP;
  831.       ASSIGN_4V( c->Fog.Color, 0.0, 0.0, 0.0, 0.0 );
  832.       c->Fog.Index = 0.0;
  833.       c->Fog.Density = 1.0;
  834.       c->Fog.Start = 0.0;
  835.       c->Fog.End = 1.0;
  836.  
  837.       /* Hint group */
  838.       c->Hint.PerspectiveCorrection = GL_DONT_CARE;
  839.       c->Hint.PointSmooth = GL_DONT_CARE;
  840.       c->Hint.LineSmooth = GL_DONT_CARE;
  841.       c->Hint.PolygonSmooth = GL_DONT_CARE;
  842.       c->Hint.Fog = GL_DONT_CARE;
  843.  
  844.       /* Lighting group */
  845.       for (i=0;i<MAX_LIGHTS;i++) {
  846.      init_light( &c->Light.Light[i], i );
  847.       }
  848.       init_lightmodel( &c->Light.Model );
  849.       init_material( &c->Light.Material[0] );
  850.       init_material( &c->Light.Material[1] );
  851.       c->Light.ShadeModel = GL_SMOOTH;
  852.       c->Light.Enabled = GL_FALSE;
  853.       c->Light.ColorMaterialFace = GL_FRONT_AND_BACK;
  854.       c->Light.ColorMaterialMode = GL_AMBIENT_AND_DIFFUSE;
  855.       c->Light.ColorMaterialEnabled = GL_FALSE;
  856.       c->Light.LastEnabled = -1;
  857.  
  858.       /* Line group */
  859.       c->Line.SmoothFlag = GL_FALSE;
  860.       c->Line.StippleFlag = GL_FALSE;
  861.       c->Line.Width = 1.0;
  862.       c->Line.StipplePattern = 0xffff;
  863.       c->Line.StippleFactor = 1;
  864.  
  865.       /* Display List group */
  866.       c->List.ListBase = 0;
  867.  
  868.       /* Pixel group */
  869.       c->Pixel.RedBias = 0.0;
  870.       c->Pixel.RedScale = 1.0;
  871.       c->Pixel.GreenBias = 0.0;
  872.       c->Pixel.GreenScale = 1.0;
  873.       c->Pixel.BlueBias = 0.0;
  874.       c->Pixel.BlueScale = 1.0;
  875.       c->Pixel.AlphaBias = 0.0;
  876.       c->Pixel.AlphaScale = 1.0;
  877.       c->Pixel.DepthBias = 0.0;
  878.       c->Pixel.DepthScale = 1.0;
  879.       c->Pixel.IndexOffset = 0;
  880.       c->Pixel.IndexShift = 0;
  881.       c->Pixel.ZoomX = 1.0;
  882.       c->Pixel.ZoomY = 1.0;
  883.       c->Pixel.MapColorFlag = GL_FALSE;
  884.       c->Pixel.MapStencilFlag = GL_FALSE;
  885.       c->Pixel.MapStoSsize = 1;
  886.       c->Pixel.MapItoIsize = 1;
  887.       c->Pixel.MapItoRsize = 1;
  888.       c->Pixel.MapItoGsize = 1;
  889.       c->Pixel.MapItoBsize = 1;
  890.       c->Pixel.MapItoAsize = 1;
  891.       c->Pixel.MapRtoRsize = 1;
  892.       c->Pixel.MapGtoGsize = 1;
  893.       c->Pixel.MapBtoBsize = 1;
  894.       c->Pixel.MapAtoAsize = 1;
  895.       c->Pixel.MapStoS[0] = 0;
  896.       c->Pixel.MapItoI[0] = 0;
  897.       c->Pixel.MapItoR[0] = 0.0;
  898.       c->Pixel.MapItoG[0] = 0.0;
  899.       c->Pixel.MapItoB[0] = 0.0;
  900.       c->Pixel.MapItoA[0] = 0.0;
  901.       c->Pixel.MapRtoR[0] = 0.0;
  902.       c->Pixel.MapGtoG[0] = 0.0;
  903.       c->Pixel.MapBtoB[0] = 0.0;
  904.       c->Pixel.MapAtoA[0] = 0.0;
  905.  
  906.       /* Point group */
  907.       c->Point.SmoothFlag = GL_FALSE;
  908.       c->Point.Size = 1.0;
  909.  
  910.       /* Polygon group */
  911.       c->Polygon.CullFlag = GL_FALSE;
  912.       c->Polygon.CullFaceMode = GL_BACK;
  913.       c->Polygon.FrontFace = GL_CCW;
  914.       c->Polygon.FrontMode = GL_FILL;
  915.       c->Polygon.BackMode = GL_FILL;
  916.       c->Polygon.Unfilled = GL_FALSE;
  917.       c->Polygon.SmoothFlag = GL_FALSE;
  918.       c->Polygon.StippleFlag = GL_FALSE;
  919.       c->Polygon.OffsetFactor = 0.0;
  920.       c->Polygon.OffsetBias = 0.0;
  921.       c->Polygon.OffsetEnabled = GL_FALSE;
  922.  
  923.       /* Polygon Stipple group */
  924.       MEMSET( c->PolygonStipple, 0xff, 32*sizeof(GLuint) );
  925.  
  926.       /* Scissor group */
  927.       c->Scissor.Enabled = GL_FALSE;
  928.       c->Scissor.X = 0;
  929.       c->Scissor.Y = 0;
  930.       c->Scissor.Width = 0;
  931.       c->Scissor.Height = 0;
  932.  
  933.       /* Stencil group */
  934.       c->Stencil.Enabled = GL_FALSE;
  935.       c->Stencil.Function = GL_ALWAYS;
  936.       c->Stencil.FailFunc = GL_KEEP;
  937.       c->Stencil.ZPassFunc = GL_KEEP;
  938.       c->Stencil.ZFailFunc = GL_KEEP;
  939.       c->Stencil.Ref = 0;
  940.       c->Stencil.ValueMask = 0xff;
  941.       c->Stencil.Clear = 0;
  942.       c->Stencil.WriteMask = 0xff;
  943.  
  944.       /* Texture group */
  945.       c->Texture.Enabled = 0;
  946.       c->Texture.EnvMode = GL_MODULATE;
  947.       ASSIGN_4V( c->Texture.EnvColor, 0.0, 0.0, 0.0, 0.0 );
  948.       ASSIGN_4V( c->Texture.BorderColor, 0.0, 0.0, 0.0, 0.0 );
  949.       c->Texture.TexGenEnabled = 0;
  950.       c->Texture.GenModeS = GL_EYE_LINEAR;
  951.       c->Texture.GenModeT = GL_EYE_LINEAR;
  952.       c->Texture.GenModeR = GL_EYE_LINEAR;
  953.       c->Texture.GenModeQ = GL_EYE_LINEAR;
  954.       ASSIGN_4V( c->Texture.ObjectPlaneS, 1.0, 0.0, 0.0, 0.0 );
  955.       ASSIGN_4V( c->Texture.ObjectPlaneT, 0.0, 1.0, 0.0, 0.0 );
  956.       ASSIGN_4V( c->Texture.ObjectPlaneR, 0.0, 0.0, 0.0, 0.0 );
  957.       ASSIGN_4V( c->Texture.ObjectPlaneQ, 0.0, 0.0, 0.0, 0.0 );
  958.       ASSIGN_4V( c->Texture.EyePlaneS, 1.0, 0.0, 0.0, 0.0 );
  959.       ASSIGN_4V( c->Texture.EyePlaneT, 0.0, 1.0, 0.0, 0.0 );
  960.       ASSIGN_4V( c->Texture.EyePlaneR, 0.0, 0.0, 0.0, 0.0 );
  961.       ASSIGN_4V( c->Texture.EyePlaneQ, 0.0, 0.0, 0.0, 0.0 );
  962.       c->Texture.WrapS1D = GL_REPEAT;
  963.       c->Texture.WrapT1D = GL_REPEAT;
  964.       c->Texture.WrapS2D = GL_REPEAT;
  965.       c->Texture.WrapT2D = GL_REPEAT;
  966.       c->Texture.MinFilter1D = GL_NEAREST_MIPMAP_LINEAR;
  967.       c->Texture.MagFilter1D = GL_LINEAR;
  968.       c->Texture.MinFilter2D = GL_NEAREST_MIPMAP_LINEAR;
  969.       c->Texture.MagFilter2D = GL_LINEAR;
  970.  
  971.       /* Transformation group */
  972.       c->Transform.MatrixMode = GL_MODELVIEW;
  973.       c->Transform.Normalize = GL_FALSE;
  974.       for (i=0;i<MAX_CLIP_PLANES;i++) {
  975.      c->Transform.ClipEnabled[i] = GL_FALSE;
  976.          ASSIGN_4V( c->Transform.ClipEquation[i], 0.0, 0.0, 0.0, 0.0 );
  977.       }
  978.       c->Transform.AnyClip = GL_FALSE;
  979.  
  980.       /* Viewport group */
  981.       c->Viewport.X = 0;
  982.       c->Viewport.Y = 0;
  983.       c->Viewport.Width = 0;
  984.       c->Viewport.Height = 0;   
  985.       c->Viewport.Near = 0.0;
  986.       c->Viewport.Far = 1.0;
  987.       c->Viewport.Sx = 0.0;  /* Sx, Tx, Sy, Ty are computed later */
  988.       c->Viewport.Tx = 0.0;
  989.       c->Viewport.Sy = 0.0;
  990.       c->Viewport.Ty = 0.0;
  991.       c->Viewport.Sz = 0.5;
  992.       c->Viewport.Tz = 0.5;
  993.  
  994.       /* Pixel transfer */
  995.       c->PackAlignment = 4;
  996.       c->PackRowLength = 0;
  997.       c->PackSkipPixels = 0;
  998.       c->PackSkipRows = 0;
  999.       c->PackSwapBytes = GL_FALSE;
  1000.       c->PackLSBFirst = GL_FALSE;
  1001.       c->UnpackAlignment = 4;
  1002.       c->UnpackRowLength = 0;
  1003.       c->UnpackSkipPixels = 0;
  1004.       c->UnpackSkipRows = 0;
  1005.       c->UnpackSwapBytes = GL_FALSE;
  1006.       c->UnpackLSBFirst = GL_FALSE;
  1007.  
  1008.       /* Feedback */
  1009.       c->FeedbackType = GL_2D;   /* TODO: verify */
  1010.       c->FeedbackBuffer = NULL;
  1011.       c->FeedbackBufferSize = 0;
  1012.       c->FeedbackCount = 0;
  1013.  
  1014.       /* Selection/picking */
  1015.       c->SelectBuffer = NULL;
  1016.       c->SelectBufferSize = 0;
  1017.       c->SelectBufferCount = 0;
  1018.       c->SelectHits = 0;
  1019.       c->NameStackDepth = 0;
  1020.  
  1021.       /* Attribute stack */
  1022.       c->AttribStackDepth = 0;
  1023.  
  1024.       /* Texture maps */
  1025.       for (i=0;i<MAX_TEXTURE_LEVELS;i++) {
  1026.      c->TextureComponents1D[i] = 0;
  1027.      c->TextureWidth1D[i] = 0;
  1028.      c->TextureBorder1D[i] = 0;
  1029.      c->TextureImage1D[i] = NULL;
  1030.      c->TextureImage1DDeleteFlag[i] = GL_FALSE;
  1031.      c->TextureComponents2D[i] = 0;
  1032.      c->TextureWidth2D[i] = 0;
  1033.      c->TextureHeight2D[i] = 0;
  1034.      c->TextureBorder2D[i] = 0;
  1035.      c->TextureImage2D[i] = NULL;
  1036.      c->TextureImage2DDeleteFlag[i] = GL_FALSE;
  1037.       }
  1038.  
  1039.       /*** Miscellaneous ***/
  1040.       c->NewState = GL_TRUE;
  1041.       c->RenderMode = GL_RENDER;
  1042.       c->Mode = GL_BITMAP;
  1043.  
  1044.       c->StippleCounter = 0;
  1045.       c->NeedNormals = GL_FALSE;
  1046.  
  1047.       if (c->RedScale==255.0F && c->GreenScale==255.0F && c->BlueScale==255.0F
  1048.           && c->AlphaScale==255.0F) {
  1049.          c->EightBitColor = GL_TRUE;
  1050.       }
  1051.       else {
  1052.          c->EightBitColor = GL_FALSE;
  1053.       }
  1054.       c->FastDrawPixels = c->RGBAflag && c->EightBitColor;
  1055.  
  1056.       c->VertexFunc = gl_execute_vertex;
  1057.       c->PointsFunc = NULL;
  1058.       c->LineFunc = NULL;
  1059.       c->PolygonFunc = NULL;
  1060.       c->AuxPolygonFunc = NULL;
  1061.  
  1062.       c->CallDepth = 0;
  1063.       c->ExecuteFlag = GL_TRUE;
  1064.       c->CompileFlag = GL_FALSE;
  1065.  
  1066.       c->BufferWidth = 0;
  1067.       c->BufferHeight = 0;
  1068.       c->DepthBuffer = NULL;
  1069.       c->AccumBuffer = NULL;
  1070.       c->StencilBuffer = NULL;
  1071.  
  1072.       c->ErrorValue = GL_NO_ERROR;
  1073.    }
  1074. }
  1075.  
  1076.  
  1077.  
  1078. /*
  1079.  * Allocate and initialize a gl_context structure.
  1080.  * Input:  rgb_flag - GL_TRUE or GL_FALSE to indicate if using RGB mode
  1081.  *         redscale, greenscale, bluescale, alphascale - if in RGB mode
  1082.  *               these values are used to scale floating point color values
  1083.  *               in [0,1] to integers in [0,scale]
  1084.  *         db_flag - GL_TRUE or GL_FALSE to indicate if using double buffering
  1085.  *         sharelist - another context to share display lists with or NULL
  1086.  */
  1087. struct gl_context *gl_new_context( GLboolean rgb_flag,
  1088.                                    GLfloat redscale,
  1089.                                    GLfloat greenscale,
  1090.                                    GLfloat bluescale,
  1091.                                    GLfloat alphascale,
  1092.                                    GLboolean db_flag,
  1093.                                    struct gl_context *shareList )
  1094. {
  1095.    struct gl_context *c;
  1096.  
  1097.    /* do some implementation tests */
  1098.    assert( sizeof(GLbyte) >= 1 );
  1099.    assert( sizeof(GLshort) >= 2 );
  1100.    assert( sizeof(GLint) >= 4 );
  1101.    assert( sizeof(GLubyte) >= 1 );
  1102.    assert( sizeof(GLushort) >= 2 );
  1103.    assert( sizeof(GLuint) >= 4 );
  1104.  
  1105.    gl_init_lists();
  1106.    gl_init_eval();
  1107.    gl_init_vb();
  1108.  
  1109.    c = (struct gl_context *) malloc( sizeof(struct gl_context) );
  1110.    if (c) {
  1111.       c->RGBAflag = rgb_flag;
  1112.  
  1113.       c->RedScale = redscale;
  1114.       c->GreenScale = greenscale;
  1115.       c->BlueScale = bluescale;
  1116.       c->AlphaScale = alphascale;
  1117.  
  1118.       gl_initialize_context( c );
  1119.  
  1120.       if (db_flag) {
  1121.          c->DBflag = GL_TRUE;
  1122.          c->Color.DrawBuffer = GL_BACK;
  1123.          c->Pixel.ReadBuffer = GL_BACK;
  1124.       }
  1125.       else {
  1126.          c->DBflag = GL_FALSE;
  1127.          c->Color.DrawBuffer = GL_FRONT;
  1128.          c->Pixel.ReadBuffer = GL_FRONT;
  1129.       }
  1130.  
  1131.       if (shareList) {
  1132.      /* share the group of display lists of another context */
  1133.      c->ListGroup = shareList->ListGroup;
  1134.       }
  1135.       else {
  1136.      /* allocate new group of display lists */
  1137.      c->ListGroup = alloc_display_list_group();
  1138.       }
  1139.       c->ListGroup->RefCount++;
  1140.  
  1141. #ifdef PROFILE
  1142.       init_timings( c );
  1143. #endif
  1144.  
  1145.       /* software alpha buffers */
  1146.       {
  1147.          char *alpha = getenv("MESA_ALPHA");
  1148.          c->FrontAlphaEnabled = c->BackAlphaEnabled = GL_FALSE;
  1149.          if (alpha) {
  1150.             if (strstr(alpha,"front")) {
  1151.                c->FrontAlphaEnabled = GL_TRUE;
  1152.             }
  1153.             if (strstr(alpha,"back")) {
  1154.                c->BackAlphaEnabled = GL_TRUE;
  1155.             }
  1156.          }
  1157.       }
  1158.  
  1159.    }
  1160.    return c;
  1161. }
  1162.  
  1163.  
  1164.  
  1165.  
  1166. /*
  1167.  * Destroy a gl_context structure.
  1168.  */
  1169. void gl_destroy_context( struct gl_context *c )
  1170. {
  1171.    if (c) {
  1172.  
  1173. #ifdef PROFILE
  1174. #ifndef WIN32
  1175.       if (getenv("MESA_PROFILE")) {
  1176. #else
  1177.       if (TRUE){
  1178. #endif
  1179.          struct gl_context *cc;
  1180.  
  1181.          if (c==CCptr) {
  1182.             cc = &CC;
  1183.          }
  1184.          else {
  1185.             cc = c;
  1186.          }
  1187.          print_timings( cc );
  1188.       }
  1189. #endif
  1190.  
  1191.       if (c->DepthBuffer)
  1192.      free(c->DepthBuffer);
  1193.       if (c->AccumBuffer)
  1194.      free(c->AccumBuffer);
  1195.       if (c->StencilBuffer)
  1196.          free(c->StencilBuffer);
  1197.  
  1198.       c->ListGroup->RefCount--;
  1199.       assert(c->ListGroup->RefCount>=0);
  1200.       if (c->ListGroup->RefCount==0) {
  1201.      /* free display list group */
  1202.      free( c->ListGroup );
  1203.       }
  1204.       free( (void *) c );
  1205.       if (c==CCptr) {
  1206.          CCptr = NULL;
  1207.       }
  1208.  
  1209.    }
  1210. }
  1211.  
  1212.  
  1213.  
  1214. /*
  1215.  * Set the current context.
  1216.  */
  1217. void gl_set_context( struct gl_context *c )
  1218. {
  1219.    if (c) {
  1220.       /* "write back" current context */
  1221.       if (CCptr) {
  1222.          MEMCPY( CCptr, &CC, sizeof(struct gl_context) );
  1223.       }
  1224.       /* "load" new context */
  1225.       CCptr = c;
  1226.       MEMCPY( &CC, c, sizeof(struct gl_context) );
  1227.    }
  1228.    PB_INIT( GL_BITMAP );
  1229.    CC.NewState = GL_TRUE;
  1230. }
  1231.  
  1232.  
  1233.  
  1234. /*
  1235.  * Copy attribute groups from one context to another.
  1236.  * Input:  src - source context
  1237.  *         dst - destination context
  1238.  *         mask - bitwise OR of GL_*_BIT flags
  1239.  */
  1240. void gl_copy_context( struct gl_context *src, struct gl_context *dst,
  1241.               GLuint mask )
  1242. {
  1243.    if (src==CCptr) {
  1244.       src = &CC;
  1245.    }
  1246.    else if (dst==CCptr) {
  1247.       dst = &CC;
  1248.    }
  1249.  
  1250.    if (mask & GL_ACCUM_BUFFER_BIT) {
  1251.       MEMCPY( &dst->Accum, &src->Accum, sizeof(struct gl_accum_attrib) );
  1252.    }
  1253.    if (mask & GL_COLOR_BUFFER_BIT) {
  1254.       MEMCPY( &dst->Color, &src->Color, sizeof(struct gl_colorbuffer_attrib) );
  1255.    }
  1256.    if (mask & GL_CURRENT_BIT) {
  1257.       MEMCPY( &dst->Current, &src->Current, sizeof(struct gl_current_attrib) );
  1258.    }
  1259.    if (mask & GL_DEPTH_BUFFER_BIT) {
  1260.       MEMCPY( &dst->Depth, &src->Depth, sizeof(struct gl_depthbuffer_attrib) );
  1261.    }
  1262.    if (mask & GL_ENABLE_BIT) {
  1263.       /* no op */
  1264.    }
  1265.    if (mask & GL_EVAL_BIT) {
  1266.       MEMCPY( &dst->Eval, &src->Eval, sizeof(struct gl_eval_attrib) );
  1267.    }
  1268.    if (mask & GL_FOG_BIT) {
  1269.       MEMCPY( &dst->Fog, &src->Fog, sizeof(struct gl_fog_attrib) );
  1270.    }
  1271.    if (mask & GL_HINT_BIT) {
  1272.       MEMCPY( &dst->Hint, &src->Hint, sizeof(struct gl_hint_attrib) );
  1273.    }
  1274.    if (mask & GL_LIGHTING_BIT) {
  1275.       MEMCPY( &dst->Light, &src->Light, sizeof(struct gl_light_attrib) );
  1276.    }
  1277.    if (mask & GL_LINE_BIT) {
  1278.       MEMCPY( &dst->Line, &src->Line, sizeof(struct gl_line_attrib) );
  1279.    }
  1280.    if (mask & GL_LIST_BIT) {
  1281.       MEMCPY( &dst->List, &src->List, sizeof(struct gl_list_attrib) );
  1282.    }
  1283.    if (mask & GL_PIXEL_MODE_BIT) {
  1284.       MEMCPY( &dst->Pixel, &src->Pixel, sizeof(struct gl_pixel_attrib) );
  1285.    }
  1286.    if (mask & GL_POINT_BIT) {
  1287.       MEMCPY( &dst->Point, &src->Point, sizeof(struct gl_point_attrib) );
  1288.    }
  1289.    if (mask & GL_POLYGON_BIT) {
  1290.       MEMCPY( &dst->Polygon, &src->Polygon, sizeof(struct gl_polygon_attrib) );
  1291.    }
  1292.    if (mask & GL_POLYGON_STIPPLE_BIT) {
  1293.       MEMCPY( &dst->PolygonStipple, &src->PolygonStipple, 32*sizeof(GLuint) );
  1294.    }
  1295.    if (mask & GL_SCISSOR_BIT) {
  1296.       MEMCPY( &dst->Scissor, &src->Scissor, sizeof(struct gl_scissor_attrib) );
  1297.    }
  1298.    if (mask & GL_STENCIL_BUFFER_BIT) {
  1299.       MEMCPY( &dst->Stencil, &src->Stencil, sizeof(struct gl_stencil_attrib) );
  1300.    }
  1301.    if (mask & GL_TEXTURE_BIT) {
  1302.       MEMCPY( &dst->Texture, &src->Texture, sizeof(struct gl_texture_attrib) );
  1303.    }
  1304.    if (mask & GL_TRANSFORM_BIT) {
  1305.       MEMCPY( &dst->Transform, &src->Transform, sizeof(struct gl_transform_attrib) );
  1306.    }
  1307.    if (mask & GL_VIEWPORT_BIT) {
  1308.       MEMCPY( &dst->Viewport, &src->Viewport, sizeof(struct gl_viewport_attrib) );
  1309.    }
  1310. }
  1311.  
  1312.  
  1313.  
  1314. /*
  1315.  * This is Mesa's error handler.  Normally, all that's done is the updating
  1316.  * of the current error value.  If Mesa is compiled with -DDEBUG or if the
  1317.  * environment variable "MESA_DEBUG" is defined then a real error message
  1318.  * is printed to stderr.
  1319.  * Input:  error - the error value
  1320.  *         s - a diagnostic string
  1321.  */
  1322. void gl_error( GLenum error, char *s )
  1323. {
  1324.    GLboolean debug;
  1325.  
  1326. #ifdef DEBUG
  1327.    debug = GL_TRUE;
  1328. #else
  1329.    if (getenv("MESA_DEBUG")) {
  1330.       debug = GL_TRUE;
  1331.    }
  1332.    else {
  1333.       debug = GL_FALSE;
  1334.    }
  1335. #endif
  1336.  
  1337.    if (debug) {
  1338.       char errstr[1000];
  1339.  
  1340.       switch (error) {
  1341.      case GL_NO_ERROR:
  1342.         strcpy( errstr, "GL_NO_ERROR" );
  1343.         break;
  1344.      case GL_INVALID_VALUE:
  1345.         strcpy( errstr, "GL_INVALID_VALUE" );
  1346.         break;
  1347.      case GL_INVALID_ENUM:
  1348.         strcpy( errstr, "GL_INVALID_ENUM" );
  1349.         break;
  1350.      case GL_INVALID_OPERATION:
  1351.         strcpy( errstr, "GL_INVALID_OPERATION" );
  1352.         break;
  1353.      case GL_STACK_OVERFLOW:
  1354.         strcpy( errstr, "GL_STACK_OVERFLOW" );
  1355.         break;
  1356.      case GL_STACK_UNDERFLOW:
  1357.         strcpy( errstr, "GL_STACK_UNDERFLOW" );
  1358.         break;
  1359.      case GL_OUT_OF_MEMORY:
  1360.         strcpy( errstr, "GL_OUT_OF_MEMORY" );
  1361.         break;
  1362.      default:
  1363.         strcpy( errstr, "unknown" );
  1364.         break;
  1365.       }
  1366.       fprintf( stderr, "Mesa Error (%s): %s\n", errstr, s );
  1367.    }
  1368.  
  1369.    if (CC.ErrorValue==GL_NO_ERROR) {
  1370.       CC.ErrorValue = error;
  1371.    }
  1372. }
  1373.  
  1374.  
  1375.  
  1376. GLenum glGetError( void )
  1377. {
  1378.    GLenum e;
  1379.  
  1380.    if (!CCptr) {
  1381.       /* No current context */
  1382.       return GL_NO_ERROR;
  1383.    }
  1384.  
  1385.    if (INSIDE_BEGIN_END) {
  1386.       gl_error( GL_INVALID_OPERATION, "glGetError" );
  1387.       return GL_INVALID_OPERATION;
  1388.    }
  1389.  
  1390.    e = CC.ErrorValue;
  1391.    CC.ErrorValue = GL_NO_ERROR;
  1392.    return e;
  1393. }
  1394.  
  1395.  
  1396.  
  1397. /*
  1398.  * Since the device driver may or may not support pixel logic ops we
  1399.  * have to make some extensive tests whenever glLogicOp, glBlendFunc,
  1400.  * glBlendEquation, glEn/Disable( GL_LOGIC_OP ), glEn/Disable( GL_BLEND ),
  1401.  * or glPopAttrib is called.
  1402.  */
  1403. static void update_pixel_logic( void )
  1404. {
  1405.    if (CC.RGBAflag) {
  1406.       /* RGBA mode blending w/ Logic Op */
  1407.       if (CC.Color.BlendEnabled && CC.Color.BlendEquation==GL_LOGIC_OP) {
  1408.      if ((*DD.logicop)( CC.Color.LogicOp )) {
  1409.         /* Device driver can do logic, don't have to do it in software */
  1410.         CC.Color.SWLogicOpEnabled = GL_FALSE;
  1411.      }
  1412.      else {
  1413.         /* Device driver can't do logic op so we do it in software */
  1414.         CC.Color.SWLogicOpEnabled = GL_TRUE;
  1415.      }
  1416.       }
  1417.       else {
  1418.      /* no logic op */
  1419.      (void) (*DD.logicop)( GL_COPY );
  1420.      CC.Color.SWLogicOpEnabled = GL_FALSE;
  1421.       }
  1422.    }
  1423.    else {
  1424.       /* CI mode Logic Op */
  1425.       if (CC.Color.LogicOpEnabled) {
  1426.      if ((*DD.logicop)( CC.Color.LogicOp )) {
  1427.         /* Device driver can do logic, don't have to do it in software */
  1428.         CC.Color.SWLogicOpEnabled = GL_FALSE;
  1429.      }
  1430.      else {
  1431.         /* Device driver can't do logic op so we do it in software */
  1432.         CC.Color.SWLogicOpEnabled = GL_TRUE;
  1433.      }
  1434.       }
  1435.       else {
  1436.      /* no logic op */
  1437.      (void) (*DD.logicop)( GL_COPY );
  1438.      CC.Color.SWLogicOpEnabled = GL_FALSE;
  1439.       }
  1440.    }
  1441. }
  1442.  
  1443.  
  1444.  
  1445. /*
  1446.  * Check if software implemented RGBA or Color Index masking is needed.
  1447.  */
  1448. static void update_pixel_masking( void )
  1449. {
  1450.    if (CC.RGBAflag) {
  1451.       if (CC.Color.ColorMask==0xf) {
  1452.          /* disable masking */
  1453.          (void) (*DD.color_mask)( GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE );
  1454.          CC.Color.SWmasking = GL_FALSE;
  1455.       }
  1456.       else {
  1457.          /* Ask DD to do color masking, if it can't we'll do it in software */
  1458.          GLboolean red   = (CC.Color.ColorMask & 8) ? GL_TRUE : GL_FALSE;
  1459.          GLboolean green = (CC.Color.ColorMask & 4) ? GL_TRUE : GL_FALSE;
  1460.          GLboolean blue  = (CC.Color.ColorMask & 2) ? GL_TRUE : GL_FALSE;
  1461.          GLboolean alpha = (CC.Color.ColorMask & 1) ? GL_TRUE : GL_FALSE;
  1462.          if ((*DD.color_mask)( red, green, blue, alpha )) {
  1463.             CC.Color.SWmasking = GL_FALSE;
  1464.          }
  1465.          else {
  1466.             CC.Color.SWmasking = GL_TRUE;
  1467.          }
  1468.       }
  1469.    }
  1470.    else {
  1471.       if (CC.Color.IndexMask==0xffffffff) {
  1472.          /* disable masking */
  1473.          (void) (*DD.index_mask)( 0xffffffff );
  1474.          CC.Color.SWmasking = GL_FALSE;
  1475.       }
  1476.       else {
  1477.          /* Ask DD to do index masking, if it can't we'll do it in software */
  1478.          if ((*DD.index_mask)( CC.Color.IndexMask )) {
  1479.             CC.Color.SWmasking = GL_FALSE;
  1480.          }
  1481.          else {
  1482.             CC.Color.SWmasking = GL_TRUE;
  1483.          }
  1484.       }
  1485.    }
  1486. }
  1487.  
  1488.  
  1489.  
  1490. /*
  1491.  * Recompute the value of CC.RasterMask, CC.ClipMask, etc. according to
  1492.  * the current context.
  1493.  */
  1494. static void update_rasterflags( void )
  1495. {
  1496.    CC.RasterMask = 0;
  1497.  
  1498.    if (CC.Color.AlphaEnabled)        CC.RasterMask |= ALPHATEST_BIT;
  1499.    if (CC.Color.BlendEnabled)        CC.RasterMask |= BLEND_BIT;
  1500.    if (CC.Depth.Test)            CC.RasterMask |= DEPTH_BIT;
  1501.    if (CC.Fog.Enabled)            CC.RasterMask |= FOG_BIT;
  1502.    if (CC.Color.SWLogicOpEnabled)    CC.RasterMask |= LOGIC_OP_BIT;
  1503.    if (CC.Scissor.Enabled)        CC.RasterMask |= SCISSOR_BIT;
  1504.    if (CC.Stencil.Enabled)        CC.RasterMask |= STENCIL_BIT;
  1505.    if (CC.Color.SWmasking)        CC.RasterMask |= MASKING_BIT;
  1506.    if (CC.FrontAlphaEnabled)        CC.RasterMask |= ALPHABUF_BIT;
  1507.    if (CC.BackAlphaEnabled)        CC.RasterMask |= ALPHABUF_BIT;
  1508.  
  1509.    /* Recompute ClipMask (what has to be interpolated when clipping) */
  1510.    CC.ClipMask = 0;
  1511.    if (CC.Texture.Enabled) {
  1512.       CC.ClipMask |= CLIP_TEXTURE_BIT;
  1513.    }
  1514.    if (CC.Light.ShadeModel==GL_SMOOTH) {
  1515.       if (CC.RGBAflag) {
  1516.      CC.ClipMask |= CLIP_FCOLOR_BIT;
  1517.      if (CC.Light.Model.TwoSide) {
  1518.         CC.ClipMask |= CLIP_BCOLOR_BIT;
  1519.      }
  1520.       }
  1521.       else {
  1522.      CC.ClipMask |= CLIP_FINDEX_BIT;
  1523.      if (CC.Light.Model.TwoSide) {
  1524.         CC.ClipMask |= CLIP_BINDEX_BIT;
  1525.      }
  1526.       }
  1527.    }
  1528.  
  1529.  
  1530.    /* Check if the equation of the plane for polygons has to be computed. */
  1531.    CC.ComputePlane = CC.Depth.Test || CC.Polygon.CullFlag
  1532.                   || CC.Light.Model.TwoSide || CC.Texture.Enabled
  1533.                   || CC.Polygon.OffsetEnabled || CC.Polygon.Unfilled;
  1534. }
  1535.  
  1536.  
  1537.  
  1538. /*
  1539.  * If CC.NewState==GL_TRUE then this function MUST be called before
  1540.  * rendering any primitive.  Basically, function pointers and miscellaneous
  1541.  * flags are updated to reflect the current state of the state machine.
  1542.  */
  1543. void gl_update_state( void )
  1544. {
  1545.    update_pixel_logic();
  1546.    update_pixel_masking();
  1547.    update_rasterflags();
  1548.  
  1549.    gl_update_lighting();
  1550.  
  1551.    /* Check if normal vectors are needed */
  1552.    if (CC.Light.Enabled
  1553.        || (CC.Texture.GenModeS==GL_SPHERE_MAP
  1554.            && (CC.Texture.TexGenEnabled & S_BIT))
  1555.        || (CC.Texture.GenModeT==GL_SPHERE_MAP
  1556.            && (CC.Texture.TexGenEnabled & T_BIT))) {
  1557.       CC.NeedNormals = GL_TRUE;
  1558.    }
  1559.    else {
  1560.       CC.NeedNormals = GL_FALSE;
  1561.    }
  1562.  
  1563.    /* Check if incoming colors can be modified during rasterization */
  1564.    if (CC.Fog.Enabled ||
  1565.        CC.Texture.Enabled ||
  1566.        CC.Color.BlendEnabled ||
  1567.        CC.Color.SWmasking ||
  1568.        CC.Color.SWLogicOpEnabled) {
  1569.       CC.MutablePixels = GL_TRUE;
  1570.    }
  1571.    else {
  1572.       CC.MutablePixels = GL_FALSE;
  1573.    }
  1574.  
  1575.    /* Check if all pixels generated are likely to be the same color */
  1576.    if (CC.Light.ShadeModel==GL_SMOOTH ||
  1577.        CC.Light.Enabled ||
  1578.        CC.Fog.Enabled ||
  1579.        CC.Texture.Enabled ||
  1580.        CC.Color.BlendEnabled ||
  1581.        CC.Color.SWmasking ||
  1582.        CC.Color.SWLogicOpEnabled) {
  1583.       CC.MonoPixels = GL_FALSE;       /* pixels probably multicolored */
  1584.    }
  1585.    else {
  1586.       /* pixels will all be same color, only glColor() can invalidate this. */
  1587.       CC.MonoPixels = GL_TRUE;
  1588.    }
  1589.  
  1590.    CC.Polygon.CullBits = 0;
  1591.    if (CC.Polygon.CullFlag) {
  1592.       if (CC.Polygon.CullFaceMode==GL_FRONT ||
  1593.           CC.Polygon.CullFaceMode==GL_FRONT_AND_BACK) {
  1594.          CC.Polygon.CullBits |= 1;
  1595.       }
  1596.       if (CC.Polygon.CullFaceMode==GL_BACK ||
  1597.           CC.Polygon.CullFaceMode==GL_FRONT_AND_BACK) {
  1598.          CC.Polygon.CullBits |= 2;
  1599.       }
  1600.    }
  1601.  
  1602.    gl_set_point_function();
  1603.    gl_set_line_function();
  1604.    gl_set_polygon_function();
  1605.    /*gl_set_triangle_function();*/
  1606.  
  1607.    CC.NewState = GL_FALSE;
  1608. }
  1609.  
  1610.