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

📄 new3dsloader.cpp

📁 多个3ds载入例子运行的时候有些慢候有些慢候有些慢
💻 CPP
📖 第 1 页 / 共 2 页
字号:
				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
}
// 关于对话框消息处理函数
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;
}
//主循环
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 \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\*
/////
/////	初始化函数
/////
///////////////////////////////// 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_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
}
//结束处理
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
}
//设置灯光(位置)
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!");
	}
}

⌨️ 快捷键说明

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