分享

《高效学习OpenGL》 之 纹理对象 glGenTextures(),glIsTexture(),glBindTexture(),glDeleteTextures()

 雪柳花明 2015-12-11

命名纹理对象:

  1. void glGenTextures (GLsizei n, GLuint *textures);  
  2. //在数组textures中返回n个当期未使用的值,表示纹理对象的名称  
  3. //零作为一个保留的纹理对象名,它不会被此函数当做纹理对象名称而返回  

判断一个纹理名称是否处于实际使用中:

  1. GLboolean glIsTexture (GLuint texture);  
  2. //如果texture是一个已绑定的纹理对象名称,并且没有删除,就返回GL_TRUE;  

创建和使用纹理对象:

  1. glBindTexture(GL_TEXTURE_2D, texName[0]);  
  2. void glBindTexture (GLenum target, GLuint texture);  
  3. //应用程序第一次在这个函数使用texture值时,这个函数将会创建一个新的纹理对象  
  4. //当texture是一个以前已经创建的纹理对象时,这个纹理对象就成为活动对象  
  5. //如果texture为0,就停止使用这个纹理对象,并返回无名称的默认纹理  

清除纹理对象:

  1. glDeleteTextures(2, texName);  
  2. void glDeleteTextures (GLsizei n, const GLuint *textures);  
  3. //删除n个纹理对象,它们的名称由textures数组提供  

实例:

  1. #include <GL/glut.h>  
  2. #include <stdlib.h>  
  3. #include <stdio.h>  
  4.   
  5. #ifdef GL_VERSION_1_1  
  6. /*  Create checkerboard texture */  
  7. #define checkImageWidth 64  
  8. #define checkImageHeight 64  
  9. static GLubyte checkImage[checkImageHeight][checkImageWidth][4];  
  10. static GLubyte otherImage[checkImageHeight][checkImageWidth][4];  
  11.   
  12. static GLuint texName[2];  
  13.   
  14. void makeCheckImages(void)  
  15. {  
  16.    int i, j, c;  
  17.       
  18.    for (i = 0; i < checkImageHeight; i++) {  
  19.       for (j = 0; j < checkImageWidth; j++) {  
  20.          c = ((((i&0x8)==0)^((j&0x8))==0))*255;  
  21.          checkImage[i][j][0] = (GLubyte) c;  
  22.          checkImage[i][j][1] = (GLubyte) c;  
  23.          checkImage[i][j][2] = (GLubyte) c;  
  24.          checkImage[i][j][3] = (GLubyte) 255;  
  25.          c = ((((i&0x10)==0)^((j&0x10))==0))*255;  
  26.          otherImage[i][j][0] = (GLubyte) c;  
  27.          otherImage[i][j][1] = (GLubyte) 0;  
  28.          otherImage[i][j][2] = (GLubyte) 0;  
  29.          otherImage[i][j][3] = (GLubyte) 255;  
  30.       }  
  31.    }  
  32. }  
  33.   
  34. void init(void)  
  35. {      
  36.    glClearColor (0.0, 0.0, 0.0, 0.0);  
  37.    glShadeModel(GL_FLAT);  
  38.    glEnable(GL_DEPTH_TEST);  
  39.   
  40.    makeCheckImages();  
  41.    glPixelStorei(GL_UNPACK_ALIGNMENT, 1);  
  42.   
  43.    glGenTextures(2, texName);  
  44.    glBindTexture(GL_TEXTURE_2D, texName[0]);  
  45.    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);  
  46.    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);  
  47.    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER,   
  48.                    GL_NEAREST);  
  49.    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER,   
  50.                    GL_NEAREST);  
  51.    glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, checkImageWidth,  
  52.                 checkImageHeight, 0, GL_RGBA, GL_UNSIGNED_BYTE,  
  53.                 checkImage);  
  54.   
  55.    glBindTexture(GL_TEXTURE_2D, texName[1]);  
  56.    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);  
  57.    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);  
  58.    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);  
  59.    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);  
  60.    glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL);  
  61.    glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, checkImageWidth,   
  62.                 checkImageHeight, 0, GL_RGBA, GL_UNSIGNED_BYTE,   
  63.                 otherImage);  
  64.   
  65.    glEnable(GL_TEXTURE_2D);  
  66. }  
  67.   
  68. void display(void)  
  69. {  
  70.    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);  
  71.    glBindTexture(GL_TEXTURE_2D, texName[0]);  
  72.    glBegin(GL_QUADS);  
  73.    glTexCoord2f(0.0, 0.0); glVertex3f(-2.0, -1.0, 0.0);  
  74.    glTexCoord2f(0.0, 1.0); glVertex3f(-2.0, 1.0, 0.0);  
  75.    glTexCoord2f(1.0, 1.0); glVertex3f(0.0, 1.0, 0.0);  
  76.    glTexCoord2f(1.0, 0.0); glVertex3f(0.0, -1.0, 0.0);  
  77.    glEnd();  
  78.    glBindTexture(GL_TEXTURE_2D, texName[1]);  
  79.    glBegin(GL_QUADS);  
  80.    glTexCoord2f(0.0, 0.0); glVertex3f(1.0, -1.0, 0.0);  
  81.    glTexCoord2f(0.0, 1.0); glVertex3f(1.0, 1.0, 0.0);  
  82.    glTexCoord2f(1.0, 1.0); glVertex3f(2.41421, 1.0, -1.41421);  
  83.    glTexCoord2f(1.0, 0.0); glVertex3f(2.41421, -1.0, -1.41421);  
  84.    glEnd();  
  85.    glFlush();  
  86. }  
  87.   
  88. void reshape(int w, int h)  
  89. {  
  90.    glViewport(0, 0, (GLsizei) w, (GLsizei) h);  
  91.    glMatrixMode(GL_PROJECTION);  
  92.    glLoadIdentity();  
  93.    gluPerspective(60.0, (GLfloat) w/(GLfloat) h, 1.0, 30.0);  
  94.    glMatrixMode(GL_MODELVIEW);  
  95.    glLoadIdentity();  
  96.    glTranslatef(0.0, 0.0, -3.6);  
  97. }  
  98.   
  99. void keyboard(unsigned char key, int x, int y)  
  100. {  
  101.    switch (key) {  
  102.       case 27:  
  103.          exit(0);  
  104.          break;  
  105.    }  
  106. }  
  107.   
  108. int main(int argc, char** argv)  
  109. {  
  110.    glutInit(&argc, argv);  
  111.    glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB | GLUT_DEPTH);  
  112.    glutInitWindowSize(250, 250);  
  113.    glutInitWindowPosition(100, 100);  
  114.    glutCreateWindow(argv[0]);  
  115.    init();  
  116.    glutReshapeFunc(reshape);  
  117.    glutDisplayFunc(display);  
  118.    glutKeyboardFunc (keyboard);  
  119.    glutMainLoop();  
  120.    return 0;   
  121. }  
  122. #else  
  123. int main(int argc, char** argv)  
  124. {  
  125.     fprintf (stderr, "This program demonstrates a feature which is not in OpenGL Version 1.0.\n");  
  126.     fprintf (stderr, "If your implementation of OpenGL Version 1.0 has the right extensions,\n");  
  127.     fprintf (stderr, "you may be able to modify this program to make it run.\n");  
  128.     return 0;  
  129. }  
  130. #endif  

运行结果:


    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多