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

相关推荐
binishuaio7 分钟前
Java 第11天 (git版本控制器基础用法)
java·开发语言·git
zz.YE9 分钟前
【Java SE】StringBuffer
java·开发语言
就是有点傻13 分钟前
WPF中的依赖属性
开发语言·wpf
洋24022 分钟前
C语言常用标准库函数
c语言·开发语言
进击的六角龙23 分钟前
Python中处理Excel的基本概念(如工作簿、工作表等)
开发语言·python·excel
wrx繁星点点25 分钟前
状态模式(State Pattern)详解
java·开发语言·ui·设计模式·状态模式
NoneCoder42 分钟前
Java企业级开发系列(1)
java·开发语言·spring·团队开发·开发
苏三有春42 分钟前
PyQt5实战——UTF-8编码器功能的实现(六)
开发语言·qt
Aniay_ivy1 小时前
深入探索 Java 8 Stream 流:高效操作与应用场景
java·开发语言·python
Vanranrr1 小时前
C++ QT
java·c++·qt