Archived

This topic is now archived and is closed to further replies.

would this be the corect way to draw to a IDirect3DTexture8

This topic is 5434 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I need to load PCX files to an IDirect3DTexture8, so far I have only found fairly vaige references on how to do this, so I am wondering is this the corect method for geting pixel(X,Y) of a IDirect3DTexture8 set to the color of my chooseing?
  if (D3D_OK != D3DXCreateTexture(g_pDevice, x, y, 1, 0, D3DFMT_A8R8G8B8, D3DPOOL_MANAGED, &d3dtexture))MessageBox(NULL, "texture creation failed", "!", MB_OK);

	D3DLOCKED_RECT img_rec;

	D3DSURFACE_DESC surf_desc;

	d3dtexture->GetLevelDesc(0,&surf_desc);

	if(surf_desc.Format == D3DFMT_A8R8G8B8){
//d3dtexture->UnlockRect(0);

	if (D3D_OK != d3dtexture->LockRect(0, &img_rec, NULL, D3DLOCK_NOSYSLOCK ))MessageBox(NULL, "lock failed", "!", MB_OK);

	//DWORD color;

	struct temp_color{
		BYTE b;
		BYTE g;
		BYTE r;
		BYTE a;
	} *pix;

	temp_color temp;
	temp.b = 128;
	temp.g = 128;
	temp.r = 128;
	temp.a = 128;

//	DWORD *pix;

//	DWORD temp = 0;

	pix = (temp_color *)img_rec.pBits;
	int pitch = img_rec.Pitch;
//	pix[0] = 0;

	int place = 0;
	for(i = 0; i<y; i++){
		for(j = 0; j<x; j++){
			place = (i*pitch)+j;
			pix[place] = temp;
		}
	}

	d3dtexture->UnlockRect(0);

	}
  
ok lets just get the thing filled with a color first so someone give me code that would fill out a IDirect3DTexture8 (of x,y dimentions) with grey pixels, includeing the ability to set a specific pixel to any arbitrary color [edited by - Bobboau on January 28, 2003 2:16:12 PM]

Share this post


Link to post
Share on other sites
ok, here is the entire function,

    void load_pcx( char *file_n, IDirect3DTexture8 *d3dtexture){

int x, y, i, size = 0, imgsize = 0;

CString pcx_string;
pcx p_file;
pcx *pcx_file = &p_file;
char file_name[128];
BYTE *image;
char c;
strcpy(file_name, file_n);
strcat(file_name, ".pcx");

FILE *pcxfile=fopen(file_name, "rb");

if(!pcxfile){
pcx_string.Format("could not load the file %s", file_name);
MessageBox(NULL, pcx_string, "!", MB_OK);
return;
}

//get the length of the file

while(!feof(pcxfile))
{
fread(&c, sizeof(char), 1, pcxfile);
size++;
if(size > 10000000)return;
}
size--;
size -= (768+128);
//get the length of the image data

fclose(pcxfile);

pcxfile=fopen(file_name, "rb");

if(!pcxfile){
pcx_string.Format("could not load the file %s", file_name);
MessageBox(NULL, pcx_string, "!", MB_OK);
return;
}
pcx_file->image = new BYTE[size];

fread(&pcx_file->head, sizeof(char), 128, pcxfile);
fread(pcx_file->image, sizeof(char), size, pcxfile);
fread(&pcx_file->palette, sizeof(palette), 1, pcxfile);

x = pcx_file->head.Xmax - pcx_file->head.Xmin + 1;
y = pcx_file->head.Ymax - pcx_file->head.Ymin + 1;
imgsize = x*y;
image = new BYTE[imgsize+1];

BYTE rep;
int offset = 0;

int j = 0;

//the decompression loop

while((offset <= size) && (j <= imgsize)){
if(x > pcx_file->head.BytesPerLine){
if(j%x == 1)//if we've hit the end of the line

offset++;//skip one

}

if(pcx_file->image[offset] > 192){//if the first two bits are set, the next six show how many times to repete the next byte

rep = (pcx_file->image[offset++] - 192 );
for(i = 0; i< rep; i++){
image[j++] = pcx_file->image[offset];
if((offset >= size) || (j > imgsize))break;
}
offset++;
}else{
image[j++] = pcx_file->image[offset++];
if((offset >= size) || (j > imgsize))break;
}
}

delete(pcx_file->image);
// int X = 7, Y = 16, I=(Y*x)+X ;

// pcx_string.Format("pixel %d,%d, pixel no. %d in %s is %d", X, Y, I, file_name, image[I]);

// MessageBox(NULL, pcx_string, "!", MB_OK);

// }


if (D3D_OK != D3DXCreateTexture(g_pDevice, x, y, 1, 0, D3DFMT_A8R8G8B8, D3DPOOL_MANAGED, &d3dtexture))MessageBox(NULL, "texture creation failed", "!", MB_OK);

D3DLOCKED_RECT img_rec;

D3DSURFACE_DESC surf_desc;

d3dtexture->GetLevelDesc(0,&surf_desc);

if(surf_desc.Format == D3DFMT_A8R8G8B8){
if (D3D_OK != d3dtexture->LockRect(0, &img_rec, NULL, D3DLOCK_NOSYSLOCK ))MessageBox(NULL, "lock failed", "!", MB_OK);

DWORD *pix;
pix = (DWORD *)img_rec.pBits;
int pitch = img_rec.Pitch;
int place = 0;
for(int I = 0; I<y; I++){
for(int J = 0; J<x; J++){
place = (I*(pitch/sizeof(DWORD)))+J;
pix[place] = D3DCOLOR_XRGB(pcx_file->palette.rgb[image[(I*x)+J]].r,pcx_file->palette.rgb[image[(I*x)+J]].g,pcx_file->palette.rgb[image[(I*x)+J]].b);;
}
}

d3dtexture->UnlockRect(0);

}

delete(image);

fclose(pcxfile);
}

ok it, crashes any time I try to set this as the texture, I have no idea if i have done any of this corectly, but as far as I can tell my PCX reading is corect, and I have followed the few directions for doing this sort of thing

[edited by - Bobboau on January 29, 2003 2:06:14 AM]

Share this post


Link to post
Share on other sites
Remember that, when you create a texture, almost all video cards will insist on making the texture dimensions powers of 2 so that, if you ask for a texture that is 35x100, it will create one which is 64x128.

Try it like this....


      if (D3D_OK != D3DXCreateTexture(g_pDevice, x, y, 1, 0, D3DFMT_A8R8G8B8, D3DPOOL_MANAGED, &d3dtexture))MessageBox(NULL, "texture creation failed", "!", MB_OK);

D3DLOCKED_RECT img_rec;

D3DSURFACE_DESC surf_desc;

d3dtexture->GetLevelDesc(0,&surf_desc);
int iTexWidth = surf_desc.Width; // This may be larger than x

int iTexHeight = surf_desc.Height; // This may be larger than y


if(surf_desc.Format == D3DFMT_A8R8G8B8){
if (D3D_OK != d3dtexture->LockRect(0, &img_rec, NULL, D3DLOCK_NOSYSLOCK ))MessageBox(NULL, "lock failed", "!", MB_OK);

struct temp_color{
BYTE b;
BYTE g;
BYTE r;
BYTE a;
} *pix;

temp_color temp;
temp.b = 128;
temp.g = 128;
temp.r = 128;
temp.a = 128;

pix = (temp_color *)img_rec.pBits;
int place = 0;
for(i = 0; i<y; i++){
for(j = 0; j<x; j++){
place = (i*iTexWidth)+j;
pix[place] = temp;
}
}

d3dtexture->UnlockRect(0);

}


You should also pay attention to the surf_desc.Width and surf_desc.Height values when setting up the texture-mapping coordinates.

[edited by - Dov Sherman on January 29, 2003 12:05:38 PM]

Share this post


Link to post
Share on other sites
so I should _NOT_ be basing the vertical position on the pitch/bytes per pixel, this is the oposite of just about everything I''ve read on this so far,
and it doesn''t seem to be working,
are there any things about the odd sized textures i should know about becase I will need to handel that

Share this post


Link to post
Share on other sites
well I''ve sort of gotten it to work, I had to pull it out of it''s own function and incert it into the function that was loading the filenames and such, and it''s so ugly and foul looking it almost makes me cry.
but it is sort of loading now, for some reason when it was broken off into it''s own function it wasn''t doing what it was suposed to...

anyway it looks like it does not indeed like oddly shaped textures, I ask for a texture that is 56X110 and I get one 64X128 (as was pointed out earlier) is there a way to get this to fit the textures I need or am I going to have to (ugg..) manualy write code to resize it into a power of 2 texture, please tell me there is just some flag I have to change

Share this post


Link to post
Share on other sites
Instead of resampling the image, how about changing your texture coordinates?

Instead of using u and v (or whatever variables you use for the texture coordinates) from range 0,0 -> 1,1, how about using tex coords from

0,0 -> 56/64, 110/128 ?

That''ll map just the part of the texture that is relevant to the ... whatever it is you''re mapping the texture to. It''ll ignore the extra little border bit around the edges.


Hope that helps/makes sense...I''m half-asleep as I''m typing this, so if it comes out unintelligable, I apologize



Josh

Share this post


Link to post
Share on other sites
well the problem is that many of the textures are tiled, so that won''t work,
useing the loadtexturefromfile it would load odd sized textures fine, so does it resize the immage when it loads it or is there something else

Share this post


Link to post
Share on other sites
well I''ve gotten it to load irregularly sized images, I did have to resample the immage to fit, but I have one last problem, I don''t think I''m decoding odd widthed PCX files corectly,
what am I suposed to do with the last few bytes

Share this post


Link to post
Share on other sites
never mind I figured it out

  					if(((j)%x == 0) && (j>0)){//if we''ve hit the end of the line

offset++;
if(!bts==((bts/2)*2)){
offset++;
offset+=bts-pcx_file->head.BytesPerLine;
}
bts=0;
}else{
bts++;
}

Share this post


Link to post
Share on other sites