资源描述:
《OpenGL纹理贴图简单例子.doc》由会员上传分享,免费在线阅读,更多相关内容在教育资源-天天文库。
1、【代码】#include#include#includestaticdoubleX=0;staticdoubleY=0;staticdoubleZ=7;//定义函数指针,在Windows环境下使用OpenGL扩展PFNGLACTIVETEXTUREARBPROCglActiveTextureARB=NULL;PFNGLMULTITEXCOORD2FARBPROCglMultiTexCoord2fARB=NULL;//纹理存储数组staticG
2、LubyteT0[64][64][4];staticGLubyteT1[64][64][4];staticGLubyteT2[64][64][4];//纹理名字staticGLuintTname0;staticGLuintTname1;staticGLuintTname2;//创建纹理voidmakeImages(){inti,j,c;//第一个纹理,渐变绿色,*64for(i=0;i<64;i++){for(j=0;j<64;j++){T0[i][j][0]=(GLubyte)0;T0[i][j][1]=(
3、GLubyte)i*5+j*5;T0[i][j][2]=(GLubyte)0;T0[i][j][3]=(GLubyte)255;}}//第二个纹理,黑白相间横条,*64for(i=0;i<64;i++){for(j=0;j<64;j++){if(i%16<8)c=255;elsec=0;T1[i][j][0]=(GLubyte)c;T1[i][j][1]=(GLubyte)c;T1[i][j][2]=(GLubyte)c;T1[i][j][3]=(GLubyte)255;}}//第三个纹理,黑白相间竖条,*6
4、4for(i=0;i<64;i++){for(j=0;j<64;j++){if(j%16<8)c=255;elsec=0;T2[i][j][0]=(GLubyte)c;T2[i][j][1]=(GLubyte)c;T2[i][j][2]=(GLubyte)c;T2[i][j][3]=(GLubyte)255;}}}voidinit(){//获取函数地址glActiveTextureARB=(PFNGLACTIVETEXTUREARBPROC)wglGetProcAddress("glActiveTexture
5、ARB");glMultiTexCoord2fARB=(PFNGLMULTITEXCOORD2FARBPROC)wglGetProcAddress("glMultiTexCoord2fARB");//场景初始化glClearColor(0.0,0.0,0.0,0.0);glShadeModel(GL_FLAT);glEnable(GL_DEPTH_TEST);makeImages();//纹理初始化glPixelStorei(GL_UNPACK_ALIGNMENT,1);//对其像素字节glGenTextur
6、es(1,&Tname0);//获取纹理名字glGenTextures(1,&Tname1);//获取纹理名字glGenTextures(1,&Tname2);//获取纹理名字//初始化纹理glBindTexture(GL_TEXTURE_2D,Tname0);//生成指定名字的纹理对象glTexImage2D(GL_TEXTURE_2D,0,GL_RGBA,64,64,0,GL_RGBA,GL_UNSIGNED_BYTE,T0);//设置纹理对象内容glTexParameteri(GL_TEXTURE_2D
7、,GL_TEXTURE_WRAP_S,GL_REPEAT);//设置纹理过滤方式glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_WRAP_T,GL_REPEAT);//设置纹理过滤方式glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);//设置纹理过滤方式glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);//设置纹理过滤方式//
8、同上,初始化纹理glBindTexture(GL_TEXTURE_2D,Tname1);glTexImage2D(GL_TEXTURE_2D,0,GL_RGBA,64,64,0,GL_RGBA,GL_UNSIGNED_BYTE,T1);glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_WRAP_S,GL_REPEAT);glTexParameteri(GL_TEXTU