Added very simply osgparametric example which uses an OpenGL shader program
to create an animated parametric surface.
This commit is contained in:
parent
34be5166ff
commit
900739bd7e
@ -204,6 +204,7 @@ EXAMPLE_DIRS = \
|
|||||||
osgmultiplecameras\
|
osgmultiplecameras\
|
||||||
osgoccluder\
|
osgoccluder\
|
||||||
osgpagedlod\
|
osgpagedlod\
|
||||||
|
osgparametric\
|
||||||
osgparticle\
|
osgparticle\
|
||||||
osgparticleeffects\
|
osgparticleeffects\
|
||||||
osgpbuffer\
|
osgpbuffer\
|
||||||
|
@ -1158,6 +1158,33 @@ Package=<4>
|
|||||||
|
|
||||||
###############################################################################
|
###############################################################################
|
||||||
|
|
||||||
|
Project: "Example osgparametric"=.\examples\osgparametric\osgparametric.dsp - Package Owner=<4>
|
||||||
|
|
||||||
|
Package=<5>
|
||||||
|
{{{
|
||||||
|
}}}
|
||||||
|
|
||||||
|
Package=<4>
|
||||||
|
{{{
|
||||||
|
Begin Project Dependency
|
||||||
|
Project_Dep_Name Core osg
|
||||||
|
End Project Dependency
|
||||||
|
Begin Project Dependency
|
||||||
|
Project_Dep_Name Core osgDB
|
||||||
|
End Project Dependency
|
||||||
|
Begin Project Dependency
|
||||||
|
Project_Dep_Name Core osgGA
|
||||||
|
End Project Dependency
|
||||||
|
Begin Project Dependency
|
||||||
|
Project_Dep_Name Core osgProducer
|
||||||
|
End Project Dependency
|
||||||
|
Begin Project Dependency
|
||||||
|
Project_Dep_Name Core osgUtil
|
||||||
|
End Project Dependency
|
||||||
|
}}}
|
||||||
|
|
||||||
|
###############################################################################
|
||||||
|
|
||||||
Project: "Example osgparticleeffects"=.\examples\osgparticleeffects\osgparticleeffects.dsp - Package Owner=<4>
|
Project: "Example osgparticleeffects"=.\examples\osgparticleeffects\osgparticleeffects.dsp - Package Owner=<4>
|
||||||
|
|
||||||
Package=<5>
|
Package=<5>
|
||||||
|
97
VisualStudio/examples/osgparametric/osgparametric.dsp
Executable file
97
VisualStudio/examples/osgparametric/osgparametric.dsp
Executable file
@ -0,0 +1,97 @@
|
|||||||
|
# Microsoft Developer Studio Project File - Name="Example osgparametric" - Package Owner=<4>
|
||||||
|
# Microsoft Developer Studio Generated Build File, Format Version 6.00
|
||||||
|
# ** DO NOT EDIT **
|
||||||
|
|
||||||
|
# TARGTYPE "Win32 (x86) Console Application" 0x0103
|
||||||
|
|
||||||
|
CFG=Example osgparametric - Win32 Release
|
||||||
|
!MESSAGE This is not a valid makefile. To build this project using NMAKE,
|
||||||
|
!MESSAGE use the Export Makefile command and run
|
||||||
|
!MESSAGE
|
||||||
|
!MESSAGE NMAKE /f "osgparametric.mak".
|
||||||
|
!MESSAGE
|
||||||
|
!MESSAGE You can specify a configuration when running NMAKE
|
||||||
|
!MESSAGE by defining the macro CFG on the command line. For example:
|
||||||
|
!MESSAGE
|
||||||
|
!MESSAGE NMAKE /f "osgparametric.mak" CFG="Example osgparametric - Win32 Release"
|
||||||
|
!MESSAGE
|
||||||
|
!MESSAGE Possible choices for configuration are:
|
||||||
|
!MESSAGE
|
||||||
|
!MESSAGE "Example osgparametric - Win32 Release" (based on "Win32 (x86) Console Application")
|
||||||
|
!MESSAGE "Example osgparametric - Win32 Debug" (based on "Win32 (x86) Console Application")
|
||||||
|
!MESSAGE
|
||||||
|
|
||||||
|
# Begin Project
|
||||||
|
# PROP AllowPerConfigDependencies 0
|
||||||
|
# PROP Scc_ProjName ""
|
||||||
|
# PROP Scc_LocalPath ""
|
||||||
|
CPP=cl.exe
|
||||||
|
RSC=rc.exe
|
||||||
|
|
||||||
|
!IF "$(CFG)" == "Example osgparametric - Win32 Release"
|
||||||
|
|
||||||
|
# PROP BASE Use_MFC 0
|
||||||
|
# PROP BASE Use_Debug_Libraries 0
|
||||||
|
# PROP BASE Output_Dir "Release"
|
||||||
|
# PROP BASE Intermediate_Dir "Release"
|
||||||
|
# PROP BASE Target_Dir ""
|
||||||
|
# PROP Use_MFC 0
|
||||||
|
# PROP Use_Debug_Libraries 0
|
||||||
|
# PROP Output_Dir "Release"
|
||||||
|
# PROP Intermediate_Dir "Release"
|
||||||
|
# PROP Ignore_Export_Lib 0
|
||||||
|
# PROP Target_Dir ""
|
||||||
|
MTL=midl.exe
|
||||||
|
# ADD BASE CPP /nologo /W3 /GX /O2 /D "WIN32" /D "NDEBUG" /D "_CONSOLE" /D "_MBCS" /YX /FD /c
|
||||||
|
# ADD CPP /nologo /MD /W3 /GR /GX /O2 /I "../../../include" /I "../../../../OpenThreads/include" /I "../../../../Producer/include" /I "../../../../3rdParty/include" /D "WIN32" /D "NDEBUG" /D "_CONSOLE" /D "_MBCS" /YX /FD /Zm200 /c
|
||||||
|
# ADD BASE RSC /l 0x809 /d "NDEBUG"
|
||||||
|
# ADD RSC /l 0x809 /d "NDEBUG"
|
||||||
|
BSC32=bscmake.exe
|
||||||
|
# ADD BASE BSC32 /nologo
|
||||||
|
# ADD BSC32 /nologo
|
||||||
|
LINK32=link.exe
|
||||||
|
# ADD BASE LINK32 kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib /nologo /subsystem:console /machine:I386
|
||||||
|
# ADD LINK32 OpenThreadsWin32.lib /nologo /subsystem:console /pdb:none /machine:I386 /out:"../../../bin/osgparametric.exe" /libpath:"../../../lib" /libpath:"../../../../OpenThreads/lib/win32" /libpath:"../../../../Producer/lib" /libpath:"../../../../3rdParty/lib"
|
||||||
|
|
||||||
|
!ELSEIF "$(CFG)" == "Example osgparametric - Win32 Debug"
|
||||||
|
|
||||||
|
# PROP BASE Use_MFC 0
|
||||||
|
# PROP BASE Use_Debug_Libraries 1
|
||||||
|
# PROP BASE Output_Dir "Debug"
|
||||||
|
# PROP BASE Intermediate_Dir "Debug"
|
||||||
|
# PROP BASE Target_Dir ""
|
||||||
|
# PROP Use_MFC 0
|
||||||
|
# PROP Use_Debug_Libraries 1
|
||||||
|
# PROP Output_Dir "Debug"
|
||||||
|
# PROP Intermediate_Dir "Debug"
|
||||||
|
# PROP Ignore_Export_Lib 0
|
||||||
|
# PROP Target_Dir ""
|
||||||
|
MTL=midl.exe
|
||||||
|
# ADD BASE CPP /nologo /W3 /Gm /GX /Zi /Od /D "WIN32" /D "_DEBUG" /D "_CONSOLE" /D "_MBCS" /YX /FD /c
|
||||||
|
# ADD CPP /nologo /MDd /W3 /Gm /GR /GX /Zi /Od /I "../../../include" /I "../../../../OpenThreads/include" /I "../../../../Producer/include" /I "../../../../3rdParty/include" /D "_CONSOLE" /D "_MBCS" /D "FL_DLL" /D "WIN32" /D "_DEBUG" /FR /YX /FD /Zm200 /c
|
||||||
|
# ADD BASE RSC /l 0x809 /d "_DEBUG"
|
||||||
|
# ADD RSC /l 0x809 /d "_DEBUG"
|
||||||
|
BSC32=bscmake.exe
|
||||||
|
# ADD BASE BSC32 /nologo
|
||||||
|
# ADD BSC32 /nologo
|
||||||
|
LINK32=link.exe
|
||||||
|
# ADD BASE LINK32 kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib /nologo /subsystem:console /debug /machine:I386 /pdbtype:sept
|
||||||
|
# ADD LINK32 OpenThreadsWin32d.lib /nologo /subsystem:console /debug /machine:I386 /nodefaultlib:"libcmt" /out:"../../../bin/osgparametricd.exe" /pdbtype:sept /libpath:"../../../lib" /libpath:"../../../../OpenThreads/lib/win32" /libpath:"../../../../Producer/lib" /libpath:"../../../../3rdParty/lib"
|
||||||
|
# SUBTRACT LINK32 /incremental:no
|
||||||
|
|
||||||
|
!ENDIF
|
||||||
|
|
||||||
|
# Begin Target
|
||||||
|
|
||||||
|
# Name "Example osgparametric - Win32 Release"
|
||||||
|
# Name "Example osgparametric - Win32 Debug"
|
||||||
|
# Begin Source File
|
||||||
|
|
||||||
|
SOURCE=..\..\..\examples\osgparametric\osgparametric.cpp
|
||||||
|
# End Source File
|
||||||
|
# End Target
|
||||||
|
# Begin Group "Resource Files"
|
||||||
|
|
||||||
|
# PROP Default_Filter "ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe"
|
||||||
|
# End Group
|
||||||
|
# End Project
|
17
examples/osgparametric/GNUmakefile
Normal file
17
examples/osgparametric/GNUmakefile
Normal file
@ -0,0 +1,17 @@
|
|||||||
|
TOPDIR = ../..
|
||||||
|
include $(TOPDIR)/Make/makedefs
|
||||||
|
|
||||||
|
CXXFILES =\
|
||||||
|
osgparametric.cpp\
|
||||||
|
|
||||||
|
LIBS += -losgProducer -lProducer -losgText -losgGA -losgDB -losgUtil -losg $(GL_LIBS) $(X_LIBS) $(OTHER_LIBS)
|
||||||
|
|
||||||
|
INSTFILES = \
|
||||||
|
$(CXXFILES)\
|
||||||
|
GNUmakefile.inst=GNUmakefile
|
||||||
|
|
||||||
|
EXEC = osgparametric
|
||||||
|
|
||||||
|
INC += $(X_INC)
|
||||||
|
|
||||||
|
include $(TOPDIR)/Make/makerules
|
14
examples/osgparametric/GNUmakefile.inst
Normal file
14
examples/osgparametric/GNUmakefile.inst
Normal file
@ -0,0 +1,14 @@
|
|||||||
|
TOPDIR = ../..
|
||||||
|
include $(TOPDIR)/Make/makedefs
|
||||||
|
|
||||||
|
CXXFILES =\
|
||||||
|
osgparametric.cpp\
|
||||||
|
|
||||||
|
LIBS += -losgProducer -lProducer -losgDB -losgText -losgUtil -losg $(GL_LIBS) $(X_LIBS) $(OTHER_LIBS)
|
||||||
|
|
||||||
|
EXEC = osgparametric
|
||||||
|
|
||||||
|
INC += $(PRODUCER_INCLUDE_DIR) $(X_INC)
|
||||||
|
LDFLAGS += $(PRODUCER_LIB_DIR)
|
||||||
|
|
||||||
|
include $(TOPDIR)/Make/makerules
|
193
examples/osgparametric/osgparametric.cpp
Normal file
193
examples/osgparametric/osgparametric.cpp
Normal file
@ -0,0 +1,193 @@
|
|||||||
|
#include <osg/Vec3>
|
||||||
|
#include <osg/Vec4>
|
||||||
|
#include <osg/Quat>
|
||||||
|
#include <osg/Matrix>
|
||||||
|
#include <osg/ShapeDrawable>
|
||||||
|
#include <osg/Geometry>
|
||||||
|
#include <osg/Geode>
|
||||||
|
#include <osg/Texture2D>
|
||||||
|
|
||||||
|
#include <osgDB/FileUtils>
|
||||||
|
#include <osgDB/ReadFile>
|
||||||
|
|
||||||
|
#include <osgUtil/Optimizer>
|
||||||
|
|
||||||
|
#include <osgProducer/Viewer>
|
||||||
|
|
||||||
|
char vertexShaderSource[] =
|
||||||
|
"uniform vec2 xCoeff; \n"
|
||||||
|
"uniform vec2 yCoeff; \n"
|
||||||
|
"\n"
|
||||||
|
"void main(void) \n"
|
||||||
|
"{ \n"
|
||||||
|
"\n"
|
||||||
|
" gl_TexCoord[0] = gl_Vertex; \n"
|
||||||
|
" gl_Vertex.z = gl_Vertex.x*xCoeff[0] + gl_Vertex.x*gl_Vertex.x* xCoeff[1] + \n"
|
||||||
|
" gl_Vertex.y*yCoeff[1] + gl_Vertex.y*gl_Vertex.y* yCoeff[1]; \n"
|
||||||
|
" gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;\n"
|
||||||
|
"}\n";
|
||||||
|
|
||||||
|
char fragmentShaderSource[] =
|
||||||
|
"uniform sampler2D baseTexture; \n"
|
||||||
|
"\n"
|
||||||
|
"void main(void) \n"
|
||||||
|
"{ \n"
|
||||||
|
" gl_FragColor = texture2D( baseTexture, gl_TexCoord[0].xy); \n"
|
||||||
|
"}\n";
|
||||||
|
|
||||||
|
|
||||||
|
class UniformVarying : public osg::Uniform::Callback
|
||||||
|
{
|
||||||
|
virtual void operator () (osg::Uniform* uniform, osg::NodeVisitor* nv)
|
||||||
|
{
|
||||||
|
const osg::FrameStamp* fs = nv->getFrameStamp();
|
||||||
|
float value = sinf(fs->getReferenceTime());
|
||||||
|
uniform->set(osg::Vec2(value,-value));
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
osg::Node* createModel()
|
||||||
|
{
|
||||||
|
osg::Geode* geode = new osg::Geode;
|
||||||
|
|
||||||
|
osg::Geometry* geom = new osg::Geometry;
|
||||||
|
geode->addDrawable(geom);
|
||||||
|
|
||||||
|
// dimensions for ~one million triangles :-)
|
||||||
|
unsigned int num_x = 708;
|
||||||
|
unsigned int num_y = 708;
|
||||||
|
|
||||||
|
osg::Vec3Array* vertices = new osg::Vec3Array( num_x * num_y );
|
||||||
|
|
||||||
|
float dx = 1.0f/(float)(num_x-1);
|
||||||
|
float dy = 1.0f/(float)(num_y-1);
|
||||||
|
osg::Vec3 row(0.0f,0.0f,0.0);
|
||||||
|
|
||||||
|
unsigned int vert_no = 0;
|
||||||
|
for(unsigned int iy=0; iy<num_y; ++iy)
|
||||||
|
{
|
||||||
|
osg::Vec3 column = row;
|
||||||
|
for(unsigned int ix=0;ix<num_x;++ix)
|
||||||
|
{
|
||||||
|
(*vertices)[vert_no++] = column;
|
||||||
|
column.x() += dx;
|
||||||
|
}
|
||||||
|
row.y() += dy;
|
||||||
|
}
|
||||||
|
|
||||||
|
geom->setVertexArray(vertices);
|
||||||
|
|
||||||
|
for(unsigned int iy=0; iy<num_y-1; ++iy)
|
||||||
|
{
|
||||||
|
unsigned int element_no = 0;
|
||||||
|
osg::DrawElementsUInt* elements = new osg::DrawElementsUInt(GL_TRIANGLE_STRIP, num_x*2);
|
||||||
|
unsigned int index = iy * num_x;
|
||||||
|
for(unsigned int ix = 0; ix<num_x; ++ix)
|
||||||
|
{
|
||||||
|
(*elements)[element_no++] = index + num_x;
|
||||||
|
(*elements)[element_no++] = index++;
|
||||||
|
}
|
||||||
|
geom->addPrimitiveSet(elements);
|
||||||
|
}
|
||||||
|
|
||||||
|
geom->setUseVertexBufferObjects(true);
|
||||||
|
|
||||||
|
|
||||||
|
osg::StateSet* stateset = geom->getOrCreateStateSet();
|
||||||
|
|
||||||
|
osg::Program* program = new osg::Program;
|
||||||
|
stateset->setAttribute(program);
|
||||||
|
|
||||||
|
osg::Shader* vertex_shader = new osg::Shader(osg::Shader::VERTEX, vertexShaderSource);
|
||||||
|
program->addShader(vertex_shader);
|
||||||
|
|
||||||
|
|
||||||
|
osg::Shader* fragment_shader = new osg::Shader(osg::Shader::FRAGMENT, fragmentShaderSource);
|
||||||
|
program->addShader(fragment_shader);
|
||||||
|
|
||||||
|
|
||||||
|
osg::Uniform* xCoeff = new osg::Uniform("xCoeff",osg::Vec2(1.0,-1.0f));
|
||||||
|
xCoeff->setUpdateCallback(new UniformVarying);
|
||||||
|
stateset->addUniform(xCoeff);
|
||||||
|
|
||||||
|
osg::Uniform* yCoeff = new osg::Uniform("yCoeff",osg::Vec2(-1.0f,1.0f));
|
||||||
|
stateset->addUniform(yCoeff);
|
||||||
|
|
||||||
|
|
||||||
|
stateset->setTextureAttributeAndModes(0,new osg::Texture2D(osgDB::readImageFile("lz.rgb")));
|
||||||
|
|
||||||
|
osg::Uniform* baseTextureSampler = new osg::Uniform("baseTexture",0);
|
||||||
|
stateset->addUniform(baseTextureSampler);
|
||||||
|
|
||||||
|
return geode;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
int main(int argc, char *argv[])
|
||||||
|
{
|
||||||
|
// use an ArgumentParser object to manage the program arguments.
|
||||||
|
osg::ArgumentParser arguments(&argc,argv);
|
||||||
|
|
||||||
|
// set up the usage document, in case we need to print out how to use this program.
|
||||||
|
arguments.getApplicationUsage()->setDescription(arguments.getApplicationName()+" is the example which demonstrate support for ARB_vertex_program.");
|
||||||
|
arguments.getApplicationUsage()->setCommandLineUsage(arguments.getApplicationName()+" [options] filename ...");
|
||||||
|
arguments.getApplicationUsage()->addCommandLineOption("-h or --help","Display this information");
|
||||||
|
|
||||||
|
// construct the viewer.
|
||||||
|
osgProducer::Viewer viewer(arguments);
|
||||||
|
|
||||||
|
// set up the value with sensible default event handlers.
|
||||||
|
viewer.setUpViewer(osgProducer::Viewer::STANDARD_SETTINGS);
|
||||||
|
|
||||||
|
// get details on keyboard and mouse bindings used by the viewer.
|
||||||
|
viewer.getUsage(*arguments.getApplicationUsage());
|
||||||
|
|
||||||
|
// if user request help write it out to cout.
|
||||||
|
if (arguments.read("-h") || arguments.read("--help"))
|
||||||
|
{
|
||||||
|
arguments.getApplicationUsage()->write(std::cout);
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
// any option left unread are converted into errors to write out later.
|
||||||
|
arguments.reportRemainingOptionsAsUnrecognized();
|
||||||
|
|
||||||
|
// report any errors if they have occured when parsing the program aguments.
|
||||||
|
if (arguments.errors())
|
||||||
|
{
|
||||||
|
arguments.writeErrorMessages(std::cout);
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
// load the nodes from the commandline arguments.
|
||||||
|
osg::Node* model = createModel();
|
||||||
|
if (!model)
|
||||||
|
{
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
// add a viewport to the viewer and attach the scene graph.
|
||||||
|
viewer.setSceneData(model);
|
||||||
|
|
||||||
|
// create the windows and run the threads.
|
||||||
|
viewer.realize();
|
||||||
|
|
||||||
|
while( !viewer.done() )
|
||||||
|
{
|
||||||
|
// wait for all cull and draw threads to complete.
|
||||||
|
viewer.sync();
|
||||||
|
|
||||||
|
// update the scene by traversing it with the the update visitor which will
|
||||||
|
// call all node update callbacks and animations.
|
||||||
|
viewer.update();
|
||||||
|
|
||||||
|
// fire off the cull and draw traversals of the scene.
|
||||||
|
viewer.frame();
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
// wait for all cull and draw threads to complete before exit.
|
||||||
|
viewer.sync();
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user