Pixel distortion OpenGL sprite rendering

To determine what I'm trying to do: I want to be able to take an arbitrary 2x2 × 2 PNG sprite image and display only the pixels of interest for a given x / y position on the screen.

My results - the problem - the main distortion - it looks awful! (Note that these SSs are in the iPhone sim, but on the real retina they look the same .. junky). Here is a screenshot of the original PNG in the “preview” - which looks great (any rendering options that I describe in this question look almost exactly like the younger one)

I used to ask a question about displaying non-permissions texture 2 as a sprite using OpenGL ES 2.0 (although this applies to any OpenGL as well). I am close, but I have some problems that I cannot solve. I think there are probably a few errors - I think there is some kind of error in which I basically smooth out what I show by rendering larger than crush x2 or vice versa, but I do not see this. In addition, there is one mistake, and I can not deal with them. I cannot visually identify them, but I know for sure that they are there.

I work in 960 x 640 landscape format (on the iPhone4 Retina screen). Therefore, I expect that 0-> 959 moves from left to right, 0-> 639 moves from bottom to top. (And I think I see the opposite - but this is not the issue in question)

To do everything that I try to achieve in this test case, FULL SCREEN 960x640 is displayed for the PNG file. Only one of them. First I show a red background so that it is obvious if I close the screen or not.

: , "glViewport" setFramebuffer . , , , 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 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!!! 639f 640f

BG_Y_DEST 959.0f, show throug. , 958f 960 959f

, .

: [2] - OpenGL ES 2 Xcode

- (void)setFramebuffer 
{
    if (context)
    {
        [EAGLContext setCurrentContext:context];

        if (!defaultFramebuffer)
            [self createFramebuffer];

        glBindFramebuffer(GL_FRAMEBUFFER, defaultFramebuffer);

        glViewport(0, 0, framebufferWidth, framebufferHeight);
    }
}

[3]: - OpenGL ES 2 Xcode

- (BOOL)presentFramebuffer
{
    BOOL success = FALSE;

    if (context)
    {
        [EAGLContext setCurrentContext:context];

        glBindRenderbuffer(GL_RENDERBUFFER, colorRenderbuffer);

        success = [context presentRenderbuffer:GL_RENDERBUFFER];
    }

    return success;
}

[4] - ( PNG - , ... , ARGB RGBA, - A = 1.0 , , , RGBA .): update: 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];
+1
1

[(EAGLView *) self.view setContentScaleFactor: 2.0f];

iPhone . .

+1

Source: https://habr.com/ru/post/1786940/


All Articles