home *** CD-ROM | disk | FTP | other *** search
/ Learn 3D Graphics Programming on the PC / Learn_3D_Graphics_Programming_on_the_PC_Ferraro.iso / rwwin / booksub.c_ / booksub.bin
Text File  |  1995-11-14  |  12KB  |  409 lines

  1. /**********************************************************************
  2.  *
  3.  * File :     booksub.c
  4.  *
  5.  * Abstract : functions associated with rfView that deal generally with the book. 
  6.  *
  7.  *            This application had been written to be compatible with
  8.  *            both the fixed and floating-point versions of the
  9.  *            RenderWare library, i.e., it uses the macros CREAL,
  10.  *            INT2REAL, RAdd, RDiv, RSub etc. If your application is
  11.  *            intended for the floating-point version of the library
  12.  *            only these macros are not necessary.
  13.  *
  14.  *            Please note that this application is intended for
  15.  *            demonstration purposes only. No support will be
  16.  *            provided for this code and it comes with no warranty.
  17.  *
  18.  **********************************************************************
  19.  *
  20.  * This file is a product of Richard F. Ferraro
  21.  *
  22.  * This file is provided as is with no warranties of any kind and is
  23.  * provided without any obligation on Richard F. Ferraro. or Criterion Software or
  24.  * Canon Inc. to assist in its use or modification.
  25.  *
  26.  * Richard F. Ferraro or Criterion Software Ltd. will not, under any
  27.  * circumstances, be liable for any lost revenue or other damages arising
  28.  * from the use of this file.
  29.  *
  30.  * Copyright (c) 1995 Richard F. Ferraro
  31.  * All Rights Reserved.
  32.  *
  33.  * RenderWare is a trademark of Canon Inc.
  34.  *
  35.  **********************************************************************/
  36.  
  37.                     
  38. #include <windows.h>
  39.  
  40. #include <stdlib.h>
  41. #include <stdio.h>
  42. #include <math.h>
  43. #include <string.h>
  44.  
  45. #include <rwlib.h>
  46. #include <rwwin.h>
  47.  
  48. #include "rfview.h"
  49.  
  50. // ******************************************************* bird's eye sprite *********************************************
  51. /*
  52.  * Build the sprite which will show the bird's eye view.
  53.  */
  54. RwClump *
  55. CreateBirdsEyeSprite(void)
  56. {
  57.     RwRaster *raster;
  58.     
  59.     /*
  60.      * We create the raster and texture into which we will copy the
  61.      * bird's eye camera's rendering. This is fairly complex as we
  62.      * want to modify the texture the sprite shows. If it were not
  63.      * for this we could simply create a sprite as follows:
  64.      * RwCreateSprite(RwGetNamedTexture("mandel"));
  65.      */
  66.     raster = RwCreateRaster(128, 128);
  67.     if (raster == NULL)
  68.     {
  69.         return NULL;
  70.     }
  71.     BirdsEyeTexture = RwCreateTexture(raster);
  72.     if (BirdsEyeTexture == NULL)
  73.     {
  74.         RwDestroyRaster(raster);
  75.         return NULL;
  76.     }
  77.     
  78.     /*
  79.      * Create the actual sprite using this texture.
  80.      */
  81.     BirdsEyeSprite = RwCreateSprite(BirdsEyeTexture);
  82.     if (BirdsEyeSprite == NULL)
  83.     {
  84.         RwDestroyTexture(BirdsEyeTexture);
  85.         BirdsEyeTexture = NULL;
  86.         return NULL;
  87.     }
  88.     
  89.     return BirdsEyeSprite;
  90. }
  91.  
  92. /*
  93.  * Destroy the palette sprite.
  94.  */
  95. void
  96. DestroyBirdsEyeSprite(void)
  97. {
  98.     if (BirdsEyeSprite != NULL)
  99.     {
  100.         RwDestroyClump(BirdsEyeSprite);
  101.         BirdsEyeSprite = NULL;
  102.         RwDestroyTexture(BirdsEyeTexture); 
  103.         BirdsEyeTexture = NULL;
  104.     }
  105. }
  106.  
  107.  
  108. // ****************************************** determine current color mode *************************************************
  109. /* are we in 8 or 16-bit mode */
  110. int
  111. Is8or16bpp(HWND window)
  112. {
  113.     HDC  dc;
  114.     int  depth;
  115.  
  116.     dc = GetDC(window);
  117.     depth = GetDeviceCaps(dc, BITSPIXEL);
  118.     ReleaseDC(window, dc);
  119.     return(depth);
  120. }
  121.  
  122. // ***************************************** Palette Viewer Sprite ***********************************
  123. /*
  124.  * Destroy the palette sprite.
  125.  */
  126. void
  127. DestroyPaletteSprite(void)
  128. {
  129.     if (PaletteSprite != NULL)
  130.     {
  131.         RwDestroyClump(PaletteSprite);
  132.         PaletteSprite = NULL;
  133.         RwDestroyTexture(PaletteTexture); 
  134.         PaletteTexture = NULL;
  135.     }
  136. }
  137.  
  138. /*
  139.  * Build the sprite which will show the palette.  It is 128 by 128 with 16 by 16 boxes.  Each box contains a number
  140.    incrementing from 0 to 255 left-to-right and top-down.
  141.  */
  142. RwClump *
  143. CreatePaletteSprite(void)
  144. {
  145.     RwRaster *raster;
  146.     unsigned char *ptrRaster, *address;    
  147.     RwInt32 boxwidth,boxheight,width,height,columnOffset,i,j;
  148.     unsigned char color;
  149.     ClumpUserData *userData;
  150.     /* Allocate the user data for the new clump. */
  151.     userData = (ClumpUserData *)malloc(sizeof(ClumpUserData));
  152.     if (userData == NULL) return NULL;
  153.     
  154.     /*
  155.      * We create the raster and texture into which we will build a palette bitmap.
  156.      */
  157.     raster = RwCreateRaster(128, 128);
  158.     if (raster == NULL)
  159.     {
  160.         return NULL;
  161.     }
  162.     
  163.     height = 128;  boxheight = height/16;      // only want 16 different boxes across
  164.     width  = 128;  boxwidth = width/16;        // only want 16 different boxes down
  165.     ptrRaster = RwGetRasterPixels(raster);    
  166.  
  167.     for (j=0; j<height; j++) {
  168.       columnOffset = (j * width);
  169.       for (i=0; i<width; i++) {
  170.          color = (unsigned char)((j/boxheight)*16 + i/boxwidth);
  171.          address = ptrRaster + columnOffset + i;
  172.          *address = color;
  173.          }
  174.       }
  175.     RwReleaseRasterPixels(raster,ptrRaster);    
  176.     
  177.     PaletteTexture = RwCreateTexture(raster);
  178.     if (PaletteTexture == NULL)
  179.     {
  180.         RwDestroyRaster(raster);
  181.         return NULL;
  182.     }
  183.     
  184.     /*
  185.      * Create the actual sprite using this texture.
  186.      */
  187.     PaletteSprite = RwCreateSprite(PaletteTexture);
  188.     if (PaletteSprite == NULL)
  189.     {
  190.         RwDestroyTexture(PaletteTexture);
  191.         PaletteTexture = NULL;
  192.         return NULL;
  193.     }
  194.     userData->light = NULL;
  195.     userData->datatype = rfIsPaletteSprite;
  196.     RwSetClumpData(PaletteSprite, (void *)userData);    
  197.     return PaletteSprite;
  198. }
  199.  
  200. // ************************************************ modify a raster ************************************************
  201. /* A rectangle that is 1/2 the heightand 1/2 the width of a raster is written in the center of the raster. */
  202.  
  203. // Modify a texture's raster of a tagged polygon when in an 8-bit per pixel mode.  
  204. BOOL rfModifyTextureRaster8(RwClump *SelectedClump)
  205. {
  206.  unsigned char *ptrRaster,*address;
  207.  RwTexture *modTexture;
  208.  RwRaster *modRaster;
  209.  RwInt32 stride,width,height,columnOffset,i,j;
  210.  RwPolygon3d *modPolygon;
  211.  
  212.   modPolygon = RwFindTaggedPolygon(SelectedClump,MODTEXTURE);
  213.   if(modPolygon!=NULL) {
  214.     modTexture = RwGetPolygonTexture(modPolygon);
  215.     if(modTexture!=NULL) {
  216.       modRaster = RwGetTextureRaster(modTexture);
  217.       if(modRaster!=NULL) {
  218.         height = RwGetRasterHeight(modRaster);
  219.         width  = RwGetRasterWidth(modRaster);
  220.         stride = RwGetRasterStride(modRaster);
  221.    
  222.         ptrRaster = RwGetRasterPixels(modRaster);
  223.         for (j=height/4; j<3*height/4; j++) {
  224.           columnOffset = (j * stride);
  225.            for (i=width/4; i<3*width/4; i++) {
  226.              address = ptrRaster + columnOffset + i;
  227.              *address = (unsigned char) MODCOLOR;
  228.              }
  229.            }
  230.         RwReleaseRasterPixels(modRaster,ptrRaster);
  231.         return TRUE;
  232.         }
  233.       }                   
  234.     }     
  235.   return FALSE;
  236. }
  237.  
  238. // Modify a texture's raster of a tagged polygon when in an 8-bit per pixel mode.  
  239. BOOL rfModifyTextureRaster16(RwClump *SelectedClump)
  240. {
  241.  unsigned short *ptrRaster,*address;
  242.  RwTexture *modTexture;
  243.  RwRaster *modRaster;
  244.  RwInt32 columnOffset,stride,width,height,i,j;
  245.  RwPolygon3d *modPolygon;
  246.  
  247.   modPolygon = RwFindTaggedPolygon(SelectedClump,MODTEXTURE);
  248.   if(modPolygon!=NULL) {
  249.     modTexture = RwGetPolygonTexture(modPolygon);
  250.     if(modTexture!=NULL) {
  251.       modRaster = RwGetTextureRaster(modTexture);
  252.       if(modRaster!=NULL) {
  253.         height = RwGetRasterHeight(modRaster);
  254.         width  = RwGetRasterWidth(modRaster);
  255.         stride = RwGetRasterStride(modRaster)/2;  // want it to be # of words not bytes
  256.    
  257.         ptrRaster =  (unsigned short *)RwGetRasterPixels(modRaster);
  258.         for (j=height/4; j<3*height/4; j++) {
  259.           columnOffset = (j * stride);
  260.            for (i=width/4; i<3*width/4; i++) {
  261.              address = ptrRaster + columnOffset + i;
  262.              *address = (unsigned int) MODCOLOR;
  263.              }
  264.            }
  265.         RwReleaseRasterPixels(modRaster,(unsigned char *)ptrRaster);
  266.         return TRUE;
  267.         }
  268.       }
  269.     }     
  270.   return FALSE;
  271. }
  272.  
  273. // Modify the camera's backdrop raster when in an 8-bit per pixel mode. 
  274. BOOL rfModifyBackdropRaster8(RwCamera *modCamera)
  275. {
  276.  unsigned char *ptrRaster,*address;
  277.  RwRaster *modRaster;
  278.  RwInt32 stride,width,height,columnOffset,i,j;
  279.  
  280.     modRaster = RwGetCameraBackdrop(modCamera);
  281.     if(modRaster!=NULL) {
  282.       height = RwGetRasterHeight(modRaster);
  283.       width  = RwGetRasterWidth(modRaster);
  284.       stride = RwGetRasterStride(modRaster);
  285.   
  286.       ptrRaster = RwGetRasterPixels(modRaster);
  287.     for (j=height/4; j<3*height/4; j++) {
  288.         columnOffset = (j * stride);
  289.         for (i=width/4; i<3*(width/4); i++) {
  290.              address = ptrRaster + columnOffset + i;
  291.              *address = (unsigned char) MODCOLOR;
  292.              }
  293.            }
  294.       RwReleaseRasterPixels(modRaster,ptrRaster);
  295.       return TRUE;
  296.  
  297.    }     
  298.   return FALSE;
  299. }
  300.  
  301. // Modify the camera's backdrop raster when in a 16-bit per pixel mode. 
  302. BOOL rfModifyBackdropRaster16(RwCamera *modCamera)
  303. {
  304.  unsigned short *ptrRaster,*address;
  305.  RwRaster *modRaster;
  306.  RwInt32 stride,width,height,columnOffset,i,j;
  307.  
  308.     modRaster = RwGetCameraBackdrop(modCamera);
  309.     if(modRaster!=NULL) {
  310.       height = RwGetRasterHeight(modRaster);
  311.       width  = RwGetRasterWidth(modRaster);
  312.       stride = RwGetRasterStride(modRaster)/2;  // want it to be # of words not bytes
  313.    
  314.       ptrRaster = (unsigned short *)RwGetRasterPixels(modRaster);
  315.         for (j=height/4; j<3*height/4; j++) {
  316.           columnOffset = (j * stride);
  317.            for (i=width/4; i<3*width/4; i++) {
  318.              address = ptrRaster + columnOffset + i;
  319.              *address = (unsigned int) MODCOLOR;
  320.              }
  321.            }
  322.       RwReleaseRasterPixels(modRaster,(unsigned char *)ptrRaster);
  323.       return TRUE;
  324.  
  325.    }     
  326.   return FALSE;
  327. }
  328.  
  329. // ************************************************* modify texture animation speed *******************************************
  330. // change the texture animation frame rate from 1 to 10
  331. BOOL rfChangeAnimateSpeed(RwClump *SelectedClump,int speedUp)
  332. {
  333.  RwTexture *modTexture;
  334.  RwPolygon3d *modPolygon;
  335.  RwInt32 speed;
  336.  
  337.   modPolygon = RwFindTaggedPolygon(SelectedClump,MODTEXTURE);
  338.   if(modPolygon!=NULL) {
  339.     modTexture = RwGetPolygonTexture(modPolygon);
  340.     if(modTexture!=NULL) {
  341.       speed = RwGetTextureFrameStep(modTexture);
  342.       if(speedUp>=0) // speed up
  343.         {
  344.         if(speed < 10) // maximum speed = 10
  345.           {
  346.           speed ++;
  347.           RwSetTextureFrameStep(modTexture,speed);
  348.           }
  349.         }
  350.       else // slow down
  351.         {
  352.         if(speed > 1)    // minimum speed = 1
  353.           {
  354.           speed --;
  355.           RwSetTextureFrameStep(modTexture,speed);
  356.           }
  357.         }  
  358.       return TRUE;  
  359.       }
  360.     }     
  361.   return FALSE;
  362. }
  363.  
  364.  
  365. // ************************************** view Window and Field of View Conversion **********************
  366. /* Convert from an angle and aspect ratio to a view window width and height */ 
  367. void rfAngleToViewwindow(void)
  368. {
  369.  double side,radAngle;
  370.  
  371.  radAngle = TwoPI * rfCam.angle / 360;
  372.  side = 2 * tan(radAngle/2);
  373.  
  374.  if(rfCam.aspect>1.0)   // if horizontal is greater than vertical
  375.   {
  376.    rfCam.vw_width = FL2REAL(side);
  377.    rfCam.vw_height = FL2REAL(side/rfCam.aspect);
  378.   }
  379.  else             // if vertical is greater than horizontal
  380.   {
  381.    rfCam.vw_height = FL2REAL(side);
  382.    rfCam.vw_width = FL2REAL(side*rfCam.aspect);
  383.   }
  384.    
  385.  }
  386.  
  387.  
  388. /* Convert from an view window width and height to a angle and aspect ratio */ 
  389. void rfViewwindowToAngle(void)
  390. {
  391.  double side,radAngle;
  392.  
  393.  rfCam.aspect = REAL2FL(RDiv( rfCam.vw_width, rfCam.vw_height));
  394.  
  395.  if(rfCam.aspect>1.0)   // if horizontal is greater than vertical
  396.   {
  397.    side = (double)REAL2FL(rfCam.vw_width);
  398.   }
  399.  else             // if vertical is greater than horizontal
  400.   {
  401.    side = (double)REAL2FL(rfCam.vw_height);
  402.   }
  403.  
  404.   radAngle = 2 * atan(side/2);
  405.   rfCam.angle = 360.0 * (radAngle / TwoPI);  
  406.   }
  407.  
  408.  
  409.