home *** CD-ROM | disk | FTP | other *** search
/ Amiga MA Magazine 1998 #6 / amigamamagazinepolishissue1998.iso / coders / mesa-1.2.8 / src / points.c < prev    next >
C/C++ Source or Header  |  1996-05-27  |  12KB  |  465 lines

  1. /* points.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: points.c,v 1.24 1995/12/30 17:18:40 brianp Exp $
  26.  
  27. $Log: points.c,v $
  28.  * Revision 1.24  1995/12/30  17:18:40  brianp
  29.  * divide texture S and T by Q, per Bill Triggs
  30.  *
  31.  * Revision 1.23  1995/12/30  00:58:12  brianp
  32.  * use integer vertex colors instead of floating point
  33.  *
  34.  * Revision 1.22  1995/12/20  15:26:05  brianp
  35.  * changed VB color indexes to GLuint
  36.  *
  37.  * Revision 1.21  1995/12/19  22:17:05  brianp
  38.  * removed 0.5 offset from window coordinates thanks to CC.RasterOffsetX/Y
  39.  *
  40.  * Revision 1.20  1995/10/17  21:41:56  brianp
  41.  * removed simple_ci/rgba_points() functions because of new device driver
  42.  *
  43.  * Revision 1.19  1995/09/28  19:39:10  brianp
  44.  * replaced ClipFlag[] with Unclipped[]
  45.  *
  46.  * Revision 1.18  1995/09/20  18:20:39  brianp
  47.  * prototype device driver changes described
  48.  *
  49.  * Revision 1.17  1995/09/13  14:50:38  brianp
  50.  * render an array of points rather than single points
  51.  *
  52.  * Revision 1.16  1995/07/25  18:36:32  brianp
  53.  * convert window coords from floats to ints by rounding, not truncating
  54.  *
  55.  * Revision 1.15  1995/07/15  14:03:58  brianp
  56.  * added texture mapped points
  57.  *
  58.  * Revision 1.14  1995/06/20  16:20:50  brianp
  59.  * do float-to-int depth scaling here instead of in draw.c
  60.  *
  61.  * Revision 1.13  1995/06/05  20:27:26  brianp
  62.  * better clipping of points with size > 1
  63.  *
  64.  * Revision 1.12  1995/05/22  21:02:41  brianp
  65.  * Release 1.2
  66.  *
  67.  * Revision 1.11  1995/05/12  16:57:22  brianp
  68.  * replaced CC.Mode!=0 with INSIDE_BEGIN_END
  69.  *
  70.  * Revision 1.10  1995/04/18  15:48:23  brianp
  71.  * fixed assignment of NULL to function pointers to prevent warnings on Suns
  72.  *
  73.  * Revision 1.9  1995/04/12  15:36:15  brianp
  74.  * updated to use DD.draw_* function pointers
  75.  *
  76.  * Revision 1.8  1995/03/24  15:33:32  brianp
  77.  * introduced VB
  78.  *
  79.  * Revision 1.7  1995/03/07  14:20:55  brianp
  80.  * updated for new XSetForeground/GC scheme
  81.  *
  82.  * Revision 1.6  1995/03/04  19:29:44  brianp
  83.  * 1.1 beta revision
  84.  *
  85.  * Revision 1.5  1995/03/04  19:16:47  brianp
  86.  * added size clamp
  87.  *
  88.  * Revision 1.4  1995/03/02  19:18:34  brianp
  89.  * new RasterMask logic
  90.  *
  91.  * Revision 1.3  1995/02/27  22:48:59  brianp
  92.  * modified for PB
  93.  *
  94.  * Revision 1.2  1995/02/27  15:08:12  brianp
  95.  * added Vcolor/Vindex scheme
  96.  *
  97.  * Revision 1.1  1995/02/24  14:26:49  brianp
  98.  * Initial revision
  99.  *
  100.  */
  101.  
  102.  
  103. #include "context.h"
  104. #include "dd.h"
  105. #include "feedback.h"
  106. #include "list.h"
  107. #include "macros.h"
  108. #include "pb.h"
  109. #include "span.h"
  110. #include "vb.h"
  111.  
  112.  
  113.  
  114.  
  115. void glPointSize( GLfloat size )
  116. {
  117.    if (CC.CompileFlag) {
  118.       gl_save_pointsize( size );
  119.    }
  120.    if (CC.ExecuteFlag) {
  121.       if (size<=0.0) {
  122.      gl_error( GL_INVALID_VALUE, "glPointSize" );
  123.      return;
  124.       }
  125.       if (INSIDE_BEGIN_END) {
  126.      gl_error( GL_INVALID_OPERATION, "glPointSize" );
  127.      return;
  128.       }
  129.  
  130.       CC.Point.Size = size;
  131.       CC.NewState = GL_TRUE;
  132.    }
  133. }
  134.  
  135.  
  136.  
  137. /**********************************************************************/
  138. /*****                    Rasterization                           *****/
  139. /**********************************************************************/
  140.  
  141.  
  142. /*
  143.  * There are 3 pairs (RGBA, CI) of point rendering functions:
  144.  *   1. simple:  size=1 and no special rasterization functions (fastest)
  145.  *   2. size1:  size=1 and any rasterization functions
  146.  *   3. general:  any size and rasterization functions (slowest)
  147.  *
  148.  * All point rendering functions take the same two arguments: first and
  149.  * last which specify that the points specified by VB[first] through
  150.  * VB[last] are to be rendered.
  151.  */
  152.  
  153.  
  154.  
  155. /*
  156.  * Put points in feedback buffer.
  157.  */
  158. static void feedback_points( GLuint first, GLuint last )
  159. {
  160.    GLuint i;
  161.    GLint shift = CC.ColorShift;
  162.  
  163.    for (i=first;i<=last;i++) {
  164.       if (VB.Unclipped[i]) {
  165.          GLfloat x, y, z, w;
  166.          GLfloat color[4];
  167.  
  168.          x = VB.Win[i][0] - CC.RasterOffsetX;
  169.          y = VB.Win[i][1] - CC.RasterOffsetY;
  170.          z = VB.Win[i][2];
  171.          w = VB.Clip[i][3];
  172.  
  173.          /* convert color from integer back to a float in [0,1] */
  174.          color[0] = (GLfloat) (VB.Color[i][0] >> shift) / CC.RedScale;
  175.          color[1] = (GLfloat) (VB.Color[i][1] >> shift) / CC.GreenScale;
  176.          color[2] = (GLfloat) (VB.Color[i][2] >> shift) / CC.BlueScale;
  177.          color[3] = (GLfloat) (VB.Color[i][3] >> shift) / CC.AlphaScale;
  178.  
  179.          APPEND_TOKEN( (GLfloat) GL_POINT_TOKEN );
  180.          gl_feedback_vertex( x, y, z, w, color,
  181.                              (GLfloat) VB.Index[i], VB.TexCoord[i] );
  182.       }
  183.    }
  184. }
  185.  
  186.  
  187.  
  188. /*
  189.  * Put points in selection buffer.
  190.  */
  191. static void select_points( GLuint first, GLuint last )
  192. {
  193.    GLuint i;
  194.  
  195.    for (i=first;i<=last;i++) {
  196.       if (VB.Unclipped[i]) {
  197.          GLfloat z = VB.Win[i][2];
  198.  
  199.          CC.HitFlag = GL_TRUE;
  200.          if (z < CC.HitMinZ) {
  201.             CC.HitMinZ = z;
  202.          }
  203.          if (z < CC.HitMinZ) {
  204.             CC.HitMaxZ = z;
  205.          }
  206.       }
  207.    }
  208. }
  209.  
  210.  
  211. /*
  212.  * CI points with size == 1.0
  213.  */
  214. static void size1_ci_points( GLuint first, GLuint last )
  215. {
  216.    GLuint i;
  217.  
  218.    for (i=first;i<=last;i++) {
  219.       if (VB.Unclipped[i]) {
  220.          GLint x, y, z;
  221.          x = (GLint)  VB.Win[i][0];
  222.          y = (GLint)  VB.Win[i][1];
  223.          z = (GLint) (VB.Win[i][2] * DEPTH_SCALE);
  224.          PB_WRITE_CI_PIXEL( x, y, z, VB.Index[i] );
  225.       }
  226.    }
  227.    PB_CHECK_FLUSH
  228. }
  229.  
  230.  
  231. /*
  232.  * RGBA points with size == 1.0
  233.  */
  234. static void size1_rgba_points( GLuint first, GLuint last )
  235. {
  236.    GLuint i;
  237.    GLint shift = CC.ColorShift;
  238.  
  239.    for (i=first;i<=last;i++) {
  240.       if (VB.Unclipped[i]) {
  241.          GLint x, y, z;
  242.          GLint red, green, blue, alpha;
  243.  
  244.          x = (GLint)  VB.Win[i][0];
  245.          y = (GLint)  VB.Win[i][1];
  246.          z = (GLint) (VB.Win[i][2] * DEPTH_SCALE);
  247.  
  248.          red   = VB.Color[i][0] >> shift;
  249.          green = VB.Color[i][1] >> shift;
  250.          blue  = VB.Color[i][2] >> shift;
  251.          alpha = VB.Color[i][3] >> shift;
  252.  
  253.          PB_WRITE_RGBA_PIXEL( x, y, z, red, green, blue, alpha );
  254.       }
  255.    }
  256.    PB_CHECK_FLUSH
  257. }
  258.  
  259.  
  260.  
  261. /*
  262.  * General CI points.
  263.  */
  264. static void general_ci_points( GLuint first, GLuint last )
  265. {
  266.    GLuint i;
  267.    GLint isize;
  268.  
  269.    isize = (GLint) (CLAMP(CC.Point.Size,MIN_POINT_SIZE,MAX_POINT_SIZE) + 0.5F);
  270.  
  271.    for (i=first;i<=last;i++) {
  272.       if (VB.Unclipped[i]) {
  273.          GLint x, y, z;
  274.          GLint x0, x1, y0, y1;
  275.          GLint ix, iy;
  276.  
  277.          x = (GLint)  VB.Win[i][0];
  278.          y = (GLint)  VB.Win[i][1];
  279.          z = (GLint) (VB.Win[i][2] * DEPTH_SCALE);
  280.  
  281.          if (isize&1) {
  282.             /* odd size */
  283.             x0 = x - isize/2;
  284.             x1 = x + isize/2;
  285.             y0 = y - isize/2;
  286.             y1 = y + isize/2;
  287.          }
  288.          else {
  289.             /* even size */
  290.             x0 = (GLint) (x + 0.5F) - isize/2;
  291.             x1 = x0 + isize-1;
  292.             y0 = (GLint) (y + 0.5F) - isize/2;
  293.             y1 = y0 + isize-1;
  294.          }
  295.  
  296.          PB_SET_INDEX( VB.Index[i] );
  297.  
  298.          for (iy=y0;iy<=y1;iy++) {
  299.             for (ix=x0;ix<=x1;ix++) {
  300.                PB_WRITE_PIXEL( ix, iy, z );
  301.             }
  302.          }
  303.          PB_CHECK_FLUSH
  304.       }
  305.    }
  306. }
  307.  
  308.  
  309. /*
  310.  * General RGBA points.
  311.  */
  312. static void general_rgba_points( GLuint first, GLuint last )
  313. {
  314.    GLuint i;
  315.    GLint isize;
  316.    GLint shift = CC.ColorShift;
  317.  
  318.    isize = (GLint) (CLAMP(CC.Point.Size,MIN_POINT_SIZE,MAX_POINT_SIZE) + 0.5F);
  319.  
  320.    for (i=first;i<=last;i++) {
  321.       if (VB.Unclipped[i]) {
  322.          GLint x, y, z;
  323.          GLint x0, x1, y0, y1;
  324.          GLint ix, iy;
  325.  
  326.          x = (GLint)  VB.Win[i][0];
  327.          y = (GLint)  VB.Win[i][1];
  328.          z = (GLint) (VB.Win[i][2] * DEPTH_SCALE);
  329.  
  330.          if (isize&1) {
  331.             /* odd size */
  332.             x0 = x - isize/2;
  333.             x1 = x + isize/2;
  334.             y0 = y - isize/2;
  335.             y1 = y + isize/2;
  336.          }
  337.          else {
  338.             /* even size */
  339.             x0 = (GLint) (x + 0.5F) - isize/2;
  340.             x1 = x0 + isize-1;
  341.             y0 = (GLint) (y + 0.5F) - isize/2;
  342.             y1 = y0 + isize-1;
  343.          }
  344.  
  345.          PB_SET_COLOR( VB.Color[i][0] >> shift,
  346.                        VB.Color[i][1] >> shift,
  347.                        VB.Color[i][2] >> shift,
  348.                        VB.Color[i][3] >> shift );
  349.  
  350.          for (iy=y0;iy<=y1;iy++) {
  351.             for (ix=x0;ix<=x1;ix++) {
  352.                PB_WRITE_PIXEL( ix, iy, z );
  353.             }
  354.          }
  355.          PB_CHECK_FLUSH
  356.       }
  357.    }
  358. }
  359.  
  360.  
  361.  
  362.  
  363. /*
  364.  * Textured RGBA points.
  365.  */
  366. static void textured_rgba_points( GLuint first, GLuint last )
  367. {
  368.    GLuint i;
  369.    GLint shift = CC.ColorShift;
  370.  
  371.    for (i=first;i<=last;i++) {
  372.       if (VB.Unclipped[i]) {
  373.          GLint x, y, z;
  374.          GLint x0, x1, y0, y1;
  375.          GLint ix, iy;
  376.          GLint isize;
  377.          GLint red, green, blue, alpha;
  378.          GLfloat s, t;
  379.  
  380.          x = (GLint)  VB.Win[i][0];
  381.          y = (GLint)  VB.Win[i][1];
  382.          z = (GLint) (VB.Win[i][2] * DEPTH_SCALE);
  383.  
  384.          isize = (GLint)
  385.                    (CLAMP(CC.Point.Size,MIN_POINT_SIZE,MAX_POINT_SIZE) + 0.5F);
  386.          if (isize<1) {
  387.             isize = 1;
  388.          }
  389.  
  390.          if (isize&1) {
  391.             /* odd size */
  392.             x0 = x - isize/2;
  393.             x1 = x + isize/2;
  394.             y0 = y - isize/2;
  395.             y1 = y + isize/2;
  396.          }
  397.          else {
  398.             /* even size */
  399.             x0 = (GLint) (x + 0.5F) - isize/2;
  400.             x1 = x0 + isize-1;
  401.             y0 = (GLint) (y + 0.5F) - isize/2;
  402.             y1 = y0 + isize-1;
  403.          }
  404.  
  405.          red   = VB.Color[i][0] >> shift;
  406.          green = VB.Color[i][1] >> shift;
  407.          blue  = VB.Color[i][2] >> shift;
  408.          alpha = VB.Color[i][3] >> shift;
  409.          s = VB.TexCoord[i][0] / VB.TexCoord[i][3];
  410.          t = VB.TexCoord[i][1] / VB.TexCoord[i][3];
  411.  
  412. /*    don't think this is needed
  413.          PB_SET_COLOR( red, green, blue, alpha );
  414. */
  415.  
  416.          for (iy=y0;iy<=y1;iy++) {
  417.             for (ix=x0;ix<=x1;ix++) {
  418.                PB_WRITE_TEX_PIXEL( ix, iy, z, red, green, blue, alpha, s, t );
  419.             }
  420.          }
  421.          PB_CHECK_FLUSH
  422.       }
  423.    }
  424. }
  425.  
  426.  
  427.  
  428.  
  429. /*
  430.  * Examine the current context to determine which point drawing function
  431.  * should be used.
  432.  */
  433. void gl_set_point_function( void )
  434. {
  435.    /* TODO: antialiased points */
  436.  
  437.    if (CC.RenderMode==GL_RENDER) {
  438.       CC.PointsFunc = (*DD.get_points_func)();
  439.       if (CC.PointsFunc) {
  440.          /* Device driver will draw points. */
  441.       }
  442.       else if (CC.Texture.Enabled) {
  443.      CC.PointsFunc = textured_rgba_points;
  444.       }
  445.       else if (CC.Point.Size==1.0) {
  446.          /* size=1, any raster ops */
  447.          CC.PointsFunc = CC.RGBAflag ? size1_rgba_points : size1_ci_points;
  448.       }
  449.       else {
  450.      /* every other kind of point rendering */
  451.      CC.PointsFunc = CC.RGBAflag ? general_rgba_points : general_ci_points;
  452.       }
  453.    }
  454.    else if (CC.RenderMode==GL_FEEDBACK) {
  455.       CC.PointsFunc = feedback_points;
  456.    }
  457.    else {
  458.       /* GL_SELECT mode */
  459.       CC.PointsFunc = select_points;
  460.    }
  461.  
  462. }
  463.  
  464.  
  465.