#include <osgViewer/Viewer
>
#include <osg/Geometry
>
#include <osg/Material
>
#include <osgDB/ReadFile
>
#include <osg/Image
>
#include <osg/Texture2D
>
#include <osg/Texture1D
>
void configureShaders(osg
::StateSet* stateSet)
{
const std
::string vertexSource
=
"#version 330\n"
"\n"
"layout (location = 0) in vec4 Position;\n"
"layout (location = 1) in vec4 color;\n"
"layout (location = 2) in vec3 Normal;\n"
"layout (location = 3) in vec2 Texcoord;\n"
"uniform mat3 osg_NormalMatrix;\n"
"out vec4 vcolor;\n"
"out vec3 Norm;\n"
"out vec2 texcoord;\n"
"void main()\n"
"{\n"
" gl_Position = Position;\n"
" vcolor = color;\n"
" Norm = osg_NormalMatrix * Normal; \n"
" texcoord = Texcoord; \n"
"} \n";
osg
::Shader* vShader
= new osg
::Shader(osg
::Shader::VERTEX, vertexSource);
const std
::string geomSource
=
"#version 330 \n"
"layout(triangles) in; \n"
"layout(triangle_strip) out; \n"
"layout(max_vertices = 9) out; \n"
"uniform mat4 osg_ModelViewProjectionMatrix; \n"
"in vec4 vcolor[];\n"
"in vec3 Norm[]; \n"
"in vec2 texcoord[]; \n "
"out vec2 tex; \n"
"out vec4 fcolor; \n"
"out vec3 norm; \n"
"uniform sampler2D Tex;\n"
"uniform sampler1D Tex1D;\n"
"void main(void) \n"
"{ \n"
" for (int i = 0; i < gl_in.length(); i++) \n"
" { \n"
" gl_Position = osg_ModelViewProjectionMatrix*gl_in[i].gl_Position; \n"
" tex=texcoord[i]; \n"
" fcolor=vcolor[i]; \n"
" norm=Norm[i]; \n"
" EmitVertex(); \n"
" } \n"
" EndPrimitive(); \n"
" gl_Position = osg_ModelViewProjectionMatrix*gl_in[0].gl_Position; \n"
" tex=texcoord[0]; \n"
" fcolor=vcolor[0]; \n"
" norm=Norm[0]; \n"
" EmitVertex(); \n"
" gl_Position = osg_ModelViewProjectionMatrix*gl_in[2].gl_Position; \n"
" tex=texcoord[2]; \n"
" fcolor=vcolor[2]; \n"
" norm=Norm[2]; \n"
" EmitVertex(); \n"
" gl_Position = osg_ModelViewProjectionMatrix*vec4(1.0,0.0,0.0,1.0); \n"
" tex=texcoord[1]; \n"
" fcolor=vcolor[2]; \n"
" norm=Norm[0]; \n"
" EmitVertex(); \n"
" EndPrimitive(); \n"
"} \n";
osg
::Shader* gShader
= new osg
::Shader(osg
::Shader::GEOMETRY, geomSource);
const std
::string fragmentSource
=
"#version 330 \n"
" \n"
"layout (location = 0) out vec4 fragData; \n"
" \n"
"uniform sampler2D Texture;\n"
"in vec4 fcolor; \n"
"in vec3 norm; \n"
"in vec2 tex; \n"
"void main() \n"
"{ \n"
"vec4 textureColor=texture(Texture,tex) * fcolor;\n"
" fragData = textureColor; \n"
"} \n";
osg
::Shader* fShader
= new osg
::Shader(osg
::Shader::FRAGMENT, fragmentSource);
osg
::Program* program
= new osg
::Program;
program
->addShader(vShader);
program
->addShader(gShader);
program
->addShader(fShader);
stateSet
->setAttribute(program);
}
void main()
{
osg
::ref_ptr<osg
::Group> root
= new osg
::Group;
osgViewer
::Viewer viewer;
osg
::ref_ptr<osg
::Vec3Array> vertices
= new osg
::Vec3Array;
vertices
->push_back(osg
::Vec3(
0.0f,
0.0f,
0.0f));
vertices
->push_back(osg
::Vec3(
0.0f,
0.0f,
1.0f));
vertices
->push_back(osg
::Vec3(
1.0f,
0.0f,
1.0f));
osg
::ref_ptr<osg
::Vec4Array> V_C
= new osg
::Vec4Array;
V_C
->push_back(osg
::Vec4(
0.0,
1.0,
1.0,
1.0));
V_C
->push_back(osg
::Vec4(
0.0,
0.0,
1.0,
1.0));
V_C
->push_back(osg
::Vec4(
1.0,
1.0,
1.0,
1.0));
V_C
->push_back(osg
::Vec4(
0.0,
1.0,
0.0,
1.0));
osg
::ref_ptr<osg
::Vec3Array> V_N
= new osg
::Vec3Array;
V_N
->push_back(osg
::Vec3(
0.0,
1.0,
0.0));
osg
::ref_ptr<osg
::Vec2Array> V_T
= new osg
::Vec2Array;
V_T
->push_back(osg
::Vec2(
0.0,
0.0));
V_T
->push_back(osg
::Vec2(
1.0,
0.0));
V_T
->push_back(osg
::Vec2(
1.0,
1.0));
V_T
->push_back(osg
::Vec2(
0.0,
1.0));
osg
::ref_ptr<osg
::Geometry> geom
= new osg
::Geometry();
geom
->setVertexArray(vertices);
geom
->setVertexAttribArray(
1, V_C);
geom
->setVertexAttribBinding(
1, osg
::Geometry::BIND_PER_VERTEX);
geom
->setVertexAttribArray(
2, V_N);
geom
->setVertexAttribBinding(
2, osg
::Geometry::BIND_OVERALL);
geom
->setTexCoordArray(
0, V_T);
osg
::ref_ptr<osg
::Image> image1
= osgDB
::readImageFile(
"Images/brick.jpg");
if (
!image1)
{
std
::cout << " cannot Open image !!" << std
::endl;
}
osg
::ref_ptr<osg
::Texture2D> texture1
= new osg
::Texture2D();
texture1
->setDataVariance(osg
::Object::DYNAMIC);
texture1
->setImage(image1
.get());
geom
->getOrCreateStateSet()
->setTextureAttributeAndModes(
0, texture1
.get(), osg
::StateAttribute::ON);
geom
->getOrCreateStateSet()
->addUniform(
new osg
::Uniform(
"Texture",
0));
geom
->getOrCreateStateSet()
->addUniform(
new osg
::Uniform(
"Tex",
0));
geom
->addPrimitiveSet(
new osg
::DrawArrays(GL_TRIANGLES,
0,
3));
configureShaders(geom
->getOrCreateStateSet());
root
->addChild(geom);
const int width(
800), height(
450);
const std
::string version(
"3.1");
osg
::ref_ptr< osg
::GraphicsContext::Traits > traits
= new osg
::GraphicsContext::Traits();
traits
->x
= 50; traits
->y
= 30;
traits
->width
= width; traits
->height
= height;
traits
->windowDecoration
= true;
traits
->doubleBuffer
= true;
traits
->glContextVersion
= version;
osg
::ref_ptr< osg
::GraphicsContext > gc
= osg
::GraphicsContext::createGraphicsContext(traits
.get());
if (
!gc
.valid())
{
osg
::notify(osg
::FATAL)
<< "Unable to create OpenGL v" << version
<< " context." << std
::endl;
return;
}
osg
::Camera* cam
= viewer
.getCamera();
cam
->setGraphicsContext(gc
.get());
cam
->setProjectionMatrix(osg
::Matrix::perspective(
30., (double)width
/ (double)height,
1.,
100.));
cam
->setViewport(
new osg
::Viewport(
0,
0, width, height));
gc
->getState()
->setUseModelViewAndProjectionUniforms(
true);
gc
->getState()
->setUseVertexAttributeAliasing(
true);
viewer
.setSceneData(root
.get());
viewer
.run();
return;
}