TextureAviMovie.cpp
资源名称:gloop.zip [点击查看]
上传用户:shxiangxiu
上传日期:2007-01-03
资源大小:1101k
文件大小:9k
源码类别:
OpenGL
开发平台:
Visual C++
- /////////////////////////////////////////////////////////////////////////////
- // TextureAviMovie.cpp : implementation file
- //
- // glOOP (OpenGL Object Oriented Programming library)
- // Copyright (c) Craig Fahrnbach 1997, 1998
- //
- // OpenGL is a registered trademark of Silicon Graphics
- //
- //
- // This program is provided for educational and personal use only and
- // is provided without guarantee or warrantee expressed or implied.
- //
- // Commercial use is strickly prohibited without written permission
- // from ImageWare Development.
- //
- // This program is -not- in the public domain.
- //
- // This source code has been adapted, with permission, from:
- // IDC Imagery
- // --GLVid C++ Class---
- // Kister S閎astien
- // leseb@compuserve.com
- //
- // 08/08/98 Added capability to display avi files of any size and color
- // depth.
- // 10/04/98 Added audio capability
- /////////////////////////////////////////////////////////////////////////////
- #include "stdafx.h"
- #include "glOOP.h"
- #include "DIBUtil.h"
- #include <time.h>
- #include <vfw.h>
- #ifdef _DEBUG
- #define new DEBUG_NEW
- #undef THIS_FILE
- static char THIS_FILE[] = __FILE__;
- #endif
- /////////////////////////////////////////////////////////////////////////////
- // CTextureAviMovie Global definitions
- /////////////////////////////////////////////////////////////////////////////
- // CTextureAviMovie
- IMPLEMENT_DYNAMIC(CTextureAviMovie, CTexture)
- /////////////////////////////////////////////////////////////////////////////
- // CTextureAviMovie construction
- CTextureAviMovie::CTextureAviMovie()
- {
- // Opens the AVI File library
- AVIFileInit();
- // Clear the pointers
- m_pFile = NULL; // File interface pointer
- m_pBMI = NULL; // Pointer to our current AVI frame DIB
- m_pAnimAVI = NULL; // Pointer to our animation object (AVI 'driver')
- m_pGetFrame = NULL; // Pointer to an AVI GetFrame object
- m_pVideoStream = NULL; // Pointer to the AVI Video stream interface.
- m_pAudioPlayer = NULL; // Pointer to our audio player
- // Clear the handles
- m_hPalette = NULL; // Handle to our applications palette
- // Set our default flags
- m_bEnableAudio = TRUE; // Enable audio flag
- m_bPlayContinuous = TRUE; // Play AVI file continuously?
- m_bStartAudio = TRUE; // Start the audio track?
- // Set default values
- m_lLastTimeIndex = 0L; // Time index of the stream last played;
- }
- /////////////////////////////////////////////////////////////////////////////
- // CTextureAviMovie Destructor
- CTextureAviMovie::~CTextureAviMovie()
- {
- // Close and delete our audio player device
- if(m_pAudioPlayer)
- {
- m_pAudioPlayer->Close();
- delete m_pAudioPlayer;
- }
- // Close our AVI stream
- if(m_pGetFrame)
- AVIStreamGetFrameClose(m_pGetFrame);
- // Release the file
- if(m_pFile)
- AVIFileRelease(m_pFile);
- // Closes AVI File library
- AVIFileExit();
- }
- /////////////////////////////////////////////////////////////////////////////
- // CTextureAviMovie virtual overrides
- BOOL CTextureAviMovie::OpenTexture(char* pFileName, HPALETTE hPalette)
- {
- int iResult;
- // Initialize the AVI file
- iResult = InitAVI(pFileName);
- if(iResult < 0)
- return FALSE;
- // Get the first frame from the AVI stream
- if(!GetFrameRGBA(0L, hPalette))
- return FALSE;
- // For this type of TextureMap, we do NOT want to create
- // a display list.
- m_bCreateList = FALSE;
- // Save the file name of the texture map
- m_szFileName.Format("%s", pFileName);
- // Save the handle to our palette for future use
- m_hPalette = hPalette;
- return TRUE;
- }
- void CTextureAviMovie::Serialize(CArchive& ar, int iVersion)
- {
- CString szBuffer;
- if (ar.IsStoring())
- {
- // Save the Object Class header...
- szBuffer.Format("%sCTextureAviMovie {n", szIndent);
- ar.WriteString(szBuffer);
- // Save the this objects' specific data...
- // szBuffer.Format("%stSolid < %d >n", szIndent, m_bSolid);
- // ar.WriteString(szBuffer);
- // Save the base class CTexture data...
- CTexture::Serialize(ar, iVersion);
- szBuffer.Format("%s}n", szIndent); // end of texture map def
- ar.WriteString(szBuffer);
- // Now that we have saved the CTexture derived and base data
- // serialize the CAnimAVI data.
- if(m_pAnimAVI)
- m_pAnimAVI->Serialize(ar, iVersion);
- }
- else
- {
- // Read the derived class data..
- ar.ReadString(szBuffer); // Read the class header
- // ar.ReadString(szBuffer);
- // szBuffer.TrimLeft(); // Remove leading white spaces
- // sscanf(szBuffer, "Solid < %d >n", &m_bSolid);
- // Read the base class CTexture data...
- CTexture::Serialize(ar, iVersion);
- // Now that we have read the CTexture derived and base data
- // serialize the CAnimAVI data.
- if(m_pAnimAVI)
- m_pAnimAVI->Serialize(ar, iVersion);
- }
- }
- /////////////////////////////////////////////////////////////////////////////
- // CTextureAviMovie function implementation
- DWORD CTextureAviMovie::InitAVI(LPSTR szFile)
- {
- AVIFILEINFO FAR pfi; // AVI File Info header pointer
- AVISTREAMINFO FAR psi; // AVI Stream Info header pointer
- UINT wResult; // Return error code
- // Open the AVI file and get the address of a file interface used to access it
- wResult = AVIFileOpen(&m_pFile,
- szFile,
- OF_READ | OF_SHARE_DENY_NONE,
- NULL);
- if(wResult)
- {
- if(the3dEngine.m_bDisplayErrorMessages)
- {
- char buf[128];
- sprintf(buf, "CTextureAviMovie::InitAVI - Could not open file: '%s'n", szFile);
- AfxMessageBox(buf, MB_OK);
- }
- return (wResult); // Return Error
- }
- // Get the stream interface that is associated with the AVI file.
- wResult = AVIFileGetStream(m_pFile,
- &m_pVideoStream,
- streamtypeVIDEO,
- 0);
- if(wResult)
- return (wResult); // Return Error
- // Define the length of the video (necessary for RGBA frame reading)
- // and its size.
- m_lVideoLength = AVIStreamLengthTime(m_pVideoStream);
- // Get the AVI video frame
- m_pGetFrame = AVIStreamGetFrameOpen(m_pVideoStream, NULL);
- if(m_pGetFrame == NULL)
- return (1); // Error: No frame
- wResult = AVIFileInfo(m_pFile, &pfi, sizeof(AVIFILEINFO FAR));
- wResult = AVIStreamInfo(m_pVideoStream, &psi, sizeof(AVISTREAMINFO FAR));
- // Now that we have the video stream open, attempt to create our
- // CAudioPlayer
- m_pAudioPlayer = CAudioPlayer::Create(szFile);
- return (0); // No errors
- }
- void CTextureAviMovie::Play(double dTime)
- {
- UINT wResult; // Return error code
- long lTime;
- if(m_bStartAudio && m_bEnableAudio && m_pAudioPlayer)
- {
- // If we have an Audio player device and the enable flag is set,
- // Open the file and Start!
- wResult = PlayAudio(dTime);
- if(wResult)
- m_bEnableAudio = FALSE; // Error. Audio track not present,
- // could not Open the audio track, or
- // audio device is Busy.
- }
- // Calculate the index (time) of the requested video frame. Note that this
- // algorithum will cause the AVI to continually cycle.
- lTime = (long)((int)(dTime*1000)) % m_lVideoLength;
- if(m_pAudioPlayer)
- {
- if(lTime < m_lLastTimeIndex)
- {
- // We have wrapped back around to the beginning of the AVI file,
- // Force synchronization of the audio track although this is only
- // necessary if the avi has been playing for quite some time.
- m_pAudioPlayer->SynchAudio((double)lTime/1000.f);
- }
- }
- GetFrameRGBA(lTime, m_hPalette);
- // Save the time index
- m_lLastTimeIndex = lTime;
- }
- DWORD CTextureAviMovie::PlayAudio(double dTime)
- {
- UINT wResult; // Return error code
- // If we have an Audio player device and the enable flags are set,
- // Open the file and Start!
- if(m_bStartAudio && m_bEnableAudio && m_pAudioPlayer)
- {
- wResult = m_pAudioPlayer->Open(m_szFileName.GetBuffer(128));
- if(!wResult)
- {
- wResult = m_pAudioPlayer->Play(dTime);
- if(!wResult)
- m_bStartAudio = FALSE; // Reset the flag since our audio
- } // track has started.
- return (wResult);
- }
- return (1);
- }
- void CTextureAviMovie::Stop()
- {
- // Stop our audio player
- StopAudio();
- // Reset our flag so that we can restart
- m_bStartAudio = TRUE;
- // Set default values
- m_lLastTimeIndex = 0L; // Time index of the stream last played;
- // Get the first frame
- GetFrameRGBA(0L, m_hPalette);
- }
- void CTextureAviMovie::StopAudio()
- {
- // Stop our audio player
- if(m_pAudioPlayer)
- m_pAudioPlayer->Stop();
- }
- BOOL CTextureAviMovie::GetFrameRGBA(long lTime, HPALETTE hPalette)
- {
- long lSample;
- // Convert from milliseconds to sample
- lSample = AVIStreamTimeToSample(m_pVideoStream, lTime);
- // Get the address of the decompressed video frame.
- // Note: The frame data is returned as a packed DIB.
- m_pBMI = (BITMAPINFO*) AVIStreamGetFrame(m_pGetFrame, lSample);
- if(!m_pBMI)
- return FALSE;
- // Convert DIB to a 32bpp DIB compatable with OpenGL
- m_pBits = (GLubyte*)ScaleDIB(hPalette, (HANDLE)m_pBMI);
- if(!m_pBits)
- return FALSE;
- // Now we have a square 32bpp DIB, use it as the texture.
- m_bApplyImage = TRUE;
- // Reset our AVI frame DIB pointer
- m_pBMI = NULL;
- return TRUE;
- }