定义我要做的事情:我希望能够从^ 2x ^ 2大小的PNG中获取任意“精灵”图像,并且只显示感兴趣的像素到给定的x / y位置屏幕。
我的结果are the problem - major distortion - 看起来很糟糕! (注意这些SS是在iPhone的SIM卡中,但在真正的视网膜设备上,它们看起来是相同的... junky)。以下是“预览”中源PNG的屏幕截图 - which looks wonderful(我在此问题中描述的渲染的任何变体看起来几乎都与垃圾一样)
以前,我asked a question使用OpenGL ES 2.0将非2次幂纹理显示为精灵(尽管这适用于任何OpenGL)。我很接近,但我有一些我无法解决的问题。我认为可能存在多个错误 - 我认为存在一些错误,我基本上是通过渲染大而不是压缩x2来反映我所显示的,反之亦然但是我看不到它。此外,还有一个错误,我无法处理它们。我不能在视觉上识别出它们,但我知道它们确实在那里。
我正在使用960 x 640 风景(在iPhone4视网膜显示屏上)。所以我期望0-> 959从左到右移动,0-> 639从下到上移动。 (而且我认为我看到了相反的情况 - 但这不是这个问题的意思)
为了简单起见,我在这个测试用例中试图实现的是一个PNG文件的FULL SCREEN 960x640显示。只是其中之一。我首先显示一个红色背景,这样很明显我是否覆盖了屏幕。
更新:我意识到setFramebuffer调用中的'glViewport'正在向后设置我的宽度和高度。我注意到了这一点,因为当我将几何体设置为从0,0到100,100时,它会绘制一个矩形而不是一个正方形。当我交换这些时,该调用确实绘制了一个正方形。但是,使用相同的调用,我的整个屏幕填充顶点范围0,0 - > 480,320(半'分辨率')..不明白。然而,无论我从哪里继续推进,我仍然没有得到一个好看的结果
这是我的顶点着色器:
attribute vec4 a_position;
attribute vec2 a_texCoord;
varying vec2 v_texCoord;
// Gives 'landscape' full screen..
mat4 projectionMatrix = mat4( 2.0/640.0, 0.0, 0.0, -1.0,
0.0, 2.0/960.0, 0.0, -1.0,
0.0, 0.0, -1.0, 0.0,
0.0, 0.0, 0.0, 1.0);
// Gives a 1/4 of screen.. (not doing 2.0/.. was suggested in previous SO Q)
/*mat4 projectionMatrix = mat4( 1.0/640.0, 0.0, 0.0, -1.0,
0.0, 1.0/960.0, 0.0, -1.0,
0.0, 0.0, -1.0, 0.0,
0.0, 0.0, 0.0, 1.0); */
// Apply the projection matrix to the position and pass the texCoord
void main()
{
gl_Position = a_position;
gl_Position *= projectionMatrix;
v_texCoord = a_texCoord;
}
这是我的片段着色器:
precision mediump float;
varying vec2 v_texCoord;
uniform sampler2D s_texture;
void main()
{
gl_FragColor = texture2D(s_texture, v_texCoord);
}
这是我的抽奖代码:
#define MYWIDTH 960.0f
#define MYHEIGHT 640.0f
// I have to refer to 'X' as height although I'd assume I use 'Y' here..
// I think my X and Y throughout this whole block of code is screwed up
// But, I have experimented flipping them all and verifying that if they
// Are taken from the way they're set now to swapping X and Y that things
// end up being turned the wrong way. So this is a mess, but unlikely my problem
#define BG_X_ORIGIN 0.0f
// ALSO NOTE HERE: I have to put my 'dest' at 640.0f.. --- see note [1] below
#define BG_X_DEST 640.0f
#define BG_Y_ORIGIN 0.0f
// --- see note [1] below
#define BG_Y_DEST 960.0f
// These are texturing coordinates, I texture starting at '0' px and then
// I calculate a percentage of the texture to use based on how many pixels I use
// divided by the actual size of the image (1024x1024)
#define BG_X_ZERO 0.0f
#define BG_Y_USEPERCENTAGE BG_X_DEST / 1023.0f
#define BG_Y_ZERO 0.0f
#define BG_X_USEPERCENTAGE BG_Y_DEST / 1023.0f
// glViewport(0, 0, MYWIDTH, MYHEIGHT);
// See note 2.. it sets glViewport basically, provided by Xcode project template
[(EAGLView *)self.view setFramebuffer];
// Big hack just to get things going - like I said before, these could be backwards
// w/respect to X and Y
static const GLfloat backgroundVertices[] = {
BG_X_ORIGIN, BG_Y_ORIGIN,
BG_X_DEST, BG_Y_ORIGIN,
BG_X_ORIGIN, BG_Y_DEST,
BG_X_DEST, BG_Y_DEST
};
static const GLfloat backgroundTexCoords[] = {
BG_X_ZERO, BG_Y_USEPERCENTAGE,
BG_X_USEPERCENTAGE, BG_Y_USEPERCENTAGE,
BG_X_ZERO, BG_Y_ZERO,
BG_X_USEPERCENTAGE, BG_Y_ZERO
};
// Turn on texturing
glEnable(GL_TEXTURE_2D);
// Clear to RED so that it's obvious when I'm not drawing my sprite on screen
glClearColor(1.0f, 0.0f, 0.0f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT);
// Texturing parameters - these make sense.. don't think they are the issue
glActiveTexture(GL_TEXTURE0);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_NEAREST);//GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_NEAREST);//GL_LINEAR);
// Update attribute values.
glVertexAttribPointer(ATTRIB_VERTEX, 2, GL_FLOAT, 0, 0, backgroundVertices);
glEnableVertexAttribArray(ATTRIB_VERTEX);
glVertexAttribPointer(ATTRIB_TEXCOORD, 2, GL_FLOAT, 0, 0, backgroundTexCoords);
glEnableVertexAttribArray(ATTRIB_TEXCOORD);
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, background->textureId);
// I don't understand what this uniform does in the texture2D call in shader.
glUniform1f(uniforms[UNIFORM_SAMPLERLOC], 0);
// Draw the geometry...
glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
// present the framebuffer see note [3]
[(EAGLView *)self.view presentFramebuffer];
注意[1] :
如果我将BG_X_DEST设置为639.0f,我无法完全覆盖640像素,我会在右侧显示红色。但这对我来说没有意义 - 我的目标是像素完美,我必须绘制我的精灵几何从0到640,这是641像素,当我只有640 !!! red line appearing with 639f instead of 640f
如果我将BG_Y_DEST设置为959.0f,我就不会通过红线显示。 red line top bug appearing with 958f instead of 960 or 959f
这可能是我所发生的错误的一个很好的线索。
注意:[2] - 由Xcode包含在OpenGL ES 2框架中
- (void)setFramebuffer
{
if (context)
{
[EAGLContext setCurrentContext:context];
if (!defaultFramebuffer)
[self createFramebuffer];
glBindFramebuffer(GL_FRAMEBUFFER, defaultFramebuffer);
glViewport(0, 0, framebufferWidth, framebufferHeight);
}
}
注意[3]: - 由Xcode包含在OpenGL ES 2框架中
- (BOOL)presentFramebuffer
{
BOOL success = FALSE;
if (context)
{
[EAGLContext setCurrentContext:context];
glBindRenderbuffer(GL_RENDERBUFFER, colorRenderbuffer);
success = [context presentRenderbuffer:GL_RENDERBUFFER];
}
return success;
}
注意[4] - 相关的图片加载代码(我使用过带有和没有alpha通道的PNG,实际上它似乎没有任何区别......我也试图改变我的代码是ARGB而不是RGBA,这是错误的 - 因为A = 1.0无处不在,我得到一个非常RED的图像,这让我觉得RGBA实际上是有效的,而且这段代码是正确的。):更新:我已经使用CG / ImageIO调用将这个纹理加载切换到一个完全不同的设置,它看起来与此相同所以我认为它不是由图像库完成的某种混叠或颜色压缩(除非它们都是相同的基础电话,这是可能的..)
// Otherwise it isn't already loaded
glEnable(GL_TEXTURE_2D);
glActiveTexture(GL_TEXTURE0);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_NEAREST);//GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_NEAREST);//GL_LINEAR);
// TODO Next 2 can prob go later on..
glGenTextures(1, &(newTexture->textureId)); // generate Texture
// Use this before 'drawing' the texture to the memory...
glBindTexture(GL_TEXTURE_2D, newTexture->textureId);
NSString *path = [[NSBundle mainBundle]
pathForResource:[NSString stringWithUTF8String:newTexture->filename.c_str()] ofType:@"png"];
NSData *texData = [[NSData alloc] initWithContentsOfFile:path];
UIImage *image = [[UIImage alloc] initWithData:texData];
if (image == nil)
NSLog(@"Do real error checking here");
newTexture->width = CGImageGetWidth(image.CGImage);
newTexture->height = CGImageGetHeight(image.CGImage);
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
void *imageData = malloc(newTexture->height * newTexture->width * 4 );
CGContextRef myContext = CGBitmapContextCreate
(imageData, newTexture->width, newTexture->height, 8, 4 * newTexture->width, colorSpace,
kCGImageAlphaPremultipliedLast | kCGBitmapByteOrder32Big );
CGColorSpaceRelease(colorSpace);
CGContextClearRect(myContext, CGRectMake(0, 0, newTexture->width, newTexture->height));
CGContextDrawImage(myContext, CGRectMake(0, 0, newTexture->width, newTexture->height), image.CGImage);
// Texture is created!
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, newTexture->width, newTexture->height, 0,
GL_RGBA, GL_UNSIGNED_BYTE, imageData);
CGContextRelease(myContext);
free(imageData);
[image release];
[texData release];
答案 0 :(得分:1)
[(EAGLView *)self.view setContentScaleFactor:2.0f];
默认情况下,iPhone窗口会进行缩放以达到其高分辨率模式。这破坏了我的图像质量..
感谢所有帮助人员