下面的代码可以在Opengl中显示图像/纹理。该方法应该以正确的高宽比显示图像,并放大/缩小图像。
图像在水平轴上似乎没有保持其纵横比。为什么?
(注: OpenGL观看宽度为-1至0,高度为1至-1)。
private void renderImage(Rectangle dst, float magnification) {
float width, height;
float horizontalOffset, verticalOffset;
// Default: Fill screen horizontally
width = 1f;
height = dst.getHeight()/(float) dst.getWidth();
// magnification
width *= magnification;
height *= magnification;
// Offsets
horizontalOffset = width/2f;
verticalOffset = height/2f;
// Do the actual OpenGL rendering
glBegin (GL_QUADS);
// Right top
glTexCoord2f(0.0f, 0.0f);
glVertex2f(-0.5f + horizontalOffset, verticalOffset);
// Right bottom
glTexCoord2f(0.0f, 1.0f);
glVertex2f(-0.5f + horizontalOffset, -verticalOffset);
// Left bottom
glTexCoord2f(1.0f,1.0f);
glVertex2f(-0.5f - horizontalOffset, -verticalOffset);
// Left top
glTexCoord2f(1.0f, 0.0f);
glVertex2f(-0.5f - horizontalOffset, verticalOffset);
glEnd();
}发布于 2013-10-03 17:27:51
我对OpenGL没有任何经验,但是从您的代码来看,您的默认填充似乎有一些可疑之处。
// Default: Fill screen horizontally
width = 1f;
height = dst.getHeight()/(float) dst.getWidth();这是将您的“宽度”变量设置为常数值1,而“高度”变量依赖于要传入的矩形的高度和宽度,然后该矩形将用于计算偏移量。
// Offsets
horizontalOffset = width/2f;
verticalOffset = height/2f;在我的经验中,这可能会导致您已经指出的问题,首先尝试在调试器中遍历这个函数来分析宽度和高度变量所持有的值,或者尝试更改。
// Default: Fill screen horizontally
width = 1f;
height = dst.getHeight()/(float) dst.getWidth();至
// Default: Fill screen horizontally
width = 1f;
height = 1f;然后重新运行这个,看看它是否对您的输出有影响。
https://stackoverflow.com/questions/19164842
复制相似问题