Help with lightning ??

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

Moderators: cheriff, TyRaNiD

Post Reply
sandberg
Posts: 90
Joined: Wed Oct 05, 2005 1:25 am
Location: Denmark

Help with lightning ??

Post by sandberg »

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: Select all

/*
 * 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&#40;"Cube Sample", 0, 1, 1&#41;;
PSP_MAIN_THREAD_ATTR&#40;THREAD_ATTR_USER&#41;;

#define printf	pspDebugScreenPrintf

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

struct Vertex
&#123;
	unsigned int color;
	float x,y,z;
&#125;;

struct Vertex __attribute__&#40;&#40;aligned&#40;16&#41;&#41;&#41; vertices&#91;12*3&#93; =
&#123;
	&#123; 0xff7f0000,-1,-1, 1&#125;, // 0
	&#123; 0xff7f0000,-1, 1, 1&#125;, // 4
	&#123; 0xff7f0000, 1, 1, 1&#125;, // 5

	&#123; 0xff7f0000,-1,-1, 1&#125;, // 0
	&#123; 0xff7f0000, 1, 1, 1&#125;, // 5
	&#123; 0xff7f0000, 1,-1, 1&#125;, // 1

	&#123; 0xff7f0000,-1,-1,-1&#125;, // 3
	&#123; 0xff7f0000, 1,-1,-1&#125;, // 2
	&#123; 0xff7f0000, 1, 1,-1&#125;, // 6

	&#123; 0xff7f0000,-1,-1,-1&#125;, // 3
	&#123; 0xff7f0000, 1, 1,-1&#125;, // 6
	&#123; 0xff7f0000,-1, 1,-1&#125;, // 7

	&#123; 0xff007f00, 1,-1,-1&#125;, // 0
	&#123; 0xff007f00, 1,-1, 1&#125;, // 3
	&#123; 0xff007f00, 1, 1, 1&#125;, // 7

	&#123; 0xff007f00, 1,-1,-1&#125;, // 0
	&#123; 0xff007f00, 1, 1, 1&#125;, // 7
	&#123; 0xff007f00, 1, 1,-1&#125;, // 4

	&#123; 0xff007f00,-1,-1,-1&#125;, // 0
	&#123; 0xff007f00,-1, 1,-1&#125;, // 3
	&#123; 0xff007f00,-1, 1, 1&#125;, // 7

	&#123; 0xff007f00,-1,-1,-1&#125;, // 0
	&#123; 0xff007f00,-1, 1, 1&#125;, // 7
	&#123; 0xff007f00,-1,-1, 1&#125;, // 4

	&#123; 0xff00007f,-1, 1,-1&#125;, // 0
	&#123; 0xff00007f, 1, 1,-1&#125;, // 1
	&#123; 0xff00007f, 1, 1, 1&#125;, // 2

	&#123; 0xff00007f,-1, 1,-1&#125;, // 0
	&#123; 0xff00007f, 1, 1, 1&#125;, // 2
	&#123; 0xff00007f,-1, 1, 1&#125;, // 3

	&#123; 0xff00007f,-1,-1,-1&#125;, // 4
	&#123; 0xff00007f,-1,-1, 1&#125;, // 7
	&#123; 0xff00007f, 1,-1, 1&#125;, // 6

	&#123; 0xff00007f,-1,-1,-1&#125;, // 4
	&#123; 0xff00007f, 1,-1, 1&#125;, // 6
	&#123; 0xff00007f, 1,-1,-1&#125;, // 5
&#125;;

int SetupCallbacks&#40;&#41;;

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

/* small matrix library */

void matrix_identity&#40;float* matrix&#41;
&#123;
	matrix&#91;&#40;0<<2&#41;+0&#93; = 1.0f;
	matrix&#91;&#40;0<<2&#41;+1&#93; = 0.0f;
	matrix&#91;&#40;0<<2&#41;+2&#93; = 0.0f;
	matrix&#91;&#40;0<<2&#41;+3&#93; = 0.0f;

	matrix&#91;&#40;1<<2&#41;+0&#93; = 0.0f;
	matrix&#91;&#40;1<<2&#41;+1&#93; = 1.0f;
	matrix&#91;&#40;1<<2&#41;+2&#93; = 0.0f;
	matrix&#91;&#40;1<<2&#41;+3&#93; = 0.0f;

	matrix&#91;&#40;2<<2&#41;+0&#93; = 0.0f;
	matrix&#91;&#40;2<<2&#41;+1&#93; = 0.0f;
	matrix&#91;&#40;2<<2&#41;+2&#93; = 1.0f;
	matrix&#91;&#40;2<<2&#41;+3&#93; = 0.0f;

	matrix&#91;&#40;3<<2&#41;+0&#93; = 0.0f;
	matrix&#91;&#40;3<<2&#41;+1&#93; = 0.0f;
	matrix&#91;&#40;3<<2&#41;+2&#93; = 0.0f;
	matrix&#91;&#40;3<<2&#41;+3&#93; = 1.0f;
&#125;

void matrix_projection&#40;float* matrix, float fovy, float aspect, float near, float far&#41;
&#123;
	matrix_identity&#40;matrix&#41;;

	float angle = &#40;fovy / 2.0f&#41; * &#40;M_PI/180.0f&#41;;
	float cotangent = cosf&#40;angle&#41; / sinf&#40;angle&#41;;

	matrix&#91;&#40;0<<2&#41;+0&#93; = cotangent / aspect;
	matrix&#91;&#40;1<<2&#41;+1&#93; = cotangent;
	matrix&#91;&#40;2<<2&#41;+2&#93; = &#40;far + near&#41; / &#40;near - far&#41;;
	matrix&#91;&#40;3<<2&#41;+2&#93; = 2.0f * &#40;far * near&#41; / &#40;near - far&#41;;
	matrix&#91;&#40;2<<2&#41;+3&#93; = -1;
	matrix&#91;&#40;3<<2&#41;+3&#93; = 0.0f;
&#125;

void matrix_multiply&#40;float* result, float* a, float* b&#41;
&#123;
	unsigned int i,j,k;
	float temp&#91;16&#93;;

	for &#40;i = 0; i < 4; ++i&#41;
	&#123;
		for &#40;j = 0; j < 4; ++j&#41;
		&#123;
			float t = 0.0f;
			for &#40;k = 0; k < 4; ++k&#41;
				t += a&#91;&#40;k << 2&#41;+j&#93; * b&#91;&#40;i << 2&#41;+k&#93;;
			temp&#91;&#40;i << 2&#41;+j&#93; = t;
		&#125;
	&#125;

	memcpy&#40;result,temp,sizeof&#40;float&#41;*16&#41;;
&#125;

void matrix_translate&#40;float* matrix, float x, float y, float z&#41;
&#123;
	float temp&#91;16&#93;;

	matrix_identity&#40;temp&#41;;
	temp&#91;&#40;3 << 2&#41;+0&#93; = x;
	temp&#91;&#40;3 << 2&#41;+1&#93; = y;
	temp&#91;&#40;3 << 2&#41;+2&#93; = z;

	matrix_multiply&#40;matrix,matrix,temp&#41;;
&#125;

void matrix_setrotatex&#40;float* matrix, float angle&#41;
&#123;
	float cs = cosf&#40;angle&#41;;
	float sn = sinf&#40;angle&#41;;

	matrix_identity&#40;matrix&#41;;
	matrix&#91;&#40;1<<2&#41;+1&#93; = cs;
	matrix&#91;&#40;1<<2&#41;+2&#93; = sn;
	matrix&#91;&#40;2<<2&#41;+1&#93; = -sn;
	matrix&#91;&#40;2<<2&#41;+2&#93; = cs;
&#125;

void matrix_setrotatey&#40;float* matrix, float angle&#41;
&#123;
	float cs = cosf&#40;angle&#41;;
	float sn = sinf&#40;angle&#41;;

	matrix_identity&#40;matrix&#41;;
	matrix&#91;&#40;0<<2&#41;+0&#93; = cs;
	matrix&#91;&#40;0<<2&#41;+2&#93; = -sn;
	matrix&#91;&#40;2<<2&#41;+0&#93; = sn;
	matrix&#91;&#40;2<<2&#41;+2&#93; = cs;
&#125;

void matrix_setrotatez&#40;float* matrix, float angle&#41;
&#123;
	float cs = cosf&#40;angle&#41;;
	float sn = sinf&#40;angle&#41;;

	matrix_identity&#40;matrix&#41;;
	matrix&#91;&#40;0<<2&#41;+0&#93; = cs;
	matrix&#91;&#40;0<<2&#41;+1&#93; = sn;
	matrix&#91;&#40;1<<2&#41;+0&#93; = -sn;
	matrix&#91;&#40;1<<2&#41;+1&#93; = cs;
&#125;

void matrix_rotate&#40;float* matrix, float x, float y, float z&#41;
&#123;
	float temp&#91;16&#93;;

	matrix_setrotatex&#40;temp,x&#41;;
	matrix_multiply&#40;matrix,matrix,temp&#41;;

	matrix_setrotatey&#40;temp,y&#41;;
	matrix_multiply&#40;matrix,matrix,temp&#41;;

	matrix_setrotatez&#40;temp,z&#41;;
	matrix_multiply&#40;matrix,matrix,temp&#41;;
&#125;

int main&#40;int argc, char* argv&#91;&#93;&#41;
&#123;
	SetupCallbacks&#40;&#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_CW&#41;;
	sceGuShadeModel&#40;GU_SMOOTH&#41;;
	sceGuEnable&#40;GU_CULL_FACE&#41;;
	sceGuEnable&#40;GU_CLIP_PLANES&#41;;
	sceGuFinish&#40;&#41;;
	sceGuSync&#40;0,0&#41;;

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

	// run sample

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

	for&#40;;;&#41;
	&#123;
		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;;

		// setup a light
		ScePspFVector3 lpos = &#123; 5, 0, -20 &#125;;
		sceGuEnable&#40;GU_LIGHTING&#41;;
		sceGuEnable&#40;GU_LIGHT0&#41;;
		sceGuLight&#40;0, GU_DIRECTIONAL, GU_DIFFUSE, &lpos&#41;;
		sceGuLightColor&#40;0, GU_DIFFUSE, 0xffffff&#41;;

		matrix_identity&#40;&#40;float*&#41;&projection&#41;;
		matrix_projection&#40;&#40;float*&#41;&projection,75.0f,16.0/9.0f,1.0f,1000.0f&#41;;
		sceGuSetMatrix&#40;GU_PROJECTION,&projection&#41;;

		matrix_identity&#40;&#40;float*&#41;&view&#41;;
		sceGuSetMatrix&#40;GU_VIEW,&view&#41;;

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

		// draw cube
		sceGuDrawArray&#40;GU_TRIANGLES,GU_COLOR_8888|GU_VERTEX_32BITF|GU_TRANSFORM_3D,12*3,0,vertices&#41;;

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

		sceDisplayWaitVblankStart&#40;&#41;;
		sceGuSwapBuffers&#40;&#41;;

		val++;
	&#125;

	sceGuTerm&#40;&#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;
Br, Sandberg
Paco
Posts: 54
Joined: Sun Oct 09, 2005 6:53 pm

Post by Paco »

You need vertex normals in your model for lighting to work. Check the "lights" example and work from that one.
Paco
sandberg
Posts: 90
Joined: Wed Oct 05, 2005 1:25 am
Location: Denmark

Post by sandberg »

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
Post Reply