forums.ps2dev.org Forum Index forums.ps2dev.org
Homebrew PS2, PSP & PS3 Development Discussions
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

GU-guru needed.

 
Post new topic   Reply to topic    forums.ps2dev.org Forum Index -> PSP Development
View previous topic :: View next topic  
Author Message
LuMo



Joined: 21 Aug 2005
Posts: 410
Location: Austria

PostPosted: Wed May 03, 2006 1:58 am    Post subject: GU-guru needed. Reply with quote

hi!
as you might know (from pspupdates)
i wrote the 3db-loader; some time passed and i added bit more stuff (not too much)
well i did not touch the code for some weeks;
now i got back changed some lines, and nothing will be shown anymore;
i looked through the code and wasnt able to find my fault,
so if anyone is willed to point me to my mistake it would be great
(will share the src with this guy, and publish the code when fixed, as it is quite final then)

hope there is a willed pro to assist
greets
lumo
_________________
"Good artists copy, great artists steal."
Pablo Picasso
go2lumo.com
Back to top
View user's profile Send private message Visit poster's website
LuMo



Joined: 21 Aug 2005
Posts: 410
Location: Austria

PostPosted: Wed May 03, 2006 7:33 pm    Post subject: Reply with quote

here is the code which manages the 'scene' (loader-code works fine)
Code:
#include <pspkernel.h>
#include <pspiofilemgr.h> //new
#include <malloc.h>
#include <pspdisplay.h>
#include <pspdebug.h>
#include <pspctrl.h>

#include <stdio.h>
#include <stdlib.h>
#include <math.h>

#include <sys/types.h>
#include <dirent.h>
#include <stdio.h>
#include <unistd.h>
#include <string>

#include <pspgu.h>
#include <pspgum.h>

#include "3dbClasses.h"

using namespace std;

PSP_MODULE_INFO( "3D Mesh Test", 0, 1, 1 );
PSP_MAIN_THREAD_ATTR( THREAD_ATTR_USER );

//setup GU

#define printf pspDebugScreenPrintf



#define LIGHT_DISTANCE 3.0f                  //new light

char debugvalue[255];

static unsigned int __attribute__((aligned(16))) list[262144];

int
SetupCallbacks();
Debugger debug;
O3DB mesh;
O3DB axis;
int screenShotNumber = 0;

int
main( int argc, char *argv [] )
{
 SceCtrlData pad;
 SetupCallbacks();
//  FileNode * root = ReadDir(".", ".3db"); // new
//  FileNode * tmp = root;                  // new

 // setup ctrl
 sceCtrlSetSamplingCycle(0);
 sceCtrlSetSamplingMode(PSP_CTRL_MODE_ANALOG);

 // setup GU
 sceGuInit();
 sceGuStart(GU_DIRECT, list);
 sceGuDrawBuffer(GU_PSM_8888, (void *)0, BUF_WIDTH);
 sceGuDispBuffer(SCR_WIDTH, SCR_HEIGHT, (void *)0x88000, BUF_WIDTH);
 sceGuDepthBuffer((void *)0x110000, BUF_WIDTH);
 sceGuOffset(2048 - (SCR_WIDTH / 2), 2048 - (SCR_HEIGHT / 2));
 sceGuViewport(2048, 2048, SCR_WIDTH, SCR_HEIGHT);
 sceGuDepthRange(0xc350, 0x2710);
 sceGuScissor(0, 0, SCR_WIDTH, SCR_HEIGHT);
 sceGuEnable(GU_SCISSOR_TEST);
 sceGuDepthFunc(GU_GEQUAL);
 sceGuEnable(GU_DEPTH_TEST);
 sceGuFrontFace(GU_CCW); //backfaceculling
 sceGuShadeModel(GU_SMOOTH);

 // ENABLE ALPHA BLENDING
 sceGuAlphaFunc(GU_GREATER,0x0,0xff);
 sceGuEnable(GU_ALPHA_TEST);

 //sceGuEnable(GU_CULL_FACE); //enable faceculling
 sceGuEnable(GU_CLIP_PLANES);

 //lightning
 sceGuEnable(GU_LIGHTING);
 sceGuEnable(GU_LIGHT0);

 /* allocate memeory for texture */
  char * filename = "lumo.3db";           //argv[1];//"cube.3db";
  mesh.load(filename);
 
 
  filename = "axis.3db";
  axis.load(filename);
  sceKernelDcacheWritebackAll();


/* model matrix */
   ScePspFMatrix4* modelRotateMatrix = new ScePspFMatrix4();
   ScePspFMatrix4* modelTranslateMatrix = new ScePspFMatrix4();
   ScePspFMatrix4* modelMatrix = new ScePspFMatrix4();
  sceGumLoadIdentity();
  sceGumStoreMatrix(modelRotateMatrix);  // store identity in the modelRotateMatrix
  sceGumStoreMatrix(modelTranslateMatrix);  // store identity in the modelTranslateMatrix
 
 
 if (mesh.version>3)
 {
      /* Load png to ARGB texture */
     mesh.prepareTexture();
     sceKernelDcacheWritebackAll();
 }

 sceGuFinish();
 sceGuSync(0, 0);

 sceDisplayWaitVblankStart();
 sceGuDisplay(GU_TRUE);



 if (mesh.version>3)
 {
    /* Pointer to VRAM. This is past the display buffer, back buffer and depth buffer */
    mesh.VRAM_pointer = (unsigned char *) ((0x04000000) + (2 * FRAME_SIZE +  ZBUF_SIZE));
    /* Upload the swizzled texture to VRAM */
    jsaTCacheSwizzleUpload((unsigned char *)mesh.VRAM_pointer, mesh.texture, mesh.texture_width * PIXEL_SIZE, mesh.texture_height);
 }
 sceKernelDcacheWritebackAll();

 //int delta = 0;
 unsigned int oldButtons = 0;


     ScePspFVector3 trans =
     {
       0, 0, 0
     };

     ScePspFVector3 rot =
     {
       0, 0, 0
     };

 while( true )
 {
  sceCtrlReadBufferPositive(& pad, 1);

  sceGuStart(GU_DIRECT, list);

  // clear screen
  sceGuClearColor(0xff554433);
  sceGuClearDepth(0);
  sceGuClear(GU_COLOR_BUFFER_BIT | GU_DEPTH_BUFFER_BIT);

  //FPS
  //drawFPS();

  // setup matrices
  sceGumMatrixMode(GU_PROJECTION);
  sceGumLoadIdentity();
  sceGumPerspective(75.0f, 16.0f / 9.0f, 0.5f, 1000.0f);
  sceGumMatrixMode(GU_VIEW);
  sceGumLoadIdentity();
   ScePspFVector3 viewPos =
     {
       0, 0, -27
     };
   sceGumTranslate(& viewPos );
 
  sceGumMatrixMode(GU_MODEL);
  sceGumLoadIdentity();

  if( 127 - pad.Ly>50 )
    rot.x -= 2.0f *(M_PI / 180.0f);

  if( 127 - pad.Ly<-50 )
    rot.x += 2.0f *(M_PI / 180.0f);

  if( 127 - pad.Lx>50 )
    rot.y -= 2.0f *(M_PI / 180.0f);

  if( 127 - pad.Lx<-50 )
    rot.y += 2.0f *(M_PI / 180.0f);
  else
  {
    //rotate z-direction
    /*
    if( pad.Buttons & PSP_CTRL_TRIANGLE )
      rot.z -= 1.0f *(M_PI / 180.0f);

    if( pad.Buttons & PSP_CTRL_CROSS )
      rot.z += 1.0f *(M_PI / 180.0f);
      */
    //rotate y-direction
    if( pad.Buttons & PSP_CTRL_TRIANGLE )
      rot.x -= 1.0f *(M_PI / 180.0f);

    if( pad.Buttons & PSP_CTRL_CROSS )
      rot.x += 1.0f *(M_PI / 180.0f);

    //rotate x-direction
    if( pad.Buttons & PSP_CTRL_SQUARE )
      rot.y -= 1.0f *(M_PI / 180.0f);

    if( pad.Buttons & PSP_CTRL_CIRCLE )
      rot.y += 1.0f *(M_PI / 180.0f);

    //zoom

    if( pad.Buttons & PSP_CTRL_RTRIGGER )
      trans.z -= 0.5f;

    if( pad.Buttons & PSP_CTRL_LTRIGGER )
      trans.z += 0.5f;

    //strafe up/down
    if( pad.Buttons & PSP_CTRL_UP )
      trans.y += 0.5f;

    if( pad.Buttons & PSP_CTRL_DOWN )
      trans.y -= 0.5f;

    //strafe-left/righ
    if( pad.Buttons & PSP_CTRL_LEFT )
      trans.x -= 0.5f;

    if( pad.Buttons & PSP_CTRL_RIGHT )
      trans.x += 0.5f;

    // do a screenshot
    if( oldButtons != pad.Buttons )
    {
      oldButtons = pad.Buttons;

      if( pad.Buttons & PSP_CTRL_SELECT )
      {
        //screenshot
        FILE * pFile;

        char int2char[255];
        sprintf(int2char, "screenshot#%i.png", screenShotNumber);
        pFile = fopen(int2char, "wb");

        if( pFile != NULL )
        {
          fclose(pFile);
          screenshot(int2char);
          screenShotNumber++;
        }
        else
        {
          fclose(pFile);
        }
      }

      // quit
      if( pad.Buttons & PSP_CTRL_START )
      {
            sceGumLoadIdentity();
            sceGumStoreMatrix(modelRotateMatrix);  // store identity in the modelRotateMatrix
            sceGumStoreMatrix(modelTranslateMatrix);  // store identity in the modelTranslateMatrix
            // restarts transformation
          //break;
      }
    }
  }

  // new light
  if( mesh.version>2 ) // newer versions support normal vectors
  {
    int i = 0;
    ScePspFVector3 lpos =
    {
      0, 0, 0
    };

    sceGuLight(i, GU_DIRECTIONAL, GU_DIFFUSE_AND_SPECULAR, & lpos);
    sceGuLightColor(i, GU_DIFFUSE, 0xffffffff);
    sceGuLightColor(i, GU_SPECULAR, 0xffffffff);

    sceGuLightAtt(i, 1.0f, 0.0f, 0.0f);

    sceGuSpecular(12.0f);
    sceGuAmbient(0x00bbbbbb);
  }
  else
  {
    // no lighnting -> disable it
    sceGuDisable(GU_LIGHTING);
    sceGuDisable(GU_LIGHT0);
  }
  // end new light

  sceGumMatrixMode(GU_MODEL);

  {
     sceGumLoadMatrix(modelTranslateMatrix);
     sceGumTranslate(& trans);
     sceGumStoreMatrix(modelTranslateMatrix);// store new matrix
    
     sceGumLoadMatrix(modelRotateMatrix);
     sceGumRotateXYZ(& rot);
      sceGumStoreMatrix(modelRotateMatrix);// store new matrix
    
     sceGumLoadIdentity();
     sceGumMultMatrix(modelTranslateMatrix); // then translate
     sceGumMultMatrix(modelRotateMatrix); // first rotate
     sceGumMultMatrix(modelMatrix);
     sceGumStoreMatrix(modelMatrix);// store new matrix
     // reset tramsformation data
     rot.x = rot.y = rot.z = 0;
     trans.x = trans.y = trans.z = 0;
  }
   // prepare to draw axis
  sceGumMatrixMode(GU_MODEL);
  sceGumLoadIdentity();
  // draw axis (V3)
  sceGuDisable(GU_TEXTURE_2D);
  sceGumDrawArray(GU_TRIANGLES,
                  GU_INDEX_16BIT | GU_NORMAL_32BITF | GU_COLOR_8888 | GU_VERTEX_32BITF | GU_TRANSFORM_3D,
                  (int)axis.faceCount * 3,
                   axis.faces,
                   axis.verticesV3);

 
  // load model matrix
  sceGumLoadMatrix(modelMatrix);
  // draw model

  {
      switch( mesh.version )
      {
        case -2:  // wireframe
           sceGuDisable(GU_TEXTURE_2D);
          sceGumDrawArray(GU_LINES,
                          GU_INDEX_16BIT | GU_COLOR_8888 | GU_VERTEX_32BITF | GU_TRANSFORM_3D,
                          (int)mesh.faceCount * 2,
                          mesh.faces,
                          mesh.verticesV1);
          break;

        case -1:  // point-model
           sceGuDisable(GU_TEXTURE_2D);
          sceGumDrawArray(GU_POINTS,
                          GU_COLOR_8888 | GU_VERTEX_32BITF | GU_TRANSFORM_3D,
                          (int)mesh.faceCount, // same as vertexCount
                          0,
                          mesh.verticesV1);
          break;

        case 1:
           sceGuDisable(GU_TEXTURE_2D);
          sceGumDrawArray(GU_TRIANGLES,
                          GU_INDEX_16BIT | GU_COLOR_8888 | GU_VERTEX_32BITF | GU_TRANSFORM_3D,
                          (int)mesh.faceCount * 3,
                          mesh.faces,
                          mesh.verticesV1);
          break;

        case 2:
           sceGuDisable(GU_TEXTURE_2D);
          sceGumDrawArray(GU_TRIANGLES,
                          GU_INDEX_16BIT | GU_COLOR_8888 | GU_VERTEX_32BITF | GU_TRANSFORM_3D,
                          (int)mesh.faceCount * 3,
                          mesh.faces,
                          mesh.verticesV2);
          break;

        case 3:
           sceGuDisable(GU_TEXTURE_2D);
          sceGumDrawArray(GU_TRIANGLES,
                          GU_INDEX_16BIT | GU_NORMAL_32BITF | GU_COLOR_8888 | GU_VERTEX_32BITF | GU_TRANSFORM_3D,
                          (int)mesh.faceCount * 3,
                          mesh.faces,
                          mesh.verticesV3);
          break;

        case 4:
          sceGuEnable(GU_TEXTURE_2D);
          sceGuTexMode(GU_PSM_8888,0,0,GU_TRUE);
          sceGuTexFunc(GU_TFX_REPLACE,GU_TCC_RGBA); // replaces vertex paint
          sceGuTexEnvColor(0x000000);  // black
          sceGuTexFilter(GU_LINEAR_MIPMAP_NEAREST,GU_LINEAR_MIPMAP_NEAREST);
          sceGuTexScale(1.0f,1.0f);
          sceGuTexOffset(0.0f,0.0f);

          /* Set texture as current texture for the GU */
          sceGuTexImage(0,mesh.texture_width, mesh.texture_height, mesh.texture_width, mesh.VRAM_pointer);

          // enable this when V4 will be released ;)
          sceGumDrawArray(GU_TRIANGLES,
                          GU_INDEX_16BIT | GU_TEXTURE_32BITF | GU_COLOR_8888 | GU_NORMAL_32BITF | GU_VERTEX_32BITF | GU_TRANSFORM_3D,
                          (int)mesh.faceCount * 3,
                          mesh.faces,
                          mesh.verticesV4);
          break;

        case 5:
          sceGuEnable(GU_TEXTURE_2D);
          sceGuTexMode(GU_PSM_8888,0,0,GU_TRUE);
          sceGuTexFunc(GU_TFX_REPLACE,GU_TCC_RGBA); // replaces vertex paint
          sceGuTexEnvColor(0x000000);  // black
          sceGuTexFilter(GU_LINEAR_MIPMAP_NEAREST,GU_LINEAR_MIPMAP_NEAREST);
          sceGuTexScale(1.0f,1.0f);
          sceGuTexOffset(0.0f,0.0f);

          /* Set texture as current texture for the GU */
          sceGuTexImage(0,mesh.texture_width, mesh.texture_height, mesh.texture_width, mesh.VRAM_pointer);

          // enable this when V4 will be released ;)
          sceGumDrawArray(GU_TRIANGLES,
                          GU_INDEX_16BIT | GU_TEXTURE_32BITF | GU_NORMAL_32BITF | GU_VERTEX_32BITF | GU_TRANSFORM_3D,
                          (int)mesh.faceCount * 3,
                          mesh.faces,
                          mesh.verticesV5);
          break;

      } // end switch
    }

  sceGuFinish();

  sceGuSync(0, 0);
  sceDisplayWaitVblankStart();
  sceGuSwapBuffers();
 }
 //NukeAllNodes(root); // cleanup the model-list

 sceGuTerm();

 free(mesh.texture);

 sceKernelExitGame();
 return 0;
}

/* Exit callback */
int
exit_callback( int arg1, int arg2, void *common )
{
 sceKernelExitGame();
 return 0;
}

/* Callback thread */
int
CallbackThread( SceSize args, void *argp )
{
 int cbid;

 cbid = sceKernelCreateCallback("Exit Callback", exit_callback, NULL);
 sceKernelRegisterExitCallback(cbid);

 sceKernelSleepThreadCB();

 return 0;
}

/* Sets up the callback thread and returns its thread id */
int
SetupCallbacks( void )
{
 int thid = 0;

 thid = sceKernelCreateThread("update_thread", CallbackThread, 0x11, 0xFA0, 0, 0);

 if( thid>=0 )
 {
  sceKernelStartThread(thid, 0, 0);
 }

 return thid;
}


_________________
"Good artists copy, great artists steal."
Pablo Picasso
go2lumo.com
Back to top
View user's profile Send private message Visit poster's website
Display posts from previous:   
Post new topic   Reply to topic    forums.ps2dev.org Forum Index -> PSP Development All times are GMT + 10 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group