"LookAt" Function?

General Programming help in C, C++ or ASM, Compiling / Debugging, and R3000A Central Processing Unit (CPU) information
Post Reply
User avatar
MrQuetch
Curious PSXDEV User
Curious PSXDEV User
Posts: 23
Joined: April 1st, 2018, 9:34 am
Motto: You can accomplish anything.
Looking to: Create professional homebrew.
Location: United States

"LookAt" Function?

Post by MrQuetch » April 3rd, 2018, 6:30 am

Hello, everyone.

I have noticed that for a lot of different gaming consoles there are "LookAt" functions. Basically, I would like to set a point for a camera in 3D space, and have it look at another point somewhere else in 3D space. Would the PS1 happen to have the equivalent of that? If so, where could I find one? If not, how could I go about creating my own?

I have been looking through the PS1 SDK, but have not found anything yet. I'm assuming this is an easy problem with an easy fix.

Any help is appreciated. Thanks.

User avatar
LameGuy64
Psy-Q Enthusiast
Psy-Q Enthusiast
Posts: 349
Joined: April 10th, 2013, 3:47 pm
I am a: Hobbyist Game Developer
Motto: Commercial or not, play it!
PlayStation Model: H2000/7000
Skype: lameguy64
Location: Philippines

Re: "LookAt" Function?

Post by LameGuy64 » April 21st, 2018, 11:55 am

I have this lookat routine which may do just the trick:

Code: Select all

void crossProduct(SVECTOR *v0, SVECTOR *v1, VECTOR *out) {

	out->vx = ((v0->vy*v1->vz)-(v0->vz*v1->vy))>>12;
	out->vy = ((v0->vz*v1->vx)-(v0->vx*v1->vz))>>12;
	out->vz = ((v0->vx*v1->vy)-(v0->vy*v1->vx))>>12;

}

void LookAt(VECTOR *eye, VECTOR *at, SVECTOR *up, MATRIX *mtx) {

	VECTOR taxis;
	SVECTOR zaxis;
	SVECTOR xaxis;
	SVECTOR yaxis;
	VECTOR pos;
	VECTOR vec;

	setVector(&taxis, at->vx-eye->vx, at->vy-eye->vy, at->vz-eye->vz);
	VectorNormalS(&taxis, &zaxis);
    crossProduct(&zaxis, up, &taxis);
	VectorNormalS(&taxis, &xaxis);
	crossProduct(&zaxis, &xaxis, &taxis);
	VectorNormalS(&taxis, &yaxis);

	mtx->m[0][0] = xaxis.vx;	mtx->m[1][0] = yaxis.vx;	mtx->m[2][0] = zaxis.vx;
	mtx->m[0][1] = xaxis.vy;	mtx->m[1][1] = yaxis.vy;	mtx->m[2][1] = zaxis.vy;
	mtx->m[0][2] = xaxis.vz;	mtx->m[1][2] = yaxis.vz;	mtx->m[2][2] = zaxis.vz;

	//ApplyMatrixLV(mtx, eye, &vec);
	//TransMatrix(mtx, &vec);

	//mtx->t[0] = eye->vx;
	//mtx->t[1] = eye->vy;
	//mtx->t[2] = eye->vz;

	pos.vx = -eye->vx;//>>12;
	pos.vy = -eye->vy;//>>12;
	pos.vz = -eye->vz;//>>12;

	ApplyMatrixLV(mtx, &pos, &vec);
	TransMatrix(mtx, &vec);
}
Syntax should be similar to OpenGL's lookat function. Reply back if you have any troubles.
Please don't forget to include my name if you share my work around. Credit where it is due.

Dev. Console: SCPH-7000 with SCPH-7501 ROM, MM3, PAL color fix, Direct AV ports, DB-9 port for Serial I/O, and a Xplorer FX with Caetla 0.35.

DTL-H2000 PC: Dell Optiplex GX110, Windows 98SE & Windows XP, Pentium III 933MHz, 384MB SDRAM, ATI Radeon 7000 VE 64MB, Soundblaster Audigy, 40GB Seagate HDD, Hitachi Lite-on CD-RW Drive, ZIP 250 and 3.5" Floppy.

User avatar
MrQuetch
Curious PSXDEV User
Curious PSXDEV User
Posts: 23
Joined: April 1st, 2018, 9:34 am
Motto: You can accomplish anything.
Looking to: Create professional homebrew.
Location: United States

Re: "LookAt" Function?

Post by MrQuetch » May 1st, 2018, 10:24 am

LameGuy64 wrote:
April 21st, 2018, 11:55 am
I have this lookat routine which may do just the trick:

Code: Select all

void crossProduct(SVECTOR *v0, SVECTOR *v1, VECTOR *out) {

	out->vx = ((v0->vy*v1->vz)-(v0->vz*v1->vy))>>12;
	out->vy = ((v0->vz*v1->vx)-(v0->vx*v1->vz))>>12;
	out->vz = ((v0->vx*v1->vy)-(v0->vy*v1->vx))>>12;

}

void LookAt(VECTOR *eye, VECTOR *at, SVECTOR *up, MATRIX *mtx) {

	VECTOR taxis;
	SVECTOR zaxis;
	SVECTOR xaxis;
	SVECTOR yaxis;
	VECTOR pos;
	VECTOR vec;

	setVector(&taxis, at->vx-eye->vx, at->vy-eye->vy, at->vz-eye->vz);
	VectorNormalS(&taxis, &zaxis);
    crossProduct(&zaxis, up, &taxis);
	VectorNormalS(&taxis, &xaxis);
	crossProduct(&zaxis, &xaxis, &taxis);
	VectorNormalS(&taxis, &yaxis);

	mtx->m[0][0] = xaxis.vx;	mtx->m[1][0] = yaxis.vx;	mtx->m[2][0] = zaxis.vx;
	mtx->m[0][1] = xaxis.vy;	mtx->m[1][1] = yaxis.vy;	mtx->m[2][1] = zaxis.vy;
	mtx->m[0][2] = xaxis.vz;	mtx->m[1][2] = yaxis.vz;	mtx->m[2][2] = zaxis.vz;

	//ApplyMatrixLV(mtx, eye, &vec);
	//TransMatrix(mtx, &vec);

	//mtx->t[0] = eye->vx;
	//mtx->t[1] = eye->vy;
	//mtx->t[2] = eye->vz;

	pos.vx = -eye->vx;//>>12;
	pos.vy = -eye->vy;//>>12;
	pos.vz = -eye->vz;//>>12;

	ApplyMatrixLV(mtx, &pos, &vec);
	TransMatrix(mtx, &vec);
}
Syntax should be similar to OpenGL's lookat function. Reply back if you have any troubles.
Hi, LameGuy64.

I'm sorry for the late reply. I thought this was going to be an easy fix, but it looks like it's going to take longer than I expected. Regardless of the length, thank you for the snippet of code. It's been awhile since I programmed for the PS1, so I'm going to familiarize myself with it again before I implement this piece. I will let you know if I run into any trouble.

Post Reply

Who is online

Users browsing this forum: No registered users and 2 guests