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

  1. /* $Id: svgamesa.c,v 1.3 1996/04/25 20:49:54 brianp Exp $ */
  2.  
  3.  
  4. /*
  5.  * Mesa 3-D graphics library
  6.  * Version:  1.2
  7.  * Copyright (C) 1995-1996  Brian Paul  (brianp@ssec.wisc.edu)
  8.  *
  9.  * This library is free software; you can redistribute it and/or
  10.  * modify it under the terms of the GNU Library General Public
  11.  * License as published by the Free Software Foundation; either
  12.  * version 2 of the License, or (at your option) any later version.
  13.  *
  14.  * This library is distributed in the hope that it will be useful,
  15.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  17.  * Library General Public License for more details.
  18.  *
  19.  * You should have received a copy of the GNU Library General Public
  20.  * License along with this library; if not, write to the Free
  21.  * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  22.  */
  23.  
  24.  
  25. /*
  26. $Log: svgamesa.c,v $
  27.  * Revision 1.3  1996/04/25  20:49:54  brianp
  28.  * removed optional, unimplemented DD functions
  29.  *
  30.  * Revision 1.2  1996/01/16  17:06:56  brianp
  31.  * changed "svgamesa.h" to "GL/svgamesa.h"
  32.  *
  33.  * Revision 1.1  1996/01/16  15:16:41  brianp
  34.  * Initial revision
  35.  *
  36.  */
  37.  
  38.  
  39. /*
  40.  * Linux SVGA/Mesa interface.
  41.  *
  42.  * This interface is not finished!  Still have to implement pixel
  43.  * reading functions and double buffering.  Then, look into accelerated
  44.  * line and polygon rendering.
  45.  */
  46.  
  47.  
  48. #ifdef SVGA
  49.  
  50.  
  51. #include <stdio.h>
  52. #include <vga.h>
  53. #include "GL/svgamesa.h"
  54. #include "context.h"
  55. #include "dd.h"
  56. #include "xform.h"
  57.  
  58.  
  59.  
  60. struct svgamesa_context {
  61.    struct gl_context *glctx;    /* the core Mesa context */
  62.    GLuint index;        /* current color index */
  63.    GLint red, green, blue;    /* current rgb color */
  64.    GLint width, height;        /* size of color buffer */
  65.    GLint depth;            /* bits per pixel (8,16,24 or 32) */
  66. };
  67.  
  68.  
  69. static SVGAMesaContext SVGAMesa = NULL;    /* the current context */
  70.  
  71.  
  72.  
  73. /*
  74.  * Convert Mesa window Y coordinate to VGA screen Y coordinate:
  75.  */
  76. #define FLIP(Y)  (SVGAMesa->height-(Y)-1)
  77.  
  78.  
  79.  
  80. /**********************************************************************/
  81. /*****                 Miscellaneous functions                    *****/
  82. /**********************************************************************/
  83.  
  84.  
  85. static void buffer_size( GLuint *width, GLuint *height, GLuint *depth )
  86. {
  87.    int colors;
  88.    *width = SVGAMesa->width = vga_getxdim();
  89.    *height = SVGAMesa->height = vga_getydim();
  90.    colors = vga_getcolors();
  91.    if (colors==256)  *depth = 8;
  92.    else if (colors=32768)  *depth = 16;
  93.    else  *depth = 24;
  94. }
  95.  
  96.  
  97. /* Set current color index */
  98. static void set_index( GLuint index )
  99. {
  100.    SVGAMesa->index = index;
  101.    vga_setcolor( index );
  102. }
  103.  
  104.  
  105. /* Set current drawing color */
  106. static void set_color( GLubyte red, GLubyte green,
  107.                        GLubyte blue, GLubyte alpha )
  108. {
  109.    SVGAMesa->red = red;
  110.    SVGAMesa->green = green;
  111.    SVGAMesa->blue = blue;
  112.    vga_setrgbcolor( red, green, blue );
  113. }
  114.  
  115.  
  116. static void clear_index( GLuint index )
  117. {
  118.    /* TODO: Implements glClearIndex() */
  119. }
  120.  
  121.  
  122. static void clear_color( GLubyte red, GLubyte green,
  123.                          GLubyte blue, GLubyte alpha )
  124. {
  125.    /* TODO: Implements glClearColor() */
  126. }
  127.  
  128.  
  129. static void clear( GLboolean all,
  130.                    GLint x, GLint y, GLint width, GLint height )
  131. {
  132.    vga_clear();
  133. }
  134.  
  135.  
  136. static GLboolean set_buffer( GLenum mode )
  137. {
  138.    /* TODO: implement double buffering and use this function to select */
  139.    /* between front and back buffers. */
  140.    return GL_TRUE;
  141. }
  142.  
  143.  
  144. static points_func choose_points_function( void )
  145. {
  146.    /* No accelerated point drawing functions */
  147.    return NULL;
  148. }
  149.  
  150.  
  151. static line_func choose_line_function( void )
  152. {
  153.    /* No accelerated line drawing functions */
  154.    return NULL;
  155. }
  156.  
  157.  
  158. static polygon_func choose_polygon_function( void )
  159. {
  160.    /* No accelerated polygon drawing functions */
  161.    return NULL;
  162. }
  163.  
  164.  
  165.  
  166. /**********************************************************************/
  167. /*****            Write spans of pixels                           *****/
  168. /**********************************************************************/
  169.  
  170.  
  171. static void write_index_span( GLuint n, GLint x, GLint y,
  172.                               const GLuint index[],
  173.                               const GLubyte mask[] )
  174. {
  175.    int i;
  176.    y = FLIP(y);
  177.    for (i=0;i<n;i++,x++) {
  178.       if (mask[i]) {
  179.          vga_setcolor( index[i] );
  180.          vga_drawpixel( x, y );
  181.       }
  182.    }
  183. }
  184.  
  185.  
  186.  
  187. static void write_monoindex_span(GLuint n,GLint x,GLint y,const GLubyte mask[])
  188. {
  189.    int i;
  190.    y = FLIP(y);
  191.    /* use current color index */
  192.    vga_setcolor( SVGAMesa->index );
  193.    for (i=0;i<n;i++,x++) {
  194.       if (mask[i]) {
  195.          vga_drawpixel( x, y );
  196.       }
  197.    }
  198. }
  199.  
  200.  
  201.  
  202. static void write_color_span( GLuint n, GLint x, GLint y,
  203.                               const GLubyte red[], const GLubyte green[],
  204.                               const GLubyte blue[], const GLubyte alpha[],
  205.                               const GLubyte mask[] )
  206. {
  207.    int i;
  208.    y=FLIP(y);
  209.    if (mask) {
  210.       /* draw some pixels */
  211.       for (i=0; i<n; i++, x++) {
  212.          if (mask[i]) {
  213.             vga_setrgbcolor( red[i], green[i], blue[i] );
  214.             vga_drawpixel( x, y );
  215.          }
  216.       }
  217.    }
  218.    else {
  219.       /* draw all pixels */
  220.       for (i=0; i<n; i++, x++) {
  221.          vga_setrgbcolor( red[i], green[i], blue[i] );
  222.          vga_drawpixel( x, y );
  223.       }
  224.    }
  225. }
  226.  
  227.  
  228.  
  229. static void write_monocolor_span( GLuint n, GLint x, GLint y,
  230.                                   const GLubyte mask[])
  231. {
  232.    int i;
  233.    y=FLIP(y);
  234.    /* use current rgb color */
  235.    vga_setrgbcolor( SVGAMesa->red, SVGAMesa->green, SVGAMesa->blue );
  236.    for (i=0; i<n; i++, x++) {
  237.       if (mask[i]) {
  238.          vga_drawpixel( x, y );
  239.       }
  240.    }
  241. }
  242.  
  243.  
  244.  
  245. /**********************************************************************/
  246. /*****                 Read spans of pixels                       *****/
  247. /**********************************************************************/
  248.  
  249.  
  250. static void read_index_span( GLuint n, GLint x, GLint y, GLuint index[])
  251. {
  252.    int i;
  253.    y = FLIP(y);
  254.    for (i=0; i<n; i++,x++) {
  255.       index[i] = vga_getpixel( x, y );
  256.    }
  257. }
  258.  
  259.  
  260.  
  261. static void read_color_span( GLuint n, GLint x, GLint y,
  262.                              GLubyte red[], GLubyte green[],
  263.                              GLubyte blue[], GLubyte alpha[] )
  264. {
  265.    int i;
  266.    for (i=0; i<n; i++, x++) {
  267.       /* TODO */
  268.    }
  269. }
  270.  
  271.  
  272.  
  273. /**********************************************************************/
  274. /*****                  Write arrays of pixels                    *****/
  275. /**********************************************************************/
  276.  
  277.  
  278. static void write_index_pixels( GLuint n, const GLint x[], const GLint y[],
  279.                                 const GLuint index[], const GLubyte mask[] )
  280. {
  281.    int i;
  282.    for (i=0; i<n; i++) {
  283.       if (mask[i]) {
  284.          vga_setcolor( index[i] );
  285.          vga_drawpixel( x[i], FLIP(y[i]) );
  286.       }
  287.    }
  288. }
  289.  
  290.  
  291.  
  292. static void write_monoindex_pixels( GLuint n,
  293.                                     const GLint x[], const GLint y[],
  294.                                     const GLubyte mask[] )
  295. {
  296.    int i;
  297.    /* use current color index */
  298.    vga_setcolor( SVGAMesa->index );
  299.    for (i=0; i<n; i++) {
  300.       if (mask[i]) {
  301.          vga_drawpixel( x[i], FLIP(y[i]) );
  302.       }
  303.    }
  304. }
  305.  
  306.  
  307.  
  308. static void write_color_pixels( GLuint n, const GLint x[], const GLint y[],
  309.                                 const GLubyte r[], const GLubyte g[],
  310.                                 const GLubyte b[], const GLubyte a[],
  311.                                 const GLubyte mask[] )
  312. {
  313.    int i;
  314.    for (i=0; i<n; i++) {
  315.       if (mask[i]) {
  316.          vga_setrgbcolor( r[i], g[i], b[i] );
  317.          vga_drawpixel( x[i], FLIP(y[i]) );
  318.       }
  319.    }
  320. }
  321.  
  322.  
  323.  
  324. static void write_monocolor_pixels( GLuint n,
  325.                                     const GLint x[], const GLint y[],
  326.                                     const GLubyte mask[] )
  327. {
  328.    int i;
  329.    /* use current rgb color */
  330.    vga_setrgbcolor( SVGAMesa->red, SVGAMesa->green, SVGAMesa->blue );
  331.    for (i=0; i<n; i++) {
  332.       if (mask[i]) {
  333.          vga_drawpixel( x[i], FLIP(y[i]) );
  334.       }
  335.    }
  336. }
  337.  
  338.  
  339.  
  340.  
  341. /**********************************************************************/
  342. /*****                   Read arrays of pixels                    *****/
  343. /**********************************************************************/
  344.  
  345. /* Read an array of color index pixels. */
  346. static void read_index_pixels( GLuint n, const GLint x[], const GLint y[],
  347.                                GLuint index[], const GLubyte mask[] )
  348. {
  349.    int i;
  350.    for (i=0; i<n; i++,x++) {
  351.       index[i] = vga_getpixel( x[i], FLIP(y[i]) );
  352.    }
  353. }
  354.  
  355.  
  356.  
  357. static void read_color_pixels( GLuint n, const GLint x[], const GLint y[],
  358.                                GLubyte red[], GLubyte green[],
  359.                                GLubyte blue[], GLubyte alpha[],
  360.                                const GLubyte mask[] )
  361. {
  362.    /* TODO */
  363. }
  364.  
  365.  
  366.  
  367. static void svgamesa_setup_DD_pointers( void )
  368. {
  369.    /* Initialize all the pointers in the DD struct.  Do this whenever */
  370.    /* a new context is made current or we change buffers via set_buffer! */
  371.  
  372.    DD.update_state = svgamesa_setup_DD_pointers;
  373.  
  374.    DD.clear_index = clear_index;
  375.    DD.clear_color = clear_color;
  376.    DD.clear = clear;
  377.  
  378.    DD.index = set_index;
  379.    DD.color = set_color;
  380.  
  381.    DD.set_buffer = set_buffer;
  382.    DD.buffer_size = buffer_size;
  383.  
  384.    DD.get_points_func = choose_points_function;
  385.    DD.get_line_func = choose_line_function;
  386.    DD.get_polygon_func = choose_polygon_function;
  387.  
  388.    /* Pixel/span writing functions: */
  389.    /* TODO: use different funcs for 8, 16, 32-bit depths */
  390.    DD.write_color_span       = write_color_span;
  391.    DD.write_monocolor_span   = write_monocolor_span;
  392.    DD.write_color_pixels     = write_color_pixels;
  393.    DD.write_monocolor_pixels = write_monocolor_pixels;
  394.    DD.write_index_span       = write_index_span;
  395.    DD.write_monoindex_span   = write_monoindex_span;
  396.    DD.write_index_pixels     = write_index_pixels;
  397.    DD.write_monoindex_pixels = write_monoindex_pixels;
  398.  
  399.    /* Pixel/span reading functions: */
  400.    /* TODO: use different funcs for 8, 16, 32-bit depths */
  401.    DD.read_index_span = read_index_span;
  402.    DD.read_color_span = read_color_span;
  403.    DD.read_index_pixels = read_index_pixels;
  404.    DD.read_color_pixels = read_color_pixels;
  405. }
  406.  
  407.  
  408.  
  409. /*
  410.  * Create a new VGA/Mesa context and return a handle to it.
  411.  */
  412. SVGAMesaContext SVGAMesaCreateContext( void )
  413. {
  414.    SVGAMesaContext ctx;
  415.    GLboolean rgb_flag;
  416.    GLfloat redscale, greenscale, bluescale, alphascale;
  417.    GLboolean db_flag;
  418.    int colors;
  419.  
  420.    /* determine if we're in RGB or color index mode */
  421.    colors = vga_getcolors();
  422.    if (colors==32768) {
  423.       rgb_flag = GL_TRUE;
  424.       redscale = greenscale = bluescale = alphascale = 255.0;
  425.    }
  426.    else {
  427.       rgb_flag = GL_FALSE;
  428.       redscale = greenscale = bluescale = alphascale = 0.0;
  429.    }
  430.    db_flag = GL_FALSE;
  431.  
  432.    ctx = (SVGAMesaContext) malloc( sizeof(struct svgamesa_context) );
  433.  
  434.    ctx->glctx = gl_new_context( rgb_flag,
  435.                                 redscale, greenscale, bluescale, alphascale,
  436.                                 db_flag, NULL );
  437.  
  438.    ctx->index = 1;
  439.    ctx->red = ctx->green = ctx->blue = 255;
  440.  
  441.    ctx->width = ctx->height = 0;  /* temporary until first "make-current" */
  442.  
  443.    return ctx;
  444. }
  445.  
  446.  
  447.  
  448.  
  449. /*
  450.  * Destroy the given VGA/Mesa context.
  451.  */
  452. void SVGAMesaDestroyContext( SVGAMesaContext ctx )
  453. {
  454.    if (ctx) {
  455.       gl_destroy_context( ctx->glctx );
  456.       free( ctx );
  457.       if (ctx==SVGAMesa) {
  458.          SVGAMesa = NULL;
  459.       }
  460.    }
  461. }
  462.  
  463.  
  464.  
  465. /*
  466.  * Make the specified VGA/Mesa context the current one.
  467.  */
  468. void SVGAMesaMakeCurrent( SVGAMesaContext ctx )
  469. {
  470.    SVGAMesa = ctx;
  471.    gl_set_context( ctx->glctx );
  472.    svgamesa_setup_DD_pointers();
  473.  
  474.    if (ctx->width==0 || ctx->height==0) {
  475.       /* setup initial viewport */
  476.       ctx->width = vga_getxdim();
  477.       ctx->height = vga_getydim();
  478.       gl_viewport( 0, 0, ctx->width, ctx->height );
  479.    }
  480. }
  481.  
  482.  
  483.  
  484. /*
  485.  * Return a handle to the current VGA/Mesa context.
  486.  */
  487. SVGAMesaContext SVGAMesaGetCurrentContext( void )
  488. {
  489.    return SVGAMesa;
  490. }
  491.  
  492.  
  493. #else
  494.  
  495. /*
  496.  * Need this to provide at least one external definition.
  497.  */
  498.  
  499. int gl_svga_dummy_function(void)
  500. {
  501.    return 0;
  502. }
  503.  
  504. #endif  /*SVGA*/
  505.  
  506.