OpenGL 多重纹理示例代码

类别:编程语言 点击:0 评论:0 推荐:

多重纹理理论不作介绍,我也不清楚底层实现,nehe的opengl教程中也有示例。毕业课题中涉及多重纹理的使用,我只是粘贴部分实现代码,对于一些人来说,这已经足够了,:)

我的显卡是gf5700,查询arb扩展,最多支持4个纹理,所以每个pass也就最多加载4个纹理,我把纹理送入显卡,由Cg操纵纹理,所以,下边代码没有过多对纹理的操作,

#include <gl/gl.h>
#include <gl/glu.h>
#include "gl/glext.h"  //glext.h需要另外去下载

//定义extensions入口宏
#define INIT_ENTRY_POINT( funcname, type )     \
 static type funcname= (type) wglGetProcAddress(#funcname);   \
 if ( !funcname ){          \
 TRACE2(_T("!! Fail to Get Proc address: "#funcname \
 " (%s,%d)\n"),__FILE__,__LINE__);      \
 }   //Mac end

,,,

//检测一下显卡对多重纹理的支持情况

 int nUnits,nSize;
 glGetIntegerv(GL_MAX_TEXTURE_UNITS_ARB, &nUnits);
 glGetIntegerv(GL_MAX_RECTANGLE_TEXTURE_SIZE_NV, &nSize);
 TRACE2("GL_MAX_TEXTURE_UNITS_ARB =%d ,GL_MAX_RECTANGLE_TEXTURE_SIZE_NV =%d \n",nUnits,nSize);

定义纹理,我在里边使用了矩形纹理,这样纹理坐标的范围就不是[0,1],而是[0,w],[0,h],它的使用也很简单,以后有空在说一些它的东西,

  glGenTextures(1,&param.nVerTex);
 glBindTexture(GL_TEXTURE_RECTANGLE_NV,param.nVerTex);
 glTexImage2D(GL_TEXTURE_RECTANGLE_NV,0,GL_RGB, param.sizeVertexTex.cx, param.sizeVertexTex.cy,0,GL_RGB,GL_FLOAT,param.pfVertexTex);
 glTexParameteri(GL_TEXTURE_RECTANGLE_NV,GL_TEXTURE_MAG_FILTER,GL_NEAREST);
 glTexParameteri(GL_TEXTURE_RECTANGLE_NV,GL_TEXTURE_MIN_FILTER,GL_NEAREST);
 glTexParameteri(GL_TEXTURE_RECTANGLE_NV,GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
 glTexParameteri(GL_TEXTURE_RECTANGLE_NV,GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
,,,

创建物体,贴上纹理,

 INIT_ENTRY_POINT(
  glActiveTextureARB, PFNGLACTIVETEXTUREARBPROC );
 INIT_ENTRY_POINT(
  glMultiTexCoord2fARB, PFNGLMULTITEXCOORD2FARBPROC );

 //绑定纹理
 glActiveTextureARB(GL_TEXTURE0_ARB);
 glEnable(GL_TEXTURE_RECTANGLE_NV);
 glBindTexture(GL_TEXTURE_RECTANGLE_NV,pTexA->nIdxTex);
 
 glActiveTextureARB(GL_TEXTURE1_ARB);
 glEnable(GL_TEXTURE_RECTANGLE_NV);
 glBindTexture(GL_TEXTURE_RECTANGLE_NV,pTexA->nVerTex);

 glActiveTextureARB(GL_TEXTURE2_ARB);
 glEnable(GL_TEXTURE_RECTANGLE_NV);
 glBindTexture(GL_TEXTURE_RECTANGLE_NV,pTexB->nIdxTex);

 glActiveTextureARB(GL_TEXTURE3_ARB);
 glEnable(GL_TEXTURE_RECTANGLE_NV);
 glBindTexture(GL_TEXTURE_RECTANGLE_NV,pTexB->nVerTex);
 
 glBegin(GL_QUADS);

 //左下
 glMultiTexCoord2fARB(GL_TEXTURE0_ARB,0,0);
 glMultiTexCoord2fARB(GL_TEXTURE1_ARB,0,0);
 glMultiTexCoord2fARB(GL_TEXTURE2_ARB,0,0);
 glMultiTexCoord2fARB(GL_TEXTURE3_ARB,0,0);
 glVertex3f(0.0f,0.0f,-10.0f);
 //右下
 glMultiTexCoord2fARB(GL_TEXTURE0_ARB,50,0);
 glMultiTexCoord2fARB(GL_TEXTURE1_ARB,100.0f,0.0f);
 glMultiTexCoord2fARB(GL_TEXTURE2_ARB,10.0f,0.0f);
 glMultiTexCoord2fARB(GL_TEXTURE3_ARB,10.0f,0.0f);
 glVertex3f(100.0f,0.0f,-10.0f);
 //右上
 glMultiTexCoord2fARB(GL_TEXTURE0_ARB,50,50);
 glMultiTexCoord2fARB(GL_TEXTURE1_ARB,10.0f,10.0f);
 glMultiTexCoord2fARB(GL_TEXTURE2_ARB,10.0f,1.0f);
 glMultiTexCoord2fARB(GL_TEXTURE3_ARB,10.0f,1.0f);
 glVertex3f(100.0f,100.0f,-10.0f);
 //左上
 glMultiTexCoord2fARB(GL_TEXTURE0_ARB,0,50);
 glMultiTexCoord2fARB(GL_TEXTURE1_ARB,0.0f,10.0f);
 glMultiTexCoord2fARB(GL_TEXTURE2_ARB,0.0f,10.0f);
 glMultiTexCoord2fARB(GL_TEXTURE3_ARB,0.0f,10.0f);
 glVertex3f(0.0f,100.0f,-10.0f);

 glEnd();

 glFlush();

 glActiveTextureARB(GL_TEXTURE0_ARB);
 glDisable(GL_TEXTURE_RECTANGLE_NV);
 
 glActiveTextureARB(GL_TEXTURE1_ARB);
 glDisable(GL_TEXTURE_RECTANGLE_NV);

 glActiveTextureARB(GL_TEXTURE2_ARB);
 glDisable(GL_TEXTURE_RECTANGLE_NV);

 glActiveTextureARB(GL_TEXTURE3_ARB);
 glDisable(GL_TEXTURE_RECTANGLE_NV);

本文地址:http://com.8s8s.com/it/it27513.htm