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

📄 ogrepaginglandscapepagedata2d_spline.cpp

📁 使用stl技术,(还没看,是听说的)
💻 CPP
字号:
/********************************************************************************
	OgrePagingLandScapePageData2D_Spline.cpp
	*****************************************************************************
	A NURBS-based heightfield generator for use with the paginglandscapeplugin
	
	Note that it could easily be adapted for use as a general NURBS surface
	generator.
	*****************************************************************************
	begin                : Sat Nov 9 2003
	copyright            : (C) 2003 Chris "Antiarc" Heald
	email                : antiarc@captionthis.com
********************************************************************************/

/********************************************************************************
*																				*
*   This program is free software; you can redistribute it and/or modify		*
*   it under the terms of the GNU Lesser General Public License as published by *
*   the Free Software Foundation; either version 2 of the License, or			*
*   (at your option) any later version.											*
*																				*
********************************************************************************/

#include <stdlib.h>
#include <iostream>
#include "time.h"
#include "math.h"



#include <OgreStringConverter.h>
#include <OgreException.h>
#include "OgreLogManager.h"

#include "OgrePagingLandScapePageData2D_Spline.h"
#include "OgrePagingLandScapeOptions.h"

namespace Ogre
{

PagingLandScapePageData2D_Spline::PagingLandScapePageData2D_Spline()
: PagingLandScapePageData2D()
{
}

//-----------------------------------------------------------------------
PagingLandScapePageData2D_Spline::~PagingLandScapePageData2D_Spline()
{
	if(mSurface != 0)
		delete mSurface;
	if(mPoints != 0)
		delete[] mPoints;
	mLoaded = false;
}

//-----------------------------------------------------------------------
void PagingLandScapePageData2D_Spline::load( float mX, float mZ ) {
	if(!mLoaded) {
		PagingLandScapeOptions *options = PagingLandScapeOptions::getSingletonPtr();

		int resolution	= options->PageSize;
		mResolution		= resolution;
		mMax			= mResolution * mResolution;
		int pCount		= 50;
		int mDegree		= 3;
		float MAX		= 500;
		float MIN		= 0;
		int tessLevel	= mResolution;
 
		srand(time(NULL));
		int j = 0;

		mPoints = new Point4D[pCount * pCount];
		const int knotVecSize = pCount + mDegree + 1;
		float *knots = new float[knotVecSize];
		int i;
		for(i=0;i<knotVecSize;i++) {
			if (i<mDegree)
				knots[i] = 0;
			else if (i>knotVecSize-mDegree)
				knots[i] = knotVecSize - (2 * mDegree) + 1;
			else
				knots[i] = i - mDegree + 1;
		}
		int dataSize = pCount * pCount;
		for(i=0;i<dataSize; i++) {
			mPoints[i].x = (int)i/pCount;
			mPoints[i].y = ((double)rand() / (double)(MAX));
			mPoints[i].w = 1;
			mPoints[i].z = 0;//i % pCount;
		}
		mSurface = new CDRGNURBSSurface();
		mSurface->Init(mDegree, mDegree, pCount, pCount, mPoints, knots, knots, tessLevel, tessLevel);
		mSurface->TessellateSurface();
		delete[] knots;
		delete[] mPoints;
		mLoaded = true;
	}
}

//-----------------------------------------------------------------------
void PagingLandScapePageData2D_Spline::unload() {
	if(mSurface != 0)
		delete mSurface;
	mLoaded = false;
}

float PagingLandScapePageData2D_Spline::getHeight(float xt, float zt) {
	int Pos = (xt * mResolution) + zt;
	if ( mMax > Pos ) {
		float retval = mSurface->getData(Pos).y;
		return retval;
	} else {
		return 0.0;
	}
}             
//-----------------------------------------------------------------------

//-----------------------------------------------------------------------
float PagingLandScapePageData2D_Spline::getError( float iniX, float iniZ, float endX, float endZ )
{
	int stepX = ( endX - iniX ) / 2;
	int stepZ = ( endZ - iniZ ) / 2;
	int Height[ 9 ];
	int Interpolated[ 5 ];
	int *pInt = Height;

	for ( int k = iniZ; k <= endZ; k += stepZ )
	{
		for ( int i = iniX; i <= endX; i += stepX )
		{
			*pInt++ = getHeight( i, k );
		}
	}
	Interpolated[ 0 ] = ( Height[ 0 ] + Height[ 2 ] ) / 2;
	Interpolated[ 1 ] = ( Height[ 0 ] + Height[ 6 ] ) / 2; 
	Interpolated[ 3 ] = ( Height[ 2 ] + Height[ 8 ] ) / 2;
	Interpolated[ 4 ] = ( Height[ 6 ] + Height[ 8 ] ) / 2;
	Interpolated[ 2 ] = ( Interpolated[ 1 ] + Interpolated[ 3 ] ) / 2;
	
	if ( Interpolated[ 0 ] >= Height[ 1 ] )
	{
		Interpolated[ 0 ] = Interpolated[ 0 ] - Height[ 1 ];
	}
	else
	{
		Interpolated[ 0 ]= Height[ 1 ] - Interpolated[ 0 ];
	}

	if ( Interpolated[ 1 ] >= Height[ 3 ] )
	{
		Interpolated[ 1 ] = Interpolated[ 1 ] - Height[ 3 ];
	}
	else
	{
		Interpolated[ 1 ]= Height[ 3 ] - Interpolated[ 1 ];
	}

	if ( Interpolated[ 2 ] >= Height[ 4 ] )
	{
		Interpolated[ 2 ] = Interpolated[ 2 ] - Height[ 4 ];
	}
	else
	{
		Interpolated[ 2 ]= Height[ 4 ] - Interpolated[ 2 ];
	}

	if ( Interpolated[ 3 ] >= Height[ 5 ] )
	{
		Interpolated[ 3 ] = Interpolated[ 3 ] - Height[ 5 ];
	}
	else
	{
		Interpolated[ 3 ]= Height[ 5 ] - Interpolated[ 3 ];
	}

	if ( Interpolated[ 4 ] >= Height[ 7 ] )
	{
		Interpolated[ 4 ] = Interpolated[ 4 ] - Height[ 7 ];
	}
	else
	{
		Interpolated[ 4 ]= Height[ 7 ] - Interpolated[ 4 ];
	}

	return (float)( _Max( Interpolated[ 0 ], _Max( Interpolated[ 1 ], _Max( Interpolated[ 2 ], _Max( Interpolated[ 3 ], Interpolated[ 4 ] ) ) ) ) );
}

} //namespace

⌨️ 快捷键说明

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