GraphicsBase.cpp
上传用户:lhwx1029
上传日期:2013-03-07
资源大小:1173k
文件大小:9k
源码类别:

3D图形编程

开发平台:

Visual C++

  1. /** 3DGPL *************************************************
  2.  * ()                                                     *
  3.  * 2D basic graphics.                                     *
  4.  *                                                        *
  5.  * Ifdefs:                                                *
  6.  *  _CI_                     Colour/Intensity model;      *
  7.  *  _RGB_                    RGB model;                   *
  8.  *  _Z_BUFFER_               Depth array;                 *
  9.  *  _PAINTER_                Back front order.            *
  10.  *                                                        *
  11.  * Defines:                                               *
  12.  *  G_init_graphics          Initializing graphics;       *
  13.  *  G_clear                  Clearing the bitmap;         *
  14.  *                                                        *
  15.  *  G_pixel                  Pixel into the colourmap;    *
  16.  *  G_dot                    Dot into the colourmap;      *
  17.  *  G_line                   Line into a colourmap.       *
  18.  *                                                        *
  19.  * (c) 1995-98 Sergei Savchenko, (savs@cs.mcgill.ca)      *
  20. **********************************************************/
  21. extern "C";
  22. #include "RayTracing.h"           /* hardware specific stuff */
  23. #include "Colour.h"               /* colour and light */
  24. #include "Clipper.h"             /* 2D clipping routines */
  25. #include "Graphics.h"           /* graphics functions */
  26. #include <stdlib.h>                         /* malloc */
  27. #include <limits.h>                         /* INT_MAX */
  28. HW_pixel *G_c_buffer;                       /* the bitmap's bits */
  29. int G_page_start;                           /* always 0 for _MONO_ */
  30. long G_c_buffer_size;                       /* allocated size for clearings */
  31. #if defined(_Z_BUFFER_)
  32. int *G_z_buffer;                            /* Z buffer */
  33. long G_z_buffer_size;                       /* it's size */
  34. #endif
  35. /**********************************************************
  36.  * Setting page for stereo images.                        *
  37.  *                                                        *
  38.  * SETS: G_page_start                                     *
  39.  * -----                                                  *
  40. **********************************************************/
  41. #if defined(_STEREO_)
  42. void G_page(int page_no)                    /* between two pages */
  43. {
  44.  if(page_no==G_LEFT_EYE) G_page_start=0;
  45.  else G_page_start=HW_SCREEN_X_SIZE;
  46. }
  47. #endif
  48. /**********************************************************
  49.  * Allocating space for the colourmap.                    *
  50.  *                                                        *
  51.  * RETURNS: Pointer to the allocated colourmap.           *
  52.  * --------                                               *
  53.  * SETS: G_c_buffer,G_c_buffer_size,G_z_buffer,           *
  54.  * ----- G_z_buffer_size                                  *
  55. **********************************************************/
  56. //为颜色表分配空间
  57. void G_init_graphics(void)
  58. {
  59. //空白空间的计算
  60.  G_c_buffer_size=HW_SCREEN_LINE_SIZE*HW_SCREEN_Y_SIZE;
  61. //HW_pixel==int
  62.  G_c_buffer=(HW_pixel*)malloc(G_c_buffer_size*sizeof(HW_pixel));
  63.  G_page_start=0;                            /* page 0 by default */
  64.  if(G_c_buffer==NULL) HW_error("(Graphics) Not enough memory.n");
  65. #if defined(_Z_BUFFER_)                     /* for both pages */
  66.  G_z_buffer_size=HW_SCREEN_LINE_SIZE*HW_SCREEN_Y_SIZE;
  67. //不同的就是int,HW_pixel
  68.  G_z_buffer=(int*)malloc(G_z_buffer_size*sizeof(int));
  69.  if(G_z_buffer==NULL) HW_error("(Graphics) Not enough memory.n");
  70. #endif
  71. }
  72. /**********************************************************
  73.  * Clearing the bitmap with the specified colour.         *
  74. **********************************************************/
  75. #if defined(_CI_)
  76. void G_clear(HW_pixel colour,int intensity)
  77. #endif
  78. #if defined(_RGB_)
  79. void G_clear(HW_pixel colour,int red,int green,int blue)
  80. #endif
  81. {
  82. #if defined(_CI_)
  83.  HW_set_pixel(G_c_buffer,G_c_buffer_size,CL_light(colour,intensity));
  84. #endif
  85. #if defined(_RGB_)
  86.  HW_set_pixel(G_c_buffer,G_c_buffer_size,CL_light(colour,red,green,blue));
  87. #endif
  88. #if defined(_Z_BUFFER_)
  89.  HW_set_int(G_z_buffer,G_z_buffer_size,INT_MAX);
  90. #endif
  91. }
  92. /**********************************************************
  93.  * Setting a pixel.                                       *
  94. **********************************************************/
  95. void G_pixel(int *vertex,HW_pixel colour)
  96. {
  97.  long pos;
  98.  pos=vertex[1]*HW_SCREEN_LINE_SIZE+vertex[0]+G_page_start;
  99.  G_c_buffer[pos]=colour;
  100. }
  101. /**********************************************************
  102.  * Rendering a dot.                                       *
  103. **********************************************************/
  104. #if defined(_CI_)
  105. void G_dot(int *vertex,HW_pixel colour,int intensity)
  106. #endif
  107. #if defined(_RGB_)
  108. void G_dot(int *vertex,HW_pixel colour,int red,int green,int blue)
  109. #endif
  110. {
  111.  long pos;
  112.  if( (vertex[0]>=0)&&(vertex[0]<HW_SCREEN_X_SIZE) &&
  113.      (vertex[1]>=0)&&(vertex[1]<HW_SCREEN_Y_SIZE) )
  114.  {
  115.   pos=vertex[1]*HW_SCREEN_LINE_SIZE+vertex[0]+G_page_start;
  116. #if defined(_Z_BUFFER_)
  117.   if(vertex[2]<G_z_buffer[pos])             /* doing Z check */
  118. #endif
  119. #if defined(_CI_)
  120.    G_c_buffer[pos]=CL_light(colour,intensity);
  121. #endif
  122. #if defined(_RGB_)
  123.    G_c_buffer[pos]=CL_light(colour,red,green,blue);
  124. #endif
  125.  }
  126. }
  127. /**********************************************************
  128.  * Rendering a line.                                      *
  129. **********************************************************/
  130. #if defined(_CI_)
  131. void G_line(int *vertex1,int *vertex2,
  132.             HW_pixel colour,int intensity
  133.    )
  134. #endif
  135. #if defined(_RGB_)
  136. void G_line(int *vertex1,int *vertex2,
  137.             HW_pixel colour,int red,int green,int blue
  138.            )
  139. #endif
  140. {
  141.  register int inc_ah,inc_al;
  142.  register int i;
  143.  int *v1,*v2,pos;
  144.  int dx,dy,long_d,short_d;
  145.  int d,add_dh,add_dl;
  146.  int inc_xh,inc_yh,inc_xl,inc_yl;
  147.  register HW_pixel *adr_c=G_c_buffer;
  148. #if defined(_Z_BUFFER_)
  149.  int *adr_z=G_z_buffer;
  150.  HW_32_bit cur_z,inc_z;
  151. #endif
  152. #if defined(_CI_)
  153.  HW_pixel hwcolour=CL_light(colour,intensity);
  154. #endif
  155. #if defined(_RGB_)
  156.  HW_pixel hwcolour=CL_light(colour,red,green,blue);
  157. #endif
  158.  v1=(int*)vertex1;
  159.  v2=(int*)vertex2;
  160.  if(C_line_x_clipping(&v1,&v2,2))           /* horizontal clipping */
  161.  {
  162.   if(C_line_y_clipping(&v1,&v2,2))          /* vertical clipping */
  163.   {
  164.    dx=v2[0]-v1[0]; dy=v2[1]-v1[1];          /* ranges */
  165.    if(dx<0){dx=-dx; inc_xh=-1; inc_xl=-1;}  /* making sure dx and dy >0 */
  166.    else    {        inc_xh=1;  inc_xl=1; }  /* adjusting increments */
  167.    if(dy<0){dy=-dy;inc_yh=-HW_SCREEN_LINE_SIZE;
  168.                    inc_yl=-HW_SCREEN_LINE_SIZE;
  169.            }                                /* to get to the neighboring */
  170.    else    {       inc_yh= HW_SCREEN_LINE_SIZE;
  171.                    inc_yl= HW_SCREEN_LINE_SIZE;
  172.            }
  173.    if(dx>dy){long_d=dx;short_d=dy;inc_yl=0;}/* long range,&make sure either */
  174.    else     {long_d=dy;short_d=dx;inc_xl=0;}/* x or y is changed in L case */
  175.    inc_ah=inc_xh+inc_yh;
  176.    inc_al=inc_xl+inc_yl;                    /* increments for point address */
  177.    d=2*short_d-long_d;                      /* initial value of d */
  178.    add_dl=2*short_d;                        /* d adjustment for H case */
  179.    add_dh=2*(short_d-long_d);               /* d adjustment for L case */
  180.    pos=v1[1]*HW_SCREEN_LINE_SIZE+v1[0]+G_page_start;
  181.    adr_c+=pos;                              /* address of first point */
  182. #if defined(_Z_BUFFER_)
  183.    adr_z+=pos;                              /* same for z-buffer */
  184.    cur_z=v1[2];
  185.    if(long_d!=0) inc_z=(v2[2]-cur_z)/long_d;
  186. #endif
  187.    for(i=0;i<=long_d;i++)                   /* for all points in long range */
  188.    {
  189. #if defined(_Z_BUFFER_)
  190.     if(cur_z<*adr_z)
  191. #endif
  192.      *adr_c=hwcolour;                       /* rendering */
  193.     if(d>=0)
  194.     {
  195.      d+=add_dh;
  196.      adr_c+=inc_ah;                         /* new in the colour-buffer */
  197. #if defined(_Z_BUFFER_)
  198.      adr_z+=inc_ah;                         /* new in Z-buffer */
  199. #endif
  200.     }                                       /* previous point was H type */
  201.     else
  202.     {
  203.      d+=add_dl;
  204.      adr_c+=inc_al;                         /* new in the colour-buffer */
  205. #if defined(_Z_BUFFER_)
  206.      adr_z+=inc_al;                         /* new in Z-buffer */
  207. #endif
  208.     }                                       /* previous point was L type */
  209. #if defined(_Z_BUFFER_)
  210.     cur_z+=inc_z;
  211. #endif
  212.    }
  213.   }
  214.  }
  215. }
  216. /**********************************************************/