• 大小: 54KB
    文件类型: .rar
    金币: 2
    下载: 1 次
    发布日期: 2021-06-16
  • 语言: 其他
  • 标签: qt  opengl  es    纹理  

资源简介

看书过后,用qt实现的一个把2维纹理贴到球面的工程,其中还包含混合光,采用opengl es 3.0;初学者可以学习下

资源截图

代码片段和文件信息

#include “balltexturerender.h“

BallTextureRender::~BallTextureRender()
{
    if(texture_){
        texture_->destroy();
        delete texture_;
    }
}

void BallTextureRender::initsize(float r QImage &img)
{
    program_.addCacheableShaderFromSourceFile(QOpenGLShader::Vertex“vsrc.vert“);
    program_.addCacheableShaderFromSourceFile(QOpenGLShader::Fragment“fsrc.frag“);
    program_.link();

    r_ = r;
    int angleSpan = 10; //弧度 = 角度 * PI / 180
    for(int vAngle = -90; vAngle < 90; vAngle = vAngle + angleSpan){ //生成球面顶点
        for(int hAngle = 0; hAngle <= 360; hAngle = hAngle + angleSpan){
            float x0 = r * ::cos(vAngle * PI / 180) * ::cos(hAngle * PI / 180);
            float y0 = r * ::cos(vAngle * PI / 180) * ::sin(hAngle * PI / 180);
            float z0 = r * ::sin(vAngle * PI / 180);

            float x1 = r * ::cos(vAngle * PI / 180) * ::cos((hAngle + angleSpan) * PI / 180);
            float y1 = r * ::cos(vAngle * PI / 180) * ::sin((hAngle + angleSpan) * PI / 180);
            float z1 = r * ::sin(vAngle * PI / 180);

            float x2 = r * ::cos((vAngle + angleSpan) * PI / 180) * ::cos((hAngle + angleSpan) * PI / 180);
            float y2 = r * ::cos((vAngle + angleSpan) * PI / 180) * ::sin((hAngle + angleSpan) * PI / 180);
            float z2 = r * ::sin((vAngle + angleSpan) * PI / 180);

            float x3 = r * ::cos((vAngle + angleSpan) * PI / 180) * ::cos(hAngle * PI / 180);
            float y3 = r * ::cos((vAngle + angleSpan) * PI / 180) * ::sin(hAngle * PI / 180);
            float z3 = r * ::sin((vAngle + angleSpan) * PI / 180);

            points_ << x1 << y1 << z1 << x3 << y3 << z3
                   << x0 << y0 << z0 << x1 << y1 << z1
                   << x2 << y2 << z2 << x3 << y3 << z3;
        }
    }

    texture_ = new QOpenGLTexture(img);
    vbo_.create();
    vbo_.bind();
    vbo_.allocate(points_.constData()points_.count() * sizeof GLfloat);
}

void BallTextureRender::render(QOpenGLExtraFunctions *f QMatrix4x4 &pMatrix QMatrix4x4 &vMatrix QMatrix4x4 &mMatrix QVector3D &lightLocation QVector3D &camera)
{
    f->glEnable(GL_DEPTH_TEST);
    f->glEnable(GL_CULL_FACE);
    program_.bind();
    vbo_.bind();
    f->glActiveTexture(GL_TEXTURE0 + 0);
    program_.setUniformValue(“uPMatrix“pMatrix);
    program_.setUniformValue(“uVMatrix“vMatrix);
    program_.setUniformValue(“uMMatrix“mMatrix);
    program_.setUniformValue(“uLightLocation“lightLocation);
    program_.setUniformValue(“uCamera“camera);
    program_.setUniformValue(“uR“r_);
    program_.setUniformValue(“sTexture“0);
    program_.enableAttributeArray(0);
    program_.enableAttributeArray(1);

    texture_->bind(0);
    program_.setAttributeBuffer(0GL_FLOAT030);
    program_.setAttributeBuffer(1GL_FLOAT030);
    f->glDrawArrays(GL_TRIANGLES0points_.count() / 3);

    program_.disableAttributeArray(0);
    program_.disableAttributeArray(1);

 属性            大小     日期    时间   名称
----------- ---------  ---------- -----  ----

     文件          7  2018-10-01 10:00  balltexture\.git\COMMIT_EDITMSG

     文件        316  2018-10-01 09:30  balltexture\.git\config

     文件         73  2018-10-01 09:30  balltexture\.git\description

     文件         23  2018-10-01 09:30  balltexture\.git\HEAD

     文件        478  2018-10-01 09:30  balltexture\.git\hooks\applypatch-msg.sample

     文件        896  2018-10-01 09:30  balltexture\.git\hooks\commit-msg.sample

     文件       3327  2018-10-01 09:30  balltexture\.git\hooks\fsmonitor-watchman.sample

     文件        189  2018-10-01 09:30  balltexture\.git\hooks\post-update.sample

     文件        424  2018-10-01 09:30  balltexture\.git\hooks\pre-applypatch.sample

     文件       1638  2018-10-01 09:30  balltexture\.git\hooks\pre-commit.sample

     文件       1348  2018-10-01 09:30  balltexture\.git\hooks\pre-push.sample

     文件       4898  2018-10-01 09:30  balltexture\.git\hooks\pre-rebase.sample

     文件        544  2018-10-01 09:30  balltexture\.git\hooks\pre-receive.sample

     文件       1492  2018-10-01 09:30  balltexture\.git\hooks\prepare-commit-msg.sample

     文件       3610  2018-10-01 09:30  balltexture\.git\hooks\update.sample

     文件        689  2018-10-01 10:00  balltexture\.git\index

     文件        240  2018-10-01 09:30  balltexture\.git\info\exclude

     文件        516  2018-10-01 10:00  balltexture\.git\logs\HEAD

     文件        516  2018-10-01 10:00  balltexture\.git\logs\refs\heads\master

     文件        206  2018-10-01 09:30  balltexture\.git\logs\refs\remotes\origin\HEAD

     文件        310  2018-10-01 10:00  balltexture\.git\logs\refs\remotes\origin\master

     文件        642  2018-10-01 09:30  balltexture\.git\objects\03\b0cb84d616b1d6922ff2414173c56b93ac5e77

     文件        159  2018-10-01 10:00  balltexture\.git\objects\10\9a702f1a9bc1eedd51d055814a7c6994410194

     文件        583  2018-10-01 09:30  balltexture\.git\objects\15\7200734bea87909efceb55e894654e55ca04bf

     文件        979  2018-10-01 09:30  balltexture\.git\objects\19\d130a93302fc0450e7af11f4c6ccd65f3aba6d

     文件        320  2018-10-01 09:30  balltexture\.git\objects\39\60aac3ea6b150f1314cbe57574169ca4edd345

     文件        356  2018-10-01 09:30  balltexture\.git\objects\3d\5f5bd2d0cc3d0a8a668b46d5344fa2ee9105b4

     文件        271  2018-10-01 10:00  balltexture\.git\objects\5e\4303dea9402d850e76f45c26c82954e0fc959b

     文件        188  2018-10-01 09:30  balltexture\.git\objects\8a\d088890da3fdabb2929a108c45f63bbbf6b5e0

     文件        136  2018-10-01 09:30  balltexture\.git\objects\90\b6d53fb950c511da62d02bc34fe46d660069f5

............此处省略56个文件信息

评论

共有 条评论