GU-guru needed.

Discuss the development of new homebrew software, tools and libraries.

Moderators: cheriff, TyRaNiD

Post Reply
LuMo
Posts: 410
Joined: Sun Aug 21, 2005 2:45 am
Location: Austria
Contact:

GU-guru needed.

Post by LuMo »

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
LuMo
Posts: 410
Joined: Sun Aug 21, 2005 2:45 am
Location: Austria
Contact:

Post by LuMo »

here is the code which manages the 'scene' (loader-code works fine)

Code: Select all

#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&#40; "3D Mesh Test", 0, 1, 1 &#41;;
PSP_MAIN_THREAD_ATTR&#40; THREAD_ATTR_USER &#41;;

//setup GU

#define printf pspDebugScreenPrintf



#define LIGHT_DISTANCE 3.0f                  //new light

char debugvalue&#91;255&#93;;

static unsigned int __attribute__&#40;&#40;aligned&#40;16&#41;&#41;&#41; list&#91;262144&#93;;

int
SetupCallbacks&#40;&#41;;
Debugger debug;
O3DB mesh;
O3DB axis;
int screenShotNumber = 0;

int
main&#40; int argc, char *argv &#91;&#93; &#41;
&#123;
 SceCtrlData pad;
 SetupCallbacks&#40;&#41;;
//  FileNode * root = ReadDir&#40;".", ".3db"&#41;; // new
//  FileNode * tmp = root;                  // new

 // setup ctrl
 sceCtrlSetSamplingCycle&#40;0&#41;;
 sceCtrlSetSamplingMode&#40;PSP_CTRL_MODE_ANALOG&#41;;

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

 // ENABLE ALPHA BLENDING
 sceGuAlphaFunc&#40;GU_GREATER,0x0,0xff&#41;;
 sceGuEnable&#40;GU_ALPHA_TEST&#41;;

 //sceGuEnable&#40;GU_CULL_FACE&#41;; //enable faceculling
 sceGuEnable&#40;GU_CLIP_PLANES&#41;;

 //lightning
 sceGuEnable&#40;GU_LIGHTING&#41;;
 sceGuEnable&#40;GU_LIGHT0&#41;;

 /* allocate memeory for texture */
  char * filename = "lumo.3db";           //argv&#91;1&#93;;//"cube.3db";
  mesh.load&#40;filename&#41;;
  
  
  filename = "axis.3db";
  axis.load&#40;filename&#41;;
  sceKernelDcacheWritebackAll&#40;&#41;;


/* model matrix */
	ScePspFMatrix4* modelRotateMatrix = new ScePspFMatrix4&#40;&#41;;
	ScePspFMatrix4* modelTranslateMatrix = new ScePspFMatrix4&#40;&#41;;
	ScePspFMatrix4* modelMatrix = new ScePspFMatrix4&#40;&#41;;
  sceGumLoadIdentity&#40;&#41;;
  sceGumStoreMatrix&#40;modelRotateMatrix&#41;;  // store identity in the modelRotateMatrix
  sceGumStoreMatrix&#40;modelTranslateMatrix&#41;;  // store identity in the modelTranslateMatrix
  
  
 if &#40;mesh.version>3&#41;
 &#123;
      /* Load png to ARGB texture */
     mesh.prepareTexture&#40;&#41;;
     sceKernelDcacheWritebackAll&#40;&#41;;
 &#125;

 sceGuFinish&#40;&#41;;
 sceGuSync&#40;0, 0&#41;;

 sceDisplayWaitVblankStart&#40;&#41;;
 sceGuDisplay&#40;GU_TRUE&#41;;



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

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


	  ScePspFVector3 trans =
	  &#123;
	    0, 0, 0
	  &#125;;

	  ScePspFVector3 rot =
	  &#123;
	    0, 0, 0
	  &#125;;

 while&#40; true &#41;
 &#123;
  sceCtrlReadBufferPositive&#40;& pad, 1&#41;;

  sceGuStart&#40;GU_DIRECT, list&#41;;

  // clear screen
  sceGuClearColor&#40;0xff554433&#41;;
  sceGuClearDepth&#40;0&#41;;
  sceGuClear&#40;GU_COLOR_BUFFER_BIT | GU_DEPTH_BUFFER_BIT&#41;;

  //FPS
  //drawFPS&#40;&#41;;

  // setup matrices
  sceGumMatrixMode&#40;GU_PROJECTION&#41;;
  sceGumLoadIdentity&#40;&#41;;
  sceGumPerspective&#40;75.0f, 16.0f / 9.0f, 0.5f, 1000.0f&#41;;
  sceGumMatrixMode&#40;GU_VIEW&#41;;
  sceGumLoadIdentity&#40;&#41;;
	ScePspFVector3 viewPos =
	  &#123;
	    0, 0, -27
	  &#125;;
	sceGumTranslate&#40;& viewPos &#41;;
  
  sceGumMatrixMode&#40;GU_MODEL&#41;;
  sceGumLoadIdentity&#40;&#41;;

  if&#40; 127 - pad.Ly>50 &#41;
    rot.x -= 2.0f *&#40;M_PI / 180.0f&#41;;

  if&#40; 127 - pad.Ly<-50 &#41;
    rot.x += 2.0f *&#40;M_PI / 180.0f&#41;;

  if&#40; 127 - pad.Lx>50 &#41;
    rot.y -= 2.0f *&#40;M_PI / 180.0f&#41;;

  if&#40; 127 - pad.Lx<-50 &#41;
    rot.y += 2.0f *&#40;M_PI / 180.0f&#41;;
  else
  &#123;
    //rotate z-direction
    /*
    if&#40; pad.Buttons & PSP_CTRL_TRIANGLE &#41;
      rot.z -= 1.0f *&#40;M_PI / 180.0f&#41;;

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

    if&#40; pad.Buttons & PSP_CTRL_CROSS &#41;
      rot.x += 1.0f *&#40;M_PI / 180.0f&#41;;

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

    if&#40; pad.Buttons & PSP_CTRL_CIRCLE &#41;
      rot.y += 1.0f *&#40;M_PI / 180.0f&#41;;

    //zoom

    if&#40; pad.Buttons & PSP_CTRL_RTRIGGER &#41;
      trans.z -= 0.5f;

    if&#40; pad.Buttons & PSP_CTRL_LTRIGGER &#41;
      trans.z += 0.5f;

    //strafe up/down
    if&#40; pad.Buttons & PSP_CTRL_UP &#41;
      trans.y += 0.5f;

    if&#40; pad.Buttons & PSP_CTRL_DOWN &#41;
      trans.y -= 0.5f;

    //strafe-left/righ
    if&#40; pad.Buttons & PSP_CTRL_LEFT &#41;
      trans.x -= 0.5f;

    if&#40; pad.Buttons & PSP_CTRL_RIGHT &#41;
      trans.x += 0.5f;

    // do a screenshot
    if&#40; oldButtons != pad.Buttons &#41;
    &#123;
      oldButtons = pad.Buttons;

      if&#40; pad.Buttons & PSP_CTRL_SELECT &#41;
      &#123;
        //screenshot
        FILE * pFile;

        char int2char&#91;255&#93;;
        sprintf&#40;int2char, "screenshot#%i.png", screenShotNumber&#41;;
        pFile = fopen&#40;int2char, "wb"&#41;;

        if&#40; pFile != NULL &#41;
        &#123;
          fclose&#40;pFile&#41;;
          screenshot&#40;int2char&#41;;
          screenShotNumber++;
        &#125;
        else
        &#123;
          fclose&#40;pFile&#41;;
        &#125;
      &#125;

      // quit
      if&#40; pad.Buttons & PSP_CTRL_START &#41;
      &#123;
      		sceGumLoadIdentity&#40;&#41;;
      		sceGumStoreMatrix&#40;modelRotateMatrix&#41;;  // store identity in the modelRotateMatrix
      		sceGumStoreMatrix&#40;modelTranslateMatrix&#41;;  // store identity in the modelTranslateMatrix
      		// restarts transformation
          //break;
      &#125;
    &#125;
  &#125;

  // new light
  if&#40; mesh.version>2 &#41; // newer versions support normal vectors
  &#123;
    int i = 0;
    ScePspFVector3 lpos =
    &#123;
      0, 0, 0
    &#125;;

    sceGuLight&#40;i, GU_DIRECTIONAL, GU_DIFFUSE_AND_SPECULAR, & lpos&#41;;
    sceGuLightColor&#40;i, GU_DIFFUSE, 0xffffffff&#41;;
    sceGuLightColor&#40;i, GU_SPECULAR, 0xffffffff&#41;;

    sceGuLightAtt&#40;i, 1.0f, 0.0f, 0.0f&#41;;

    sceGuSpecular&#40;12.0f&#41;;
    sceGuAmbient&#40;0x00bbbbbb&#41;;
  &#125;
  else
  &#123;
    // no lighnting -> disable it
    sceGuDisable&#40;GU_LIGHTING&#41;;
    sceGuDisable&#40;GU_LIGHT0&#41;;
  &#125;
  // end new light

  sceGumMatrixMode&#40;GU_MODEL&#41;;

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

  
  // load model matrix
  sceGumLoadMatrix&#40;modelMatrix&#41;;
  // draw model

  &#123;
      switch&#40; mesh.version &#41;
      &#123;
        case -2&#58;  // wireframe
        	sceGuDisable&#40;GU_TEXTURE_2D&#41;;
          sceGumDrawArray&#40;GU_LINES,
                          GU_INDEX_16BIT | GU_COLOR_8888 | GU_VERTEX_32BITF | GU_TRANSFORM_3D,
                          &#40;int&#41;mesh.faceCount * 2,
                          mesh.faces,
                          mesh.verticesV1&#41;;
          break;

        case -1&#58;  // point-model
        	sceGuDisable&#40;GU_TEXTURE_2D&#41;;
          sceGumDrawArray&#40;GU_POINTS,
                          GU_COLOR_8888 | GU_VERTEX_32BITF | GU_TRANSFORM_3D,
                          &#40;int&#41;mesh.faceCount, // same as vertexCount
                          0,
                          mesh.verticesV1&#41;;
          break;

        case 1&#58;
        	sceGuDisable&#40;GU_TEXTURE_2D&#41;;
          sceGumDrawArray&#40;GU_TRIANGLES,
                          GU_INDEX_16BIT | GU_COLOR_8888 | GU_VERTEX_32BITF | GU_TRANSFORM_3D,
                          &#40;int&#41;mesh.faceCount * 3,
                          mesh.faces,
                          mesh.verticesV1&#41;;
          break;

        case 2&#58;
        	sceGuDisable&#40;GU_TEXTURE_2D&#41;;
          sceGumDrawArray&#40;GU_TRIANGLES,
                          GU_INDEX_16BIT | GU_COLOR_8888 | GU_VERTEX_32BITF | GU_TRANSFORM_3D,
                          &#40;int&#41;mesh.faceCount * 3,
                          mesh.faces,
                          mesh.verticesV2&#41;;
          break;

        case 3&#58;
        	sceGuDisable&#40;GU_TEXTURE_2D&#41;;
          sceGumDrawArray&#40;GU_TRIANGLES,
                          GU_INDEX_16BIT | GU_NORMAL_32BITF | GU_COLOR_8888 | GU_VERTEX_32BITF | GU_TRANSFORM_3D,
                          &#40;int&#41;mesh.faceCount * 3,
                          mesh.faces,
                          mesh.verticesV3&#41;;
          break;

        case 4&#58;
          sceGuEnable&#40;GU_TEXTURE_2D&#41;;
          sceGuTexMode&#40;GU_PSM_8888,0,0,GU_TRUE&#41;;
          sceGuTexFunc&#40;GU_TFX_REPLACE,GU_TCC_RGBA&#41;; // replaces vertex paint
          sceGuTexEnvColor&#40;0x000000&#41;;  // black
          sceGuTexFilter&#40;GU_LINEAR_MIPMAP_NEAREST,GU_LINEAR_MIPMAP_NEAREST&#41;;
          sceGuTexScale&#40;1.0f,1.0f&#41;;
          sceGuTexOffset&#40;0.0f,0.0f&#41;;

          /* Set texture as current texture for the GU */
          sceGuTexImage&#40;0,mesh.texture_width, mesh.texture_height, mesh.texture_width, mesh.VRAM_pointer&#41;;

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

        case 5&#58;
          sceGuEnable&#40;GU_TEXTURE_2D&#41;;
          sceGuTexMode&#40;GU_PSM_8888,0,0,GU_TRUE&#41;;
          sceGuTexFunc&#40;GU_TFX_REPLACE,GU_TCC_RGBA&#41;; // replaces vertex paint
          sceGuTexEnvColor&#40;0x000000&#41;;  // black
          sceGuTexFilter&#40;GU_LINEAR_MIPMAP_NEAREST,GU_LINEAR_MIPMAP_NEAREST&#41;;
          sceGuTexScale&#40;1.0f,1.0f&#41;;
          sceGuTexOffset&#40;0.0f,0.0f&#41;;

          /* Set texture as current texture for the GU */
          sceGuTexImage&#40;0,mesh.texture_width, mesh.texture_height, mesh.texture_width, mesh.VRAM_pointer&#41;;

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

      &#125; // end switch
    &#125;

  sceGuFinish&#40;&#41;;

  sceGuSync&#40;0, 0&#41;;
  sceDisplayWaitVblankStart&#40;&#41;;
  sceGuSwapBuffers&#40;&#41;;
 &#125;
 //NukeAllNodes&#40;root&#41;; // cleanup the model-list

 sceGuTerm&#40;&#41;;

 free&#40;mesh.texture&#41;;

 sceKernelExitGame&#40;&#41;;
 return 0;
&#125;

/* Exit callback */
int
exit_callback&#40; int arg1, int arg2, void *common &#41;
&#123;
 sceKernelExitGame&#40;&#41;;
 return 0;
&#125;

/* Callback thread */
int
CallbackThread&#40; SceSize args, void *argp &#41;
&#123;
 int cbid;

 cbid = sceKernelCreateCallback&#40;"Exit Callback", exit_callback, NULL&#41;;
 sceKernelRegisterExitCallback&#40;cbid&#41;;

 sceKernelSleepThreadCB&#40;&#41;;

 return 0;
&#125;

/* Sets up the callback thread and returns its thread id */
int
SetupCallbacks&#40; void &#41;
&#123;
 int thid = 0;

 thid = sceKernelCreateThread&#40;"update_thread", CallbackThread, 0x11, 0xFA0, 0, 0&#41;;

 if&#40; thid>=0 &#41;
 &#123;
  sceKernelStartThread&#40;thid, 0, 0&#41;;
 &#125;

 return thid;
&#125;

"Good artists copy, great artists steal."
Pablo Picasso
go2lumo.com
Post Reply