Share Pastebin
Guest
Public paste!

Geecko

By: a guest | Jul 7th, 2010 | Syntax: C | Size: 9.78 KB | Hits: 126 | Expires: Never
Copy text to clipboard
  1. /*
  2.  * PSP Software Development Kit - http://www.pspdev.org
  3.  * -----------------------------------------------------------------------
  4.  * Licensed under the BSD license, see LICENSE in PSPSDK root for details.
  5.  *
  6.  * Copyright (c) 2005 Jesper Svennevid
  7.  */
  8.  
  9. // Modified by Geecko, 2010
  10.  
  11. #include <pspkernel.h>
  12. #include <pspdisplay.h>
  13. #include <pspctrl.h>
  14. #include <pspdebug.h>
  15. #include <stdlib.h>
  16. #include <stdio.h>
  17. #include <math.h>
  18. #include <string.h>
  19.  
  20. #include <pspgu.h>
  21. #include <pspgum.h>
  22.  
  23. #include "callbacks.h"
  24. #include "vram.h"
  25. #include "intraFont.h"
  26.  
  27.  
  28. PSP_MODULE_INFO("3D Cube Sample", 0, 1, 1);
  29. PSP_MAIN_THREAD_ATTR(THREAD_ATTR_USER);
  30.  
  31. static unsigned int __attribute__((aligned(16))) list[262144];
  32. extern unsigned char logo_start[];
  33. SceCtrlData pad, oldPad;
  34. intraFont* font;
  35.  
  36. int dist=0, step = 0, dir = 1, anaglyph = 0;
  37. float cam_pos = 0.25f, cam_rot = 0.042f;
  38.  
  39. struct Vertex
  40. {
  41.   float u, v;
  42.   unsigned int color;
  43.   float x,y,z;
  44. };
  45.  
  46. // cube vertices
  47. struct Vertex __attribute__((aligned(16))) vertices[12*3] =
  48. {
  49.   {0, 0, 0xffcf0000,-1,-1, 1}, // 0
  50.   {1, 0, 0xffcf0000,-1, 1, 1}, // 4
  51.   {1, 1, 0xffcf0000, 1, 1, 1}, // 5
  52.  
  53.   {0, 0, 0xffcf0000,-1,-1, 1}, // 0
  54.   {1, 1, 0xffcf0000, 1, 1, 1}, // 5
  55.   {0, 1, 0xffcf0000, 1,-1, 1}, // 1
  56.  
  57.   {0, 0, 0xffcf0000,-1,-1,-1}, // 3
  58.   {1, 0, 0xffcf0000, 1,-1,-1}, // 2
  59.   {1, 1, 0xffcf0000, 1, 1,-1}, // 6
  60.  
  61.   {0, 0, 0xffcf0000,-1,-1,-1}, // 3
  62.   {1, 1, 0xffcf0000, 1, 1,-1}, // 6
  63.   {0, 1, 0xffcf0000,-1, 1,-1}, // 7
  64.  
  65.   {0, 0, 0xff00cf00, 1,-1,-1}, // 0
  66.   {1, 0, 0xff00cf00, 1,-1, 1}, // 3
  67.   {1, 1, 0xff00cf00, 1, 1, 1}, // 7
  68.  
  69.   {0, 0, 0xff00cf00, 1,-1,-1}, // 0
  70.   {1, 1, 0xff00cf00, 1, 1, 1}, // 7
  71.   {0, 1, 0xff00cf00, 1, 1,-1}, // 4
  72.  
  73.   {0, 0, 0xff00cf00,-1,-1,-1}, // 0
  74.   {1, 0, 0xff00cf00,-1, 1,-1}, // 3
  75.   {1, 1, 0xff00cf00,-1, 1, 1}, // 7
  76.  
  77.   {0, 0, 0xff00cf00,-1,-1,-1}, // 0
  78.   {1, 1, 0xff00cf00,-1, 1, 1}, // 7
  79.   {0, 1, 0xff00cf00,-1,-1, 1}, // 4
  80.  
  81.   {0, 0, 0xff0000cf,-1, 1,-1}, // 0
  82.   {1, 0, 0xff0000cf, 1, 1,-1}, // 1
  83.   {1, 1, 0xff0000cf, 1, 1, 1}, // 2
  84.  
  85.   {0, 0, 0xff0000cf,-1, 1,-1}, // 0
  86.   {1, 1, 0xff0000cf, 1, 1, 1}, // 2
  87.   {0, 1, 0xff0000cf,-1, 1, 1}, // 3
  88.  
  89.   {0, 0, 0xff0000cf,-1,-1,-1}, // 4
  90.   {1, 0, 0xff0000cf,-1,-1, 1}, // 7
  91.   {1, 1, 0xff0000cf, 1,-1, 1}, // 6
  92.  
  93.   {0, 0, 0xff0000cf,-1,-1,-1}, // 4
  94.   {1, 1, 0xff0000cf, 1,-1, 1}, // 6
  95.   {0, 1, 0xff0000cf, 1,-1,-1}, // 5
  96. };
  97.  
  98. #define BUF_WIDTH (512)
  99. #define SCR_WIDTH (480)
  100. #define SCR_HEIGHT (272)
  101.  
  102. void drawScene()
  103. {
  104.   sceGuClear(GU_DEPTH_BUFFER_BIT);
  105.  
  106.   // setup texture
  107.   sceGuTexMode(GU_PSM_4444,0,0,0);
  108.   sceGuTexImage(0,64,64,64,logo_start);
  109.   sceGuTexFunc(GU_TFX_MODULATE,GU_TCC_RGB);
  110.   sceGuTexFilter(GU_LINEAR,GU_LINEAR);
  111.   sceGuTexScale(1.0f,1.0f);
  112.   sceGuTexOffset(0.0f,0.0f);
  113.   sceGuColor(0xffffffff);
  114.   sceGuDisable(GU_BLEND);
  115.  
  116.   // First cube (centered)
  117.   sceGumMatrixMode(GU_MODEL);
  118.   sceGumLoadIdentity();
  119.   {
  120.     ScePspFVector3 pos = { 0, 0, -3.f - dist/50.f };
  121.     ScePspFVector3 rot = { step * 0.79f * (GU_PI/180.0f), step * 0.38f * (GU_PI/180.0f), step * 0.32f * (GU_PI/180.0f) };
  122.     sceGumTranslate(&pos);
  123.     sceGumRotateXYZ(&rot);
  124.   }
  125.   sceGumDrawArray(GU_TRIANGLES,GU_TEXTURE_32BITF|GU_COLOR_8888|GU_VERTEX_32BITF|GU_TRANSFORM_3D,12*3,0,vertices);
  126.  
  127.   // Satellite 1
  128.   {
  129.     ScePspFVector3 pos = { 0, 5.f, 0};
  130.     ScePspFVector3 rot = { -step * 0.49f * (GU_PI/180.0f), step * 0.08f * (GU_PI/180.0f), step * 0.53f * (GU_PI/180.0f) };
  131.     sceGumTranslate(&pos);
  132.     sceGumRotateXYZ(&rot);
  133.   }
  134.   sceGumDrawArray(GU_TRIANGLES,GU_TEXTURE_32BITF|GU_COLOR_8888|GU_VERTEX_32BITF|GU_TRANSFORM_3D,12*3,0,vertices);
  135.  
  136.   // Satellite 2
  137.   {
  138.     ScePspFVector3 pos = { 0, 0, 7.5f};
  139.     ScePspFVector3 rot = { step * 0.23f * (GU_PI/180.0f), step * 0.94f * (GU_PI/180.0f), step * 0.45f * (GU_PI/180.0f) };
  140.     sceGumTranslate(&pos);
  141.     sceGumRotateXYZ(&rot);
  142.   }
  143.   sceGumDrawArray(GU_TRIANGLES,GU_TEXTURE_32BITF|GU_COLOR_8888|GU_VERTEX_32BITF|GU_TRANSFORM_3D,12*3,0,vertices);
  144.  
  145.   // Satellite 3
  146.   {
  147.     ScePspFVector3 pos = { 10.f, 0, 0};
  148.     ScePspFVector3 rot = { -step * 0.75f * (GU_PI/180.0f), step * 0.94f * (GU_PI/180.0f), -step * 0.39f * (GU_PI/180.0f) };
  149.     sceGumTranslate(&pos);
  150.     sceGumRotateXYZ(&rot);
  151.   }
  152.   sceGumDrawArray(GU_TRIANGLES,GU_TEXTURE_32BITF|GU_COLOR_8888|GU_VERTEX_32BITF|GU_TRANSFORM_3D,12*3,0,vertices);
  153.  
  154.   // Satellite 4
  155.   {
  156.     ScePspFVector3 pos = { 0, 0, 12.5f};
  157.     ScePspFVector3 rot = { -step * 0.29f * (GU_PI/180.0f), -step * 0.38f * (GU_PI/180.0f), step * 0.81f * (GU_PI/180.0f) };
  158.     sceGumTranslate(&pos);
  159.     sceGumRotateXYZ(&rot);
  160.   }
  161.   sceGumDrawArray(GU_TRIANGLES,GU_TEXTURE_32BITF|GU_COLOR_8888|GU_VERTEX_32BITF|GU_TRANSFORM_3D,12*3,0,vertices);
  162.  
  163.   // Satellite 5
  164.   {
  165.     ScePspFVector3 pos = { 0, 15.f, 0};
  166.     ScePspFVector3 rot = { -step * 0.93f * (GU_PI/180.0f), -step * 0.28f * (GU_PI/180.0f), -step * 0.4f * (GU_PI/180.0f) };
  167.     sceGumTranslate(&pos);
  168.     sceGumRotateXYZ(&rot);
  169.   }
  170.   sceGumDrawArray(GU_TRIANGLES,GU_TEXTURE_32BITF|GU_COLOR_8888|GU_VERTEX_32BITF|GU_TRANSFORM_3D,12*3,0,vertices);
  171.  
  172.   // Satellite 6
  173.   {
  174.     ScePspFVector3 pos = { 0, 0, 17.5f};
  175.     ScePspFVector3 rot = { -step * 0.79f * (GU_PI/180.0f), -step * 0.98f * (GU_PI/180.0f), -step * 1.32f * (GU_PI/180.0f) };
  176.     sceGumTranslate(&pos);
  177.     sceGumRotateXYZ(&rot);
  178.   }
  179.   sceGumDrawArray(GU_TRIANGLES,GU_TEXTURE_32BITF|GU_COLOR_8888|GU_VERTEX_32BITF|GU_TRANSFORM_3D,12*3,0,vertices);
  180.  
  181.   // Satellite 7
  182.   {
  183.     ScePspFVector3 pos = { 0, 0, 20.f};
  184.     ScePspFVector3 rot = { step * 0.45f * (GU_PI/180.0f), -step * 0.28f * (GU_PI/180.0f), -step * 0.39f * (GU_PI/180.0f) };
  185.     sceGumTranslate(&pos);
  186.     sceGumRotateXYZ(&rot);
  187.   }
  188.   sceGumDrawArray(GU_TRIANGLES,GU_TEXTURE_32BITF|GU_COLOR_8888|GU_VERTEX_32BITF|GU_TRANSFORM_3D,12*3,0,vertices);
  189.  
  190.   // Satellite 8
  191.   {
  192.     ScePspFVector3 pos = { 22.5f, 0, 0};
  193.     ScePspFVector3 rot = { step * 0.79f * (GU_PI/180.0f), -step * 0.98f * (GU_PI/180.0f), -step * 0.42f * (GU_PI/180.0f) };
  194.     sceGumTranslate(&pos);
  195.     sceGumRotateXYZ(&rot);
  196.   }
  197.   sceGumDrawArray(GU_TRIANGLES,GU_TEXTURE_32BITF|GU_COLOR_8888|GU_VERTEX_32BITF|GU_TRANSFORM_3D,12*3,0,vertices);
  198. }
  199.  
  200. int main(int argc, char* argv[])
  201. {
  202.   setupCallbacks();
  203.  
  204.   // setup IntraFont
  205.   intraFontInit();
  206.   font = intraFontLoad("flash0:/font/ltn8.pgf",INTRAFONT_CACHE_MED);
  207.  
  208.   // setup GU
  209.  
  210.   void* fbp0 = getStaticVramBuffer(BUF_WIDTH,SCR_HEIGHT,GU_PSM_8888);
  211.   void* fbp1 = getStaticVramBuffer(BUF_WIDTH,SCR_HEIGHT,GU_PSM_8888);
  212.   void* zbp = getStaticVramBuffer(BUF_WIDTH,SCR_HEIGHT,GU_PSM_4444);
  213.  
  214.   sceGuInit();
  215.  
  216.   sceGuStart(GU_DIRECT,list);
  217.   sceGuDrawBuffer(GU_PSM_8888,fbp0,BUF_WIDTH);
  218.   sceGuDispBuffer(SCR_WIDTH,SCR_HEIGHT,fbp1,BUF_WIDTH);
  219.   sceGuDepthBuffer(zbp,BUF_WIDTH);
  220.   sceGuOffset(2048 - (SCR_WIDTH/2),2048 - (SCR_HEIGHT/2));
  221.   sceGuViewport(2048,2048,SCR_WIDTH,SCR_HEIGHT);
  222.   sceGuDepthRange(65535,0);
  223.   sceGuScissor(0,0,SCR_WIDTH,SCR_HEIGHT);
  224.   sceGuEnable(GU_SCISSOR_TEST);
  225.   sceGuDepthFunc(GU_GEQUAL);
  226.   sceGuEnable(GU_DEPTH_TEST);
  227.   sceGuFrontFace(GU_CW);
  228.   sceGuShadeModel(GU_SMOOTH);
  229.   sceGuEnable(GU_CULL_FACE);
  230.   sceGuEnable(GU_TEXTURE_2D);
  231.   sceGuEnable(GU_CLIP_PLANES);
  232.   sceGuFinish();
  233.   sceGuSync(0,0);
  234.  
  235.   sceDisplayWaitVblankStart();
  236.   sceGuDisplay(GU_TRUE);
  237.  
  238.   // clear setup
  239.   sceGuClearColor(0xffffffff);
  240.   sceGuClearDepth(0);
  241.  
  242.   // setup projection matrix
  243.   sceGumMatrixMode(GU_PROJECTION);
  244.   sceGumLoadIdentity();
  245.   sceGumPerspective(60.0f,16.0f/9.0f,0.5f,1000.0f);
  246.  
  247.   // run sample
  248.  
  249.   while(running())
  250.   {
  251.     oldPad = pad;
  252.     sceCtrlPeekBufferPositive(&pad,1);
  253.     if ((pad.Buttons & PSP_CTRL_CROSS) && !(oldPad.Buttons & PSP_CTRL_CROSS))
  254.       anaglyph = !anaglyph; // switches 3D mode.
  255.  
  256.     if (anaglyph)
  257.     {
  258.       if      (pad.Buttons & PSP_CTRL_LEFT)  cam_pos -= 0.01f;
  259.       else if (pad.Buttons & PSP_CTRL_RIGHT) cam_pos += 0.01f;
  260.       if      (pad.Buttons & PSP_CTRL_DOWN)  cam_rot -= 0.01f;
  261.       else if (pad.Buttons & PSP_CTRL_UP)    cam_rot += 0.01f;
  262.     }
  263.  
  264.     sceGuStart(GU_DIRECT,list);
  265.  
  266.     // clear screen
  267.     sceGuClear(GU_COLOR_BUFFER_BIT);
  268.  
  269.     if (!anaglyph)
  270.     {
  271.       // render scene for the each eye
  272.       sceGumMatrixMode(GU_VIEW);
  273.       sceGumLoadIdentity();
  274.       drawScene();
  275.     }
  276.     else
  277.     {
  278.       // render scene for the left eye
  279.       sceGumMatrixMode(GU_VIEW);
  280.       sceGumLoadIdentity();
  281.       {
  282.         ScePspFVector3 pos = { cam_pos, 0, 0 };
  283.         ScePspFVector3 rot = { 0, cam_rot, 0 };
  284.         sceGumTranslate(&pos);
  285.         sceGumRotateXYZ(&rot);
  286.       }
  287.       sceGuPixelMask(0x00ffff00); // Only cyan
  288.       drawScene();
  289.  
  290.       // render scene for the right eye
  291.       sceGumMatrixMode(GU_VIEW);
  292.       sceGumLoadIdentity();
  293.       {
  294.         ScePspFVector3 pos = { -cam_pos, 0, 0 };
  295.         ScePspFVector3 rot = { 0, -cam_rot, 0 };
  296.         sceGumTranslate(&pos);
  297.         sceGumRotateXYZ(&rot);
  298.       }
  299.       sceGuPixelMask(0x000000ff); // Only red
  300.       drawScene();
  301.     }
  302.  
  303.     // UI
  304.     sceGuPixelMask(0x00000000);
  305.     sceGuDisable(GU_DEPTH_TEST);
  306.     sceGuEnable(GU_BLEND);
  307.     sceGuBlendFunc(GU_ADD,GU_SRC_ALPHA,GU_ONE_MINUS_SRC_ALPHA,0,0);
  308.  
  309.     intraFontSetStyle(font,1.0f,0xFF000000,0xFFFFFFFF,INTRAFONT_ALIGN_CENTER);
  310.     intraFontPrintf(font,480/2,272-10,(anaglyph)?"3D Mode Enabled - Press Cross to switch":
  311.                                                  "3D Mode Disabled - Press Cross to switch");
  312.     if (anaglyph)
  313.     {
  314.       intraFontSetStyle(font,0.9f,0xFF000000,0xFFFFFFFF,INTRAFONT_ALIGN_LEFT);
  315.       intraFontPrintf(font,1,10,"%g/%g",cam_pos,cam_rot);
  316.     }
  317.  
  318.     sceGuFinish();
  319.     sceGuSync(0,0);
  320.     sceDisplayWaitVblankStart();
  321.     sceGuSwapBuffers();
  322.  
  323.     step++; // For rotation purposes
  324.     dist += dir; // Distance of the main cube
  325.     if (dist > 60*15 || dist < 0) dir = -dir;
  326.   }
  327.  
  328.   sceGuTerm();
  329.  
  330.   sceKernelExitGame();
  331.   return 0;
  332. }