Initialize the height-field
| 43 | |
| 44 | // Initialize the height-field |
| 45 | bool HeightField::init(int nbGridColumns, int nbGridRows, |
| 46 | const void* heightFieldData, HeightDataType dataType, |
| 47 | std::vector<Message>& messages, decimal integerHeightScale) { |
| 48 | |
| 49 | bool isValid = true; |
| 50 | |
| 51 | if (nbGridColumns < 2 || nbGridRows < 2) { |
| 52 | |
| 53 | // Add a warning message for the user |
| 54 | messages.push_back(Message("The number of grid columns and grid rows must be at least two", Message::Type::Error)); |
| 55 | return false; |
| 56 | } |
| 57 | |
| 58 | mHeightFieldData.reserve(nbGridColumns * nbGridRows); |
| 59 | mHeightFieldData.addWithoutInit(nbGridColumns * nbGridRows); |
| 60 | |
| 61 | mNbColumns = nbGridColumns; |
| 62 | mNbRows = nbGridRows; |
| 63 | mWidth = static_cast<decimal>(nbGridColumns - 1); |
| 64 | mLength = static_cast<decimal>(nbGridRows - 1); |
| 65 | mIntegerHeightScale = integerHeightScale; |
| 66 | mHeightDataType = dataType; |
| 67 | |
| 68 | // Copy the height values from the user into the height-field |
| 69 | copyData(heightFieldData); |
| 70 | |
| 71 | assert(mMinHeight <= mMaxHeight); |
| 72 | |
| 73 | const decimal halfHeight = (mMaxHeight - mMinHeight) * decimal(0.5); |
| 74 | assert(halfHeight >= 0); |
| 75 | |
| 76 | assert(mWidth >= 1); |
| 77 | assert(mLength >= 1); |
| 78 | |
| 79 | // Compute the local AABB of the height field |
| 80 | mBounds.setMin(Vector3(-mWidth * decimal(0.5), -halfHeight, -mLength * decimal(0.5))); |
| 81 | mBounds.setMax(Vector3(mWidth * decimal(0.5), halfHeight, mLength * decimal(0.5))); |
| 82 | |
| 83 | assert(mHeightFieldData.size() == mNbRows * mNbColumns); |
| 84 | |
| 85 | return isValid; |
| 86 | } |
| 87 | |
| 88 | // Copy the data from the user into the height-field array |
| 89 | void HeightField::copyData(const void* heightFieldData) { |