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 

Help with lightning ??

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



Joined: 05 Oct 2005
Posts: 90
Location: Denmark

PostPosted: Mon Oct 17, 2005 8:07 am    Post subject: Help with lightning ?? Reply with quote

Hi,

I'm trying to get the light feature on the PSP to work, but without any luck. I've been looking through this site, but didn't find anything. Basically I'm right now playing with the cube sample and I've just added a diffuse light. But that doesn't work. Could someone take a look at the source below and see what I does wrong.

It seems like the light is rotated somehow ?

Code:

/*
 * PSP Software Development Kit - http://www.pspdev.org
 * -----------------------------------------------------------------------
 * Licensed under the BSD license, see LICENSE in PSPSDK root for details.
 *
 * Copyright (c) 2005 Jesper Svennevid
 */

#include <pspkernel.h>
#include <pspdisplay.h>
#include <pspdebug.h>
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#include <string.h>

#include <pspgu.h>

PSP_MODULE_INFO("Cube Sample", 0, 1, 1);
PSP_MAIN_THREAD_ATTR(THREAD_ATTR_USER);

#define printf   pspDebugScreenPrintf

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

struct Vertex
{
   unsigned int color;
   float x,y,z;
};

struct Vertex __attribute__((aligned(16))) vertices[12*3] =
{
   { 0xff7f0000,-1,-1, 1}, // 0
   { 0xff7f0000,-1, 1, 1}, // 4
   { 0xff7f0000, 1, 1, 1}, // 5

   { 0xff7f0000,-1,-1, 1}, // 0
   { 0xff7f0000, 1, 1, 1}, // 5
   { 0xff7f0000, 1,-1, 1}, // 1

   { 0xff7f0000,-1,-1,-1}, // 3
   { 0xff7f0000, 1,-1,-1}, // 2
   { 0xff7f0000, 1, 1,-1}, // 6

   { 0xff7f0000,-1,-1,-1}, // 3
   { 0xff7f0000, 1, 1,-1}, // 6
   { 0xff7f0000,-1, 1,-1}, // 7

   { 0xff007f00, 1,-1,-1}, // 0
   { 0xff007f00, 1,-1, 1}, // 3
   { 0xff007f00, 1, 1, 1}, // 7

   { 0xff007f00, 1,-1,-1}, // 0
   { 0xff007f00, 1, 1, 1}, // 7
   { 0xff007f00, 1, 1,-1}, // 4

   { 0xff007f00,-1,-1,-1}, // 0
   { 0xff007f00,-1, 1,-1}, // 3
   { 0xff007f00,-1, 1, 1}, // 7

   { 0xff007f00,-1,-1,-1}, // 0
   { 0xff007f00,-1, 1, 1}, // 7
   { 0xff007f00,-1,-1, 1}, // 4

   { 0xff00007f,-1, 1,-1}, // 0
   { 0xff00007f, 1, 1,-1}, // 1
   { 0xff00007f, 1, 1, 1}, // 2

   { 0xff00007f,-1, 1,-1}, // 0
   { 0xff00007f, 1, 1, 1}, // 2
   { 0xff00007f,-1, 1, 1}, // 3

   { 0xff00007f,-1,-1,-1}, // 4
   { 0xff00007f,-1,-1, 1}, // 7
   { 0xff00007f, 1,-1, 1}, // 6

   { 0xff00007f,-1,-1,-1}, // 4
   { 0xff00007f, 1,-1, 1}, // 6
   { 0xff00007f, 1,-1,-1}, // 5
};

int SetupCallbacks();

#define BUF_WIDTH (512)
#define SCR_WIDTH (480)
#define SCR_HEIGHT (272)
#define PIXEL_SIZE (4) /* change this if you change to another screenmode */
#define FRAME_SIZE (BUF_WIDTH * SCR_HEIGHT * PIXEL_SIZE)
#define ZBUF_SIZE (BUF_WIDTH SCR_HEIGHT * 2) /* zbuffer seems to be 16-bit? */

/* small matrix library */

void matrix_identity(float* matrix)
{
   matrix[(0<<2)+0] = 1.0f;
   matrix[(0<<2)+1] = 0.0f;
   matrix[(0<<2)+2] = 0.0f;
   matrix[(0<<2)+3] = 0.0f;

   matrix[(1<<2)+0] = 0.0f;
   matrix[(1<<2)+1] = 1.0f;
   matrix[(1<<2)+2] = 0.0f;
   matrix[(1<<2)+3] = 0.0f;

   matrix[(2<<2)+0] = 0.0f;
   matrix[(2<<2)+1] = 0.0f;
   matrix[(2<<2)+2] = 1.0f;
   matrix[(2<<2)+3] = 0.0f;

   matrix[(3<<2)+0] = 0.0f;
   matrix[(3<<2)+1] = 0.0f;
   matrix[(3<<2)+2] = 0.0f;
   matrix[(3<<2)+3] = 1.0f;
}

void matrix_projection(float* matrix, float fovy, float aspect, float near, float far)
{
   matrix_identity(matrix);

   float angle = (fovy / 2.0f) * (M_PI/180.0f);
   float cotangent = cosf(angle) / sinf(angle);

   matrix[(0<<2)+0] = cotangent / aspect;
   matrix[(1<<2)+1] = cotangent;
   matrix[(2<<2)+2] = (far + near) / (near - far);
   matrix[(3<<2)+2] = 2.0f * (far * near) / (near - far);
   matrix[(2<<2)+3] = -1;
   matrix[(3<<2)+3] = 0.0f;
}

void matrix_multiply(float* result, float* a, float* b)
{
   unsigned int i,j,k;
   float temp[16];

   for (i = 0; i < 4; ++i)
   {
      for (j = 0; j < 4; ++j)
      {
         float t = 0.0f;
         for (k = 0; k < 4; ++k)
            t += a[(k << 2)+j] * b[(i << 2)+k];
         temp[(i << 2)+j] = t;
      }
   }

   memcpy(result,temp,sizeof(float)*16);
}

void matrix_translate(float* matrix, float x, float y, float z)
{
   float temp[16];

   matrix_identity(temp);
   temp[(3 << 2)+0] = x;
   temp[(3 << 2)+1] = y;
   temp[(3 << 2)+2] = z;

   matrix_multiply(matrix,matrix,temp);
}

void matrix_setrotatex(float* matrix, float angle)
{
   float cs = cosf(angle);
   float sn = sinf(angle);

   matrix_identity(matrix);
   matrix[(1<<2)+1] = cs;
   matrix[(1<<2)+2] = sn;
   matrix[(2<<2)+1] = -sn;
   matrix[(2<<2)+2] = cs;
}

void matrix_setrotatey(float* matrix, float angle)
{
   float cs = cosf(angle);
   float sn = sinf(angle);

   matrix_identity(matrix);
   matrix[(0<<2)+0] = cs;
   matrix[(0<<2)+2] = -sn;
   matrix[(2<<2)+0] = sn;
   matrix[(2<<2)+2] = cs;
}

void matrix_setrotatez(float* matrix, float angle)
{
   float cs = cosf(angle);
   float sn = sinf(angle);

   matrix_identity(matrix);
   matrix[(0<<2)+0] = cs;
   matrix[(0<<2)+1] = sn;
   matrix[(1<<2)+0] = -sn;
   matrix[(1<<2)+1] = cs;
}

void matrix_rotate(float* matrix, float x, float y, float z)
{
   float temp[16];

   matrix_setrotatex(temp,x);
   matrix_multiply(matrix,matrix,temp);

   matrix_setrotatey(temp,y);
   matrix_multiply(matrix,matrix,temp);

   matrix_setrotatez(temp,z);
   matrix_multiply(matrix,matrix,temp);
}

int main(int argc, char* argv[])
{
   SetupCallbacks();

   // 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_CW);
   sceGuShadeModel(GU_SMOOTH);
   sceGuEnable(GU_CULL_FACE);
   sceGuEnable(GU_CLIP_PLANES);
   sceGuFinish();
   sceGuSync(0,0);

   sceDisplayWaitVblankStart();
   sceGuDisplay(GU_TRUE);

   // run sample

   int val = 0;
   ScePspFMatrix4 projection;
   ScePspFMatrix4 view;
   ScePspFMatrix4 world;

   for(;;)
   {
      sceGuStart(GU_DIRECT,list);

      // clear screen

      sceGuClearColor(0xff554433);
      sceGuClearDepth(0);
      sceGuClear(GU_COLOR_BUFFER_BIT|GU_DEPTH_BUFFER_BIT);

      // setup a light
      ScePspFVector3 lpos = { 5, 0, -20 };
      sceGuEnable(GU_LIGHTING);
      sceGuEnable(GU_LIGHT0);
      sceGuLight(0, GU_DIRECTIONAL, GU_DIFFUSE, &lpos);
      sceGuLightColor(0, GU_DIFFUSE, 0xffffff);

      matrix_identity((float*)&projection);
      matrix_projection((float*)&projection,75.0f,16.0/9.0f,1.0f,1000.0f);
      sceGuSetMatrix(GU_PROJECTION,&projection);

      matrix_identity((float*)&view);
      sceGuSetMatrix(GU_VIEW,&view);

      matrix_identity((float*)&world);
      matrix_translate((float*)&world,0,0,-3.5f);
      matrix_rotate((float*)&world,val * 0.79f * (M_PI/180.0f), val * 0.98f * (M_PI/180.0f), val * 1.32f * (M_PI/180.0f));
      sceGuSetMatrix(GU_MODEL,&world);

      // draw cube
      sceGuDrawArray(GU_TRIANGLES,GU_COLOR_8888|GU_VERTEX_32BITF|GU_TRANSFORM_3D,12*3,0,vertices);

      sceGuFinish();
      sceGuSync(0,0);

      sceDisplayWaitVblankStart();
      sceGuSwapBuffers();

      val++;
   }

   sceGuTerm();

   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;
}

_________________
Br, Sandberg
Back to top
View user's profile Send private message
Paco



Joined: 09 Oct 2005
Posts: 54

PostPosted: Mon Oct 17, 2005 8:42 am    Post subject: Reply with quote

You need vertex normals in your model for lighting to work. Check the "lights" example and work from that one.
_________________
Paco
Back to top
View user's profile Send private message
sandberg



Joined: 05 Oct 2005
Posts: 90
Location: Denmark

PostPosted: Mon Oct 17, 2005 9:31 am    Post subject: Reply with quote

Paco wrote:
You need vertex normals in your model for lighting to work. Check the "lights" example and work from that one.


Thanks Paco .. Just added vertex normals to my 3ds converter and now everything works smoothly on the PSP.
_________________
Br, Sandberg
Back to top
View user's profile Send private message
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