Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/amazon-web-services/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Qt QGLFramebufferObject是否可以大于视口?_Qt_Opengl_Qt5 - Fatal编程技术网

Qt QGLFramebufferObject是否可以大于视口?

Qt QGLFramebufferObject是否可以大于视口?,qt,opengl,qt5,Qt,Opengl,Qt5,QGLFramebufferObject是否可以大于视口?例如,如果我有一个300x300像素的视口,fbo可以是600x300吗。。。所以显示了fbo的一半…然后应用翻译,显示了另一半。第一次生成文本();称为缩放是正确的,第二次x维度被拉伸 main.h #include <QGLWidget> #include <QGLFunctions> #include <QGLFramebufferObject> #include <QFont> #i

QGLFramebufferObject是否可以大于视口?例如,如果我有一个300x300像素的视口,fbo可以是600x300吗。。。所以显示了fbo的一半…然后应用翻译,显示了另一半。第一次生成文本();称为缩放是正确的,第二次x维度被拉伸

main.h

#include <QGLWidget>
#include <QGLFunctions>
#include <QGLFramebufferObject>
#include <QFont>
#include <QGLShader>

class glview : public QGLWidget, protected QGLFunctions
{
    Q_OBJECT

public:
    explicit glview(QWidget *parent = 0);
    ~glview();
    QSize sizeHint() const;

protected:
    void initializeGL();
    void resizeGL(int w, int h);
    void paintGL();

private:
    void make_text(void);
    QGLFramebufferObject *fbo;
    QFont font;
    quint32 vbo_id[1], texture_id;
    QGLShaderProgram *txtovlp;
    QTimer *delay;

    private slots:
    void refresh(void);
};
#包括
#包括
#包括
#包括
#包括
类glview:公共QGLWidget,受保护的QGLFunctions
{
Q_对象
公众:
显式glview(QWidget*parent=0);
~glview();
QSize sizeHint()常量;
受保护的:
void initializeGL();
无效尺寸(整数w,整数h);
void paintGL();
私人:
作废制单文本(作废);
QGLFramebufferObject*fbo;
QFont字体;
quint32 vbo_id[1],纹理_id;
QGLShaderProgram*txtovlp;
QTimer*延迟;
专用插槽:
作废刷新(作废);
};
main.cpp

#include <QApplication>
#include <QPainter>
#include <QTimer>
#include "main.h"

struct txtr_vrtx {
    GLfloat   x;
    GLfloat   y;
    GLfloat   z;
    GLfloat   tx;
    GLfloat   ty;
}__attribute__((packed)) txtr_geo[] = {
    //   x, y, z, tx,ty
    {0, 0, 0, 0, 0},
    {0, 3, 0, 0, 1},
    {6, 3, 0, 1, 1},
    {6, 0, 0, 1, 0},
};

int main(int argc, char *argv[])
{
    QApplication app(argc, argv);
    glview widget;
    widget.show();
    return app.exec();
}

glview::glview(QWidget *parent) : QGLWidget(parent)
{
    fbo = NULL;
    font.setFamily("Helvetica");
    delay = new QTimer;
    delay->start(2000);
    connect(delay, SIGNAL(timeout()), this, SLOT(refresh()));
}

glview::~glview()
{
    delete fbo;
}

void glview::refresh(void)
{
    delay->stop();
    qDebug() << "refresh fired";
    make_text();
    updateGL();
}

void glview::make_text(void)
{
    glBindBuffer(GL_ARRAY_BUFFER, 0);       // must unbind for QPainter

    glEnable(GL_TEXTURE_2D);
    if (fbo)
        delete fbo;
    makeCurrent();
    fbo =  new QGLFramebufferObject(600, 300, GL_TEXTURE_2D);
    fbo->bind();
    texture_id = fbo->texture();

    QPainter painter(fbo);
    font.setPointSize(20);
    painter.setFont(font);
    painter.eraseRect(0,0,600,300);
    painter.setPen(Qt::blue);
    painter.drawText(100, 140, "FBO");
    painter.setPen(Qt::red);
    painter.drawText(400, 140, "FBO");
    painter.end();

    fbo->release();
}

QSize glview::sizeHint() const
{
    return QSize(300, 300);
}

void glview::initializeGL()
{
    initializeGLFunctions();
    qglClearColor(Qt::white);

    QGLShader *txtovlp_vshader = new QGLShader(QGLShader::Vertex, this);
    const char *txtovlp_vsrc =
            "attribute highp vec4 vertex;\n"
            "attribute mediump vec2 texCoord;\n"
            "varying mediump vec2 texc;\n"
            "uniform mediump mat4 matrix;\n"
            "void main(void)\n"
            "{\n"
            "    gl_Position = matrix * vertex;\n"
            "    texc = texCoord;\n"
            "}\n";
    txtovlp_vshader->compileSourceCode(txtovlp_vsrc);

    QGLShader *txtovlp_fshader = new QGLShader(QGLShader::Fragment, this);
    const char *txtovlp_fsrc =
            "uniform sampler2D texture;\n"
            "varying mediump vec2 texc;\n"
            "void main(void)\n"
            "{\n"
            "    gl_FragColor = texture2D(texture, texc.st);\n"
            "}\n";
    txtovlp_fshader->compileSourceCode(txtovlp_fsrc);

    txtovlp = new QGLShaderProgram(this);
    txtovlp->addShader(txtovlp_vshader);
    txtovlp->addShader(txtovlp_fshader);
    txtovlp->link();

    glGenBuffers(1, vbo_id);
    glBindBuffer(GL_ARRAY_BUFFER, vbo_id[0]);
    glBufferData(GL_ARRAY_BUFFER, sizeof(txtr_geo), txtr_geo, GL_STATIC_DRAW);

    make_text();

    glEnable(GL_DEPTH_TEST);
}

void glview::resizeGL(int w, int h)
{
    glViewport(0, 0, w, h);
}

void glview::paintGL()
{
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

    QMatrix4x4 matrix;
    matrix.ortho(0, 3, 0, 3, -1, 1);
    //matrix.translate(-3,0,0);

    txtovlp->bind();
    txtovlp->setUniformValue("matrix", matrix);

    glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
    glEnable(GL_BLEND);
    glEnable(GL_TEXTURE_2D);

    glBindBuffer(GL_ARRAY_BUFFER, vbo_id[0]);
    glBindTexture(GL_TEXTURE_2D, texture_id);

    int txtr_vertexLocation = txtovlp->attributeLocation("vertex");
    txtovlp->enableAttributeArray(txtr_vertexLocation);
    glVertexAttribPointer(txtr_vertexLocation, 3, GL_FLOAT, GL_FALSE, sizeof(struct txtr_vrtx), 0);

    int texCoordLocation = txtovlp->attributeLocation("texCoord");
    txtovlp->enableAttributeArray(texCoordLocation);
    glVertexAttribPointer(texCoordLocation, 2, GL_FLOAT, GL_FALSE, sizeof(struct txtr_vrtx), ((char*)NULL + 12));

    glDrawArrays(GL_TRIANGLE_FAN, 0, 4);

    glDisable(GL_TEXTURE_2D);
    glDisable(GL_BLEND);

    glFlush();
}
#包括
#包括
#包括
#包括“main.h”
结构txtr\U vrtx{
GLX;
GLY;
GLZ;
GLTX;
GLTY;
}__属性(压缩)txtr\u geo[]={
//x,y,z,tx,ty
{0, 0, 0, 0, 0},
{0, 3, 0, 0, 1},
{6, 3, 0, 1, 1},
{6, 0, 0, 1, 0},
};
int main(int argc,char*argv[])
{
QApplication应用程序(argc、argv);
glview小部件;
widget.show();
返回app.exec();
}
glview::glview(QWidget*父项):QGLWidget(父项)
{
fbo=NULL;
font.setFamily(“Helvetica”);
延迟=新的QTimer;
延迟->启动(2000);
连接(延迟、信号(超时())、此、插槽(刷新());
}
glview::~glview()
{
删除固定基地组织;
}
void glview::刷新(void)
{
延迟->停止();
qDebug()绑定();
纹理_id=fbo->texture();
油漆工(fbo);
字体设置点大小(20);
painter.setFont(字体);
橡皮擦(0,0600300);
画师:设置笔(Qt::蓝色);
画师.绘图文本(100140,“FBO”);
画师:设置笔(Qt::红色);
画师.绘图文本(400140,“FBO”);
结束();
fbo->release();
}
QSize glview::sizeHint()常量
{
返回QSize(300300);
}
void glview::initializeGL()
{
initializeGLFunctions();
qglClearColor(Qt::白色);
QGLShader*txtovlp_vshader=新的QGLShader(QGLShader::Vertex,this);
常量字符*txtovlp\u vsrc=
“属性highp vec4顶点;\n”
“属性mediump vec2 texCoord;\n”
“可变介质矢量2 texc;\n”
“统一mediump mat4矩阵;\n”
“作废主(作废)\n”
“{\n”
“gl_位置=矩阵*顶点;\n”
“texc=texCoord;\n”
“}\n”;
txtovlp_vshader->编译源代码(txtovlp_vsrc);
QGLShader*txtovlp_fshader=新的QGLShader(QGLShader::Fragment,this);
常量字符*txtovlp_fsrc=
“二维纹理的均匀采样;\n”
“可变介质矢量2 texc;\n”
“作废主(作废)\n”
“{\n”
gl_FragColor=texture2D(纹理,texc.st);\n
“}\n”;
txtovlp_fshader->编译资源代码(txtovlp_fsrc);
txtovlp=新的QGLShaderProgram(本程序);
txtovlp->addShader(txtovlp_vshader);
txtovlp->addShader(txtovlp_fshader);
txtovlp->link();
glGenBuffers(1,vbo_id);
glBindBuffer(GL_数组_BUFFER,vbo_id[0]);
glBufferData(GL_数组_缓冲区、sizeof(txtr_geo)、txtr_geo、GL_静态_绘图);
生成文本();
glEnable(GLU深度试验);
}
void glview::resizeGL(int w,int h)
{
glViewport(0,0,w,h);
}
void glview::paintGL()
{
glClear(GL_颜色_缓冲_位| GL_深度_缓冲_位);
QMatrix4x4矩阵;
正交矩阵(0,3,0,3,-1,1);
//矩阵转换(-3,0,0);
txtovlp->bind();
txtovlp->setUniformValue(“矩阵”,矩阵);
glBlendFunc(GL_SRC_ALPHA,GL_ONE_减去GL_SRC_ALPHA);
glEnable(GL_混合物);
glEnable(GL_纹理_2D);
glBindBuffer(GL_数组_BUFFER,vbo_id[0]);
glBindTexture(GL_纹理_2D,纹理_id);
int txtr_vertexLocation=txtovlp->attributeLocation(“顶点”);
txtovlp->enableAttributeArray(txtr_vertexLocation);
glvertexattributepointer(txtr_vertexLocation,3,GL_FLOAT,GL_FALSE,sizeof(struct txtr_vrtx),0);
int texCoordLocation=txtovlp->attributeLocation(“texCoord”);
txtovlp->enableAttributeArray(texCoordLocation);
glvertexattributepointer(texCoordLocation,2,GL_FLOAT,GL_FALSE,sizeof(struct txtr_vrtx),((char*)NULL+12));
gldrawArray(GL_三角形_扇,0,4);
glDisable(GL_纹理_2D);
glDisable(GLU混合);
glFlush();
}

QPainter会破坏GL上下文,我了解到它还包括视口。通过添加glViewport(0,0,width(),height());在make_text()的末尾(完成QPaint后),为下一个绘制事件恢复视口