zoukankan      html  css  js  c++  java
  • OpenGL ES2.0贴图

    1、定义传入着色器的顶点数据及索引

    //传入结构体
    typedef struct {
        float Position[3]; 
        float TexCoord[2];
    } Vertex;
    //顶点数据 
    const Vertex Vertices[] = {
        {{1, -1, 0}, {1, 1}},
        {{1, 1, 0},{1, 0}},
        {{-1, 1, 0}, {0, 0}},
        {{-1, -1, 0},{0, 1}},
    }; 
    //顶点索引
    const GLubyte Indices[] = { 
        0, 1, 2,
        2, 3, 0
    };

    2、生成图片纹理并获得其索引 方法

    + (GLuint)setupTexture:(UIImage *)pImage {
     
        CGImageRef spriteImage = pImage.CGImage;
        if (!spriteImage) {
            NSLog(@"Failed to load image ");
            exit(1);
        }
         
        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);
        
     
        CGContextDrawImage(spriteContext, CGRectMake(0, 0, width, height), spriteImage);
        
        CGContextRelease(spriteContext);
        
      
        GLuint texName;
        //生成纹理数
        glGenTextures(1, &texName);
        glBindTexture(GL_TEXTURE_2D, texName);
        
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
        //生成图片纹理
        glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, spriteData);
        
        free(spriteData);        
        return texName;    
    }

    3、获取传入GLSL的槽位

     //获得图片纹理索引
     _imageTexture = [OpenGLHelper setupTexture:pImage];
    //获得在GLSL中纹理的索引
       _texCoordSlot = glGetAttribLocation(_programHandle, "TexCoordIn");
        glEnableVertexAttribArray(_texCoordSlot);
        _textureUniform = glGetUniformLocation(_programHandle, "Texture");
    //获得GLSL中顶点的索引
       _positionSlot = glGetAttribLocation(_programHandle, "vPosition");

    4、在render(渲染)方法中加入

         //传入顶点着色器的数据
        glVertexAttribPointer(_positionSlot, 3, GL_FLOAT, GL_FALSE,
                              sizeof(Vertex), 0); 
        glVertexAttribPointer(_texCoordSlot, 2, GL_FLOAT, GL_FALSE,
                              sizeof(Vertex), (GLvoid*) (sizeof(float) * 3
                                                         ));
            
        glActiveTexture(GL_TEXTURE0);
        glBindTexture(GL_TEXTURE_2D, _imageTexture);
        glUniform1i(_textureUniform, 0);
       
        // 设置边缘截取模式  防止图片读取有问题
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
        
        glDrawElements(GL_TRIANGLES, sizeof(Indices)/sizeof(Indices[0]),
                       GL_UNSIGNED_BYTE, 0); 
  • 相关阅读:
    RIP2与OSPFv2 动态路由协议区别
    Linux平台下SSD的TRIM指令的最佳使用方式(不区别对待NVMe)
    MLNX网卡驱动安装
    字符串/字符数组读入(char/string)
    【NOIP2016模拟3】图书列表
    活动选择-贪心
    数列极差问题-STL优先队列-贪心
    货物搬运-贪心
    【NOIP 2002提高】均分纸牌-贪心
    【HAOI2008】糖果传递-贪心
  • 原文地址:https://www.cnblogs.com/stratrail/p/3277407.html
Copyright © 2011-2022 走看看