美文网首页
OpenGL 纹理翻转策略

OpenGL 纹理翻转策略

作者: 瀚_ | 来源:发表于2019-07-06 18:03 被阅读0次

    旋转矩阵翻转图形

    GLuint rotate = glGetUniformLocation(self.myPrograme, "rotateMatrix");
        float radians = 180 * 3.14159f / 180.0f;
        float s = sin(radians);
        float c = cos(radians);
        
      
        GLfloat zRotation[16] = {
            c, -s, 0, 0,
            s, c, 0, 0,
            0, 0, 1.0, 0,
            0.0, 0, 0, 1.0
        };
        
       glUniformMatrix4fv(rotate, 1, GL_FALSE, (GLfloat *)&zRotation[0]);
    

    解压图片时,将图片源文件翻转

    CGImageRef spriteImage = [UIImage imageNamed:fileName].CGImage;
    
    size_t width = CGImageGetWidth(spriteImage);
    size_t height = CGImageGetHeight(spriteImage);
    GLubyte * spriteData = (GLubyte *) calloc(width * height * 4, sizeof(GLubyte));
    
    CGContextRef spriteContext = CGBitmapContextCreate(spriteData, width, height, 8, width*4,CGImageGetColorSpace(spriteImage), kCGImageAlphaPremultipliedLast);
      
    CGRect rect = CGRectMake(0, 0, width, height);
    CGContextDrawImage(spriteContext, CGRectMake(0, 0, width, height), spriteImage);
    
    CGContextTranslateCTM(spriteContext, rect.origin.x, rect.origin.y);
    CGContextTranslateCTM(spriteContext, 0, rect.size.height);
    CGContextScaleCTM(spriteContext, 1.0, -1.0);
    CGContextTranslateCTM(spriteContext, -rect.origin.x, -rect.origin.y);
    CGContextDrawImage(spriteContext, rect, spriteImage); 
    
    CGContextRelease(spriteContext);
    glBindTexture(GL_TEXTURE_2D, 0);
    

    修改片元着色器纹理坐标

    varying lowp vec2 varyTextCoord;
    uniform sampler2D colorMap;
    void main()
    {
        gl_FragColor = texture2D(colorMap, vec2(varyTextCoord.x,1.0-varyTextCoord.y));
    }
    
    

    修改顶点着色器,纹理坐标

    attribute vec4 position;
    attribute vec2 textCoordinate;
    varying lowp vec2 varyTextCoord;
    
    void main()
    {
        varyTextCoord = vec2(textCoordinate.x,1.0-textCoordinate.y);
        gl_Position = position;
    }
    

    直接从源纹理坐标数据修改

    GLfloat attrArr[] =
         {
         0.5f, -0.5f, 0.0f,        1.0f, 1.0f, //右下
         -0.5f, 0.5f, 0.0f,        0.0f, 0.0f, // 左上
         -0.5f, -0.5f, 0.0f,       0.0f, 1.0f, // 左下
         0.5f, 0.5f, 0.0f,         1.0f, 0.0f, // 右上
         -0.5f, 0.5f, 0.0f,        0.0f, 0.0f, // 左上
         0.5f, -0.5f, 0.0f,        1.0f, 1.0f, // 右下
         };
    

    来自:
    OpenGL 2D纹理单元&纹理翻转解决策略

    相关文章

      网友评论

          本文标题:OpenGL 纹理翻转策略

          本文链接:https://www.haomeiwen.com/subject/vgkmhctx.html