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 / ddsample.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-10-30  |  16.0 KB  |  648 lines

  1. /* $Id: ddsample.c,v 1.3 1995/10/30 15:28:55 brianp Exp $ */
  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. $Log: ddsample.c,v $
  26.  * Revision 1.3  1995/10/30  15:28:55  brianp
  27.  * pass mask array to read_[index|color]_pixels()
  28.  *
  29.  * Revision 1.2  1995/10/19  15:51:39  brianp
  30.  * updated set_color, clear_color and gl_new_context arguments
  31.  *
  32.  * Revision 1.1  1995/10/17  21:24:20  brianp
  33.  * Initial revision
  34.  *
  35.  */
  36.  
  37.  
  38.  
  39. /*
  40.  * This is a sample template for writing new Mesa device drivers.
  41.  * Let's say you're interfacing Mesa to a window/operating system
  42.  * called FOO.  Replace all occurances of FOO with the real name
  43.  * you select for your interface  (i.e. XMesa, WMesa, AMesa).
  44.  *
  45.  * You'll have to design an API for clients to use, defined in a
  46.  * header called Mesa/include/GL/FOOmesa.h
  47.  *
  48.  * Note that you'll usually have to flip Y coordinates since Mesa's
  49.  * window coordinates start at the bottom and increase upward.  Most
  50.  * window system's Y-axis increases downward
  51.  *
  52.  * See dd.h for more device driver info.
  53.  * See the other device driver implementations for ideas.
  54.  *
  55.  * The Makefile should compile this module along with the rest of
  56.  * the core Mesa library.
  57.  */
  58.  
  59.  
  60. #include <stdlib.h>
  61. #include "gl/FOOMesa.h"
  62. #include "context.h"
  63. #include "dd.h"
  64. #include "xform.h"
  65. #include "vb.h"
  66.  
  67.  
  68.  
  69.  
  70. /*
  71.  * This is the FOO/Mesa context structure.  This usually contains
  72.  * info about what window/buffer we're rendering too, the current
  73.  * drawing color, etc.
  74.  */
  75. struct FOOmesa_context
  76. {
  77.    struct gl_context *gl_ctx;    /* the core library context */
  78.    GLboolean db_flag;    /* double buffered? */
  79.    GLboolean rgb_flag;    /* RGB mode? */
  80.    GLuint depth;        /* bits per pixel (1, 8, 24, etc) */
  81.    unsigned long pixel;    /* current color index or RGBA pixel value */
  82.    unsigned long clearpixel; /* pixel for clearing the color buffers */
  83.  
  84.    /* etc... */
  85. };
  86.  
  87.  
  88.  
  89.  
  90. /**********************************************************************/
  91. /*****              Miscellaneous device driver funcs             *****/
  92. /**********************************************************************/
  93.  
  94.  
  95. static void finish( void )
  96. {
  97.    /* implements glFinish if possible */
  98. }
  99.  
  100.  
  101.  
  102. static void flush( void )
  103. {
  104.    /* implements glFlush if possible */
  105. }
  106.  
  107.  
  108.  
  109. static void clear_index( GLuint index )
  110. {
  111.    /* implement glClearIndex */
  112.    /* usually just save the value in the context struct */
  113. }
  114.  
  115.  
  116.  
  117. static void clear_color( GLubyte r, GLubyte g, GLubyte b, GLubyte a )
  118. {
  119.    /* implement glClearColor */
  120.    /* color components are floats in [0,1] */
  121.    /* usually just save the value in the context struct */
  122. }
  123.  
  124.  
  125.  
  126. static void clear( GLboolean all, GLint x, GLint y, GLint width, GLint height )
  127. {
  128. /*
  129.  * Clear the specified region of the color buffer using the clear color
  130.  * or index as specified by one of the two functions above.
  131.  * If all==GL_TRUE, clear whole buffer */
  132.  */
  133. }
  134.  
  135.  
  136.  
  137. static void set_index( GLuint index )
  138. {
  139.    /* Set the current color index. */
  140. }
  141.  
  142.  
  143.  
  144. static void set_color( GLubyte r, GLubyte g, GLubyte b, GLubyte a )
  145. {
  146.    /* Set the current RGBA color. */
  147.    /* r is in 0..CC.RedScale */
  148.    /* g is in 0..CC.GreenScale */
  149.    /* b is in 0..CC.BlueScale */
  150.    /* a is in 0..CC.AlphaScale */
  151. }
  152.  
  153.  
  154.  
  155. static GLboolean index_mask( GLuint mask )
  156. {
  157.    /* implement glIndexMask if possible, else return GL_FALSE */
  158. }
  159.  
  160.  
  161.  
  162. static GLboolean color_mask( GLboolean rmask, GLboolean gmask,
  163.                              GLboolean bmask, GLboolean amask)
  164. {
  165.    /* implement glColorMask if possible, else return GL_FALSE */
  166. }
  167.  
  168.  
  169.  
  170. static GLboolean logicop( GLenum op )
  171. {
  172.    /*
  173.     * Implements glLogicOp if possible.  Return GL_TRUE if the device driver
  174.     * can perform the operation, otherwise return GL_FALSE.  If GL_FALSE
  175.     * is returned, the logic op will be done in software by Mesa.
  176.     */
  177. }
  178.  
  179.  
  180. static void dither( GLboolean enable )
  181. {
  182.    /* enable/disable dithering if applicable */
  183. }
  184.  
  185.  
  186.  
  187. static GLboolean set_buffer( GLenum mode )
  188. {
  189.    /* set the current drawing/reading buffer, return GL_TRUE or GL_FALSE */
  190.    /* for success/failure */
  191.    setup_DD_pointers();
  192. }
  193.  
  194.  
  195.  
  196. static void buffer_size( GLuint *width, GLuint *height, GLuint *depth )
  197. {
  198.    /* return the width, height and depth (bits/pixel) of the current buffer */
  199.    /* if anything special has to been done when the buffer/window is */
  200.    /* resized, do it now */
  201. }
  202.  
  203.  
  204.  
  205. /**********************************************************************/
  206. /*****           Accelerated point, line, polygon rendering       *****/
  207. /**********************************************************************/
  208.  
  209.  
  210. static void fast_points_function( GLuint first, GLuint last )
  211. {
  212.    /* Render a number of points by some hardware/OS accerated method */
  213.    if (VB.MonoColor) {
  214.       /* draw all points using the current color (set_color) */
  215.       for (i=first;i<=last;i++) {
  216.          if (VB.Unclipped[i]) {
  217.             /* compute window coordinate */
  218.             int x, y;
  219.             x =       (GLint) (VB.Win[i][0] + 0.05F);
  220.             y = FLIP( (GLint) (VB.Win[i][1] + 0.05F) );
  221.             PLOT_PIXEL( x, y, currentcolor );
  222.          }
  223.       }
  224.    }
  225.    else {
  226.       /* each point is a different color */
  227.       for (i=first;i<=last;i++) {
  228.          if (VB.Unclipped[i]) {
  229.             int x, y;
  230.             x =       (GLint) (VB.Win[i][0] + 0.05F);
  231.             y = FLIP( (GLint) (VB.Win[i][1] + 0.05F) );
  232.             PLOT_PIXEL( x, y, VB.Color[i] );
  233.          }
  234.       }
  235.    }
  236. }
  237.  
  238.  
  239.  
  240. static points_func choose_points_function( void )
  241. {
  242.    /* Examine the current rendering state and return a pointer to a */
  243.    /* fast point-rendering function if possible. */
  244.    if (CC.Point.Size==1.0 && !CC.Point.SmoothFlag && CC.RasterMask==0
  245.        && !CC.Texture.Enabled && /* ETC, ETC */) {
  246.       return fast_points_function;
  247.    }
  248.    else {
  249.       return NULL;
  250.    }
  251. }
  252.  
  253.  
  254.  
  255. static void fast_line_function( GLuint v0, GLuint v1, GLuint pv )
  256. {
  257.    /* Render a line by some hardware/OS accerated method */
  258.    int x0, y0, x1, y1;
  259.    unsigned long pixel;
  260.  
  261.    if (VB.MonoColor) {
  262.       pixel = current color;
  263.    }
  264.    else {
  265.       pixel = VB.Color[pv];
  266.    }
  267.  
  268.    x0 =       (int) (VB.Win[v0][0] + 0.05F);
  269.    y0 = FLIP( (int) (VB.Win[v0][1] + 0.05F) );
  270.    x1 =       (int) (VB.Win[v1][0] + 0.05F);
  271.    y1 = FLIP( (int) (VB.Win[v1][1] + 0.05F) );
  272.  
  273.    DRAW_LINE( x0,y0, x1,y1, pixel );
  274. }
  275.  
  276.  
  277.  
  278. static line_func choose_line_function( void )
  279. {
  280.    /* Examine the current rendering state and return a pointer to a */
  281.    /* fast line-rendering function if possible. */
  282.    if (CC.Line.Width==1.0 && !CC.Line.SmoothFlag && !CC.Line.StippleFlag
  283.        && CC.Light.ShadeModel==GL_FLAT && CC.RasterMask==0
  284.        && !CC.Texture.Enabled && /* ETC, ETC */ ) {
  285.       return fast_line_function;
  286.    }
  287.    else {
  288.       return NULL;
  289.    }
  290. }
  291.  
  292.  
  293. static void fast_polygon_function( GLuint n, GLuint vlist[], GLuint pv )
  294. {
  295.    /* Render a line by some hardware/OS accerated method */
  296.    int i;
  297.  
  298.    if (VB.MonoColor) {
  299.       pixel = current color or index;
  300.    }
  301.    else {
  302.       pixel = VB.Color[pv] or VB.Index[pv];
  303.    }
  304.  
  305.    BEGIN_POLYGON;
  306.    for (i=0; i<n; i++) {
  307.       int x, y;
  308.       j = vlist[i];
  309.       x =       (int) (VB.Win[j][0] + 0.05F);
  310.       y = FLIP( (int) (VB.Win[j][1] + 0.05F) );
  311.       VERTEX( x, y );
  312.    }
  313.    END_POLYGON;
  314. }
  315.  
  316.  
  317.  
  318. static polygon_func choose_polygon_function( void )
  319. {
  320.    /* Examine the current rendering state and return a pointer to a */
  321.    /* fast polygon-rendering function if possible. */
  322.    if (!CC.Polygon.SmoothFlag && !CC.Polygon.StippleFlag
  323.        && CC.Light.ShadeModel==GL_FLAT && CC.RasterMask==0
  324.        && !CC.Texture.Enabled && /* ETC, ETC */ ) {
  325.       return fast_polygon_function;
  326.    }
  327.    else {
  328.       return NULL;
  329.    }
  330. }
  331.  
  332.  
  333.  
  334. /**********************************************************************/
  335. /*****            Write spans of pixels                           *****/
  336. /**********************************************************************/
  337.  
  338.  
  339. static void write_index_span( GLuint n, GLint x, GLint y,
  340.                               const GLuint index[],
  341.                               const GLubyte mask[] )
  342. {
  343.    int i;
  344.    for (i=0;i<n;i++) {
  345.       if (mask[i]) {
  346.          /* draw pixel (x[i],y[i]) using index[i] */
  347.       }
  348.    }
  349. }
  350.  
  351.  
  352.  
  353. static void write_monoindex_span(GLuint n,GLint x,GLint y,const GLubyte mask[])
  354. {
  355.    int i;
  356.    for (i=0;i<n;i++) {
  357.       if (mask[i]) {
  358.          /* draw pixel (x[i],y[i]) using current color index */
  359.       }
  360.    }
  361. }
  362.  
  363.  
  364.  
  365. static void write_color_span( GLuint n, GLint x, GLint y,
  366.                               const GLubyte red[], const GLubyte green[],
  367.                               const GLubyte blue[], const GLubyte alpha[],
  368.                               const GLubyte mask[] )
  369. {
  370.    int i;
  371.    y=FLIP(y);
  372.    if (mask) {
  373.       /* draw some pixels */
  374.       for (i=0; i<n; i++, x++) {
  375.          if (mask[i]) {
  376.             /* draw pixel x,y using color red[i]/green[i]/blue[i]/alpha[i] */
  377.          }
  378.       }
  379.    }
  380.    else {
  381.       /* draw all pixels */
  382.       for (i=0; i<n; i++, x++) {
  383.          /* draw pixel x,y using color red[i]/green[i]/blue[i]/alpha[i] */
  384.       }
  385.    }
  386. }
  387.  
  388.  
  389.  
  390. static void write_monocolor_span( GLuint n, GLint x, GLint y,
  391.                                   const GLubyte mask[])
  392. {
  393.    int i;
  394.    y=FLIP(y);
  395.    for (i=0; i<n; i++, x++) {
  396.       if (mask[i]) {
  397.          plot pixel (x,y) using current color
  398.       }
  399.    }
  400. }
  401.  
  402.  
  403.  
  404. /**********************************************************************/
  405. /*****                 Read spans of pixels                       *****/
  406. /**********************************************************************/
  407.  
  408.  
  409. static void read_index_span( GLuint n, GLint x, GLint y, GLuint index[])
  410. {
  411.    int i;
  412.    for (i=0; i<n; i++) {
  413.       index[i] = read_pixel(x[i],y[i]);
  414.    }
  415. }
  416.  
  417.  
  418.  
  419. static void read_color_span( GLuint n, GLint x, GLint y,
  420.                              GLubyte red[], GLubyte green[],
  421.                              GLubyte blue[], GLubyte alpha[] )
  422. {
  423.    int i;
  424.    for (i=0; i<n; i++, x++) {
  425.       red[i] = read_red( x, y );
  426.       green[i] = read_green( x, y );
  427.       /* etc */
  428.    }
  429. }
  430.  
  431.  
  432.  
  433. /**********************************************************************/
  434. /*****              Write arrays of pixels                        *****/
  435. /**********************************************************************/
  436.  
  437.  
  438. static void write_index_pixels( GLuint n, const GLint x[], const GLint y[],
  439.                                 const GLuint index[], const GLubyte mask[] )
  440. {
  441.    int i;
  442.    for (i=0; i<n; i++) {
  443.       if (mask[i]) {
  444.          plot pixel x[i], y[i] using index[i]
  445.       }
  446.    }
  447. }
  448.  
  449.  
  450.  
  451. static void write_monoindex_pixels( GLuint n,
  452.                                     const GLint x[], const GLint y[],
  453.                                     const GLubyte mask[] )
  454. {
  455.    int i;
  456.    for (i=0; i<n; i++) {
  457.       if (mask[i]) {
  458.          write pixel x[i], y[i] using current index
  459.       }
  460.    }
  461. }
  462.  
  463.  
  464.  
  465. static void write_color_pixels( GLuint n, const GLint x[], const GLint y[],
  466.                                 const GLubyte r[], const GLubyte g[],
  467.                                 const GLubyte b[], const GLubyte a[],
  468.                                 const GLubyte mask[] )
  469. {
  470.    int i;
  471.    for (i=0; i<n; i++) {
  472.       if (mask[i]) {
  473.          write pixel x[i], y[i] using red[i],green[i],blue[i],alpha[i]
  474.       }
  475.    }
  476. }
  477.  
  478.  
  479.  
  480. static void write_monocolor_pixels( GLuint n,
  481.                                     const GLint x[], const GLint y[],
  482.                                     const GLubyte mask[] )
  483. {
  484.    int i;
  485.    for (i=0; i<n; i++) {
  486.       if (mask[i]) {
  487.          write pixel x[i], y[i] using current color
  488.       }
  489.    }
  490. }
  491.  
  492.  
  493.  
  494.  
  495. /**********************************************************************/
  496. /*****                   Read arrays of pixels                    *****/
  497. /**********************************************************************/
  498.  
  499. /* Read an array of color index pixels. */
  500. static void read_index_pixels( GLuint n, const GLint x[], const GLint y[],
  501.                                GLuint indx[], const GLubyte mask[] )
  502. {
  503.   int i;
  504.   for (i=0; i<n; i++) {
  505.      if (mask[i]) {
  506.         index[i] = read_pixel x[i], y[i]
  507.      }
  508.   }
  509. }
  510.  
  511.  
  512.  
  513. static void read_color_pixels( GLuint n, const GLint x[], const GLint y[],
  514.                                GLubyte red[], GLubyte green[],
  515.                                GLubyte blue[], GLubyte alpha[],
  516.                                const GLubyte mask[] )
  517. {
  518.    int i;
  519.    for (i=0; i<n; i++) {
  520.       if (mask[i]) {
  521.          red[i] = read_red( x[i], y[i] );
  522.          green[i] = read_green( x[i], y[i] );
  523.          /* etc */
  524.       }
  525.    }
  526. }
  527.  
  528.  
  529.  
  530.  
  531. /**********************************************************************/
  532. /**********************************************************************/
  533.  
  534.  
  535. static void setup_DD_pointers( void )
  536. {
  537.    /* Initialize all the pointers in the DD struct.  Do this whenever */
  538.    /* a new context is made current or we change buffers via set_buffer! */
  539.  
  540.    DD.finish = finish;
  541.    DD.flush = flush;
  542.  
  543.    DD.clear_index = clear_index;
  544.    DD.clear_color = clear_color;
  545.    DD.clear = clear;
  546.  
  547.    DD.index = set_index;
  548.    DD.color = set_color;
  549.    DD.index_mask = index_mask;
  550.    DD.color_mask = color_mask;
  551.  
  552.    DD.logicop = logicop;
  553.    DD.dither = dither;
  554.  
  555.    DD.set_buffer = set_buffer;
  556.    DD.buffer_size = buffer_size;
  557.  
  558.    DD.get_points_func = choose_points_function;
  559.    DD.get_line_func = choose_line_function;
  560.    DD.get_polygon_func = choose_polygon_function;
  561.  
  562.    /* Pixel/span writing functions: */
  563.    DD.write_color_span       = write_color_span;
  564.    DD.write_monocolor_span   = write_monocolor_span;
  565.    DD.write_color_pixels     = write_color_pixels;
  566.    DD.write_monocolor_pixels = write_monocolor_pixels;
  567.    DD.write_index_span       = write_index_span;
  568.    DD.write_monoindex_span   = write_monoindex_span;
  569.    DD.write_index_pixels     = write_index_pixels;
  570.    DD.write_monoindex_pixels = write_monoindex_pixels;
  571.  
  572.    /* Pixel/span reading functions: */
  573.    DD.read_index_span = read_index_span;
  574.    DD.read_color_span = read_color_span;
  575.    DD.read_index_pixels = read_index_pixels;
  576.    DD.read_color_pixels = read_color_pixels;
  577. }
  578.  
  579.  
  580.  
  581. /**********************************************************************/
  582. /*****               FOO/Mesa API Functions                       *****/
  583. /**********************************************************************/
  584.  
  585.  
  586. /*
  587.  * Implement the client-visible FOO/Mesa interface functions defined
  588.  * in Mesa/include/GL/FOOmesa.h
  589.  */
  590.  
  591.  
  592.  
  593. FOOMesaContext FOOMesaCreateContext( /* some args */,
  594.                                      GLboolean rgb_flag,
  595.                                      GLboolean db_flag )
  596. {
  597.    /* Create a new FOO/Mesa context */
  598.    /* Be sure to initialize the following in the core Mesa context: */
  599.    /* RedScale, GreenScale, BlueScale, AlphaScale */
  600.    /* DrawBuffer, ReadBuffer */
  601.    /* RGBAflag, DBflag */
  602.    FOOMesaContext c;
  603.  
  604.    c->gl_ctx = gl_new_context( rgb_flag,
  605.                                redscale, greenscale, bluescale, alphascale,
  606.                                db_flag, sharelist );
  607.  
  608.  
  609.   return newcontext;
  610. }
  611.  
  612.  
  613.  
  614. void FOOMesaDestroyContext( FOOMesaContext c )
  615. {
  616.    /* destroy a FOO/Mesa context */
  617.    gl_destroy_context( c->gl_ctx );
  618.    free( c );
  619. }
  620.  
  621.  
  622.  
  623. void FOOMesaMakeCurrent( FOOMesaContext c )
  624. {
  625.    /* Make the specified context the current one */
  626.    /* the order of operations here is very important! */
  627.    gl_set_context( c->gl_ctx );
  628.    Current-context = c;
  629.    setup_DD_pointers();
  630.    if (Current->gl_ctx->Viewport.Width==0) {
  631.       /* initialize viewport to window size */
  632.       gl_viewport( 0, 0, Current->width, Current->height );
  633.    }
  634.    /* whatever else... */
  635. }
  636.  
  637.  
  638.  
  639. void FOOMesaSwapBuffers( void )
  640. {
  641.    /* copy/swap back buffer to front if applicable */
  642. }
  643.  
  644.  
  645.  
  646. /* you may need other FOO/Mesa functions too.......... */
  647.  
  648.