Advertisement Jump to content
Sign in to follow this  
Kelly Smith

OpenGL Screen Capture, Modify, and Draw Question

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

Hello Everyone,
 
I am building an application that needs to capture the current screen, perform pixel-wise analysis of the captured data, and ideally draw a modified version of the data back onto the screen.  Currently I have successfully found a way to capture pixel data from the screen and store it in memory using DirectX 9 and accessing the front buffer.  I am not necessarily married to DirectX 9, however it has the advantage of being already installed on the target audience's computers.
 
// Requires the D3D9 device to be initialized.
void grabScreen(const int WIDTH, const int HEIGHT, BYTE* pBits){

	d3ddev->CreateOffscreenPlainSurface(WIDTH, HEIGHT,
		D3DFMT_A8R8G8B8, D3DPOOL_SCRATCH, &pSurface, NULL);
	d3ddev->GetFrontBufferData(0, pSurface);
	//D3DXSaveSurfaceToFile(L"Desktop.jpg", D3DXIFF_JPG, pSurface, NULL, NULL);


	D3DLOCKED_RECT lockedRect;
	pSurface->LockRect(&lockedRect, NULL,
		D3DLOCK_NO_DIRTY_UPDATE |
		D3DLOCK_NOSYSLOCK | D3DLOCK_READONLY);
	for (int i = 0; i < HEIGHT; i++)
	{
		memcpy((BYTE*)pBits + i * WIDTH * 32 / 8,
			   (BYTE*)lockedRect.pBits + i * lockedRect.Pitch,
			   WIDTH * 32 / 8);
	}
	pSurface->UnlockRect();

}
 
Next I pass *pBits through my analysis and modification routines to get what I would like to then be drawn back to the screen in some capacity.  I am not looking to replace the screen necessarily, and am in fact operating on a subset of the actual screen.  I have been looking all over the internet for a way to draw this pixel map back to the screen, however I cannot find a way.  In the past, I have used a Pixel Buffer Object in OpenGL to achieve a similar result, but cannot find a way to do it in DirectX.
 
My initial thought was to put it back into a surface and then render that surface:
bool ScreenManager::generateSurface(BYTE *pData, UINT width, UINT height, IDirect3DSurface9* pSurface){

	LPDIRECT3DDEVICE9 d3ddev = *this->device;

	d3ddev->CreateOffscreenPlainSurface(width, height,
		D3DFMT_A8R8G8B8, D3DPOOL_SCRATCH, &pSurface, NULL);

	D3DLOCKED_RECT lockedRect;
	pSurface->LockRect(&lockedRect, NULL,
		D3DLOCK_NOSYSLOCK);

	int bytesPerPixel = pixelSize / 8;
	for (int i = 0; i < height; i++)
	{
		memcpy(
			(BYTE*)lockedRect.pBits + i * lockedRect.Pitch,
			(BYTE*)pData + i * width * bytesPerPixel,
			width * bytesPerPixel);
	}

	pSurface->UnlockRect();

	return true;
}
But, I cannot find a way to directly render a surface.   I think I am probably missing some fundamental understanding of how DirectX works, and I apologize for my ignorance.
 
Any and all help would be greatly appreciated!
 
Thanks,
Kelly

Share this post


Link to post
Share on other sites
Advertisement

hi,

I wrote an application that take the screen and did some extra work to the pixels and then render it to the target,but i'm using directx 11.In Dx11 this work is very simple, u must create a resource(texture) and call map then do your works over pixels and then unmap the resource.

i hope this helps u.

Share this post


Link to post
Share on other sites
Unfortunately, I don't think that I can require DX11 for the application, however the concept of using a texture to display my pixels on an arbitrary plane might be the right way to accomplish this. I will see if I can find a way to do this in DX9. Thanks for the tip smile.png

Share this post


Link to post
Share on other sites

Reading back GPU-written data on he CPU kill the parallelism between the two processors, and causes both of them to stall for long periods of time. It's a great way to absolutely kill your peformance.

 

What are you actually trying to accomplish here?

Share this post


Link to post
Share on other sites

Reading back GPU-written data on he CPU kill the parallelism between the two processors, and causes both of them to stall for long periods of time. It's a great way to absolutely kill your peformance.

 

What are you actually trying to accomplish here?

I want to write some date to a texture, what is the best procedure that i do not need to reading back GPU-written data?

Thanks.

Share this post


Link to post
Share on other sites

Reading back GPU-written data on he CPU kill the parallelism between the two processors, and causes both of them to stall for long periods of time. It's a great way to absolutely kill your peformance.
 
What are you actually trying to accomplish here?


I need the raw pixel data of the screen so that I can perform real-time image analysis on it. Perhaps at some point I could optimize with a GPGPU solution like OpenCL and try to avoid the performance hit of copying memory between the device twice, but currently it is inherent to the problem at hand. Essentially, I am doing real time image recognition and tracking. Achieving sustained 30fps would be awesome, but not absolutely necessary.

I found a less than perfect solution by hooking my DX9 code up to a C# WPF application with a DLL and can easily convert my BYTE[] into a Bitmap and display it on the screen. I am easily achieving 30 frames per second right now, but have not created all of my image processing yet. Ideally, I would like to do the rendering by directly working with DX9 from the DLL and just using the WPF window for display, but this has allowed me to continue prototyping the other parts of my application in the meantime.

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.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!