首页 最新 热门 推荐

  • 首页
  • 最新
  • 热门
  • 推荐

C++ MFC SnowWorld

  • 25-02-19 03:41
  • 2940
  • 7014
blog.csdn.net

目录

效果

项目 

代码

下载


效果

SnowWorld

项目 

代码

// ChildView.cpp : implementation of the CChildView class
//

#include "stdafx.h"
#include "SnowWorld.h"
#include "ChildView.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

GLfloat LightAmbient  [] = { 0.5f, 0.5f, 0.5f, 1.0f };
GLfloat LightDiffuse  [] = { 1.0f, 1.0f, 1.0f, 1.0f };
GLfloat LightPosition [] = { 0.0f, 0.0f, 2.0f, 1.0f };

/
// CChildView

CChildView::CChildView()
{
    m_pClientDC = NULL;
    m_lpThread  = NULL;

    m_nFloorIndex = 0;
}

CChildView::~CChildView()
{
    if ( m_lpThread != NULL )
    {
        delete m_lpThread;
        m_lpThread = NULL;
    }
}


BEGIN_MESSAGE_MAP(CChildView,CWnd )
    //{{AFX_MSG_MAP(CChildView)
    ON_WM_CREATE()

    ON_WM_SIZE()

    ON_WM_ERASEBKGND()
    ON_WM_PAINT()

    ON_WM_TIMER()

    ON_WM_DESTROY()
    //}}AFX_MSG_MAP
END_MESSAGE_MAP()


/
// CChildView message handlers

BOOL CChildView::PreCreateWindow(CREATESTRUCT& cs) 
{
    if ( ! CWnd::PreCreateWindow( cs ) )
        return FALSE;

    cs.dwExStyle |= WS_EX_CLIENTEDGE;
    cs.style &= ~WS_BORDER;
    cs.lpszClass = AfxRegisterWndClass( CS_HREDRAW | CS_VREDRAW | CS_DBLCLKS, 
        ::LoadCursor( NULL, IDC_ARROW ), HBRUSH( COLOR_WINDOW + 1 ), NULL );

    return TRUE;
}

BOOL CChildView::PreTranslateMessage( MSG* pMsg )
{
    if ( pMsg->message == WM_KEYDOWN && ( pMsg->wParam == VK_RETURN || pMsg->wParam == VK_SPACE ) )
    {
        if ( ++m_nFloorIndex >= 3 )
            m_nFloorIndex = 0;
    }

    return CWnd::PreTranslateMessage( pMsg );
}

int CChildView::OnCreate( LPCREATESTRUCT lpCreateStruct )
{
    if ( CWnd::OnCreate( lpCreateStruct ) == -1 )
    {
        TRACE0( "Failed to create the CChildView" );
        return -1;
    }

    m_lpThread = AfxBeginThread( Thread, NULL, THREAD_PRIORITY_NORMAL, 0, CREATE_SUSPENDED );
    ASSERT( m_lpThread );

    InitOpenGL();

    SetTimer( 1, 50, NULL );

    m_lpThread->ResumeThread();
    return 0;
}

void CChildView::OnSize( UINT nType, int cx, int cy )
{
    CWnd::OnSize( nType, cx, cy );

    if ( cy == 0 )
        cy = 1;

    glViewport( 0, 0, cx, cy );        // Reset The Current View-port
    glMatrixMode( GL_PROJECTION );    // Select The Projection Matrix
    glLoadIdentity();                // Reset The Projection Matrix        

    // Calculate The Aspect Ratio Of The Window
    gluPerspective( 45.0f, ( GLfloat ) cx / ( GLfloat ) cy, 0.1f, 400.0f );
    glMatrixMode( GL_MODELVIEW );    // Select The Model-view Matrix
    glLoadIdentity();        
}

void CChildView::OnPaint() 
{
    CPaintDC dc(this); // device context for painting
    CBitmap bitmap;
    bitmap.LoadBitmap( IDB_SNOW_FLOOR );

    // TODO: Add your message handler code here
    DrawOpenGLScene();
    // Do not call CWnd::OnPaint() for painting messages
}

BOOL CChildView::OnEraseBkgnd( CDC* pDC )
{
    UNREFERENCED_PARAMETER( pDC );

    return TRUE;
}

void CChildView::OnDestroy()
{
    HGLRC hrc = NULL;
    hrc = ::wglGetCurrentContext();

    ::wglMakeCurrent( NULL, NULL );
    if ( hrc != NULL )
        ::wglDeleteContext( hrc );

    if ( m_pClientDC != NULL )
    {
        delete m_pClientDC;
        m_pClientDC = NULL;
    }
    ASSERT( m_pClientDC == NULL );

    CWnd::OnDestroy();
}

// Initialize the OpenGL context device...

VOID CChildView::InitOpenGL()
{
    m_pClientDC = new CClientDC( this );
    ASSERT( m_pClientDC != NULL );
    ASSERT_VALID( m_pClientDC );

    if ( ! SetupPixelFormat() )
        return;

    PIXELFORMATDESCRIPTOR pfd;

    int n = ::GetPixelFormat( m_pClientDC->GetSafeHdc());
    ::DescribePixelFormat( m_pClientDC->GetSafeHdc(), n, sizeof( pfd ), & pfd );

    HGLRC hrc = NULL;
    hrc = wglCreateContext( m_pClientDC->GetSafeHdc() );
    ASSERT( hrc );
    wglMakeCurrent( m_pClientDC->GetSafeHdc(), hrc );

    if ( ! LoadOpenGLTextures() )
        return;

    glEnable( GL_TEXTURE_2D );
    glShadeModel( GL_SMOOTH );                // Enable Smooth Shading
    glClearColor( 0.5f, 0.5f, 0.5f, 0.5f );    // Black Background
    glClearDepth( 1.0f );                    // Depth Buffer Setup

    glHint( GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST );    // Really Nice Perspective Calculations

    glLightfv( GL_LIGHT1, GL_AMBIENT,  LightAmbient  );
    glLightfv( GL_LIGHT1, GL_DIFFUSE,  LightDiffuse  );
    glLightfv( GL_LIGHT1, GL_POSITION, LightPosition );
    glEnable( GL_LIGHT1 );

    glBlendFunc( GL_SRC_ALPHA,GL_ONE );        // Set The Blending Function For Translucency
    glEnable( GL_BLEND );    
}

// Load the texture from the define file path

BOOL CChildView::LoadOpenGLTextures()
{
    glGenTextures( E_TEXTURECOUNT, & m_textureSnow[ 0 ] );
    LoadImageFromResID( IDB_SNOW_LARGE,  m_textureSnow[ 0 ] );
    LoadImageFromResID( IDB_SNOW_MIDDLE, m_textureSnow[ 1 ] );
    LoadImageFromResID( IDB_SNOW_SMALL,  m_textureSnow[ 2 ] );

    InitSnow();

    LoadImageFromResID( IDB_SNOW_MIDDLE, m_textureSnow[ 3 ] );

    return TRUE;
}

VOID CChildView::SetDefaultTextureParams()
{
    glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR );
    glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR );
} 

BOOL CChildView::SetupPixelFormat()
{
    static PIXELFORMATDESCRIPTOR pfd = {
        sizeof( PIXELFORMATDESCRIPTOR ),// size of this pfd
        1,                              // version number
        PFD_DRAW_TO_WINDOW |            // support window
        PFD_SUPPORT_OPENGL |        // support OpenGL
        PFD_DOUBLEBUFFER,           // double buffered
        PFD_TYPE_RGBA,                  // RGBA type
        24,                             // 24-bit color depth
        0, 0, 0, 0, 0, 0,               // color bits ignored
        0,                              // no alpha buffer
        0,                              // shift bit ignored
        0,                              // no accumulation buffer
        0, 0, 0, 0,                     // accum bits ignored
        32,                             // 32-bit z-buffer
        0,                              // no stencil buffer
        0,                              // no auxiliary buffer
        PFD_MAIN_PLANE,                 // main layer
        0,                              // reserved
        0, 0, 0                         // layer masks ignored
    };

    int pixelformat = 0;

    ASSERT( m_pClientDC != NULL );
    if ( ( pixelformat = ChoosePixelFormat( m_pClientDC->GetSafeHdc(), & pfd ) ) == 0 )
    {
        AfxMessageBox( _T( "ChoosePixelFormat failed" ) );
        return FALSE;
    }

    if ( SetPixelFormat( m_pClientDC->GetSafeHdc(), pixelformat, & pfd ) == FALSE )
    {
        AfxMessageBox( _T( "SetPixelFormat failed" ) );
        return FALSE;
    }

    return TRUE;
}

VOID CChildView::DrawOpenGLScene()
{
    static GLfloat wAngleY = 10.0f;

    glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );

    glLoadIdentity();
    glTranslatef( 0.0f, -30.0f, -150.0f );
    glRotatef( wAngleY, 0.0f, 1.0f, 0.0f );

    glBindTexture( GL_TEXTURE_2D, m_textureSnow[ m_nFloorIndex ] );

    glColor4f( 1.0f, 1.0f, 1.0f, 0.5 );

    glBegin( GL_QUADS );
    glNormal3f( 0.0f, 1.0f, 0.0f );
    glTexCoord2f( 0.0f, 0.0f ); glVertex3f(  100.0f, 0.0f, -100.0f );
    glTexCoord2f( 1.0f, 0.0f ); glVertex3f(  100.0f, 0.0f,  100.0f );
    glTexCoord2f( 1.0f, 1.0f ); glVertex3f( -100.0f, 0.0f,  100.0f );
    glTexCoord2f( 0.0f, 1.0f ); glVertex3f( -100.0f, 0.0f, -100.0f );
    glEnd();

    for ( int nIndex = 0; nIndex < E_SNOWCOUNT; nIndex++ )
    {
        glLoadIdentity();
        glTranslatef( 0.0f, -30.0f, -150.0f ); 
        glRotatef( wAngleY, 0.0f, 1.0f, 0.0f );

        glBindTexture( GL_TEXTURE_2D, m_textureSnow[ m_snow[ nIndex ].nIndexTexture ] );
        glTranslatef( m_snow[ nIndex ].x, m_snow[ nIndex ].y, m_snow[ nIndex ].z );

        glRotatef( m_snow[ nIndex ].xrot, 1.0f, 0.0f, 0.0f );
        glRotatef( m_snow[ nIndex ].yrot, 0.0f, 1.0f, 0.0f );
        glRotatef( m_snow[ nIndex ].zrot, 0.0f, 0.0f, 1.0f );

        glBegin( GL_QUADS );
        glNormal3f( 0.0f, 1.0f, 0.0f );
        glTexCoord2f( 0.0f, 0.0f ); glVertex3f(  1.0f, 0.0f, -1.0f );
        glTexCoord2f( 1.0f, 0.0f ); glVertex3f(  1.0f, 0.0f,  1.0f );
        glTexCoord2f( 1.0f, 1.0f ); glVertex3f( -1.0f, 0.0f,  1.0f );
        glTexCoord2f( 0.0f, 1.0f ); glVertex3f( -1.0f, 0.0f, -1.0f );
        glEnd();

        m_snow[ nIndex ].y -= m_snow[ nIndex ].dropSpeed;
        if( m_snow[ nIndex ].y < -33 )
            m_snow[ nIndex ].y = 125.0f;

        m_snow[ nIndex ].xrot += m_snow[ nIndex ].rotSpeed;
        m_snow[ nIndex ].yrot += m_snow[ nIndex ].rotSpeed;
        m_snow[ nIndex ].zrot += m_snow[ nIndex ].rotSpeed;
    }

    wAngleY += 0.2f;

    glFinish();
    SwapBuffers( wglGetCurrentDC() );
}

BOOL CChildView::LoadImageFromResID( UINT nResID, GLuint& texture )
{
    ASSERT( nResID > 0 );

    HBITMAP hBitmap = ( HBITMAP ) LoadImage( \
        AfxGetInstanceHandle(), \
        MAKEINTRESOURCE( nResID ), \
        IMAGE_BITMAP, \
        0, 0, \
        LR_CREATEDIBSECTION );
    ASSERT( hBitmap != NULL );

    DIBSECTION ds;
    GetObject( hBitmap, sizeof( DIBSECTION ), ( LPVOID ) & ds );

    glBindTexture( GL_TEXTURE_2D, texture );
    SetDefaultTextureParams();

    glTexImage2D( GL_TEXTURE_2D, 0, 3, ds.dsBm.bmWidth, ds.dsBm.bmHeight, 0, GL_RGB, GL_UNSIGNED_BYTE, ds.dsBm.bmBits );
    DeleteObject( hBitmap );

    return TRUE;
}

void CChildView::OnTimer(UINT_PTR nIDEvent )
{
    DrawOpenGLScene();

    CWnd::OnTimer( nIDEvent );

    // Eat spurious WM_TIMER message
    MSG msg;
    while( ::PeekMessage( & msg, m_hWnd, WM_TIMER, WM_TIMER, PM_REMOVE ) )
        ;
}

VOID CChildView::InitSnow()
{
    srand( GetTickCount() );

    for ( int nIndex = 0; nIndex < E_SNOWCOUNT; nIndex++ )
    {
        m_snow[ nIndex ].nIndexTexture = rand() % 3;

        m_snow[ nIndex ].x = GLfloat( rand() % 200 - 100 );
        m_snow[ nIndex ].y = GLfloat( rand() % 200 - 100 );
        m_snow[ nIndex ].z = 100.0f + GLfloat( rand() % 25 );
    }

    for ( int i = 0; i < E_SNOWCOUNT; i++ )
    {
        m_snow[ i ].nIndexTexture = rand() % 3;
        m_snow[ i ].x             = GLfloat( rand() % 200 - 100 );
        m_snow[ i ].z             = GLfloat( rand() % 200 - 100 );
        m_snow[ i ].y             = 100.0f + GLfloat( rand() % 25 );

        m_snow[ i ].xrot          = 0;
        m_snow[ i ].yrot          = 0;
        m_snow[ i ].zrot          = 0;

        m_snow[ i ].dropSpeed     = 0.01f * ( rand() % 50 + 2 );
        m_snow[ i ].rotSpeed      =  0.1f * ( rand() % 10 + 2 );
    }
}

// The thread which will play the music
//  for the scene...

UINT CChildView::Thread( LPVOID lParam )
{
    UNREFERENCED_PARAMETER( lParam );

    for ( ; ; )
    {
        HRSRC hResInfo = FindResource( AfxGetInstanceHandle(), MAKEINTRESOURCE( IDR_MUSIC_SNOW ), "WAVE" );
        ASSERT( hResInfo );
        HRSRC hRes = ( HRSRC ) LoadResource( AfxGetInstanceHandle(), hResInfo );
        ASSERT( hRes );
        LPSTR lpRes = ( LPSTR ) LockResource(hRes);
        sndPlaySound(lpRes, SND_MEMORY | SND_SYNC | SND_NODEFAULT ); 
        FreeResource( hRes ); 
    }

    return 0;
}

  1. // ChildView.cpp : implementation of the CChildView class
  2. //
  3. #include "stdafx.h"
  4. #include "SnowWorld.h"
  5. #include "ChildView.h"
  6. #ifdef _DEBUG
  7. #define new DEBUG_NEW
  8. #undef THIS_FILE
  9. static char THIS_FILE[] = __FILE__;
  10. #endif
  11. GLfloat LightAmbient [] = { 0.5f, 0.5f, 0.5f, 1.0f };
  12. GLfloat LightDiffuse [] = { 1.0f, 1.0f, 1.0f, 1.0f };
  13. GLfloat LightPosition [] = { 0.0f, 0.0f, 2.0f, 1.0f };
  14. /
  15. // CChildView
  16. CChildView::CChildView()
  17. {
  18. m_pClientDC = NULL;
  19. m_lpThread = NULL;
  20. m_nFloorIndex = 0;
  21. }
  22. CChildView::~CChildView()
  23. {
  24. if ( m_lpThread != NULL )
  25. {
  26. delete m_lpThread;
  27. m_lpThread = NULL;
  28. }
  29. }
  30. BEGIN_MESSAGE_MAP(CChildView,CWnd )
  31. //{{AFX_MSG_MAP(CChildView)
  32. ON_WM_CREATE()
  33. ON_WM_SIZE()
  34. ON_WM_ERASEBKGND()
  35. ON_WM_PAINT()
  36. ON_WM_TIMER()
  37. ON_WM_DESTROY()
  38. //}}AFX_MSG_MAP
  39. END_MESSAGE_MAP()
  40. /
  41. // CChildView message handlers
  42. BOOL CChildView::PreCreateWindow(CREATESTRUCT& cs)
  43. {
  44. if ( ! CWnd::PreCreateWindow( cs ) )
  45. return FALSE;
  46. cs.dwExStyle |= WS_EX_CLIENTEDGE;
  47. cs.style &= ~WS_BORDER;
  48. cs.lpszClass = AfxRegisterWndClass( CS_HREDRAW | CS_VREDRAW | CS_DBLCLKS,
  49. ::LoadCursor( NULL, IDC_ARROW ), HBRUSH( COLOR_WINDOW + 1 ), NULL );
  50. return TRUE;
  51. }
  52. BOOL CChildView::PreTranslateMessage( MSG* pMsg )
  53. {
  54. if ( pMsg->message == WM_KEYDOWN && ( pMsg->wParam == VK_RETURN || pMsg->wParam == VK_SPACE ) )
  55. {
  56. if ( ++m_nFloorIndex >= 3 )
  57. m_nFloorIndex = 0;
  58. }
  59. return CWnd::PreTranslateMessage( pMsg );
  60. }
  61. int CChildView::OnCreate( LPCREATESTRUCT lpCreateStruct )
  62. {
  63. if ( CWnd::OnCreate( lpCreateStruct ) == -1 )
  64. {
  65. TRACE0( "Failed to create the CChildView" );
  66. return -1;
  67. }
  68. m_lpThread = AfxBeginThread( Thread, NULL, THREAD_PRIORITY_NORMAL, 0, CREATE_SUSPENDED );
  69. ASSERT( m_lpThread );
  70. InitOpenGL();
  71. SetTimer( 1, 50, NULL );
  72. m_lpThread->ResumeThread();
  73. return 0;
  74. }
  75. void CChildView::OnSize( UINT nType, int cx, int cy )
  76. {
  77. CWnd::OnSize( nType, cx, cy );
  78. if ( cy == 0 )
  79. cy = 1;
  80. glViewport( 0, 0, cx, cy ); // Reset The Current View-port
  81. glMatrixMode( GL_PROJECTION ); // Select The Projection Matrix
  82. glLoadIdentity(); // Reset The Projection Matrix
  83. // Calculate The Aspect Ratio Of The Window
  84. gluPerspective( 45.0f, ( GLfloat ) cx / ( GLfloat ) cy, 0.1f, 400.0f );
  85. glMatrixMode( GL_MODELVIEW ); // Select The Model-view Matrix
  86. glLoadIdentity();
  87. }
  88. void CChildView::OnPaint()
  89. {
  90. CPaintDC dc(this); // device context for painting
  91. CBitmap bitmap;
  92. bitmap.LoadBitmap( IDB_SNOW_FLOOR );
  93. // TODO: Add your message handler code here
  94. DrawOpenGLScene();
  95. // Do not call CWnd::OnPaint() for painting messages
  96. }
  97. BOOL CChildView::OnEraseBkgnd( CDC* pDC )
  98. {
  99. UNREFERENCED_PARAMETER( pDC );
  100. return TRUE;
  101. }
  102. void CChildView::OnDestroy()
  103. {
  104. HGLRC hrc = NULL;
  105. hrc = ::wglGetCurrentContext();
  106. ::wglMakeCurrent( NULL, NULL );
  107. if ( hrc != NULL )
  108. ::wglDeleteContext( hrc );
  109. if ( m_pClientDC != NULL )
  110. {
  111. delete m_pClientDC;
  112. m_pClientDC = NULL;
  113. }
  114. ASSERT( m_pClientDC == NULL );
  115. CWnd::OnDestroy();
  116. }
  117. // Initialize the OpenGL context device...
  118. VOID CChildView::InitOpenGL()
  119. {
  120. m_pClientDC = new CClientDC( this );
  121. ASSERT( m_pClientDC != NULL );
  122. ASSERT_VALID( m_pClientDC );
  123. if ( ! SetupPixelFormat() )
  124. return;
  125. PIXELFORMATDESCRIPTOR pfd;
  126. int n = ::GetPixelFormat( m_pClientDC->GetSafeHdc());
  127. ::DescribePixelFormat( m_pClientDC->GetSafeHdc(), n, sizeof( pfd ), & pfd );
  128. HGLRC hrc = NULL;
  129. hrc = wglCreateContext( m_pClientDC->GetSafeHdc() );
  130. ASSERT( hrc );
  131. wglMakeCurrent( m_pClientDC->GetSafeHdc(), hrc );
  132. if ( ! LoadOpenGLTextures() )
  133. return;
  134. glEnable( GL_TEXTURE_2D );
  135. glShadeModel( GL_SMOOTH ); // Enable Smooth Shading
  136. glClearColor( 0.5f, 0.5f, 0.5f, 0.5f ); // Black Background
  137. glClearDepth( 1.0f ); // Depth Buffer Setup
  138. glHint( GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST ); // Really Nice Perspective Calculations
  139. glLightfv( GL_LIGHT1, GL_AMBIENT, LightAmbient );
  140. glLightfv( GL_LIGHT1, GL_DIFFUSE, LightDiffuse );
  141. glLightfv( GL_LIGHT1, GL_POSITION, LightPosition );
  142. glEnable( GL_LIGHT1 );
  143. glBlendFunc( GL_SRC_ALPHA,GL_ONE ); // Set The Blending Function For Translucency
  144. glEnable( GL_BLEND );
  145. }
  146. // Load the texture from the define file path
  147. BOOL CChildView::LoadOpenGLTextures()
  148. {
  149. glGenTextures( E_TEXTURECOUNT, & m_textureSnow[ 0 ] );
  150. LoadImageFromResID( IDB_SNOW_LARGE, m_textureSnow[ 0 ] );
  151. LoadImageFromResID( IDB_SNOW_MIDDLE, m_textureSnow[ 1 ] );
  152. LoadImageFromResID( IDB_SNOW_SMALL, m_textureSnow[ 2 ] );
  153. InitSnow();
  154. LoadImageFromResID( IDB_SNOW_MIDDLE, m_textureSnow[ 3 ] );
  155. return TRUE;
  156. }
  157. VOID CChildView::SetDefaultTextureParams()
  158. {
  159. glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR );
  160. glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR );
  161. }
  162. BOOL CChildView::SetupPixelFormat()
  163. {
  164. static PIXELFORMATDESCRIPTOR pfd = {
  165. sizeof( PIXELFORMATDESCRIPTOR ),// size of this pfd
  166. 1, // version number
  167. PFD_DRAW_TO_WINDOW | // support window
  168. PFD_SUPPORT_OPENGL | // support OpenGL
  169. PFD_DOUBLEBUFFER, // double buffered
  170. PFD_TYPE_RGBA, // RGBA type
  171. 24, // 24-bit color depth
  172. 0, 0, 0, 0, 0, 0, // color bits ignored
  173. 0, // no alpha buffer
  174. 0, // shift bit ignored
  175. 0, // no accumulation buffer
  176. 0, 0, 0, 0, // accum bits ignored
  177. 32, // 32-bit z-buffer
  178. 0, // no stencil buffer
  179. 0, // no auxiliary buffer
  180. PFD_MAIN_PLANE, // main layer
  181. 0, // reserved
  182. 0, 0, 0 // layer masks ignored
  183. };
  184. int pixelformat = 0;
  185. ASSERT( m_pClientDC != NULL );
  186. if ( ( pixelformat = ChoosePixelFormat( m_pClientDC->GetSafeHdc(), & pfd ) ) == 0 )
  187. {
  188. AfxMessageBox( _T( "ChoosePixelFormat failed" ) );
  189. return FALSE;
  190. }
  191. if ( SetPixelFormat( m_pClientDC->GetSafeHdc(), pixelformat, & pfd ) == FALSE )
  192. {
  193. AfxMessageBox( _T( "SetPixelFormat failed" ) );
  194. return FALSE;
  195. }
  196. return TRUE;
  197. }
  198. VOID CChildView::DrawOpenGLScene()
  199. {
  200. static GLfloat wAngleY = 10.0f;
  201. glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
  202. glLoadIdentity();
  203. glTranslatef( 0.0f, -30.0f, -150.0f );
  204. glRotatef( wAngleY, 0.0f, 1.0f, 0.0f );
  205. glBindTexture( GL_TEXTURE_2D, m_textureSnow[ m_nFloorIndex ] );
  206. glColor4f( 1.0f, 1.0f, 1.0f, 0.5 );
  207. glBegin( GL_QUADS );
  208. glNormal3f( 0.0f, 1.0f, 0.0f );
  209. glTexCoord2f( 0.0f, 0.0f ); glVertex3f( 100.0f, 0.0f, -100.0f );
  210. glTexCoord2f( 1.0f, 0.0f ); glVertex3f( 100.0f, 0.0f, 100.0f );
  211. glTexCoord2f( 1.0f, 1.0f ); glVertex3f( -100.0f, 0.0f, 100.0f );
  212. glTexCoord2f( 0.0f, 1.0f ); glVertex3f( -100.0f, 0.0f, -100.0f );
  213. glEnd();
  214. for ( int nIndex = 0; nIndex < E_SNOWCOUNT; nIndex++ )
  215. {
  216. glLoadIdentity();
  217. glTranslatef( 0.0f, -30.0f, -150.0f );
  218. glRotatef( wAngleY, 0.0f, 1.0f, 0.0f );
  219. glBindTexture( GL_TEXTURE_2D, m_textureSnow[ m_snow[ nIndex ].nIndexTexture ] );
  220. glTranslatef( m_snow[ nIndex ].x, m_snow[ nIndex ].y, m_snow[ nIndex ].z );
  221. glRotatef( m_snow[ nIndex ].xrot, 1.0f, 0.0f, 0.0f );
  222. glRotatef( m_snow[ nIndex ].yrot, 0.0f, 1.0f, 0.0f );
  223. glRotatef( m_snow[ nIndex ].zrot, 0.0f, 0.0f, 1.0f );
  224. glBegin( GL_QUADS );
  225. glNormal3f( 0.0f, 1.0f, 0.0f );
  226. glTexCoord2f( 0.0f, 0.0f ); glVertex3f( 1.0f, 0.0f, -1.0f );
  227. glTexCoord2f( 1.0f, 0.0f ); glVertex3f( 1.0f, 0.0f, 1.0f );
  228. glTexCoord2f( 1.0f, 1.0f ); glVertex3f( -1.0f, 0.0f, 1.0f );
  229. glTexCoord2f( 0.0f, 1.0f ); glVertex3f( -1.0f, 0.0f, -1.0f );
  230. glEnd();
  231. m_snow[ nIndex ].y -= m_snow[ nIndex ].dropSpeed;
  232. if( m_snow[ nIndex ].y < -33 )
  233. m_snow[ nIndex ].y = 125.0f;
  234. m_snow[ nIndex ].xrot += m_snow[ nIndex ].rotSpeed;
  235. m_snow[ nIndex ].yrot += m_snow[ nIndex ].rotSpeed;
  236. m_snow[ nIndex ].zrot += m_snow[ nIndex ].rotSpeed;
  237. }
  238. wAngleY += 0.2f;
  239. glFinish();
  240. SwapBuffers( wglGetCurrentDC() );
  241. }
  242. BOOL CChildView::LoadImageFromResID( UINT nResID, GLuint& texture )
  243. {
  244. ASSERT( nResID > 0 );
  245. HBITMAP hBitmap = ( HBITMAP ) LoadImage( \
  246. AfxGetInstanceHandle(), \
  247. MAKEINTRESOURCE( nResID ), \
  248. IMAGE_BITMAP, \
  249. 0, 0, \
  250. LR_CREATEDIBSECTION );
  251. ASSERT( hBitmap != NULL );
  252. DIBSECTION ds;
  253. GetObject( hBitmap, sizeof( DIBSECTION ), ( LPVOID ) & ds );
  254. glBindTexture( GL_TEXTURE_2D, texture );
  255. SetDefaultTextureParams();
  256. glTexImage2D( GL_TEXTURE_2D, 0, 3, ds.dsBm.bmWidth, ds.dsBm.bmHeight, 0, GL_RGB, GL_UNSIGNED_BYTE, ds.dsBm.bmBits );
  257. DeleteObject( hBitmap );
  258. return TRUE;
  259. }
  260. void CChildView::OnTimer(UINT_PTR nIDEvent )
  261. {
  262. DrawOpenGLScene();
  263. CWnd::OnTimer( nIDEvent );
  264. // Eat spurious WM_TIMER message
  265. MSG msg;
  266. while( ::PeekMessage( & msg, m_hWnd, WM_TIMER, WM_TIMER, PM_REMOVE ) )
  267. ;
  268. }
  269. VOID CChildView::InitSnow()
  270. {
  271. srand( GetTickCount() );
  272. for ( int nIndex = 0; nIndex < E_SNOWCOUNT; nIndex++ )
  273. {
  274. m_snow[ nIndex ].nIndexTexture = rand() % 3;
  275. m_snow[ nIndex ].x = GLfloat( rand() % 200 - 100 );
  276. m_snow[ nIndex ].y = GLfloat( rand() % 200 - 100 );
  277. m_snow[ nIndex ].z = 100.0f + GLfloat( rand() % 25 );
  278. }
  279. for ( int i = 0; i < E_SNOWCOUNT; i++ )
  280. {
  281. m_snow[ i ].nIndexTexture = rand() % 3;
  282. m_snow[ i ].x = GLfloat( rand() % 200 - 100 );
  283. m_snow[ i ].z = GLfloat( rand() % 200 - 100 );
  284. m_snow[ i ].y = 100.0f + GLfloat( rand() % 25 );
  285. m_snow[ i ].xrot = 0;
  286. m_snow[ i ].yrot = 0;
  287. m_snow[ i ].zrot = 0;
  288. m_snow[ i ].dropSpeed = 0.01f * ( rand() % 50 + 2 );
  289. m_snow[ i ].rotSpeed = 0.1f * ( rand() % 10 + 2 );
  290. }
  291. }
  292. // The thread which will play the music
  293. // for the scene...
  294. UINT CChildView::Thread( LPVOID lParam )
  295. {
  296. UNREFERENCED_PARAMETER( lParam );
  297. for ( ; ; )
  298. {
  299. HRSRC hResInfo = FindResource( AfxGetInstanceHandle(), MAKEINTRESOURCE( IDR_MUSIC_SNOW ), "WAVE" );
  300. ASSERT( hResInfo );
  301. HRSRC hRes = ( HRSRC ) LoadResource( AfxGetInstanceHandle(), hResInfo );
  302. ASSERT( hRes );
  303. LPSTR lpRes = ( LPSTR ) LockResource(hRes);
  304. sndPlaySound(lpRes, SND_MEMORY | SND_SYNC | SND_NODEFAULT );
  305. FreeResource( hRes );
  306. }
  307. return 0;
  308. }

下载

源码下载

天天代码码天天
微信公众号
.NET 人工智能实践
注:本文转载自blog.csdn.net的天天代码码天天的文章"https://lw112190.blog.csdn.net/article/details/142320055"。版权归原作者所有,此博客不拥有其著作权,亦不承担相应法律责任。如有侵权,请联系我们删除。
复制链接
复制链接
相关推荐
发表评论
登录后才能发表评论和回复 注册

/ 登录

评论记录:

未查询到任何数据!
回复评论:

分类栏目

后端 (14832) 前端 (14280) 移动开发 (3760) 编程语言 (3851) Java (3904) Python (3298) 人工智能 (10119) AIGC (2810) 大数据 (3499) 数据库 (3945) 数据结构与算法 (3757) 音视频 (2669) 云原生 (3145) 云平台 (2965) 前沿技术 (2993) 开源 (2160) 小程序 (2860) 运维 (2533) 服务器 (2698) 操作系统 (2325) 硬件开发 (2492) 嵌入式 (2955) 微软技术 (2769) 软件工程 (2056) 测试 (2865) 网络空间安全 (2948) 网络与通信 (2797) 用户体验设计 (2592) 学习和成长 (2593) 搜索 (2744) 开发工具 (7108) 游戏 (2829) HarmonyOS (2935) 区块链 (2782) 数学 (3112) 3C硬件 (2759) 资讯 (2909) Android (4709) iOS (1850) 代码人生 (3043) 阅读 (2841)

热门文章

101
推荐
关于我们 隐私政策 免责声明 联系我们
Copyright © 2020-2025 蚁人论坛 (iYenn.com) All Rights Reserved.
Scroll to Top