OpenGL - 纹理

作者: li_礼光 | 来源:发表于2017-05-09 23:02 被阅读20次

GitHub Demo : Texture

纹理效果
    //顶点数据,前三个是顶点坐标, 中间三个是顶点颜色,    最后两个是纹理坐标
    GLfloat attrArr[] = {
        //  x,     y,    z,     r,    g,    b,    a,       s,    t,
        -0.5f,  0.5f, 0.0f,  0.0f, 0.0f, 1.0f, 1.0f,    0.0f, 1.0f,
         0.5f,  0.5f, 0.0f,  0.0f, 1.0f, 0.0f, 1.0f,    1.0f, 1.0f,
        -0.5f, -0.5f, 0.0f,  1.0f, 0.0f, 1.0f, 1.0f,    0.0f, 0.0f,
         0.5f, -0.5f, 0.0f,  0.0f, 0.0f, 1.0f, 1.0f,    1.0f, 0.0f,
    };
    //顶点索引
    GLuint indices[] = {
        0, 1, 2,
        1, 2, 3,
    };
    
    //VBO
    GLuint buffer;
    glGenBuffers(1, &buffer);
    glBindBuffer(GL_ARRAY_BUFFER, buffer);
    glBufferData(GL_ARRAY_BUFFER, sizeof(attrArr), attrArr, GL_STATIC_DRAW);
    
    //IBO
    GLuint index;
    glGenBuffers(1, &index);
    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, index);
    glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indices), indices, GL_STATIC_DRAW);
    
    //坐标
    glEnableVertexAttribArray(GLKVertexAttribPosition);
    glVertexAttribPointer(GLKVertexAttribPosition, 3, GL_FLOAT, GL_FALSE, sizeof(GLfloat) * 9, (GLfloat *)NULL);
    //顶点颜色
    glEnableVertexAttribArray(GLKVertexAttribColor);
    glVertexAttribPointer(GLKVertexAttribColor, 4, GL_FLOAT, GL_FALSE, sizeof(GLfloat) * 9, (GLfloat *)NULL + 3);
   
    //纹理属性0
    glEnableVertexAttribArray(GLKVertexAttribTexCoord0);
    glVertexAttribPointer(GLKVertexAttribTexCoord0, 2, GL_FLOAT, GL_FALSE, sizeof(GLfloat) * 9, (GLfloat *)NULL + 3 + 4);
    
    //纹理属性1
    glEnableVertexAttribArray(GLKVertexAttribTexCoord1);
    glVertexAttribPointer(GLKVertexAttribTexCoord1, 2, GL_FLOAT, GL_FALSE, sizeof(GLfloat) * 9, (GLfloat *)NULL + 3 + 4);
    
    //加载纹理0
    NSString* filePath0 = [[NSBundle mainBundle] pathForResource:@"texture0.jpg" ofType:@""];
    NSDictionary* options0 = [NSDictionary dictionaryWithObjectsAndKeys:@(1), GLKTextureLoaderGenerateMipmaps, nil];
    GLKTextureInfo* textureInfo0 = [GLKTextureLoader textureWithContentsOfFile:filePath0 options:options0 error:nil];
    self.mEffect.texture2d0.enabled = GL_TRUE;
    self.mEffect.texture2d0.name = textureInfo0.name;
    
    //加载纹理1
    NSString* filePath1 = [[NSBundle mainBundle] pathForResource:@"texture1.png" ofType:@""];
    NSDictionary* options1 = [NSDictionary dictionaryWithObjectsAndKeys:@(1), GLKTextureLoaderOriginBottomLeft, nil];
    GLKTextureInfo* textureInfo1 = [GLKTextureLoader textureWithContentsOfFile:filePath1 options:options1 error:nil];
    self.mEffect.texture2d1.enabled = GL_TRUE;
    self.mEffect.texture2d1.name = textureInfo1.name;

纹理的一些相关知识

相关文章

  • GLKit常用API解析

    GLKTextureInfo 创建OpenGL纹理信息 name: OpenGL上下文中纹理名称 target: ...

  • OpenGL ES GLKit 􏰼􏰜常用API解析

    GLKTextureInfo创建OpenGL纹理信息 name : OpenGL上下文中纹理名称 target :...

  • OpenGL纹理内容

    纹理可以理解为一张图片,OpenGL渲染图片会将图片的像素保存在纹理缓存中。 OpenGL常用纹理函数 载入纹理 ...

  • OpenGL纹理

    纹理可以理解为一张图片,OpenGL渲染图片会将图片的像素保存在纹理缓存中。OpenGL常用纹理函数 载入纹理 纹...

  • OPenGL ES纹理翻转解决方案

    纹理翻转 在使用OpenGL函数加载纹理到图形时,经常遇到纹理上下颠倒的问题。原因是因为OpenGL要求纹理坐标原...

  • OpenGL坐标概念

    openGL 顶点,坐标系,纹理坐标Android OpenGL es 纹理坐标设定与贴图规则对Android o...

  • OpenGL之纹理及应用案例

    纹理介绍 OpenGL使用的图片数据(纹理)都是tga格式的,而iOS/OpenGL ES使用PNG/JPEG格式...

  • OpenGL 纹理翻转策略

    Open GL纹理翻转 在使用OpenGL函数加载纹理到图形时,经常遇到纹理上下颠倒的问题。原因是因为OpenGL...

  • OpenGL ES之旅(三)-- OpenGL ES 纹理翻转解

    纹理翻转概述 在使用OpenGL ES函数加载纹理到图形时,经常遇到纹理上下颠倒的问题。原因是因为OpenGL E...

  • 案例08:隧道

    OpenGL + OpenGL ES +Metal 系列文章汇总 本案例主要目的多个纹理如何使用,加深对纹理的使用...

网友评论

    本文标题:OpenGL - 纹理

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