|
楼主 |
发表于 2014-12-9 22:00:28
|
显示全部楼层
1、将着色器中通过osg传入的几个矩阵改为opengl自有的矩阵后,结果正确了:
可能这几个矩阵有点问题
2、自己写的出不来的原因可能就是细分着色器写的不正确,我改正了能够改正的:
1)主函数:
int main()
{
osg::ref_ptr<osgViewer::Viewer> viewer = new osgViewer::Viewer;
//viewer->setUpViewInWindow(100,100,800,600);
osg::ref_ptr<osg::Geode> geoNode = new osg::Geode;
osg::ref_ptr<osg::Geometry> quadNode =new osg::Geometry;
osg::ref_ptr<osg::Vec3Array> vertices = new osg::Vec3Array;
vertices->push_back(osg::Vec3f(0.0,0.0,0.0));
vertices->push_back(osg::Vec3f(10.0,0.0,0.0));
vertices->push_back(osg::Vec3f(10.0,10.0,0.0));
vertices->push_back(osg::Vec3f(0.0,10.0,0.0));
quadNode->setVertexArray(vertices.get());
const unsigned int Faces[] = {
0, 1, 2, 3
};
osg:rawElementsUInt* de = new osg::DrawElementsUInt(osg:rimitiveSet::PATCHES,4,Faces);
quadNode->addPrimitiveSet(de);
osg::StateSet* stateset = quadNode->getOrCreateStateSet();
osg::Program* program = new osg::Program;
program->addShader(new osg::Shader(osg::Shader::VERTEX, vertShader));
program->addShader(new osg::Shader(osg::Shader::TESSCONTROL, teconShader));
program->addShader(new osg::Shader(osg::Shader::TESSEVALUATION, teevaShader));
program->addShader(new osg::Shader(osg::Shader::FRAGMENT, fragShader));
stateset->setAttributeAndModes(program, osg::StateAttribute::ON);
quadNode->setInitialBound(osg::BoundingBox(-1.0f,-1.0f,-1.0f,1.0f,1.0f,1.0f) );
geoNode->addDrawable(quadNode);
viewer->setSceneData(geoNode.get());
viewer->addEventHandler(new osgViewer::StatsHandler);
viewer->addEventHandler(new osgGA::StateSetManipulator(viewer->getCamera()->getOrCreateStateSet()));
return viewer->run();
}
2)顶点着色器
static const char* vertShader = {
"#version 400\n"
"layout(location = 0) in vec3 in_Vertex;\n"
"void main(){\n"
" gl_Position = vec4(in_Vertex,1);\n"
"}\n"
};
3)细分控制着色器
static const char* teconShader = {
"#version 400\n"
"layout(vertices = 4) out;\n"
//"in vec4 vPosition;\n"
//"out vec4 tcPosition;\n"
"#define ID gl_InvocationID\n"
"void main()\n"
"{\n"
" gl_out[ID].gl_Position = gl_in[ID].gl_Position;\n"
" if (ID == 0) {\n"
" gl_TessLevelOuter[0] = float(4);\n"
" gl_TessLevelOuter[1] = float(4);\n"
" gl_TessLevelOuter[2] = float(4);\n"
" gl_TessLevelOuter[3] = float(4);\n"
" gl_TessLevelInner[0] = float(3);\n"
" gl_TessLevelInner[1] = float(3);\n"
" }\n"
"}\n"
};
4)细分计算着色器
static const char* teevaShader = {
"#version 400\n"
"layout(quads,equal_spacing,ccw) in;\n"
"void main()\n"
"{\n"
" float u = gl_TessCoord.x;\n"
" float v = gl_TessCoord.y;\n"
" vec4 p0 = gl_in[0].gl_Position;\n"
" vec4 p1 = gl_in[1].gl_Position;\n"
" vec4 p2 = gl_in[2].gl_Position;\n"
" vec4 p3 = gl_in[3].gl_Position;\n"
" gl_Position = p0*(1-u)*(1-v)+p1*u*(1-v)+p3*v*(1-u)+p2*u*v;\n"
" gl_Position = gl_ModelViewProjectionMatrix * gl_Position;\n"
"}\n"
};
片元着色器就不写了,没有影响。
但是出来的是这个:
有没有高手指点一下,真的很困惑 |
|