科技行者

行者学院 转型私董会 科技行者专题报道 网红大战科技行者

知识库

知识库 安全导航

至顶网软件频道基础软件第二人生的源码分析(十八)人物纹理显示的实现

第二人生的源码分析(十八)人物纹理显示的实现

  • 扫一扫
    分享文章到微信

  • 扫一扫
    关注官方公众号
    至顶头条

第二人生的源码分析(十八)人物纹理显示的实现

作者:蔡军生 来源:CSDN 2008年1月21日

关键字: 显示 人物纹理

  • 评论
  • 分享微博
  • 分享邮件

上面已经介绍怎么样显示Mesh数据,也就是人物模型已经显示出来,那么同样的模型怎么样构造出各种各样的人物来呢?在第二人生里是使用各种各样的纹理图片来实现的。下面就来仔细地分析这段代码,如下:

#001  U32 LLViewerJointMesh::drawShape( F32 pixelArea, BOOL first_pass)

#002  {

#003       if (!mValid || !mMesh || !mFace || !mVisible ||

#004              mFace->mVertexBuffer.isNull() ||

#005              mMesh->getNumFaces() == 0)

#006       {

#007              return 0;

#008       }

#009 

这段代码检查Mesh是否有效。

 

 

#010       U32 triangle_count = 0;

#011 

#012       stop_glerror();

#013      

#014       //----------------------------------------------------------------

#015       // setup current color

#016       //----------------------------------------------------------------

#017       if (!gRenderForSelect)

#018       {

#019              if ((mFace->getPool()->getVertexShaderLevel() > 0))

#020              {

#021                     glColor4f(0,0,0,1);

#022                    

#023                     if (gMaterialIndex > 0)

#024                     {

#025                            glVertexAttrib4fvARB(gMaterialIndex, mColor.mV);

#026                     }

#027                    

#028                     if (mShiny && gSpecularIndex > 0)

#029                     {

#030                            glVertexAttrib4fARB(gSpecularIndex, 1,1,1,1);

#031                     }

#032              }

#033              else

#034              {

#035                     glColor4fv(mColor.mV);

#036              }

#037       }

#038 

这段代码设置人物材料的颜色。

 

 

 

#039       stop_glerror();

#040      

#041       LLGLSSpecular specular(LLColor4(1.f,1.f,1.f,1.f), gRenderForSelect ? 0.0f : mShiny && !(mFace->getPool()->getVertexShaderLevel() > 0));

#042 

#043       LLGLEnable texture_2d((gRenderForSelect && isTransparent()) ? GL_TEXTURE_2D : 0);

#044      

#045       //----------------------------------------------------------------

#046       // setup current texture

#047       //----------------------------------------------------------------

#048       llassert( !(mTexture.notNull() && mLayerSet) );  // mutually exclusive

#049 

#050       if (mTestImageName)

#051       {

#052              LLImageGL::bindExternalTexture( mTestImageName, 0, GL_TEXTURE_2D );

#053 

#054              if (mIsTransparent)

#055              {

#056                     glColor4f(1.f, 1.f, 1.f, 1.f);

#057              }

#058              else

#059              {

#060                     glColor4f(0.7f, 0.6f, 0.3f, 1.f);

#061                     glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE,           GL_COMBINE_ARB);

#062                     glTexEnvi(GL_TEXTURE_ENV, GL_COMBINE_RGB_ARB,         GL_INTERPOLATE_ARB);

#063 

#064                     glTexEnvi(GL_TEXTURE_ENV, GL_SOURCE0_RGB_ARB,             GL_PREVIOUS_ARB);

#065                     glTexEnvi(GL_TEXTURE_ENV, GL_OPERAND0_RGB_ARB,          GL_SRC_COLOR);

#066 

#067                     glTexEnvi(GL_TEXTURE_ENV, GL_SOURCE1_RGB_ARB,             GL_TEXTURE);

#068                     glTexEnvi(GL_TEXTURE_ENV, GL_OPERAND1_RGB_ARB,          GL_SRC_COLOR);

#069                    

#070                     glTexEnvi(GL_TEXTURE_ENV, GL_SOURCE2_RGB_ARB,             GL_TEXTURE);

#071                     glTexEnvi(GL_TEXTURE_ENV, GL_OPERAND2_RGB_ARB,     GL_ONE_MINUS_SRC_ALPHA);

#072              }

#073       }

#074       else if( mLayerSet )

#075       {

#076              if(    mLayerSet->hasComposite() )

#077              {

#078                     mLayerSet->getComposite()->bindTexture();

#079              }

#080              else

#081              {

#082                     llwarns << "Layerset without composite" << llendl;

#083                     gImageList.getImage(IMG_DEFAULT)->bind();

#084              }

#085       }

#086       else

#087       if ( mTexture.notNull() )

#088       {

#089              mTexture->bind();

#090              if (!mTexture->getClampS()) {

#091                     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);

#092              }

#093              if (!mTexture->getClampT()) {

#094                     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);

#095              }

#096       }

#097       else

#098       {

#099              gImageList.getImage(IMG_DEFAULT_AVATAR)->bind();

#100       }

#101      

#102       LLGLDisable tex(gRenderForSelect && !isTransparent() ? GL_TEXTURE_2D : 0);

#103 

#104       if (gRenderForSelect)

#105       {

#106              if (isTransparent())

#107              {

#108                     glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE,           GL_COMBINE_ARB);

#109                     glTexEnvi(GL_TEXTURE_ENV, GL_COMBINE_RGB_ARB,              GL_REPLACE);

#110                     glTexEnvi(GL_TEXTURE_ENV, GL_COMBINE_ALPHA_ARB,          GL_MODULATE);

#111 

#112                     glTexEnvi(GL_TEXTURE_ENV, GL_SOURCE0_RGB_ARB,             GL_PREVIOUS_ARB);

#113                     glTexEnvi(GL_TEXTURE_ENV, GL_OPERAND0_RGB_ARB,          GL_SRC_COLOR);

#114 

#115                     glTexEnvi(GL_TEXTURE_ENV, GL_SOURCE0_ALPHA_ARB,         GL_TEXTURE);  // GL_TEXTURE_ENV_COLOR is

#116  set in renderPass1

#117                     glTexEnvi(GL_TEXTURE_ENV, GL_OPERAND0_ALPHA_ARB,       GL_SRC_ALPHA);

#118              }

#119       }

#120       else

#121       {

#122              //----------------------------------------------------------------

#123              // by default, backface culling is enabled

#124              //----------------------------------------------------------------

#125              /*if (sRenderPass == AVATAR_RENDER_PASS_CLOTHING_INNER)

#126              {

#127                     LLImageGL::bindExternalTexture( sClothingMaskImageName, 1, GL_TEXTURE_2D );

#128 

#129                     glClientActiveTextureARB(GL_TEXTURE0_ARB);

#130                     glActiveTextureARB(GL_TEXTURE0_ARB);

#131                     glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE,           GL_COMBINE_ARB);

#132                     glTexEnvi(GL_TEXTURE_ENV, GL_COMBINE_RGB_ARB,              GL_MODULATE);

#133                     glTexEnvi(GL_TEXTURE_ENV, GL_COMBINE_ALPHA_ARB,          GL_REPLACE);

#134 

#135                     glTexEnvi(GL_TEXTURE_ENV, GL_SOURCE0_RGB_ARB,             GL_TEXTURE);

#136                     glTexEnvi(GL_TEXTURE_ENV, GL_OPERAND0_RGB_ARB,          GL_SRC_COLOR);

#137 

#138                     glTexEnvi(GL_TEXTURE_ENV, GL_SOURCE0_ALPHA_ARB,    GL_PRIMARY_COLOR_ARB);

#139                     glTexEnvi(GL_TEXTURE_ENV, GL_OPERAND0_ALPHA_ARB,       GL_SRC_ALPHA);

#140 

#141                     glTexEnvi(GL_TEXTURE_ENV, GL_SOURCE1_RGB_ARB,        GL_PRIMARY_COLOR_ARB);

#142                     glTexEnvi(GL_TEXTURE_ENV, GL_OPERAND1_RGB_ARB,          GL_SRC_COLOR);

#143 

#144                     glClientActiveTextureARB(GL_TEXTURE1_ARB);

#145                     glEnable(GL_TEXTURE_2D); // Texture unit 1

#146                     glActiveTextureARB(GL_TEXTURE1_ARB);

#147                     glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE,           GL_COMBINE_ARB);

#148                     glTexEnvfv(GL_TEXTURE_ENV, GL_TEXTURE_ENV_COLOR,       sClothingInnerColor.mV);

#149                     glTexEnvi(GL_TEXTURE_ENV, GL_COMBINE_RGB_ARB,         GL_INTERPOLATE_ARB);

#150                     glTexEnvi(GL_TEXTURE_ENV, GL_COMBINE_ALPHA_ARB,          GL_REPLACE);

#151 

#152                     glTexEnvi(GL_TEXTURE_ENV, GL_SOURCE0_ALPHA_ARB,         GL_PREVIOUS_ARB);

#153                     glTexEnvi(GL_TEXTURE_ENV, GL_OPERAND0_ALPHA_ARB,       GL_SRC_ALPHA);

#154 

#155                     glTexEnvi(GL_TEXTURE_ENV, GL_SOURCE0_RGB_ARB,             GL_CONSTANT_ARB);

#156                     glTexEnvi(GL_TEXTURE_ENV, GL_OPERAND0_RGB_ARB,          GL_SRC_COLOR);

#157 

#158                     glTexEnvi(GL_TEXTURE_ENV, GL_SOURCE1_RGB_ARB,             GL_PREVIOUS_ARB);

#159                     glTexEnvi(GL_TEXTURE_ENV, GL_OPERAND1_RGB_ARB,          GL_SRC_COLOR);

#160 

#161                     glTexEnvi(GL_TEXTURE_ENV, GL_SOURCE2_RGB_ARB,             GL_TEXTURE);

#162                     glTexEnvi(GL_TEXTURE_ENV, GL_OPERAND2_RGB_ARB,          GL_SRC_ALPHA);

#163              }

#164              else if (sRenderPass == AVATAR_RENDER_PASS_CLOTHING_OUTER)

#165              {

#166                     glAlphaFunc(GL_GREATER, 0.1f);

#167                     LLImageGL::bindExternalTexture( sClothingMaskImageName, 1, GL_TEXTURE_2D );

#168 

#169                     glClientActiveTextureARB(GL_TEXTURE0_ARB);

#170                     glActiveTextureARB(GL_TEXTURE0_ARB);

#171                     glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE,           GL_COMBINE_ARB);

#172                     glTexEnvi(GL_TEXTURE_ENV, GL_COMBINE_RGB_ARB,              GL_MODULATE);

#173                     glTexEnvi(GL_TEXTURE_ENV, GL_COMBINE_ALPHA_ARB,          GL_REPLACE);

#174 

#175                     glTexEnvi(GL_TEXTURE_ENV, GL_SOURCE0_RGB_ARB,             GL_TEXTURE);

#176                     glTexEnvi(GL_TEXTURE_ENV, GL_OPERAND0_RGB_ARB,          GL_SRC_COLOR);

#177 

#178                     glTexEnvi(GL_TEXTURE_ENV, GL_SOURCE0_ALPHA_ARB,    GL_PRIMARY_COLOR_ARB);

#179                     glTexEnvi(GL_TEXTURE_ENV, GL_OPERAND0_ALPHA_ARB,       GL_SRC_ALPHA);

#180 

#181                     glTexEnvi(GL_TEXTURE_ENV, GL_SOURCE1_RGB_ARB,        GL_PRIMARY_COLOR_ARB);

#182                     glTexEnvi(GL_TEXTURE_ENV, GL_OPERAND1_RGB_ARB,          GL_SRC_COLOR);

#183 

#184                     glClientActiveTextureARB(GL_TEXTURE1_ARB);

#185                     glEnable(GL_TEXTURE_2D); // Texture unit 1

#186                     glActiveTextureARB(GL_TEXTURE1_ARB);

#187                     glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE,           GL_COMBINE_ARB);

#188                     glTexEnvi(GL_TEXTURE_ENV, GL_COMBINE_RGB_ARB,              GL_REPLACE);

#189                     glTexEnvi(GL_TEXTURE_ENV, GL_COMBINE_ALPHA_ARB,          GL_MODULATE);

#190 

#191                     glTexEnvi(GL_TEXTURE_ENV, GL_SOURCE0_RGB_ARB,             GL_PREVIOUS_ARB);

#192                     glTexEnvi(GL_TEXTURE_ENV, GL_OPERAND0_RGB_ARB,          GL_SRC_COLOR);

#193 

#194                     glTexEnvi(GL_TEXTURE_ENV, GL_SOURCE0_ALPHA_ARB,         GL_TEXTURE);

#195                     glTexEnvi(GL_TEXTURE_ENV, GL_OPERAND0_ALPHA_ARB,       GL_SRC_ALPHA);

#196              }*/

#197       }

#198 

这段代码实现纹理图片的设置和纹理参数配置。

 

 

 

#199       mFace->mVertexBuffer->setBuffer(sRenderMask);

#200 

#201       U32 start = mMesh->mFaceVertexOffset;

#202       U32 end = start + mMesh->mFaceVertexCount - 1;

#203       U32 count = mMesh->mFaceIndexCount;

#204       U32* indicesp = ((U32*) mFace->mVertexBuffer->getIndicesPointer()) + mMesh->mFaceIndexOffset;

#205 

#206       if (mMesh->hasWeights())

#207       {

#208              if ((mFace->getPool()->getVertexShaderLevel() > 0))

#209              {

#210                     if (first_pass)

#211                     {

#212                            uploadJointMatrices();

#213                     }

#214                     llDrawRangeElements(GL_TRIANGLES, start, end, count, GL_UNSIGNED_INT, indicesp);

#215              }

#216              else

#217              {

#218                     llDrawRangeElements(GL_TRIANGLES, start, end, count, GL_UNSIGNED_INT, indicesp);

#219              }

#220       }

#221       else

#222       {

#223              glPushMatrix();

#224              LLMatrix4 jointToWorld = getWorldMatrix();

#225              glMultMatrixf((GLfloat*)jointToWorld.mMatrix);

#226              llDrawRangeElements(GL_TRIANGLES, start, end, count, GL_UNSIGNED_INT, indicesp);

#227              glPopMatrix();

#228       }

#229 

#230       triangle_count += mMesh->mFaceIndexCount;

#231      

#232       if (mTestImageName)

#233       {

#234              glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);

#235       }

#236 

#237       /*if (sRenderPass != AVATAR_RENDER_PASS_SINGLE)

#238       {

#239              LLImageGL::unbindTexture(1, GL_TEXTURE_2D);

#240              glActiveTextureARB(GL_TEXTURE1_ARB);

#241              glTexEnvi(GL_TEXTURE_ENV, GL_COMBINE_RGB_ARB,              GL_MODULATE);

#242 

#243              // Return to the default texture.

#244              LLImageGL::unbindTexture(0, GL_TEXTURE_2D);

#245              glClientActiveTextureARB(GL_TEXTURE0_ARB);

#246              glActiveTextureARB(GL_TEXTURE0_ARB);

#247 

#248              glTexEnvi(GL_TEXTURE_ENV, GL_COMBINE_RGB_ARB,              GL_MODULATE);

#249              glAlphaFunc(GL_GREATER, 0.01f);

#250       }*/

#251 

#252       if (mTexture.notNull()) {

#253              if (!mTexture->getClampS()) {

#254                     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);

#255              }

#256              if (!mTexture->getClampT()) {

#257                     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);

#258              }

#259       }

#260 

#261       return triangle_count;

#262  }

最后一段代码实现Mesh数据的显示。

在这个函数里调用下面的函数来绑定纹理到特定的阶段显示:

#001  BOOL LLImageGL::bind(const S32 stage) const

#002  {

#003       if (stage == -1)

#004       {

#005              return FALSE;

#006       }

#007       BOOL res = bindTextureInternal(stage);

#008       //llassert(res);

#009       return res;

#010  }

在函数bindTextureInternal里调用OpenGL函数设置特定的纹理图片。下面是一个中国人在第二人生里扮演的角色,很有特色的,如下图:

 

查看本文来源
    • 评论
    • 分享微博
    • 分享邮件
    邮件订阅

    如果您非常迫切的想了解IT领域最新产品与技术信息,那么订阅至顶网技术邮件将是您的最佳途径之一。

    重磅专题
    往期文章
    最新文章