axmol/core/3d/CCTerrain.cpp

1846 lines
60 KiB
C++
Raw Normal View History

2019-11-23 20:27:39 +08:00
/****************************************************************************
Copyright (c) 2015-2016 Chukong Technologies Inc.
Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
2022-01-04 12:36:20 +08:00
https://adxeproject.github.io/
2019-11-23 20:27:39 +08:00
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
****************************************************************************/
#include "3d/CCTerrain.h"
USING_NS_CC;
#include <stdlib.h>
#include <float.h>
#include <set>
2021-12-25 10:04:45 +08:00
#include <stddef.h> // offsetof
2019-11-23 20:27:39 +08:00
#include "renderer/CCRenderer.h"
#include "renderer/ccShaders.h"
#include "renderer/backend/Device.h"
#include "renderer/backend/Program.h"
#include "renderer/backend/Buffer.h"
#include "base/CCDirector.h"
#include "base/ccTypes.h"
#include "base/CCEventType.h"
#include "2d/CCCamera.h"
#include "platform/CCImage.h"
#include "3d/CC3DProgramInfo.h"
2020-10-04 23:43:57 +08:00
#include "base/ccUtils.h"
2019-11-23 20:27:39 +08:00
NS_CC_BEGIN
2021-12-25 10:04:45 +08:00
namespace
{
// It's used for creating a default texture when lightMap is nullpter
static unsigned char cc_2x2_white_image[] = {
// RGBA8888
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
} // namespace
2019-11-23 20:27:39 +08:00
2021-12-25 10:04:45 +08:00
Terrain* Terrain::create(TerrainData& parameter, CrackFixedType fixedType)
2019-11-23 20:27:39 +08:00
{
2021-12-25 10:04:45 +08:00
Terrain* terrain = new Terrain();
2019-11-23 20:27:39 +08:00
if (terrain->initWithTerrainData(parameter, fixedType))
{
terrain->autorelease();
return terrain;
}
CC_SAFE_DELETE(terrain);
return terrain;
}
2021-12-25 10:04:45 +08:00
bool Terrain::initWithTerrainData(TerrainData& parameter, CrackFixedType fixedType)
2019-11-23 20:27:39 +08:00
{
this->setSkirtHeightRatio(parameter._skirtHeightRatio);
2021-12-25 10:04:45 +08:00
this->_terrainData = parameter;
this->_crackFixedType = fixedType;
2019-11-23 20:27:39 +08:00
this->_isCameraViewChanged = true;
2021-12-25 10:04:45 +08:00
// chunksize
2019-11-23 20:27:39 +08:00
this->_chunkSize = parameter._chunkSize;
2021-12-25 10:04:45 +08:00
bool initResult = true;
2019-11-23 20:27:39 +08:00
2021-12-25 10:04:45 +08:00
// init heightmap
2019-11-23 20:27:39 +08:00
initResult &= this->initHeightMap(parameter._heightMapSrc);
2021-12-25 10:04:45 +08:00
// init textures alpha map,detail Maps
2019-11-23 20:27:39 +08:00
initResult &= this->initTextures();
initResult &= this->initProperties();
return initResult;
}
void cocos2d::Terrain::setLightMap(std::string_view fileName)
2019-11-23 20:27:39 +08:00
{
CC_SAFE_RELEASE(_lightMap);
2021-12-08 00:11:53 +08:00
auto image = new Image();
2019-11-23 20:27:39 +08:00
image->initWithImageFile(fileName);
2021-12-08 00:11:53 +08:00
_lightMap = new Texture2D();
2019-11-23 20:27:39 +08:00
_lightMap->initWithImage(image);
2021-12-25 10:04:45 +08:00
Texture2D::TexParams tRepeatParams; // set texture parameters
2019-11-23 20:27:39 +08:00
tRepeatParams.magFilter = tRepeatParams.minFilter = backend::SamplerFilter::LINEAR;
2021-12-25 10:04:45 +08:00
tRepeatParams.sAddressMode = backend::SamplerAddressMode::REPEAT;
tRepeatParams.tAddressMode = backend::SamplerAddressMode::REPEAT;
2019-11-23 20:27:39 +08:00
_lightMap->setTexParameters(tRepeatParams);
}
void cocos2d::Terrain::setLightDir(const Vec3& lightDir)
{
_lightDir = lightDir;
}
bool Terrain::initProperties()
{
auto* program = backend::Program::getBuiltinProgram(backend::ProgramType::TERRAIN_3D);
setProgramState(new backend::ProgramState(program), false);
2019-11-23 20:27:39 +08:00
_stateBlock.depthWrite = true;
2021-12-25 10:04:45 +08:00
_stateBlock.depthTest = true;
_stateBlock.cullFace = backend::CullMode::FRONT;
2019-11-23 20:27:39 +08:00
setDrawWire(false);
setIsEnableFrustumCull(true);
setAnchorPoint(Vec2(0, 0));
return true;
}
2021-12-25 10:04:45 +08:00
void Terrain::draw(cocos2d::Renderer* renderer, const cocos2d::Mat4& transform, uint32_t flags)
2019-11-23 20:27:39 +08:00
{
auto modelMatrix = getNodeToWorldTransform();
if (memcmp(&modelMatrix, &_terrainModelMatrix, sizeof(Mat4)) != 0)
{
_terrainModelMatrix = modelMatrix;
_quadRoot->preCalculateAABB(_terrainModelMatrix);
}
2021-12-25 10:04:45 +08:00
auto& projectionMatrix = _director->getMatrix(MATRIX_STACK_TYPE::MATRIX_STACK_PROJECTION);
auto finalMatrix = projectionMatrix * transform;
2019-11-23 20:27:39 +08:00
_programState->setUniform(_mvpMatrixLocation, &finalMatrix.m, sizeof(finalMatrix.m));
_programState->setUniform(_lightDirLocation, &_lightDir, sizeof(_lightDir));
if (!_alphaMap)
{
_programState->setTexture(_detailMapLocation[0], 0, _detailMapTextures[0]->getBackendTexture());
int hasAlphaMap = 0;
_programState->setUniform(_alphaIsHasAlphaMapLocation, &hasAlphaMap, sizeof(hasAlphaMap));
}
else
{
2021-12-25 10:04:45 +08:00
float detailMapSize[4] = {0.0f, 0.0f, 0.0f, 0.0f};
2019-11-23 20:27:39 +08:00
for (int i = 0; i < _maxDetailMapValue; ++i)
{
_programState->setTexture(_detailMapLocation[i], i, _detailMapTextures[i]->getBackendTexture());
detailMapSize[i] = _terrainData._detailMaps[i]._detailMapSize;
}
_programState->setUniform(_detailMapSizeLocation, detailMapSize, sizeof(detailMapSize));
int hasAlphaMap = 1;
_programState->setUniform(_alphaIsHasAlphaMapLocation, &hasAlphaMap, sizeof(hasAlphaMap));
_programState->setTexture(_alphaMapLocation, 4, _alphaMap->getBackendTexture());
}
if (_lightMap)
{
int hasLightMap = 1;
_programState->setUniform(_lightMapCheckLocation, &hasLightMap, sizeof(hasLightMap));
_programState->setTexture(_lightMapLocation, 5, _lightMap->getBackendTexture());
}
else
{
int hasLightMap = 0;
_programState->setUniform(_lightMapCheckLocation, &hasLightMap, sizeof(hasLightMap));
#ifdef CC_USE_METAL
_programState->setTexture(_lightMapLocation, 5, _dummyTexture->getBackendTexture());
#endif
}
auto camera = Camera::getVisitingCamera();
if (memcmp(&_CameraMatrix, &camera->getViewMatrix(), sizeof(Mat4)) != 0)
{
_isCameraViewChanged = true;
2021-12-25 10:04:45 +08:00
_CameraMatrix = camera->getViewMatrix();
2019-11-23 20:27:39 +08:00
}
if (_isCameraViewChanged)
{
auto m = camera->getNodeToWorldTransform();
2021-12-25 10:04:45 +08:00
// set lod
2019-11-23 20:27:39 +08:00
setChunksLOD(Vec3(m.m[12], m.m[13], m.m[14]));
}
if (_isCameraViewChanged)
{
2021-12-25 10:04:45 +08:00
_quadRoot->resetNeedDraw(true); // reset it
// camera frustum culling
2019-11-23 20:27:39 +08:00
if (_isEnableFrustumCull)
{
_quadRoot->cullByCamera(camera, _terrainModelMatrix);
}
}
_quadRoot->draw();
if (_isCameraViewChanged)
{
_isCameraViewChanged = false;
}
}
bool Terrain::initHeightMap(std::string_view heightMap)
2019-11-23 20:27:39 +08:00
{
2021-12-08 00:11:53 +08:00
_heightMapImage = new Image();
2019-11-23 20:27:39 +08:00
_heightMapImage->initWithImageFile(heightMap);
2021-12-25 10:04:45 +08:00
_data = _heightMapImage->getData();
_imageWidth = _heightMapImage->getWidth();
2019-11-23 20:27:39 +08:00
_imageHeight = _heightMapImage->getHeight();
2021-12-25 10:04:45 +08:00
// only the image size is the Powers Of Two(POT) or POT+1
if ((utils::isPOT(_imageWidth) && utils::isPOT(_imageHeight)) ||
(utils::isPOT(_imageWidth - 1) && utils::isPOT(_imageHeight - 1)))
2019-11-23 20:27:39 +08:00
{
int chunk_amount_y = _imageHeight / _chunkSize.height;
int chunk_amount_x = _imageWidth / _chunkSize.width;
loadVertices();
calculateNormal();
memset(_chunkesArray, 0, sizeof(_chunkesArray));
for (int m = 0; m < chunk_amount_y; m++)
{
for (int n = 0; n < chunk_amount_x; n++)
{
2021-12-25 10:04:45 +08:00
_chunkesArray[m][n] = new Chunk(this);
2019-11-23 20:27:39 +08:00
_chunkesArray[m][n]->_size = _chunkSize;
_chunkesArray[m][n]->generate(_imageWidth, _imageHeight, m, n, _data);
}
}
2021-12-25 10:04:45 +08:00
// calculate the neighbor
2019-11-23 20:27:39 +08:00
for (int m = 0; m < chunk_amount_y; m++)
{
for (int n = 0; n < chunk_amount_x; n++)
{
2021-12-25 10:04:45 +08:00
if (n - 1 >= 0)
_chunkesArray[m][n]->_left = _chunkesArray[m][n - 1];
if (n + 1 < chunk_amount_x)
_chunkesArray[m][n]->_right = _chunkesArray[m][n + 1];
if (m - 1 >= 0)
_chunkesArray[m][n]->_back = _chunkesArray[m - 1][n];
if (m + 1 < chunk_amount_y)
_chunkesArray[m][n]->_front = _chunkesArray[m + 1][n];
2019-11-23 20:27:39 +08:00
}
}
2021-12-08 00:11:53 +08:00
_quadRoot = new QuadTree(0, 0, _imageWidth, _imageHeight, this);
2019-11-23 20:27:39 +08:00
setLODDistance(_chunkSize.width, 2 * _chunkSize.width, 3 * _chunkSize.width);
return true;
}
else
{
CCLOG("warning: the height map size is not POT or POT + 1");
return false;
}
}
Terrain::Terrain()
: _alphaMap(nullptr)
, _lightMap(nullptr)
, _lightDir(-1.f, -1.f, 0.f)
#if CC_ENABLE_CACHE_TEXTURE_DATA
, _backToForegroundListener(nullptr)
#endif
{
#if CC_ENABLE_CACHE_TEXTURE_DATA
2021-12-25 10:04:45 +08:00
_backToForegroundListener =
EventListenerCustom::create(EVENT_RENDERER_RECREATED, [this](EventCustom*) { reload(); });
_director->getEventDispatcher()->addEventListenerWithFixedPriority(_backToForegroundListener, 1);
2019-11-23 20:27:39 +08:00
#endif
#ifdef CC_USE_METAL
2021-12-25 10:04:45 +08:00
auto image = new Image();
2019-11-23 20:27:39 +08:00
bool CC_UNUSED isOK = image->initWithRawData(cc_2x2_white_image, sizeof(cc_2x2_white_image), 2, 2, 8);
CCASSERT(isOK, "The 2x2 empty texture was created unsuccessfully.");
2021-12-08 00:11:53 +08:00
_dummyTexture = new Texture2D();
2019-11-23 20:27:39 +08:00
_dummyTexture->initWithImage(image);
CC_SAFE_RELEASE(image);
#endif
}
void Terrain::setChunksLOD(const Vec3& cameraPos)
{
int chunk_amount_y = _imageHeight / _chunkSize.height;
int chunk_amount_x = _imageWidth / _chunkSize.width;
for (int m = 0; m < chunk_amount_y; m++)
for (int n = 0; n < chunk_amount_x; n++)
{
2021-12-25 10:04:45 +08:00
AABB aabb = _chunkesArray[m][n]->_parent->_worldSpaceAABB;
auto center = aabb.getCenter();
float dist = Vec2(center.x, center.z).distance(Vec2(cameraPos.x, cameraPos.z));
2019-11-23 20:27:39 +08:00
_chunkesArray[m][n]->_currentLod = 3;
for (int i = 0; i < 3; ++i)
{
if (dist <= _lodDistance[i])
{
_chunkesArray[m][n]->_currentLod = i;
break;
}
}
}
}
2021-12-25 10:04:45 +08:00
float Terrain::getHeight(float x, float z, Vec3* normal) const
2019-11-23 20:27:39 +08:00
{
Vec2 pos(x, z);
2021-12-25 10:04:45 +08:00
// top-left
Vec2 tl(-1 * _terrainData._mapScale * _imageWidth / 2, -1 * _terrainData._mapScale * _imageHeight / 2);
2019-11-23 20:27:39 +08:00
auto mulResult = getNodeToWorldTransform() * Vec4(tl.x, 0.0f, tl.y, 1.0f);
tl.set(mulResult.x, mulResult.z);
Vec2 to_tl = pos - tl;
2021-12-25 10:04:45 +08:00
// real size
Vec2 size(_imageWidth * _terrainData._mapScale, _imageHeight * _terrainData._mapScale);
2019-11-23 20:27:39 +08:00
mulResult = getNodeToWorldTransform() * Vec4(size.x, 0.0f, size.y, 0.0f);
size.set(mulResult.x, mulResult.z);
2021-12-25 10:04:45 +08:00
float width_ratio = to_tl.x / size.x;
2019-11-23 20:27:39 +08:00
float height_ratio = to_tl.y / size.y;
float image_x = width_ratio * _imageWidth;
float image_y = height_ratio * _imageHeight;
2021-12-25 10:04:45 +08:00
float u = image_x - (int)image_x;
float v = image_y - (int)image_y;
float i = (int)image_x;
float j = (int)image_y;
2019-11-23 20:27:39 +08:00
if (image_x >= _imageWidth - 1 || image_y >= _imageHeight - 1 || image_x < 0 || image_y < 0)
{
if (normal)
{
normal->setZero();
}
return 0;
}
else
{
2021-12-25 10:04:45 +08:00
float a = getImageHeight(i, j) * getScaleY();
float b = getImageHeight(i, j + 1) * getScaleY();
float c = getImageHeight(i + 1, j) * getScaleY();
float d = getImageHeight(i + 1, j + 1) * getScaleY();
2019-11-23 20:27:39 +08:00
if (normal)
{
normal->x = c - b;
normal->y = 2;
normal->z = d - a;
normal->normalize();
2021-12-25 10:04:45 +08:00
//(*normal) = (1-u)*(1-v)*getNormal(i,j)+ (1-u)*v*getNormal(i,j+1) + u*(1-v)*getNormal(i+1,j)+
// u*v*getNormal(i+1,j+1);
2019-11-23 20:27:39 +08:00
}
2021-12-25 10:04:45 +08:00
float result = (1 - u) * (1 - v) * getImageHeight(i, j) * getScaleY() +
(1 - u) * v * getImageHeight(i, j + 1) * getScaleY() +
u * (1 - v) * getImageHeight(i + 1, j) * getScaleY() +
u * v * getImageHeight(i + 1, j + 1) * getScaleY();
2019-11-23 20:27:39 +08:00
return result;
}
}
float Terrain::getHeight(const Vec2& pos, Vec3* normal) const
{
return getHeight(pos.x, pos.y, normal);
}
float Terrain::getImageHeight(int pixel_x, int pixel_y) const
{
int byte_stride = 1;
switch (_heightMapImage->getPixelFormat())
{
case backend::PixelFormat::BGRA8:
2019-11-23 20:27:39 +08:00
byte_stride = 4;
break;
2021-12-25 10:04:45 +08:00
case backend::PixelFormat::RGB8:
2019-11-23 20:27:39 +08:00
byte_stride = 3;
break;
case backend::PixelFormat::L8:
2019-11-23 20:27:39 +08:00
byte_stride = 1;
break;
default:
break;
}
2021-12-25 10:04:45 +08:00
return _data[(pixel_y * _imageWidth + pixel_x) * byte_stride] * 1.0 / 255 * _terrainData._mapHeight -
0.5 * _terrainData._mapHeight;
2019-11-23 20:27:39 +08:00
}
void Terrain::loadVertices()
{
_maxHeight = -99999;
_minHeight = 99999;
for (int i = 0; i < _imageHeight; ++i)
{
for (int j = 0; j < _imageWidth; j++)
{
float height = getImageHeight(j, i);
TerrainVertexData v;
2021-12-25 10:04:45 +08:00
v._position = Vec3(j * _terrainData._mapScale - _imageWidth / 2 * _terrainData._mapScale, // x
height, // y
i * _terrainData._mapScale - _imageHeight / 2 * _terrainData._mapScale); // z
v._texcoord = Tex2F(j * 1.0 / _imageWidth, i * 1.0 / _imageHeight);
2019-11-23 20:27:39 +08:00
_vertices.push_back(v);
2021-12-25 10:04:45 +08:00
// update the min & max height;
if (height > _maxHeight)
_maxHeight = height;
if (height < _minHeight)
_minHeight = height;
2019-11-23 20:27:39 +08:00
}
}
}
void Terrain::calculateNormal()
{
_indices.clear();
2021-12-25 10:04:45 +08:00
// we generate whole terrain indices(global indices) for correct normal calculate
2019-11-23 20:27:39 +08:00
for (int i = 0; i < _imageHeight - 1; i += 1)
{
for (int j = 0; j < _imageWidth - 1; j += 1)
{
int nLocIndex = i * _imageWidth + j;
_indices.push_back(nLocIndex);
_indices.push_back(nLocIndex + _imageWidth);
_indices.push_back(nLocIndex + 1);
_indices.push_back(nLocIndex + 1);
_indices.push_back(nLocIndex + _imageWidth);
_indices.push_back(nLocIndex + _imageWidth + 1);
}
}
2021-12-25 10:04:45 +08:00
for (size_t i = 0, size = _indices.size(); i < size; i += 3)
{
2019-11-23 20:27:39 +08:00
unsigned int Index0 = _indices[i];
unsigned int Index1 = _indices[i + 1];
unsigned int Index2 = _indices[i + 2];
2021-12-25 10:04:45 +08:00
Vec3 v1 = _vertices[Index1]._position - _vertices[Index0]._position;
Vec3 v2 = _vertices[Index2]._position - _vertices[Index0]._position;
2019-11-23 20:27:39 +08:00
Vec3 Normal;
Vec3::cross(v1, v2, &Normal);
Normal.normalize();
_vertices[Index0]._normal += Normal;
_vertices[Index1]._normal += Normal;
_vertices[Index2]._normal += Normal;
}
2021-12-25 10:04:45 +08:00
for (auto& vertex : _vertices)
{
2019-11-23 20:27:39 +08:00
vertex._normal.normalize();
}
2021-12-25 10:04:45 +08:00
// global indices no need at all
2019-11-23 20:27:39 +08:00
_indices.clear();
}
void Terrain::setDrawWire(bool bool_value)
{
_isDrawWire = bool_value;
}
void Terrain::setLODDistance(float lod_1, float lod_2, float lod_3)
{
_lodDistance[0] = lod_1;
_lodDistance[1] = lod_2;
_lodDistance[2] = lod_3;
}
void Terrain::setIsEnableFrustumCull(bool bool_value)
{
_isEnableFrustumCull = bool_value;
}
Terrain::~Terrain()
{
CC_SAFE_RELEASE(_alphaMap);
CC_SAFE_RELEASE(_lightMap);
CC_SAFE_RELEASE(_heightMapImage);
CC_SAFE_RELEASE(_dummyTexture);
delete _quadRoot;
for (int i = 0; i < 4; ++i)
{
if (_detailMapTextures[i])
{
_detailMapTextures[i]->release();
}
}
for (int i = 0; i < MAX_CHUNKES; ++i)
{
for (int j = 0; j < MAX_CHUNKES; j++)
{
if (_chunkesArray[i][j])
{
delete _chunkesArray[i][j];
}
}
}
#if CC_ENABLE_CACHE_TEXTURE_DATA
_director->getEventDispatcher()->removeEventListener(_backToForegroundListener);
2019-11-23 20:27:39 +08:00
#endif
}
cocos2d::Vec3 Terrain::getNormal(int pixel_x, int pixel_y) const
{
2021-12-25 10:04:45 +08:00
float a = getImageHeight(pixel_x, pixel_y) * getScaleY();
float b = getImageHeight(pixel_x, pixel_y + 1) * getScaleY();
float c = getImageHeight(pixel_x + 1, pixel_y) * getScaleY();
float d = getImageHeight(pixel_x + 1, pixel_y + 1) * getScaleY();
2019-11-23 20:27:39 +08:00
Vec3 normal;
normal.x = c - b;
normal.y = 2;
normal.z = d - a;
normal.normalize();
return normal;
}
2021-12-25 10:04:45 +08:00
cocos2d::Vec3 Terrain::getIntersectionPoint(const Ray& ray) const
2019-11-23 20:27:39 +08:00
{
Vec3 collisionPoint;
if (getIntersectionPoint(ray, collisionPoint))
{
return collisionPoint;
}
else
{
return Vec3(0, 0, 0);
}
}
2021-12-25 10:04:45 +08:00
bool Terrain::getIntersectionPoint(const Ray& ray_, Vec3& intersectionPoint) const
2019-11-23 20:27:39 +08:00
{
// convert ray from world space to local space
Ray ray(ray_);
getWorldToNodeTransform().transformPoint(&(ray._origin));
2021-12-25 10:04:45 +08:00
std::set<Chunk*> closeList;
2019-11-23 20:27:39 +08:00
Vec2 start = Vec2(ray_._origin.x, ray_._origin.z);
2021-12-25 10:04:45 +08:00
Vec2 dir = Vec2(ray._direction.x, ray._direction.z);
start = convertToTerrainSpace(start);
2019-11-23 20:27:39 +08:00
start.x /= (_terrainData._chunkSize.width + 1);
start.y /= (_terrainData._chunkSize.height + 1);
2021-12-25 10:04:45 +08:00
Vec2 delta = dir.getNormalized();
auto width = float(_imageWidth) / (_terrainData._chunkSize.width + 1);
auto height = float(_imageHeight) / (_terrainData._chunkSize.height + 1);
bool hasIntersect = false;
2019-11-23 20:27:39 +08:00
float intersectionDist = FLT_MAX;
Vec3 tmpIntersectionPoint;
for (;;)
{
int x1 = floorf(start.x);
int x2 = ceilf(start.x);
int y1 = floorf(start.y);
int y2 = ceilf(start.y);
2021-12-25 10:04:45 +08:00
for (int x = x1; x <= x2; x++)
{
for (int y = y1; y <= y2; y++)
{
2019-11-23 20:27:39 +08:00
auto chunk = getChunkByIndex(x, y);
if (chunk)
{
if (closeList.find(chunk) == closeList.end())
{
if (chunk->getIntersectPointWithRay(ray, tmpIntersectionPoint))
{
float dist = (ray._origin - tmpIntersectionPoint).length();
if (intersectionDist > dist)
{
2021-12-25 10:04:45 +08:00
hasIntersect = true;
intersectionDist = dist;
2019-11-23 20:27:39 +08:00
intersectionPoint = tmpIntersectionPoint;
}
}
closeList.insert(chunk);
}
}
}
}
if ((delta.x > 0 && start.x > width) || (delta.x < 0 && start.x < 0))
{
break;
}
if ((delta.y > 0 && start.y > height) || (delta.y < 0 && start.y < 0))
{
break;
}
start.x += delta.x;
start.y += delta.y;
}
return hasIntersect;
}
void Terrain::setMaxDetailMapAmount(int max_value)
{
_maxDetailMapValue = max_value;
}
cocos2d::Vec2 Terrain::convertToTerrainSpace(const Vec2& worldSpaceXZ) const
{
Vec2 pos(worldSpaceXZ.x, worldSpaceXZ.y);
2021-12-25 10:04:45 +08:00
// top-left
Vec2 tl(-1 * _terrainData._mapScale * _imageWidth / 2, -1 * _terrainData._mapScale * _imageHeight / 2);
auto result = getNodeToWorldTransform() * Vec4(tl.x, 0.0f, tl.y, 1.0f);
2019-11-23 20:27:39 +08:00
tl.set(result.x, result.z);
Vec2 to_tl = pos - tl;
2021-12-25 10:04:45 +08:00
// real size
Vec2 size(_imageWidth * _terrainData._mapScale, _imageHeight * _terrainData._mapScale);
result = getNodeToWorldTransform() * Vec4(size.x, 0.0f, size.y, 0.0f);
2019-11-23 20:27:39 +08:00
size.set(result.x, result.z);
2021-12-25 10:04:45 +08:00
float width_ratio = to_tl.x / size.x;
2019-11-23 20:27:39 +08:00
float height_ratio = to_tl.y / size.y;
float image_x = width_ratio * _imageWidth;
float image_y = height_ratio * _imageHeight;
return Vec2(image_x, image_y);
}
void Terrain::resetHeightMap(std::string_view heightMap)
2019-11-23 20:27:39 +08:00
{
_heightMapImage->release();
_vertices.clear();
free(_data);
for (int i = 0; i < MAX_CHUNKES; ++i)
{
for (int j = 0; j < MAX_CHUNKES; j++)
{
if (_chunkesArray[i][j])
{
delete _chunkesArray[i][j];
}
}
}
delete _quadRoot;
initHeightMap(heightMap);
}
float Terrain::getMinHeight()
{
return _minHeight;
}
float Terrain::getMaxHeight()
{
return _maxHeight;
}
cocos2d::AABB Terrain::getAABB()
{
return _quadRoot->_worldSpaceAABB;
}
2021-12-25 10:04:45 +08:00
Terrain::QuadTree* Terrain::getQuadTree()
2019-11-23 20:27:39 +08:00
{
return _quadRoot;
}
std::vector<float> Terrain::getHeightData() const
{
std::vector<float> data;
data.resize(_imageWidth * _imageHeight);
2021-12-25 10:04:45 +08:00
for (int i = 0; i < _imageHeight; ++i)
{
for (int j = 0; j < _imageWidth; j++)
{
int idx = i * _imageWidth + j;
2019-11-23 20:27:39 +08:00
data[idx] = (_vertices[idx]._position.y);
}
}
return data;
}
2021-12-25 10:04:45 +08:00
Terrain::Chunk* cocos2d::Terrain::getChunkByIndex(int x, int y) const
2019-11-23 20:27:39 +08:00
{
2021-12-25 10:04:45 +08:00
if (x < 0 || y < 0 || x >= MAX_CHUNKES || y >= MAX_CHUNKES)
return nullptr;
2019-11-23 20:27:39 +08:00
return _chunkesArray[y][x];
}
2021-12-25 10:04:45 +08:00
void Terrain::setAlphaMap(cocos2d::Texture2D* newAlphaMapTexture)
2019-11-23 20:27:39 +08:00
{
CC_SAFE_RETAIN(newAlphaMapTexture);
CC_SAFE_RELEASE(_alphaMap);
_alphaMap = newAlphaMapTexture;
}
void Terrain::setDetailMap(unsigned int index, DetailMap detailMap)
{
if (index > 4)
{
CCLOG("invalid DetailMap index %d\n", index);
}
_terrainData._detailMaps[index] = detailMap;
if (_detailMapTextures[index])
{
_detailMapTextures[index]->release();
}
2021-12-08 00:11:53 +08:00
_detailMapTextures[index] = new Texture2D();
2021-12-25 10:04:45 +08:00
auto textImage = new Image();
2019-11-23 20:27:39 +08:00
textImage->initWithImageFile(detailMap._detailMapSrc);
_detailMapTextures[index]->initWithImage(textImage);
delete textImage;
}
2021-12-25 10:04:45 +08:00
Terrain::ChunkIndices Terrain::lookForIndicesLOD(int neighborLod[4], int selfLod, bool* result)
2019-11-23 20:27:39 +08:00
{
(*result) = false;
ChunkIndices tmp;
tmp._indexBuffer = nullptr;
2021-12-25 10:04:45 +08:00
tmp._size = 0;
2019-11-23 20:27:39 +08:00
if (_chunkLodIndicesSet.empty())
{
(*result) = false;
return tmp;
}
else
{
int test[5];
memcpy(test, neighborLod, sizeof(int[4]));
test[4] = selfLod;
for (size_t i = 0, size = _chunkLodIndicesSet.size(); i < size; ++i)
{
if (memcmp(test, _chunkLodIndicesSet[i]._relativeLod, sizeof(test)) == 0)
{
(*result) = true;
return _chunkLodIndicesSet[i]._chunkIndices;
}
}
}
(*result) = false;
return tmp;
}
2021-12-25 10:04:45 +08:00
Terrain::ChunkIndices Terrain::insertIndicesLOD(int neighborLod[4], int selfLod, uint16_t* indices, int size)
2019-11-23 20:27:39 +08:00
{
ChunkLODIndices lodIndices;
memcpy(lodIndices._relativeLod, neighborLod, sizeof(int[4]));
2021-12-25 10:04:45 +08:00
lodIndices._relativeLod[4] = selfLod;
2019-11-23 20:27:39 +08:00
lodIndices._chunkIndices._size = size;
2021-12-25 10:04:45 +08:00
auto buffer = backend::Device::getInstance()->newBuffer(sizeof(uint16_t) * size, backend::BufferType::INDEX,
backend::BufferUsage::STATIC);
2019-11-23 20:27:39 +08:00
buffer->updateData(indices, sizeof(uint16_t) * size);
CC_SAFE_RELEASE_NULL(lodIndices._chunkIndices._indexBuffer);
lodIndices._chunkIndices._indexBuffer = buffer;
this->_chunkLodIndicesSet.push_back(lodIndices);
return lodIndices._chunkIndices;
}
2021-12-25 10:04:45 +08:00
Terrain::ChunkIndices Terrain::lookForIndicesLODSkrit(int selfLod, bool* result)
2019-11-23 20:27:39 +08:00
{
ChunkIndices badResult;
badResult._indexBuffer = nullptr;
2021-12-25 10:04:45 +08:00
badResult._size = 0;
2019-11-23 20:27:39 +08:00
if (this->_chunkLodIndicesSkirtSet.empty())
{
(*result) = false;
return badResult;
}
for (size_t i = 0, size = _chunkLodIndicesSkirtSet.size(); i < size; ++i)
{
if (_chunkLodIndicesSkirtSet[i]._selfLod == selfLod)
{
(*result) = true;
return _chunkLodIndicesSkirtSet[i]._chunkIndices;
}
}
(*result) = false;
return badResult;
}
2021-12-25 10:04:45 +08:00
Terrain::ChunkIndices Terrain::insertIndicesLODSkirt(int selfLod, uint16_t* indices, int size)
2019-11-23 20:27:39 +08:00
{
ChunkLODIndicesSkirt skirtIndices;
2021-12-25 10:04:45 +08:00
skirtIndices._selfLod = selfLod;
2019-11-23 20:27:39 +08:00
skirtIndices._chunkIndices._size = size;
2021-12-25 10:04:45 +08:00
auto buffer = backend::Device::getInstance()->newBuffer(sizeof(uint16_t) * size, backend::BufferType::INDEX,
backend::BufferUsage::STATIC);
buffer->updateData(indices, sizeof(uint16_t) * size);
2019-11-23 20:27:39 +08:00
CC_SAFE_RELEASE_NULL(skirtIndices._chunkIndices._indexBuffer);
skirtIndices._chunkIndices._indexBuffer = buffer;
this->_chunkLodIndicesSkirtSet.push_back(skirtIndices);
return skirtIndices._chunkIndices;
}
void Terrain::setSkirtHeightRatio(float ratio)
{
_skirtRatio = ratio;
}
void Terrain::onEnter()
{
Node::onEnter();
_terrainModelMatrix = getNodeToWorldTransform();
_quadRoot->preCalculateAABB(_terrainModelMatrix);
cacheUniformAttribLocation();
}
void Terrain::cacheUniformAttribLocation()
{
2021-12-25 10:04:45 +08:00
auto vertexLayout = _programState->getVertexLayout();
2019-11-23 20:27:39 +08:00
const auto& attributeInfo = _programState->getProgram()->getActiveAttributes();
2021-12-25 10:04:45 +08:00
auto iter = attributeInfo.find("a_position");
if (iter != attributeInfo.end())
2019-11-23 20:27:39 +08:00
{
vertexLayout->setAttribute("a_position", iter->second.location, backend::VertexFormat::FLOAT3, 0, false);
}
iter = attributeInfo.find("a_texCoord");
2021-12-25 10:04:45 +08:00
if (iter != attributeInfo.end())
2019-11-23 20:27:39 +08:00
{
2021-12-25 10:04:45 +08:00
vertexLayout->setAttribute("a_texCoord", iter->second.location, backend::VertexFormat::FLOAT2,
offsetof(TerrainVertexData, _texcoord), false);
2019-11-23 20:27:39 +08:00
}
iter = attributeInfo.find("a_normal");
2021-12-25 10:04:45 +08:00
if (iter != attributeInfo.end())
2019-11-23 20:27:39 +08:00
{
2021-12-25 10:04:45 +08:00
vertexLayout->setAttribute("a_normal", iter->second.location, backend::VertexFormat::FLOAT3,
offsetof(TerrainVertexData, _normal), false);
2019-11-23 20:27:39 +08:00
}
vertexLayout->setLayout(sizeof(TerrainVertexData));
_alphaMapLocation.reset();
for (int i = 0; i < 4; ++i)
{
_detailMapLocation[i].reset();
}
_detailMapSizeLocation.reset();
_alphaIsHasAlphaMapLocation = _programState->getUniformLocation("u_has_alpha");
2021-12-25 10:04:45 +08:00
_lightMapCheckLocation = _programState->getUniformLocation("u_has_light_map");
2019-11-23 20:27:39 +08:00
if (!_alphaMap)
{
_detailMapLocation[0] = _programState->getUniformLocation("u_texture0");
}
else
{
for (int i = 0; i < _maxDetailMapValue; ++i)
{
char str[20];
sprintf(str, "u_texture%d", i);
_detailMapLocation[i] = _programState->getUniformLocation(str);
}
2021-12-25 10:04:45 +08:00
_detailMapSizeLocation = _programState->getUniformLocation("u_detailSize"); // float[4]
2019-11-23 20:27:39 +08:00
_alphaMapLocation = _programState->getUniformLocation("u_alphaMap");
}
2021-12-25 10:04:45 +08:00
_lightMapLocation = _programState->getUniformLocation("u_lightMap");
_lightDirLocation = _programState->getUniformLocation("u_lightDir");
2019-11-23 20:27:39 +08:00
_mvpMatrixLocation = _programState->getUniformLocation("u_MVPMatrix");
}
bool Terrain::initTextures()
{
for (int i = 0; i < 4; ++i)
{
_detailMapTextures[i] = nullptr;
}
Texture2D::TexParams texParam;
texParam.sAddressMode = backend::SamplerAddressMode::REPEAT;
texParam.tAddressMode = backend::SamplerAddressMode::REPEAT;
if (_terrainData._alphaMapSrc.empty())
{
2021-12-08 00:11:53 +08:00
auto textImage = new Image();
2019-11-23 20:27:39 +08:00
textImage->initWithImageFile(_terrainData._detailMaps[0]._detailMapSrc);
2021-12-08 00:11:53 +08:00
auto texture = new Texture2D();
2019-11-23 20:27:39 +08:00
texture->initWithImage(textImage);
texture->generateMipmap();
_detailMapTextures[0] = texture;
2021-12-25 10:04:45 +08:00
texParam.minFilter = backend::SamplerFilter::LINEAR;
texParam.magFilter = backend::SamplerFilter::LINEAR;
2019-11-23 20:27:39 +08:00
texture->setTexParameters(texParam);
delete textImage;
}
else
{
2021-12-25 10:04:45 +08:00
// alpha map
2021-12-08 00:11:53 +08:00
auto image = new Image();
2019-11-23 20:27:39 +08:00
image->initWithImageFile(_terrainData._alphaMapSrc);
2021-12-08 00:11:53 +08:00
_alphaMap = new Texture2D();
2019-11-23 20:27:39 +08:00
_alphaMap->initWithImage(image);
texParam.sAddressMode = backend::SamplerAddressMode::CLAMP_TO_EDGE;
texParam.tAddressMode = backend::SamplerAddressMode::CLAMP_TO_EDGE;
2021-12-25 10:04:45 +08:00
texParam.minFilter = backend::SamplerFilter::LINEAR;
texParam.magFilter = backend::SamplerFilter::LINEAR;
2019-11-23 20:27:39 +08:00
_alphaMap->setTexParameters(texParam);
delete image;
for (int i = 0; i < _terrainData._detailMapAmount; ++i)
{
2021-12-08 00:11:53 +08:00
auto textImage = new Image();
2019-11-23 20:27:39 +08:00
textImage->initWithImageFile(_terrainData._detailMaps[i]._detailMapSrc);
2021-12-08 00:11:53 +08:00
auto texture = new Texture2D();
2019-11-23 20:27:39 +08:00
texture->initWithImage(textImage);
delete textImage;
texture->generateMipmap();
_detailMapTextures[i] = texture;
texParam.sAddressMode = backend::SamplerAddressMode::REPEAT;
texParam.tAddressMode = backend::SamplerAddressMode::REPEAT;
2021-12-25 10:04:45 +08:00
texParam.minFilter = backend::SamplerFilter::LINEAR;
texParam.magFilter = backend::SamplerFilter::LINEAR;
2019-11-23 20:27:39 +08:00
texture->setTexParameters(texParam);
}
}
setMaxDetailMapAmount(_terrainData._detailMapAmount);
return true;
}
void Terrain::reload()
{
int chunk_amount_y = _imageHeight / _chunkSize.height;
int chunk_amount_x = _imageWidth / _chunkSize.width;
for (int m = 0; m < chunk_amount_y; m++)
{
for (int n = 0; n < chunk_amount_x; n++)
{
_chunkesArray[m][n]->finish();
}
}
initTextures();
_chunkLodIndicesSet.clear();
_chunkLodIndicesSkirtSet.clear();
}
void Terrain::Chunk::finish()
{
2021-12-25 10:04:45 +08:00
// generate two VBO ,the first for vertices, we just setup datas once ,won't changed at all
// the second vbo for the indices, because we use level of detail technique to each chunk, so we will modified
// frequently
2019-11-23 20:27:39 +08:00
CC_SAFE_RELEASE_NULL(_buffer);
2021-12-25 10:04:45 +08:00
_buffer = backend::Device::getInstance()->newBuffer(sizeof(TerrainVertexData) * _originalVertices.size(),
backend::BufferType::VERTEX, backend::BufferUsage::DYNAMIC);
2019-11-23 20:27:39 +08:00
2021-12-25 10:04:45 +08:00
_buffer->updateData(&_originalVertices[0], sizeof(TerrainVertexData) * _originalVertices.size());
2019-11-23 20:27:39 +08:00
calculateSlope();
for (int i = 0; i < 4; ++i)
{
int step = 1 << _currentLod;
2021-12-25 10:04:45 +08:00
// reserve the indices size, the first part is the core part of the chunk, the second part & third part is for
// fix crack
int indicesAmount = (_terrain->_chunkSize.width / step + 1) * (_terrain->_chunkSize.height / step + 1) * 6 +
(_terrain->_chunkSize.height / step) * 6 + (_terrain->_chunkSize.width / step) * 6;
2019-11-23 20:27:39 +08:00
_lod[i]._indices.reserve(indicesAmount);
}
_oldLod = -1;
}
void Terrain::Chunk::bindAndDraw()
{
if (_terrain->_isCameraViewChanged || _oldLod < 0)
{
switch (_terrain->_crackFixedType)
{
case CrackFixedType::SKIRT:
updateIndicesLODSkirt();
break;
case CrackFixedType::INCREASE_LOWER:
updateVerticesForLOD();
updateIndicesLOD();
break;
default:
break;
}
}
2021-12-25 10:04:45 +08:00
auto* renderer = Director::getInstance()->getRenderer();
2019-11-23 20:27:39 +08:00
CCASSERT(_buffer && _chunkIndices._indexBuffer, "buffer should not be nullptr");
_command.setIndexBuffer(_chunkIndices._indexBuffer, backend::IndexFormat::U_SHORT);
_command.setVertexBuffer(_buffer);
2021-12-25 10:04:45 +08:00
_command.getPipelineDescriptor().programState = _terrain->_programState;
2019-11-23 20:27:39 +08:00
_command.setIndexDrawInfo(0, _chunkIndices._size);
renderer->addCommand(&_command);
CC_INCREMENT_GL_DRAWN_BATCHES_AND_VERTICES(1, _chunkIndices._size);
}
2021-12-25 10:04:45 +08:00
void Terrain::Chunk::generate(int imgWidth, int imageHei, int m, int n, const unsigned char* /*data*/)
2019-11-23 20:27:39 +08:00
{
_posY = m;
_posX = n;
switch (_terrain->_crackFixedType)
{
case CrackFixedType::SKIRT:
{
2021-12-25 10:04:45 +08:00
for (int i = _size.height * m; i <= _size.height * (m + 1); ++i)
2019-11-23 20:27:39 +08:00
{
2021-12-25 10:04:45 +08:00
if (i >= imageHei)
break;
for (int j = _size.width * n; j <= _size.width * (n + 1); j++)
2019-11-23 20:27:39 +08:00
{
2021-12-25 10:04:45 +08:00
if (j >= imgWidth)
break;
auto v = _terrain->_vertices[i * imgWidth + j];
2019-11-23 20:27:39 +08:00
_originalVertices.push_back(v);
}
}
// add four skirts
2021-12-25 10:04:45 +08:00
float skirtHeight = _terrain->_skirtRatio * _terrain->_terrainData._mapScale * 8;
2019-11-23 20:27:39 +08:00
//#1
_terrain->_skirtVerticesOffset[0] = (int)_originalVertices.size();
2021-12-25 10:04:45 +08:00
for (int i = _size.height * m; i <= _size.height * (m + 1); ++i)
2019-11-23 20:27:39 +08:00
{
2021-12-25 10:04:45 +08:00
auto v = _terrain->_vertices[i * imgWidth + _size.width * (n + 1)];
2019-11-23 20:27:39 +08:00
v._position.y -= skirtHeight;
_originalVertices.push_back(v);
}
//#2
_terrain->_skirtVerticesOffset[1] = (int)_originalVertices.size();
2021-12-25 10:04:45 +08:00
for (int j = _size.width * n; j <= _size.width * (n + 1); j++)
2019-11-23 20:27:39 +08:00
{
2021-12-25 10:04:45 +08:00
auto v = _terrain->_vertices[_size.height * (m + 1) * imgWidth + j];
2019-11-23 20:27:39 +08:00
v._position.y -= skirtHeight;
_originalVertices.push_back(v);
}
//#3
_terrain->_skirtVerticesOffset[2] = (int)_originalVertices.size();
2021-12-25 10:04:45 +08:00
for (int i = _size.height * m; i <= _size.height * (m + 1); ++i)
2019-11-23 20:27:39 +08:00
{
2021-12-25 10:04:45 +08:00
auto v = _terrain->_vertices[i * imgWidth + _size.width * n];
2019-11-23 20:27:39 +08:00
v._position.y -= skirtHeight;
_originalVertices.push_back(v);
}
//#4
_terrain->_skirtVerticesOffset[3] = (int)_originalVertices.size();
2021-12-25 10:04:45 +08:00
for (int j = _size.width * n; j <= _size.width * (n + 1); j++)
2019-11-23 20:27:39 +08:00
{
2021-12-25 10:04:45 +08:00
auto v = _terrain->_vertices[_size.height * m * imgWidth + j];
2019-11-23 20:27:39 +08:00
v._position.y -= skirtHeight;
2021-12-25 10:04:45 +08:00
// v.position.y = -5;
2019-11-23 20:27:39 +08:00
_originalVertices.push_back(v);
}
}
break;
case CrackFixedType::INCREASE_LOWER:
{
2021-12-25 10:04:45 +08:00
for (int i = _size.height * m; i <= _size.height * (m + 1); ++i)
2019-11-23 20:27:39 +08:00
{
2021-12-25 10:04:45 +08:00
if (i >= imageHei)
break;
for (int j = _size.width * n; j <= _size.width * (n + 1); j++)
2019-11-23 20:27:39 +08:00
{
2021-12-25 10:04:45 +08:00
if (j >= imgWidth)
break;
auto v = _terrain->_vertices[i * imgWidth + j];
2019-11-23 20:27:39 +08:00
_originalVertices.push_back(v);
}
}
}
break;
}
2021-12-25 10:04:45 +08:00
// store triangle:
2019-11-23 20:27:39 +08:00
for (int i = 0; i < _size.height; ++i)
{
for (int j = 0; j < _size.width; j++)
{
int nLocIndex = i * (_size.width + 1) + j;
2021-12-25 10:04:45 +08:00
Triangle a(_originalVertices[nLocIndex]._position,
_originalVertices[nLocIndex + 1 * (_size.width + 1)]._position,
_originalVertices[nLocIndex + 1]._position);
Triangle b(_originalVertices[nLocIndex + 1]._position,
_originalVertices[nLocIndex + 1 * (_size.width + 1)]._position,
_originalVertices[nLocIndex + 1 * (_size.width + 1) + 1]._position);
2019-11-23 20:27:39 +08:00
_trianglesList.push_back(a);
_trianglesList.push_back(b);
}
}
calculateAABB();
finish();
}
2021-12-25 10:04:45 +08:00
Terrain::Chunk::Chunk(Terrain* terrain)
2019-11-23 20:27:39 +08:00
{
2021-12-25 10:04:45 +08:00
_terrain = terrain;
2019-11-23 20:27:39 +08:00
_currentLod = 0;
2021-12-25 10:04:45 +08:00
_left = nullptr;
_right = nullptr;
_back = nullptr;
_front = nullptr;
_oldLod = -1;
2019-11-23 20:27:39 +08:00
for (int i = 0; i < 4; ++i)
{
_neighborOldLOD[i] = -1;
}
_command.init(_terrain->_globalZOrder);
_command.setTransparent(false);
_command.set3D(true);
_command.setPrimitiveType(MeshCommand::PrimitiveType::TRIANGLE);
_command.setDrawType(MeshCommand::DrawType::ELEMENT);
_command.setBeforeCallback(CC_CALLBACK_0(Terrain::onBeforeDraw, terrain));
_command.setAfterCallback(CC_CALLBACK_0(Terrain::onAfterDraw, terrain));
2021-12-25 10:04:45 +08:00
auto& pipelineDescriptor = _command.getPipelineDescriptor();
2019-11-23 20:27:39 +08:00
pipelineDescriptor.blendDescriptor.blendEnabled = false;
}
void Terrain::Chunk::updateIndicesLOD()
{
int currentNeighborLOD[4];
if (_left)
{
currentNeighborLOD[0] = _left->_currentLod;
}
2021-12-25 10:04:45 +08:00
else
{
currentNeighborLOD[0] = -1;
}
2019-11-23 20:27:39 +08:00
if (_right)
{
currentNeighborLOD[1] = _right->_currentLod;
}
2021-12-25 10:04:45 +08:00
else
{
currentNeighborLOD[1] = -1;
}
2019-11-23 20:27:39 +08:00
if (_back)
{
currentNeighborLOD[2] = _back->_currentLod;
}
2021-12-25 10:04:45 +08:00
else
{
currentNeighborLOD[2] = -1;
}
2019-11-23 20:27:39 +08:00
if (_front)
{
currentNeighborLOD[3] = _front->_currentLod;
}
2021-12-25 10:04:45 +08:00
else
{
currentNeighborLOD[3] = -1;
}
2019-11-23 20:27:39 +08:00
if (_oldLod == _currentLod && (memcmp(currentNeighborLOD, _neighborOldLOD, sizeof(currentNeighborLOD)) == 0))
{
2021-12-25 10:04:45 +08:00
return; // no need to update
2019-11-23 20:27:39 +08:00
}
bool isOk;
_chunkIndices = _terrain->lookForIndicesLOD(currentNeighborLOD, _currentLod, &isOk);
if (isOk)
{
return;
}
memcpy(_neighborOldLOD, currentNeighborLOD, sizeof(currentNeighborLOD));
2021-12-25 10:04:45 +08:00
_oldLod = _currentLod;
int gridY = static_cast<int>(_size.height);
int gridX = static_cast<int>(_size.width);
2019-11-23 20:27:39 +08:00
int step = 1 << _currentLod;
2021-12-25 10:04:45 +08:00
if ((_left && _left->_currentLod > _currentLod) || (_right && _right->_currentLod > _currentLod) ||
(_back && _back->_currentLod > _currentLod) || (_front && _front->_currentLod > _currentLod))
// need update indices.
2019-11-23 20:27:39 +08:00
{
2021-12-25 10:04:45 +08:00
// t-junction inner
2019-11-23 20:27:39 +08:00
_lod[_currentLod]._indices.clear();
for (int i = step; i < gridY - step; i += step)
{
for (int j = step; j < gridX - step; j += step)
{
int nLocIndex = i * (gridX + 1) + j;
_lod[_currentLod]._indices.push_back(nLocIndex);
_lod[_currentLod]._indices.push_back(nLocIndex + step * (gridX + 1));
_lod[_currentLod]._indices.push_back(nLocIndex + step);
_lod[_currentLod]._indices.push_back(nLocIndex + step);
_lod[_currentLod]._indices.push_back(nLocIndex + step * (gridX + 1));
_lod[_currentLod]._indices.push_back(nLocIndex + step * (gridX + 1) + step);
}
}
2021-12-25 10:04:45 +08:00
// fix T-crack
2019-11-23 20:27:39 +08:00
int next_step = 1 << (_currentLod + 1);
2021-12-25 10:04:45 +08:00
if (_left && _left->_currentLod > _currentLod) // left
2019-11-23 20:27:39 +08:00
{
for (int i = 0; i < gridY; i += next_step)
{
2021-12-25 10:04:45 +08:00
_lod[_currentLod]._indices.push_back(i * (gridX + 1) + step);
_lod[_currentLod]._indices.push_back(i * (gridX + 1));
_lod[_currentLod]._indices.push_back((i + next_step) * (gridX + 1));
2019-11-23 20:27:39 +08:00
2021-12-25 10:04:45 +08:00
_lod[_currentLod]._indices.push_back(i * (gridX + 1) + step);
_lod[_currentLod]._indices.push_back((i + next_step) * (gridX + 1));
_lod[_currentLod]._indices.push_back((i + step) * (gridX + 1) + step);
2019-11-23 20:27:39 +08:00
2021-12-25 10:04:45 +08:00
_lod[_currentLod]._indices.push_back((i + step) * (gridX + 1) + step);
_lod[_currentLod]._indices.push_back((i + next_step) * (gridX + 1));
_lod[_currentLod]._indices.push_back((i + next_step) * (gridX + 1) + step);
2019-11-23 20:27:39 +08:00
}
}
2021-12-25 10:04:45 +08:00
else
{
2019-11-23 20:27:39 +08:00
int start = 0;
2021-12-25 10:04:45 +08:00
int end = gridY;
if (_front && _front->_currentLod > _currentLod)
end -= step;
if (_back && _back->_currentLod > _currentLod)
start += step;
2019-11-23 20:27:39 +08:00
for (int i = start; i < end; i += step)
{
2021-12-25 10:04:45 +08:00
_lod[_currentLod]._indices.push_back(i * (gridX + 1) + step);
_lod[_currentLod]._indices.push_back(i * (gridX + 1));
_lod[_currentLod]._indices.push_back((i + step) * (gridX + 1));
2019-11-23 20:27:39 +08:00
2021-12-25 10:04:45 +08:00
_lod[_currentLod]._indices.push_back(i * (gridX + 1) + step);
_lod[_currentLod]._indices.push_back((i + step) * (gridX + 1));
_lod[_currentLod]._indices.push_back((i + step) * (gridX + 1) + step);
2019-11-23 20:27:39 +08:00
}
}
2021-12-25 10:04:45 +08:00
if (_right && _right->_currentLod > _currentLod) // LEFT
2019-11-23 20:27:39 +08:00
{
for (int i = 0; i < gridY; i += next_step)
{
2021-12-25 10:04:45 +08:00
_lod[_currentLod]._indices.push_back(i * (gridX + 1) + gridX);
_lod[_currentLod]._indices.push_back(i * (gridX + 1) + gridX - step);
_lod[_currentLod]._indices.push_back((i + step) * (gridX + 1) + gridX - step);
2019-11-23 20:27:39 +08:00
2021-12-25 10:04:45 +08:00
_lod[_currentLod]._indices.push_back(i * (gridX + 1) + gridX);
_lod[_currentLod]._indices.push_back((i + step) * (gridX + 1) + gridX - step);
_lod[_currentLod]._indices.push_back((i + next_step) * (gridX + 1) + gridX - step);
2019-11-23 20:27:39 +08:00
2021-12-25 10:04:45 +08:00
_lod[_currentLod]._indices.push_back(i * (gridX + 1) + gridX);
_lod[_currentLod]._indices.push_back((i + next_step) * (gridX + 1) + gridX - step);
_lod[_currentLod]._indices.push_back((i + next_step) * (gridX + 1) + gridX);
2019-11-23 20:27:39 +08:00
}
}
2021-12-25 10:04:45 +08:00
else
{
2019-11-23 20:27:39 +08:00
int start = 0;
2021-12-25 10:04:45 +08:00
int end = gridY;
if (_front && _front->_currentLod > _currentLod)
end -= step;
if (_back && _back->_currentLod > _currentLod)
start += step;
2019-11-23 20:27:39 +08:00
for (int i = start; i < end; i += step)
{
2021-12-25 10:04:45 +08:00
_lod[_currentLod]._indices.push_back(i * (gridX + 1) + gridX);
_lod[_currentLod]._indices.push_back(i * (gridX + 1) + gridX - step);
_lod[_currentLod]._indices.push_back((i + step) * (gridX + 1) + gridX - step);
2019-11-23 20:27:39 +08:00
2021-12-25 10:04:45 +08:00
_lod[_currentLod]._indices.push_back(i * (gridX + 1) + gridX);
_lod[_currentLod]._indices.push_back((i + step) * (gridX + 1) + gridX - step);
_lod[_currentLod]._indices.push_back((i + step) * (gridX + 1) + gridX);
2019-11-23 20:27:39 +08:00
}
}
2021-12-25 10:04:45 +08:00
if (_front && _front->_currentLod > _currentLod) // front
2019-11-23 20:27:39 +08:00
{
for (int i = 0; i < gridX; i += next_step)
{
2021-12-25 10:04:45 +08:00
_lod[_currentLod]._indices.push_back((gridY - step) * (gridX + 1) + i);
_lod[_currentLod]._indices.push_back(gridY * (gridX + 1) + i);
_lod[_currentLod]._indices.push_back((gridY - step) * (gridX + 1) + i + step);
2019-11-23 20:27:39 +08:00
2021-12-25 10:04:45 +08:00
_lod[_currentLod]._indices.push_back((gridY - step) * (gridX + 1) + i + step);
_lod[_currentLod]._indices.push_back(gridY * (gridX + 1) + i);
_lod[_currentLod]._indices.push_back(gridY * (gridX + 1) + i + next_step);
2019-11-23 20:27:39 +08:00
2021-12-25 10:04:45 +08:00
_lod[_currentLod]._indices.push_back((gridY - step) * (gridX + 1) + i + step);
_lod[_currentLod]._indices.push_back(gridY * (gridX + 1) + i + next_step);
_lod[_currentLod]._indices.push_back((gridY - step) * (gridX + 1) + i + next_step);
2019-11-23 20:27:39 +08:00
}
}
else
{
for (int i = step; i < gridX - step; i += step)
{
2021-12-25 10:04:45 +08:00
_lod[_currentLod]._indices.push_back((gridY - step) * (gridX + 1) + i);
_lod[_currentLod]._indices.push_back(gridY * (gridX + 1) + i);
_lod[_currentLod]._indices.push_back((gridY - step) * (gridX + 1) + i + step);
2019-11-23 20:27:39 +08:00
2021-12-25 10:04:45 +08:00
_lod[_currentLod]._indices.push_back((gridY - step) * (gridX + 1) + i + step);
_lod[_currentLod]._indices.push_back(gridY * (gridX + 1) + i);
_lod[_currentLod]._indices.push_back(gridY * (gridX + 1) + i + step);
2019-11-23 20:27:39 +08:00
}
}
2021-12-25 10:04:45 +08:00
if (_back && _back->_currentLod > _currentLod) // back
2019-11-23 20:27:39 +08:00
{
for (int i = 0; i < gridX; i += next_step)
{
_lod[_currentLod]._indices.push_back(i);
2021-12-25 10:04:45 +08:00
_lod[_currentLod]._indices.push_back(step * (gridX + 1) + i);
_lod[_currentLod]._indices.push_back(step * (gridX + 1) + i + step);
2019-11-23 20:27:39 +08:00
_lod[_currentLod]._indices.push_back(i);
2021-12-25 10:04:45 +08:00
_lod[_currentLod]._indices.push_back(step * (gridX + 1) + i + step);
2019-11-23 20:27:39 +08:00
_lod[_currentLod]._indices.push_back(i + next_step);
_lod[_currentLod]._indices.push_back(i + next_step);
2021-12-25 10:04:45 +08:00
_lod[_currentLod]._indices.push_back(step * (gridX + 1) + i + step);
_lod[_currentLod]._indices.push_back(step * (gridX + 1) + i + next_step);
2019-11-23 20:27:39 +08:00
}
}
2021-12-25 10:04:45 +08:00
else
{
2019-11-23 20:27:39 +08:00
for (int i = step; i < gridX - step; i += step)
{
_lod[_currentLod]._indices.push_back(i);
2021-12-25 10:04:45 +08:00
_lod[_currentLod]._indices.push_back(step * (gridX + 1) + i);
_lod[_currentLod]._indices.push_back(step * (gridX + 1) + i + step);
2019-11-23 20:27:39 +08:00
_lod[_currentLod]._indices.push_back(i);
2021-12-25 10:04:45 +08:00
_lod[_currentLod]._indices.push_back(step * (gridX + 1) + i + step);
2019-11-23 20:27:39 +08:00
_lod[_currentLod]._indices.push_back(i + step);
}
}
2021-12-25 10:04:45 +08:00
_chunkIndices = _terrain->insertIndicesLOD(currentNeighborLOD, _currentLod, &_lod[_currentLod]._indices[0],
(int)_lod[_currentLod]._indices.size());
2019-11-23 20:27:39 +08:00
}
2021-12-25 10:04:45 +08:00
else
{
// No lod difference, use simple method
2019-11-23 20:27:39 +08:00
_lod[_currentLod]._indices.clear();
for (int i = 0; i < gridY; i += step)
{
for (int j = 0; j < gridX; j += step)
{
int nLocIndex = i * (gridX + 1) + j;
_lod[_currentLod]._indices.push_back(nLocIndex);
_lod[_currentLod]._indices.push_back(nLocIndex + step * (gridX + 1));
_lod[_currentLod]._indices.push_back(nLocIndex + step);
_lod[_currentLod]._indices.push_back(nLocIndex + step);
_lod[_currentLod]._indices.push_back(nLocIndex + step * (gridX + 1));
_lod[_currentLod]._indices.push_back(nLocIndex + step * (gridX + 1) + step);
}
}
2021-12-25 10:04:45 +08:00
_chunkIndices = _terrain->insertIndicesLOD(currentNeighborLOD, _currentLod, &_lod[_currentLod]._indices[0],
(int)_lod[_currentLod]._indices.size());
2019-11-23 20:27:39 +08:00
}
}
void Terrain::Chunk::calculateAABB()
{
2021-12-25 10:04:45 +08:00
std::vector<Vec3> pos;
2019-11-23 20:27:39 +08:00
for (size_t i = 0, size = _originalVertices.size(); i < size; ++i)
{
pos.push_back(_originalVertices[i]._position);
}
_aabb.updateMinMax(&pos[0], pos.size());
}
void Terrain::Chunk::calculateSlope()
{
2021-12-25 10:04:45 +08:00
// find max slope
2019-11-23 20:27:39 +08:00
auto lowest = _originalVertices[0]._position;
for (size_t i = 0, size = _originalVertices.size(); i < size; ++i)
{
if (_originalVertices[i]._position.y < lowest.y)
{
lowest = _originalVertices[i]._position;
}
}
auto highest = _originalVertices[0]._position;
for (size_t i = 0, size = _originalVertices.size(); i < size; ++i)
{
if (_originalVertices[i]._position.y > highest.y)
{
highest = _originalVertices[i]._position;
}
}
Vec2 a(lowest.x, lowest.z);
Vec2 b(highest.x, highest.z);
float dist = a.distance(b);
2021-12-25 10:04:45 +08:00
_slope = (highest.y - lowest.y) / dist;
2019-11-23 20:27:39 +08:00
}
bool Terrain::Chunk::getIntersectPointWithRay(const Ray& ray, Vec3& intersectPoint)
{
if (!ray.intersects(_aabb))
return false;
float minDist = FLT_MAX;
2021-12-25 10:04:45 +08:00
bool isFind = false;
2019-11-23 20:27:39 +08:00
for (const auto& triangle : _trianglesList)
{
Vec3 p;
if (triangle.getIntersectPoint(ray, p))
{
float dist = ray._origin.distance(p);
if (dist < minDist)
{
intersectPoint = p;
2021-12-25 10:04:45 +08:00
minDist = dist;
2019-11-23 20:27:39 +08:00
}
isFind = true;
}
}
return isFind;
}
void Terrain::Chunk::updateVerticesForLOD()
{
2021-12-25 10:04:45 +08:00
if (_oldLod == _currentLod)
{
return;
} // no need to update vertices
2019-11-23 20:27:39 +08:00
_currentVertices = _originalVertices;
2021-12-25 10:04:45 +08:00
int gridY = _size.height;
int gridX = _size.width;
2019-11-23 20:27:39 +08:00
if (_currentLod >= 2 && std::abs(_slope) > 1.2f)
{
int step = 1 << _currentLod;
for (int i = step; i < gridY - step; i += step)
for (int j = step; j < gridX - step; j += step)
{
// use linear-sample adjust vertices height
float height = 0;
2021-12-25 10:04:45 +08:00
float count = 0;
2019-11-23 20:27:39 +08:00
for (int n = i - step / 2; n < i + step / 2; n++)
{
for (int m = j - step / 2; m < j + step / 2; m++)
{
2021-12-25 10:04:45 +08:00
float weight = (step / 2 - std::abs(n - i)) * (step / 2 - std::abs(m - j));
height += _originalVertices[m * (gridX + 1) + n]._position.y;
2019-11-23 20:27:39 +08:00
count += weight;
}
}
2021-12-25 10:04:45 +08:00
_currentVertices[i * (gridX + 1) + j]._position.y = height / count;
2019-11-23 20:27:39 +08:00
}
}
_oldLod = _currentLod;
}
Terrain::Chunk::~Chunk()
{
CC_SAFE_RELEASE_NULL(_buffer);
}
void Terrain::Chunk::updateIndicesLODSkirt()
{
2021-12-25 10:04:45 +08:00
if (_oldLod == _currentLod)
return;
2019-11-23 20:27:39 +08:00
_oldLod = _currentLod;
bool isOk;
_chunkIndices = _terrain->lookForIndicesLODSkrit(_currentLod, &isOk);
2021-12-25 10:04:45 +08:00
if (isOk)
return;
2019-11-23 20:27:39 +08:00
int gridY = _size.height;
int gridX = _size.width;
2021-12-25 10:04:45 +08:00
int step = 1 << _currentLod;
int k = 0;
2019-11-23 20:27:39 +08:00
for (int i = 0; i < gridY; i += step, k += step)
{
for (int j = 0; j < gridX; j += step)
{
int nLocIndex = i * (gridX + 1) + j;
_lod[_currentLod]._indices.push_back(nLocIndex);
_lod[_currentLod]._indices.push_back(nLocIndex + step * (gridX + 1));
_lod[_currentLod]._indices.push_back(nLocIndex + step);
_lod[_currentLod]._indices.push_back(nLocIndex + step);
_lod[_currentLod]._indices.push_back(nLocIndex + step * (gridX + 1));
_lod[_currentLod]._indices.push_back(nLocIndex + step * (gridX + 1) + step);
}
}
2021-12-25 10:04:45 +08:00
// add skirt
2019-11-23 20:27:39 +08:00
//#1
for (int i = 0; i < gridY; i += step)
{
int nLocIndex = i * (gridX + 1) + gridX;
_lod[_currentLod]._indices.push_back(nLocIndex);
_lod[_currentLod]._indices.push_back(nLocIndex + step * (gridX + 1));
2021-12-25 10:04:45 +08:00
_lod[_currentLod]._indices.push_back((gridY + 1) * (gridX + 1) + i);
2019-11-23 20:27:39 +08:00
2021-12-25 10:04:45 +08:00
_lod[_currentLod]._indices.push_back((gridY + 1) * (gridX + 1) + i);
2019-11-23 20:27:39 +08:00
_lod[_currentLod]._indices.push_back(nLocIndex + step * (gridX + 1));
2021-12-25 10:04:45 +08:00
_lod[_currentLod]._indices.push_back((gridY + 1) * (gridX + 1) + i + step);
2019-11-23 20:27:39 +08:00
}
//#2
for (int j = 0; j < gridX; j += step)
{
2021-12-25 10:04:45 +08:00
int nLocIndex = (gridY) * (gridX + 1) + j;
2019-11-23 20:27:39 +08:00
_lod[_currentLod]._indices.push_back(nLocIndex);
_lod[_currentLod]._indices.push_back(_terrain->_skirtVerticesOffset[1] + j);
_lod[_currentLod]._indices.push_back(nLocIndex + step);
_lod[_currentLod]._indices.push_back(nLocIndex + step);
_lod[_currentLod]._indices.push_back(_terrain->_skirtVerticesOffset[1] + j);
_lod[_currentLod]._indices.push_back(_terrain->_skirtVerticesOffset[1] + j + step);
}
//#3
for (int i = 0; i < gridY; i += step)
{
int nLocIndex = i * (gridX + 1);
_lod[_currentLod]._indices.push_back(nLocIndex);
_lod[_currentLod]._indices.push_back(_terrain->_skirtVerticesOffset[2] + i);
2021-12-25 10:04:45 +08:00
_lod[_currentLod]._indices.push_back((i + step) * (gridX + 1));
2019-11-23 20:27:39 +08:00
2021-12-25 10:04:45 +08:00
_lod[_currentLod]._indices.push_back((i + step) * (gridX + 1));
2019-11-23 20:27:39 +08:00
_lod[_currentLod]._indices.push_back(_terrain->_skirtVerticesOffset[2] + i);
_lod[_currentLod]._indices.push_back(_terrain->_skirtVerticesOffset[2] + i + step);
}
//#4
for (int j = 0; j < gridX; j += step)
{
int nLocIndex = j;
_lod[_currentLod]._indices.push_back(nLocIndex + step);
_lod[_currentLod]._indices.push_back(_terrain->_skirtVerticesOffset[3] + j);
_lod[_currentLod]._indices.push_back(nLocIndex);
_lod[_currentLod]._indices.push_back(_terrain->_skirtVerticesOffset[3] + j + step);
_lod[_currentLod]._indices.push_back(_terrain->_skirtVerticesOffset[3] + j);
_lod[_currentLod]._indices.push_back(nLocIndex + step);
}
2021-12-25 10:04:45 +08:00
_chunkIndices = _terrain->insertIndicesLODSkirt(_currentLod, &_lod[_currentLod]._indices[0],
(int)_lod[_currentLod]._indices.size());
2019-11-23 20:27:39 +08:00
}
2021-12-25 10:04:45 +08:00
Terrain::QuadTree::QuadTree(int x, int y, int w, int h, Terrain* terrain)
2019-11-23 20:27:39 +08:00
{
2021-12-25 10:04:45 +08:00
_terrain = terrain;
_needDraw = true;
_parent = nullptr;
_tl = nullptr;
_tr = nullptr;
_bl = nullptr;
_br = nullptr;
_posX = x;
_posY = y;
2019-11-23 20:27:39 +08:00
this->_height = h;
2021-12-25 10:04:45 +08:00
this->_width = w;
if (_width > terrain->_chunkSize.width && _height > terrain->_chunkSize.height) // subdivision
2019-11-23 20:27:39 +08:00
{
2021-12-25 10:04:45 +08:00
_isTerminal = false;
this->_tl = new QuadTree(x, y, _width / 2, _height / 2, terrain);
2019-11-23 20:27:39 +08:00
this->_tl->_parent = this;
2021-12-25 10:04:45 +08:00
this->_tr = new QuadTree(x + _width / 2, y, _width / 2, _height / 2, terrain);
2019-11-23 20:27:39 +08:00
this->_tr->_parent = this;
2021-12-25 10:04:45 +08:00
this->_bl = new QuadTree(x, y + _height / 2, _width / 2, _height / 2, terrain);
2019-11-23 20:27:39 +08:00
this->_bl->_parent = this;
2021-12-25 10:04:45 +08:00
this->_br = new QuadTree(x + _width / 2, y + _height / 2, _width / 2, _height / 2, terrain);
2019-11-23 20:27:39 +08:00
this->_br->_parent = this;
_localAABB.merge(_tl->_localAABB);
_localAABB.merge(_tr->_localAABB);
_localAABB.merge(_bl->_localAABB);
_localAABB.merge(_br->_localAABB);
}
2021-12-25 10:04:45 +08:00
else // is terminal Node
2019-11-23 20:27:39 +08:00
{
2021-12-25 10:04:45 +08:00
int m = _posY / terrain->_chunkSize.height;
int n = _posX / terrain->_chunkSize.width;
_chunk = terrain->_chunkesArray[m][n];
_isTerminal = true;
_localAABB = _chunk->_aabb;
2019-11-23 20:27:39 +08:00
_chunk->_parent = this;
2021-12-25 10:04:45 +08:00
for (auto& triangle : _chunk->_trianglesList)
2019-11-23 20:27:39 +08:00
{
triangle.transform(_terrain->getNodeToWorldTransform());
}
}
_worldSpaceAABB = _localAABB;
_worldSpaceAABB.transform(_terrain->getNodeToWorldTransform());
}
void Terrain::QuadTree::draw()
{
2021-12-25 10:04:45 +08:00
if (!_needDraw)
return;
if (_isTerminal)
{
2019-11-23 20:27:39 +08:00
this->_chunk->bindAndDraw();
}
else
{
this->_tl->draw();
this->_tr->draw();
this->_br->draw();
this->_bl->draw();
}
}
void Terrain::QuadTree::resetNeedDraw(bool value)
{
this->_needDraw = value;
if (!_isTerminal)
{
_tl->resetNeedDraw(value);
_tr->resetNeedDraw(value);
_bl->resetNeedDraw(value);
_br->resetNeedDraw(value);
}
}
2021-12-25 10:04:45 +08:00
void Terrain::QuadTree::cullByCamera(const Camera* camera, const Mat4& worldTransform)
2019-11-23 20:27:39 +08:00
{
2021-12-25 10:04:45 +08:00
if (!camera->isVisibleInFrustum(&_worldSpaceAABB))
2019-11-23 20:27:39 +08:00
{
this->resetNeedDraw(false);
}
2021-12-25 10:04:45 +08:00
else if (!_isTerminal)
{
2019-11-23 20:27:39 +08:00
_tl->cullByCamera(camera, worldTransform);
_tr->cullByCamera(camera, worldTransform);
_bl->cullByCamera(camera, worldTransform);
_br->cullByCamera(camera, worldTransform);
}
}
2021-12-25 10:04:45 +08:00
void Terrain::QuadTree::preCalculateAABB(const Mat4& worldTransform)
2019-11-23 20:27:39 +08:00
{
_worldSpaceAABB = _localAABB;
_worldSpaceAABB.transform(worldTransform);
2021-12-25 10:04:45 +08:00
if (!_isTerminal)
{
2019-11-23 20:27:39 +08:00
_tl->preCalculateAABB(worldTransform);
_tr->preCalculateAABB(worldTransform);
_bl->preCalculateAABB(worldTransform);
_br->preCalculateAABB(worldTransform);
}
}
Terrain::QuadTree::~QuadTree()
{
2021-12-25 10:04:45 +08:00
if (_tl)
delete _tl;
if (_tr)
delete _tr;
if (_bl)
delete _bl;
if (_br)
delete _br;
2019-11-23 20:27:39 +08:00
}
Terrain::TerrainData::TerrainData(std::string_view heightMapsrc,
std::string_view textureSrc,
2021-12-25 10:04:45 +08:00
const Vec2& chunksize,
float height,
float scale)
2019-11-23 20:27:39 +08:00
{
2021-12-25 10:04:45 +08:00
this->_heightMapSrc = heightMapsrc;
2019-11-23 20:27:39 +08:00
this->_detailMaps[0]._detailMapSrc = textureSrc;
2021-12-25 10:04:45 +08:00
this->_alphaMapSrc = "";
this->_chunkSize = chunksize;
this->_mapHeight = height;
this->_mapScale = scale;
_skirtHeightRatio = 1;
}
Terrain::TerrainData::TerrainData(std::string_view heightMapsrc,
std::string_view alphamap,
2021-12-25 10:04:45 +08:00
const DetailMap& detail1,
const DetailMap& detail2,
const DetailMap& detail3,
const DetailMap& detail4,
const Vec2& chunksize,
float height,
float scale)
{
this->_heightMapSrc = heightMapsrc;
this->_alphaMapSrc = alphamap;
2019-11-23 20:27:39 +08:00
this->_detailMaps[0] = detail1;
this->_detailMaps[1] = detail2;
this->_detailMaps[2] = detail3;
this->_detailMaps[3] = detail4;
2021-12-25 10:04:45 +08:00
this->_chunkSize = chunksize;
this->_mapHeight = height;
this->_mapScale = scale;
_detailMapAmount = 4;
_skirtHeightRatio = 1;
}
Terrain::TerrainData::TerrainData(std::string_view heightMapsrc,
std::string_view alphamap,
2021-12-25 10:04:45 +08:00
const DetailMap& detail1,
const DetailMap& detail2,
const DetailMap& detail3,
const Vec2& chunksize /*= Vec2(32,32)*/,
float height /*= 2*/,
float scale /*= 0.1*/)
{
this->_heightMapSrc = heightMapsrc;
this->_alphaMapSrc = alphamap;
2019-11-23 20:27:39 +08:00
this->_detailMaps[0] = detail1;
this->_detailMaps[1] = detail2;
this->_detailMaps[2] = detail3;
2021-12-25 10:04:45 +08:00
this->_chunkSize = chunksize;
this->_mapHeight = height;
this->_mapScale = scale;
_detailMapAmount = 3;
_skirtHeightRatio = 1;
2019-11-23 20:27:39 +08:00
}
2021-12-25 10:04:45 +08:00
Terrain::TerrainData::TerrainData() {}
2019-11-23 20:27:39 +08:00
2021-12-25 10:04:45 +08:00
Terrain::ChunkIndices::ChunkIndices(const Terrain::ChunkIndices& other)
2019-11-23 20:27:39 +08:00
{
_indexBuffer = other._indexBuffer;
CC_SAFE_RETAIN(_indexBuffer);
_size = other._size;
}
2021-12-25 10:04:45 +08:00
Terrain::ChunkIndices& Terrain::ChunkIndices::operator=(const Terrain::ChunkIndices& other)
2019-11-23 20:27:39 +08:00
{
2021-12-25 10:04:45 +08:00
if (other._indexBuffer != _indexBuffer)
2019-11-23 20:27:39 +08:00
{
CC_SAFE_RELEASE_NULL(_indexBuffer);
_indexBuffer = other._indexBuffer;
CC_SAFE_RETAIN(_indexBuffer);
}
_size = other._size;
return *this;
}
Terrain::ChunkIndices::~ChunkIndices()
{
CC_SAFE_RELEASE_NULL(_indexBuffer);
}
Terrain::DetailMap::DetailMap(std::string_view detailMapPath, float size /*= 35*/)
2019-11-23 20:27:39 +08:00
{
2021-12-25 10:04:45 +08:00
this->_detailMapSrc = detailMapPath;
2019-11-23 20:27:39 +08:00
this->_detailMapSize = size;
}
Terrain::DetailMap::DetailMap()
{
2021-12-25 10:04:45 +08:00
_detailMapSrc = "";
2019-11-23 20:27:39 +08:00
_detailMapSize = 35;
}
Terrain::Triangle::Triangle(const Vec3& p1, const Vec3& p2, const Vec3& p3)
{
_p1 = p1;
_p2 = p2;
_p3 = p3;
}
void Terrain::Triangle::transform(const cocos2d::Mat4& matrix)
{
matrix.transformPoint(&_p1);
matrix.transformPoint(&_p2);
matrix.transformPoint(&_p3);
}
2021-12-25 10:04:45 +08:00
// Please refer to 3D Math Primer for Graphics and Game Development
2019-11-23 20:27:39 +08:00
bool Terrain::Triangle::getIntersectPoint(const Ray& ray, Vec3& intersectPoint) const
{
// E1
Vec3 E1 = _p2 - _p1;
// E2
Vec3 E2 = _p3 - _p1;
// P
Vec3 P;
Vec3::cross(ray._direction, E2, &P);
// determinant
float det = E1.dot(P);
// keep det > 0, modify T accordingly
Vec3 T;
if (det > 0)
{
T = ray._origin - _p1;
}
else
{
2021-12-25 10:04:45 +08:00
T = _p1 - ray._origin;
2019-11-23 20:27:39 +08:00
det = -det;
}
// If determinant is near zero, ray lies in plane of triangle
if (det < 0.0001f)
return false;
2021-12-25 10:04:45 +08:00
float t; // ray dist
float u, v; // barycentric coordinate
// Calculate u and make sure u <= 1
2019-11-23 20:27:39 +08:00
u = T.dot(P);
if (u < 0.0f || u > det)
return false;
// Q
Vec3 Q;
Vec3::cross(T, E1, &Q);
// Calculate v and make sure u + v <= 1
v = ray._direction.dot(Q);
if (v < 0.0f || u + v > det)
return false;
// Calculate t, scale parameters, ray intersects triangle
t = E2.dot(Q);
float fInvDet = 1.0f / det;
t *= fInvDet;
intersectPoint = ray._origin + ray._direction * t;
return true;
}
void Terrain::onBeforeDraw()
{
_stateBlockOld.save();
_stateBlock.apply();
}
void Terrain::onAfterDraw()
{
_stateBlockOld.apply();
}
void Terrain::StateBlock::save()
{
auto renderer = Director::getInstance()->getRenderer();
2021-12-25 10:04:45 +08:00
depthWrite = renderer->getDepthWrite();
depthTest = renderer->getDepthTest();
cullFace = renderer->getCullMode();
winding = renderer->getWinding();
2019-11-23 20:27:39 +08:00
}
void Terrain::StateBlock::apply()
{
auto renderer = Director::getInstance()->getRenderer();
renderer->setDepthTest(depthTest);
renderer->setDepthWrite(depthWrite);
renderer->setCullMode(cullFace);
renderer->setWinding(winding);
}
NS_CC_END