📄 new3dsloader.cpp
字号:
// New3DSLoader.cpp : Defines the entry point for the application.
//
#if !defined(AFX_STDAFX_H__A9DB83DB_A9FD_11D0_BFD1_444553540000__INCLUDED_)
#define AFX_STDAFX_H__A9DB83DB_A9FD_11D0_BFD1_444553540000__INCLUDED_
#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000
#define WIN32_LEAN_AND_MEAN // Exclude rarely-used stuff from Windows headers
// Windows Header Files:
#include <windows.h>
// C RunTime Header Files
#include <stdlib.h>
#include <malloc.h>
#include <memory.h>
#include <tchar.h>
#define SCREEN_WIDTH 800 // We want our screen width 800 pixels
#define SCREEN_HEIGHT 600 // We want our screen height 600 pixels
#define SCREEN_DEPTH 16 // We want 16 bits per pixel
// Local Header Files
#include <gl\gl.h> // Header File For The OpenGL32 Library
#include <gl\glu.h> // Header File For The GLu32 Library
#include <gl\glaux.h>
#include <mmsystem.h>
//程序路径
extern char g_sAppPath[512];
//素材路径
extern char g_sMediaPath[512];
//纹理ID
extern UINT g_Texture[100];
//函数声明
void CreateTexture(UINT textureArray[], LPSTR strFileName, int textureID);
void GetFilePath(char * filename);
//{{AFX_INSERT_LOCATION}}
// Microsoft Visual C++ will insert additional declarations immediately before the previous line.
#endif // !defined(AFX_STDAFX_H__A9DB83DB_A9FD_11D0_BFD1_444553540000__INCLUDED_)
#include <math.h>
#include "resource.h"
#pragma comment(lib, "opengl32.lib")
#pragma comment(lib, "glu32.lib")
#pragma comment(lib, "glaux.lib")
#include "3dsModel.h"
#define SCREEN_WIDTH 800 // We want our screen width 800 pixels
#define SCREEN_HEIGHT 600 // We want our screen height 600 pixels
#define SCREEN_DEPTH 16 // We want 16 bits per pixel
//程序路径
char g_sAppPath[512];
//模型路径
char g_sMediaPath[512];
//纹理号表(100个)
UINT g_Texture[100] = {0}; // This holds the texture info, referenced by an ID
//全局参数
bool g_bFullScreen = true;
UINT g_ViewMode; // Set full screen as default
BOOL g_bLighting=FALSE;
HWND g_hWnd; // This is the handle for the window
RECT g_rRect; // This holds the window dimensions
HDC g_hDC; // General HDC - (handle to device context)
HGLRC g_hRC; // General OpenGL_DC - Our Rendering Context for OpenGL
HINSTANCE g_hInstance; // This holds the global hInstance for UnregisterClass() in DeInit()
float g_fEyeX=0;
float g_fEyeZ=0;
float g_fEyeY=5;
float g_fRotateAngle=3.14f;
//铲车
float g_fChanCheOffy=0;
//旋转标志
BOOL g_bRotating=TRUE;
//4个模型
C3DSModel g_3dsModel[4];
//
ATOM MyRegisterClass(HINSTANCE hInstance);
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
LRESULT CALLBACK About(HWND, UINT, WPARAM, LPARAM);
HWND CreateMyWindow(LPSTR strWindowName, int width, int height, DWORD dwStyle, bool bFullScreen, HINSTANCE hInstance);
void ChangeToFullScreen();
WPARAM MainLoop();
void InitializeOpenGL(int width, int height);
void SizeOpenGLScreen(int width, int height);
bool bSetupPixelFormat(HDC hdc);
void Init(HWND hWnd);
void DeInit();
void RenderScene();
void UpdateScene();
void PlayTheSound();
//设置灯光(位置)
void SetLight();
int APIENTRY WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow)
{
HWND hWnd;
//应用程序路径
GetModuleFileName(hInstance,g_sAppPath,512);
GetFilePath (g_sAppPath);
//素材路径
strcpy(g_sMediaPath,g_sAppPath);
strcat(g_sMediaPath,"models\\");
//是否使用全屏幕模式
if(MessageBox(NULL, "是否使用全屏幕模式?", "窗口选择", MB_YESNO | MB_ICONQUESTION) == IDNO)
g_bFullScreen = false;
//生成窗口
hWnd = CreateMyWindow("www.bvrain.com", SCREEN_WIDTH, SCREEN_HEIGHT, 0, g_bFullScreen, hInstance);
// 如果没有成功,退出
if(hWnd == NULL) return true;
// 初始化OPENGL
Init(hWnd);
// 主循环
return MainLoop();
}
//生成窗口
HWND CreateMyWindow(LPSTR strWindowName, int width, int height, DWORD dwStyle, bool bFullScreen, HINSTANCE hInstance)
{
HWND hWnd;
// 注册窗口类
MyRegisterClass(hInstance);
//全屏幕判断
if(bFullScreen && !dwStyle) // Check if we wanted full screen mode
{ // Set the window properties for full screen mode
dwStyle = WS_POPUP | WS_CLIPSIBLINGS | WS_CLIPCHILDREN;
ChangeToFullScreen(); // Go to full screen
//ShowCursor(FALSE); // Hide the cursor
}
else if(!dwStyle) // Assign styles to the window depending on the choice
dwStyle = WS_OVERLAPPEDWINDOW | WS_CLIPSIBLINGS | WS_CLIPCHILDREN;
g_hInstance = hInstance; // Assign our global hInstance to the window's hInstance
RECT rWindow;
if(bFullScreen)
{
rWindow.left = 0;
rWindow.right = width; // Set Right Value To Requested Width
rWindow.top = 0;
rWindow.bottom = height;
}
else
{
rWindow.left = (GetSystemMetrics(SM_CXSCREEN)-width)/2; // Set Left Value To 0
rWindow.right = rWindow.left+width; // Set Right Value To Requested Width
rWindow.top = (GetSystemMetrics(SM_CYSCREEN)-height)/2; // Set Top Value To 0
rWindow.bottom = rWindow.top+height; // Set Bottom Value To Requested Height
}
AdjustWindowRect( &rWindow, dwStyle, false); // Adjust Window To True Requested Size
// Create the window
hWnd = CreateWindow("bvrain", strWindowName, dwStyle, rWindow.left, rWindow.top,
width, height,
NULL, NULL, hInstance, NULL);
if(!hWnd) return NULL; // If we could get a handle, return NULL
ShowWindow(hWnd, SW_SHOWNORMAL); // Show the window
UpdateWindow(hWnd); // Draw the window
SetFocus(hWnd); // Sets Keyboard Focus To The Window
return hWnd;
}
//注册窗口类
ATOM MyRegisterClass(HINSTANCE hInstance)
{
WNDCLASSEX wcex;
wcex.cbSize = sizeof(WNDCLASSEX);
wcex.style = CS_HREDRAW | CS_VREDRAW;
wcex.lpfnWndProc = (WNDPROC)WndProc;
wcex.cbClsExtra = 0;
wcex.cbWndExtra = 0;
wcex.hInstance = hInstance;
wcex.hIcon = LoadIcon(hInstance, (LPCTSTR)IDI_NEW3DSLOADER);
wcex.hCursor = LoadCursor(NULL, IDC_ARROW);
wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
wcex.lpszMenuName = (LPCSTR)IDC_NEW3DSLOADER;
wcex.lpszClassName = "bvrain";
wcex.hIconSm = LoadIcon(wcex.hInstance, (LPCTSTR)IDI_SMALL);
return RegisterClassEx(&wcex);
}
//窗口消息处理函数
LRESULT CALLBACK WndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
int wmId, wmEvent;
LONG lRet = 0;
PAINTSTRUCT ps;
switch (uMsg)
{
case WM_COMMAND:
wmId = LOWORD(wParam);
wmEvent = HIWORD(wParam);
// Parse the menu selections:
switch (wmId)
{
case IDM_ABOUT:
DialogBox(g_hInstance, (LPCTSTR)IDD_ABOUTBOX, hWnd, (DLGPROC)About);
break;
case IDM_EXIT:
PostQuitMessage(0);
break;
default:
return DefWindowProc(hWnd, uMsg, wParam, lParam);
}
break;
case WM_SIZE: // If the window is resized
if(!g_bFullScreen) // Do this only if we are NOT in full screen
{
SizeOpenGLScreen(LOWORD(lParam),HIWORD(lParam));// LoWord=Width, HiWord=Height
GetClientRect(hWnd, &g_rRect); // Get the window rectangle
}
break;
case WM_PAINT: // If we need to repaint the scene
BeginPaint(hWnd, &ps); // Init the paint struct
EndPaint(hWnd, &ps); // EndPaint, Clean up
break;
case WM_LBUTTONDOWN: // If the left mouse button was clicked
g_bRotating=!g_bRotating;
break;
case WM_RBUTTONDOWN: // If the right mouse button was clicked.
g_bLighting = !g_bLighting; // Turn lighting ON/OFF
if(g_bLighting) { // If lighting is ON
glEnable(GL_LIGHTING); // Enable OpenGL lighting
} else {
glDisable(GL_LIGHTING); // Disable OpenGL lighting
}
break;
case WM_KEYDOWN: // If we pressed a key
switch(wParam) { // Check if we hit a key
case VK_ESCAPE: // If we hit the escape key
PostQuitMessage(0); // Send a QUIT message to the window
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -