gpt4 book ai didi

opengl - 如何缩放glDrawPixels?

转载 作者:行者123 更新时间:2023-12-02 08:38:03 26 4
gpt4 key购买 nike

我需要缩放 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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com