class GLWidget : public QOpenGLWidget, protected QOpenGLFunctions_3_3_Core {
public:
explicit GLWidget(QWidget* parent = nullptr):QOpenGLWidget(parent),m_program(new QOpenGLShaderProgram(this)) {m_models[0].yTrans = -100.0f;m_models[1].yTrans = 100.0f;}
protected:
struct Model {
std::vector<float> vertices;
std::vector<float> normals;
GLuint vao = 0;
GLuint vbo[2] = {0};
float xRot = 0.0f, yRot = 0.0f, zRot = 0.0f;
float xTrans = 0.0f, yTrans = 0.0f;
float scale = 0.005f;
};
Model m_models[2];
int m_selectedModel = 0;
QOpenGLShaderProgram* m_program;
QPoint m_lastPos;
QMatrix4x4 m_projection;
void initializeGL() override {
initializeOpenGLFunctions();
glClearColor(0.9f, 0.9f, 0.9f, 1.0f);
glEnable(GL_DEPTH_TEST);
m_program->addShaderFromSourceFile(QOpenGLShader::Vertex, “:/shaders/phong.vert”);
m_program->addShaderFromSourceFile(QOpenGLShader::Fragment, “:/shaders/phong.frag”);
m_program->link();
loadModel(m_models[0], “:/models/Fixture.STL”, -100.0f);
loadModel(m_models[1], “:/models/Light.STL”, 100.0f);
}
void paintGL() override {
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
m_program->bind();
m_program->setUniformValue(“projection”, m_projection);
m_program->setUniformValue(“lightPos”, QVector3D(0, 0, 500));
m_program->setUniformValue(“lightColor”, QVector3D(1, 1, 1));
QMatrix4x4 view;
view.translate(0, 0, -500);
drawBasePlane(view);
for (auto& model : m_models) {
QMatrix4x4 modelMat;
modelMat.translate(model.xTrans, model.yTrans, 0);
modelMat.rotate(model.xRot, 1, 0, 0);
modelMat.rotate(model.yRot, 0, 1, 0);
modelMat.scale(model.scale);
m_program->setUniformValue("model", modelMat);
m_program->setUniformValue("view", view);
m_program->setUniformValue("normalMat", modelMat.normalMatrix());
glBindVertexArray(model.vao);
glDrawArrays(GL_TRIANGLES, 0, model.vertices.size()/3);
}
m_program->release();
}
private:
void loadModel(Model& model, const QString& path, float yOffset) {
Assimp::Importer importer;
const aiScene* scene = importer.ReadFile(path.toStdString(),aiProcess_Triangulate | aiProcess_GenNormals | aiProcess_FlipUVs);
if (!scene || !scene->mRootNode) {qWarning(“Failed to load model: %s”, importer.GetErrorString()); return;}
for (unsigned i = 0; i < scene->mNumMeshes; ++i) {
const aiMesh* mesh = scene->mMeshes[i];
for (unsigned j = 0; j < mesh->mNumFaces; ++j) {
const aiFace& face = mesh->mFaces[j];
for (unsigned k = 0; k < 3; ++k) {
const aiVector3D& v = mesh->mVertices[face.mIndices[k]];
const aiVector3D& n = mesh->mNormals[face.mIndices[k]];
model.vertices.push_back(v.x);
model.vertices.push_back(v.y + yOffset);
model.vertices.push_back(v.z);
model.normals.push_back(n.x);
model.normals.push_back(n.y);
model.normals.push_back(n.z);
}
}
}
glGenVertexArrays(1, &model.vao);
glGenBuffers(2, model.vbo);
glBindVertexArray(model.vao);
glBindBuffer(GL_ARRAY_BUFFER, model.vbo[0]);
glBufferData(GL_ARRAY_BUFFER, model.vertices.size() * sizeof(float),model.vertices.data(), GL_STATIC_DRAW);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, nullptr);
glEnableVertexAttribArray(0);
glBindBuffer(GL_ARRAY_BUFFER, model.vbo[1]);
glBufferData(GL_ARRAY_BUFFER,model.normals.size() * sizeof(float),model.normals.data(), GL_STATIC_DRAW);
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 0, nullptr);
glEnableVertexAttribArray(1);
glBindVertexArray(0);
}
分析这段代码,获取模型1的x长度,将其显示在中轴线并且沿Y轴负方向旋转90度,将模型2沿Y方向旋转90度,获取模型2的x长度,将其显示在中轴线上