OpenGL常用函数笔记

2018-05-02  本文已影响45人  whlpkk

glTexImage2D

void glTexImage2D (
    GLenum target, 
    GLint level, 
    GLint internalformat, 
    GLsizei width, 
    GLsizei height, 
    GLint border, 
    GLenum format, 
    GLenum type, 
    const GLvoid *pixels
);

为当前绑定的纹理对象开辟内存并填充pixels数据到相应内存,生成纹理图像

glFramebufferTexture2D

void glFramebufferTexture2D (
    GLenum target, 
    GLenum attachment, 
    GLenum textarget, 
    GLuint texture, 
    GLint level
);

把纹理附件绑定到帧缓冲

eg: 颜色附件

unsigned int texture;
glGenTextures(1, &texture);
glBindTexture(GL_TEXTURE_2D, texture);

//对于这个纹理,我们仅仅分配了内存而没有填充它。填充这个纹理将会在我们渲染到帧缓冲之后来进行
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, 800, 600, 0, GL_RGB, GL_UNSIGNED_BYTE, NULL);

glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);

//把纹理附件绑定到帧缓冲
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, texture, 0);

深度模板附件

glTexImage2D(GL_TEXTURE_2D, 0, GL_DEPTH24_STENCIL8, 800, 600, 0, GL_DEPTH_STENCIL, GL_UNSIGNED_INT_24_8, NULL);

glFramebufferTexture2D(GL_FRAMEBUFFER, GL_DEPTH_STENCIL_ATTACHMENT, GL_TEXTURE_2D, texture, 0);

CVOpenGLESTextureCacheCreateTextureFromImage

CVOpenGLESTextureCacheCreateTextureFromImage(
    CFAllocatorRef  _Nullable allocator, 
    CVOpenGLESTextureCacheRef  _Nonnull textureCache, 
    CVImageBufferRef  _Nonnull sourceImage, 
    CFDictionaryRef  _Nullable textureAttributes, 
    GLenum target, 
    GLint internalFormat, 
    GLsizei width, 
    GLsizei height, 
    GLenum format, 
    GLenum type, 
    size_t planeIndex, 
    CVOpenGLESTextureRef  _Nullable * _Nonnull textureOut
);

根据图片生成纹理对象

eg:

#pragma mark - 7、创建纹理对象,渲染采集图片到屏幕
- (void)setupTexture:(CMSampleBufferRef)sampleBuffer
{
    // 获取图片信息
    CVImageBufferRef imageBufferRef = CMSampleBufferGetImageBuffer(sampleBuffer);

    // 获取图片宽度
    GLsizei bufferWidth = (GLsizei)CVPixelBufferGetWidth(imageBufferRef);
    _bufferWidth = bufferWidth;
    GLsizei bufferHeight = (GLsizei)CVPixelBufferGetHeight(imageBufferRef);
    _bufferHeight = bufferHeight;

    // 创建亮度纹理
    // 激活纹理单元0, 不激活,创建纹理会失败
    glActiveTexture(GL_TEXTURE0);

    // 创建纹理对象
    CVReturn err;
    err = CVOpenGLESTextureCacheCreateTextureFromImage(kCFAllocatorDefault, _textureCacheRef, imageBufferRef, NULL, GL_TEXTURE_2D, GL_LUMINANCE, bufferWidth, bufferHeight, GL_LUMINANCE, GL_UNSIGNED_BYTE, 0, &_luminanceTextureRef);
    if (err) {
        NSLog(@"Error at CVOpenGLESTextureCacheCreateTextureFromImage %d", err);
    }
    // 获取纹理对象id
    _luminanceTexture = CVOpenGLESTextureGetName(_luminanceTextureRef);
     
    // 绑定纹理
    glBindTexture(CVOpenGLESTextureGetTarget(renderTexture), _luminanceTexture);
    
    //CVOpenGLESTextureGetTarget(renderTexture)获取纹理绑定的target,在这里是GL_TEXTURE_2D,所以下面写也可以
    //glBindTexture(GL_TEXTURE_2D, _luminanceTexture);
   

    // 设置纹理滤波
    glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
    glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);

    // 激活单元1
    glActiveTexture(GL_TEXTURE1);

    // 创建色度纹理
    err = CVOpenGLESTextureCacheCreateTextureFromImage(kCFAllocatorDefault, _textureCacheRef, imageBufferRef, NULL, GL_TEXTURE_2D, GL_LUMINANCE_ALPHA, bufferWidth / 2, bufferHeight / 2, GL_LUMINANCE_ALPHA, GL_UNSIGNED_BYTE, 1, &_chrominanceTextureRef);
    if (err) {
        NSLog(@"Error at CVOpenGLESTextureCacheCreateTextureFromImage %d", err);
    }
    // 获取纹理对象
    _chrominanceTexture = CVOpenGLESTextureGetName(_chrominanceTextureRef);

    // 绑定纹理
    glBindTexture(GL_TEXTURE_2D, _chrominanceTexture);

    // 设置纹理滤波
    glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
    glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
}
上一篇下一篇

猜你喜欢

热点阅读