home *** CD-ROM | disk | FTP | other *** search
- /**********************************************************************
- *
- * File : booksub.c
- *
- * Abstract : functions associated with rfView that deal generally with the book.
- *
- * This application had been written to be compatible with
- * both the fixed and floating-point versions of the
- * RenderWare library, i.e., it uses the macros CREAL,
- * INT2REAL, RAdd, RDiv, RSub etc. If your application is
- * intended for the floating-point version of the library
- * only these macros are not necessary.
- *
- * Please note that this application is intended for
- * demonstration purposes only. No support will be
- * provided for this code and it comes with no warranty.
- *
- **********************************************************************
- *
- * This file is a product of Richard F. Ferraro
- *
- * This file is provided as is with no warranties of any kind and is
- * provided without any obligation on Richard F. Ferraro. or Criterion Software or
- * Canon Inc. to assist in its use or modification.
- *
- * Richard F. Ferraro or Criterion Software Ltd. will not, under any
- * circumstances, be liable for any lost revenue or other damages arising
- * from the use of this file.
- *
- * Copyright (c) 1995 Richard F. Ferraro
- * All Rights Reserved.
- *
- * RenderWare is a trademark of Canon Inc.
- *
- **********************************************************************/
-
-
- #include <windows.h>
-
- #include <stdlib.h>
- #include <stdio.h>
- #include <math.h>
- #include <string.h>
-
- #include <rwlib.h>
- #include <rwwin.h>
-
- #include "rfview.h"
-
- // ******************************************************* bird's eye sprite *********************************************
- /*
- * Build the sprite which will show the bird's eye view.
- */
- RwClump *
- CreateBirdsEyeSprite(void)
- {
- RwRaster *raster;
-
- /*
- * We create the raster and texture into which we will copy the
- * bird's eye camera's rendering. This is fairly complex as we
- * want to modify the texture the sprite shows. If it were not
- * for this we could simply create a sprite as follows:
- * RwCreateSprite(RwGetNamedTexture("mandel"));
- */
- raster = RwCreateRaster(128, 128);
- if (raster == NULL)
- {
- return NULL;
- }
- BirdsEyeTexture = RwCreateTexture(raster);
- if (BirdsEyeTexture == NULL)
- {
- RwDestroyRaster(raster);
- return NULL;
- }
-
- /*
- * Create the actual sprite using this texture.
- */
- BirdsEyeSprite = RwCreateSprite(BirdsEyeTexture);
- if (BirdsEyeSprite == NULL)
- {
- RwDestroyTexture(BirdsEyeTexture);
- BirdsEyeTexture = NULL;
- return NULL;
- }
-
- return BirdsEyeSprite;
- }
-
- /*
- * Destroy the palette sprite.
- */
- void
- DestroyBirdsEyeSprite(void)
- {
- if (BirdsEyeSprite != NULL)
- {
- RwDestroyClump(BirdsEyeSprite);
- BirdsEyeSprite = NULL;
- RwDestroyTexture(BirdsEyeTexture);
- BirdsEyeTexture = NULL;
- }
- }
-
-
- // ****************************************** determine current color mode *************************************************
- /* are we in 8 or 16-bit mode */
- int
- Is8or16bpp(HWND window)
- {
- HDC dc;
- int depth;
-
- dc = GetDC(window);
- depth = GetDeviceCaps(dc, BITSPIXEL);
- ReleaseDC(window, dc);
- return(depth);
- }
-
- // ***************************************** Palette Viewer Sprite ***********************************
- /*
- * Destroy the palette sprite.
- */
- void
- DestroyPaletteSprite(void)
- {
- if (PaletteSprite != NULL)
- {
- RwDestroyClump(PaletteSprite);
- PaletteSprite = NULL;
- RwDestroyTexture(PaletteTexture);
- PaletteTexture = NULL;
- }
- }
-
- /*
- * Build the sprite which will show the palette. It is 128 by 128 with 16 by 16 boxes. Each box contains a number
- incrementing from 0 to 255 left-to-right and top-down.
- */
- RwClump *
- CreatePaletteSprite(void)
- {
- RwRaster *raster;
- unsigned char *ptrRaster, *address;
- RwInt32 boxwidth,boxheight,width,height,columnOffset,i,j;
- unsigned char color;
- ClumpUserData *userData;
- /* Allocate the user data for the new clump. */
- userData = (ClumpUserData *)malloc(sizeof(ClumpUserData));
- if (userData == NULL) return NULL;
-
- /*
- * We create the raster and texture into which we will build a palette bitmap.
- */
- raster = RwCreateRaster(128, 128);
- if (raster == NULL)
- {
- return NULL;
- }
-
- height = 128; boxheight = height/16; // only want 16 different boxes across
- width = 128; boxwidth = width/16; // only want 16 different boxes down
- ptrRaster = RwGetRasterPixels(raster);
-
- for (j=0; j<height; j++) {
- columnOffset = (j * width);
- for (i=0; i<width; i++) {
- color = (unsigned char)((j/boxheight)*16 + i/boxwidth);
- address = ptrRaster + columnOffset + i;
- *address = color;
- }
- }
- RwReleaseRasterPixels(raster,ptrRaster);
-
- PaletteTexture = RwCreateTexture(raster);
- if (PaletteTexture == NULL)
- {
- RwDestroyRaster(raster);
- return NULL;
- }
-
- /*
- * Create the actual sprite using this texture.
- */
- PaletteSprite = RwCreateSprite(PaletteTexture);
- if (PaletteSprite == NULL)
- {
- RwDestroyTexture(PaletteTexture);
- PaletteTexture = NULL;
- return NULL;
- }
- userData->light = NULL;
- userData->datatype = rfIsPaletteSprite;
- RwSetClumpData(PaletteSprite, (void *)userData);
- return PaletteSprite;
- }
-
- // ************************************************ modify a raster ************************************************
- /* A rectangle that is 1/2 the heightand 1/2 the width of a raster is written in the center of the raster. */
-
- // Modify a texture's raster of a tagged polygon when in an 8-bit per pixel mode.
- BOOL rfModifyTextureRaster8(RwClump *SelectedClump)
- {
- unsigned char *ptrRaster,*address;
- RwTexture *modTexture;
- RwRaster *modRaster;
- RwInt32 stride,width,height,columnOffset,i,j;
- RwPolygon3d *modPolygon;
-
- modPolygon = RwFindTaggedPolygon(SelectedClump,MODTEXTURE);
- if(modPolygon!=NULL) {
- modTexture = RwGetPolygonTexture(modPolygon);
- if(modTexture!=NULL) {
- modRaster = RwGetTextureRaster(modTexture);
- if(modRaster!=NULL) {
- height = RwGetRasterHeight(modRaster);
- width = RwGetRasterWidth(modRaster);
- stride = RwGetRasterStride(modRaster);
-
- ptrRaster = RwGetRasterPixels(modRaster);
- for (j=height/4; j<3*height/4; j++) {
- columnOffset = (j * stride);
- for (i=width/4; i<3*width/4; i++) {
- address = ptrRaster + columnOffset + i;
- *address = (unsigned char) MODCOLOR;
- }
- }
- RwReleaseRasterPixels(modRaster,ptrRaster);
- return TRUE;
- }
- }
- }
- return FALSE;
- }
-
- // Modify a texture's raster of a tagged polygon when in an 8-bit per pixel mode.
- BOOL rfModifyTextureRaster16(RwClump *SelectedClump)
- {
- unsigned short *ptrRaster,*address;
- RwTexture *modTexture;
- RwRaster *modRaster;
- RwInt32 columnOffset,stride,width,height,i,j;
- RwPolygon3d *modPolygon;
-
- modPolygon = RwFindTaggedPolygon(SelectedClump,MODTEXTURE);
- if(modPolygon!=NULL) {
- modTexture = RwGetPolygonTexture(modPolygon);
- if(modTexture!=NULL) {
- modRaster = RwGetTextureRaster(modTexture);
- if(modRaster!=NULL) {
- height = RwGetRasterHeight(modRaster);
- width = RwGetRasterWidth(modRaster);
- stride = RwGetRasterStride(modRaster)/2; // want it to be # of words not bytes
-
- ptrRaster = (unsigned short *)RwGetRasterPixels(modRaster);
- for (j=height/4; j<3*height/4; j++) {
- columnOffset = (j * stride);
- for (i=width/4; i<3*width/4; i++) {
- address = ptrRaster + columnOffset + i;
- *address = (unsigned int) MODCOLOR;
- }
- }
- RwReleaseRasterPixels(modRaster,(unsigned char *)ptrRaster);
- return TRUE;
- }
- }
- }
- return FALSE;
- }
-
- // Modify the camera's backdrop raster when in an 8-bit per pixel mode.
- BOOL rfModifyBackdropRaster8(RwCamera *modCamera)
- {
- unsigned char *ptrRaster,*address;
- RwRaster *modRaster;
- RwInt32 stride,width,height,columnOffset,i,j;
-
- modRaster = RwGetCameraBackdrop(modCamera);
- if(modRaster!=NULL) {
- height = RwGetRasterHeight(modRaster);
- width = RwGetRasterWidth(modRaster);
- stride = RwGetRasterStride(modRaster);
-
- ptrRaster = RwGetRasterPixels(modRaster);
- for (j=height/4; j<3*height/4; j++) {
- columnOffset = (j * stride);
- for (i=width/4; i<3*(width/4); i++) {
- address = ptrRaster + columnOffset + i;
- *address = (unsigned char) MODCOLOR;
- }
- }
- RwReleaseRasterPixels(modRaster,ptrRaster);
- return TRUE;
-
- }
- return FALSE;
- }
-
- // Modify the camera's backdrop raster when in a 16-bit per pixel mode.
- BOOL rfModifyBackdropRaster16(RwCamera *modCamera)
- {
- unsigned short *ptrRaster,*address;
- RwRaster *modRaster;
- RwInt32 stride,width,height,columnOffset,i,j;
-
- modRaster = RwGetCameraBackdrop(modCamera);
- if(modRaster!=NULL) {
- height = RwGetRasterHeight(modRaster);
- width = RwGetRasterWidth(modRaster);
- stride = RwGetRasterStride(modRaster)/2; // want it to be # of words not bytes
-
- ptrRaster = (unsigned short *)RwGetRasterPixels(modRaster);
- for (j=height/4; j<3*height/4; j++) {
- columnOffset = (j * stride);
- for (i=width/4; i<3*width/4; i++) {
- address = ptrRaster + columnOffset + i;
- *address = (unsigned int) MODCOLOR;
- }
- }
- RwReleaseRasterPixels(modRaster,(unsigned char *)ptrRaster);
- return TRUE;
-
- }
- return FALSE;
- }
-
- // ************************************************* modify texture animation speed *******************************************
- // change the texture animation frame rate from 1 to 10
- BOOL rfChangeAnimateSpeed(RwClump *SelectedClump,int speedUp)
- {
- RwTexture *modTexture;
- RwPolygon3d *modPolygon;
- RwInt32 speed;
-
- modPolygon = RwFindTaggedPolygon(SelectedClump,MODTEXTURE);
- if(modPolygon!=NULL) {
- modTexture = RwGetPolygonTexture(modPolygon);
- if(modTexture!=NULL) {
- speed = RwGetTextureFrameStep(modTexture);
- if(speedUp>=0) // speed up
- {
- if(speed < 10) // maximum speed = 10
- {
- speed ++;
- RwSetTextureFrameStep(modTexture,speed);
- }
- }
- else // slow down
- {
- if(speed > 1) // minimum speed = 1
- {
- speed --;
- RwSetTextureFrameStep(modTexture,speed);
- }
- }
- return TRUE;
- }
- }
- return FALSE;
- }
-
-
- // ************************************** view Window and Field of View Conversion **********************
- /* Convert from an angle and aspect ratio to a view window width and height */
- void rfAngleToViewwindow(void)
- {
- double side,radAngle;
-
- radAngle = TwoPI * rfCam.angle / 360;
- side = 2 * tan(radAngle/2);
-
- if(rfCam.aspect>1.0) // if horizontal is greater than vertical
- {
- rfCam.vw_width = FL2REAL(side);
- rfCam.vw_height = FL2REAL(side/rfCam.aspect);
- }
- else // if vertical is greater than horizontal
- {
- rfCam.vw_height = FL2REAL(side);
- rfCam.vw_width = FL2REAL(side*rfCam.aspect);
- }
-
- }
-
-
- /* Convert from an view window width and height to a angle and aspect ratio */
- void rfViewwindowToAngle(void)
- {
- double side,radAngle;
-
- rfCam.aspect = REAL2FL(RDiv( rfCam.vw_width, rfCam.vw_height));
-
- if(rfCam.aspect>1.0) // if horizontal is greater than vertical
- {
- side = (double)REAL2FL(rfCam.vw_width);
- }
- else // if vertical is greater than horizontal
- {
- side = (double)REAL2FL(rfCam.vw_height);
- }
-
- radAngle = 2 * atan(side/2);
- rfCam.angle = 360.0 * (radAngle / TwoPI);
- }
-
-
-