Page 1 of 1

Can anyone help me with textures and displaying it?

Posted: May 16th, 2013, 3:39 pm
by LameGuy64
I've been trying to upload and draw a texture but it doesn't seem to work for me. I don't really know why and I doubt anyone can help because those who know how to properly upload and display a texture have probably left this forum by now.

What I wanted to do is to be able to upload an 8-bit 160x120 texture from a 2-dimensional array which I'll then draw as a 320x240 sprite to fill up the entire screen. The problem is, I can't get it to work.

This is for my little work-in-progress side-project I call M-Demo (which will be like a cracktro only with CDDA music). The texture thing which I'm having trouble with is for the real-time 8-bit plasma background.

Compile the following source code with PsyQ...

makefile.mak:

Code: Select all

all:
   ccpsx -O -Xo$80010000 main.c -omain.cpe,main.sym

for_menu:
   ccpsx -O -Xo$80010000 main.c \psx\lib\none2.obj -oarot.cpe
main.c:

Code: Select all

// Includes
#include <sys/types.h>      
#include <libetc.h>      
#include <libgte.h>
#include <libgpu.h>
#include <libgs.h>
#include <stdio.h>


// Some definitions
#define OT_LENGTH 10

#define PACKETMAX (2048)      
#define PACKETMAX2 (PACKETMAX*24)   


// Object tables
GsOT myOT[2];
GsOT_TAG myOT_TAG[2][1<<OT_LENGTH];
PACKET GPUPacketArea[2][PACKETMAX2];



// Plasma tables
#include "PlasmaCLUT.h"


// Prototypes
int main(void);
void Init(void);
void Display(int ActiveBuffer);


// Main procedure
int main(void)
{


	int ActiveBuffer=0;
	u_short OTpos=0;


	// For the plasma texture stuff
	short px;
	short py;
	unsigned char pBuff[159][119]; // Enough for a 160x120 8-bit texture
	u_short pClutID;
	u_short pTexID;

	RECT pTex;
	GsSPRITE pSprite;


	// Init stuff
	Init();



	/* Plasma effect notes:
		320, 0, P5 - Texture coords
		320, 255   - CLUT coords
	*/ 


	// This is where I'm having trouble with
	pSprite.attribute = 1 * (2 ^ 24); // Set bits 24-25 to 1 for 8-bit CLUT texture
	pSprite.x 	= 0;
	pSprite.y 	= 0;
	pSprite.w 	= 160;
	pSprite.h 	= 120;
	pSprite.tpage 	= 5;
	pSprite.cx 	= 320;
	pSprite.cy	= 256;

	pSprite.r 	= 128;
	pSprite.g	= 128;
	pSprite.b	= 128;
	pSprite.scalex	= 4096;
	pSprite.scaley	= 4096;


	// Fractal texture coordinates (for LoadImage)
	pTex.x = 320;
	pTex.y = 0;
	pTex.w = 80;
	pTex.h = 120;


	// Load the CLUT
	pClutID = LoadClut((u_long*)&PlasmaCLUT[0], 320, 256);

	// Draw a fractal test pattern
	for (py=0; py<120; py=py+1)
	{
		for (px=0; px<160; px=px+1)
		{
			pBuff[px][py]=px|py;
		}
	}

	// Upload the procedurally generated fractal texture
	pTexID = LoadTPage((u_long*)&pBuff[0][0], 1, 0, 320, 0, 160, 120); // This doesn't seem to work
	//result = LoadImage(&pTex, (u_long*)&pBuff[0][0]); // This doesn't seem to work as well

	// I can't tell because stupid ePSXe doesn't let you view the entire framebuffer.


	// Main loop
	while (1)
	{
		ActiveBuffer = GsGetActiveBuff();
		GsSetWorkBase((PACKET*)GPUPacketArea[ActiveBuffer]);
		GsClearOt(0, 0, &myOT[ActiveBuffer]);


		// Print some debugging junk
		FntPrint("Buffer=%d\n", ActiveBuffer);
		FntPrint("TexID=%d\n", pTexID);
		FntPrint("ClutID=%d\n", pClutID);


		// Draw the fractal texture (it doesn't work...I don't know why)
		GsSortSprite(&pSprite, &myOT[ActiveBuffer], OTpos);
		OTpos = OTpos + 1;


		// Display and reset stuff for the next frame
		Display(ActiveBuffer);
		OTpos=0;

	}

}


// Init
void Init(void)
{

	long result=0;

	// Initialize pads
	PadInit(0);

	// Initialize the graphics
	GsInitGraph(320, 240, GsNONINTER|GsOFSGPU, 1, 0);
	GsDefDispBuff(0, 0, 0, 240);

	myOT[0].length = OT_LENGTH;
	myOT[1].length = OT_LENGTH;
	myOT[0].org = myOT_TAG[0];
	myOT[1].org = myOT_TAG[1];

	GsClearOt(0, 0, &myOT[0]);
	GsClearOt(0, 0, &myOT[1]);

	ResetGraph(0);
	SetVideoMode(MODE_NTSC);
	SetGraphDebug(0);

	// Initialize debug font
	FntLoad(960, 256);
	FntOpen(0, 0, 320, 240, 0, 512);

}


void Display(int ActiveBuffer)
{

	FntFlush(-1);

	DrawSync(0);

	VSync(0);
	GsSwapDispBuff();
	GsSortClear(0, 70, 0, &myOT[ActiveBuffer]);
	GsDrawOt(&myOT[ActiveBuffer]);

}
PlasmaCLUT.h:

Code: Select all

unsigned short PlasmaCLUT[] = {
	0x0000,
	0x0420,
	0x0840,
	0x0C61,
	0x1081,
	0x14A1,
	0x18C2,
	0x1CE2,
	0x2522,
	0x2943,
	0x2D63,
	0x3183,
	0x35A4,
	0x39C5,
	0x3DE5,
	0x4205,
	0x4626,
	0x4A46,
	0x4E66,
	0x5287,
	0x56A7,
	0x5AC7,
	0x5AC8,
	0x5EE8,
	0x6308,
	0x6729,
	0x6B49,
	0x6B4A,
	0x6F6A,
	0x6F6A,
	0x738B,
	0x738B,
	0x77AC,
	0x7BCC,
	0x7BCD,
	0x7BCD,
	0x7BCD,
	0x7FEE,
	0x7FEE,
	0x7FEE,
	0x7FEF,
	0x7FEF,
	0x7FEF,
	0x7FEF,
	0x7FF0,
	0x7FF0,
	0x7FF0,
	0x7FF1,
	0x7FF1,
	0x7BD1,
	0x7BD2,
	0x7BD2,
	0x7BD2,
	0x77B3,
	0x77B3,
	0x7393,
	0x6F74,
	0x6F74,
	0x6B54,
	0x6B54,
	0x6735,
	0x6315,
	0x6315,
	0x5EF6,
	0x5AD6,
	0x56B6,
	0x5296,
	0x4E77,
	0x4A57,
	0x4637,
	0x4218,
	0x4218,
	0x39D8,
	0x35B8,
	0x3199,
	0x2D79,
	0x2959,
	0x2539,
	0x211A,
	0x1CFA,
	0x14BA,
	0x109A,
	0x0C7B,
	0x085B,
	0x043B,
	0x001B,
	0x043B,
	0x085B,
	0x0C7C,
	0x14BC,
	0x18DC,
	0x1CFC,
	0x211C,
	0x253C,
	0x295D,
	0x2D7D,
	0x35BD,
	0x39DD,
	0x3DFD,
	0x3DFE,
	0x421E,
	0x463E,
	0x4A5E,
	0x4E7E,
	0x529E,
	0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x6729,
	0x6309,
	0x5EE8,
	0x5AC8,
	0x56A8,
	0x5287,
	0x5287,
	0x4E67,
	0x4A46,
	0x4626,
	0x4206,
	0x3DE5,
	0x39C5,
	0x35A4,
	0x3184,
	0x2943,
	0x2523,
	0x2103,
	0x1CE2,
	0x18C2,
	0x14A1,
	0x0C61,
	0x0841,
	0x0420,
	0x0000,
	0x0000
};

Re: Can anyone help me with textures and displaying it?

Posted: May 16th, 2013, 8:46 pm
by Orion_
There are many C language syntax error in your program:
First, unsigned char pBuff[159][119]; // Enough for a 160x120 8-bit texture
is not correct, it should be pBuff[160][120];

This pSprite.attribute = 1 * (2 ^ 24); is not correct either,
'^' is not a "power of" in C
'^' is xor.
so use: pSprite.attribute = 1 << 24;

Also, initialise, pSprite.u and pSprite.v to 0

Don't hardcore the tpage: use: GetTPage(1/*8bits*/, 0, pTex.x, pTex.y);

And then use LoadImage

Re: Can anyone help me with textures and displaying it?

Posted: May 17th, 2013, 1:29 am
by LameGuy64
Then how can I draw the tpage? GsSortSprite still does nothing to the screen.

Re: Can anyone help me with textures and displaying it?

Posted: May 17th, 2013, 4:22 am
by Orion_
I just tried and it's working for me on epsxe.

I corrected some little thing on the order of x/y of your texture tab.

Code: Select all

   int ActiveBuffer=0;
   u_short OTpos=0;


   // For the plasma texture stuff
   short px;
   short py;
   unsigned char pBuff[120][160]; // Enough for a 160x120 8-bit texture
   u_short pClutID;
   u_short pTexID;

   RECT pTex;
   GsSPRITE pSprite;


   // Init stuff
   Init();



   /* Plasma effect notes:
	  320, 0, P5 - Texture coords
	  320, 255   - CLUT coords
   */


   // This is where I'm having trouble with
   pSprite.attribute = 1 << 24; // Set bits 24-25 to 1 for 8-bit CLUT texture
   pSprite.x    = 0;
   pSprite.y    = 0;
   pSprite.w    = 160;
   pSprite.h    = 120;
   pSprite.tpage    = GetTPage(1, 0, 320, 0);
   pSprite.cx    = 320;
   pSprite.cy   = 256;

   pSprite.r    = 128;
   pSprite.g   = 128;
   pSprite.b   = 128;
   pSprite.scalex   = 4096;
   pSprite.scaley   = 4096;


   // Fractal texture coordinates (for LoadImage)
   pTex.x = 320;
   pTex.y = 0;
   pTex.w = 80;
   pTex.h = 120;


   // Load the CLUT
   pClutID = LoadClut((u_long*)&PlasmaCLUT[0], 320, 256);

   // Draw a fractal test pattern
   for (py=0; py<120; py++)
   {
	  for (px=0; px<160; px++)
	  {
		 pBuff[py][px]=px|py;
	  }
   }

Re: Can anyone help me with textures and displaying it?

Posted: May 17th, 2013, 10:02 am
by LameGuy64
It finally works! Thanks for the help!

All I had to do now is fix the CLUT for the plasma texture (I got the color bits incorrectly ordered) and port my high-speed plasma routine.

Re: Can anyone help me with textures and displaying it?

Posted: May 18th, 2013, 7:25 am
by inc^lightforce
when it's done, please show us a video :) --> or publish the psx exe .

Re: Can anyone help me with textures and displaying it?

Posted: May 18th, 2013, 11:34 pm
by LameGuy64
No problem! Give me 1 or 2 days before release.

Its just a cute little demo with some graphical potential tacked onto it (real-time plasma!) to show that my production label, Meido-Tek Productions, enters the PSX-DEV scene.

Re: Can anyone help me with textures and displaying it?

Posted: May 21st, 2013, 10:16 pm
by bizarro2011
maybe you can help me.
you created it.

unsigned short PlasmaCLUT[] = {
0x0000,

need to create it.

unsigned long PlasmaCLUT[] = {
0x04210000,

you can explain to me how it's done.

Re: Can anyone help me with textures and displaying it?

Posted: May 21st, 2013, 11:48 pm
by LameGuy64
You don't need to convert the data values to unsigned long in order to be able to upload it to the framebuffer...Just pass the pointer to the array as (u_long*)&YourArray[0] and should work on all variable types provided that the size of the array in bytes meets to what the procedure needs. Also, the PlasmaCLUT array in my demo is generated by a program I wrote in QB64 for the plasma background...Each array element is a SRGB color component (1 bit transparency, 5 bits red, 5 bits green, 5 bits blue).

Variable type size reference:
char - 1 byte.
short - 2 bytes.
int/long/float - 4 bytes.

The same sizes applies to the unsigned counterparts of the variables.


Example pointer passing:

Code: Select all

pClutID = LoadClut((u_long*)&PlasmaCLUT[0], 320, 256);

Re: Can anyone help me with textures and displaying it?

Posted: May 23rd, 2013, 2:51 am
by bizarro2011
Here's what I do.

works well.

extern u_long mat0[]; <----- image Tim here. (I do not know create)

tpage[0] = LoadTPage(mat0+0x80, 0, 0, 640, 0, 256,256);
clut[0] = LoadClut(mat0, 0,500);


-------------------------x-----------------------------------
just does not work.

extern unsigned char mat0[]; <------ image Tim here. (know to create)

tpage[0] = LoadTPage(mat0+0x80, 0, 0, 640, 0, 256,256);
clut[0] = LoadClut(mat0, 0,500);

Re: Can anyone help me with textures and displaying it?

Posted: May 23rd, 2013, 10:41 am
by LameGuy64
Like I said, pass the array pointers as (u_long*).

So your code from this:

Code: Select all

extern unsigned char mat0[];

tpage[0] = LoadTPage(mat0+0x80, 0, 0, 640, 0, 256,256);
clut[0] = LoadClut(mat0, 0,500);
Should be this:

Code: Select all

extern unsigned char mat0[];

tpage[0] = LoadTPage((u_long*)mat0[0x80], 0, 0, 640, 0, 256,256);
clut[0] = LoadClut((u_long*)mat0[0], 0,500);

Re: Can anyone help me with textures and displaying it?

Posted: May 23rd, 2013, 11:59 am
by bizarro2011
error
cast to pointer from integer of different size.

what can be?

Re: Can anyone help me with textures and displaying it?

Posted: May 23rd, 2013, 4:51 pm
by LameGuy64
Oops, I've made a mistake on the corrected code...You're supposed to add a & before the array name when passing an array with an index.

So, it should be like this:

Code: Select all

extern unsigned char mat0[];

tpage[0] = LoadTPage((u_long*)&mat0[0x80], 0, 0, 640, 0, 256,256);
clut[0] = LoadClut((u_long*)&mat0[0], 0,500);

Re: Can anyone help me with textures and displaying it?

Posted: May 24th, 2013, 1:35 pm
by bizarro2011
Now is working.
thank you.