OSG 绘制贝塞尔曲线

OSG 绘制贝塞尔曲线_第1张图片

在某网友的版本上增加了颜色

//贝赛尔曲线方程P(t) = (1-t) 3 *P0 + 3*t*(1-t) 2 *P1 + 3*t 2 *(1-t)*P2 + t 3 *P3

static const char* vertSource = {
    "#version 120\n"
    "#extension GL_EXT_geometry_shader4 : enable\n"
    "void main()\n"
    "{gl_Position = ftransform(); }\n"
};

static const char* fragSource = {
    "#version 120\n"
    "#extension GL_EXT_geometry_shader4 : enable\n"
    "void main()\n"
    "{gl_FragColor = vec4(0,1,0,1); }\n"
};

static const char* geomSource = {
    "#version 120\n"
    "#extension GL_EXT_geometry_shader4 : enable\n"
    "uniform int segments;\n"
    "void main(void)\n"
    "{\n"
    " float delta = 1.0 / float(segments);\n"
    " vec4 v;\n"
    " for ( int i=0; i<=segments; ++i )\n"
    " {\n"
    " float t = delta * float(i);\n"
    " float t2 = t * t;\n"
    " float one_minus_t = 1.0 - t;\n"
    " float one_minus_t2 = one_minus_t * one_minus_t;\n"
    " v = gl_PositionIn[0] * one_minus_t2 * one_minus_t+ \n"
    " gl_PositionIn[1] * 3.0 * t * one_minus_t2 +\n"
    " gl_PositionIn[2] * 3.0 * t2 * one_minus_t +\n"
    " gl_PositionIn[3] * t2 * t;\n"
    " gl_Position = v;\n"
    " EmitVertex();\n"
    " }\n"
    " EndPrimitive();\n"
    "}\n"
};

osg::ref_ptr vertices = new osg::Vec3Array;
    vertices->push_back(osg::Vec3(0.0f, 0.0f, 0.0f));
    vertices->push_back(osg::Vec3(1.0f, 1.0f, 1.0f));
    vertices->push_back(osg::Vec3(2.0f, 1.0f, -1.0f));
    vertices->push_back(osg::Vec3(3.0f, 0.0f, 0.0f));
    osg::ref_ptr controlPoints = new osg::Geometry;
    controlPoints->setVertexArray(vertices.get());
    controlPoints->addPrimitiveSet(new osg::DrawArrays(GL_LINES_ADJACENCY_EXT, 0, 4));

    osg::ref_ptr geode = new osg::Geode;
    geode->addDrawable(controlPoints.get());

    int segments = 50;
    osg::ref_ptr program = new osg::Program;
    program->addShader(new osg::Shader(osg::Shader::VERTEX, vertSource));
    program->addShader(new osg::Shader(osg::Shader::GEOMETRY, geomSource));
    program->addShader(new osg::Shader(osg::Shader::FRAGMENT,fragSource));
    program->setParameter(GL_GEOMETRY_VERTICES_OUT_EXT, segments + 1);
    program->setParameter(GL_GEOMETRY_INPUT_TYPE_EXT,GL_LINES_ADJACENCY_EXT);
    program->setParameter(GL_GEOMETRY_OUTPUT_TYPE_EXT,GL_LINE_STRIP);

    osg::ref_ptr lineWidth = new osg::LineWidth;
    lineWidth->setWidth(2.0f);

    osg::StateSet* stateset = geode->getOrCreateStateSet();
    stateset->setAttributeAndModes(program.get(),osg::StateAttribute::ON);
    stateset->setAttribute(lineWidth.get());
    stateset->setMode(GL_LIGHTING, osg::StateAttribute::ON);
    stateset->addUniform(new osg::Uniform("segments", segments));

    pRoot->addChild(geode);

参考:OSG 实现曲线,赛贝尔曲线_Jeacean的博客-CSDN博客_osg 绘制曲线

你可能感兴趣的:(其它,Qt,开发语言,OSG,贝塞尔曲线,颜色)