|
本帖最后由 mxl12315 于 2013-3-22 10:48 编辑
最近将砖块着色器的例子改了一下,用于实现固定高度以上(下)的着色效果。给与一个高度值,高于这个height的着红色,相反则着绿色。
目前,着色器运行正常,无报错异常,我在场景中加载了六个模型,模型加载的方式均为:
osg::Node *model1 = osgDB::readNodeFile("*****.3DM");
osg::MatrixTransform* mt1 = new osg::MatrixTransform;
mt1->setMatrix(osg::Matrix::translate(osg::Vec3(150,0,0)));
mt1->addChild(model2);
效果没问题:
可是,当我继续加载模型,哪怕加一个。。。程序运行后就死了。。。着色器效果没有,而且场景也定住了。我开始以为模型是不是复杂了,顶点和片元着色器需要处理时间,可是我等了半小时,多加了一个模型就一直这样~加载方式还都是一样
截图:
请教各位高手,难道着色器不能应用于大场景吗?效率我真没希望什么,但是不是效果要出现呢?
着色器代码:
//定义着色器的源代码
static const char* vertexShader = {
"varying float MCposition; \n"
"uniform float BrickSize;\n"
"attribute vec4 gl_MultiTexCoord0;\n"
"void main()\n"
"{\n"
" MCposition=gl_Vertex.z; \n"
" gl_Position = ftransform();\n"
" gl_TexCoord[0] = gl_MultiTexCoord0;\n"
"}\n"
};
static const char* fragShader = {
"uniform vec4 BrickColor;\n"
"uniform float BrickSize;\n"
"uniform float BrickPct;\n"
"varying float MCposition;\n"
"uniform sampler2D sourceTex; \n"
"uniform vec4 LastColor;\n"
"void main()\n"
"{\n"
"vec4 texResult = texture2D(sourceTex, gl_TexCoord[0].st); \n"
"vec4 color;\n"
"float position,useBrick;\n"
"position=BrickSize-BrickPct;\n" //筛选条件BrickSize - 坐标BrickPct 0.0 = 剩余高度position
"useBrick=step(position,MCposition);\n" //若高度Position > 最高点MCposition ->0; 否则返回1
"color=mix(LastColor,BrickColor,useBrick);\n"
"gl_FragColor=color;\n"
"}\n"
};
void createShaders(osg::Node *node,bool flag)
{
osg::StateSet *stateset=node->getOrCreateStateSet();
osg:rogram *program=new osg::Program();
osg::Uniform *BrickColor=new osg::Uniform("BrickColor",osg::Vec4(0.8,0.0,0.0,0.3));
osg::Uniform *sourceTex = new osg::Uniform( "sourceTex", 0);
osg::Uniform *BrickSize=new osg::Uniform("BrickSize",height); //height可以指定一个数值
osg::Uniform *BrickPct=new osg::Uniform("BrickPct",0.0f);
osg::Uniform *LastColor=new osg::Uniform("LastColor",osg::Vec4(0.0,0.8,0.0,0.3));
program->addShader(new osg::Shader(osg::Shader::VERTEX,vertexShader));
program->addShader(new osg::Shader(osg::Shader::FRAGMENT,fragShader));
stateset->addUniform(LastColor);
stateset->addUniform(BrickColor);
stateset->addUniform(sourceTex);
stateset->addUniform(BrickSize);
stateset->addUniform(BrickPct);
if (flag==true)
{
stateset->setAttributeAndModes(program,osg::StateAttribute::ON);
}
if (flag==false)
{
stateset->removeAttribute(osg::StateAttribute::PROGRAM);
}
};
main函数中:
createShaders(root->getChild(*),true);
注:root就加载了模型,其他的没有~
|
|