?? new3dsloader.cpp
字號:
break;
}
break;
case WM_CLOSE: // If the window is being closed
PostQuitMessage(0); // Send a QUIT Message to the window
break;
default: // Return by default
lRet = DefWindowProc (hWnd, uMsg, wParam, lParam);
break;
}
return lRet; // Return by default
}
// 關(guān)于對話框消息處理函數(shù)
LRESULT CALLBACK About(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
{
switch (message)
{
case WM_INITDIALOG:
return TRUE;
case WM_COMMAND:
if (LOWORD(wParam) == IDOK || LOWORD(wParam) == IDCANCEL)
{
EndDialog(hDlg, LOWORD(wParam));
return TRUE;
}
break;
}
return FALSE;
}
//主循環(huán)
WPARAM MainLoop()
{
MSG msg;
while(1) // Do our infinate loop
{ // Check if there was a message
if (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
{
if(msg.message == WM_QUIT) // If the message wasnt to quit
break;
TranslateMessage(&msg); // Find out what the message does
DispatchMessage(&msg); // Execute the message
}
else // if there wasn't a message
{
UpdateScene();
RenderScene(); // Update the screen
}
}
DeInit(); // Clean up and free all allocated memory
return(msg.wParam); // Return from the program
}
///////////////////////////////// INIT GAME WINDOW \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\*
/////
///// 初始化函數(shù)
/////
///////////////////////////////// INIT GAME WINDOW \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\*
void Init(HWND hWnd)
{
GLfloat glDarkColor[] = { 0.1f, 0.1f, 0.1f, 1.0f };
GLfloat glfLightAmbient[] = { 0.2f, 0.2f, 0.2f, 1.0f };
GLfloat glfLightDiffuse[] = { 0.7f, 0.7f, 0.7f, 1.0f };
GLfloat glfLightSpecular[] = { 0.5f, 0.5f, 0.5f, 1.0f };
GLfloat glfLingtPosition1[] = { -200.0f, 200.0f, 100.0f, 1.0f };
GLfloat glfLingtPosition2[] = { 100.0f, 200.0f, 100.0f, 1.0f };
GLfloat glfLingtPosition3[] = { 10.0f, -100.0f, -100.0f, 1.0f };
g_hWnd = hWnd; // Assign the window handle to a global window handle
GetClientRect(g_hWnd, &g_rRect); // Assign the windows rectangle to a global RECT
InitializeOpenGL(g_rRect.right, g_rRect.bottom); // Init OpenGL with the global rect
//讀取3個(gè)模型
g_3dsModel[0].LoadModelFromFile("ground.3ds");
g_3dsModel[1].LoadModelFromFile("cheku.3ds");
g_3dsModel[2].LoadModelFromFile("chanche.3ds");
g_3dsModel[3].LoadModelFromFile("tpole.3ds");
g_3dsModel[3].AlignBottom();
PlayTheSound();
glLightfv (GL_LIGHT0, GL_AMBIENT, glfLightAmbient);
glLightfv (GL_LIGHT0, GL_DIFFUSE, glfLightSpecular);
glLightfv (GL_LIGHT0, GL_SPECULAR, glDarkColor);
glLightfv (GL_LIGHT0, GL_POSITION, glfLingtPosition1);
glLightfv (GL_LIGHT1, GL_AMBIENT, glDarkColor);
glLightfv (GL_LIGHT1, GL_DIFFUSE, glfLightSpecular);
glLightfv (GL_LIGHT1, GL_SPECULAR, glfLightAmbient);
glLightfv (GL_LIGHT1, GL_POSITION, glfLingtPosition2);
glLightfv (GL_LIGHT2, GL_AMBIENT, glDarkColor);
glLightfv (GL_LIGHT2, GL_DIFFUSE, glfLightAmbient);
glLightfv (GL_LIGHT2, GL_SPECULAR, glfLightAmbient);
glLightfv (GL_LIGHT2, GL_POSITION, glfLingtPosition3);
glEnable(GL_LIGHT0); // Turn on a light with defaults set
glEnable(GL_LIGHT1);
glEnable(GL_LIGHT2);
glEnable(GL_LIGHTING);
}
//初始化OpenGL
void InitializeOpenGL(int width, int height)
{
g_hDC = GetDC(g_hWnd); // This sets our global HDC
// We don't free this hdc until the end of our program
if (!bSetupPixelFormat(g_hDC)) // This sets our pixel format/information
PostQuitMessage (0); // If there's an error, quit
g_hRC = wglCreateContext(g_hDC); // This creates a rendering context from our hdc
wglMakeCurrent(g_hDC, g_hRC); // This makes the rendering context we just created the one we want to use
glEnable(GL_TEXTURE_2D); // Enables Texture Mapping
glEnable(GL_DEPTH_TEST); // Enables Depth Testing
SizeOpenGLScreen(width, height); // Setup the screen translations and viewport
}
//改變窗口大小
void SizeOpenGLScreen(int width, int height) // Initialize The GL Window
{
if (height==0) // Prevent A Divide By Zero error
{
height=1; // Make the Height Equal One
}
glViewport(0,0,width,height); // Make our viewport the whole window
// We could make the view smaller inside
// Our window if we wanted too.
// The glViewport takes (x, y, width, height)
// This basically means, what our our drawing boundries
glMatrixMode(GL_PROJECTION); // Select The Projection Matrix
glLoadIdentity(); // Reset The Projection Matrix
// Calculate The Aspect Ratio Of The Window
// The parameters are:
// (view angle, aspect ration of the width to the height,
// The closest distance to the camera before it clips,
// FOV // Ratio // The farthest distance before it stops drawing)
gluPerspective(45.0f,(GLfloat)width/(GLfloat)height, 1.0f ,800.0f);
glMatrixMode(GL_MODELVIEW); // Select The Modelview Matrix
glLoadIdentity(); // Reset The Modelview Matrix
}
//結(jié)束處理
void DeInit()
{
sndPlaySound(NULL,SND_MEMORY|SND_ASYNC|SND_NODEFAULT|SND_LOOP);
if (g_hRC)
{
wglMakeCurrent(NULL, NULL); // This frees our rendering memory and sets everything back to normal
wglDeleteContext(g_hRC); // Delete our OpenGL Rendering Context
}
if (g_hDC)
ReleaseDC(g_hWnd, g_hDC); // Release our HDC from memory
if(g_bFullScreen) // If we were in full screen
{
ChangeDisplaySettings(NULL,0); // If So Switch Back To The Desktop
ShowCursor(TRUE); // Show Mouse Pointer
}
UnregisterClass("bvrain", g_hInstance); // Free the window class
PostQuitMessage (0); // Post a QUIT message to the window
}
//設(shè)置燈光(位置)
void SetLight()
{
static GLfloat glfLingtPosition1[] = { -200.0f, 200.0f, 100.0f, 1.0f };
static GLfloat glfLingtPosition2[] = { 100.0f, 200.0f, 100.0f, 1.0f };
static GLfloat glfLingtPosition3[] = { 10.0f, -100.0f, -100.0f, 1.0f };
glLightfv (GL_LIGHT0, GL_POSITION, glfLingtPosition1);
glLightfv (GL_LIGHT1, GL_POSITION, glfLingtPosition2);
glLightfv (GL_LIGHT2, GL_POSITION, glfLingtPosition3);
}
//更新場景
void UpdateScene()
{
static DWORD elapsedTime = 0;
static DWORD lastTime = 0;
static float froyang =0;
DWORD time = GetTickCount();
elapsedTime = time - lastTime;
if (elapsedTime >=40) //40 毫秒
{
if(g_bRotating)
{
g_fRotateAngle+=float(PI/360);
if(g_fRotateAngle>=2*PI)g_fRotateAngle=0.0f;
g_fEyeX=30*sinf(g_fRotateAngle);
g_fEyeZ=30*cosf(g_fRotateAngle);
}
//鏟車顫動
froyang+=float(PI/2);
if(froyang>=2*PI)froyang=0.0f;
g_fChanCheOffy=0.01f*sinf(froyang);
lastTime = time;
}
}
//繪制場景
void RenderScene()
{
glClearColor(0.8f,0.8f,1.0f,1);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // Clear The Screen And The Depth Buffer
glLoadIdentity(); // Reset The matrix
gluLookAt(g_fEyeX,g_fEyeY,g_fEyeZ,0,0,0,0,1,0);
SetLight();
//地面
g_3dsModel[0].Draw();
//車庫
g_3dsModel[1].Draw();
//鏟車
glPushMatrix();
glTranslatef(-10,1.3f+g_fChanCheOffy,0);
glScalef(0.025f,0.025f,0.025f);
g_3dsModel[2].Draw();
glPopMatrix();
//電線桿
glTranslatef(-10,0,18);
g_3dsModel[3].Draw();
glTranslatef(20,0,0);
g_3dsModel[3].Draw();
SwapBuffers(g_hDC); // Swap the backbuffers to the foreground
}
//播放引擎聲(沒有用DirectSound)
void PlayTheSound()
{
HRSRC hRes; // resource handle to wave file
HGLOBAL hData;
BOOL bOk = FALSE;
HMODULE hModule=NULL;
hRes = ::FindResource(g_hInstance,_T("MOTOR"),_T("WAVE"));
if (hRes != NULL && (hData = ::LoadResource(g_hInstance, hRes)) != NULL)
{
// found the resource, play it
bOk = sndPlaySound((LPCTSTR)::LockResource(hData),
SND_MEMORY|SND_ASYNC|SND_NODEFAULT|SND_LOOP);
FreeResource(hData);
}
if (!bOk)
{
//_T("Unable to play sound!");
}
}
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -