[SRC] 3D Projection & Textured Quad

Graphic based area of development (Graphics Processing Unit), including the Geometry Transform Engine (GTE), TIM, STR (MDEC), etc.
Post Reply
User avatar
ddoodm
Curious PSXDEV User
Curious PSXDEV User
Posts: 15
Joined: Nov 18, 2012
Location: Sydney, Australia
Contact:

[SRC] 3D Projection & Textured Quad

Post by ddoodm » June 11th, 2013, 6:59 pm

Hello there! First up, please excuse any informalities; this is my first post here - been a loiterer for a while. :)

I thought I'd share an example of 3D perspective projection onto a flat textured quad with the Psy-Q SDK. The source code includes detailed comments (pretty close to 'i++' redundancy) and binaries (including a PS-EXE and BIN/CUE pair).

UPDATE: I have since studied the correct way to implement an OT. Do not follow this demo if you're learning the basics of setting up a double-buffered environment.

The program features:
  • Use of an Ordering Table (not that it's really necessary).
  • A struct-based double-buffer setup.
  • GTE perspective rotation-translation matrices modifiable by controller input (D-Pad, cross & triangle).
Known Bugs:
  • The display V-Shifts upwards on my SCPH-7502 (PAL). If anyone has a suggestion for that, that's cool!
  • The pSX emulator does not draw the poly at all. Loads my texture pattern and CLUT into VRAM, but it's a bit picky with my poly for some reason. Still learning.
Many thanks to Shadow for the ClearVRAM procedure and auto-VMode thingy!

Here it is!
sprite3d.zip
Preview:
Image

Thanks, and happy coding!
You do not have the required permissions to view the files attached to this post.
Last edited by ddoodm on June 20th, 2013, 2:03 pm, edited 1 time in total.
^____________^

User avatar
Shadow
Verified
Admin / PSXDEV
Admin / PSXDEV
Posts: 2670
Joined: Dec 31, 2012
PlayStation Model: H2000/5502
Discord: Shadow^PSXDEV

Post by Shadow » June 11th, 2013, 7:59 pm

Welcome Deinyon! I'm impressed by your work in MAX, VRay and the PSX.
Anyway, it works fine under PSXeven and XEBRA. PSXFIN (pSX) is just having the problem.
Looking at your code, I can see why. You should use a complete isolated/separate function for doing the drawing and not an unsigned long as a pointer in defined structure, but you can try just adding in a VSync(0) to your drawing routine.

This display function works perfect and has double-buffering:

Code: Select all

void display()
{
   // refresh the font
   if (DEBUG) FntFlush(-1);

   // get the current buffer
   CurrentBuffer=GsGetActiveBuff();

   // setup the packet workbase
   GsSetWorkBase((PACKET*)GPUOutputPacket[CurrentBuffer]);

   // clear the ordering table
   GsClearOt(0,0,&WorldOrderingTable[CurrentBuffer]);

   // insert sprites into the ordering table
   GsSortSprite(&ImageTIM[0], &WorldOrderingTable[CurrentBuffer], 0);

   // wait for all drawing to finish
   DrawSync(0);

   // wait for v_blank interrupt
   VSync(0);

   // flip double buffers
   GsSwapDispBuff();

   // clear the ordering table with a background colour
   GsSortClear(0,0,0,&WorldOrderingTable[CurrentBuffer]);

   // Draw the ordering table for the CurrentBuffer
   GsDrawOt(&WorldOrderingTable[CurrentBuffer]);
}
Take a look at this code to get your other prototypes and functions declared accordingly:
http://www.psxdev.net/help/hello-world_source/MAIN.C

Note that

Code: Select all

// clear the ordering table with a background colour
GsSortClear(0,0,0,&WorldOrderingTable[CurrentBuffer]);
will actually clear all OT's. If you have several of them, it can cause a flicker effect once on a re-draw period.
Development Console: SCPH-5502 with 8MB RAM, MM3 Modchip, PAL 60 Colour Modification (for NTSC), PSIO Switch Board, DB-9 breakout headers for both RGB and Serial output and an Xplorer with CAETLA 0.34.

PlayStation Development PC: Windows 98 SE, Pentium 3 at 400MHz, 128MB SDRAM, DTL-H2000, DTL-H2010, DTL-H201A, DTL-S2020 (with 4GB SCSI-2 HDD), 21" Sony G420, CD-R burner, 3.25" and 5.25" Floppy Diskette Drives, ZIP 100 Diskette Drive and an IBM Model M keyboard.

User avatar
ddoodm
Curious PSXDEV User
Curious PSXDEV User
Posts: 15
Joined: Nov 18, 2012
Location: Sydney, Australia
Contact:

Post by ddoodm » June 11th, 2013, 8:14 pm

Hi Shadow, thanks a lot for the info and for your kind welcome. You're incredibly helpful!

I'll try implementing my drawing routine with your snippet ASAP. I'm pretty confused about how this "packet workbase" stuff works. I'll have a peek at the devref. Thanks again!
^____________^

User avatar
Shadow
Verified
Admin / PSXDEV
Admin / PSXDEV
Posts: 2670
Joined: Dec 31, 2012
PlayStation Model: H2000/5502
Discord: Shadow^PSXDEV

Post by Shadow » June 11th, 2013, 8:22 pm

You're welcome.
The competition finished though and LameGuy64 won it.
I was re-coding it for you, but give it a shot again yourself.
Development Console: SCPH-5502 with 8MB RAM, MM3 Modchip, PAL 60 Colour Modification (for NTSC), PSIO Switch Board, DB-9 breakout headers for both RGB and Serial output and an Xplorer with CAETLA 0.34.

PlayStation Development PC: Windows 98 SE, Pentium 3 at 400MHz, 128MB SDRAM, DTL-H2000, DTL-H2010, DTL-H201A, DTL-S2020 (with 4GB SCSI-2 HDD), 21" Sony G420, CD-R burner, 3.25" and 5.25" Floppy Diskette Drives, ZIP 100 Diskette Drive and an IBM Model M keyboard.

User avatar
ddoodm
Curious PSXDEV User
Curious PSXDEV User
Posts: 15
Joined: Nov 18, 2012
Location: Sydney, Australia
Contact:

Post by ddoodm » June 11th, 2013, 8:37 pm

Oh, okay, yeah his plasma background is amazing!

Haha, well thanks heaps again. I will try and give it another shot though!
^____________^

User avatar
LameGuy64
Verified
Psy-Q Enthusiast
Psy-Q Enthusiast
Posts: 388
Joined: Apr 10, 2013
I am a: Hobbyist Game Developer
Motto: Commercial or not, play it!
PlayStation Model: H2000/7000
Location: Philippines
Contact:

Post by LameGuy64 » June 11th, 2013, 11:15 pm

I'll take this little snippet of yours as a reference while I'm learning how the 3D graphics system of the PlayStation works. I'm not really sure if textured quads suffer from affine texture distortion that most PlayStation games suffer from.
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
ddoodm
Curious PSXDEV User
Curious PSXDEV User
Posts: 15
Joined: Nov 18, 2012
Location: Sydney, Australia
Contact:

Post by ddoodm » June 11th, 2013, 11:54 pm

As you've probably noticed from the demo, the texture is greatly distorted as a result of affine texture mapping when you move the plane close to the viewing screen, because the texture is interpolated based on 2D screen-space coordinates. I'm not sure how feasible perspective-correct interpolation would be on the PlayStation.

Really like your winning CodeBlast entry, by the way! Especially liked the plasma business!! :)
^____________^

User avatar
LameGuy64
Verified
Psy-Q Enthusiast
Psy-Q Enthusiast
Posts: 388
Joined: Apr 10, 2013
I am a: Hobbyist Game Developer
Motto: Commercial or not, play it!
PlayStation Model: H2000/7000
Location: Philippines
Contact:

Post by LameGuy64 » June 12th, 2013, 10:42 am

One thing I know about reducing texture distortion on the PlayStation is to increase the number of polygons on the surface. I know this sounds inefficient but that's how most PlayStation games reduce texture distortion and it also helps with reducing sorting and clipping errors.

And I'm glad that you liked my little CodeBlast entry...Its just something I put together in a week to get started with programming homebrew for the PlayStation and its one of my first programs to be written in C. I never expected to win first place in the competition but I'm happy I did!
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
ddoodm
Curious PSXDEV User
Curious PSXDEV User
Posts: 15
Joined: Nov 18, 2012
Location: Sydney, Australia
Contact:

Post by ddoodm » June 12th, 2013, 11:10 am

Yeah, I was going to mention that one of the Developer Seminars suggested that we subdivide our meshes as they approach the camera. Not sure how trowing in more polys reduces distortion, but it soulds like you're correct. :)

It sure worked very well as an experiment, can't wait to see your next (hopefully 3D) project!
^____________^

Tommy
Active PSXDEV User
Active PSXDEV User
Posts: 48
Joined: Apr 19, 2014

Post by Tommy » April 22nd, 2014, 8:34 am

The PlayStation doesn't use true perspective correct texturing because it's expensive: one divide per pixel. Affine/linear interpolation eliminates that divide.

Because you specify the texture coordinates directly at vertices, they're always correct there. So the more vertices you specify, the more individual positions at which the texture coordinates are definitely correct. Arguing it another way: you're doing a bunch of extra divides outside of the pixel plotting loop because they're not going to be done inside it.

The PlayStation draws quads as two triangles. Compared with the Saturn and 3DO that can produce less pleasing results but it's primarily psychological and the decision just to optimise for triangles and let people use any old geometry is why developers loved the system back in the day. Most of those awful Saturn ports took triangular geometry and just pushed one edge of a quad down to zero length effectively to draw triangles but much more expensively. That's why Sega's own games were so much better than most of everyone else's — the hardware didn't fit well with normal industry practise but Sega were designed specifically for it.

Historical note: in PC world, the same problem was eventually 'solved' by texture mappers like those in Descent and Quake that do a correct perspective divide within the pixel plotting loop but only every 32 or 16 pixels and linearly interpolate in between. Which is a bit like automatically subdividing geometry when it gets larger but at a different place in the pipeline. Quake's particularly clever because it uses the two-piped nature of the Pentium to do the divide simultaneously with the interpolation, making it effectively free.

User avatar
MrQuetch
Active PSXDEV User
Active PSXDEV User
Posts: 42
Joined: Apr 01, 2018
I am a: Programmer and artist.
Motto: You can accomplish anything.
Location: United States

Post by MrQuetch » April 1st, 2018, 3:55 pm

I know this thread is a bit old, but my *.tim image file doesn't appear correctly, while the one in ddoom's sample does. I have used the TIMTOOL program to load my *.tim file and save the project, but now I don't know exactly where to go next. Do I copy and paste the "tim project" into my PS1 project with the source code? Thanks.

Edit: Got it working. I mixed the yellow and green squares with the blue ones - which was a silly mistake.

Post Reply

Who is online

Users browsing this forum: No registered users and 2 guests