2021-03-29 23:12:15 +08:00
|
|
|
// This is public domain software and comes with
|
|
|
|
// absolutely no warranty. Use of public domain software
|
|
|
|
// may vary between counties, but in general you are free
|
|
|
|
// to use and distribute this software for any purpose.
|
|
|
|
|
|
|
|
|
|
|
|
// Example: OSG using an OpenGL 3.1 context.
|
|
|
|
// The comment block at the end of the source describes building OSG
|
|
|
|
// for use with OpenGL 3.x.
|
|
|
|
|
|
|
|
#include <osgViewer/Viewer>
|
|
|
|
#include <osgDB/ReadFile>
|
2021-04-01 00:34:39 +08:00
|
|
|
#include <osgDB/WriteFile>
|
2021-03-29 23:12:15 +08:00
|
|
|
#include <osg/GraphicsContext>
|
|
|
|
#include <osg/Camera>
|
|
|
|
#include <osg/Viewport>
|
|
|
|
#include <osg/StateSet>
|
|
|
|
#include <osg/Program>
|
|
|
|
#include <osg/Shader>
|
2021-04-01 00:34:39 +08:00
|
|
|
#include <osg/DrawMeshTasks>
|
2021-03-29 23:12:15 +08:00
|
|
|
#include <osgUtil/Optimizer>
|
|
|
|
|
|
|
|
int main( int argc, char** argv )
|
|
|
|
{
|
|
|
|
osg::ArgumentParser arguments( &argc, argv );
|
|
|
|
|
|
|
|
static const char* meshSource = \
|
|
|
|
"#version 450 \n"
|
|
|
|
"#extension GL_NV_mesh_shader : enable\n"
|
|
|
|
"layout(local_size_x = 3) in;"
|
|
|
|
"layout(max_vertices = 64) out;"
|
|
|
|
"layout(max_primitives = 126) out;"
|
|
|
|
"layout(triangles) out;"
|
|
|
|
"const vec3 vertices[3] = {vec3(-1,-1,0), vec3(1,-1,0), vec3(0,1,0)};"
|
|
|
|
"void main()"
|
|
|
|
"{"
|
|
|
|
"uint id = gl_LocalInvocationID.x;"
|
|
|
|
"gl_MeshVerticesNV[id].gl_Position = vec4(vertices[id], 2);"
|
|
|
|
"gl_PrimitiveIndicesNV[id] = id;"
|
|
|
|
"gl_PrimitiveCountNV = 1;"
|
|
|
|
"}";
|
|
|
|
|
|
|
|
|
|
|
|
static const char* fragmentSource = \
|
|
|
|
"#version 450 \n"
|
|
|
|
"#extension GL_NV_fragment_shader_barycentric : enable\n"
|
|
|
|
"out vec4 color;"
|
|
|
|
"void main()"
|
|
|
|
"{"
|
|
|
|
"color = vec4(gl_BaryCoordNV, 1.0);"
|
|
|
|
"}";
|
|
|
|
|
|
|
|
osg::ref_ptr<osg::Shader> vShader = new osg::Shader( osg::Shader::MESH, meshSource );
|
|
|
|
osg::ref_ptr<osg::Shader> fShader = new osg::Shader( osg::Shader::FRAGMENT, fragmentSource );
|
|
|
|
|
|
|
|
osg::ref_ptr<osg::Program> program = new osg::Program;
|
|
|
|
program->addShader( vShader.get() );
|
|
|
|
program->addShader( fShader.get() );
|
|
|
|
|
2021-04-01 00:34:39 +08:00
|
|
|
osg::ref_ptr<osg::Node> drawMesh = new osg::DrawMeshTasks(0, 1);
|
2021-03-29 23:12:15 +08:00
|
|
|
drawMesh->getOrCreateStateSet()->setAttribute( program.get() );
|
|
|
|
|
2021-04-01 00:34:39 +08:00
|
|
|
osgDB::writeNodeFile(*drawMesh, "test.osgt");
|
|
|
|
|
2021-03-31 20:50:13 +08:00
|
|
|
osgViewer::Viewer viewer(arguments);
|
2021-03-29 23:12:15 +08:00
|
|
|
|
|
|
|
viewer.setSceneData( drawMesh );
|
|
|
|
|
|
|
|
// for non GL3/GL4 and non GLES2 platforms we need enable the osg_ uniforms that the shaders will use,
|
|
|
|
// you don't need thse two lines on GL3/GL4 and GLES2 specific builds as these will be enable by default.
|
2021-03-31 20:50:13 +08:00
|
|
|
//gc->getState()->setUseModelViewAndProjectionUniforms(true);
|
|
|
|
//gc->getState()->setUseVertexAttributeAliasing(true);
|
2021-03-29 23:12:15 +08:00
|
|
|
|
|
|
|
return( viewer.run() );
|
|
|
|
}
|