首先这个在android上是支持的
其次,想用文件名加坐标加trans,是不行的,会提示动态链接缺少,osgdb方面的
所以只有用变换节点来实现
以下是小米手机,400个3m的模型,压力不大,纹理没渲染出来,如果纹理渲染出来了,应该开始卡了。
奉献一个代码
#pragma once
//Standard libraries
#include "JNIUtils.h"
#include <string>
#include <sstream>
#include <osg/Vec3>
#include <osg/Node>
#include <osg/Group>
#include <osg/MatrixTransform>
#include <osg/PagedLOD>
#include <osgDB/ReadFile>
struct PageLODFileRang
{
std::string fileName;
float minDistance;
float maxDistance;
public :
PageLODFileRang(const std::string& file,const float& min,const float& max)
{
fileName = file;
minDistance = min;
maxDistance = max;
}
};
class XPageLODGroup:
public osg::Group
{
private:
std::vector <PageLODFileRang> m_pageLODFileData;
std::vector<osg::Vec3> m_positionList;
public:
XPageLODGroup();
XPageLODGroup( const std::vector<PageLODFileRang>& pageLODFileData, const std::vector<osg::Vec3>& positionList);
~XPageLODGroup(void);
public:
void AddPageLODFileData(const std::string& fileName, const float& minDistance, const float& maxDistance);
void AddPosition(const float& x, const float& y, const float& z);
void RemoveAllChildren(void);
void ClearLODFileData(void);
void ClearPositionData(void);
void GenerateLOD(void);
};
#include <jni.h>
#include <android/log.h>
#include "JNIUtils.h"
#include "XPageLODGroup.h"
#define LOG_TAG "osgvr.android.core.PageLODGroup"
#define LOGI(...) __android_log_print(ANDROID_LOG_INFO,LOG_TAG,__VA_ARGS__)
#define LOGE(...) __android_log_print(ANDROID_LOG_ERROR,LOG_TAG,__VA_ARGS__)
XPageLODGroup::XPageLODGroup()
{
}
XPageLODGroup::XPageLODGroup(const std::vector<PageLODFileRang>& pageLODFileData, const std::vector<osg::Vec3>& positionList)
{
m_pageLODFileData = pageLODFileData;
m_positionList = positionList;
}
XPageLODGroup::~XPageLODGroup(void)
{
}
void XPageLODGroup::GenerateLOD(void)
{
LOGE( "trying to GenerateLOD");
RemoveAllChildren();
if(m_pageLODFileData.size() >0 && m_positionList.size()>0)
{
osg::ref_ptr< osg::Node> lowNode = osgDB::readNodeFile(m_pageLODFileData[0].fileName);
if(lowNode.valid())
{
float r = lowNode->getBound().radius();
std::vector<osg::Vec3>::iterator iter;
for ( iter = m_positionList.begin() ; iter != m_positionList.end() ; iter++ )
{
osg::ref_ptr<osg::PagedLOD> lod = new osg::PagedLOD;
lod->setCenter(osg::Vec3(0.0,0.0,0.0) );
lod->addChild( lowNode, m_pageLODFileData[0].minDistance*r, m_pageLODFileData[0].maxDistance*r );
int index = 1;
std::vector<PageLODFileRang>::iterator iterFileRang;
for ( iterFileRang = m_pageLODFileData.begin()+1 ; iterFileRang != m_pageLODFileData.end() ; iterFileRang++ )
{
lod->setFileName( index, (*iterFileRang).fileName );
lod->setRange( index, (*iterFileRang).minDistance*r, (*iterFileRang).maxDistance*r );
index++;
}
//创建矩阵变换节点mt
osg::ref_ptr<osg::MatrixTransform> mt = new osg::MatrixTransform() ;
//创建一个矩阵
mt->addChild(lod.get());
mt->setMatrix(osg::Matrixd::translate(*iter));
addChild( mt.get() );
}
}
}
}
void XPageLODGroup::AddPageLODFileData(const std::string& fileName,const float& minDistance,const float& maxDistance)
{
m_pageLODFileData.push_back(PageLODFileRang(fileName,minDistance,maxDistance));
}
void XPageLODGroup::AddPosition(const float& x,const float& y,const float& z)
{
m_positionList.push_back(osg::Vec3(x,y,z));
}
void XPageLODGroup::RemoveAllChildren(void)
{
int n = getNumChildren();
for (int i = 0;i< n;i++)
{
removeChild(i);
}
}
void XPageLODGroup::ClearLODFileData(void)
{
m_pageLODFileData.clear();
}
void XPageLODGroup::ClearPositionData(void)
{
m_positionList.clear();
}
extern "C"
{
JNIEXPORT jlong JNICALL Java_osgvr_android_core_PageLODGroup_nativeCreatePageLODGroup(JNIEnv *, jclass)
{
XPageLODGroup* lodGroup = new XPageLODGroup();
lodGroup->ref();
return reinterpret_cast<jlong> (lodGroup);
}
JNIEXPORT void JNICALL Java_osgvr_android_core_PageLODGroup_nativeAddPageLODFileData(JNIEnv * env, jclass,jlong cptr,jstring filename,jfloat min,jfloat max)
{
std::string fileName = jstring2string(env,filename);
XPageLODGroup* lodGroup = reinterpret_cast<XPageLODGroup*> (cptr);
if (lodGroup == NULL)
return;
lodGroup->AddPageLODFileData(fileName,min, max);
}
JNIEXPORT void JNICALL Java_osgvr_android_core_PageLODGroup_nativeAddPosition(JNIEnv *, jclass,jlong cptr,jfloat x,jfloat y,jfloat z)
{
XPageLODGroup* lodGroup = reinterpret_cast<XPageLODGroup*> (cptr);
if (lodGroup == NULL)
return;
lodGroup->AddPosition(x, y,z);
}
JNIEXPORT jlong JNICALL Java_osgvr_android_core_PageLODGroup_nativeGenerateLOD(JNIEnv *, jclass,jlong cptr)
{
LOGE( "nativeGenerateLOD begin");
XPageLODGroup* lodGroup = reinterpret_cast<XPageLODGroup*> (cptr);
if (lodGroup == NULL)
return NULL;
lodGroup->GenerateLOD();
osg::Group* group = (XPageLODGroup*)lodGroup;
group->ref();
LOGE( "nativeGenerateLOD end");
return reinterpret_cast<jlong> (group);
}
}