⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 main.cpp

📁 本人的毕业设计 提供给大家 共同学习
💻 CPP
📖 第 1 页 / 共 3 页
字号:
				
				if( g_RotateWanbi < -40.0f )
				{					
				}
				else
				{
					g_RotateWanbi -= 3.0f;
					if( g_RotateWanbi < 0.0f )
					{
						g_RotateWanbixiayouganda = 1.05*g_RotateWanbi;
						g_RotateWanbixiayouganxiao = 0.04*g_RotateWanbi;						
					}
					else
					{
						g_RotateWanbixiayouganxiao = -0.10*g_RotateWanbi;
						g_RotateWanbixiayouganda = 0.9*g_RotateWanbi;
					}					
				}
				break;

			case 0x4B:
				if( g_RotateWanbi > 50.0f )
				{
				}
				else
				{
					g_RotateWanbi += 3.0f;
					if( g_RotateWanbi > 0.0f )
					{
						g_RotateWanbixiayouganxiao = -0.10*g_RotateWanbi;
						g_RotateWanbixiayouganda = 0.9*g_RotateWanbi;
					}
					else
					{
						g_RotateWanbixiayouganda = 1.05*g_RotateWanbi;
						g_RotateWanbixiayouganxiao = 0.04*g_RotateWanbi;						
					}
				}
				break;

/*			case VK_RIGHT:								
				if( g_RotateWhole <= -360.0f )
				{
					g_RotateWhole = 0.0f;
				}

				g_RotateWhole -= 1.0f;

				break;

			case VK_LEFT:								
				if( g_RotateWhole >= 360.0f )
				{
					g_RotateWhole = 0.0f;
				}

				g_RotateWhole += 1.0f;
				
				break;*/
		}
		break;

	case WM_KEYUP:
		keys[wParam] = false;
		break;

    case WM_CLOSE:	

        PostQuitMessage(0);						
        break; 
     
    default:	/*other message*/									
        lRet = DefWindowProc (hWnd, uMsg, wParam, lParam); 
        break; 
    } 
 
    return lRet;										
}



//  从文件中创建纹理
void CreateTexture(UINT textureArray[], LPSTR strFileName, int textureID)
{
	AUX_RGBImageRec *pBitmap = NULL;
	
	if(!strFileName)									// 如果无此文件,则直接返回
		return;

	pBitmap = auxDIBImageLoad(strFileName);				// 装入位图,并保存数据
	
	if(pBitmap == NULL)									// 如果装入位图失败,则退出
		exit(0);

	// 生成纹理
	glGenTextures(1, &textureArray[textureID]);

	// 设置像素对齐格式
	glPixelStorei (GL_UNPACK_ALIGNMENT, 1);

	glBindTexture(GL_TEXTURE_2D, textureArray[textureID]);

	gluBuild2DMipmaps(GL_TEXTURE_2D, 3, pBitmap->sizeX, pBitmap->sizeY, GL_RGB, GL_UNSIGNED_BYTE, pBitmap->data);

	glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR_MIPMAP_NEAREST);
	glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR_MIPMAP_LINEAR);

	if (pBitmap)										// 释放位图占用的资源
	{
		if (pBitmap->data)						
		{
			free(pBitmap->data);				
		}

		free(pBitmap);					
	}
}

void CreateSceneTexture(LPSTR strFileName, GLuint &textureID)
{
	AUX_RGBImageRec *pBitmap = NULL;
	
	if(!strFileName)									// 如果无此文件,则直接返回
		return;

	pBitmap = auxDIBImageLoad(strFileName);				// 装入位图,并保存数据
	
	if(pBitmap == NULL)									// 如果装入位图失败,则退出
		exit(0);

	// 生成纹理
	glGenTextures(1, &textureID);

	// 设置像素对齐格式
	glPixelStorei (GL_UNPACK_ALIGNMENT, 1);

	glBindTexture(GL_TEXTURE_2D, textureID);

	gluBuild2DMipmaps(GL_TEXTURE_2D, 3, pBitmap->sizeX, pBitmap->sizeY, GL_RGB, GL_UNSIGNED_BYTE, pBitmap->data);

	glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR_MIPMAP_NEAREST);
	glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR_MIPMAP_LINEAR);
	glTexParameterf(GL_TEXTURE_2D,GL_TEXTURE_WRAP_S, GL_REPEAT );
	glTexParameterf(GL_TEXTURE_2D,GL_TEXTURE_WRAP_T, GL_REPEAT );

	if (pBitmap)										// 释放位图占用的资源
	{
		if (pBitmap->data)						
		{
			free(pBitmap->data);				
		}

		free(pBitmap);
	}
}

void CreateSceneTextureWithAlpha(LPSTR strFileName, GLuint &textureID)
{
	AUX_RGBImageRec *pBitmap = NULL;
	
	if(!strFileName)									// 如果无此文件,则直接返回
		return;

	pBitmap = auxDIBImageLoad(strFileName);				// 装入位图,并保存数据
	
	if(pBitmap == NULL)									// 如果装入位图失败,则退出
		exit(0);

	unsigned char *pBitmapWithAlpha = (unsigned char *)malloc(pBitmap->sizeX*pBitmap->sizeY*4/3);
	for(int src=0, dst=0; src<pBitmap->sizeX*pBitmap->sizeY; src+=3, dst+=4 )
	{
		if( pBitmap->data[src] == 0 && pBitmap->data[src+1] == 0 && pBitmap->data[src+2] )
		{
			pBitmapWithAlpha[dst+3] = 0;
		}
		else
		{
			pBitmapWithAlpha[dst+3] = 0xFF;
		}

		pBitmapWithAlpha[dst] = pBitmap->data[src];
		pBitmapWithAlpha[dst+1] = pBitmap->data[src+1];
		pBitmapWithAlpha[dst+2] = pBitmap->data[src+2];
	}
	// 生成纹理
	glGenTextures(1, &textureID);

	// 设置像素对齐格式
	glPixelStorei (GL_UNPACK_ALIGNMENT, 1);

	glBindTexture(GL_TEXTURE_2D, textureID);

	gluBuild2DMipmaps(GL_TEXTURE_2D, 4, pBitmap->sizeX, pBitmap->sizeY, GL_RGBA, GL_UNSIGNED_BYTE, pBitmapWithAlpha);

	glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR_MIPMAP_NEAREST);
	glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR_MIPMAP_LINEAR);
	glTexParameterf(GL_TEXTURE_2D,GL_TEXTURE_WRAP_S, GL_REPEAT );
	glTexParameterf(GL_TEXTURE_2D,GL_TEXTURE_WRAP_T, GL_REPEAT );

	if (pBitmap)										// 释放位图占用的资源
	{
		if (pBitmap->data)						
		{
			free(pBitmap->data);				
		}

		free(pBitmap);
	}

	if( pBitmapWithAlpha )
	{
		free( pBitmapWithAlpha );
	}
}

void ChangeToFullScreen()
{
	DEVMODE dmSettings;								

	memset(&dmSettings,0,sizeof(dmSettings));		

	if(!EnumDisplaySettings(NULL,ENUM_CURRENT_SETTINGS,&dmSettings))
	{
		MessageBox(NULL, "Could Not Enum Display Settings", "Error", MB_OK);
		return;
	}

	dmSettings.dmPelsWidth	= SCREEN_WIDTH;		
	dmSettings.dmPelsHeight	= SCREEN_HEIGHT;	
	
	int result = ChangeDisplaySettings(&dmSettings,CDS_FULLSCREEN);	

	if(result != DISP_CHANGE_SUCCESSFUL)
	{

		MessageBox(NULL, "Display Mode Not Compatible", "Error", MB_OK);
		PostQuitMessage(0);
	}
}

HWND CreateMyWindow(LPSTR strWindowName, int width, int height, DWORD dwStyle, bool bFullScreen, HINSTANCE hInstance)
{
	HWND hWnd;
	WNDCLASS wndclass;
	
	memset(&wndclass, 0, sizeof(WNDCLASS));			
	wndclass.style = CS_HREDRAW | CS_VREDRAW;		
	wndclass.lpfnWndProc = WinProc;				
	wndclass.hInstance = hInstance;				
	wndclass.hIcon = LoadIcon(hInstance, (LPCTSTR)IDI_ICON_GRAB);	
	wndclass.hCursor = LoadCursor(NULL, IDC_ARROW);	
	wndclass.hbrBackground = (HBRUSH) (COLOR_WINDOW+1);
	wndclass.lpszClassName = "GameTutorials";		

	RegisterClass(&wndclass);						
	
	if(bFullScreen && !dwStyle) 					
	{												
		dwStyle = WS_POPUP | WS_CLIPSIBLINGS | WS_CLIPCHILDREN;
		ChangeToFullScreen();					
//		ShowCursor(FALSE);						
	}
	else if(!dwStyle)	
	{
		dwStyle = WS_OVERLAPPEDWINDOW | WS_CLIPSIBLINGS | WS_CLIPCHILDREN;
	}		
	
	g_hInstance = hInstance;						

	RECT rWindow;
	rWindow.left	= 0;				
	rWindow.right	= width;			
	rWindow.top	    = 0;					
	rWindow.bottom	= height;					
	AdjustWindowRect( &rWindow, dwStyle, false);	

												
	hWnd = CreateWindow("GameTutorials", strWindowName, dwStyle, 0, 0,
						rWindow.right  - rWindow.left, rWindow.bottom - rWindow.top, 
						NULL, NULL, hInstance, NULL);

	if(!hWnd) return NULL;					

	ShowWindow(hWnd, SW_SHOWNORMAL);	
	UpdateWindow(hWnd);					

	SetFocus(hWnd);					

	return hWnd;
}

bool bSetupPixelFormat(HDC hdc) 
{ 
    PIXELFORMATDESCRIPTOR pfd; 
    int pixelformat; 
 
    pfd.nSize = sizeof(PIXELFORMATDESCRIPTOR);			
    pfd.nVersion = 1;								
													
    pfd.dwFlags = PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER; 
    pfd.dwLayerMask = PFD_MAIN_PLANE;			
    pfd.iPixelType = PFD_TYPE_RGBA;				
    pfd.cColorBits = SCREEN_DEPTH;				
    pfd.cDepthBits = SCREEN_DEPTH;				
    pfd.cAccumBits = 0;						
    pfd.cStencilBits = 0;				
 
    if ( (pixelformat = ChoosePixelFormat(hdc, &pfd)) == FALSE ) 
    { 
        MessageBox(NULL, "ChoosePixelFormat failed", "Error", MB_OK); 
        return FALSE; 
    } 
 
    if (SetPixelFormat(hdc, pixelformat, &pfd) == FALSE) 
    { 
        MessageBox(NULL, "SetPixelFormat failed", "Error", MB_OK); 
        return FALSE; 
    } 
 
    return TRUE;							
}

void SizeOpenGLScreen(int width, int height)		
{
	if (height==0)										
	{
		height=1;										
	}

	glViewport(0,0,width,height);						

	glMatrixMode(GL_PROJECTION);		
	glLoadIdentity();						
	gluPerspective(45.0f,(GLfloat)width/(GLfloat)height, .5f ,150.0f);

	glMatrixMode(GL_MODELVIEW);						
	glLoadIdentity();									
}


void InitializeOpenGL(int width, int height) 
{  
    g_hDC = GetDC(g_hWnd);							
											
    if (!bSetupPixelFormat(g_hDC))			
        PostQuitMessage (0);					

    g_hRC = wglCreateContext(g_hDC);			
    wglMakeCurrent(g_hDC, g_hRC);			

	glEnable(GL_TEXTURE_2D);				
	glEnable(GL_DEPTH_TEST);
	glEnable( GL_CULL_FACE );

//	InitSandParticles();			

	SizeOpenGLScreen(width, height);	
}

void DeInit()
{
	if (g_hRC)											
	{
		wglMakeCurrent(NULL, NULL);						
		wglDeleteContext(g_hRC);						
	}
	
	if (g_hDC) 
		ReleaseDC(g_hWnd, g_hDC);						
		
	if(g_bFullScreen)									
	{
		ChangeDisplaySettings(NULL,0);					
		ShowCursor(TRUE);							
	}

	UnregisterClass("GameTutorials", g_hInstance);	

	PostQuitMessage (0);							
}

BOOL CALLBACK SceneSelectProc(HWND hwndDlg, UINT message, WPARAM wParam, LPARAM lParam) 
{ 
    switch (message) 
    { 
        case WM_COMMAND: 
            switch (LOWORD(wParam)) 
            { 
			  case IDC_BUTTON_ALLSCENE:
				  ClearStatus();
				  if( iSceneFlag == 1 )
				  {
					  MessageBox(hwndDlg, "already in this scene", "NOTE", MB_OK );
					  return TRUE;
				  }
				  if( bFirstEntryAll )
				  {
//					  ClearScene();
					  InitAllScene();
				  }
				  
				  iSceneFlag = 1;
				  
//				  MessageBox(NULL, "allscene", "NOTE", MB_OK );
				  bFirstEntryAll = false;
				  EndDialog(hwndDlg, wParam);
				  return TRUE; 



//				  iSceneFlag = 2;
				  
//				  bFirstEntrySand = false;
//				  MessageBox(NULL, "sand", "NOTE", MB_OK );
//				  EndDialog(hwndDlg, wParam);
//				  return TRUE; 

			  case IDC_BUTTON_SOIL:
				  ClearStatus();

				  if( iSceneFlag == 3 )
				  {
					  MessageBox(hwndDlg, "already in this scene", "NOTE", MB_OK );
					  return true;
				  }

				  iSceneFlag = 3;
				  if( bFirstEntrySoil )
				  {
					  InitSoilScene();
				  }
				  
				  bFirstEntrySoil = false;
//				  MessageBox(NULL, "soil", "NOTE", MB_OK );
				  EndDialog(hwndDlg, wParam);
				  return TRUE; 


			  case IDOK:
				  if( bFirstEntryAll && bFirstEntrySand && bFirstEntrySoil
					  && bFirstEntryLoad && bFirstEntryStone )
				  {
					  exit( 1 );
				  }

				  ClearStatus();

				  EndDialog(hwndDlg, wParam);
				  return TRUE; 
            }
			break;
    } 
    return FALSE; 
} 

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hprev, PSTR cmdline, int ishow)
{	
	DialogBox( hInstance, MAKEINTRESOURCE(IDD_DIALOG_SCENESELECT), g_hWnd, (DLGPROC)SceneSelectProc );

	HWND hWnd;

	// 判断用户是否需要全屏显示

	g_bFullScreen = true;
	
	hWnd = CreateMyWindow("挖掘机虚拟", SCREEN_WIDTH, SCREEN_HEIGHT, 0, g_bFullScreen, hInstance);

	ShowCursor( false );
	if(hWnd == NULL)
	{
		return true;
	}
	// 初始化OpenGL
	Init(hWnd);	
	
	return MainLoop();						
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -