查看: 919|回复: 0

osg使用高版本的opengl纹理缺失

[复制链接]

该用户从未签到

发表于 2023-11-16 16:15:55 | 显示全部楼层 |阅读模式
#include <osgViewer/Viewer>
#include <osgDB/ReadFile>
#include <osg/GraphicsContext>
#include <osg/Camera>
#include <osg/Viewport>
#include <osg/StateSet>
#include <osg/Program>
#include <osg/Shader>
#include <osgUtil/Optimizer>

#include <osgGA/StateSetManipulator>
#include <osgGA/TrackballManipulator>
#include <osgGA/FlightManipulator>
#include <osgGA/KeySwitchMatrixManipulator>
#include <osgGA/AnimationPathManipulator>
#include <osgGA/TerrainManipulator>
#include <osgGA/DriveManipulator>

#include <osgViewer/ViewerEventHandlers>

void configureShaders(osg::StateSet* stateSet)
{
    const std::string vertexSource =
        "#version 130 \n"
        " \n"
        "uniform mat4 osg_ModelViewProjectionMatrix; \n"
        "uniform mat3 osg_NormalMatrix; \n"
        "uniform vec3 ecLightDir; \n"
        " \n"
        "in vec4 osg_Vertex; \n"
        "in vec3 osg_Normal; \n"
        "out vec4 color; \n"
        " \n"
        "void main() \n"
        "{ \n"
        "    vec3 ecNormal = normalize( osg_NormalMatrix * osg_Normal ); \n"
        "    float diffuse = max( dot( ecLightDir, ecNormal ), 0. ); \n"
        "    color = vec4( vec3( diffuse ), 1. ); \n"
        " \n"
        "    gl_Position = osg_ModelViewProjectionMatrix * osg_Vertex; \n"
        "} \n";
    osg::Shader* vShader = new osg::Shader(osg::Shader::VERTEX, vertexSource);

    const std::string fragmentSource =
        "#version 130 \n"
        " \n"
        "in vec4 color; \n"
        "out vec4 fragData; \n"
        " \n"
        "void main() \n"
        "{ \n"
        "    fragData = color; \n"
        "} \n";
    osg::Shader* fShader = new osg::Shader(osg::Shader::FRAGMENT, fragmentSource);

    osg:rogram* program = new osg::Program;
    program->addShader(vShader);
    program->addShader(fShader);
    stateSet->setAttribute(program);

    osg::Vec3f lightDir(0., 0.5, 1.);
    lightDir.normalize();
    stateSet->addUniform(new osg::Uniform("ecLightDir", lightDir));
}

int main(int argc, char** argv)
{  
    std::vector<std::string> fileList;
    fileList.push_back("glider.osg");
    // 创建节点.
    osg::ref_ptr<osg::Node> root = osgDB::readRefNodeFiles(fileList);
    if (root == NULL)
    {
        osg::notify(osg::FATAL) << "Unable to load model from command line." << std::endl;
        return(1);
    }

    osgUtil::Optimizer optimizer;
    optimizer.optimize(root.get(), osgUtil::Optimizer::ALL_OPTIMIZATIONS | osgUtil::Optimizer::TESSELLATE_GEOMETRY);

    // 应该初始化gl变量的值.
    configureShaders(root->getOrCreateStateSet());

    const int width(800), height(450);
    const std::string version("3.0");

    // 应该是创并配置设备(画板).
    osg::ref_ptr< osg::GraphicsContext::Traits > traits = new osg::GraphicsContext::Traits();
    // 渲染窗口的x坐标.
    traits->x = 20;
    // 渲染窗口的y坐标.
    traits->y = 30;
    // 渲染窗口的宽.
    traits->width = width;
    // 渲染窗口的高.
    traits->height = height;
    // 是否出现工具栏.
    traits->windowDecoration = true;
    // 是否启用双缓冲.
    traits->doubleBuffer = true;
    // 设置gl版本.
    traits->glContextVersion = version;
    // unknow 注释掉也未观察到影响.
    traits->readDISPLAY();
    // unknow 注释掉也未观察到影响.
    traits->setUndefinedScreenDetailsToDefaultScreen();
    // 应是创并gl上下文.
    osg::ref_ptr< osg::GraphicsContext > gc = osg::GraphicsContext::createGraphicsContext(traits.get());
    if (!gc.valid())
    {
        osg::notify(osg::FATAL) << "Unable to create OpenGL v" << version << " context." << std::endl;
        return(1);
    }

    // osg观察器.
    osgViewer::Viewer viewer;

    // Create a Camera that uses the above OpenGL context.
    osg::Camera* cam = viewer.getCamera();

    // 添加状态事件.
    viewer.addEventHandler(new osgGA::StateSetManipulator(viewer.getCamera()->getOrCreateStateSet()));

    // 窗口大小变化事件.
    viewer.addEventHandler(new osgViewer::WindowSizeHandler);

    // 添加一些常用状态设置
    viewer.addEventHandler(new osgViewer::StatsHandler);

    // 设置操作器
    {
        osg::ref_ptr<osgGA::KeySwitchMatrixManipulator>keyswitchManipulator = new osgGA::KeySwitchMatrixManipulator;

        keyswitchManipulator->addMatrixManipulator('1', "Trackball", new osgGA::TrackballManipulator());
        keyswitchManipulator->addMatrixManipulator('2', "Flight", new osgGA::FlightManipulator());
        keyswitchManipulator->addMatrixManipulator('3', "Drive", new osgGA:riveManipulator());
        keyswitchManipulator->addMatrixManipulator('4', "Terrain", new osgGA::TrackballManipulator());
        viewer.setCameraManipulator(keyswitchManipulator.get());
    }

    viewer.addEventHandler(new osgViewer::RecordCameraPathHandler);
   

    cam->setGraphicsContext(gc.get());
    // Must set perspective projection for fovy and aspect.
    cam->setProjectionMatrix(osg::Matrix::perspective(30., (double)width / (double)height, 1., 100.));
    // Unlike OpenGL, OSG viewport does *not* default to window dimensions.
    cam->setViewport(new osg::Viewport(0, 0, width, height));

    viewer.setSceneData(root);

    // for non GL3/GL4 and non GLES2 platforms we need enable the osg_ uniforms that the shaders will use,
    // you don't need thse two lines on GL3/GL4 and GLES2 specific builds as these will be enable by default.
    gc->getState()->setUseModelViewAndProjectionUniforms(true);
    gc->getState()->setUseVertexAttributeAliasing(true);


    viewer.realize();
    return(viewer.run());
}
微信图片_20231116161454.png
微信图片_20231116161459.png
您需要登录后才可以回帖 登录 | 注册

本版积分规则

OSG中国官方论坛-有您OSG在中国才更好

网站简介:osgChina是国内首个三维相关技术开源社区,旨在为国内更多的技术开发人员提供最前沿的技术资讯,为更多的三维从业者提供一个学习、交流的技术平台。

联系我们

  • 工作时间:09:00--18:00
  • 反馈邮箱:1315785073@qq.com
快速回复 返回顶部 返回列表