• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
EpicCupcakes

Reading Pixel Data in DirectX 9

10 posts in this topic

Hello all. I'm a very new game programmer, and I've been working on my first game for a few weeks now, but I've hit a snag. I'm trying to create boundries in my game using pixels of a certain color specified to represent impassable walls, but I don't know how to actually check the color of a pixel in a point in space.

This problem is likely due to the fact that I don't know what variables are in the LPDIRECT3DSURFACE9 object, and I don't know how that object holds whatever it does hold or anything.

My question is ths: What variable(s) in the LPDIRECT3DSURFACE9 object hold that object's pixel color data, and how can I access, test, or write to that data?

Any answers will be greatly appreciated!
0

Share this post


Link to post
Share on other sites
To get the pixel data of a IDirect3DSurface9 object (what a LPDIRECT3DSURFACE9 points to), you need to use its [url=http://msdn.microsoft.com/en-us/library/windows/desktop/bb205896%28v=vs.85%29.aspx]LockRect function[/url] which will allow you to access the data on the surface. Remember to use IDirect3DSurface9::UnlockRect when you are finished with the pixel data.
1

Share this post


Link to post
Share on other sites
[quote name='RulerOfNothing' timestamp='1331255161' post='4920549']
To get the pixel data of a IDirect3DSurface9 object (what a LPDIRECT3DSURFACE9 points to), you need to use its [url="http://msdn.microsoft.com/en-us/library/windows/desktop/bb205896%28v=vs.85%29.aspx"]LockRect function[/url] which will allow you to access the data on the surface. Remember to use IDirect3DSurface9::UnlockRect when you are finished with the pixel data.
[/quote]

So you lock a portion of the surface first, then access the pixel data, and then unlock it when you are finished, right?
Could you tell me exactly how a person should access the pixel data, if for example, you wanted to check if the point (52x, 15y, 0Z) had a black pixel?
0

Share this post


Link to post
Share on other sites
The first step is to pass a pointer to a D3DLOCKED_RECT structure to the LockRect function, like this:
[CODE]
D3DLOCKED_RECT lockedRect;
surface->LockRect(&lockedRect,NULL,0);
[/CODE]
where surface should be replaced by the surface you want to get the pixel data from. Next you need to access the raw pixel data, however this depends on what format the surface is in. Assuming 32-bit colour, you could just do this:
[CODE]
unsigned int *pixels=lockedRect.pBits;
if(pixels[52+15*width]&COLOUR_MASK==0)
{
//then the pixel at (52,15) is black
}
[/CODE]
where COLOUR_MASK is simply a representation of where the colour bytes are (so if you have a RGBA format it should be 0xFFFFFF00)
1

Share this post


Link to post
Share on other sites
[quote name='RulerOfNothing' timestamp='1331257271' post='4920554']
The first step is to pass a pointer to a D3DLOCKED_RECT structure to the LockRect function, like this:
[CODE]
D3DLOCKED_RECT lockedRect;
surface->LockRect(&lockedRect,NULL,0);
[/CODE]
where surface should be replaced by the surface you want to get the pixel data from. Next you need to access the raw pixel data, however this depends on what format the surface is in. Assuming 32-bit colour, you could just do this:
[CODE]
unsigned int *pixels=lockedRect.pBits;
if(pixels[52+15*width]&COLOUR_MASK==0)
{
//then the pixel at (52,15) is black
}
[/CODE]
where COLOUR_MASK is simply a representation of where the colour bytes are (so if you have a RGBA format it should be 0xFFFFFF00)
[/quote]

Thank you so much! You can't imagine how wonderfully useful this information is! I'll be sure to put this to good use. Thank you again for all your help!
0

Share this post


Link to post
Share on other sites
[quote name='RulerOfNothing' timestamp='1331257271' post='4920554']
The first step is to pass a pointer to a D3DLOCKED_RECT structure to the LockRect function, like this:
[CODE]
D3DLOCKED_RECT lockedRect;
surface->LockRect(&lockedRect,NULL,0);
[/CODE]
where surface should be replaced by the surface you want to get the pixel data from. Next you need to access the raw pixel data, however this depends on what format the surface is in. Assuming 32-bit colour, you could just do this:
[CODE]
unsigned int *pixels=lockedRect.pBits;
if(pixels[52+15*width]&COLOUR_MASK==0)
{
//then the pixel at (52,15) is black
}
[/CODE]
where COLOUR_MASK is simply a representation of where the colour bytes are (so if you have a RGBA format it should be 0xFFFFFF00)
[/quote]

One last question, and I'm through. When I try to compile your example, I get an error stating that "Conversion from 'void*' to pointer to non-'void' requires an explicit cast" reguarding the line:
unsigned int *pixels=lockedRect.pBits;

Do you know how I can correct this?
0

Share this post


Link to post
Share on other sites
Ah yes, you just change unsigned int *pixels=lockedRect.pBits; to unsigned int *pixels=(unsigned int*)lockedRect.pBits;. Sorry about that; it has been a while since I did this sort of thing.
0

Share this post


Link to post
Share on other sites
I should maybe just add that it's not safe to index the pixels this way. A row in the texture's memory doesn't have to be (texture_width_in_pixels * pixel_size) large, you should use lockedRect.Pitch instead.
It probably will work anyway, especially for power-of-two textures (512, 1024 etc), but you can never be really sure. And if you are using textures like 800x600 then the chance that DirectX will add some padding to the rows is very big. You may realise that a 800x600 texture in fact occupies the same memory as if it was 1024x600, with 224 unused blank "pixels" added to every row - then width of the texture is only 800 but pitch is (1024 * number_of_bytes_per_pixel).
0

Share this post


Link to post
Share on other sites
[quote name='Tom KQT' timestamp='1331291367' post='4920633']
I should maybe just add that it's not safe to index the pixels this way. A row in the texture's memory doesn't have to be (texture_width_in_pixels * pixel_size) large, you should use lockedRect.Pitch instead.
It probably will work anyway, especially for power-of-two textures (512, 1024 etc), but you can never be really sure. And if you are using textures like 800x600 then the chance that DirectX will add some padding to the rows is very big. You may realise that a 800x600 texture in fact occupies the same memory as if it was 1024x600, with 224 unused blank "pixels" added to every row - then width of the texture is only 800 but pitch is (1024 * number_of_bytes_per_pixel).
[/quote]

I think I see what you mean, here. My program is now compiling correctly, but when I try to access the pixel's memory, I get an access violation message. Could you give an example as to how one would use lockedRect.Pitch to find out, for example, if a pixel at (64x, 14y) is black?
0

Share this post


Link to post
Share on other sites
You should very rearly (never) read any data back to system memory from gpu. And if you do, you want to reduce the data to minimal and do it asynchonously. One example I've encountered is reading a 1x1 pixel for current screen luminance, and asynchronously if possible.. The impassable walls you want is easily stored in system memory.

Any simulation you want to do with GPU better stay at there.
0

Share this post


Link to post
Share on other sites
[quote name='EpicCupcakes' timestamp='1331333596' post='4920786']
I think I see what you mean, here. My program is now compiling correctly, but when I try to access the pixel's memory, I get an access violation message. Could you give an example as to how one would use lockedRect.Pitch to find out, for example, if a pixel at (64x, 14y) is black?
[/quote]

Try this for example http://www.toymaker.info/Games/html/direct3d_faq.html#D3D9
They are reading all pixels in the texture in two for loops, so it's easy to convert it into reading just one specific pixel.
0

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0