Advertisement

Window -> AUX_RGBImageRec (here we go again....)

Started by October 20, 2000 01:24 PM
4 comments, last by IainC 24 years, 1 month ago
Hi guys Here's another one for all you hardcore Win32/OpenGL peeps... I want to create a texture from a snapshot of an active window, given a HWND. So say I did... HWND Notepad = NULL; Notepad = FindWindow("Notepad", ""); ... to get a handle on a running instance of Notepad, I want to use a snapshot of the window as a texture. Now I can use GDI to capture the window as a bitmap with something along the lines of... (code was here) ... but I haven't got a clue how to get a AUX_RGBImageRec out of it - any ideas? Of course I don't really care if the structure I'm using is a AUX_RGBImageRec or not, anything will do so long as I can pour my screenshot data into it and then use it to generate an OpenGL texture. Having struggled with this for days, I'm now ready to plead to those with superior Win32 skills. Brgrds, and thanks, IainC Edited by - IainC on 10/23/00 6:05:51 PM
[size="2"]www.coldcity.com code, art, life
Well for your case you don''t need the AUX_RGBImageRec.
Once you have the ''snapshot'' of the window, you can load it into the texture maps like so.

GLuint TextureHandle;
// Should be global or resident. If its a local function
// variable it won''t work properly.

// Bind the texture to this handle.
glBindTexture(GL_TEXTURE_2D, TextureHandle);
// Which types of filtering you want to use on the texture.
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_NEAREST);
// This is where you want to be. What you want to worry about is replacing TextureImage[0]->sizeX, and TextureImage[0]->sizeY, with the width and height of the ''snapshot'' bits. Remember, you should leave all textures to 64/128/256 sizes. Meaning 64*64 pixels, etc etc.
glTexImage2D(GL_TEXTURE_2D, 0, 3, TextureImage[0]->sizeX, TextureImage[0]->sizeY, 0, GL_RGB, GL_UNSIGNED_BYTE, TextureImage[0]->data);
// Lastly, you need to pass the actual bits..
TextureImage[0]->data represents this. The define GL_UNSIGNED_BYTE is the pixel format that the data is in. Your options are:
GL_UNSIGNED_BYTE, GL_BYTE, GL_BITMAP, GL_UNSIGNED_SHORT, GL_SHORT, GL_UNSIGNED_INT, GL_INT, and GL_FLOAT.
If your running VC you can check the MSDN help for the full function description, but the way it is is basic and should probably stay that way.
So take your bits from the ''snapshot'' and load them into there.

Now in your drawscene, you glBindTexture(GL_TEXTURE_2D, TextureHandle); DrawCube(); and you should have your texture.

This isn''t a very good explanation, nor am I a hardcore OpenGL, rather new started the other day, but from my experience from past and current, I believe it should work. Nothing lost to try it

OpenGLNewbie

-----------------------------
What doesn''t kill me today,
will be there tomorrow..
-----------------------------What doesn''t kill me today,will be there tomorrow..
Advertisement
Hmm... To get the dimensions of the snapshot bitmap you''ve gotta use this, I''m pretty sure...

RECT BitmapRect;
GetWindowRect(&BitmapRect);

Then replace

TextureImage[0]->sizeX with (BitmapRect.right - BitmapRect.left)

and TextureImage[0]->sizeY with (BitmapRect.bottom - BitmapRect.top)

I''m not sure if this will grab the window that you''ve set active, or the window that you''re coding, but it might work...

S.
Please delete your old posts, people (including me) don''t feel they need to help if they see you answering your own questions.

S.
Also, if you feel that it''s absolutely imperative that we see all of your thoughts, then please just include the lines you have changed and mention where they used to be in your original code...

S.
Sorry, didn''t mean to offend. Have removed all my posts apart from the thread start. The code was actually changing pretty radically each post. I was trying to work through it myself; I personally don''t have a lot of time for posters who give up, ask a pleading question then don''t continue to work on their own till someone else does it all for them, and don''t like to come across as such. But yeah, it was getting a bit obfuscated

Anyway, I''ve come so far and have the following:

int CTexture::CreateFromWindow(HWND hwnd)
{
hwnd = FindWindow("Shell_TrayWnd", "");

HDC hdcScreen = GetWindowDC(hwnd); // Screen DC
HDC hdcCompatible = CreateCompatibleDC(hdcScreen); // Memory DC
HBITMAP hBitmap = CreateCompatibleBitmap(hdcScreen, 20, 20);
SelectObject(hdcCompatible, hBitmap);
BitBlt(hdcCompatible, 0,0, 20, 20, hdcScreen, 0,0, SRCCOPY);

BITMAP BM;
::GetObject (hBitmap, sizeof (BM), &BM);

if (BM.bmBitsPixel != 24) // Must be 24 bit or it won''t be this simple...
{
char text[256];
sprintf(text, "Eeek! This is %d bit, not 24 bit! Giving up creation", BM.bmBitsPixel);
MessageBox (NULL, text, "CTexture::LoadTexture",MB_OK);
return FALSE;
}

glGenTextures(1, &texture); // Generate name for and bind texture.
glBindTexture(GL_TEXTURE_2D, texture);

glPixelStorei(GL_UNPACK_ALIGNMENT, 4); // Set up all the the texture mapping params.
glPixelStorei(GL_UNPACK_ROW_LENGTH, 0);
glPixelStorei(GL_UNPACK_SKIP_ROWS, 0);
glPixelStorei(GL_UNPACK_SKIP_PIXELS, 0);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER,GL_LINEAR_MIPMAP_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
gluBuild2DMipmaps( GL_TEXTURE_2D, 3, BM.bmWidth, BM.bmHeight, GL_RGB, GL_UNSIGNED_BYTE, BM.bmBits );

return TRUE;
}

... which I think may stand a fair chance of working if the screen''s set to 24 bit, but unfortunately my TNT2 M64 can''t do a 24bit mode at all (only 8, 16 or 32) so I can''t test it.

Any other mode would nullify the handy overlap I think I see between a 24bit Windows DIB and the OGL image format.

Anyway, I really need something to do this for any bitdepth...

Any help gratefully received, I''m still stressing over this.

Once again, sorry to have offended the more experienced members of the board.

Brgrds,
Iain
[size="2"]www.coldcity.com code, art, life

This topic is closed to new replies.

Advertisement