void GLWidget::loadHeightMap(QString filepath, QVector3D center, QVector3D size, QVector2D maxUV, QOpenGLTexture* texture) { QImage img; img.load(filepath); float boxSizeX = size.x()/img.width(); float boxSizeZ = size.z()/img.height(); float offsetX = -boxSizeX * img.width()/2 + center.x(); float offsetZ = -boxSizeZ * img.height()/2; float minY = center.y() - size.y() / 2; float maxY = center.y() + size.y() / 2; int groupSize = (img.width() - 1) * (img.height() - 1) * 2; for(int i = 0 ; i < img.height() - 1 ; i++) { for(int j = 0 ; j < img.width() - 1 ; j++) { float X1 = offsetX + boxSizeX * i; float X2 = offsetX + boxSizeX * (i+1); float Z1 = offsetZ + boxSizeZ * j; float Z2 = offsetZ + boxSizeZ * (j+1); float Y00 = QColor(img.pixel(j, i)).red() / 255.0f * (maxY - minY) + minY; float Y01 = QColor(img.pixel(j+1, i)).red() / 255.0f * (maxY - minY) + minY; float Y10 = QColor(img.pixel(j, i+1)).red() / 255.0f * (maxY - minY) + minY; float Y11 = QColor(img.pixel(j+1, i+1)).red() / 255.0f * (maxY - minY) + minY; QVector2D texCoord00(maxUV.x() * i / img.width(), maxUV.y() * j / img.height()); QVector2D texCoord01(maxUV.x() * (i+1) / img.width(), maxUV.y() * j / img.height()); QVector2D texCoord10(maxUV.x() * i / img.width(), maxUV.y() * (j+1) / img.height()); QVector2D texCoord11(maxUV.x() * (i+1) / img.width(), maxUV.y() * (j+1) / img.height()); addTriangleCollider(QVector3D(X1, Y00, Z1), QVector3D(X1, Y01, Z2), QVector3D(X2, Y11, Z2), groupSize, texCoord00, texCoord10, texCoord11, texture); addTriangleCollider(QVector3D(X2, Y11, Z2), QVector3D(X2, Y10, Z1), QVector3D(X1, Y00, Z1), groupSize, texCoord11, texCoord01, texCoord00, texture); } } }