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

📄 utility.cpp

📁 ROAM实时动态LOD地形渲染
💻 CPP
📖 第 1 页 / 共 2 页
字号:
	glColor3f(1, 1, 1);

	SetDrawModeContext();
}

// ---------------------------------------------------------------------
// Key Binding Functions
//
void KeyObserveToggle(void)
{
	gCameraMode++;
	if ( gCameraMode > FLY_MODE )
		gCameraMode = FOLLOW_MODE;

	// Turn animation back on...
	if (gCameraMode == FOLLOW_MODE)
		gAnimating = 1;
}

void KeyDrawModeSurf(void)
{
	gDrawMode++;
	if ( gDrawMode > DRAW_USE_WIREFRAME )
		gDrawMode = DRAW_USE_TEXTURE;

	SetDrawModeContext( );
}

void KeyForward(void)
{
	switch ( gCameraMode )
	{
	default:
	case FOLLOW_MODE:
		break;

	case OBSERVE_MODE:
		gCameraPosition[2] += 5.0f;
		break;

	case DRIVE_MODE:
		gViewPosition[0] += 5.0f * sinf( gCameraRotation[ROTATE_YAW] * M_PI / 180.0f );
		gViewPosition[2] -= 5.0f * cosf( gCameraRotation[ROTATE_YAW] * M_PI / 180.0f );

		if ( gViewPosition[0] > MAP_SIZE ) gViewPosition[0] = MAP_SIZE;
		if ( gViewPosition[0] < 0) gViewPosition[0] = 0;

		if ( gViewPosition[2] > MAP_SIZE ) gViewPosition[2] = MAP_SIZE;
		if ( gViewPosition[2] < 0) gViewPosition[2] = 0;

		gViewPosition[1] = (MULT_SCALE * gHeightMap[(int)gViewPosition[0] + ((int)gViewPosition[2] * MAP_SIZE)]) + 4.0f;
		break;

	case FLY_MODE:
		gViewPosition[0] += 5.0f * sinf( gCameraRotation[ROTATE_YAW]   * M_PI / 180.0f )
								 * cosf( gCameraRotation[ROTATE_PITCH] * M_PI / 180.0f );
		gViewPosition[2] -= 5.0f * cosf( gCameraRotation[ROTATE_YAW]   * M_PI / 180.0f )
								 * cosf( gCameraRotation[ROTATE_PITCH] * M_PI / 180.0f );
		gViewPosition[1] -= 5.0f * sinf( gCameraRotation[ROTATE_PITCH] * M_PI / 180.0f );
		break;
	}
}

void KeyLeft(void)
{
	if ( gCameraMode == OBSERVE_MODE )
		gCameraRotation[1] -= 5.0f;
}

void KeyBackward(void)
{
	switch ( gCameraMode )
	{
	default:
	case FOLLOW_MODE:
		break;
		
	case OBSERVE_MODE:
		gCameraPosition[2] -= 5.0f;
		break;

	case DRIVE_MODE:
		gViewPosition[0] -= 5.0f * sinf( gCameraRotation[ROTATE_YAW] * M_PI / 180.0f );
		gViewPosition[2] += 5.0f * cosf( gCameraRotation[ROTATE_YAW] * M_PI / 180.0f );

		if ( gViewPosition[0] > MAP_SIZE ) gViewPosition[0] = MAP_SIZE;
		if ( gViewPosition[0] < 0) gViewPosition[0] = 0;

		if ( gViewPosition[2] > MAP_SIZE ) gViewPosition[2] = MAP_SIZE;
		if ( gViewPosition[2] < 0) gViewPosition[2] = 0;

		gViewPosition[1] = (MULT_SCALE * gHeightMap[(int)gViewPosition[0] + ((int)gViewPosition[2] * MAP_SIZE)]) + 4.0f;
		break;

	case FLY_MODE:
		gViewPosition[0] -= 5.0f * sinf( gCameraRotation[ROTATE_YAW]   * M_PI / 180.0f )
								 * cosf( gCameraRotation[ROTATE_PITCH] * M_PI / 180.0f );
		gViewPosition[2] += 5.0f * cosf( gCameraRotation[ROTATE_YAW]   * M_PI / 180.0f )
								 * cosf( gCameraRotation[ROTATE_PITCH] * M_PI / 180.0f );
		gViewPosition[1] += 5.0f * sinf( gCameraRotation[ROTATE_PITCH] * M_PI / 180.0f );
		break;
	}
}

void KeyRight(void)
{
	if ( gCameraMode == OBSERVE_MODE )
		gCameraRotation[1] += 5.0f;
}

void KeyAnimateToggle(void)
{
	gAnimating = !gAnimating;
}

void KeyDrawFrustumToggle(void)
{
	gDrawFrustum = !gDrawFrustum;
}

void KeyUp(void)
{
	if ( gCameraMode == OBSERVE_MODE )
		gCameraPosition[1] -= 5.0f;
}

void KeyDown(void)
{
	if ( gCameraMode == OBSERVE_MODE )
		gCameraPosition[1] += 5.0f;
}

void KeyMoreDetail(void)
{
	gDesiredTris += 500;
	if ( gDesiredTris > 20000 )
		gDesiredTris = 20000;
}

void KeyLessDetail(void)
{
	gDesiredTris -= 500;
	if ( gDesiredTris < 500 )
		gDesiredTris = 500;
}

// ---------------------------------------------------------------------
// Called when the window has changed size
//
void ChangeSize(GLsizei w, GLsizei h)
{
	GLfloat fAspect;
	// Prevent a divide by zero, when window is too short
	// (you cant make a window of zero width).
	if(h == 0)
		h = 1;

	// Set the viewport to be the entire window
    glViewport(0, 0, w, h);

	fAspect = (GLfloat)w/(GLfloat)h;

	// Reset coordinate system
	glMatrixMode(GL_PROJECTION);
	glLoadIdentity();
	
	{
		// Produce the perspective projection
		double  left;
		double  right;
		double  bottom;
		double  top;
		
		right = NEAR_CLIP * tan( gFovX/2.0 * M_PI/180.0f );
		top = right / fAspect;
		bottom = -top;
		left = -right;
		glFrustum(left, right, bottom, top, NEAR_CLIP, FAR_CLIP);
	}

	glMatrixMode(GL_MODELVIEW);
	glLoadIdentity();
}

// --------------------------------------------------------------------
void KeyFOVDown(void)
{
	gFovX -= 1.0f;
	ChangeSize(WINDOW_WIDTH, WINDOW_HEIGHT);
}

void KeyFOVUp(void)
{
	gFovX += 1.0f;
	ChangeSize(WINDOW_WIDTH, WINDOW_HEIGHT);
}


// ---------------------------------------------------------------------
// Called to update the window
//
void RenderScene(void)
{
	// Clear the GL buffer
	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

	glMatrixMode(GL_MODELVIEW);
	glPushMatrix();

	switch (gCameraMode)
	{
	default:
	case FOLLOW_MODE:
		// Face the center of the map, while walking in a circle around the midpoint (animated).
		glRotatef(-gAnimateAngle, 0.f, 1.f, 0.f);
		glTranslatef(-gViewPosition[0], -gViewPosition[1] , -gViewPosition[2]);

		gClipAngle = -gAnimateAngle;
		break;

	case OBSERVE_MODE:
		
		// Face the center of the map from a certain distance & allow user to orbit around the midpoint.
		//
		// Move to the Camera Position
		glTranslatef(0.f, gCameraPosition[1], gCameraPosition[2]);

		// Rotate to the Camera Rotation angle
		glRotatef(gCameraRotation[ROTATE_PITCH], 1.f, 0.f, 0.f);
		glRotatef(gCameraRotation[ROTATE_YAW],   0.f, 1.f, 0.f);

		// Adjust the origin to be the center of the map...
		glTranslatef(-((GLfloat) MAP_SIZE * 0.5f), 0.f, -((GLfloat) MAP_SIZE * 0.5f));
		
		gClipAngle = -gAnimateAngle;
		break;

	case DRIVE_MODE:
	case FLY_MODE:
		gAnimating = 0;

		// Rotate to the Camera Rotation angle
		glRotatef(gCameraRotation[ROTATE_PITCH], 1.f, 0.f, 0.f);
		glRotatef(gCameraRotation[ROTATE_YAW],   0.f, 1.f, 0.f);

		// Move to the Camera Position
		glTranslatef(-gViewPosition[0], -gViewPosition[1] , -gViewPosition[2]);

		gClipAngle = gCameraRotation[ROTATE_YAW];
		break;
	}

	// Perform the actual rendering of the mesh.
	roamDrawFrame();

	if ( gDrawFrustum )
		drawFrustum();

	glPopMatrix();

	// Increment the frame counter.
	gNumFrames++;
}

// ---------------------------------------------------------------------
// Called when user moves the mouse
//
void MouseMove(int mouseX, int mouseY)
{
	// If we're rotating, perform the updates needed
	if ( gRotating &&
		(gCameraMode != FOLLOW_MODE))
	{
		int dx, dy;
		
		// Check for start flag
		if ( gStartX == -1 )
		{
			gStartX = mouseX;
			gStartY = mouseY;
		}
		
		// Find the delta of the mouse
		dx = mouseX - gStartX;

		if ( gCameraMode == OBSERVE_MODE )
			dy = mouseY - gStartY;
		else
			dy = gStartY - mouseY;		// Invert mouse in Drive/Fly mode

		// Update the camera rotations
		gCameraRotation[0] += (GLfloat) dy * 0.5f;
		gCameraRotation[1] += (GLfloat) dx * 0.5f;
	
		// Reset the deltas for the next idle call
		gStartX = mouseX;
		gStartY = mouseY;
	}
}

// ---------------------------------------------------------------------
// Called when application is idle
//
void IdleFunction(void)
{
	// If animating, move the view position
	if (gAnimating)
	{
		gAnimateAngle += 0.4f;

		gViewPosition[0] = ((GLfloat) MAP_SIZE / 4.f) + ((sinf(gAnimateAngle * M_PI / 180.f) + 1.f) * ((GLfloat) MAP_SIZE / 4.f));
		gViewPosition[2] = ((GLfloat) MAP_SIZE / 4.f) + ((cosf(gAnimateAngle * M_PI / 180.f) + 1.f) * ((GLfloat) MAP_SIZE / 4.f));

		gViewPosition[1] = (MULT_SCALE * gHeightMap[(int)gViewPosition[0] + ((int)gViewPosition[2] * MAP_SIZE)]) + 4.0f;
	}
}

// ---------------------------------------------------------------------
// This function does any needed initialization on the rendering
// context.  Here it sets up and initializes the lighting for
// the scene.
void SetupRC()
{
	glEnable(GL_DEPTH_TEST);	// Hidden surface removal
	glFrontFace(GL_CCW);		// Counter clock-wise polygons face out
	glEnable(GL_CULL_FACE);		// Cull back-facing triangles

	glClearColor( 0.0f, 0.0f, 0.0f, 0.0f );

// ----------- LIGHTING SETUP -----------
	// Light values and coordinates
	GLfloat  whiteLight[]    = { 0.45f,  0.45f, 0.45f, 1.0f };
	GLfloat  ambientLight[]  = { 0.25f,  0.25f, 0.25f, 1.0f };
	GLfloat  diffuseLight[]  = { 0.50f,  0.50f, 0.50f, 1.0f };
	GLfloat	 lightPos[]      = { 0.00f,300.00f, 0.00f, 0.0f };
	
	// Setup and enable light 0
	glLightModelfv(GL_LIGHT_MODEL_AMBIENT,ambientLight);
	glLightfv(GL_LIGHT0,GL_AMBIENT,ambientLight);
	glLightfv(GL_LIGHT0,GL_DIFFUSE,diffuseLight);
	glLightfv(GL_LIGHT0,GL_POSITION,lightPos);
	glEnable(GL_LIGHT0);

	// Enable color tracking
	glEnable(GL_COLOR_MATERIAL);
	
	// Set Material properties to follow glColor values
	glColorMaterial(GL_FRONT, GL_AMBIENT_AND_DIFFUSE);

	// Set the color for the landscape
	glMaterialfv( GL_FRONT, GL_AMBIENT_AND_DIFFUSE, whiteLight );

// ----------- TEXTURE SETUP -----------
	// Use Generated Texture Coordinates
	static GLfloat s_vector[4] = { 1.0/(GLfloat)TEXTURE_SIZE, 0, 0, 0 };
	static GLfloat t_vector[4] = { 0, 0, 1.0/(GLfloat)TEXTURE_SIZE, 0 };

	glTexGeni( GL_S, GL_TEXTURE_GEN_MODE, GL_OBJECT_LINEAR );
	glTexGenfv( GL_S, GL_OBJECT_PLANE, s_vector );

	glTexGeni( GL_T, GL_TEXTURE_GEN_MODE, GL_OBJECT_LINEAR );
	glTexGenfv( GL_T, GL_OBJECT_PLANE, t_vector );
}

⌨️ 快捷键说明

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