Distorted Sphere

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

Moderators: cheriff, TyRaNiD

Post Reply
void
Posts: 2
Joined: Sat Sep 03, 2005 12:25 pm

Distorted Sphere

Post by void »

I'm trying to write a simple program that renders a sphere with a few lights rotating around it. The function to calculate the vertices for the sphere was taken from an older program of mine that I know works correctly. However, the sphere appears slightly distorted here.

It looks sort of normal, but a few vertices here and there are at the origin. Here is what I mean:

http://img297.imageshack.us/my.php?image=image019iv.png

And here is the code:

structures from the header:

Code: Select all

struct Vertex
{
	float nx,ny,nz;
	float x,y,z;
};

struct SphereObj
{
	Vertex *mesh;
	int slices, stacks;
	float radius;
};

Code: Select all

#include <pspkernel.h>
#include <pspdisplay.h>
#include <pspdebug.h>
#include <pspctrl.h>
#include <pspgu.h>
#include <pspgum.h>
#include <png.h>
#include <math.h>
#include <stdlib.h>
#include "graphics.h"
#include "main.h"

PSP_MODULE_INFO&#40;"Light Demo", 0, 1, 1&#41;;
PSP_MAIN_THREAD_ATTR&#40;THREAD_ATTR_USER&#41;;

///// Global Variables ////////////////
static unsigned int __attribute__&#40;&#40;aligned&#40;16&#41;&#41;&#41; list&#91;262144&#93;;
extern Vertex vertices&#91;&#93;;
SphereObj obj;

///// Function Prototypes /////////////
int Setup&#40;&#41;;
int Render&#40;&#41;;
void Shutdown&#40;&#41;;

void DrawCube&#40;float x, float y, float z, float scale&#41;;
void CreateSphere&#40;SphereObj *obj, int slices, int stacks, float radius&#41;;
void RenderSphere&#40;SphereObj *obj&#41;;
void DeleteSphere&#40;SphereObj *obj&#41;;

int IsJoyInDeadZone&#40;SceCtrlData *pad&#41;;
int SetupCallbacks&#40;&#41;;
int exit_callback&#40;int arg1, int arg2, void *common&#41;;
int CallbackThread&#40;SceSize args, void *argp&#41;;
int SetupCallbacks&#40;void&#41;;

int main&#40;int argc, char* argv&#91;&#93;&#41;
&#123;
	Setup&#40;&#41;;

	while&#40;true&#41;
	&#123;
		if&#40;!Render&#40;&#41;&#41;
			break;
	&#125;

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

int Setup&#40;&#41;
&#123;
	SetupCallbacks&#40;&#41;;

	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;;
	sceGuEnable&#40;GU_LIGHTING&#41;;
	sceGuEnable&#40;GU_LIGHT0&#41;;
	sceGuEnable&#40;GU_LIGHT1&#41;;
	sceGuEnable&#40;GU_LIGHT2&#41;;
	sceGuFinish&#40;&#41;;
	sceGuSync&#40;0,0&#41;;

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

	sceCtrlSetSamplingCycle&#40;0&#41;;
	sceCtrlSetSamplingMode&#40;PSP_CTRL_MODE_ANALOG&#41;;

	CreateSphere&#40;&obj,20,10,2.0f&#41;;

	return 1;
&#125;

int Render&#40;&#41;
&#123;
	static float fRotateX = 0.0f, fRotateY = 0.0f, fRotateZ = 0.0f;
	static float fLightRot&#91;4&#93; = &#123;0.0f,0.0f,0.0f,0.0f&#125;;
	SceCtrlData pad;

	sceCtrlReadBufferPositive&#40;&pad,1&#41;;
	if&#40;pad.Buttons == PSP_CTRL_TRIANGLE&#41;
	&#123;
		return 0;
	&#125;
	if&#40;!IsJoyInDeadZone&#40;&pad&#41;&#41;
	&#123;
		fRotateX += &#40;&#40;float&#41;&#40;pad.Ly - 128&#41; / 64.0f&#41;;
		fRotateY += &#40;&#40;float&#41;&#40;pad.Lx - 128&#41; / 64.0f&#41;;
	&#125;

	sceGuStart&#40;GU_DIRECT,list&#41;;
	sceGuClearColor&#40;0xFF000000&#41;;
	sceGuClearDepth&#40;0&#41;;
	sceGuClear&#40;GU_COLOR_BUFFER_BIT|GU_DEPTH_BUFFER_BIT&#41;;
	
	unsigned int color&#91;3&#93; = &#123; 0xffff0000, 0xff00ff00, 0xff0000ff &#125;;
	ScePspFVector3 lpos&#91;3&#93; = &#123; &#123;cosf&#40;DegToRad&#40;fLightRot&#91;0&#93;&#41;&#41; * 5.0f, sinf&#40;DegToRad&#40;fLightRot&#91;0&#93;&#41;&#41; * 5.0f, 0&#125;,
							   &#123;0, sinf&#40;DegToRad&#40;fLightRot&#91;1&#93;&#41;&#41; * 5.0f, cosf&#40;DegToRad&#40;fLightRot&#91;1&#93;&#41;&#41; * 5.0f&#125;,
							   &#123;sinf&#40;DegToRad&#40;fLightRot&#91;2&#93;&#41;&#41; * 5.0f,cosf&#40;DegToRad&#40;fLightRot&#91;2&#93;&#41;&#41; * 5.0f,  0&#125; &#125;;

	sceGuLight&#40;0,GU_POINTLIGHT,GU_DIFFUSE,&lpos&#91;0&#93;&#41;;
	sceGuLightColor&#40;0,GU_DIFFUSE,color&#91;0&#93;&#41;;
	sceGuLight&#40;1,GU_POINTLIGHT,GU_DIFFUSE,&lpos&#91;1&#93;&#41;;
	sceGuLightColor&#40;1,GU_DIFFUSE,color&#91;1&#93;&#41;;
	sceGuLight&#40;2,GU_POINTLIGHT,GU_DIFFUSE,&lpos&#91;2&#93;&#41;;
	sceGuLightColor&#40;2,GU_DIFFUSE,color&#91;2&#93;&#41;;

	sceGuAmbient&#40;0x00222222&#41;;

	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 pos = &#123; 0, 0, -8.5f &#125;;
	sceGumTranslate&#40;&pos&#41;;

	sceGumMatrixMode&#40;GU_MODEL&#41;;
	sceGumLoadIdentity&#40;&#41;;

	ScePspFVector3 rot = &#123; DegToRad&#40;fRotateX&#41;, DegToRad&#40;fRotateY&#41;, DegToRad&#40;fRotateZ&#41; &#125;;
	sceGumRotateXYZ&#40;&rot&#41;;

	sceGuColor&#40;0xFFFFFFFF&#41;;
	RenderSphere&#40;&obj&#41;;

	// Draw light sources - disable lighting temporarily
	sceGuDisable&#40;GU_LIGHTING&#41;;
	sceGumLoadIdentity&#40;&#41;;
	for&#40;int i = 0; i < 3; i++&#41;
	&#123;
		sceGuColor&#40;color&#91;i&#93;&#41;;
		DrawCube&#40;lpos&#91;i&#93;.x,lpos&#91;i&#93;.y,lpos&#91;i&#93;.z,0.075f&#41;;
	&#125;
	sceGuEnable&#40;GU_LIGHTING&#41;;

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

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

	fLightRot&#91;0&#93; += 0.5f;
	fLightRot&#91;1&#93; += 1.0f;
	fLightRot&#91;2&#93; += 1.5f;
	return 1;
&#125;

void Shutdown&#40;&#41;
&#123;
	DeleteSphere&#40;&obj&#41;;
	sceGuTerm&#40;&#41;;
	sceKernelExitGame&#40;&#41;;
&#125;

void DrawCube&#40;float x, float y, float z, float scale&#41;
&#123;
	ScePspFVector3 pos = &#123; x, y, z &#125;;
	ScePspFVector3 scl = &#123; scale, scale, scale &#125;;

	sceGumPushMatrix&#40;&#41;;
		sceGumTranslate&#40;&pos&#41;;
		sceGumScale&#40;&scl&#41;;
		sceGumDrawArray&#40;GU_TRIANGLES,GU_NORMAL_32BITF|GU_VERTEX_32BITF|GU_TRANSFORM_3D,12*3,0,vertices&#41;;
	sceGumPopMatrix&#40;&#41;;
&#125;

void CreateSphere&#40;SphereObj *obj, int slices, int stacks, float radius&#41;
&#123;
	float fTheta1, fTheta2, fTheta3;
	if&#40;radius <= 0&#41;													// Is radius valid?
		return;
	if&#40;slices <= 0 || stacks <= 0&#41;									// Is precision valid?
		return;
	if&#40;obj->mesh&#41;													// Does mesh already exist?
		free&#40;obj->mesh&#41;;											// Deallocate existing mesh
	obj->slices = slices;											// Calculate slices
	obj->stacks = stacks;											// Calculate stacks
	obj->radius	= radius;
	obj->mesh = &#40;Vertex *&#41;malloc&#40;&#40;obj->slices + 1&#41; * obj->stacks * 2 * sizeof&#40;Vertex&#41;&#41;;
	Vertex *meshptr = obj->mesh;									// Pointer to mesh data
	for&#40;int j = 0; j < obj->stacks; j++&#41;							// Loop through stacks
	&#123;
		fTheta1 = j * PI / obj->stacks - PIDIV2;
		fTheta2 = &#40;j + 1&#41; * PI / obj->stacks - PIDIV2;
		for&#40;int i = 0; i <= obj->slices; i++&#41;						// Loop through slices
		&#123;
			fTheta3 = i * TWOPI / slices;
			meshptr->nx = cosf&#40;fTheta2&#41; * cosf&#40;fTheta3&#41;;
			meshptr->ny = sinf&#40;fTheta2&#41;;
			meshptr->nz = cosf&#40;fTheta2&#41; * sinf&#40;fTheta3&#41;;
			meshptr->x	= meshptr->nx * radius;
			meshptr->y	= meshptr->ny * radius;
			meshptr->z	= meshptr->nz * radius;
			meshptr++;
			meshptr->nx = cosf&#40;fTheta1&#41; * cosf&#40;fTheta3&#41;;
			meshptr->ny = sinf&#40;fTheta1&#41;;
			meshptr->nz = cosf&#40;fTheta1&#41; * sinf&#40;fTheta3&#41;;
			meshptr->x	= meshptr->nx * radius;
			meshptr->y	= meshptr->ny * radius;
			meshptr->z	= meshptr->nz * radius;
			meshptr++;
		&#125;
	&#125;
&#125;

void RenderSphere&#40;SphereObj *obj&#41;
&#123;
	Vertex *meshptr = obj->mesh;
	for&#40;int j = 0; j < obj->stacks; j++&#41;
	&#123;
		sceGumDrawArray&#40;GU_TRIANGLE_STRIP,GU_NORMAL_32BITF|GU_VERTEX_32BITF|GU_TRANSFORM_3D,&#40;obj->slices + 1&#41; * 2,0,meshptr&#41;;
		meshptr += &#40;&#40;obj->slices + 1&#41; * 2&#41;;
	&#125;
&#125;

void DeleteSphere&#40;SphereObj *obj&#41;
&#123;
	if&#40;obj->mesh&#41;
	&#123;
		free&#40;obj->mesh&#41;;
		obj->mesh = NULL;
	&#125;
&#125;

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

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;

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;

int IsJoyInDeadZone&#40;SceCtrlData *pad&#41;
&#123;
	if&#40;pad->Lx < 110 || pad->Lx > 146&#41;
		return 0;
	if&#40;pad->Ly < 110 || pad->Ly > 146&#41;
		return 0;
	return 1;
&#125;

Any ideas as to what I'm doing wrong?
ector
Posts: 195
Joined: Thu May 12, 2005 10:22 pm

Post by ector »

CACHE, people, CACHE!!!
Seems like we're getting three questions per day asking why some vertices are distorted, not realizing that they're probably still sitting in the CPU cache, not in memory where the gfx chip can read them. Flush your cache or write your data with an "uncached" pointer ( | 0x40000000).
http://www.dtek.chalmers.se/~tronic/PSPTexTool.zip Free texture converter for PSP with source. More to come.
void
Posts: 2
Joined: Sat Sep 03, 2005 12:25 pm

Post by void »

That did it. Sorry about the dumb question, I should do some searching next time.
Post Reply