OpenGL - 纹理

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;

纹理的一些相关知识

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • [toc] 参考文章:http://blog.csdn.net/a358333644/article/detail...
    迷途小书童nb阅读 1,963评论 0 0
  • 纹理的加载 当我们设置好纹理对象后,就可以加载我们的纹理数据了。下面这个例子演示了如何加载一幅2*2像素的纹理: ...
    猿基地阅读 13,304评论 3 21
  • 写在前面的话 现实生活中,纹理最通常的作用是装饰我们的物体模型,它就像是贴纸一样贴在物体表面,使得物体表面拥有图案...
    猿基地阅读 26,905评论 0 36
  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 174,787评论 25 709
  • 愛情中有太多的無奈與辛酸, 讓我們來不及懺悔, 甚至一轉眼就消失不見。 我們似乎都逃不過命運的接近, 一雙無形的手...
    18edff4924a4阅读 237评论 0 0