home *** CD-ROM | disk | FTP | other *** search
/ Magazyn Amiga 5 / MA_Cover_5.iso / ppc / mesa / src / bitmap.c < prev    next >
Encoding:
C/C++ Source or Header  |  1998-01-31  |  6.0 KB  |  221 lines

  1. /* $Id: bitmap.c,v 1.8 1997/10/02 03:06:42 brianp Exp $ */
  2.  
  3. /*
  4.  * Mesa 3-D graphics library
  5.  * Version:  2.5
  6.  * Copyright (C) 1995-1997  Brian Paul
  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: bitmap.c,v $
  26.  * Revision 1.8  1997/10/02 03:06:42  brianp
  27.  * added #include <assert.h>
  28.  *
  29.  * Revision 1.7  1997/09/27 00:15:39  brianp
  30.  * changed parameters to gl_unpack_image()
  31.  *
  32.  * Revision 1.6  1997/07/24 01:24:45  brianp
  33.  * changed precompiled header symbol from PCH to PC_HEADER
  34.  *
  35.  * Revision 1.5  1997/06/20 02:18:09  brianp
  36.  * replaced Current.IntColor with Current.ByteColor
  37.  *
  38.  * Revision 1.4  1997/05/28 03:23:48  brianp
  39.  * added precompiled header (PCH) support
  40.  *
  41.  * Revision 1.3  1996/11/06 04:23:18  brianp
  42.  * replaced 0 with GL_COLOR_INDEX in gl_unpack_bitmap()
  43.  *
  44.  * Revision 1.2  1996/09/15 14:18:10  brianp
  45.  * now use GLframebuffer and GLvisual
  46.  *
  47.  * Revision 1.1  1996/09/13 01:38:16  brianp
  48.  * Initial revision
  49.  *
  50.  */
  51.  
  52.  
  53. #ifdef PC_HEADER
  54. #include "all.h"
  55. #else
  56. #include <assert.h>
  57. #include <stdlib.h>
  58. #include <string.h>
  59. #include "bitmap.h"
  60. #include "context.h"
  61. #include "feedback.h"
  62. #include "image.h"
  63. #include "macros.h"
  64. #include "pb.h"
  65. #include "types.h"
  66. #endif
  67.  
  68. #define BOOL_TO_FLOAT(X)        ( (GLfloat)(GLint) (X) )
  69. #define BOOL_TO_DOUBLE(X)        ( (GLdouble)(GLint) (X) )
  70.  
  71.  
  72. /*
  73.  * Unpack a bitmap image
  74.  */
  75. struct gl_image *gl_unpack_bitmap( GLcontext* ctx,
  76.                    GLsizei width, GLsizei height,
  77.                    const GLubyte *bitmap )
  78. {
  79.    return gl_unpack_image( ctx, width, height,
  80.                GL_COLOR_INDEX, GL_BITMAP, bitmap );
  81. }
  82.  
  83.  
  84.  
  85.  
  86. /*
  87.  * Do actual rendering of a bitmap.
  88.  */
  89. void gl_render_bitmap( GLcontext* ctx,
  90.                GLsizei width, GLsizei height,
  91.                GLfloat xorig, GLfloat yorig,
  92.                GLfloat xmove, GLfloat ymove,
  93.                const struct gl_image *bitmap )
  94. {
  95.    struct pixel_buffer *PB = ctx->PB;
  96.    GLint bx, by;      /* bitmap position */
  97.    GLint px, py, pz;  /* pixel position */
  98.    GLubyte *ptr;
  99.  
  100.    assert(bitmap);
  101.    assert(bitmap->Type == GL_BITMAP);
  102.    assert(bitmap->Format == GL_COLOR_INDEX);
  103.  
  104.    if (ctx->NewState) {
  105.       gl_update_state(ctx);
  106.       PB_INIT( PB, GL_BITMAP );
  107.    }
  108.  
  109.    if (ctx->Visual->RGBAflag) {
  110.       GLint r, g, b, a;
  111.       r = (GLint) (ctx->Current.RasterColor[0] * ctx->Visual->RedScale);
  112.       g = (GLint) (ctx->Current.RasterColor[1] * ctx->Visual->GreenScale);
  113.       b = (GLint) (ctx->Current.RasterColor[2] * ctx->Visual->BlueScale);
  114.       a = (GLint) (ctx->Current.RasterColor[3] * ctx->Visual->AlphaScale);
  115.       PB_SET_COLOR( ctx, PB, r, g, b, a );
  116.    }
  117.    else {
  118.       PB_SET_INDEX( ctx, PB, ctx->Current.RasterIndex );
  119.    }
  120.  
  121.    px = (GLint) ( (ctx->Current.RasterPos[0] - xorig) + 0.0F );
  122.    py = (GLint) ( (ctx->Current.RasterPos[1] - yorig) + 0.0F );
  123.    pz = (GLint) ( ctx->Current.RasterPos[2] * DEPTH_SCALE );
  124.    ptr = (GLubyte *) bitmap->Data;
  125.  
  126.    for (by=0;by<height;by++) {
  127.       GLubyte bitmask;
  128.  
  129.       /* do a row */
  130.       bitmask = 128;
  131.       for (bx=0;bx<width;bx++) {
  132.      if (*ptr&bitmask) {
  133.         PB_WRITE_PIXEL( PB, px+bx, py+by, pz );
  134.      }
  135.      bitmask = bitmask >> 1;
  136.      if (bitmask==0) {
  137.         ptr++;
  138.         bitmask = 128;
  139.      }
  140.       }
  141.  
  142.       PB_CHECK_FLUSH( ctx, PB )
  143.  
  144.       /* get ready for next row */
  145.       if (bitmask!=128)  ptr++;
  146.    }
  147.  
  148.    gl_flush_pb(ctx);
  149. }
  150.  
  151.  
  152.  
  153.  
  154. /*
  155.  * Execute a glBitmap command:
  156.  *   1. check for errors
  157.  *   2. feedback/render/select
  158.  *   3. advance raster position
  159.  */
  160. void gl_Bitmap( GLcontext* ctx,
  161.         GLsizei width, GLsizei height,
  162.         GLfloat xorig, GLfloat yorig,
  163.         GLfloat xmove, GLfloat ymove,
  164.         const struct gl_image *bitmap )
  165. {
  166.    if (width<0 || height<0) {
  167.       gl_error( ctx, GL_INVALID_VALUE, "glBitmap" );
  168.       return;
  169.    }
  170.    if (INSIDE_BEGIN_END(ctx)) {
  171.       gl_error( ctx, GL_INVALID_OPERATION, "glBitmap" );
  172.       return;
  173.    }
  174.    if (ctx->Current.RasterPosValid==GL_FALSE) {
  175.       /* do nothing */
  176.       return;
  177.    }
  178.  
  179.    if (ctx->RenderMode==GL_RENDER) {
  180.       GLboolean completed = GL_FALSE;
  181.       if (ctx->Driver.Bitmap) {
  182.      /* let device driver try to render the bitmap */
  183.      completed = (*ctx->Driver.Bitmap)( ctx, width, height, xorig, yorig,
  184.                         xmove, ymove, bitmap );
  185.       }
  186.       if (!completed) {
  187.      /* use generic function */
  188.      gl_render_bitmap( ctx, width, height, xorig, yorig,
  189.                xmove, ymove, bitmap );
  190.       }
  191.    }
  192.    else if (ctx->RenderMode==GL_FEEDBACK) {
  193.       GLfloat color[4], texcoord[4], invq;
  194.       color[0] = ctx->Current.ByteColor[0] * ctx->Visual->InvRedScale;
  195.       color[1] = ctx->Current.ByteColor[1] * ctx->Visual->InvGreenScale;
  196.       color[2] = ctx->Current.ByteColor[2] * ctx->Visual->InvBlueScale;
  197.       color[3] = ctx->Current.ByteColor[3] * ctx->Visual->InvAlphaScale;
  198.       invq = 1.0F / ctx->Current.TexCoord[3];
  199.       texcoord[0] = ctx->Current.TexCoord[0] * invq;
  200.       texcoord[1] = ctx->Current.TexCoord[1] * invq;
  201.       texcoord[2] = ctx->Current.TexCoord[2] * invq;
  202.       texcoord[3] = ctx->Current.TexCoord[3];
  203.       FEEDBACK_TOKEN( ctx, BOOL_TO_FLOAT(GL_BITMAP_TOKEN) );
  204.       /* TODO: Verify XYZW values are correct: */
  205.       gl_feedback_vertex( ctx, ctx->Current.RasterPos[0] - xorig,
  206.               ctx->Current.RasterPos[1] - yorig,
  207.               ctx->Current.RasterPos[2],
  208.               ctx->Current.RasterPos[3],
  209.               color, ctx->Current.Index, texcoord );
  210.    }
  211.    else if (ctx->RenderMode==GL_SELECT) {
  212.       /* Bitmaps don't generate selection hits.  See appendix B of 1.1 spec. */
  213.    }
  214.  
  215.    /* update raster position */
  216.    ctx->Current.RasterPos[0] += xmove;
  217.    ctx->Current.RasterPos[1] += ymove;
  218. }
  219.  
  220.  
  221.