osg3.4.1 osgearth2.8 Android安卓移植(预)

编译工具Android Studio ,使用cmake编译so动态库

osg核心模块,jpeg,png,freetype,tiff等第三方库和部分插件编译已完成,

之前遇到很多坑都没记录,现记录一下,如有时间将之前的补上:

osg编译:

编译前将include下面osg下面的GL头文件更改如下如没有自己新建一个:

/* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2009 Robert Osfield
 *
 * This library is open source and may be redistributed and/or modified under
 * the terms of the OpenSceneGraph Public License (OSGPL) version 0.0 or
 * (at your option) any later version.  The full license is in LICENSE file
 * included with this distribution, and on the openscenegraph.org website.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * OpenSceneGraph Public License for more details.
*/

#ifndef OSG_OPENGL
#define OSG_OPENGL 1

#include <osg/Config>
#include <osg/Export>
#include <osg/Types>
 
//#define OSG_GL1_AVAILABLE
//#define OSG_GL2_AVAILABLE
/* #undef OSG_GL3_AVAILABLE */
/* #undef OSG_GLES1_AVAILABLE */
/* #undef OSG_GLES2_AVAILABLE */
/* #undef OSG_GLES3_AVAILABLE */
/* #undef OSG_GL_LIBRARY_STATIC */
#define OSG_GLES2_AVAILABLE
//#define OSG_GL_LIBRARY_STATIC
//#define OSG_GL_DISPLAYLISTS_AVAILABLE
//#define OSG_GL_MATRICES_AVAILABLE
//#define OSG_GL_VERTEX_FUNCS_AVAILABLE
//#define OSG_GL_VERTEX_ARRAY_FUNCS_AVAILABLE
//#define OSG_GL_FIXED_FUNCTION_AVAILABLE
/* #undef GL_HEADER_HAS_GLINT64 */
/* #undef GL_HEADER_HAS_GLUINT64 */

#define OSG_GL1_FEATURES 0
#define OSG_GL2_FEATURES 0
#define OSG_GL3_FEATURES 0
#define OSG_GLES1_FEATURES 0
#define OSG_GLES2_FEATURES 1
#define OSG_GLES3_FEATURES 0


#ifndef WIN32

    // Required for compatibility with glext.h sytle function definitions of
    // OpenGL extensions, such as in src/osg/Point.cpp.
    #ifndef APIENTRY
        #define APIENTRY
    #endif

#else // WIN32

    #if defined(__CYGWIN__) || defined(__MINGW32__)

        #ifndef APIENTRY
                #define GLUT_APIENTRY_DEFINED
                #define APIENTRY __stdcall
        #endif
            // XXX This is from Win32's <windef.h>
        #ifndef CALLBACK
            #define CALLBACK __stdcall
        #endif

    #else // ! __CYGWIN__

        // Under Windows avoid including <windows.h>
        // to avoid name space pollution, but Win32's <GL/gl.h>
        // needs APIENTRY and WINGDIAPI defined properly.
        // XXX This is from Win32's <windef.h>
        #ifndef APIENTRY
            #define GLUT_APIENTRY_DEFINED
            #if (_MSC_VER >= 800) || defined(_STDCALL_SUPPORTED)
                #define WINAPI __stdcall
                #define APIENTRY WINAPI
            #else
                #define APIENTRY
            #endif
        #endif

            // XXX This is from Win32's <windef.h>
        #ifndef CALLBACK
            #if (_MSC_VER >= 800) || defined(_STDCALL_SUPPORTED)
                #define CALLBACK __stdcall
            #else
                #define CALLBACK
            #endif
        #endif

    #endif // __CYGWIN__

    // XXX This is from Win32's <wingdi.h> and <winnt.h>
    #ifndef WINGDIAPI
        #define GLUT_WINGDIAPI_DEFINED
        #define DECLSPEC_IMPORT __declspec(dllimport)
        #define WINGDIAPI DECLSPEC_IMPORT
    #endif

    // XXX This is from Win32's <ctype.h>
    #if !defined(_WCHAR_T_DEFINED) && !(defined(__GNUC__)&&(__GNUC__ > 2))
        typedef unsigned short wchar_t;
        #define _WCHAR_T_DEFINED
    #endif

#endif // WIN32

#if defined(OSG_GL3_AVAILABLE)
    #define GL3_PROTOTYPES 1
    #define GL_GLEXT_PROTOTYPES 1
#endif


#include <GLES2/gl2.h>



#ifndef GL_APIENTRY
    #define GL_APIENTRY APIENTRY
#endif // GL_APIENTRY


#ifndef GL_HEADER_HAS_GLINT64
    typedef int64_t GLint64;
#endif

#ifndef GL_HEADER_HAS_GLUINT64
    typedef uint64_t GLuint64;
#endif

#ifdef OSG_GL_MATRICES_AVAILABLE

    inline void glLoadMatrix(const float* mat) { glLoadMatrixf(static_cast<const GLfloat*>(mat)); }
    inline void glMultMatrix(const float* mat) { glMultMatrixf(static_cast<const GLfloat*>(mat)); }

    #ifdef OSG_GLES1_AVAILABLE
        inline void glLoadMatrix(const double* mat)
        {
            GLfloat flt_mat[16];
            for(unsigned int i=0;i<16;++i) flt_mat[i] = mat[i];
            glLoadMatrixf(flt_mat);
        }

        inline void glMultMatrix(const double* mat)
        {
            GLfloat flt_mat[16];
            for(unsigned int i=0;i<16;++i) flt_mat[i] = mat[i];
            glMultMatrixf(flt_mat);
        }

    #else
        inline void glLoadMatrix(const double* mat) { glLoadMatrixd(static_cast<const GLdouble*>(mat)); }
        inline void glMultMatrix(const double* mat) { glMultMatrixd(static_cast<const GLdouble*>(mat)); }
    #endif
#endif

// add defines for OpenGL targets that don't define them, just to ease compatibility across targets
#ifndef GL_DOUBLE
    #define GL_DOUBLE 0x140A
    typedef double GLdouble;
#endif

#ifndef GL_INT
    #define GL_INT 0x1404
#endif

#ifndef GL_UNSIGNED_INT
    #define GL_UNSIGNED_INT 0x1405
#endif

#ifndef GL_NONE
    // OpenGL ES1 doesn't provide GL_NONE
    #define GL_NONE 0x0
#endif

#if defined(OSG_GLES1_AVAILABLE) || defined(OSG_GLES2_AVAILABLE)
    //GLES defines (OES)
    #define GL_RGB8_OES                                             0x8051
    #define GL_RGBA8_OES                                            0x8058
#endif

#if defined(OSG_GLES1_AVAILABLE) || defined(OSG_GLES2_AVAILABLE) || defined(OSG_GL3_AVAILABLE)
    #define GL_POLYGON                         0x0009
    #define GL_QUADS                           0x0007
    #define GL_QUAD_STRIP                      0x0008
#endif

#if defined(OSG_GL3_AVAILABLE)
    #define GL_LUMINANCE                      0x1909
    #define GL_LUMINANCE_ALPHA                0x190A
#endif


#endif

osg核心模块编译完成后有几处需要修改:

1.osg在加载动态库插件是是没有写Android对应分支的

    将osgDB下的Registry.cpp  createLibraryNameForExtension函数和createLibraryNameForNodeKit函数修改如下:

std::string Registry::createLibraryNameForNodeKit(const std::string& name)
{
#if defined(__CYGWIN__)
    return "cyg"+name+OSG_LIBRARY_POSTFIX_WITH_QUOTES+".dll";
#elif defined(__MINGW32__)
    return "lib"+name+OSG_LIBRARY_POSTFIX_WITH_QUOTES+".dll";
#elif defined(WIN32)
    return name+OSG_LIBRARY_POSTFIX_WITH_QUOTES+".dll";
#elif macintosh
    return name+OSG_LIBRARY_POSTFIX_WITH_QUOTES;
#elif defined(__ANDROID_NDK__)
    return "lib"+name+OSG_LIBRARY_POSTFIX_WITH_QUOTES+".so";
#else
    return "lib"+name+OSG_LIBRARY_POSTFIX_WITH_QUOTES + ADDQUOTES(OSG_PLUGIN_EXTENSION);
#endif
}
std::string Registry::createLibraryNameForExtension(const std::string& ext)
{
    std::string lowercase_ext;
    for(std::string::const_iterator sitr=ext.begin();
        sitr!=ext.end();
        ++sitr)
    {
        lowercase_ext.push_back(tolower(*sitr));
    }

    ExtensionAliasMap::iterator itr=_extAliasMap.find(lowercase_ext);
    if (itr!=_extAliasMap.end() && ext != itr->second) return createLibraryNameForExtension(itr->second);

    std::string prepend = std::string("osgPlugins-")+std::string(osgGetVersion())+std::string("/");

#if defined(__CYGWIN__)
    return prepend+"cygwin_"+"osgdb_"+lowercase_ext+OSG_LIBRARY_POSTFIX_WITH_QUOTES+".dll";
#elif defined(__MINGW32__)
    return prepend+"mingw_"+"osgdb_"+lowercase_ext+OSG_LIBRARY_POSTFIX_WITH_QUOTES+".dll";
#elif defined(WIN32)
    return prepend+"osgdb_"+lowercase_ext+OSG_LIBRARY_POSTFIX_WITH_QUOTES+".dll";
#elif macintosh
    return prepend+"osgdb_"+lowercase_ext+OSG_LIBRARY_POSTFIX_WITH_QUOTES;
#elif defined(__ANDROID_NDK__)
    return std::string("lib")+"osgdb_"+lowercase_ext+OSG_LIBRARY_POSTFIX_WITH_QUOTES+".so";
#else
    return prepend+"osgdb_"+lowercase_ext+OSG_LIBRARY_POSTFIX_WITH_QUOTES+ADDQUOTES(OSG_PLUGIN_EXTENSION);
#endif

}

将osgDB下DynamicLibrary.cpp的getLibraryHandle函数修改如下:


DynamicLibrary::HANDLE DynamicLibrary::getLibraryHandle( const std::string& libraryName)
{
    HANDLE handle = NULL;

#if defined(WIN32) && !defined(__CYGWIN__)
#ifdef OSG_USE_UTF8_FILENAME
    handle = LoadLibraryW(  convertUTF8toUTF16(libraryName).c_str() );
#else
    handle = LoadLibrary( libraryName.c_str() );
#endif
#elif defined(__APPLE__) && defined(APPLE_PRE_10_3)
    NSObjectFileImage image;
    // NSModule os_handle = NULL;
    if (NSCreateObjectFileImageFromFile(libraryName.c_str(), &image) == NSObjectFileImageSuccess) {
        // os_handle = NSLinkModule(image, libraryName.c_str(), TRUE);
        handle = NSLinkModule(image, libraryName.c_str(), TRUE);
        NSDestroyObjectFileImage(image);
    }
#elif defined(__hpux)
    // BIND_FIRST is necessary for some reason
    handle = shl_load ( libraryName.c_str(), BIND_DEFERRED|BIND_FIRST|BIND_VERBOSE, 0);
    return handle;
#else // other unix

    // dlopen will not work with files in the current directory unless
    // they are prefaced with './'  (DB - Nov 5, 2003).
    std::string localLibraryName;
    if( libraryName == osgDB::getSimpleFileName( libraryName ) )
        localLibraryName = "./" + libraryName;
    else
        localLibraryName = libraryName;

#ifdef __ANDROID_NDK__
    localLibraryName = libraryName;
#endif

    handle = dlopen( localLibraryName.c_str(), RTLD_LAZY | RTLD_GLOBAL);
    if( handle == NULL )
    {
        if (fileExists(localLibraryName))
        {
            OSG_WARN << "Warning: dynamic library '" << libraryName << "' exists, but an error occurred while trying to open it:" << std::endl;
            OSG_WARN << dlerror() << std::endl;
        }
        else
        {
            OSG_INFO << "Warning: dynamic library '" << libraryName << "' does not exist (or isn't readable):" << std::endl;
            OSG_INFO << dlerror() << std::endl;
        }
    }
#endif
    return handle;
}

2.由于osg默认加载的字体是arial.ttf,安卓系统没有这个字体,而且就算有这个字体他的拼写方式也不对,现将osgText模块下的Font.cpp的findFontFile函数修改如下:

std::string osgText::findFontFile(const std::string& str)
{
    // try looking in OSGFILEPATH etc first for fonts.
    std::string filename = osgDB::findDataFile(str);
    if (!filename.empty()) return filename;

    OpenThreads::ScopedLock<OpenThreads::ReentrantMutex> lock(getFontFileMutex());

#ifdef __ANDROID_NDK__
    std::string* pStr = const_cast<std::string*>(&str);
    *pStr = "Roboto-Regular.ttf";
#endif

    static osgDB::FilePathList s_FontFilePath;
    static bool initialized = false;
    if (!initialized)
    {
        initialized = true;
    #if defined(WIN32)
        osgDB::convertStringPathIntoFilePathList(
            ".;C:/winnt/fonts;C:/windows/fonts",
            s_FontFilePath);

        char *ptr;
        if ((ptr = getenv( "windir" )))
        {
            std::string winFontPath = ptr;
            winFontPath += "\\fonts";
            s_FontFilePath.push_back(winFontPath);
        }
    #elif defined(__ANDROID_NDK__)
        std::string strSysFont = "/system/fonts/";
        s_FontFilePath.push_back(strSysFont);
    #elif defined(__APPLE__)
      osgDB::convertStringPathIntoFilePathList(
        ".:/usr/share/fonts/ttf:/usr/share/fonts/ttf/western:/usr/share/fonts/ttf/decoratives:/Library/Fonts:/System/Library/Fonts",
        s_FontFilePath);
    #else
      osgDB::convertStringPathIntoFilePathList(
        ".:/usr/share/fonts/ttf:/usr/share/fonts/ttf/western:/usr/share/fonts/ttf/decoratives",
        s_FontFilePath);
    #endif
    }



    filename = osgDB::findFileInPath(str,s_FontFilePath);
    if (!filename.empty()) return filename;

    // Try filename without pathname, if it has a path
    filename = osgDB::getSimpleFileName(str);
    if(filename!=str)
    {
        filename = osgDB::findFileInPath(filename,s_FontFilePath);
        if (!filename.empty()) return filename;
    }
    else
    {
        filename = osgText::findFontFile(std::string("fonts/")+filename);
        if (!filename.empty()) return filename;
    }

    // Not found, return empty string
    OSG_INFO<<"Warning: font file \""<<str<<"\" not found."<<std::endl;
    return std::string();
}

虽然这样不怎么好但是还是先这样。

编译后测试:

贴张纹理(需要修改自带例子和Shader,有时间回帖上)


牛,纹理没贴上去,可能和数据格式有关后面再 排查:


在Android上操作器有点问题,鼠标灵敏度太大,设置一个因子就好,但是还是有问题,在XZ轴上旋转正常,但是很Y轴很难旋转,后面继续修改操作。


  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
### 回答1: Qt OSGEarth 2.8 是一个基于 Qt 开发框架和开源的 OSGEarth 地球可视化库的版本。OSGEarth 是一个功能强大的地球可视化工具,用于创建交互式的地球场景,它是 OpenSceneGraph(OSG)的一个插件。而 Qt 是一款跨平台的图形用户界面开发框架。 Qt OSGEarth 2.8 的发布意味着它在功能和性能方面得到了进一步的改进和优化。它可能通过增加新的功能、修复已知问题和提高性能来提升用户体验。 由于使用 Qt 开发框架,Qt OSGEarth 2.8 具有很好的跨平台兼容性。这意味着开发人员可以使用同一份代码在不同的操作系统上运行他们的应用程序。无论是在 Windows、macOS 还是 Linux,使用 Qt OSGEarth 2.8 开发的应用程序都可以良好地运行。 Qt OSGEarth 2.8 可以帮助开发人员创建交互式的地球场景,这对于需要展示地理数据或地图信息的应用程序非常有用。无论是在教育、旅游、可视化分析还是 GIS 领域,Qt OSGEarth 2.8 都可以提供丰富的功能和工具来满足不同的需求。 总之,Qt OSGEarth 2.8 是一个功能强大、具有跨平台兼容性的地球可视化库,它可以帮助开发人员在不同操作系统上创建交互式的地球场景,展示地理数据和地图信息。 ### 回答2: Qt是一种跨平台的应用程序开发框架,而OSGearth2.8是基于开源软件OpenSceneGraph(OSG)的一个地球可视化工具包。Qt提供了一种方便的方式来开发具有图形界面的应用程序,而OSGearth则是在Qt的基础上提供了地球可视化功能。 使用Qt和OSGearth2.8可以开发各种类型的地球可视化应用程序,例如地图导航、地理信息系统(GIS)应用等。开发人员可以利用Qt的丰富的界面库和OSGearth的强大的地球渲染引擎来创建交互式和具有吸引力的地图应用。 在Qt和OSGearth的组合中,Qt负责应用程序的界面设计和逻辑控制,可以通过Qt的各种控件和布局工具来创建用户友好的界面。而OSGearth则负责地球可视化的部分,它利用OSG提供的渲染引擎来展现地球数据,包括地形、地图、影像等。OSGearth还提供了一些强大的功能,如地球影像的投影、三维模型的加载和渲染等。 使用Qt和OSGearth进行地球可视化开发的好处是,它们都是开源的软件,可以免费使用和修改。同时,它们也是跨平台的,可以在Windows、Mac和Linux等多个操作系统上运行。这样可以使开发人员更加方便地进行应用程序的设计和测试。 总而言之,Qt和OSGearth2.8是一对强大的组合,可以帮助开发人员实现各种类型的地球可视化应用程序,并提供了丰富的界面和功能。无论是地图导航还是GIS应用,Qt和OSGearth都能够满足开发人员的需求,并提供高效、可靠的开发解决方案。 ### 回答3: Qt OSGEarth 2.8 是一个开源的地理信息系统(GIS)软件开发工具包,它结合了Qt和OSG(OpenSceneGraph)两个技术框架。 Qt是一个跨平台的C++应用程序开发框架,它提供了丰富的图形界面和功能组件,能够方便地开发各种类型的应用程序。OSG是一个针对3D图形应用开发的工具包,它提供了强大的渲染和场景管理功能,可以让开发者创建逼真的3D场景。 Qt OSGEarth 2.8充分利用了两个框架的优势,提供了高效便捷的GIS开发环境。它支持多种数据源,包括地理信息系统(GIS)数据、图像数据和模型数据等。通过使用Qt OSGEarth 2.8,开发者可以轻松地加载和显示地理数据,并在3D场景中进行交互操作。 Qt OSGEarth 2.8还提供了丰富的功能组件和API接口,方便开发者进行数据查询、坐标转换、图层叠加和图形绘制等操作。此外,它还支持用户自定义插件,可以根据需求扩展和定制功能。 总的来说,Qt OSGEarth 2.8 是一个功能强大、易于使用的GIS开发工具包。它集成了Qt和OSG两个技术框架的优势,提供了高效的GIS开发环境,可以满足各种类型的地理信息应用需求。无论是开发地图应用、地理数据可视化应用还是虚拟仿真应用,Qt OSGEarth 2.8 都是一个不错的选择。
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值