|
楼主 |
发表于 2012-8-2 09:22:08
|
显示全部楼层
liuzhiyu123 发表于 2012-8-2 08:03
在vertex中比较高度 生成varying 传递给 frag ,在frag中通过生成varying 进行颜色的选定
您别笑话我哈,我这osg才刚入门没几天,着色器那部分只看过王锐老师书上的那个牛变色。以下是我写的代码,意思是3米以上全变色,请您指点下,看看我缺少什么导致无效果
#include <osgViewer/Viewer>
#include <osgDB/ReadFile>
#include <osg/Program>
#include <osg/Shader>
#include <osg/Uniform>
#include <osgViewer/ViewerEventHandlers>
//写一个顶点着色器
static const char* vertShader = {
"uniform float height=gl_Vertex.z;\n"
"varying vec4 color;\n"
"void main(void)\n"
"{\n"
" color = gl_Vertex.z;\n"
" gl_Position = gl_ModelViewProjectionMatrix*gl_Vertex;\n"
"}\n"
};
//片元着色器
static const char* fragShader = {
"vec4 color=osg::Vec4(0.8f,0.2f,0.1f,1.0f);\n"
"void main(void)\n"
"{\n"
" gl_FragColor = color;\n"
"}\n"
};
int main()
{
osg::ref_ptr<osgViewer::Viewer> viewer = new osgViewer::Viewer;
osg::Node* node1 = osgDB::readNodeFile("X1.osg");
osg::StateSet* stateset = node1->getOrCreateStateSet();
osg:rogram* program = new osg::Program;
program->addShader(new osg::Shader(osg::Shader::VERTEX, vertShader));
program->addShader(new osg::Shader(osg::Shader::FRAGMENT, fragShader));
osg::ref_ptr <osg::Uniform> height=new osg::Uniform("uniform",3.0f);
stateset->addUniform(height);
stateset->setAttributeAndModes(program, osg::StateAttribute::ON);
viewer->addEventHandler(new osgViewer::WindowSizeHandler);
viewer->setSceneData(node1);
viewer->run();
return 0;
} |
|