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

  1. /* $Id: feedback.c,v 1.11 1997/11/07 03:38:07 brianp Exp $ */
  2.  
  3. /*
  4.  * Mesa 3-D graphics library
  5.  * Version:  2.4
  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: feedback.c,v $
  26.  * Revision 1.11  1997/11/07 03:38:07  brianp
  27.  * added stdio.h include for SunOS 4.x
  28.  *
  29.  * Revision 1.10  1997/07/24 01:25:01  brianp
  30.  * changed precompiled header symbol from PCH to PC_HEADER
  31.  *
  32.  * Revision 1.9  1997/05/28 03:24:54  brianp
  33.  * added precompiled header (PCH) support
  34.  *
  35.  * Revision 1.8  1997/03/08 02:03:46  brianp
  36.  * better error checking
  37.  *
  38.  * Revision 1.7  1997/01/21 03:09:15  brianp
  39.  * fixed an error message in gl_RenderMode()
  40.  *
  41.  * Revision 1.6  1997/01/16 19:10:24  brianp
  42.  * kludged around a SunOS 5.x/GCC bug in write_hit_record()
  43.  *
  44.  * Revision 1.5  1996/12/13 21:03:19  brianp
  45.  * fixed feedback buffer overflow bug in write_hit_record()
  46.  *
  47.  * Revision 1.4  1996/10/22 22:58:25  brianp
  48.  * fixed bug in gl_update_hitflag() reported by Erich Eder
  49.  *
  50.  * Revision 1.3  1996/09/27 01:28:13  brianp
  51.  * added missing error check to gl_RenderMode()
  52.  *
  53.  * Revision 1.2  1996/09/15 14:17:30  brianp
  54.  * now use GLframebuffer and GLvisual
  55.  *
  56.  * Revision 1.1  1996/09/13 01:38:16  brianp
  57.  * Initial revision
  58.  *
  59.  */
  60.  
  61.  
  62. #ifdef PC_HEADER
  63. #include "all.h"
  64. #else
  65. #include <assert.h>
  66. #include <stdio.h>
  67. #include "context.h"
  68. #include "feedback.h"
  69. #include "dlist.h"
  70. #include "macros.h"
  71. #include "types.h"
  72. #endif
  73.  
  74.  
  75.  
  76. #define FB_3D           0x01
  77. #define FB_4D           0x02
  78. #define FB_INDEX        0x04
  79. #define FB_COLOR        0x08
  80. #define FB_TEXTURE      0X10
  81.  
  82. #define BOOL_TO_FLOAT(X)        ( (GLfloat)(GLint) (X) )
  83. #define BOOL_TO_DOUBLE(X)        ( (GLdouble)(GLint) (X) )
  84.  
  85.  
  86. void
  87. gl_FeedbackBuffer( GLcontext *ctx, GLsizei size, GLenum type, GLfloat *buffer )
  88. {
  89.    if (ctx->RenderMode==GL_FEEDBACK || INSIDE_BEGIN_END(ctx)) {
  90.       gl_error( ctx, GL_INVALID_OPERATION, "glFeedbackBuffer" );
  91.       return;
  92.    }
  93.  
  94.    if (size<0) {
  95.       gl_error( ctx, GL_INVALID_VALUE, "glFeedbackBuffer(size<0)" );
  96.       return;
  97.    }
  98.    if (!buffer) {
  99.       gl_error( ctx, GL_INVALID_VALUE, "glFeedbackBuffer(buffer==NULL)" );
  100.       ctx->Feedback.BufferSize = 0;
  101.       return;
  102.    }
  103.  
  104.    switch (type) {
  105.       case GL_2D:
  106.      ctx->Feedback.Mask = 0;
  107.      ctx->Feedback.Type = type;
  108.      break;
  109.       case GL_3D:
  110.      ctx->Feedback.Mask = FB_3D;
  111.      ctx->Feedback.Type = type;
  112.      break;
  113.       case GL_3D_COLOR:
  114.      ctx->Feedback.Mask = FB_3D
  115.                | (ctx->Visual->RGBAflag ? FB_COLOR : FB_INDEX);
  116.      ctx->Feedback.Type = type;
  117.      break;
  118.       case GL_3D_COLOR_TEXTURE:
  119.      ctx->Feedback.Mask = FB_3D
  120.                | (ctx->Visual->RGBAflag ? FB_COLOR : FB_INDEX)
  121.                | FB_TEXTURE;
  122.      ctx->Feedback.Type = type;
  123.      break;
  124.       case GL_4D_COLOR_TEXTURE:
  125.      ctx->Feedback.Mask = FB_3D | FB_4D
  126.                | (ctx->Visual->RGBAflag ? FB_COLOR : FB_INDEX)
  127.                | FB_TEXTURE;
  128.      ctx->Feedback.Type = type;
  129.      break;
  130.       default:
  131.      ctx->Feedback.Mask = 0;
  132.      gl_error( ctx, GL_INVALID_ENUM, "glFeedbackBuffer" );
  133.    }
  134.  
  135.    ctx->Feedback.BufferSize = size;
  136.    ctx->Feedback.Buffer = buffer;
  137.    ctx->Feedback.Count = 0;
  138. }
  139.  
  140.  
  141.  
  142. void gl_PassThrough( GLcontext *ctx, GLfloat token )
  143. {
  144.    if (INSIDE_BEGIN_END(ctx)) {
  145.       gl_error( ctx, GL_INVALID_OPERATION, "glPassThrough" );
  146.       return;
  147.    }
  148.  
  149.    if (ctx->RenderMode==GL_FEEDBACK) {
  150.       FEEDBACK_TOKEN( ctx, BOOL_TO_FLOAT(GL_PASS_THROUGH_TOKEN) );
  151.       FEEDBACK_TOKEN( ctx, token );
  152.    }
  153. }
  154.  
  155.  
  156.  
  157. /*
  158.  * Put a vertex into the feedback buffer.
  159.  */
  160. void gl_feedback_vertex( GLcontext *ctx,
  161.              GLfloat x, GLfloat y, GLfloat z, GLfloat w,
  162.              const GLfloat color[4], GLfloat index,
  163.              const GLfloat texcoord[4] )
  164. {
  165.    FEEDBACK_TOKEN( ctx, x );
  166.    FEEDBACK_TOKEN( ctx, y );
  167.    if (ctx->Feedback.Mask & FB_3D) {
  168.       FEEDBACK_TOKEN( ctx, z );
  169.    }
  170.    if (ctx->Feedback.Mask & FB_4D) {
  171.       FEEDBACK_TOKEN( ctx, w );
  172.    }
  173.    if (ctx->Feedback.Mask & FB_INDEX) {
  174.       FEEDBACK_TOKEN( ctx, index );
  175.    }
  176.    if (ctx->Feedback.Mask & FB_COLOR) {
  177.       FEEDBACK_TOKEN( ctx, color[0] );
  178.       FEEDBACK_TOKEN( ctx, color[1] );
  179.       FEEDBACK_TOKEN( ctx, color[2] );
  180.       FEEDBACK_TOKEN( ctx, color[3] );
  181.    }
  182.    if (ctx->Feedback.Mask & FB_TEXTURE) {
  183.       FEEDBACK_TOKEN( ctx, texcoord[0] );
  184.       FEEDBACK_TOKEN( ctx, texcoord[1] );
  185.       FEEDBACK_TOKEN( ctx, texcoord[2] );
  186.       FEEDBACK_TOKEN( ctx, texcoord[3] );
  187.    }
  188. }
  189.  
  190.  
  191.  
  192. /**********************************************************************/
  193. /*                              Selection                             */
  194. /**********************************************************************/
  195.  
  196.  
  197. /*
  198.  * NOTE: this function can't be put in a display list.
  199.  */
  200. void gl_SelectBuffer( GLcontext *ctx, GLsizei size, GLuint *buffer )
  201. {
  202.    if (INSIDE_BEGIN_END(ctx)) {
  203.       gl_error( ctx, GL_INVALID_OPERATION, "glSelectBuffer" );
  204.    }
  205.    if (ctx->RenderMode==GL_SELECT) {
  206.       gl_error( ctx, GL_INVALID_OPERATION, "glSelectBuffer" );
  207.    }
  208.    ctx->Select.Buffer = buffer;
  209.    ctx->Select.BufferSize = size;
  210.    ctx->Select.BufferCount = 0;
  211.  
  212.    ctx->Select.HitFlag = GL_FALSE;
  213.    ctx->Select.HitMinZ = 1.0;
  214.    ctx->Select.HitMaxZ = 0.0;
  215. }
  216.  
  217.  
  218. void gl_InitNames( GLcontext *ctx )
  219. {
  220.    if (INSIDE_BEGIN_END(ctx)) {
  221.       gl_error( ctx, GL_INVALID_OPERATION, "glInitNames" );
  222.    }
  223.    ctx->Select.NameStackDepth = 0;
  224.    ctx->Select.HitFlag = GL_FALSE;
  225.    ctx->Select.HitMinZ = 1.0;
  226.    ctx->Select.HitMaxZ = 0.0;
  227. }
  228.  
  229.  
  230.  
  231. #define WRITE_RECORD( CTX, V )                                  \
  232.     if (CTX->Select.BufferCount < CTX->Select.BufferSize) { \
  233.        CTX->Select.Buffer[CTX->Select.BufferCount] = (V);           \
  234.     }                                                       \
  235.     CTX->Select.BufferCount++;
  236.  
  237.  
  238.  
  239. void gl_update_hitflag( GLcontext *ctx, GLfloat z )
  240. {
  241.    ctx->Select.HitFlag = GL_TRUE;
  242.    if (z < ctx->Select.HitMinZ) {
  243.       ctx->Select.HitMinZ = z;
  244.    }
  245.    if (z > ctx->Select.HitMaxZ) {
  246.       ctx->Select.HitMaxZ = z;
  247.    }
  248. }
  249.  
  250.  
  251.  
  252. static void write_hit_record( GLcontext *ctx )
  253. {
  254.    GLuint i;
  255.    GLuint zmin, zmax, zscale = 0x7fffffff;
  256.  
  257.    /* HitMinZ and HitMaxZ are in [0,1].  Multiply these values by */
  258.    /* 2^32-1 and round to nearest unsigned integer. */
  259.    assert( ctx != NULL ); /* this line magically fixes a SunOS 5.x/gcc bug */
  260.    zmin = 2 * (GLuint) ((GLfloat) zscale * ctx->Select.HitMinZ);
  261.    zmax = 2 * (GLuint) ((GLfloat) zscale * ctx->Select.HitMaxZ);
  262.  
  263.    WRITE_RECORD( ctx, ctx->Select.NameStackDepth );
  264.    WRITE_RECORD( ctx, zmin );
  265.    WRITE_RECORD( ctx, zmax );
  266.    for (i=0;i<ctx->Select.NameStackDepth;i++) {
  267.       WRITE_RECORD( ctx, ctx->Select.NameStack[i] );
  268.    }
  269.  
  270.    ctx->Select.Hits++;
  271.    ctx->Select.HitFlag = GL_FALSE;
  272.    ctx->Select.HitMinZ = 1.0;
  273.    ctx->Select.HitMaxZ = -1.0;
  274. }
  275.  
  276.  
  277.  
  278. void gl_LoadName( GLcontext *ctx, GLuint name )
  279. {
  280.    if (INSIDE_BEGIN_END(ctx)) {
  281.       gl_error( ctx, GL_INVALID_OPERATION, "glLoadName" );
  282.       return;
  283.    }
  284.    if (ctx->RenderMode!=GL_SELECT) {
  285.       return;
  286.    }
  287.    if (ctx->Select.NameStackDepth==0) {
  288.       gl_error( ctx, GL_INVALID_OPERATION, "glLoadName" );
  289.       return;
  290.    }
  291.    if (ctx->Select.HitFlag) {
  292.       write_hit_record( ctx );
  293.    }
  294.    if (ctx->Select.NameStackDepth<MAX_NAME_STACK_DEPTH) {
  295.       ctx->Select.NameStack[ctx->Select.NameStackDepth-1] = name;
  296.    }
  297.    else {
  298.       ctx->Select.NameStack[MAX_NAME_STACK_DEPTH-1] = name;
  299.    }
  300. }
  301.  
  302.  
  303. void gl_PushName( GLcontext *ctx, GLuint name )
  304. {
  305.    if (INSIDE_BEGIN_END(ctx)) {
  306.       gl_error( ctx, GL_INVALID_OPERATION, "glPushName" );
  307.       return;
  308.    }
  309.    if (ctx->RenderMode!=GL_SELECT) {
  310.       return;
  311.    }
  312.    if (ctx->Select.HitFlag) {
  313.       write_hit_record( ctx );
  314.    }
  315.    if (ctx->Select.NameStackDepth<MAX_NAME_STACK_DEPTH) {
  316.       ctx->Select.NameStack[ctx->Select.NameStackDepth++] = name;
  317.    }
  318.    else {
  319.       gl_error( ctx, GL_STACK_OVERFLOW, "glPushName" );
  320.    }
  321. }
  322.  
  323.  
  324.  
  325. void gl_PopName( GLcontext *ctx )
  326. {
  327.    if (INSIDE_BEGIN_END(ctx)) {
  328.       gl_error( ctx, GL_INVALID_OPERATION, "glPopName" );
  329.       return;
  330.    }
  331.    if (ctx->RenderMode!=GL_SELECT) {
  332.       return;
  333.    }
  334.    if (ctx->Select.HitFlag) {
  335.       write_hit_record( ctx );
  336.    }
  337.    if (ctx->Select.NameStackDepth>0) {
  338.       ctx->Select.NameStackDepth--;
  339.    }
  340.    else {
  341.       gl_error( ctx, GL_STACK_UNDERFLOW, "glPopName" );
  342.    }
  343. }
  344.  
  345.  
  346.  
  347. /**********************************************************************/
  348. /*                           Render Mode                              */
  349. /**********************************************************************/
  350.  
  351.  
  352.  
  353. /*
  354.  * NOTE: this function can't be put in a display list.
  355.  */
  356. GLint gl_RenderMode( GLcontext *ctx, GLenum mode )
  357. {
  358.    GLint result;
  359.  
  360.    if (INSIDE_BEGIN_END(ctx)) {
  361.       gl_error( ctx, GL_INVALID_OPERATION, "glRenderMode" );
  362.    }
  363.  
  364.    switch (ctx->RenderMode) {
  365.       case GL_RENDER:
  366.      result = 0;
  367.      break;
  368.       case GL_SELECT:
  369.      if (ctx->Select.HitFlag) {
  370.         write_hit_record( ctx );
  371.      }
  372.      if (ctx->Select.BufferCount > ctx->Select.BufferSize) {
  373.         /* overflow */
  374. #ifdef DEBUG
  375.         gl_warning(ctx, "Feedback buffer overflow");
  376. #endif
  377.         result = -1;
  378.      }
  379.      else {
  380.         result = ctx->Select.Hits;
  381.      }
  382.      ctx->Select.BufferCount = 0;
  383.      ctx->Select.Hits = 0;
  384.      ctx->Select.NameStackDepth = 0;
  385.      break;
  386.       case GL_FEEDBACK:
  387.      if (ctx->Feedback.Count > ctx->Feedback.BufferSize) {
  388.         /* overflow */
  389.         result = -1;
  390.      }
  391.      else {
  392.         result = ctx->Feedback.Count;
  393.      }
  394.      ctx->Feedback.Count = 0;
  395.      break;
  396.       default:
  397.      gl_error( ctx, GL_INVALID_ENUM, "glRenderMode" );
  398.      return 0;
  399.    }
  400.  
  401.    switch (mode) {
  402.       case GL_RENDER:
  403.      break;
  404.       case GL_SELECT:
  405.      if (ctx->Select.BufferSize==0) {
  406.         /* haven't called glSelectBuffer yet */
  407.         gl_error( ctx, GL_INVALID_OPERATION, "glRenderMode" );
  408.      }
  409.      break;
  410.       case GL_FEEDBACK:
  411.      if (ctx->Feedback.BufferSize==0) {
  412.         /* haven't called glFeedbackBuffer yet */
  413.         gl_error( ctx, GL_INVALID_OPERATION, "glRenderMode" );
  414.      }
  415.      break;
  416.       default:
  417.      gl_error( ctx, GL_INVALID_ENUM, "glRenderMode" );
  418.      return 0;
  419.    }
  420.  
  421.    ctx->NewState |= NEW_ALL;
  422.    ctx->RenderMode = mode;
  423.  
  424.    return result;
  425. }
  426.  
  427.