Last active
November 2, 2020 17:29
-
-
Save vicrucann/bf3daadcc3af95f1c4bbee11f1b83c51 to your computer and use it in GitHub Desktop.
Pass through example of OpenSceneGraph vertex + shader shaders with OpenGL 4.00
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 <Windows.h> // comment if not Windows | |
#include <osg/Camera> | |
#include <osg/Drawable> | |
#include <osg/Geode> | |
#include <osg/Geometry> | |
#include <osg/Group> | |
#include <osg/Node> | |
#include <osg/NodeVisitor> | |
#include <osg/Object> | |
#include <osg/PrimitiveSet> | |
#include <osg/Program> | |
#include <osg/Shader> | |
#include <osg/StateSet> | |
#include <osg/Transform> | |
#include <osg/Uniform> | |
#include <osgViewer/Viewer> | |
#define GLSL400(src) "#version 400\n" #src | |
const char* vertSource = GLSL400 ( | |
uniform mat4 ModelViewProjectionMatrix; | |
layout(location = 0) in vec4 Vertex; | |
void main(void) | |
{ | |
gl_Position = ModelViewProjectionMatrix * Vertex; | |
} | |
); | |
const char* fragSource = GLSL400( | |
void main(void) | |
{ | |
gl_FragColor = vec4(0,1,0,1); | |
} | |
); | |
struct ModelViewProjectionMatrixCallback: public osg::Uniform::Callback { | |
ModelViewProjectionMatrixCallback(osg::Camera* camera) : | |
_camera(camera) { | |
} | |
virtual void operator()(osg::Uniform* uniform, osg::NodeVisitor* nv) { | |
osg::Matrixd viewMatrix = _camera->getViewMatrix(); | |
osg::Matrixd modelMatrix = osg::computeLocalToWorld(nv->getNodePath()); | |
osg::Matrixd modelViewProjectionMatrix = modelMatrix * viewMatrix * _camera->getProjectionMatrix(); | |
uniform->set(modelViewProjectionMatrix); | |
} | |
osg::Camera* _camera; | |
}; | |
int main(int, char**) | |
{ | |
::SetProcessDPIAware(); // comment if not windows | |
osgViewer::Viewer viewer; | |
osg::Camera* camera = viewer.getCamera(); | |
osg::ref_ptr<osg::Group> root = new osg::Group(); | |
osg::Vec3Array* vertices = new osg::Vec3Array; | |
osg::Vec4Array* colors = new osg::Vec4Array; | |
vertices->setName("Vertex"); | |
vertices->push_back(osg::Vec3f(0,0,0)); | |
vertices->push_back(osg::Vec3f(1,0,0)); | |
vertices->push_back(osg::Vec3f(1,0,1)); | |
vertices->push_back(osg::Vec3f(0,0,1)); | |
colors->setName("Color"); | |
colors->push_back(osg::Vec4f(1,0,0,1)); | |
colors->push_back(osg::Vec4f(0,1,0,1)); | |
colors->push_back(osg::Vec4f(1,0,1,1)); | |
colors->push_back(osg::Vec4f(0,0,1,1)); | |
// create geometry | |
osg::ref_ptr<osg::Geometry> geom = new osg::Geometry; | |
geom->addPrimitiveSet(new osg::DrawArrays(osg::PrimitiveSet::QUADS,0,vertices->size())); | |
geom->setUseDisplayList(false); | |
// defaults | |
geom->setVertexArray(vertices); | |
geom->setColorArray(colors, osg::Array::BIND_PER_VERTEX); | |
// set attributes | |
geom->setVertexAttribArray(0, vertices, osg::Array::BIND_PER_VERTEX); | |
// create shader | |
osg::ref_ptr<osg::Program> program = new osg::Program; | |
program->setName("shader"); | |
program->addShader(new osg::Shader(osg::Shader::VERTEX, vertSource)); | |
program->addShader(new osg::Shader(osg::Shader::FRAGMENT, fragSource)); | |
osg::ref_ptr<osg::Geode> geode = new osg::Geode; | |
geode->addDrawable(geom.get()); | |
osg::StateSet* state = geode->getOrCreateStateSet(); | |
state->setAttributeAndModes(program.get(), osg::StateAttribute::ON); | |
// add uniforms | |
osg::Uniform* modelViewProjectionMatrix = new osg::Uniform(osg::Uniform::FLOAT_MAT4, "ModelViewProjectionMatrix"); | |
modelViewProjectionMatrix->setUpdateCallback(new ModelViewProjectionMatrixCallback(camera)); | |
state->addUniform(modelViewProjectionMatrix); | |
// turn lights off | |
state->setMode(GL_LIGHTING, osg::StateAttribute::OFF); | |
state->setMode(GL_BLEND, osg::StateAttribute::ON); | |
// scene state run | |
root->addChild(geode.get()); | |
viewer.setSceneData(root.get()); | |
viewer.setUpViewOnSingleScreen(0); | |
return viewer.run(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment