QT-地形3D

QT-地形3D

一、 演示效果


二、关键程序

c 复制代码
#include "ShaderProgram.h"

namespace t3d::core
{

void ShaderProgram::init()
{
    initializeOpenGLFunctions();
    loadShaders();
}

void ShaderProgram::addShader(const QString &filename, QOpenGLShader::ShaderType shaderType)
{
    QOpenGLShader *shader = new QOpenGLShader(shaderType, mProgram.get());
    if (!shader->compileSourceFile(gDefaultPathShaders + filename))
        qDebug() << "Error compiling shader " << filename << " of type "
                 << static_cast<int>(shaderType);

    if (!mProgram->addShader(shader))
        qDebug() << "Error adding shader " << filename << " of type "
                 << static_cast<int>(shaderType);
}

void ShaderProgram::loadShaders()
{
    mProgram = makeUnique<QOpenGLShaderProgram>();
    addShaders();

    if (mProgram->link() == false)
        qFatal("Problem linking shaders");
    else
        qDebug() << "Initialized shaders";

    mProgram->bind();
    queryUniformLocations();
    refreshUniformValues();
    flushQueuedUniformValueChanges();
    mProgram->release();
}

bool ShaderProgram::bind()
{
    if (!mProgram->bind())
    {
        System::warn("Unable to bind OpenGL ShaderProgram");
        return false;
    }

    flushQueuedUniformValueChanges();

    return true;
}

void ShaderProgram::reloadShaders()
{
    loadShaders();
}

void ShaderProgram::enqueueUniformValueChange(const GLint *uniformLocation, QVariant value)
{
    if (mProgram && mProgram->isLinked())
    {
        mProgram->bind();
        setUniformFromQVariant(*uniformLocation, value);
        mProgram->release(); // TODO this can cause issues if used while the program was bound
                             // externally
    }
    else
        mQueuedUniformValueChanges.append(QPair<const GLint *, QVariant>(uniformLocation, value));
}

void ShaderProgram::setUniformFromQVariant(GLint location, QVariant &value)
{
    switch (static_cast<QMetaType::Type>(value.type()))
    {
    case QMetaType::Int:
        mProgram->setUniformValue(location, static_cast<GLint>(value.toInt()));
        break;
    case QMetaType::Double:
        mProgram->setUniformValue(location, static_cast<GLfloat>(value.toDouble()));
        break;
    case QMetaType::Float:
        mProgram->setUniformValue(location, static_cast<GLfloat>(value.toFloat()));
        break;
    default:
        qFatal("Trying to set a queued shader uniform value for an unknown type");
        // TODO this is all we support. Eventuall QOpenGLShaderProgram should support
        // setUniformValue directly from a QVariant.
    }
}

void ShaderProgram::flushQueuedUniformValueChanges()
{
    for (auto pair : mQueuedUniformValueChanges)
    {
        setUniformFromQVariant(*pair.first, pair.second);
    }

    mQueuedUniformValueChanges.clear();
}

}

三、下载链接

https://download.csdn.net/download/u013083044/88850945

相关推荐
在下不上天14 分钟前
Flume日志采集系统的部署,实现flume负载均衡,flume故障恢复
大数据·开发语言·python
陌小呆^O^27 分钟前
Cmakelist.txt之win-c-udp-client
c语言·开发语言·udp
I_Am_Me_43 分钟前
【JavaEE进阶】 JavaScript
开发语言·javascript·ecmascript
重生之我是数学王子1 小时前
QT基础 编码问题 定时器 事件 绘图事件 keyPressEvent QT5.12.3环境 C++实现
开发语言·c++·qt
Ai 编码助手1 小时前
使用php和Xunsearch提升音乐网站的歌曲搜索效果
开发语言·php
学习前端的小z1 小时前
【前端】深入理解 JavaScript 逻辑运算符的优先级与短路求值机制
开发语言·前端·javascript
神仙别闹1 小时前
基于C#和Sql Server 2008实现的(WinForm)订单生成系统
开发语言·c#
XINGTECODE1 小时前
海盗王集成网关和商城服务端功能golang版
开发语言·后端·golang
zwjapple1 小时前
typescript里面正则的使用
开发语言·javascript·正则表达式
小五Five1 小时前
TypeScript项目中Axios的封装
开发语言·前端·javascript