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

📄 app.cpp

📁 体现了lod(level of detail)算法 包括网格细分,空间层次
💻 CPP
字号:
#include <windows.h>
#include <stdio.h>
#include <gl\gl.h>
#include <gl\glu.h>
#include <gl\glaux.h>

#include "winmain.h"
#include "app.h"
#include "Roam.h"
#include "system.h"
#include "glFrustum.h"
#include "game.h"
#include "font.h"

#define FONT_HEIGHT	16

extern int win_x_position;
extern int win_y_position;
extern int win_width;
extern int win_height;
extern int bpp;

extern HDC			hDC;

bool DrawMenu = true;
bool FirstLoading = true;
bool lock=false;
bool wire=false;
bool boxes=false;
bool info=false;
bool piramida=false;
bool Update=false;

float farPlane;
float nearPlane;

float fov;
float LOD;
int m_tris;
int count_nodes;
int cicle;

extern bool MouseMove;
extern float h_scale, v_scale;
extern int offcet_map_x;
extern int offcet_map_z;

extern int POOL_SIZE;

g_gamestate_type g_current, g_previous, prev;

extern int g_tenFrames[10];


float fogColor[4];

CFrustum g_Frustum;

float aspect;

SpeedMeter tess_m, rend_m, sea_m, tot_m, con_m;

Landscape gLand;

void lscCleanup()
{
	//gLand.Exit();
	//DestroyFont();
}

void appInit()
{
	CreateOpenGLFont("Arial", FONT_HEIGHT);

	glClearDepth(1.0f);
	glEnable(GL_DEPTH_TEST);
	glDepthFunc(GL_LEQUAL);

	fogColor[0]=0.2f; fogColor[1]=0.5f; fogColor[2]=1.0f; fogColor[3]=1.0f;
	glEnable(GL_CULL_FACE);
}	

void appCleanup()
{
	lscCleanup();
}

void lscInit()
{
	lscCleanup();
	glPushAttrib(GL_ALL_ATTRIB_BITS);

			glColor3f(0.3f, 0.0f, 0.0f);
			glDrawText(5, 25, "Load Heightmap");
			SwapBuffers( hDC );

			Sleep(500);
			glColor3f(0.0f, 0.2f, 0.0f);
			glDrawText(5, 40, "Load textures");
			SwapBuffers( hDC );
			
			Sleep(500);
	glPopAttrib();

	gLand.Init();
}

void appInitLevel()
{
	FirstLoading=false;

	aspect=(float)win_width/(float)win_height;

	frcam_create();
	
	lscInit();

	tess_m.init(100); rend_m.init(100); sea_m.init(100); con_m.init(100);
	tot_m.init(100); tot_m.startMeter();

	DrawMenu=false;
}


void appReInit()
{}

void PressKey()
{
	if (GetKey('I')){Sleep(600);info=!info;}
	if (GetKey('C')){Sleep(600);lock=!lock;MouseMove = true;}
	if (GetKey('W')) {wire=!wire;Sleep(300);MouseMove = true;}
	if (GetKey('D')) {boxes=!boxes; Sleep(300);MouseMove = true;}
	if (GetKey('F')){Sleep(600); piramida=!piramida;}

	if (GetKey(VK_ESCAPE)) gLand.Exit();
	if (GetKey(VK_SPACE)) {g_current.speed = 0.0f;MouseMove = true;}
	if (GetKey(MK_LBUTTON)) {g_current.YPos+=0.1f;MouseMove = true;}
	if (GetKey(MK_RBUTTON)) {g_current.YPos-=0.1f;MouseMove = true;}


	if (GetKey(VK_NEXT))	{fov+=1; MouseMove = true;}
	if (GetKey(VK_PRIOR))	{fov-=1; MouseMove = true;}


	if (GetKey(VK_ADD))		{Sleep(100); LOD += 0.5f;MouseMove = true;}
	if (GetKey(VK_SUBTRACT)){Sleep(100); LOD -= 0.5f;MouseMove = true;}

	if (GetKey(VK_HOME))	{Sleep(50); farPlane += 10;MouseMove = true;}
	if (GetKey(VK_END))		{Sleep(50); farPlane -= 10;MouseMove = true;}
}

void appDraw()
{/*
	if (GetKey('B')) 
	{ 
		Sleep(200); 
		DrawMenu=!DrawMenu; 
		if (FirstLoading) appInitLevel();
	}
	*/

	if (FirstLoading) appInitLevel(); DrawMenu=false;//temp

	if (DrawMenu) MenuDraw();
	if (!DrawMenu) levelDraw();
}

void MenuDraw()
{
	glClearColor(fogColor[0], fogColor[1], fogColor[2], fogColor[3]);
	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

	glPushAttrib(GL_ALL_ATTRIB_BITS);
			glColor3f(0.3f, 0.0f, 0.0f);
			glDrawText(5, 10, "Press B to start");
	glPopAttrib();

}

void levelDraw()
{
	float fps = 0.0f;
	int i;

	Update=false;

	glClearColor(fogColor[0], fogColor[1], fogColor[2], fogColor[3]);
	glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);

	gUpdate();
	PressKey();

	if (wire) { glDisable(GL_TEXTURE_2D);  glPolygonMode(GL_FRONT, GL_LINE);}
	if (!wire) { glEnable(GL_TEXTURE_2D);  glPolygonMode(GL_FRONT, GL_FILL);}

	glMatrixMode(GL_PROJECTION);
	glLoadIdentity();
	gluPerspective(fov, aspect, nearPlane, farPlane);
	glMatrixMode(GL_MODELVIEW);
	glLoadIdentity();

	
	glRotatef(g_current.XYAngle, 0.0f, 0.0f, 1.0f);//1

	glRotatef(g_current.YZAngle, 1.0f, 0.0f, 0.0f);//2
	glRotatef(g_current.XZAngle, 0.0f, 1.0f, 0.0f);//3 conf
	

	for(i=0;i<10;i++){fps += (float)g_tenFrames[i];}
	fps = fps/10.0f;

	if ((prev.XPos!=g_current.XPos) || (prev.YPos!=g_current.YPos) || (prev.ZPos!=g_current.ZPos) ||
		(prev.XYAngle!=g_current.XYAngle) || (prev.XZAngle!=g_current.XZAngle) || 
		(prev.YZAngle!=g_current.YZAngle)) 
		Update=true;

	glPushMatrix();
	glTranslatef(-g_current.XPos, -g_current.YPos, -g_current.ZPos);
		if(!lock){
			if (MouseMove){
				g_Frustum.UpdateFrustum();
				gLand.Reset(LOD, g_current.radius, g_current.XPos, g_current.YPos, g_current.ZPos);
				gLand.Tessellate();
			}
		}
		gLand.Render();
		if (piramida) drawFrustum(g_current.XPos, 50, g_current.ZPos, g_current.XZAngle, fov);
	glPopMatrix();

	DrawAlignes(&g_current);

		tot_m.stopMeter(); tot_m.startMeter(); con_m.startMeter();
		rDrawTimeGraph(&tot_m);
		
	fps = 1000.0f/fps;

		glPushAttrib(GL_ALL_ATTRIB_BITS); 
			glDrawText(5, 10, "%.1f fps", fps);
			
			if (info){
				glDrawText(5, 25, "%.1f far", farPlane);
				glDrawText(5, 40, "%.1f FOV", fov);
				glDrawText(5, 55, "%.3f LOD", LOD);

				glDrawText(92, 10, "%.2f tess", tess_m.getAvg());
				glDrawText(92, 25, "%.2f rend", rend_m.getAvg());
				glDrawText(92, 40, "%.2f sea", sea_m.getAvg());
				glDrawText(92, 55, "%.2f con", con_m.getAvg());
				glDrawText(92, 70, "%.2f tot", tot_m.getAvg());
			
				glDrawText(165, 10, "SPD: %.1fkm/h", -g_current.speed*3600.0f);
				glDrawText(165, 25, "ALT: %.1fm", g_current.YPos);
				glDrawText(165, 40, "XZAng: %.1fm", g_current.XZAngle);
				glDrawText(165, 55, "YZAng: %.1fm", g_current.YZAngle);
				glDrawText(165, 70, "XYAng: %.1fm", g_current.XYAngle);

				glDrawText(165, 85, "X: %.1fm", (g_current.XPos/h_scale));
				glDrawText(165, 100,"Z: %.1fm", (g_current.ZPos/h_scale));
			
				glDrawText(282, 10, "%d / %d (tris / verts)", m_tris, m_tris*3);
				glDrawText(282, 25, "%.1f Mb mem_used", (float)sys_memUsed / SQR(1024.0f));
				glDrawText(282, 40, "%i / %i (nodes / max)", count_nodes, POOL_SIZE);

				glDrawText(282, 70, "%i offcet_map_x", offcet_map_x);
				glDrawText(282, 85, "%i offcet_map_z", offcet_map_z);

				glDrawText(282, 100, "%i Update", Update);


			}
			
			if(lock) glDrawText(5, 120, "MESH LOCKED");

		glPopAttrib();

		con_m.stopMeter();
		prev=g_current;


		//glFinish();
		glFlush();
}

⌨️ 快捷键说明

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