⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 readerwriterq3bsp.cpp

📁 最新osg包
💻 CPP
📖 第 1 页 / 共 2 页
字号:
            }        }          }  //int num_primitive_sets=geom->getNumPrimitiveSets();  //const osg::BoundingSphere& bs=map_geom->getBound();  map_geode->getOrCreateStateSet()->setMode(GL_LIGHTING, osg::StateAttribute::OFF);  return map_geode;}osg::Geometry* ReaderWriterQ3BSP::createMeshFace( const BSP_LOAD_FACE& aLoadFace,const std::vector<osg::Texture2D*>& aTextureArray,                                                  osg::Vec3Array& aVertexArray,std::vector<GLuint>& aIndices,                                                  osg::Vec2Array& aTextureDecalCoords,osg::Vec2Array& aTextureLMapCoords                                                ) const{  osg::Geometry* obj_geom = new osg::Geometry;  osg::Vec3Array* obj_vertex_array = new osg::Vec3Array(aLoadFace.m_numMeshIndices,                                                        &(aVertexArray)[aLoadFace.m_firstVertexIndex]                                                        );  obj_geom->setVertexArray(obj_vertex_array);  osg::DrawElementsUInt* face_indices = new osg::DrawElementsUInt(osg::PrimitiveSet::TRIANGLES,                                                  aLoadFace.m_numMeshIndices,                                                  &(aIndices)[0]+aLoadFace.m_firstMeshIndex                                                  );  obj_geom->addPrimitiveSet(face_indices);  osg::Texture2D *texture=aTextureArray[aLoadFace.m_texture];  if(texture)    {      osg::StateSet* stateset = obj_geom->getOrCreateStateSet();      stateset->setTextureAttributeAndModes(0,texture,osg::StateAttribute::ON);      stateset->setTextureAttributeAndModes(1,texture,osg::StateAttribute::ON);      osg::Vec2Array* obj_texcoords_array = new osg::Vec2Array(aLoadFace.m_numMeshIndices,                                                        &(aTextureDecalCoords)[aLoadFace.m_firstVertexIndex]                                                        );      obj_geom->setTexCoordArray(0,obj_texcoords_array);      osg::Vec2Array* obj_lmapcoords_array = new osg::Vec2Array(aLoadFace.m_numMeshIndices,                                                        &(aTextureLMapCoords)[aLoadFace.m_firstVertexIndex]                                                        );      obj_geom->setTexCoordArray(1,obj_lmapcoords_array);    }  return obj_geom;     }osg::Geometry* ReaderWriterQ3BSP::createPolygonFace(const BSP_LOAD_FACE& aLoadFace,const std::vector<osg::Texture2D*>& aTextureArray,const std::vector<osg::Texture2D*>& aTextureLMapArray,                                  osg::Vec3Array& aVertexArray,                                  osg::Vec2Array& aTextureDecalCoords,osg::Vec2Array& aTextureLMapCoords                                 ) const{  osg::Texture2D *texture=aTextureArray[aLoadFace.m_texture];  osg::Geometry* polygon_geom = new osg::Geometry;  polygon_geom->setVertexArray(&aVertexArray);  polygon_geom->setTexCoordArray(0, &aTextureDecalCoords);  polygon_geom->setTexCoordArray(1, &aTextureLMapCoords);  osg::DrawArrays* face_indices = new osg::DrawArrays(osg::PrimitiveSet::TRIANGLE_FAN,                                                      aLoadFace.m_firstVertexIndex,                                                      aLoadFace.m_numVertices                                                     );  osg::StateSet* stateset = polygon_geom->getOrCreateStateSet();  if(texture)    {      stateset->setTextureAttributeAndModes(0,texture,osg::StateAttribute::ON);      if(aLoadFace.m_lightmapIndex>=0)        {          texture=aTextureLMapArray[aLoadFace.m_lightmapIndex];          if(texture)            {              stateset->setTextureAttributeAndModes(1,texture,osg::StateAttribute::ON);            }        }      else        {          texture=aTextureLMapArray[aTextureLMapArray.size()-1];          if(texture)            {              stateset->setTextureAttributeAndModes(1,texture,osg::StateAttribute::ON);            }        }    }  else    {      osg::PolygonMode* polygon_mode=new osg::PolygonMode;      polygon_mode->setMode(osg::PolygonMode::FRONT_AND_BACK,osg::PolygonMode::LINE);      stateset->setAttributeAndModes(polygon_mode, osg::StateAttribute::ON);    }  polygon_geom->addPrimitiveSet(face_indices);  return polygon_geom;}bool ReaderWriterQ3BSP::loadTextures(const BSPLoad& aLoadData,std::vector<osg::Texture2D*>& aTextureArray) const{  int num_textures=aLoadData.m_loadTextures.size();  int i;  for(i=0;i<num_textures;i++)    {      //add file extension to the name      std::string tgaExtendedName(aLoadData.m_loadTextures[i].m_name);      tgaExtendedName+=".tga";      std::string jpgExtendedName(aLoadData.m_loadTextures[i].m_name);      jpgExtendedName+=".jpg";      osg::Image* image = osgDB::readImageFile(tgaExtendedName);      if (!image)        {          image = osgDB::readImageFile(jpgExtendedName);          if (!image)            {              aTextureArray.push_back(NULL);              continue; //?            }        }      osg::Texture2D* texture= new osg::Texture2D;      texture->setImage(image);      texture->setDataVariance(osg::Object::DYNAMIC); // protect from being optimized away as static state.      texture->setWrap(osg::Texture2D::WRAP_S, osg::Texture2D::REPEAT);      texture->setWrap(osg::Texture2D::WRAP_T, osg::Texture2D::REPEAT);      aTextureArray.push_back(texture);    }  return true;}bool ReaderWriterQ3BSP::loadLightMaps(const BSPLoad& aLoadData,std::vector<osg::Texture2D*>& aTextureArray) const{  int num_textures=aLoadData.m_loadLightmaps.size();  int i;  for(i=0;i<num_textures;i++)    {      osg::Image* image=new osg::Image;      unsigned char *data=new unsigned char[128*128*3];      memcpy(data,aLoadData.m_loadLightmaps[i].m_lightmapData,128*128*3);      image->setImage(128,128,1,GL_RGBA8,GL_RGB,GL_UNSIGNED_BYTE,data,osg::Image::USE_NEW_DELETE);      osg::Texture2D* texture= new osg::Texture2D;      texture->setImage(image);      texture->setDataVariance(osg::Object::DYNAMIC); // protect from being optimized away as static state.      texture->setFilter(osg::Texture2D::MIN_FILTER,osg::Texture2D::LINEAR_MIPMAP_LINEAR);      texture->setFilter(osg::Texture2D::MAG_FILTER,osg::Texture2D::LINEAR);      texture->setWrap(osg::Texture2D::WRAP_S, osg::Texture2D::REPEAT);      texture->setWrap(osg::Texture2D::WRAP_T, osg::Texture2D::REPEAT);      aTextureArray.push_back(texture);    }  // A continuaci髇, a馻do el blanco  osg::Image* image=new osg::Image;  unsigned char *data=new unsigned char[3];  for(int whiteidx=0;whiteidx<3;whiteidx++)  {      data[whiteidx]=255;  }  image->setImage(1,1,1,GL_RGBA8,GL_RGB,GL_UNSIGNED_BYTE,data,osg::Image::USE_NEW_DELETE);  osg::Texture2D* texture= new osg::Texture2D;  texture->setImage(image);  texture->setDataVariance(osg::Object::DYNAMIC); // protect from being optimized away as static state.  texture->setFilter(osg::Texture2D::MIN_FILTER,osg::Texture2D::LINEAR_MIPMAP_LINEAR);  texture->setFilter(osg::Texture2D::MAG_FILTER,osg::Texture2D::LINEAR);  texture->setWrap(osg::Texture2D::WRAP_S, osg::Texture2D::REPEAT);  texture->setWrap(osg::Texture2D::WRAP_T, osg::Texture2D::REPEAT);  aTextureArray.push_back(texture);  return true;}//Tessellate a biquadratic patchbool BSP_BIQUADRATIC_PATCH::Tessellate(int newTessellation,osg::Geometry* aGeometry){    m_tessellation=newTessellation;    float px, py;    BSP_VERTEX temp[3];    m_vertices.resize((m_tessellation+1)*(m_tessellation+1));    for(int v=0; v<=m_tessellation; ++v)      {        px=(float)v/m_tessellation;        m_vertices[v]=m_controlPoints[0]*((1.0f-px)*(1.0f-px))+                      m_controlPoints[3]*((1.0f-px)*px*2)+                      m_controlPoints[6]*(px*px);      }    for(int u=1; u<=m_tessellation; ++u)      {        py=(float)u/m_tessellation;        temp[0]=m_controlPoints[0]*((1.0f-py)*(1.0f-py))+                m_controlPoints[1]*((1.0f-py)*py*2)+                m_controlPoints[2]*(py*py);        temp[1]=m_controlPoints[3]*((1.0f-py)*(1.0f-py))+                m_controlPoints[4]*((1.0f-py)*py*2)+                m_controlPoints[5]*(py*py);        temp[2]=m_controlPoints[6]*((1.0f-py)*(1.0f-py))+                m_controlPoints[7]*((1.0f-py)*py*2)+                m_controlPoints[8]*(py*py);        for(int v=0; v<=m_tessellation; ++v)          {            px=(float)v/m_tessellation;            m_vertices[u*(m_tessellation+1)+v]=temp[0]*((1.0f-px)*(1.0f-px))+                                              temp[1]*((1.0f-px)*px*2)+                                              temp[2]*(px*px);          }      }    //Create indices    m_indices.resize(m_tessellation*(m_tessellation+1)*2);    int row;    for(row=0; row<m_tessellation; ++row)      {        for(int point=0; point<=m_tessellation; ++point)          {            //calculate indices            //reverse them to reverse winding            m_indices[(row*(m_tessellation+1)+point)*2+1]= row*(m_tessellation+1)+point;            m_indices[(row*(m_tessellation+1)+point)*2]=  (row+1)*(m_tessellation+1)+point;          }      }    //Fill in the arrays for multi_draw_arrays    m_trianglesPerRow.resize(m_tessellation);    m_rowIndexPointers.resize(m_tessellation);    for(row=0; row<m_tessellation; ++row)      {        m_trianglesPerRow[row]=2*(m_tessellation+1);        m_rowIndexPointers[row]=&m_indices[row*2*(m_tessellation+1)];      }    osg::Vec3Array* patch_vertex_array = new osg::Vec3Array( (m_tessellation+1)*(m_tessellation+1) );    osg::Vec2Array* patch_textcoord_array = new osg::Vec2Array( (m_tessellation+1)*(m_tessellation+1) );    osg::Vec2Array* patch_lmapcoord_array = new osg::Vec2Array( (m_tessellation+1)*(m_tessellation+1) );    for(int i=0;i<(m_tessellation+1)*(m_tessellation+1);i++)      {        (*patch_vertex_array)[i].set( m_vertices[ i ].m_position[0],                                      m_vertices[ i ].m_position[1],                                      m_vertices[ i ].m_position[2]                                    );        (*patch_textcoord_array)[i].set( m_vertices[ i ].m_decalS,                                         m_vertices[ i ].m_decalT                                       );        (*patch_lmapcoord_array)[i].set( m_vertices[ i ].m_lightmapS,                                         m_vertices[ i ].m_lightmapT                                       );      }    aGeometry->setVertexArray(patch_vertex_array);    aGeometry->setTexCoordArray(0,patch_textcoord_array);    aGeometry->setTexCoordArray(1,patch_lmapcoord_array);    for(row=0; row<m_tessellation; ++row)      {        osg::DrawElementsUInt* face_indices = new osg::DrawElementsUInt(osg::PrimitiveSet::TRIANGLE_STRIP,                                                        m_tessellation*(m_tessellation+1)*2,                                                        &m_indices[0]                                                        );        aGeometry->addPrimitiveSet(face_indices);      }    return true;}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -