Jump to content
  • Advertisement
Sign in to follow this  
majak

Rendering from Array

This topic is 4856 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

Hi I have this array: const int nb = 786432; PUCHAR pixelBuffer = new unsigned char[nb]; //the size nb is this large because the array contains 512*512 pixels, but each pixel has it's Red, Green and Blue in separate elements. So the first element is the first pixels's red component and the next is the first pixels' second component and so forth. I can't change the array, because it's an array I get from another program, so my question is how do I render this in DirectX? I've created a texture to show the image, but if I send the array as it is now, I get some very wrong things on the screen!! I do like this, I don't know if there's a better way..
   D3DLOCKED_RECT RC;
   Texture->LockRect(0, &RC, 0, 0);
   unsigned char* Pixels = (unsigned char*)RC.pBits;
   unsigned char* DPixels = (unsigned char*)Pixels;
for(unsigned int k=0; k<nb; k++){
   *DPixels++ = pixelBuffer[k];
   Pixels += RC.Pitch;
}//end for

   Texture->UnlockRect(0);

How do I cast the array, so DirectX get the correct data to show the correct picture? Thanks =)

Share this post


Link to post
Share on other sites
Advertisement
Create a new "correct" array/structure from the recieved array, and copy all data to it accordingly.

EDIT: the correct array in this source would e.g. be the buffer gotten from the Lock-call.

Share this post


Link to post
Share on other sites
I don't know what you mean by this: EDIT: the correct array in this source would e.g. be the buffer gotten from the Lock-call.

but would the correct array look like this?


struct Color
{
UCHAR red, green, blue;
};
int nbPixels = 512*512;

Color Color_Data[nbPixels];



How do I copy from the original array into this?
And can DirectX undestand this array?

Share this post


Link to post
Share on other sites
When you lock a texture in DirectX, it expects a certain format that you have specified when creating the texture buffer... just take the values from your array, and fill them into the texture buffer accordingly that is, where the red component should be, you put your red component, if there is alpha, you do what is appropriate with it and so on.

There isn't any magic about using a recieved array to insert into a texture, other than inserting your very own.

Share this post


Link to post
Share on other sites
Quote:
Original post by Syranide
When you lock a texture in DirectX, it expects a certain format that you have specified when creating the texture buffer... just take the values from your array, and fill them into the texture buffer accordingly that is, where the red component should be, you put your red component, if there is alpha, you do what is appropriate with it and so on.

There isn't any magic about using a recieved array to insert into a texture, other than inserting your very own.


I've created my texture like this:
HRESULT Result = D3DXCreateTexture(D3D_Device, the_width, the_height, 1, 0, D3DFMT_R8G8B8, D3DPOOL_MANAGED, &Texture);

I really don't undestand where and how I should put what..

Share this post


Link to post
Share on other sites
You specify a color format for the texture when creating it. In your case it´s R8G8B8, which means the first 8 bit are the R component, next 8 bit G, last 8 bit B component. So you should copy your incoming array into an array of according colors I guess.... never done that before and I am quite new to C++, but you should be able to use D3DXCOLORs for that, using the 3 integers for every pixel from your array to create the according color in a new array.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
What kind of texture do you obtain? Could you post a screenshot?

Anyway, the texture's true width is not necessarily the one you specify, it can be larger. That maybe why it doesn't work. You could try this.



for(unsigned int i=0; i<the_height; i++)
{
DPixels = Pixels;
for(unsigned int j=0; j<the_height; j++)
*DPixels++ = pixelBuffer[k];
Pixels += RC.Pitch;
}



Share this post


Link to post
Share on other sites
This is what I get when using the piece of code you wrote:
An unhandled exception of type 'System.NullReferenceException' occurred in CreateTexture.exe

Additional information: Object reference not set to an instance of an object.

I don't get, that after I have locked the texture, I have to go through every pixel, and then the height twize??

What is RC.pBits and RC.Pitch exactly?


const int nb = 786432;
PUCHAR pixelBuffer = new unsigned char[nb];
int var = retrieveAndRender(pixelBuffer, the_Device);

D3DLOCKED_RECT RC;
Texture->LockRect(0, &RC, 0, 0);
unsigned char* Pixels = (unsigned char*)RC.pBits;
unsigned char* DPixels = (unsigned char*)Pixels;

for(unsigned int k=0; k<nb; k++){
for(unsigned int i=0; i<the_height; i++){
DPixels = Pixels;
for(unsigned int j=0; j<the_height; j++)
*DPixels++ = pixelBuffer[k];
Pixels += RC.Pitch;
}
Texture->UnlockRect(0);
}




[Edited by - majak on July 22, 2005 3:53:56 AM]

Share this post


Link to post
Share on other sites
If you're still having problems, here's an idea.

You say your array is a list of unsigned chars with a predefined size each three elements in the array representing the channels of a color (r,g,b) in this exact order. Also your texture is 512x512 D3DFMT_R8G8B8. In this case all you need to do is.

D3DLOCKED_RECT r;
texture->LockRect(0, &r, 0, 0);
memcpy(r.pBits, pixelBuffer, nb);
texture->UnlockRect(0);

Hope it helps.

Mihai Gabriel Ailioai
Senior Software Developer
Digini Inc.
www.digini.com

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
There was a typo.(it was the_width in the second loop)
You don't have to loop through all pixels as a linear array.


About Pitch and pBits.

Pitch will specify the real width of your locked rectangle and may be different from the_width.


const int nb = 786432;
PUCHAR pixelBuffer = new unsigned char[nb];
int var = retrieveAndRender(pixelBuffer, the_Device);

D3DLOCKED_RECT RC;
Texture->LockRect(0, &RC, 0, 0);
unsigned char* Pixels = (unsigned char*)RC.pBits;
unsigned char* DPixels = (unsigned char*)Pixels;


for(unsigned int i=0; i<the_height; i++){
DPixels = Pixels;
for(unsigned int j=0; j<the_width; j++)
*DPixels++ = pixelBuffer[k];
Pixels += RC.Pitch;
}
Texture->UnlockRect(0);
}

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!