**
**
```cpp
#include "widget.h"
#include
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
Triangle t;
t.resize(1000,800);
t.show();
return a.exec();
}
**
#include "shader.h"
Shader::Shader(const QString& vertexPath, const QString& fragmentPath){
QOpenGLShader vertexShader(QOpenGLShader::Vertex);
bool success = vertexShader.compileSourceFile(vertexPath);
if(!success){
qDebug() << "ERROR::SHADER::VERTEX::COMPILATION_FAILED" << endl;
qDebug() << vertexShader.log() << endl;
}
QOpenGLShader fragmentShader(QOpenGLShader::Fragment);
success =fragmentShader.compileSourceFile(fragmentPath);
if(!success){
qDebug() << "ERROR::SHADER::FRAGMENT::COMPILATION_FAILED" << endl;
qDebug() << fragmentShader.log() << endl;
}
shaderProgram.addShader(&vertexShader);
shaderProgram.addShader(&fragmentShader);
success = shaderProgram.link();
if(!success){
qDebug() << "ERROR::SHADER::PROGRAM::LINKING_FAILED" << endl;
qDebug() << shaderProgram.log() << endl;
}
}
Shader::~Shader(){
}
#include "widget.h"
#include
#include
#include
#include
GLfloat vertices[100000000];
GLuint VBO, VAO;
Triangle::Triangle(){
this->setWindowTitle("STL_Show");
}
Triangle::~Triangle(){
delete ourShader;
core->glDeleteVertexArrays(1, &VAO);
core->glDeleteBuffers(1, &VBO);
// delete vertices;
}
void Triangle::initializeGL(){
//着色器部分
core = QOpenGLContext::currentContext()->versionFunctions<QOpenGLFunctions_3_3_Core>();
ourShader = new Shader(":/shader/vertexshadersource.txt", ":/shader/fragmentshadersource.txt");
//vertices = (float *) malloc(sizeof (float)*10000000);
//VAO,VBO数据部分
this->stl_Read(); //获取stl文件,并存入vertices数组。
core->glGenVertexArrays(1, &VAO);//两个参数,第一个为需要创建的缓存数量。第二个为用于存储单一ID或多个ID的GLuint变量或数组的地址
core->glGenBuffers(1, &VBO);
core->glBindVertexArray(VAO);
core->glBindBuffer(GL_ARRAY_BUFFER, VBO);
core->glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
core->glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 6 * sizeof(float), (void*)0);
core->glEnableVertexAttribArray(0);
core->glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 6 * sizeof(GLfloat), (GLvoid*)(3*sizeof(GLfloat)));
core->glEnableVertexAttribArray(1);
core->glBindBuffer(GL_ARRAY_BUFFER, 0);
core->glBindVertexArray(0);
// view matrix
view.setToIdentity();
view.lookAt(QVector3D(0.0f, 0.0f, 1.0f), QVector3D(0.0f,0.0f,0.0f), QVector3D(0.0f,1.0f,0.0f));
//开启状态
core->glClearColor(0.2f, 0.3f, 0.3f, 1.0f);
core->glEnable(GL_DEPTH_TEST);
}
void Triangle::resizeGL(int w, int h){
core->glViewport(0, 0, w, h);
projection.setToIdentity();
projection.perspective(60.0f, (GLfloat)w/(GLfloat)h, 0.1f, 100.0f);
}
void Triangle::paintGL(){
core->glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
// transform the model
model.setToIdentity();
model.translate(xtrans, ytrans, ztrans);
model.rotate(rotation);
QMatrix4x4 mv = view * model;
ourShader->setMat4("ModelViewMatrix", mv);
ourShader->setMat4("MVP", projection * mv);
ourShader->use();
core->glBindVertexArray(VAO);
//绘制三角形
core->glDrawArrays(GL_TRIANGLES, 0, VerticesCnt/6);
//绘制点云
//core->glDrawArrays(GL_POINTS, 0, VerticesCnt/2);
}
void Triangle::mousePressEvent(QMouseEvent *event)
{
mousePos = QVector2D(event->pos());
event->accept();
}
void Triangle::mouseMoveEvent(QMouseEvent *event)
{
if(event->buttons() == Qt::LeftButton)
{
QVector2D newPos = (QVector2D)event->pos();
QVector2D diff = newPos - mousePos;
qreal angle = (diff.length())/3.6;
QVector3D rotationAxis = QVector3D(diff.y(), diff.x(), 0.0).normalized();
rotation = QQuaternion::fromAxisAndAngle(rotationAxis, angle) * rotation;
mousePos = newPos;
this->update();
}
event->accept();
}
void Triangle::wheelEvent(QWheelEvent *event)
{
QPoint numDegrees = event->angleDelta() / 8;
if (numDegrees.y() > 0) {
ztrans += 0.25f;
} else if (numDegrees.y() < 0) {
ztrans -= 0.25f;
}
this->update();
event->accept();
}
void Triangle::stl_Read() //读取二进制stl文件
{
GLint fileSize; //记录文件大小
QFile STL_file(":/shader/l2.STL");
bool isOk = STL_file.open(QIODevice::ReadOnly);
fileSize = STL_file.size();
GLchar * buf = (char *)malloc(sizeof(char)*fileSize);
if(isOk == true)
{
QDataStream stream(&STL_file);
stream.readRawData(buf,fileSize);
STL_file.close();
}
const GLchar* p = buf;
GLchar name[80];
GLfloat temp;
GLint binary_i;
GLint triangle_num;
memcpy(name,p,80);//记录文件名
p += 80; //跳过文件名
memcpy(&triangle_num,p,4);//记录三角形个数
p += 4; //跳过个数标识
qDebug()<<triangle_num<<"\n"; //控制台打印三角形个数
for (binary_i=0;binary_i<triangle_num;binary_i++)
{
p += 12;//跳过头部的法向量
//vertex
memcpy(&temp,p,4);vertices[VerticesCnt] = temp ;p+=4;VerticesCnt++;//vertices[0]赋值
memcpy(&temp,p,4);vertices[VerticesCnt] = temp;p+=4;VerticesCnt++;//vertices[1]赋值
memcpy(&temp,p,4);vertices[VerticesCnt] = temp ;p+=4;VerticesCnt++;//vertices[2]赋值
vertices[VerticesCnt] = 1.0;VerticesCnt++;
vertices[VerticesCnt] = 0.9;VerticesCnt++;
vertices[VerticesCnt] = 0.6;VerticesCnt++;
//vertex
memcpy(&temp,p,4);vertices[VerticesCnt] = temp ;p+=4;VerticesCnt++;//vertices[0]赋值
memcpy(&temp,p,4);vertices[VerticesCnt] = temp ;p+=4;VerticesCnt++;//vertices[1]赋值
memcpy(&temp,p,4);vertices[VerticesCnt] = temp ;p+=4;VerticesCnt++;//vertices[2]赋值
vertices[VerticesCnt] = 1.0;VerticesCnt++;
vertices[VerticesCnt] = 0.9;VerticesCnt++;
vertices[VerticesCnt] = 0.6;VerticesCnt++;
//vertex
memcpy(&temp,p,4);vertices[VerticesCnt] = temp;p+=4;VerticesCnt++;//vertices[0]赋值
memcpy(&temp,p,4);vertices[VerticesCnt] = temp ;p+=4;VerticesCnt++;//vertices[1]赋值
memcpy(&temp,p,4);vertices[VerticesCnt] = temp ;p+=4;VerticesCnt++;//vertices[2]赋值
vertices[VerticesCnt] = 1.0;VerticesCnt++;
vertices[VerticesCnt] = 0.9;VerticesCnt++;
vertices[VerticesCnt] = 0.6;VerticesCnt++;
p += 2;//跳过尾部标志
}
free(buf);
}
参考博客:
https://blog.csdn.net/qq_27133869/article/details/105645644
https://blog.csdn.net/chaojiwudixiaofeixia/article/details/78043051