|
最近反复写一个着色器,目的是指定圆内的模型局部着红色,区域外的那部分着绿色。。。现在我已经写好着色器,包括模型顶点的遍历,但始终实现不了效果,希望各位高手进来指点下毛病
PS:新手较笨,老问着色,希望见谅~
代码:
static const char* vertSource = { //顶点着色器
"varying float ColorPX;\n"
"varying float ColorPY;\n"
"varying float dis; \n"
"uniform vec3 VertexP; \n"
"uniform float X;\n"
"uniform float Y;\n"
"void main()\n"
"{\n"
" ColorPX=VertexP.x;\n"
" ColorPY=VertexP.y;\n"
" dis=sqrt( (ColorPX-X)*(ColorPX-X) + (ColorPY-Y)*(ColorPY-Y) );\n" //顶点到圆心的距离
" gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;\n"
"}\n"
};
static const char* fragSource = { //片元着色器
"uniform vec3 BrickColor,MortarColor;\n"
"uniform float R;\n" //半径
"varying float dis; \n"
"void main()\n"
"{\n"
" vec3 color; \n"
" float useBrick; \n"
" useBrick=step(R,dis); \n" //step()方法比较‘顶点到圆心的距离’与圆区域的半径
" color=mix(MortarColor,BrickColor,useBrick); \n" //在圆内(<R)和不在圆内(>R)
" gl_FragColor=vec4(color,1.0); \n"
"}\n"
};
void createShaders( osg::ref_ptr <osg::Node> node)
{
osg::StateSet *ss=node->getOrCreateStateSet();
osg::ref_ptr<osg:rogram> program = new osg::Program;
osg::ref_ptr<osg::Shader> vertShader = new osg::Shader( osg::Shader::VERTEX, vertSource ); //顶点着色器
program->addShader( vertShader.get() );
osg::ref_ptr<osg::Shader> fragShader = new osg::Shader( osg::Shader::FRAGMENT, fragSource );//片元着色器
program->addShader( fragShader.get() );
osg::ref_ptr<osg::Uniform> R = new osg::Uniform( "R", r ); //圆的半径
ss->addUniform( R.get() );
osg::ref_ptr<osg::Uniform> X = new osg::Uniform( "X", x ); //圆心x
ss->addUniform( X.get() );
osg::ref_ptr<osg::Uniform> Y = new osg::Uniform( "Y", y ); //圆心y
ss->addUniform( Y.get() );
VertexExtractor iv; //顶点遍历
node->accept(iv);
int size_t=iv.extracted_verts.get()->size();
std::vector <osg::Vec3> ::iterator iter=iv.extracted_verts.get()->begin();
for (int i=0;i<size_t;i++)
{
std::cout<<iter->x()<<" "<<iter->y()<<std::endl; //顶点的x、y值输出
osg::Vec3 pP((iter->x()),(iter->y()),0); //自定义一个点
iter++;
osg::ref_ptr<osg::Uniform> VertexP = new osg::Uniform( "VertexP", pP); //设断点调试不为空
ss->addUniform( VertexP.get() );
osg::ref_ptr<osg::Uniform> BrickColor = new osg::Uniform( "BrickColor", osg::Vec3(1.0,0.0,0.0) );
ss->addUniform( BrickColor.get() );
osg::ref_ptr<osg::Uniform> MortarColor = new osg::Uniform( "MortarColor", osg::Vec3(0.0,0.8,0.0) );
ss->addUniform( MortarColor.get() );
}
ss->setAttributeAndModes( program.get() );
}
问题:
|
|