From 1c9993dd5db43597f472b627eda1c3595cd894f7 Mon Sep 17 00:00:00 2001 From: Robert Osfield Date: Thu, 27 Nov 2014 15:54:24 +0000 Subject: [PATCH] Added in source shaders git-svn-id: http://svn.openscenegraph.org/osg/OpenSceneGraph/trunk@14554 16af8721-9629-0410-8352-f15c8da7e697 --- src/osgTerrain/GeometryPool.cpp | 11 +++++++ .../terrain_displacement_mapping_frag.cpp | 21 ++++++++++++ .../terrain_displacement_mapping_vert.cpp | 33 +++++++++++++++++++ 3 files changed, 65 insertions(+) create mode 100644 src/osgTerrain/shaders/terrain_displacement_mapping_frag.cpp create mode 100644 src/osgTerrain/shaders/terrain_displacement_mapping_vert.cpp diff --git a/src/osgTerrain/GeometryPool.cpp b/src/osgTerrain/GeometryPool.cpp index f10cca622..d0f60738d 100644 --- a/src/osgTerrain/GeometryPool.cpp +++ b/src/osgTerrain/GeometryPool.cpp @@ -559,10 +559,21 @@ osg::ref_ptr GeometryPool::getOrCreateProgram(LayerTypes& layerTyp // OSG_NOTICE<<") creating new Program "< vertex_shader = osgDB::readShaderFile("shaders/terrain_displacement_mapping.vert"); + if (!vertex_shader) + { + #include "shaders/terrain_displacement_mapping_vert.cpp" + vertex_shader = new osg::Shader(osg::Shader::VERTEX, terrain_displacement_mapping_vert); + } program->addShader(vertex_shader.get()); osg::ref_ptr fragment_shader = osgDB::readShaderFile("shaders/terrain_displacement_mapping.frag"); + if (!fragment_shader) + { + #include "shaders/terrain_displacement_mapping_frag.cpp" + fragment_shader = new osg::Shader(osg::Shader::FRAGMENT, terrain_displacement_mapping_frag); + } program->addShader(fragment_shader.get()); return program; diff --git a/src/osgTerrain/shaders/terrain_displacement_mapping_frag.cpp b/src/osgTerrain/shaders/terrain_displacement_mapping_frag.cpp new file mode 100644 index 000000000..211f1049e --- /dev/null +++ b/src/osgTerrain/shaders/terrain_displacement_mapping_frag.cpp @@ -0,0 +1,21 @@ +char terrain_displacement_mapping_frag[] = "uniform sampler2D colorTexture1;\n" + "uniform sampler2D colorTexture2;\n" + "uniform sampler2D colorTexture3;\n" + "\n" + "varying vec2 texcoord;\n" + "varying vec4 basecolor;\n" + "\n" + "void main(void)\n" + "{\n" + " float multiplier = 1.0/3.0;\n" + " vec4 color = texture2D( colorTexture1, texcoord)*multiplier +\n" + " texture2D( colorTexture2, texcoord)*multiplier +\n" + " texture2D( colorTexture3, texcoord)*multiplier;\n" + "\n" + "#if 1\n" + " gl_FragColor = basecolor * color;\n" + "#else\n" + " gl_FragColor = basecolor;//basecolor * color;\n" + "#endif\n" + "}\n" + "\n"; diff --git a/src/osgTerrain/shaders/terrain_displacement_mapping_vert.cpp b/src/osgTerrain/shaders/terrain_displacement_mapping_vert.cpp new file mode 100644 index 000000000..34b06dad7 --- /dev/null +++ b/src/osgTerrain/shaders/terrain_displacement_mapping_vert.cpp @@ -0,0 +1,33 @@ +char terrain_displacement_mapping_vert[] = "uniform sampler2D terrainTexture;\n" + "varying vec2 texcoord;\n" + "varying vec4 basecolor;\n" + "\n" + "void main(void)\n" + "{\n" + " texcoord = gl_MultiTexCoord0.xy;\n" + " vec2 texelWorldRatio = gl_MultiTexCoord0.zw;\n" + " vec2 texelTexcoordSize = gl_Color.xy;\n" + "\n" + " vec2 texcoord_right = texcoord;\n" + " if (texcoord.x<0.5) texcoord_right.x = texcoord_right.x+texelTexcoordSize.x;\n" + " else texcoord_right.x = texcoord_right.x-texelTexcoordSize.x;\n" + "\n" + " vec2 texcoord_up = texcoord;\n" + " if (texcoord.y<0.5) texcoord_up.y = texcoord_up.y+texelTexcoordSize.y;\n" + " else texcoord_up.y = texcoord_up.y-texelTexcoordSize.y;\n" + "\n" + "\n" + " float height_center = texture2D(terrainTexture, texcoord).r;\n" + " float height_right = texture2D(terrainTexture, texcoord_right).r;\n" + " float height_up = texture2D(terrainTexture, texcoord_up).r;\n" + "\n" + " vec2 gradient = vec2((height_center-height_right)*texelWorldRatio.x, (height_center-height_up)*texelWorldRatio.y);\n" + " vec3 normal = normalize(vec3(gradient.x, gradient.y, 1.0));\n" + " float intensity = normal.z;\n" + " basecolor = vec4(intensity, intensity, intensity, 1.0);\n" + "\n" + " vec3 position = gl_Vertex.xyz + gl_Normal.xyz * height_center ;\n" + " gl_Position = gl_ModelViewProjectionMatrix * vec4(position,1.0);\n" + "\n" + "};\n" + "\n";