#ifndef TEXTURE2D_H #define TEXTURE2D_H class texture2d : protected
QOpenGLFunctions { public: texture2d(); void paint(const QMatrix4x4 &view); void
setImage(const QRect& rect, const QImage &image); void updata(); private: void
initTxture(); std::unique_ptr<QOpenGLShaderProgram> m_program;
QOpenGLVertexArrayObject m_vao; // 顶点数组对象 QOpenGLBuffer m_vbo; std::unique_ptr<
QOpenGLBuffer> m_veo; //顶点缓存对象/ unsigned m_texture; std::vector<float> m_data;
std::vector<float> m_renderData; QImage m_image; QRect m_rect; }; #endif //
TEXTURE2D_H #include "texture2d.h" #include <core/logcxx/easylogging++.h>
texture2d::texture2d() { initializeOpenGLFunctions(); m_program.reset(new
QOpenGLShaderProgram()); m_program->addCacheableShaderFromSourceFile(
QOpenGLShader::Vertex, ":/shader/texture2d.vert"); m_program->
addCacheableShaderFromSourceFile(QOpenGLShader::Fragment,
":/shader/texture2d.frag"); m_program->link(); m_data = { 200.f, 100.f, 1.f, 1.f
, 1.f, //右下 200.f, 0.f, 1.f, 1.f, 0.f, //右上 100.f, 0.f, 1.f, 0.f, 0.f, //左上
100.f, 100.f, 1.f, 0.f, 1.f, //左下 }; static unsigned int indices[] = { 0, 1, 3,
// first triangle 1, 2, 3 // second triangle }; if(!m_vao.isCreated()){ m_vao.
create(); } if(!m_vbo.isCreated()){ m_vbo.create(); } m_veo.reset(new
QOpenGLBuffer(QOpenGLBuffer::IndexBuffer)); if(!m_veo->isCreated()){ m_veo->
create(); } if(!m_program->isLinked()){ m_program->link(); } m_vao.bind(); m_vbo
.bind(); m_veo->bind(); m_vbo.allocate(m_data.data(), m_data.size()*sizeof(float
)); m_veo->allocate(indices, 6 * sizeof(int)); m_program->bind(); m_program->
setAttributeBuffer("aPos", GL_FLOAT, 0 * sizeof(float), 3, 5 * sizeof(float));
m_program->enableAttributeArray("aPos"); m_program->setAttributeBuffer(
"aTexCoord", GL_FLOAT, 3 * sizeof(float), 2, 5 * sizeof(float)); m_program->
enableAttributeArray("aTexCoord");
//glUniform1i(glGetUniformLocation(m_program->programId(), "ourTexture"), 0);
m_program->setUniformValue("ourTexture", 0); m_program->release(); m_veo.release
(); m_vbo.release(); m_vao.release(); initTxture(); } void texture2d::paint(
const QMatrix4x4 &view) { glDisable(GL_DEPTH_TEST); //glEnable(GL_TEXTURE_2D);
m_program->bind(); m_vao.bind(); QMatrix4x4 model; model.setToIdentity();
m_program->setUniformValue("view", view); m_program->setUniformValue("model",
model); glActiveTexture(GL_TEXTURE0); // 在绑定纹理之前先激活纹理单元 glBindTexture(
GL_TEXTURE_2D, m_texture); glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0);
//glDrawArrays(GL_POLYGON, 0, 4); m_vao.release(); m_program->release();
//glDisable(GL_TEXTURE_2D); } void texture2d::setImage(const QRect& rect, const
QImage& image) { m_rect = rect; m_image = image; updata(); } void texture2d::
updata() { m_data[0] = m_rect.bottomRight().x(); m_data[1] = m_rect.bottomRight(
).y(); m_data[5] = m_rect.topRight().x(); m_data[6] = m_rect.topRight().y();
m_data[10] = m_rect.topLeft().x(); m_data[11] = m_rect.topLeft().y(); m_data[15]
= m_rect.bottomLeft().x(); m_data[16] = m_rect.bottomLeft().y(); m_vbo.bind();
m_vbo.allocate(m_data.data(), m_data.size()*sizeof(float)); m_vbo.release();
m_image.convertTo(QImage::Format::Format_RGB888); glBindTexture(GL_TEXTURE_2D,
m_texture); glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, m_image.width(), m_image.
height(), 0, GL_RGB, GL_UNSIGNED_BYTE, m_image.bits());
//glGenerateMipmap(GL_TEXTURE_2D); } void texture2d::initTxture() {
glGenTextures(1, &m_texture); glBindTexture(GL_TEXTURE_2D, m_texture); // set
the texture wrapping parameters glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S
, GL_REPEAT); // set texture wrapping to GL_REPEAT (default wrapping method)
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT); // set texture
filtering parameters glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER,
GL_LINEAR); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); }

技术
今日推荐
PPT
阅读数 135
下载桌面版
GitHub
百度网盘(提取码:draw)
Gitee
云服务器优惠
阿里云优惠券
腾讯云优惠券
华为云优惠券
站点信息
问题反馈
邮箱:ixiaoyang8@qq.com
QQ群:766591547
关注微信