Advertisement
GeeckoDev

2d shadowmap pspgu

Jul 5th, 2012
181
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.95 KB | None | 0 0
  1. #include "glib2d.h"
  2. #include <pspkernel.h>
  3. #include <pspgu.h>
  4. #include <pspctrl.h>
  5. #include <math.h>
  6.  
  7. #define SM_SIZE 0.25f // A _low_ resolution must be enough.
  8.  
  9. PSP_MODULE_INFO("App",0,1,1);
  10. PSP_MAIN_THREAD_ATTR(THREAD_ATTR_USER | THREAD_ATTR_VFPU);
  11.  
  12. /* Callbacks */
  13. int exit_callback(int arg1, int arg2, void *common) {
  14.   sceKernelExitGame();
  15.   return 0; }
  16. int CallbackThread(SceSize args, void *argp) {
  17.   int cbid;
  18.   cbid = sceKernelCreateCallback("Exit Callback", exit_callback, NULL);
  19.   sceKernelRegisterExitCallback(cbid);
  20.   sceKernelSleepThreadCB();
  21.   return 0; }
  22. int SetupCallbacks() {
  23.   int thid = 0;
  24.   thid = sceKernelCreateThread("update_thread", CallbackThread, 0x11, 0xFA0, 0, 0);
  25.   if(thid >= 0) sceKernelStartThread(thid, 0, 0);
  26.   return thid; }
  27.  
  28. int main()
  29. {
  30.     g2dImage *t1, *t2, *sm;
  31.     int x[4] = {50, 200, -130, 80};
  32.     int y[4] = {70, 20, -50, 100};
  33.     int s[4] = {6, 8, 10, 15};
  34.     int a[4] = {60, 100, 150, 200};
  35.     g2dColor c[4] = {CYAN, WHITE, RED, GREEN};
  36.     int i, r=0, mode=0;
  37.     SceCtrlData pad, oldPad;
  38.  
  39.     SetupCallbacks();
  40.  
  41.     g2dInit();
  42.     t1 = g2dTexLoad("t1.png", G2D_SWIZZLE); // Background
  43.     t2 = g2dTexLoad("t2.png", G2D_SWIZZLE); // Point light
  44.     sm = g2dTexCreate(G2D_SCR_W*SM_SIZE, G2D_SCR_H*SM_SIZE, true); // Shadowmap
  45.    
  46.     while (1)
  47.     {
  48.         // Update controls
  49.         oldPad = pad;
  50.         sceCtrlPeekBufferPositive(&pad, 1);
  51.         if ( (pad.Buttons & PSP_CTRL_CROSS) &&
  52.             !(oldPad.Buttons & PSP_CTRL_CROSS))
  53.         {
  54.             mode = (mode+1) % 3;
  55.         }
  56.         // Rotation step
  57.         r = (r+1) % 360;
  58.    
  59.         /* First pass : construct the shadow map into the framebuffer */
  60.  
  61.         if (mode > 0)
  62.         {
  63.             g2dClear(BLACK);
  64.  
  65.             // Limit the draw zone
  66.             g2dSetScissor(0, 0, G2D_SCR_W*SM_SIZE, G2D_SCR_H*SM_SIZE);
  67.             // Scale
  68.             g2dSetGlobalScale(SM_SIZE);
  69.             // Color add
  70.             sceGuBlendFunc(GU_ADD, GU_SRC_ALPHA, GU_FIX, 0, WHITE);
  71.            
  72.             // Some coloured point lights
  73.             g2dBeginRects(t2);
  74.             for (i=0; i<4; i++)
  75.             {
  76.                 g2dSetCoordXYRelative(x[i], y[i]);
  77.                 g2dSetRotationRelative(i%3 ? r : -r); // Random rotations
  78.                 g2dSetAlpha(a[i]);
  79.                 g2dSetColor(c[i]);
  80.                 g2dSetScale(s[i], s[i]);
  81.                 g2dAdd();
  82.             }
  83.             g2dEnd();
  84.            
  85.             // Copy the framebuffer back to the shadowmap texture
  86.             sceGuCopyImage(GU_PSM_8888, 0, 0,
  87.                            g2d_draw_buffer.w*SM_SIZE+1,
  88.                            g2d_draw_buffer.h*SM_SIZE+1,
  89.                            g2d_draw_buffer.tw, g2d_draw_buffer.data,
  90.                            0, 0, sm->tw, sm->data);
  91.            
  92.             // Restore
  93.             g2dResetScissor();
  94.             g2dResetGlobalScale();
  95.             sceGuBlendFunc(GU_ADD, GU_SRC_ALPHA, GU_ONE_MINUS_SRC_ALPHA, 0, 0);
  96.         }
  97.  
  98.         if (mode != 1)
  99.         {        
  100.             g2dClear(BLACK);
  101.            
  102.             /* Draw dafuq you want here */
  103.  
  104.             g2dBeginRects(t1);
  105.             g2dAdd();
  106.             g2dEnd();
  107.         }
  108.  
  109.         /* Second pass : draw the shadowmap */
  110.  
  111.         if (mode == 2)
  112.         {
  113.             // Color multiply
  114.             sceGuBlendFunc(GU_ADD, GU_DST_COLOR, GU_FIX, 0, 0);
  115.             // Ignore alpha
  116.             sceGuTexFunc(GU_TFX_REPLACE, GU_TCC_RGB);
  117.            
  118.             // Draw the shadowmap texture
  119.             g2dBeginRects(sm);
  120.             g2dSetScaleWH(G2D_SCR_W+1, G2D_SCR_H+1);
  121.             g2dAdd();
  122.             g2dEnd();
  123.  
  124.             // Restore
  125.             sceGuBlendFunc(GU_ADD, GU_SRC_ALPHA, GU_ONE_MINUS_SRC_ALPHA, 0, 0);
  126.             sceGuTexFunc(GU_TFX_MODULATE, GU_TCC_RGBA);
  127.         }
  128.        
  129.         /* Framebuffer flip */
  130.  
  131.         g2dFlip(G2D_VSYNC);
  132.     }
  133.    
  134.     g2dTerm();
  135.     return 0;
  136. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement