当前位置: 首页 > wzjs >正文

商城网站建设价位网站文章不收录怎么做

商城网站建设价位,网站文章不收录怎么做,wordpress登录循环,站长工具无内鬼放心开车禁止收费Qt 提供了强大的 OpenGL 集成功能,使得在 Qt 应用中实现 3D 图形变得更加简单。以下是使用 Qt 进行 OpenGL 3D 编程的基础知识。 1. 环境配置 创建 Qt 项目 新建 Qt Widgets Application 项目 在 .pro 文件中添加 OpenGL 模块: qmake QT co…

Qt 提供了强大的 OpenGL 集成功能,使得在 Qt 应用中实现 3D 图形变得更加简单。以下是使用 Qt 进行 OpenGL 3D 编程的基础知识。

1. 环境配置

创建 Qt 项目

  1. 新建 Qt Widgets Application 项目

  2. 在 .pro 文件中添加 OpenGL 模块:

qmake

QT       += core gui opengl

基本 OpenGL 窗口类

Qt 提供了 QOpenGLWidget 作为 OpenGL 渲染的基础组件。

openglwidget.h

#ifndef OPENGLWIDGET_H
#define OPENGLWIDGET_H#include <QOpenGLWidget>
#include <QOpenGLFunctions_3_3_Core>
#include <QOpenGLVertexArrayObject>
#include <QOpenGLBuffer>
#include <QOpenGLShaderProgram>
#include <QOpenGLTexture>
#include <QTime>class OpenGLWidget : public QOpenGLWidget, protected QOpenGLFunctions_3_3_Core
{Q_OBJECT
public:explicit OpenGLWidget(QWidget *parent = nullptr);~OpenGLWidget();protected:void initializeGL() override;void resizeGL(int w, int h) override;void paintGL() override;void mousePressEvent(QMouseEvent *event) override;void mouseMoveEvent(QMouseEvent *event) override;void wheelEvent(QWheelEvent *event) override;private:QOpenGLVertexArrayObject vao;QOpenGLBuffer vbo{QOpenGLBuffer::VertexBuffer};QOpenGLBuffer ebo{QOpenGLBuffer::IndexBuffer};QOpenGLShaderProgram shaderProgram;QOpenGLTexture *texture;QTime time;QPoint lastMousePos;float xRot = 0.0f;float yRot = 0.0f;float zRot = 0.0f;float zoom = -5.0f;
};#endif // OPENGLWIDGET_H

2. 基本框架实现

openglwidget.cpp

#include "openglwidget.h"
#include <QDebug>
#include <QMouseEvent>
#include <QImage>OpenGLWidget::OpenGLWidget(QWidget *parent) : QOpenGLWidget(parent)
{setFocusPolicy(Qt::StrongFocus);setMouseTracking(true);time.start();
}OpenGLWidget::~OpenGLWidget()
{makeCurrent();vao.destroy();vbo.destroy();ebo.destroy();delete texture;doneCurrent();
}void OpenGLWidget::initializeGL()
{initializeOpenGLFunctions();glClearColor(0.2f, 0.3f, 0.3f, 1.0f);glEnable(GL_DEPTH_TEST);// 立方体顶点数据 (位置 + 颜色 + 纹理坐标)float scale = 2.0f;float vertices[] = {// 前面-0.5f*scale, -0.5f*scale,  0.5f*scale,  1.0f, 0.0f, 0.0f, 1.0f,  0.0f, 0.0f,0.5f*scale, -0.5f*scale,  0.5f*scale,  0.0f, 1.0f, 0.0f, 1.0f,  1.0f, 0.0f,0.5f*scale,  0.5f*scale,  0.5f*scale,  0.0f, 0.0f, 1.0f, 1.0f,  1.0f, 1.0f,-0.5f*scale,  0.5f*scale,  0.5f*scale,  1.0f, 1.0f, 0.0f, 1.0f,  0.0f, 1.0f,// 后面-0.5f*scale, -0.5f*scale, -0.5f*scale,  1.0f, 0.0f, 1.0f, 1.0f,  1.0f, 0.0f,0.5f*scale, -0.5f*scale, -0.5f*scale,  0.0f, 1.0f, 1.0f, 1.0f,  0.0f, 0.0f,0.5f*scale,  0.5f*scale, -0.5f*scale,  1.0f, 1.0f, 1.0f, 1.0f,  0.0f, 1.0f,-0.5f*scale,  0.5f*scale, -0.5f*scale,  0.5f, 0.5f, 0.5f, 1.0f,  1.0f, 1.0f,// 左面-0.5f*scale, -0.5f*scale, -0.5f*scale,  0.6f, 0.0f, 0.0f, 1.0f,  0.0f, 0.0f,-0.5f*scale, -0.5f*scale,  0.5f*scale,  0.6f, 0.0f, 0.0f, 1.0f,  1.0f, 0.0f,-0.5f*scale,  0.5f*scale,  0.5f*scale,  0.6f, 0.0f, 0.0f, 1.0f,  1.0f, 1.0f,-0.5f*scale,  0.5f*scale, -0.5f*scale,  0.6f, 0.0f, 0.0f, 1.0f,  0.0f, 1.0f,// 右面0.5f*scale, -0.5f*scale, -0.5f*scale,  1.0f, 0.2f, 0.2f, 1.0f,  1.0f, 0.0f,0.5f*scale, -0.5f*scale,  0.5f*scale,  1.0f, 0.2f, 0.2f, 1.0f,  0.0f, 0.0f,0.5f*scale,  0.5f*scale,  0.5f*scale,  1.0f, 0.2f, 0.2f, 1.0f,  0.0f, 1.0f,0.5f*scale,  0.5f*scale, -0.5f*scale,  1.0f, 0.2f, 0.2f, 1.0f,  1.0f, 1.0f,// 顶面-0.5f*scale,  0.5f*scale,  0.5f*scale,  1.0f, 0.1f, 0.1f, 1.0f,  0.0f, 0.0f,0.5f*scale,  0.5f*scale,  0.5f*scale,  1.0f, 0.1f, 0.1f, 1.0f,  1.0f, 0.0f,0.5f*scale,  0.5f*scale, -0.5f*scale,  1.0f, 0.1f, 0.1f, 1.0f,  1.0f, 1.0f,-0.5f*scale,  0.5f*scale, -0.5f*scale,  1.0f, 0.1f, 0.1f, 1.0f,  0.0f, 1.0f,// 底面-0.5f*scale, -0.5f*scale,  0.5f*scale,  0.5f, 0.0f, 0.0f, 1.0f,  0.0f, 0.0f,0.5f*scale, -0.5f*scale,  0.5f*scale,  0.5f, 0.0f, 0.0f, 1.0f,  1.0f, 0.0f,0.5f*scale, -0.5f*scale, -0.5f*scale,  0.5f, 0.0f, 0.0f, 1.0f,  1.0f, 1.0f,-0.5f*scale, -0.5f*scale, -0.5f*scale,  0.5f, 0.0f, 0.0f, 1.0f,  0.0f, 1.0f};// 索引数据保持不变unsigned int indices[] = {0, 1, 2, 2, 3, 0,4, 5, 6, 6, 7, 4,8, 9, 10, 10, 11, 8,12, 13, 14, 14, 15, 12,16, 17, 18, 18, 19, 16,20, 21, 22, 22, 23, 20};// 初始化 VAO, VBO 和 EBOvao.create();vbo.create();ebo.create();vao.bind();vbo.bind();vbo.allocate(vertices, sizeof(vertices));ebo.bind();ebo.allocate(indices, sizeof(indices));// 顶点着色器const char *vertexShaderSource = R"(#version 330 corelayout(location = 0) in vec3 aPos;layout(location = 1) in vec4 aColor;layout(location = 2) in vec2 aTexCoord;out vec4 ourColor;out vec2 TexCoord;uniform mat4 model;uniform mat4 view;uniform mat4 projection;void main(){gl_Position = projection * view * model * vec4(aPos, 1.0);ourColor = aColor;TexCoord = aTexCoord;})";// 片段着色器const char *fragmentShaderSource = R"(#version 330 corein vec4 ourColor;in vec2 TexCoord;out vec4 FragColor;uniform sampler2D texture1;void main(){vec4 texColor = texture(texture1, TexCoord);FragColor = texColor * ourColor;})";// 编译着色器shaderProgram.addShaderFromSourceCode(QOpenGLShader::Vertex, vertexShaderSource);shaderProgram.addShaderFromSourceCode(QOpenGLShader::Fragment, fragmentShaderSource);if (!shaderProgram.link()) {qDebug() << "Shader Program Link Error:" << shaderProgram.log();return;}shaderProgram.bind();// 设置顶点属性指针shaderProgram.setAttributeBuffer(0, GL_FLOAT, 0, 3, 9 * sizeof(float));shaderProgram.enableAttributeArray(0);shaderProgram.setAttributeBuffer(1, GL_FLOAT, 3 * sizeof(float), 4, 9 * sizeof(float));shaderProgram.enableAttributeArray(1);shaderProgram.setAttributeBuffer(2, GL_FLOAT, 7 * sizeof(float), 2, 9 * sizeof(float));shaderProgram.enableAttributeArray(2);// 加载纹理QImage img(":/textures/test.jpeg");if (img.isNull()) {qDebug() << "Failed to load texture image!";img = QImage(2, 2, QImage::Format_RGB32);img.fill(Qt::red);}texture = new QOpenGLTexture(img.mirrored());texture->setMinificationFilter(QOpenGLTexture::Linear);texture->setMagnificationFilter(QOpenGLTexture::Linear);texture->setWrapMode(QOpenGLTexture::Repeat);shaderProgram.setUniformValue("texture1", 0);vao.release();shaderProgram.release();
}void OpenGLWidget::resizeGL(int w, int h)
{glViewport(0, 0, w, h);
}void OpenGLWidget::paintGL()
{glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);shaderProgram.bind();vao.bind();texture->bind(0);QMatrix4x4 model;QMatrix4x4 view;QMatrix4x4 projection;model.rotate(xRot, 1.0f, 0.0f, 0.0f);model.rotate(yRot, 0.0f, 1.0f, 0.0f);model.rotate(zRot, 0.0f, 0.0f, 1.0f);view.translate(0.0f, 0.0f, zoom);projection.perspective(45.0f, width() / float(height()), 0.1f, 100.0f);shaderProgram.setUniformValue("model", model);shaderProgram.setUniformValue("view", view);shaderProgram.setUniformValue("projection", projection);glDrawElements(GL_TRIANGLES, 36, GL_UNSIGNED_INT, 0);texture->release();vao.release();shaderProgram.release();zRot += 0.5f;if (zRot > 360.0f) zRot -= 360.0f;//update();
}void OpenGLWidget::mousePressEvent(QMouseEvent *event)
{lastMousePos = event->pos();
}void OpenGLWidget::mouseMoveEvent(QMouseEvent *event)
{if (event->buttons() & Qt::LeftButton) {float dx = event->pos().x() - lastMousePos.x();float dy = event->pos().y() - lastMousePos.y();xRot += dy;yRot += dx;lastMousePos = event->pos();update();}
}void OpenGLWidget::wheelEvent(QWheelEvent *event)
{float delta = event->angleDelta().y() / 120.0f;zoom += delta * 0.5f;zoom = qBound(-10.0f, zoom, -1.0f);update();
}

3. 渲染 3D 立方体

准备顶点数据

  // 立方体顶点数据 (位置 + 颜色 + 纹理坐标)float scale = 2.0f;float vertices[] = {// 前面-0.5f*scale, -0.5f*scale,  0.5f*scale,  1.0f, 0.0f, 0.0f, 1.0f,  0.0f, 0.0f,0.5f*scale, -0.5f*scale,  0.5f*scale,  0.0f, 1.0f, 0.0f, 1.0f,  1.0f, 0.0f,0.5f*scale,  0.5f*scale,  0.5f*scale,  0.0f, 0.0f, 1.0f, 1.0f,  1.0f, 1.0f,-0.5f*scale,  0.5f*scale,  0.5f*scale,  1.0f, 1.0f, 0.0f, 1.0f,  0.0f, 1.0f,// 后面-0.5f*scale, -0.5f*scale, -0.5f*scale,  1.0f, 0.0f, 1.0f, 1.0f,  1.0f, 0.0f,0.5f*scale, -0.5f*scale, -0.5f*scale,  0.0f, 1.0f, 1.0f, 1.0f,  0.0f, 0.0f,0.5f*scale,  0.5f*scale, -0.5f*scale,  1.0f, 1.0f, 1.0f, 1.0f,  0.0f, 1.0f,-0.5f*scale,  0.5f*scale, -0.5f*scale,  0.5f, 0.5f, 0.5f, 1.0f,  1.0f, 1.0f,// 左面-0.5f*scale, -0.5f*scale, -0.5f*scale,  0.6f, 0.0f, 0.0f, 1.0f,  0.0f, 0.0f,-0.5f*scale, -0.5f*scale,  0.5f*scale,  0.6f, 0.0f, 0.0f, 1.0f,  1.0f, 0.0f,-0.5f*scale,  0.5f*scale,  0.5f*scale,  0.6f, 0.0f, 0.0f, 1.0f,  1.0f, 1.0f,-0.5f*scale,  0.5f*scale, -0.5f*scale,  0.6f, 0.0f, 0.0f, 1.0f,  0.0f, 1.0f,// 右面0.5f*scale, -0.5f*scale, -0.5f*scale,  1.0f, 0.2f, 0.2f, 1.0f,  1.0f, 0.0f,0.5f*scale, -0.5f*scale,  0.5f*scale,  1.0f, 0.2f, 0.2f, 1.0f,  0.0f, 0.0f,0.5f*scale,  0.5f*scale,  0.5f*scale,  1.0f, 0.2f, 0.2f, 1.0f,  0.0f, 1.0f,0.5f*scale,  0.5f*scale, -0.5f*scale,  1.0f, 0.2f, 0.2f, 1.0f,  1.0f, 1.0f,// 顶面-0.5f*scale,  0.5f*scale,  0.5f*scale,  1.0f, 0.1f, 0.1f, 1.0f,  0.0f, 0.0f,0.5f*scale,  0.5f*scale,  0.5f*scale,  1.0f, 0.1f, 0.1f, 1.0f,  1.0f, 0.0f,0.5f*scale,  0.5f*scale, -0.5f*scale,  1.0f, 0.1f, 0.1f, 1.0f,  1.0f, 1.0f,-0.5f*scale,  0.5f*scale, -0.5f*scale,  1.0f, 0.1f, 0.1f, 1.0f,  0.0f, 1.0f,// 底面-0.5f*scale, -0.5f*scale,  0.5f*scale,  0.5f, 0.0f, 0.0f, 1.0f,  0.0f, 0.0f,0.5f*scale, -0.5f*scale,  0.5f*scale,  0.5f, 0.0f, 0.0f, 1.0f,  1.0f, 0.0f,0.5f*scale, -0.5f*scale, -0.5f*scale,  0.5f, 0.0f, 0.0f, 1.0f,  1.0f, 1.0f,-0.5f*scale, -0.5f*scale, -0.5f*scale,  0.5f, 0.0f, 0.0f, 1.0f,  0.0f, 1.0f};// 索引数据保持不变unsigned int indices[] = {0, 1, 2, 2, 3, 0,4, 5, 6, 6, 7, 4,8, 9, 10, 10, 11, 8,12, 13, 14, 14, 15, 12,16, 17, 18, 18, 19, 16,20, 21, 22, 22, 23, 20};

初始化 VAO, VBO 和 EBO

    // 初始化 VAO, VBO 和 EBOvao.create();vbo.create();ebo.create();vao.bind();vbo.bind();vbo.allocate(vertices, sizeof(vertices));ebo.bind();ebo.allocate(indices, sizeof(indices));

创建着色器程序

 // 顶点着色器const char *vertexShaderSource = R"(#version 330 corelayout(location = 0) in vec3 aPos;layout(location = 1) in vec4 aColor;layout(location = 2) in vec2 aTexCoord;out vec4 ourColor;out vec2 TexCoord;uniform mat4 model;uniform mat4 view;uniform mat4 projection;void main(){gl_Position = projection * view * model * vec4(aPos, 1.0);ourColor = aColor;TexCoord = aTexCoord;})";// 编译着色器shaderProgram.addShaderFromSourceCode(QOpenGLShader::Vertex, vertexShaderSource);
 // 片段着色器const char *fragmentShaderSource = R"(#version 330 corein vec4 ourColor;in vec2 TexCoord;out vec4 FragColor;uniform sampler2D texture1;void main(){vec4 texColor = texture(texture1, TexCoord);FragColor = texColor * ourColor;})";shaderProgram.addShaderFromSourceCode(QOpenGLShader::Fragment, fragmentShaderSource);

4. 实现 3D 渲染

void OpenGLWidget::paintGL()
{glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);shaderProgram.bind();vao.bind();texture->bind(0);QMatrix4x4 model;QMatrix4x4 view;QMatrix4x4 projection;model.rotate(xRot, 1.0f, 0.0f, 0.0f);model.rotate(yRot, 0.0f, 1.0f, 0.0f);model.rotate(zRot, 0.0f, 0.0f, 1.0f);view.translate(0.0f, 0.0f, zoom);projection.perspective(45.0f, width() / float(height()), 0.1f, 100.0f);shaderProgram.setUniformValue("model", model);shaderProgram.setUniformValue("view", view);shaderProgram.setUniformValue("projection", projection);glDrawElements(GL_TRIANGLES, 36, GL_UNSIGNED_INT, 0);texture->release();vao.release();shaderProgram.release();zRot += 0.5f;if (zRot > 360.0f) zRot -= 360.0f;
}

5. 添加纹理

加载纹理

 // 加载纹理QImage img(":/textures/test.jpeg");if (img.isNull()) {qDebug() << "Failed to load texture image!";img = QImage(2, 2, QImage::Format_RGB32);img.fill(Qt::red);}texture = new QOpenGLTexture(img.mirrored());texture->setMinificationFilter(QOpenGLTexture::Linear);texture->setMagnificationFilter(QOpenGLTexture::Linear);texture->setWrapMode(QOpenGLTexture::Repeat);shaderProgram.setUniformValue("texture1", 0);

更新顶点属性指针

    // 设置顶点属性指针shaderProgram.setAttributeBuffer(0, GL_FLOAT, 0, 3, 9 * sizeof(float));shaderProgram.enableAttributeArray(0);shaderProgram.setAttributeBuffer(1, GL_FLOAT, 3 * sizeof(float), 4, 9 * sizeof(float));shaderProgram.enableAttributeArray(1);shaderProgram.setAttributeBuffer(2, GL_FLOAT, 7 * sizeof(float), 2, 9 * sizeof(float));shaderProgram.enableAttributeArray(2);

完整工程代码:

https://gitee.com/byxdaz/opengl3d-instance

运行效果图:


文章转载自:

http://RyJt3ZTE.qjngk.cn
http://0TfXpWdQ.qjngk.cn
http://Rcr2fMqv.qjngk.cn
http://0zOqvcG4.qjngk.cn
http://bHIJrXbo.qjngk.cn
http://aruR1uqy.qjngk.cn
http://DXB3RLhc.qjngk.cn
http://ogsu9qRq.qjngk.cn
http://bzjb9VlP.qjngk.cn
http://rL7P0i4W.qjngk.cn
http://q9j33nVT.qjngk.cn
http://P2Op1nO3.qjngk.cn
http://qTlrGLAD.qjngk.cn
http://VpreX5rC.qjngk.cn
http://lCAeXRqE.qjngk.cn
http://nNTN6WKv.qjngk.cn
http://NBv4LEuD.qjngk.cn
http://uQCEXNVP.qjngk.cn
http://Zo9SEAgj.qjngk.cn
http://0Bp6RqxA.qjngk.cn
http://vFxcfZzu.qjngk.cn
http://lu8Ul5Qs.qjngk.cn
http://hehoJLla.qjngk.cn
http://AHUb6XH0.qjngk.cn
http://GDkfe2va.qjngk.cn
http://77izQ5mJ.qjngk.cn
http://ICqBFfJH.qjngk.cn
http://bajzs63L.qjngk.cn
http://4OhrvlFc.qjngk.cn
http://lcmJeyLM.qjngk.cn
http://www.dtcms.com/wzjs/766664.html

相关文章:

  • 济南比较好的网站建设公司做网站最快的编程语言
  • 英语机构网站建设方案做网站工作都包括什么
  • 高端 网站建设三网合一 网站
  • 广东门户网站建设无人在线观看高清视频 单曲
  • 公司网站表达的内容怎么制作个人网页教程
  • 开一个网站建设公司需要什么怎么用html做百度首页网站
  • 成都网站制作成都商标注册查询官网入口官网
  • 中国空间站完成图wordpress字体库
  • 做传销网站的域名跟网站的区别吗
  • 网站设置始终请求电脑版海南省建设考试网站
  • 深圳公关公司首荐乐云seo如何设定旅游网站seo核心关键词
  • 洛阳做网站的公司哪家好公司宣传页的样板
  • 工业设计就业咋样东莞百度搜索优化
  • 河北建设广州分公司网站1688做网站难吗
  • wordpress视频站主题什么叫营销型网站建设
  • 百度怎么优化网站关键词网站建设 厦门
  • 手机做兼职的网站百度上能收到的企业名称网站怎么做
  • 深圳网站设计公司在什么地方如何做一个免费网页
  • wordpress主题justnews广州seo代理商
  • 安徽优化网站wordpress news
  • 网站seo方案设计企业解决方案展示平台
  • 兰州网站制作怎么样网站平台怎么建立
  • 电子商务企业网站建设发展论文百度指数可以查询到哪些内容
  • 学校 网站源码网站怎么做h5支付
  • wordpress 新变量天津关键词优化平台
  • 做二手网站有哪些问题坪山网站建设流程
  • 微网站推广云开发app
  • 开发电子商务网站的主流语言传媒公司是干什么的
  • 河南建设工程信息网站贵阳市网站建设公司
  • 做网站商城需要多少钱网站的交流的功能怎么做