我需要缩放glDrawPixels图像的结果。

我正在Qt QGLWidget中使用glDrawPixels绘制640x480像素的图像缓冲区。

我尝试在PaintGL中执行以下操作:

glScalef(windowWidth/640, windowHeight/480, 0);
glDrawPixels(640,480,GL_RGB,GL_UNSIGNED_BYTE,frame);

但这是行不通的。

我将OpenGL视口(viewport)和glOrtho的大小设置为:
void WdtRGB::paintGL() {

         glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

         // Setup the OpenGL viewpoint
         glMatrixMode(GL_PROJECTION);
         glLoadIdentity();
         glOrtho(0, windowWidth, windowHeight, 0, -1.0, 1.0);

    glDepthMask(0);
        //glRasterPos2i(0, 0);
        glScalef(windowWidth/640, windowHeight/480, 0);
        glDrawPixels(640,480,GL_RGB,GL_UNSIGNED_BYTE,frame);
    }

    //where windowWidth and windowHeight corresponds to the widget size.
    /the init functions are:

    void WdtRGB::initializeGL() {

        glClearColor ( 0.8, 0.8, 0.8, 0.0); // Background to a grey tone

        /* initialize viewing values  */
        glMatrixMode(GL_PROJECTION);
        glLoadIdentity();

        glOrtho(0, windowWidth, windowHeight, 0, -1.0, 1.0);

        glEnable (GL_DEPTH_TEST);

    }

    void WdtRGB::resizeGL(int w, int h) {
        float aspect=(float)w/(float)h;

        windowWidth = w;
        windowHeight = h;
        glViewport (0, 0, (GLsizei) w, (GLsizei) h);
        glMatrixMode (GL_PROJECTION);
        glLoadIdentity ();

        if( w <= h )
                glOrtho ( -5.0, 5.0, -5.0/aspect, 5.0/aspect, -5.0, 5.0);
        else
                glOrtho (-5.0*aspect, 5.0*aspect, -5.0, 5.0, -5.0, 5.0);

        //printf("\nresize");
        emit changeSize ( );
    }

最佳答案

听起来您实际上需要执行的工作(而不是调用glDrawPixels())是将您的图像数据加载到纹理中,并绘制窗口大小的纹理四边形。所以像这样:

glGenTextures (1, &texID);
glBindTextures (GL_TEXTURE_RECTANGLE_EXT, texID);
glTexImage2D (GL_TEXTURE_RECTANGLE_EXT, 0, GL_RGBA, 640, 480, 0, GL_RGBA, GL_UNSIGNED_INT_8_8_8_8, frame);
glBegin (GL_QUADS);
glTexCoord2f (0, 0);
glVertex2f (0, 0);
glTexCoord2f (640, 0);
glVertex2f (windowWidth, 0);
glTexCoord2f (640, 480);
glVertex2f (windowWidth, windowHeight);
glTexCoord2f (0, 480);
glVertex2f (0, windowHeight);
glEnd();

或者,如果这太多了,glPixelZoom(windowWidth/640,windowHeight/480)也可以解决问题。

关于opengl - 如何缩放glDrawPixels?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/8774521/

10-11 05:02