Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 7 additions & 12 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,19 +1,14 @@
add_library(
osgTools STATIC osgTools/StatsHandler.cpp include/osgTools/StatsHandler.h
include/osgTools/Version.h)
set(PUBLIC_HEADERS
include/osgTools/GraphicsOperations.h include/osgTools/PointShaderProgram.h
include/osgTools/StatsHandler.h include/osgTools/Version.h)
add_library(osgTools STATIC osgTools/PointShaderProgram.cpp
osgTools/StatsHandler.cpp "${PUBLIC_HEADERS}")

add_library(osgTools::osgTools ALIAS osgTools)

target_link_libraries(osgTools PUBLIC ${OPENSCENEGRAPH_LIBRARIES})
target_sources(
osgTools
PUBLIC FILE_SET
HEADERS
BASE_DIRS
include
FILES
include/osgTools/StatsHandler.h
include/osgTools/Version.h)
target_sources(osgTools PUBLIC FILE_SET HEADERS BASE_DIRS include FILES
"${PUBLIC_HEADERS}")

install(
TARGETS osgTools
Expand Down
25 changes: 25 additions & 0 deletions src/include/osgTools/GraphicsOperations.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#pragma once

#include <osg/GraphicsContext>

namespace osgTools {
//
// Graphics Operations
//

// UseVertexAttributeAliasing
//
// Enable with:
// viewer.setRealizeOperation(new UseVertexAttributeAliasing());
//
class UseVertexAttributeAliasing : public osg::GraphicsOperation {
public:
UseVertexAttributeAliasing()
: osg::Referenced(true),
osg::GraphicsOperation("GraphicsOperation", false) {}

virtual void operator()(osg::GraphicsContext* gc) {
gc->getState()->setUseVertexAttributeAliasing(true);
}
};
} // namespace osgTools
8 changes: 8 additions & 0 deletions src/include/osgTools/PointShaderProgram.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#pragma once

#include <osg/Node>
#include <osg/Program>

namespace osgTools {
osg::ref_ptr<osg::Program> createPointShaderProgram(osg::Node *targetNode);
};
60 changes: 60 additions & 0 deletions src/osgTools/PointShaderProgram.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
#include <osgTools/PointShaderProgram.h>

using namespace osgTools;

const char* vertexShaderSource = R"(
#version 330 core

in vec4 osg_Vertex;
in vec4 osg_Color;

uniform mat4 osg_ModelViewProjectionMatrix;

out vec4 vertexColor;

void main(void) {
gl_Position = osg_ModelViewProjectionMatrix * osg_Vertex;

vertexColor = osg_Color;

// Be sure and call `StateSet::setMode(GL_PROGRAM_POINT_SIZE, osg::StateAttribute::ON)`, or
// this will be a no-op! IMPORTANT!
gl_PointSize = 1.0;
}
)";

const char* fragmentShaderSource = R"(
#version 330 core
in vec4 vertexColor;

out vec4 color;

void main(void) {
// Convert to [-1, 1] so (0,0) is the center of the point.
vec2 p = gl_PointCoord * 2.0 - 1.0;

// This gives us a radial coordinate without a costly sqrt(), supposedly.
float r2 = dot(p, p);

// This explicitly recreates legacy FFP point behavior...
float alpha = exp(-r2 * 2.5);
color = vec4(vertexColor.rgb * alpha, vertexColor.a * alpha);
}
)";

osg::ref_ptr<osg::Program> osgTools::createPointShaderProgram(
osg::Node* target) {
osg::ref_ptr<osg::Program> prg = new osg::Program();

prg->addShader(new osg::Shader(osg::Shader::VERTEX, vertexShaderSource));
prg->addShader(
new osg::Shader(osg::Shader::FRAGMENT, fragmentShaderSource));

if (target) {
auto stateSet = target->getOrCreateStateSet();
stateSet->setAttributeAndModes(prg);
stateSet->setMode(GL_PROGRAM_POINT_SIZE, osg::StateAttribute::ON);
}

return prg;
}