![]() |
Multimedia »
Audio and Video »
Video
Intermediate
A class to easily generate AVI video with OpenGL and Video for WindowsBy Jonathan de HalleuxIf you want to generate an AVI video from your OpenGL application, this is the class you need. |
VC6Win2K, MFC, Dev
|
|
Advanced Search |
|
|
|
||||||||||||||||

CAviGenerator is a simple wrapper for the Video For Windows (VFW) library.
In the back, it takes care of a numerous initialization for you and hides the library function calls so
that the user can start generating movies in a minimal number of line of code and without having much knowledge about
VFW.
The class main features are:
The class CAVIGenerator takes it's inspiration from MSDN example writeavi.c.
A simple class that make creation of AVI video easy.
Each object contains general characteristics of the movie such as
BITMAPINFOHEADERIt contains also private members that are used to handle the "Video for Windows" AVI engine.
CAVIGenerator();
Default constructor, default file name is "Untitled.avi", frame rate is 30 fps.
CAVIGenerator(LPCTSTR sFileName, CView* pView, DWORD dwRate);
In-place constructor with CView. See SetBitmapHeader(CView* pView).
CAVIGenerator(LPCTSTR sFileName, LPBITMAPINFOHEADER lpbih, DWORD dwRate);
In-place constructor with BITMAPINFOHEADER. See SetBitmapHeader(LPBITMAPINFOHEADER lpbih)
void SetFileName(const CString& _sFileName)
SetFileName sets the name of the output file. The filename extension should be
.avi otherwize the creation of the AVI won't work. Checking of .avi extension
is left to the user.
void SetRate(DWORD dwRate)
SetRate sets the frame rate (fps).
void SetBitmapHeader(CView* pView);
SetBitmapHeader fills in the information about the bitmap
in m_bih using using from the view the view pView the width, height,
24 bit/color, and BRG. It makes sure that the width and height are both multiple of 4.
void SetBitmapHeader(LPBITMAPINFOHEADER lpbih);
SetBitmapHeader sets the bitmap info as in lpbih. It supposes that the
user knows what he's doing.
LPBITMAPINFOHEADER GetBitmapHeader()
GetBitmapHeader returns a pointer to bitmap info header structure.
HRESULT InitEngine();
InitEngine
initializes the VFW engine and chooses a codec. This function has to be
called before starting to grab frames. Some asserts are
made to check that bitmap info has been properly initialized.
It returns the last HRESULT. If something fails,
the error message can be retreived using GetLastErrorMessage().
CAviGenerator avi;
HRESULT hr;
hr=avi.InitEngine();
if (FAILED(hr))
{
AfxMessageBox(avi.GetLastErrorMessage());
...
}
HRESULT AddFrame(BYTE* bmBits);
AddFrame adds a frame to the movie. The data pointed by
bmBits should be compatible with the bitmap description
made by SetBitmapHeader. Typically with OpenGL,
bmBits is the buffer read when using glReadPixels.
As for InitEnginge, it returns the HRESULT.
void ReleaseEngine();
ReleaseEngine releases ressources allocated for movie and close the file.
You have to follow these steps:
CView object to the
SetBitmapHeader function. Alternative, the user can fill in the
BITMAPINFOHEADER structure himself. Make sure that dimension of bitmap are
a multiple of 4.InitEngine. A dialog box will
popup to choose and fill in the parameters of the codec.AddFrame.ReleaseEngine.And you're are done! Here's some demo pseudo-code:
CAVIGenerator AviGen; // generator BYTE* bmBits; // image buffer HRESULT hr; AviGen.SetRate(20); // set 20fps AviGen.SetBitmapHeader(GetActiveView()); // get bitmap info out of the view hr=AviGen.InitEngine() // start engine if (FAILED(hr)) { AfxMessageBox(avi.GetLastErrorMessage()); goto Cleaning; } LPBITMAPINFOHEADER lpbih=AviGen.GetBitmapInfo(); // getting bitmap info // allocating memory for bmBits bmBits=new BYTE[3 /* BRG*/ * lpbih->biWidth* lpbih->biHeight]; for (int i=0;i<100;i++) // make 100 frames { // TODO pust your draw code here. /* Draw code where bmBits is filled. For example, to read OpenGL buffer, put glReadPixels(0,0,lpbih->biWidth,lpbih->biHeight, GL_BGR_EXT,GL_UNSIGNED_BYTE,bmBits); */ // adding frame and continue if OK hr=AviGen.AddFrame(bmBits); if (FAILED(hr)) { AfxMessageBox(avi.GetLastErrorMessage()); goto Cleaning; } } // cleaning memory Cleaning: AviGen.ReleaseEngine(); // releasing ressources delete[] bmBits; // release ressources
A simple MFC SDI application that shows a cube and triangle rotating using OpenGL. When selecting the menu item Avi Generation->Generate..., you start to create a 100 frames AVI movie.
OpenGL drawing code is copy-pasted from lesson 5 of NeHe Productions.
~CAviGenerator and in example above (LPBITMAPINFOHEADER lpbih not initialized).CAVIGenerator::SetBitmapHeader(LPBITMAPINFOHEADER lpbih) function. Thanks to Lori
Gardi.
General
News
Question
Answer
Joke
Rant
Admin
|
PermaLink |
Privacy |
Terms of Use
Last Updated: 26 Oct 2002 Editor: Chris Maunder |
Copyright 2001 by Jonathan de Halleux Everything else Copyright © CodeProject, 1999-2009 Web15 | Advertise on the Code Project |