我有一个可以通过OpenGL绘制三角形的函数
我通过按下一个按钮绘制两个三角形(函数on_drawMapPushButton_clicked())。
然后我在这些三角形上方绘制一个球体。现在我看到,该球体正确地绘制在第一个三角形上,但是第二个三角形却绘制了该球体,反之亦然。
如果我第二次按下按钮,则会在第一个和第二个三角形上正确绘制spehere。
当我第三次按下按钮时,第二个三角形再次在球体上绘制。
当我第四次按下按钮时,会在第一个和第二个三角形上正确绘制spehere,依此类推。
如果我在SphereMesh QPhongMaterial而不是QPhongAlphaMaterial中使用,则始终在第一个和第二个三角形上正确绘制spehere。喜欢它必须是。
我不明白我为使球体总是绘制在三角形上做错了什么。
绘制透明球体的代码:
selectModel_ = new Qt3DExtras::QSphereMesh(selectEntity_);
selectModel_->setRadius(75);
selectModel_->setSlices(150);
selectMaterial_ = new Qt3DExtras::QPhongAlphaMaterial(selectEntity_);
selectMaterial_->setAmbient(QColor(28, 61, 136));
selectMaterial_->setDiffuse(QColor(11, 56, 159));
selectMaterial_->setSpecular(QColor(10, 67, 199));
selectMaterial_->setShininess(0.8f);
selectEntity_->addComponent(selectModel_);
selectEntity_->addComponent(selectMaterial_);
Run Code Online (Sandbox Code Playgroud)
函数drawTriangles:
void drawTriangles(QPolygonF triangles, QColor color){
int numOfVertices = triangles.size();
// Create and fill vertex buffer
QByteArray bufferBytes;
bufferBytes.resize(3 * numOfVertices * static_cast<int>(sizeof(float)));
float *positions = reinterpret_cast<float*>(bufferBytes.data());
for(auto point : triangles){
*positions++ = static_cast<float>(point.x());
*positions++ = 0.0f; //We need to drow only on the surface
*positions++ = static_cast<float>(point.y()); …Run Code Online (Sandbox Code Playgroud)