Sign in to follow this  
Jacob Roman

OpenGL Using DirectShow video on an OpenGL polygon.

Recommended Posts

Jacob Roman    120
Is it possible to use DirectShow to somehow make a video be displayed on a polygon that was created using OpenGL. If it is possible, how would I do it? Links and source code would help a lot. Thanks.

Share this post


Link to post
Share on other sites
bzroom    647
Use a sample grabber filter, and have it dump it into a 2d texture. Then apply the texture to the triangle. Or you can do a draw pixels and just draw the pixels to the screen. I would use buffer mode of the grabber filter, not callback, that way you only get the sample when u want it.


edit: One thing that took me a while to figure out is that the grabber will return bgr and so u need to create ur texture with a bgr extension. Also create it with mipmaps because the scale will probably not be power of 2.

Share this post


Link to post
Share on other sites
Jacob Roman    120
A sample grabber filter? I just want a video to be directly blitted onto a polygon like a texture. I believe DirectX has some kind of function lieing around in the DirectShow that I need to do this. I just don't know. Can you if possible show some kind of source code if you really know how to do this? And doing it by pixels maybe too slow. I'm looking for realtime speed. Plus I know very little of what I'm doing in rendering videos to polygons obviously.

Share this post


Link to post
Share on other sites
bzroom    647
what kind of video? if you just want avi then there are windows api's to get you the data to create ur texture, in the end your going to have to create a texture every frame of ur video and cover your polygon in it. If you want the software to automaticaly play any kind of video, mpeg, avi, dvd, u name it, then direct show is a good idea. Directshow makes a graph to render what ever kind of file you tell it to. In the end there is a video renderer that actualy plays the video on a window. You want to intercept the stream before it gets to the video renderer with you sample grabber, then your polygon and opengl program are the video renderer. heres my code it may not mean much to you so you'll have to do some hoemwork on directshow.

This function actualy creates the texture from the grabber.
I dont know what version of my program i took this from so it basicaly like a test and unfinished, but it works.

void cPlayer::UpdateVBuf()
{
if (!opened || !isvideo)
return;

if(false)
{
if (!vbuf)
{
pGrabber->GetCurrentBuffer(&vbufsize, NULL);


vbuf = new unsigned char[vbufsize];
//glGenTextures(1, &tex);
if (!vbuf)
{
Msg("realbad!");
}
//Msg("make tex");
}
pGrabber->GetCurrentBuffer(&vbufsize, (long*)vbuf);
}
else
{
pGrabber->GetCurrentBuffer(&vbufsize, NULL);

delete[] vbuf;
vbuf = new unsigned char[vbufsize];

if (!vbuf)
{
Msg("realbad!");
}
pGrabber->GetCurrentBuffer(&vbufsize, (long*)vbuf);
}
AM_MEDIA_TYPE mt;
HRESULT hr = pGrabber->GetConnectedMediaType(&mt);
if (FAILED(hr))
{
// Return error code.
}
// Examine the format block.
VIDEOINFOHEADER *pVih;
if ((mt.formattype == FORMAT_VideoInfo) &&
(mt.cbFormat >= sizeof(VIDEOINFOHEADER)) &&
(mt.pbFormat != NULL) )
{
pVih = (VIDEOINFOHEADER*)mt.pbFormat;

}
else
{

return;
}

//if (vbufsize == 0)
//Msg("zero buffer!");

if (vbufsize > 0)
{
glBindTexture(GL_TEXTURE_2D, tex);
//GL_BGR_EXT
gluBuild2DMipmaps(GL_TEXTURE_2D, 3, pVih->bmiHeader.biWidth,pVih->bmiHeader.biHeight,GL_BGR_EXT, GL_UNSIGNED_BYTE, vbuf);

//gluBuild2DMipmaps(GL_TEXTURE_2D, 3, pVih->bmiHeader.biWidth,pVih->bmiHeader.biHeight,GL_RGB, GL_UNSIGNED_BYTE, vbuf);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_NEAREST); // Linear Filtering
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_NEAREST); // Linear Filtering
}

}




I call this function to load a video file. Or audio file.

bool cPlayer::Open(char *filename)
{
USES_CONVERSION;
WCHAR wFile[500];
HRESULT hr;

wcsncpy(wFile, T2W(filename), NUMELMS(wFile)-1);
wFile[500-1] = 0;

JIF(CoCreateInstance(CLSID_FilterGraph, NULL, CLSCTX_INPROC_SERVER,
IID_IGraphBuilder, (void **)&pGB));

if(SUCCEEDED(hr))
{
HANDLE logfile;
char logfilename[500];

// create win32 file handle
sprintf(logfilename, "c:\\graph.txt", "");
logfile = CreateFile(logfilename, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS,
FILE_ATTRIBUTE_NORMAL, NULL);

pGB->SetLogFile((DWORD_PTR)logfile);
hr = CoCreateInstance(CLSID_SampleGrabber, NULL, CLSCTX_INPROC_SERVER,
IID_IBaseFilter, (void**)&pGrab);
if (FAILED(hr))
{
return false;
}


pGrab->QueryInterface(IID_ISampleGrabber, (void**)&pGrabber);
//JIF(pGrab->QueryInterface(IID_ISampleGrabberCB, (void**)&pSCB));

AM_MEDIA_TYPE mt;
ZeroMemory(&mt, sizeof(AM_MEDIA_TYPE));
mt.majortype = MEDIATYPE_Video;
mt.subtype = MEDIASUBTYPE_RGB24;
hr = pGrabber->SetMediaType(&mt);


//hr = CoCreateInstance(CLSID_AsyncReader, NULL, CLSCTX_INPROC_SERVER,
// IID_IBaseFilter , (void**)&pSrc);
//if (FAILED(hr))
//{
// Msg("load failed");
// return false;
//}

//hr = pGB->AddFilter(pSrc, L"Reader");
//if (FAILED(hr))
//{
//
// Msg("load failed");
// return false;
//}

////mt.majortype = MEDIATYPE_Stream;
//JIF(pGB->QueryInterface(IID_IFileSourceFilter, (void **)&pSrcCntr));
//pSrcCntr->Load(wFile, &mt);
//
//hr = pGB->AddSourceFilter(wFile, L"Source", &pSrc);
//if (FAILED(hr))
//{
// Msg("load failed");
// return false;
//}


hr = CoCreateInstance(CLSID_NullRenderer, NULL, CLSCTX_INPROC_SERVER,
IID_IBaseFilter, (void**)&pNR);
if (FAILED(hr))
{
Msg("load failed");
return false;
}

JIF(pGB->RenderFile(wFile, NULL));
JIF(pGB->QueryInterface(IID_IBasicVideo, (void **)&pBV));
JIF(pGB->QueryInterface(IID_IVideoWindow, (void **)&pVW));

if (!pBV || !pVW)
isvideo = false;
else
isvideo = true;

hr = pGB->AddFilter(pNR, L"Null Renderer");
if (FAILED(hr))
{

Msg("load failed");
return false;
}

hr = pGB->AddFilter(pGrab, L"Sample Grabber");
if (FAILED(hr))
{
return false;
}




if (isvideo)
{
IBaseFilter *tvr;
hr = pGB->FindFilterByName(L"Video Renderer", &tvr);

if (FAILED(hr))
{
isvideo = false;
goto b;
}

JIF(pGB->RemoveFilter(tvr));
IEnumFilters *fenum;
JIF(pGB->EnumFilters(&fenum));

IBaseFilter *efilt;

while(fenum->Next(1, &efilt, 0) == S_OK)
{
IPin *pin;
if (!FAILED(GetUnconnectedPin(efilt, PINDIR_OUTPUT, &pin)))
{

//found an good shot
if (!FAILED(ConnectFilters(pGB, efilt, pGrab)))
{
goto a;
}
/*else
Msg(TEXT("FAILED(hr=0x%x)", hr));*/

}
//else
//Msg(TEXT("FAILED(hr=0x%x)", hr));
}

Msg("could not find an empty connecteable filter");
a:
JIF(ConnectFilters(pGB, pGrab, pNR));
}

b:

//pGrabber->SetCallback(pSCB, 1);
JIF(pGrab->QueryInterface(IID_ISampleGrabber, (void**)&pGrabber));
pGrabber->SetOneShot(false);
pGrabber->SetBufferSamples(true);
pGB->SetDefaultSyncSource();


JIF(pGB->QueryInterface(IID_IMediaControl, (void **)&pMC));
JIF(pGB->QueryInterface(IID_IMediaEventEx, (void **)&pME));
JIF(pGB->QueryInterface(IID_IMediaSeeking, (void **)&pMS));
JIF(pGB->QueryInterface(IID_IMediaPosition, (void **)&pMP));

JIF(pGB->QueryInterface(IID_IBasicVideo, (void **)&pBV));
JIF(pGB->QueryInterface(IID_IBasicAudio, (void **)&pBA));


JIF(pME->SetNotifyWindow((OAHWND)apphnd, WM_GRAPHNOTIFY, 0));


IVideoFrameStep *pFSTest = NULL;

hr = pGB->QueryInterface(__uuidof(IVideoFrameStep), (PVOID *)&pFSTest);
//if (FAILED(hr))
//return FALSE;

hr = pFSTest->CanStep(0L, NULL);

if (hr == S_OK)
{
pFS = pFSTest;
}
else
{
pFSTest->Release();
}

IMoniker * pMoniker;
IRunningObjectTable *pROT;
if (FAILED(GetRunningObjectTable(0, &pROT)))
{
return E_FAIL;
}

WCHAR wsz[128];
wsprintfW(wsz, L"FilterGraph %08x pid %08x", (DWORD_PTR)pGB,
GetCurrentProcessId());

HRESULT hr = CreateItemMoniker(L"!", wsz, &pMoniker);
if (SUCCEEDED(hr))
{
hr = pROT->Register(ROTFLAGS_REGISTRATIONKEEPSALIVE, pGB,
pMoniker, &g_dwGraphRegister);
pMoniker->Release();
}

pROT->Release();
if (FAILED(hr))
{
Msg(TEXT("Failed to register filter graph with ROT! hr=0x%x"), hr);
g_dwGraphRegister = 0;
}
SaveGraphFile(pGB, L"c:\\graph.grf");
opened = true;
Stop();
return true;
}
return false;
Msg("load failed");
}



MSDN is your friend, directshow sdk.

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  

  • Similar Content

    • By pseudomarvin
      I assumed that if a shader is computationally expensive then the execution is just slower. But running the following GLSL FS instead just crashes
      void main() { float x = 0; float y = 0; int sum = 0; for (float x = 0; x < 10; x += 0.00005) { for (float y = 0; y < 10; y += 0.00005) { sum++; } } fragColor = vec4(1, 1, 1 , 1.0); } with unhandled exception in nvoglv32.dll. Are there any hard limits on the number of steps/time that a shader can take before it is shut down? I was thinking about implementing some time intensive computation in shaders where it would take on the order of seconds to compute a frame, is that possible? Thanks.
    • By Arulbabu Donbosco
      There are studios selling applications which is just copying any 3Dgraphic content and regenerating into another new window. especially for CAVE Virtual reality experience. so that the user opens REvite or CAD or any other 3D applications and opens a model. then when the user selects the rendered window the VR application copies the 3D model information from the OpenGL window. 
      I got the clue that the VR application replaces the windows opengl32.dll file. how this is possible ... how can we copy the 3d content from the current OpenGL window.
      anyone, please help me .. how to go further... to create an application like VR CAVE. 
       
      Thanks
    • By cebugdev
      hi all,

      i am trying to build an OpenGL 2D GUI system, (yeah yeah, i know i should not be re inventing the wheel, but this is for educational and some other purpose only),
      i have built GUI system before using 2D systems such as that of HTML/JS canvas, but in 2D system, i can directly match a mouse coordinates to the actual graphic coordinates with additional computation for screen size/ratio/scale ofcourse.
      now i want to port it to OpenGL, i know that to render a 2D object in OpenGL we specify coordiantes in Clip space or use the orthographic projection, now heres what i need help about.
      1. what is the right way of rendering the GUI? is it thru drawing in clip space or switching to ortho projection?
      2. from screen coordinates (top left is 0,0 nd bottom right is width height), how can i map the mouse coordinates to OpenGL 2D so that mouse events such as button click works? In consideration ofcourse to the current screen/size dimension.
      3. when let say if the screen size/dimension is different, how to handle this? in my previous javascript 2D engine using canvas, i just have my working coordinates and then just perform the bitblk or copying my working canvas to screen canvas and scale the mouse coordinates from there, in OpenGL how to work on a multiple screen sizes (more like an OpenGL ES question).
      lastly, if you guys know any books, resources, links or tutorials that handle or discuss this, i found one with marekknows opengl game engine website but its not free,
      Just let me know. Did not have any luck finding resource in google for writing our own OpenGL GUI framework.
      IF there are no any available online, just let me know, what things do i need to look into for OpenGL and i will study them one by one to make it work.
      thank you, and looking forward to positive replies.
    • By fllwr0491
      I have a few beginner questions about tesselation that I really have no clue.
      The opengl wiki doesn't seem to talk anything about the details.
       
      What is the relationship between TCS layout out and TES layout in?
      How does the tesselator know how control points are organized?
          e.g. If TES input requests triangles, but TCS can output N vertices.
             What happens in this case?
      In this article,
      http://www.informit.com/articles/article.aspx?p=2120983
      the isoline example TCS out=4, but TES in=isoline.
      And gl_TessCoord is only a single one.
      So which ones are the control points?
      How are tesselator building primitives?
    • By Orella
      I've been developing a 2D Engine using SFML + ImGui.
      Here you can see an image
      The editor is rendered using ImGui and the scene window is a sf::RenderTexture where I draw the GameObjects and then is converted to ImGui::Image to render it in the editor.
      Now I need to create a 3D Engine during this year in my Bachelor Degree but using SDL2 + ImGui and I want to recreate what I did with the 2D Engine. 
      I've managed to render the editor like I did in the 2D Engine using this example that comes with ImGui. 
      3D Editor preview
      But I don't know how to create an equivalent of sf::RenderTexture in SDL2, so I can draw the 3D scene there and convert it to ImGui::Image to show it in the editor.
      If you can provide code will be better. And if you want me to provide any specific code tell me.
      Thanks!
  • Popular Now