写一个cpp函数,参数有QRect, 图像宽高, 然后转换为opengl可用顶点矩阵, 结果放入GLfloat*
时间: 2023-05-22 21:07:13 浏览: 97
好的,下面是将给定的QRect和图像宽高转换为opengl可用的顶点矩阵的cpp函数:
```cpp
void transformQRectToOpenGLVertices(QRect rect, int imageWidth, int imageHeight, GLfloat* vertices) {
// 计算rect的四个顶点的坐标
float leftX = (float)rect.left() / (float)imageWidth * 2.0f - 1.0f;
float rightX = (float)(rect.right() + 1) / (float)imageWidth * 2.0f - 1.0f;
float topY = 1.0f - (float)rect.top() / (float)imageHeight * 2.0f;
float bottomY = 1.0f - (float)(rect.bottom() + 1) / (float)imageHeight * 2.0f;
// 将四个顶点的坐标存入vertices数组
vertices[0] = leftX; vertices[1] = topY;
vertices[2] = leftX; vertices[3] = bottomY;
vertices[4] = rightX; vertices[5] = bottomY;
vertices[6] = rightX; vertices[7] = topY;
}
```
可以将结果存入提供的GLfloat数组中。顶点顺序为左上角,左下角,右下角和右上角。
阅读全文