Created
December 7, 2023 17:24
-
-
Save gwaldron/8da30ff660a3addc34ba997b9befc890 to your computer and use it in GitHub Desktop.
Simple texture + matrix shader animation.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <osgViewer/Viewer> | |
#include <osg/Geometry> | |
#include <osg/Texture2D> | |
#include <osgEarth/Utils> | |
#include <osgDB/ReadFile> | |
using namespace osgEarth; | |
int main(int argc, char** argv) | |
{ | |
osg::ArgumentParser arguments(&argc, argv); | |
osgViewer::Viewer viewer(arguments); | |
auto geom = new osg::Geometry(); | |
geom->setUseVertexBufferObjects(true); | |
geom->setUseDisplayList(false); | |
osg::Vec3f verts[4] = { {0,0,0}, {1,0,0}, {1,0,1}, {0,0,1} }; | |
geom->setVertexArray(new osg::Vec3Array(4, verts)); | |
osg::Vec2f uvs[4] = { {0,0}, {1,0}, {1,1}, {0,1} }; | |
geom->setTexCoordArray(0, new osg::Vec2Array(4, uvs)); | |
std::uint8_t indices[6] = { 0,1,2, 0,2,3 }; | |
geom->addPrimitiveSet(new osg::DrawElementsUByte(GL_TRIANGLES, 6, indices)); | |
auto ss = geom->getOrCreateStateSet(); | |
auto image = osgDB::readRefImageFile("https://github.com/gwaldron/osgearth/blob/master/data/icon.png?raw=true"); | |
auto tex = new osg::Texture2D(image); | |
ss->setTextureAttributeAndModes(0, tex, 1); | |
auto mat = new osg::Uniform("mat", osg::Matrixf::identity()); | |
ss->addUniform(mat); | |
auto vs = new osg::Shader(osg::Shader::VERTEX, R"( | |
#version 130 | |
out vec2 uv; | |
void main(void) { | |
gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex; | |
uv = gl_MultiTexCoord0.st; | |
} | |
)"); | |
auto fs = new osg::Shader(osg::Shader::FRAGMENT, R"( | |
#version 130 | |
in vec2 uv; | |
uniform sampler2D tex; | |
uniform mat4 mat; | |
out vec4 frag_color; | |
void main(void) { | |
frag_color = texture(tex, (mat*vec4(uv,0,1)).st); | |
} | |
)"); | |
auto program = new osg::Program(); | |
program->addShader(vs); | |
program->addShader(fs); | |
ss->setAttribute(program, 1); | |
geom->setUpdateCallback(new LambdaCallback<>([mat](osg::NodeVisitor& nv) { | |
static float n = 0.0f; | |
osg::Matrixf m; | |
float sn = sin(n), cn = cos(n); | |
m(3, 0) = (0.25 * sn); | |
m(3, 1) = (0.25 * cn); | |
mat->set(m); | |
n += 0.01; | |
})); | |
viewer.setThreadingModel(viewer.SingleThreaded); | |
viewer.setSceneData(geom); | |
return viewer.run(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment