寻找外贸客户的网站手机端怎么变成电脑端
news/
2025/10/4 3:48:28/
文章来源:
寻找外贸客户的网站,手机端怎么变成电脑端,东莞人才市场官网,新新手手网网站站建建设设1前面我们已经熟悉了opengl自定义顶点生成一个立方体#xff0c;并且我们实现了立方体的旋转#xff0c;光照等功能。下面我们来用opengl来加载一个obj文件。准备我们首先准备一个简单的obj文件#xff08;head.obj#xff09;。资源在本页下载 2 在obj文件里面#xff0c… 1前面我们已经熟悉了opengl自定义顶点生成一个立方体并且我们实现了立方体的旋转光照等功能。下面我们来用opengl来加载一个obj文件。准备我们首先准备一个简单的obj文件head.obj。资源在本页下载 2 在obj文件里面我们关注下 v 字段和f字段 v 字段就是顶点 f字段代表了面意思是每个面的顶点编号。 结构如下
v -0.99179999 -2.98999995 4.05410025 表示顶点值
f 12791 127 126 表示这个面的三个点在 v 里面的index值是12791 也是就是v[12791]的值的顶点是这个面的顶点值。
3 下面我们来解析obj文件
bool load(QString fileName, QVectorfloat vPoints)
{if (fileName.mid(fileName.lastIndexOf(.)) ! .obj fileName.mid(fileName.lastIndexOf(.)) ! .OBJ){qDebug() file is not a obj file.;return false;}QFile objFile(fileName);if (!objFile.open(QIODevice::ReadOnly)){qDebug() open fileName failed;return false;}else{qDebug() open fileName success!;}QVectorfloat vertextPoints, texturePoints, normalPoints;QVectorFace facesIndexs;while (!objFile.atEnd()){QByteArray lineData objFile.readLine();QListQByteArray strValues lineData.trimmed().split( );QString dataType strValues.takeFirst();for(int i0;istrValues.size();i){double nData strValues.at(i).toDouble();QString strTemp QString::number(nData,f,4);if (dataType v){vertextPoints.append(strTemp.toFloat());}else if (dataType vt){texturePoints.append(strValues.at(i).toFloat());}else if (dataType vn){normalPoints.append(strValues.at(i).toFloat());}else if (dataType f){Face ondInfo;if(strValues.at(i).contains(/)){QListQByteArray strTemp strValues.at(i).split(/);if(strTemp.size()2){ondInfo.vertices strTemp.at(0).toInt();ondInfo.texCoords strTemp.at(1).toInt();}else if(strTemp.size()3){ondInfo.vertices strTemp.at(0).toInt();ondInfo.texCoords strTemp.at(1).toInt();ondInfo.normals strTemp.at(2).toInt();}}else{ondInfo.vertices strValues.at(i).toInt();//qDebug()Face ondInfoondInfo.vertices;}facesIndexs.append(ondInfo);}}}objFile.close();int count 0;for (int i0;ifacesIndexs.size();i){int vIndex facesIndexs[i].vertices - 1;if(vIndex * 3 vertextPoints.size() ||vIndex * 3 1 vertextPoints.size()||vIndex * 3 2vertextPoints.size() ){vPoints vertextPoints.at(vIndex * 3);vPoints vertextPoints.at(vIndex * 3 1);vPoints vertextPoints.at(vIndex * 3 2);// qDebug()vIndexivertextPoints.size()vIndex * 3;}else{// qDebug()vIndex errorivertextPoints.size()vIndex * 3;}}vertextPoints.clear();texturePoints.clear();normalPoints.clear();facesIndexs.clear();return true;
} 接着我们来写加载代码
和以前的一样就是写glsl语句
#ifndef TESTOBJOPENGL_H
#define TESTOBJOPENGL_H#include QObject
#include QWidget
#include QOpenGLWidget
#include QOpenGLExtraFunctions
#include QOpenGLBuffer
#include QOpenGLShaderProgram
#include QOpenGLVertexArrayObject
#include QTimer
#include QOpenGLTexture
#include QOpenGLWidget
#include QOpenGLFunctions
#include QOpenGLBuffer
#include QOpenGLVertexArrayObject
#include QTimer
#include QMouseEventQT_FORWARD_DECLARE_CLASS(QOpenGLShaderProgram);
QT_FORWARD_DECLARE_CLASS(QOpenGLTexture)
class testobjOpengl : public QOpenGLWidget, protected QOpenGLFunctions
{
public:testobjOpengl( QWidget *parentnullptr);~testobjOpengl();
protected:void initializeGL() override;void paintGL() override;void resizeGL(int width, int height) override;void rotateBy(int xAngle, int yAngle, int zAngle);bool load(QString fileName, QVectorfloat vPoints);struct Face{int vertices;int texCoords;int normals;Face(){vertices -1;texCoords -1;normals -1;}};
private:QVectorfloat m_vPoints;int m_xRot;int m_yRot;int m_zRot;QOpenGLShaderProgram *m_program nullptr;QOpenGLVertexArrayObject vao;QOpenGLBuffer vbo;QVector3D cameraPos;QVector3D cameraTarget;QVector3D cameraDirection;QOpenGLTexture *texture;int m_projMatrixLoc;int m_mvMatrixLoc;int m_normalMatrixLoc;int m_lightPosLoc;QMatrix4x4 m_proj;QMatrix4x4 m_camera;QMatrix4x4 m_world;QVector3D m_camera_pos;QTimer* timer;int m_yPos0;int m_zPos0;};#endif // TESTOBJOPENGL_H#include testobjopengl.hstatic const char *vertexShaderSourceCore #version 330\nlayout (location 0) in vec4 vertex;\nlayout (location 1) in vec3 normal;\nout vec3 vert;\nout vec3 vertNormal;\nuniform mat4 matrix;\nuniform mat4 view;\nuniform mat4 projection;\nuniform mat3 normalMatrix;\nvoid main() {\n vert vertex.xyz;\n vertNormal normalMatrix * normal;\n gl_Position projection*view* matrix * vertex;\n}\n;
static const char *fragmentShaderSourceCore #version 150\nin highp vec3 vert;\nin highp vec3 vertNormal;\nout highp vec4 fragColor;\nuniform highp vec3 lightPos;\nvoid main() {\n highp vec3 L normalize(lightPos - vert);\n highp float NL max(dot(normalize(vertNormal), L), 0.0);\n highp vec3 color vec3(1.0, 1.0, 0.0);\n highp vec3 col clamp(color * 0.2 color * 0.8 * NL, 0.0, 1.0);\n fragColor vec4(col, 1.0);\n}\n;testobjOpengl::testobjOpengl(QWidget *parent): QOpenGLWidget(parent),m_xRot(0),m_yRot(0),m_zRot(0)
{m_vPoints.clear();cameraPos QVector3D(0, 0, 3);QString strTemp F:/Tree2/head.obj;bool ret load(strTemp,m_vPoints);if(ret){timer new QTimer;timer-setInterval(100);connect(timer,QTimer::timeout,this,[]{qDebug()timeout;rotateBy(10 * 16, 10 * 16, -1 * 16);});timer-start();}else{qDebug()1111111111111111;}}
void testobjOpengl::rotateBy(int xAngle, int yAngle, int zAngle)
{float cameraSpeed 0.2;m_camera_pos - cameraSpeed * QVector3D(0, 0, -1);//由远到近if(m_yPos100){m_yPos 0;}m_yPos10;if(m_zPos100){m_zPos 0;}m_zPos10;// m_camera_pos QVector3D(0, m_yPos, m_zPos);//由远到近// m_camera_pos.setY(m_yPos);m_xRot xAngle;m_yRot yAngle;m_zRot zAngle;update();//timer-stop();
}
testobjOpengl::~testobjOpengl()
{}void testobjOpengl::initializeGL()
{initializeOpenGLFunctions();m_program new QOpenGLShaderProgram;m_program-addShaderFromSourceCode(QOpenGLShader::Vertex, vertexShaderSourceCore);m_program-addShaderFromSourceCode(QOpenGLShader::Fragment, fragmentShaderSourceCore);if (m_program-link()){qDebug() link success!;}else{qDebug() link failed;}m_program-bindAttributeLocation(vertex, 0);m_program-bindAttributeLocation(normal, 1);m_program-link();m_program-bind();// m_projMatrixLoc m_program-uniformLocation(projection);
// m_mvMatrixLoc m_program-uniformLocation(matrix);
// m_normalMatrixLoc m_program-uniformLocation(normalMatrix);
// m_lightPosLoc m_program-uniformLocation(lightPos);vbo.create();vbo.bind();vbo.allocate(m_vPoints.data(), m_vPoints.size() * sizeof(float));QOpenGLFunctions *f QOpenGLContext::currentContext()-functions();f-glEnableVertexAttribArray(0);f-glEnableVertexAttribArray(1);f-glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(GLfloat), nullptr);f-glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(GLfloat), reinterpret_castvoid *(3 * sizeof(GLfloat)));vbo.release();m_program-setUniformValue(m_lightPosLoc, QVector3D(10, 10, 10));
}void testobjOpengl::paintGL()
{glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);glEnable(GL_DEPTH_TEST);glEnable(GL_CULL_FACE);QMatrix4x4 m;//m.ortho(-0.5f, 0.5f, 0.5f, -0.5f, 4.0f, 15.0f);m.setToIdentity();m.translate(0.0f, 0.0f, 0.0f);m.rotate(m_xRot / 16.0f, 1.0f, 0.0f, 0.0f);m.rotate(90, 0.0f, 1.0f, 0.0f);m.rotate(0, 0.0f, 0.0f, 1.0f);m.scale(0.5);QMatrix4x4 view;view.setToIdentity();view.lookAt(QVector3D(0, 20, 20), QVector3D(0,0,0), QVector3D(1,0,0));m_program-bind();m_program-setUniformValue(projection, m_proj);m_program-setUniformValue(matrix, m);m_program-setUniformValue(view, view);m_program-setUniformValue(lightPos, QVector3D(10, 10, 0));QMatrix3x3 normalMatrix m.normalMatrix();m_program-setUniformValue(normalMatrix, normalMatrix);glDrawArrays(GL_TRIANGLES, 0, m_vPoints.size()/3);m_program-release();
}void testobjOpengl::resizeGL(int width, int height)
{m_proj.setToIdentity();m_proj.perspective(45.0f, GLfloat(width) / height, 0.01f, 100.0f);
}
bool testobjOpengl::load(QString fileName, QVectorfloat vPoints)
{if (fileName.mid(fileName.lastIndexOf(.)) ! .obj fileName.mid(fileName.lastIndexOf(.)) ! .OBJ){qDebug() file is not a obj file.;return false;}QFile objFile(fileName);if (!objFile.open(QIODevice::ReadOnly)){qDebug() open fileName failed;return false;}else{qDebug() open fileName success!;}QVectorfloat vertextPoints, texturePoints, normalPoints;QVectorFace facesIndexs;while (!objFile.atEnd()){QByteArray lineData objFile.readLine();QListQByteArray strValues lineData.trimmed().split( );QString dataType strValues.takeFirst();for(int i0;istrValues.size();i){double nData strValues.at(i).toDouble();QString strTemp QString::number(nData,f,4);if (dataType v){vertextPoints.append(strTemp.toFloat());}else if (dataType vt){texturePoints.append(strValues.at(i).toFloat());}else if (dataType vn){normalPoints.append(strValues.at(i).toFloat());}else if (dataType f){Face ondInfo;if(strValues.at(i).contains(/)){QListQByteArray strTemp strValues.at(i).split(/);if(strTemp.size()2){ondInfo.vertices strTemp.at(0).toInt();ondInfo.texCoords strTemp.at(1).toInt();}else if(strTemp.size()3){ondInfo.vertices strTemp.at(0).toInt();ondInfo.texCoords strTemp.at(1).toInt();ondInfo.normals strTemp.at(2).toInt();}}else{ondInfo.vertices strValues.at(i).toInt();//qDebug()Face ondInfoondInfo.vertices;}facesIndexs.append(ondInfo);}}}objFile.close();int count 0;for (int i0;ifacesIndexs.size();i){int vIndex facesIndexs[i].vertices - 1;if(vIndex * 3 vertextPoints.size() ||vIndex * 3 1 vertextPoints.size()||vIndex * 3 2vertextPoints.size() ){vPoints vertextPoints.at(vIndex * 3);vPoints vertextPoints.at(vIndex * 3 1);vPoints vertextPoints.at(vIndex * 3 2);// qDebug()vIndexivertextPoints.size()vIndex * 3;}else{// qDebug()vIndex errorivertextPoints.size()vIndex * 3;}}vertextPoints.clear();texturePoints.clear();normalPoints.clear();facesIndexs.clear();return true;
}我们开始调用 testobjOpengl w;
w.show(); 运行结果
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/926568.shtml
如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!