home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Professional / OS2PRO194.ISO / os2 / graphic / dkb / source / pm.c < prev    next >
C/C++ Source or Header  |  1992-08-04  |  9KB  |  296 lines

  1. /*
  2.  
  3. DKB Raytracer for OS/2 2.0 Presentation Manager
  4. by Michael Caldwell (mcaldwel@netcom.com)
  5.  
  6. */
  7.  
  8.  
  9. #define INCL_PM
  10. #include <os2.h>
  11. #include <time.h>
  12. #include "stats.h"
  13.  
  14.  
  15. extern long Number_Of_Pixels, Number_Of_Rays, Number_Of_Pixels_Supersampled;
  16. extern long Ray_Sphere_Tests, Ray_Sphere_Tests_Succeeded;
  17. extern long Ray_Plane_Tests, Ray_Plane_Tests_Succeeded;
  18. extern long Ray_Triangle_Tests, Ray_Triangle_Tests_Succeeded;
  19. extern long Ray_Quadric_Tests, Ray_Quadric_Tests_Succeeded;
  20. extern long Ray_Quartic_Tests, Ray_Quartic_Tests_Succeeded;
  21. extern long Bounding_Region_Tests, Bounding_Region_Tests_Succeeded;
  22. extern long Calls_To_Noise, Calls_To_DNoise;
  23. extern long Shadow_Ray_Tests, Shadow_Rays_Succeeded;
  24. extern long Reflected_Rays_Traced, Refracted_Rays_Traced;
  25. extern long Transmitted_Rays_Traced;
  26. extern time_t Start_Time;
  27. extern int Screen_Width;
  28. extern int Screen_Height;
  29. extern HAB hab;
  30. extern HWND hwndMain;
  31. extern PSZ pszImage;
  32.  
  33.  
  34. HDC hdc = (HDC) NULL;
  35. HPS hps = (HPS) NULL;
  36. HWND hwndStats = (HWND) NULL;
  37. HWND hwndImage = (HWND) NULL;
  38. LONG yLast;
  39.  
  40.  
  41. static HWND hwndFrame = (HWND) NULL;
  42.  
  43.  
  44. MRESULT EXPENTRY StatsWndProc (HWND, ULONG, MPARAM, MPARAM);
  45. VOID PrintToTranscript (PSZ pszFormat, ...);
  46.  
  47.  
  48. VOID SetStatisticItem (SHORT sID, LONG lValue) {
  49.     PSZ pszValue [32];
  50.  
  51.     sprintf (pszValue, "%ld", lValue);
  52.     WinSetDlgItemText (hwndStats, sID, (PSZ) pszValue);
  53. }
  54.  
  55.  
  56. VOID UpdateStats (SHORT y) {
  57.     PSZ pszElapsedTime [32];
  58.     time_t tElapsed;
  59.     struct tm * ptmElapsed;
  60.  
  61.     if (! hwndStats) {
  62.         hwndStats = WinLoadDlg (HWND_DESKTOP, (HWND) NULL, StatsWndProc,
  63.                 (HMODULE) NULL, ID_STATS, NULL);
  64.     }
  65.     if (y == -2) {
  66.         WinShowWindow (hwndStats, TRUE);
  67.         WinSetDlgItemText (hwndStats, ID_LINE, "");
  68.         SetStatisticItem (ID_PIXELS, 0L);
  69.         SetStatisticItem (ID_RAYS, 0L);
  70.         SetStatisticItem (ID_SUPERPIXELS, 0L);
  71.         SetStatisticItem (ID_SPHERE_TESTS, 0L);
  72.         SetStatisticItem (ID_SPHERE_HITS, 0L);
  73.         SetStatisticItem (ID_PLANE_TESTS, 0L);
  74.         SetStatisticItem (ID_PLANE_HITS, 0L);
  75.         SetStatisticItem (ID_TRIANGLE_TESTS, 0L);
  76.         SetStatisticItem (ID_TRIANGLE_HITS, 0L);
  77.         SetStatisticItem (ID_QUADRIC_TESTS, 0L);
  78.         SetStatisticItem (ID_QUADRIC_HITS, 0L);
  79.         SetStatisticItem (ID_QUARTIC_TESTS, 0L);
  80.         SetStatisticItem (ID_QUARTIC_HITS, 0L);
  81.         SetStatisticItem (ID_BOUNDS_TESTS, 0L);
  82.         SetStatisticItem (ID_BOUNDS_HITS, 0L);
  83.         SetStatisticItem (ID_NOISE, 0L);
  84.         SetStatisticItem (ID_DNOISE, 0L);
  85.         SetStatisticItem (ID_SRAY_TESTS, 0L);
  86.         SetStatisticItem (ID_BOBJECTS, 0L);
  87.         SetStatisticItem (ID_REFLECTED_RAYS, 0L);
  88.         SetStatisticItem (ID_REFRACTED_RAYS, 0L);
  89.         SetStatisticItem (ID_TRANSMITTED_RAYS, 0L);
  90.         WinSetDlgItemText (hwndStats, ID_ELAPSED_TIME, "");
  91.         return;
  92.     }
  93.     SetStatisticItem (ID_LINE, y);
  94.     SetStatisticItem (ID_PIXELS, Number_Of_Pixels);
  95.     SetStatisticItem (ID_RAYS, Number_Of_Rays);
  96.     SetStatisticItem (ID_SUPERPIXELS, Number_Of_Pixels_Supersampled);
  97.     SetStatisticItem (ID_SPHERE_TESTS, Ray_Sphere_Tests);
  98.     SetStatisticItem (ID_SPHERE_HITS, Ray_Sphere_Tests_Succeeded);
  99.     SetStatisticItem (ID_PLANE_TESTS, Ray_Plane_Tests);
  100.     SetStatisticItem (ID_PLANE_HITS, Ray_Plane_Tests_Succeeded);
  101.     SetStatisticItem (ID_TRIANGLE_TESTS, Ray_Triangle_Tests);
  102.     SetStatisticItem (ID_TRIANGLE_HITS, Ray_Triangle_Tests_Succeeded);
  103.     SetStatisticItem (ID_QUADRIC_TESTS, Ray_Quadric_Tests);
  104.     SetStatisticItem (ID_QUADRIC_HITS, Ray_Quadric_Tests_Succeeded);
  105.     SetStatisticItem (ID_QUARTIC_TESTS, Ray_Quartic_Tests);
  106.     SetStatisticItem (ID_QUARTIC_HITS, Ray_Quartic_Tests_Succeeded);
  107.     SetStatisticItem (ID_BOUNDS_TESTS, Bounding_Region_Tests);
  108.     SetStatisticItem (ID_BOUNDS_HITS, Bounding_Region_Tests_Succeeded);
  109.     SetStatisticItem (ID_NOISE, Calls_To_Noise);
  110.     SetStatisticItem (ID_DNOISE, Calls_To_DNoise);
  111.     SetStatisticItem (ID_SRAY_TESTS, Shadow_Ray_Tests);
  112.     SetStatisticItem (ID_BOBJECTS, Shadow_Rays_Succeeded);
  113.     SetStatisticItem (ID_REFLECTED_RAYS, Reflected_Rays_Traced);
  114.     SetStatisticItem (ID_REFRACTED_RAYS, Refracted_Rays_Traced);
  115.     SetStatisticItem (ID_TRANSMITTED_RAYS, Transmitted_Rays_Traced);
  116.     tElapsed = time (NULL) - Start_Time;
  117.     ptmElapsed = gmtime (& tElapsed);
  118.     sprintf (pszElapsedTime, "%dd %02d:%02d:%02d", ptmElapsed->tm_yday,
  119.             ptmElapsed->tm_hour, ptmElapsed->tm_min, ptmElapsed->tm_sec);
  120.     WinSetDlgItemText (hwndStats, ID_ELAPSED_TIME, (PSZ) pszElapsedTime);
  121. }
  122.  
  123.  
  124. MRESULT EXPENTRY StatsWndProc (HWND hwnd, ULONG ulMessage, MPARAM mp1,
  125.         MPARAM mp2) {
  126.     switch (ulMessage) {
  127.         case WM_CLOSE: {
  128.         /* use dismiss dialog to just hide the window */
  129.             WinDismissDlg (hwnd, 0);
  130.             return (0L);
  131.         }
  132.     }/*endSwitch*/
  133.     return (WinDefDlgProc (hwnd, ulMessage, mp1, mp2));
  134. }/* end StatsWndProc */
  135.  
  136.  
  137. void display_init (int x, int y) {
  138.     BITMAPINFOHEADER2 bmih;
  139.     DEVOPENSTRUC dos [5] = {NULL, "DISPLAY", NULL, NULL, NULL};
  140.     HBITMAP hbm;
  141.     LONG alData [2];
  142.     SIZEL sizel;
  143.     ULONG fl;
  144.  
  145. /* destroy any previous display */
  146.     if (hps) {
  147.         hbm = GpiSetBitmap (hps, (HBITMAP) NULL);
  148.         if (hbm == BMB_ERROR) {
  149.             PrintToTranscript ("GpiSetBitmap returned an error.\n");
  150.         }
  151.         if (GpiDeleteBitmap (hbm) == GPI_ERROR) {
  152.             PrintToTranscript ("GpiDeleteBitmap failed.\n");
  153.         }
  154.     /* no need to do the associate
  155.         if (GpiAssociate (hps, (HDC) NULL) == GPI_ERROR) {
  156.             PrintToTranscript ("GpiAssociate failed.\n");
  157.         }
  158.     */
  159.         if (GpiDestroyPS (hps) == GPI_ERROR) {
  160.             PrintToTranscript ("GpiDestroyPS failed.\n");
  161.         }
  162.         if (DevCloseDC (hdc) == DEV_ERROR) {
  163.             PrintToTranscript ("DevCloseDC failed.\n");
  164.         }
  165.     }
  166.     hdc = DevOpenDC (hab, OD_MEMORY, "*", 5L, (PDEVOPENDATA) & dos, (HDC) NULL);
  167.     if (hdc == DEV_ERROR) {
  168.         PrintToTranscript ("DevOpenDC failed.\n");
  169.         return;
  170.     }
  171.     sizel.cx = x;
  172.     sizel.cy = y;
  173.     hps = GpiCreatePS (hab, hdc, & sizel, GPIA_ASSOC | GPIT_MICRO | PU_PELS);
  174.     if (hps == GPI_ERROR) {
  175.         PrintToTranscript ("GpiCreatePS failed.\n");
  176.         DevCloseDC (hdc);
  177.         return;
  178.     }
  179.     if (GpiQueryDeviceBitmapFormats (hps, 2L, alData) == GPI_ERROR) {
  180.         PrintToTranscript ("GpiQueryDeviceBitmapFormats failed.\n");
  181.         return;
  182.     }
  183.     if (x * y * alData [0] * alData [1] / 8 > 65535) {
  184.         PrintToTranscript ("Image bitmap cannot be > 64K.\n");
  185.         GpiDestroyPS (hps);
  186.         DevCloseDC (hdc);
  187.         hps = (HPS) NULL;
  188.         hdc = (HDC) NULL;
  189.         if (hwndFrame) {
  190.             WinSendMsg (hwndFrame, WM_CLOSE, 0L, 0L);
  191.         }
  192.         return;
  193.     }
  194.     memset (& bmih, 0, sizeof (BITMAPINFOHEADER2));
  195.     bmih.cbFix = sizeof (BITMAPINFOHEADER2);
  196.     bmih.cx = x;
  197.     bmih.cy = y;
  198.     bmih.cPlanes = alData [0];
  199.     bmih.cBitCount = alData [1];
  200.     hbm = GpiCreateBitmap (hps, & bmih, 0L, (PBYTE) NULL, (PBITMAPINFO2) NULL);
  201.     if (hbm == GPI_ERROR) {
  202.         PrintToTranscript ("GpiCreateBitmap failed.\n");
  203.         return;
  204.     }
  205.     if (GpiSetBitmap (hps, hbm) == BMB_ERROR) {
  206.         PrintToTranscript ("GpiSetBitmap failed.\n");
  207.         return;
  208.     }
  209.     if (GpiErase (hps) == GPI_ERROR) {
  210.         PrintToTranscript ("GpiErase failed.\n");
  211.         return;
  212.     }
  213.     if (GpiCreateLogColorTable (hps, 0L, LCOLF_RGB, 0L, 0L, (PLONG) NULL)
  214.             == GPI_ERROR) {
  215.         PrintToTranscript ("GpiCreateLogColorTable failed.\n");
  216.         return;
  217.     }
  218. /* create window */
  219.     x += WinQuerySysValue (HWND_DESKTOP, SV_CXDLGFRAME) * 2;
  220.     y += WinQuerySysValue (HWND_DESKTOP, SV_CYDLGFRAME) * 2
  221.             + WinQuerySysValue (HWND_DESKTOP, SV_CYTITLEBAR);
  222.     if (! hwndFrame) {
  223.         fl = FCF_DLGBORDER | FCF_TITLEBAR | FCF_SYSMENU | FCF_MINBUTTON
  224.                 | FCF_ICON;
  225.         hwndFrame = WinCreateStdWindow (HWND_DESKTOP, 0, & fl, pszImage,
  226.                 pszImage, 0, (HMODULE) NULL, ID_STATS, & hwndImage);
  227.         WinSetWindowPos (hwndFrame, (HWND) NULL, 0, 0, x, y, SWP_MOVE
  228.                 | SWP_SIZE | SWP_SHOW);
  229.     } else {
  230.         WinSetWindowPos (hwndFrame, (HWND) NULL, 0, 0, x, y, SWP_SIZE);
  231.     }
  232.     WinInvalidateRect (hwndImage, NULL, FALSE);
  233.     yLast = Screen_Height - 1;
  234. }
  235.  
  236.  
  237. VOID SetPixel (SHORT x, SHORT y, LONG lColor) {
  238.     POINTL ptl;
  239.     RECTL rcl;
  240.  
  241. /* if there's no bitmap or no window then forget it */
  242.     if (! hps || ! hwndImage) {
  243.         return;
  244.     }
  245.     y = Screen_Height - y - 1;
  246.     ptl.x = x;
  247.     ptl.y = y;
  248.     if (GpiSetColor (hps, lColor) == GPI_ERROR) {
  249.         PrintToTranscript ("GpiSetColor failed.\n");
  250.     }
  251.     if (GpiSetPel (hps, & ptl) == GPI_ERROR) {
  252.         PrintToTranscript ("GpiSetPel failed.\n");
  253.     }
  254.     if (y != yLast || y == 0 && x == Screen_Width - 1) {
  255.         rcl.xLeft = 0;
  256.         rcl.yBottom = yLast;
  257.         rcl.xRight = Screen_Width;
  258.         rcl.yTop = yLast + 1;
  259.         WinInvalidateRect (hwndImage, & rcl, FALSE);
  260.         yLast = y;
  261.     }
  262. }
  263.  
  264.  
  265. MRESULT EXPENTRY ImageWndProc (HWND hwnd, ULONG ulMessage, MPARAM mp1,
  266.         MPARAM mp2) {
  267.     switch (ulMessage) {
  268.         case WM_PAINT: {
  269.             HPS hpsScreen;
  270.             POINTL aptl [3];
  271.             RECTL rcl;
  272.  
  273.             hpsScreen = WinBeginPaint (hwnd, (HPS) NULL, & rcl);
  274.             aptl [0].x = rcl.xLeft;
  275.             aptl [0].y = rcl.yBottom;
  276.             aptl [1].x = rcl.xRight;
  277.             aptl [1].y = rcl.yTop;
  278.             aptl [2].x = rcl.xLeft;
  279.             aptl [2].y = rcl.yBottom;
  280.             if (GpiBitBlt (hpsScreen, hps, 3L, aptl, ROP_SRCCOPY, 0L)
  281.                     == GPI_ERROR) {
  282.                 PrintToTranscript ("GpiBitBlt failed.\n");
  283.             }
  284.             WinEndPaint (hpsScreen);
  285.             return (0L);
  286.         }
  287.         case WM_CLOSE: {
  288.             WinDestroyWindow (hwndFrame);
  289.             hwndFrame = (HWND) NULL;
  290.             hwndImage = (HWND) NULL;
  291.             return (0L);
  292.         }
  293.     }/*endSwitch*/
  294.     return (WinDefWindowProc (hwnd, ulMessage, mp1, mp2));
  295. }/* end ImageWndProc */
  296.