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

相关推荐
沐知全栈开发6 分钟前
Servlet 点击计数器
开发语言
m0Java门徒10 分钟前
Java 递归全解析:从原理到优化的实战指南
java·开发语言
桃子酱紫君1 小时前
华为配置篇-BGP实验
开发语言·华为·php
QTX187301 小时前
JavaScript 中的原型链与继承
开发语言·javascript·原型模式
shaoing1 小时前
MySQL 错误 报错:Table ‘performance_schema.session_variables’ Doesn’t Exist
java·开发语言·数据库
The Future is mine2 小时前
Python计算经纬度两点之间距离
开发语言·python
Enti7c2 小时前
HTML5和CSS3的一些特性
开发语言·css3
AAA废品回收站陈师傅2 小时前
19信号和槽_信号和槽的基本概念
qt
爱吃巧克力的程序媛2 小时前
在 Qt 创建项目时,Qt Quick Application (Compat) 和 Qt Quick Application
开发语言·qt
独好紫罗兰3 小时前
洛谷题单3-P5719 【深基4.例3】分类平均-python-流程图重构
开发语言·python·算法