資源描述:
《OpenGL紋理貼圖簡單例子.doc》由會員上傳分享,免費在線閱讀,更多相關內(nèi)容在教育資源-天天文庫。
1、【代碼】#include#include#includestaticdoubleX=0;staticdoubleY=0;staticdoubleZ=7;//定義函數(shù)指針,在Windows環(huán)境下使用OpenGL擴展PFNGLACTIVETEXTUREARBPROCglActiveTextureARB=NULL;PFNGLMULTITEXCOORD2FARBPROCglMultiTexCoord2fARB=NULL;//紋理存儲數(shù)組staticG
2、LubyteT0[64][64][4];staticGLubyteT1[64][64][4];staticGLubyteT2[64][64][4];//紋理名字staticGLuintTname0;staticGLuintTname1;staticGLuintTname2;//創(chuàng)建紋理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(){//獲取函數(shù)地址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);//對其像素字節(jié)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);//設置紋理對象內(nèi)容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