前面,用漫游器表示视点有些太笨重了,实际上也没有必要,用回调函数就可以了。
class EyePointCallback : public osg::UniformCallback
{
public:
EyePointCallback(osg::ref_ptrosg::Camera camera)
{
_camera = camera;
}
virtual void operator() (osg::Uniform* uniform, osg::NodeVisitor* nv)
{
osg::Vec3 eye, center, up;
_camera->getViewMatrixAsLookAt(eye, center, up);
uniform->set(eye);
}
private:
osg::ref_ptrosg::Camera _camera;
};
osg::ref_ptr<osg::Camera> camera = viewer->getCamera();
osg::ref_ptr<osg::Uniform> camPosUniform = new osg::Uniform("camPos", osg::Vec3f(0, 0, 0));
camPosUniform->setUpdateCallback(new EyePointCallback(camera));
stateset->addUniform(camPosUniform);
这里顺便把投影坐标系和摄像机坐标系的也加上,(暂时不用。)

代码如下:
//通过Liblas读取.las文件,并在osg中显示出来,用shader,先在片元着色器指定使用绿色
#include
#include
#include <osgDB/ReadFile>
#include <osgDB/WriteFile>
#include <osgUtil/Optimizer>
#include <osg/CoordinateSystemNode>
#include <osg/Switch>
#include <osg/Types>
#include <osgText/Text>
#include <osgViewer/Viewer>
#include <osgViewer/ViewerEventHandlers>
#include <osgGA/TrackballManipulator>
#include <osgGA/FlightManipulator>
#include <osgGA/DriveManipulator>
#include <osgGA/KeySwitchMatrixManipulator>
#include <osgGA/StateSetManipulator>
#include <osgGA/AnimationPathManipulator>
#include <osgGA/TerrainManipulator>
#include <osgGA/SphericalManipulator>
#include <osgGA/Device>
#include
#include <osg/Shader>
#include <osg/BlendFunc>
#include <osg/blendColor>
#include <osg/Point>
#include <osg/Shapedrawable>
#include <osgUtil/SmoothingVisitor>
static const char * vertexShader_PBR =
{
“in vec3 aPos; \n”
“in vec3 aNormal; \n”
“varying vec3 WorldPos; \n”
“varying vec3 Normal; \n”
“uniform mat4 normalMatrix; \n”
“uniform mat4 projection;\n”
“uniform mat4 view;\n”
“void main() \n”
“{ \n”
" WorldPos = aPos; \n"
" Normal = vec3(normalMatrix * vec4(aNormal,1.0)); \n"
" gl_Position = ftransform(); \n"
//" gl_Position = projection * view * vec4(aPos,1.0); \n"
“}\n”
};
static const char psShader_PBR =
{
“#version 330 core \n”
“out vec4 FragColor; \n”
“varying vec3 WorldPos; \n”
“varying vec3 Normal; \n”
“uniform vec3 albedo; \n”
“uniform float metallic; \n”
“uniform float roughness; \n”
“uniform float ao; \n”
“uniform vec3 lightPositions[4]; \n”
“uniform vec3 lightColors[4]; \n”
“uniform vec3 camPos; \n”
“const float PI = 3.14159265359; \n”
“float DistributionGGX(vec3 N, vec3 H, float roughness) \n”
“{

博客介绍了在OSG编程中,可用回调函数替代漫游器表示视点,并给出了相关回调函数类的代码。同时展示了通过Liblas读取.las文件并在OSG中显示的代码,包含顶点着色器和片元着色器代码,以及创建球体几何体的函数代码。
最低0.47元/天 解锁文章
1420





