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

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