1. 程式人生 > >osg著色器Shader入門級

osg著色器Shader入門級

未用shader的效果:

使用shader後的效果:

./data/myFrag.frag

    uniform sampler2D texUnit0;

    void main(){
        //texUnit0由外面呼叫程式傳進來,gl_TexCoord[]輸入紋理座標陣列;這句話的意思 從紋理取樣器texUnit0中取得紋理座標對應的紋理畫素值。
        vec4 color = texture2D(texUnit0,gl_TexCoord[0].xy); 
        
        //簡單的將這三個顏色值相加。
        float gcolor = color.r+color.g+color.b;
        
//灰度值輸出(gl_FragColor是每個片元著色器必須設定的輸出) gl_FragColor = vec4(gcolor,gcolor,gcolor,color.a); }

main.cpp

#include <osgViewer/Viewer>

#include <osg/Node>
#include <osg/Geode>
#include <osg/Group>
#include <osg/ShapeDrawable>

#include <osgDB/ReadFile>
#include 
<osgDB/WriteFile> #include <osgUtil/Optimizer> //繪製多個預定義的幾何體 osg::ref_ptr<osg::Geode> createShape() { //建立一個葉節點 osg::ref_ptr<osg::Geode> geode = new osg::Geode(); //設定半徑和高度 float radius = 0.8f; float height = 1.0f; //建立精細度物件,精細度越高,細分就越多 osg::ref_ptr<osg::TessellationHints> hints = new
osg::TessellationHints; //設定精細度為0.5f hints->setDetailRatio(0.5f); //新增一個球體,第一個引數是預定義幾何體物件,第二個是精細度,預設為0 geode->addDrawable(new osg::ShapeDrawable(new osg::Sphere(osg::Vec3(0.0f,0.0f,0.0f),radius),hints.get())); //新增一個正方體 geode->addDrawable(new osg::ShapeDrawable(new osg::Box(osg::Vec3(2.0f,0.0f,0.0f),2*radius),hints.get())); return geode.get() ; } int main() { //建立Viewer物件,場景瀏覽器 osg::ref_ptr<osgViewer::Viewer> viewer = new osgViewer::Viewer(); osg::ref_ptr<osg::Group> root = new osg::Group(); //新增到場景 osg::ref_ptr<osg::Group> myGroup = new osg::Group; myGroup->addChild(createShape()); myGroup->getOrCreateStateSet()->setTextureAttribute( 0,new osg::Texture2D(osgDB::readImageFile("C:\\55.jpg"))); myGroup->getOrCreateStateSet()->setTextureMode(0,GL_TEXTURE_2D,osg::StateAttribute::ON); /************************使用片元著色器************************************/ osg::ref_ptr<osg::StateSet> ss = myGroup->getOrCreateStateSet(); //new一個屬性類,用於新增屬性, osg::ref_ptr<osg::Program> texProgram = new osg::Program; //new一個片元著色器 osg::ref_ptr<osg::Shader> texFragObj = new osg::Shader(osg::Shader::FRAGMENT); texFragObj->loadShaderSourceFromFile("./data/myFrag.frag"); texProgram->addShader(texFragObj); //傳參 形參是texUnit0 0作為實參 osg::ref_ptr<osg::Uniform> texUniform = new osg::Uniform("texUnit0",0); ss->setAttributeAndModes(texProgram); ss->addUniform(texUniform); root->addChild(myGroup.get()); /************************使用片元著色器************************************/ //優化場景資料 osgUtil::Optimizer optimizer ; optimizer.optimize(root.get()) ; viewer->setSceneData(root.get()); viewer->setUpViewInWindow(20,20,600,600); viewer->realize(); viewer->run(); return 0 ; }