From Andreas Henne, Support for GL3 core profile in osgText

git-svn-id: http://svn.openscenegraph.org/osg/OpenSceneGraph/trunk@14717 16af8721-9629-0410-8352-f15c8da7e697
This commit is contained in:
Robert Osfield
2015-02-25 18:56:29 +00:00
parent 7d50c8e634
commit 47db2da8b4
10 changed files with 282 additions and 118 deletions

View File

@@ -19,17 +19,10 @@ void textInfo(osgText::Text* text)
for(unsigned int i = 0; i < s.size(); i++)
{
osg::Vec2 ul = gq.getCoords()[0 + (i * 4)]; // upperLeft
osg::Vec2 ll = gq.getCoords()[1 + (i * 4)]; // lowerLeft
osg::Vec2 lr = gq.getCoords()[2 + (i * 4)]; // lowerRight
osg::Vec2 ur = gq.getCoords()[3 + (i * 4)]; // upperRight
/*
osg::Vec3 ul = gq.getTransformedCoords(0)[0 + (i * 4)];
osg::Vec3 ll = gq.getTransformedCoords(0)[1 + (i * 4)];
osg::Vec3 lr = gq.getTransformedCoords(0)[2 + (i * 4)];
osg::Vec3 ur = gq.getTransformedCoords(0)[3 + (i * 4)];
*/
osg::Vec2 ul = (*gq.getCoords())[0 + (i * 4)]; // upperLeft
osg::Vec2 ll = (*gq.getCoords())[1 + (i * 4)]; // lowerLeft
osg::Vec2 lr = (*gq.getCoords())[2 + (i * 4)]; // lowerRight
osg::Vec2 ur = (*gq.getCoords())[3 + (i * 4)]; // upperRight
osg::notify(osg::NOTICE)
<< "'" << static_cast<char>(s[i]) << "':"

View File

@@ -36,6 +36,39 @@
#include <osgText/Font>
#include <osgText/Text>
// These shaders are only required for GL3/GL4 core profile.
// The fragment shader uses the red component of the font texture and not the alpha channel (GL_ALPHA is deprecated in the core profile),.
// osgText will write to GL_RED instead of GL_ALPHA if it is compiled with OSG_GL3_AVAILABLE but not with OSG_GL2_AVAILABLE or OSG_GL1_AVAILABLE.
#if defined(OSG_GL3_AVAILABLE) && !defined(OSG_GL2_AVAILABLE) && !defined(OSG_GL1_AVAILABLE)
static const char *gl3TextVertexShader = {
"#version 330 core\n"
"in vec4 osg_Vertex;\n"
"in vec4 osg_Color;\n"
"in vec4 osg_MultiTexCoord0;\n"
"uniform mat4 osg_ModelViewProjectionMatrix;\n"
"out vec2 texCoord;\n"
"out vec4 vertexColor;\n"
"void main(void)\n"
"{\n"
" gl_Position = osg_ModelViewProjectionMatrix * osg_Vertex;\n"
" texCoord = osg_MultiTexCoord0.xy;\n"
" vertexColor = osg_Color; \n"
"}\n"
};
static const char *gl3TextFragmentShader = {
"#version 330 core\n"
"uniform sampler2D glyphTexture;\n"
"in vec2 texCoord;\n"
"in vec4 vertexColor;\n"
"out vec4 color;\n"
"void main(void)\n"
"{\n"
" color = vertexColor * texture(glyphTexture, texCoord).rrrr;\n"
"}\n"
};
#endif
osg::Group* createHUDText()
{
@@ -44,9 +77,20 @@ osg::Group* createHUDText()
osgText::Font* font = osgText::readFontFile("fonts/arial.ttf");
osg::setNotifyLevel(osg::INFO);
osg::Geode* geode = new osg::Geode;
rootNode->addChild(geode);
bool useVBOs = false;
#if defined(OSG_GL3_AVAILABLE) && !defined(OSG_GL2_AVAILABLE) && !defined(OSG_GL1_AVAILABLE)
useVBOs = true;
osg::Program* program = new osg::Program;
program->addShader(new osg::Shader(osg::Shader::VERTEX, gl3TextVertexShader));
program->addShader(new osg::Shader(osg::Shader::FRAGMENT, gl3TextFragmentShader));
geode->getOrCreateStateSet()->setAttributeAndModes(program, osg::StateAttribute::ON);
#endif
float windowHeight = 1024.0f;
float windowWidth = 1280.0f;
float margin = 50.0f;
@@ -62,6 +106,7 @@ osg::Group* createHUDText()
{
osgText::Text* text = new osgText::Text;
text->setUseVertexBufferObjects(useVBOs);
text->setFont(font);
text->setColor(layoutColor);
text->setCharacterSize(layoutCharacterSize);
@@ -77,6 +122,7 @@ osg::Group* createHUDText()
{
osgText::Text* text = new osgText::Text;
text->setUseVertexBufferObjects(useVBOs);
text->setFont(font);
text->setColor(layoutColor);
text->setCharacterSize(layoutCharacterSize);
@@ -92,6 +138,7 @@ osg::Group* createHUDText()
{
osgText::Text* text = new osgText::Text;
text->setUseVertexBufferObjects(useVBOs);
text->setFont(font);
text->setColor(layoutColor);
text->setPosition(osg::Vec3(margin,windowHeight-margin,0.0f));
@@ -117,6 +164,7 @@ osg::Group* createHUDText()
{
osgText::Text* text = new osgText::Text;
text->setUseVertexBufferObjects(useVBOs);
text->setFont(font);
text->setColor(fontSizeColor);
text->setCharacterSize(fontSizeCharacterSize);
@@ -132,6 +180,7 @@ osg::Group* createHUDText()
cursor.y() -= fontSizeCharacterSize;
{
osgText::Text* text = new osgText::Text;
text->setUseVertexBufferObjects(useVBOs);
text->setFont(font);
text->setColor(fontSizeColor);
text->setCharacterSize(fontSizeCharacterSize);
@@ -147,6 +196,7 @@ osg::Group* createHUDText()
cursor.y() -= fontSizeCharacterSize;
{
osgText::Text* text = new osgText::Text;
text->setUseVertexBufferObjects(useVBOs);
text->setFont(font);
text->setColor(fontSizeColor);
text->setCharacterSize(fontSizeCharacterSize);
@@ -171,6 +221,7 @@ osg::Group* createHUDText()
{
osgText::Text* text = new osgText::Text;
text->setUseVertexBufferObjects(useVBOs);
text->setFont(font);
text->setColor(characterSizeColor);
text->setFontResolution(20,20);
@@ -186,6 +237,7 @@ osg::Group* createHUDText()
cursor.y() -= 30.0f;
{
osgText::Text* text = new osgText::Text;
text->setUseVertexBufferObjects(useVBOs);
text->setFont(font);
text->setColor(characterSizeColor);
text->setFontResolution(30,30);
@@ -201,6 +253,7 @@ osg::Group* createHUDText()
cursor.y() -= 50.0f;
{
osgText::Text* text = new osgText::Text;
text->setUseVertexBufferObjects(useVBOs);
text->setFont(font);
text->setColor(characterSizeColor);
text->setFontResolution(40,40);
@@ -292,6 +345,7 @@ osg::Group* createHUDText()
{
osgText::Text* text = new osgText::Text;
text->setUseVertexBufferObjects(useVBOs);
text->setColor(fontColor);
text->setPosition(cursor);
text->setCharacterSize(fontCharacterSize);
@@ -307,6 +361,7 @@ osg::Group* createHUDText()
osgText::Font* arial = osgText::readFontFile("fonts/arial.ttf");
osgText::Text* text = new osgText::Text;
text->setUseVertexBufferObjects(useVBOs);
text->setColor(fontColor);
text->setPosition(cursor);
text->setCharacterSize(fontCharacterSize);
@@ -324,6 +379,7 @@ osg::Group* createHUDText()
osgText::Font* times = osgText::readFontFile("fonts/times.ttf");
osgText::Text* text = new osgText::Text;
text->setUseVertexBufferObjects(useVBOs);
text->setColor(fontColor);
text->setPosition(cursor);
text->setCharacterSize(fontCharacterSize);
@@ -344,6 +400,7 @@ osg::Group* createHUDText()
osgText::Font* dirtydoz = osgText::readFontFile("fonts/dirtydoz.ttf");
osgText::Text* text = new osgText::Text;
text->setUseVertexBufferObjects(useVBOs);
text->setColor(fontColor);
text->setPosition(cursor);
text->setCharacterSize(fontCharacterSize);
@@ -361,6 +418,7 @@ osg::Group* createHUDText()
osgText::Font* fudd = osgText::readFontFile("fonts/fudd.ttf");
osgText::Text* text = new osgText::Text;
text->setUseVertexBufferObjects(useVBOs);
text->setColor(fontColor);
text->setPosition(cursor);
text->setCharacterSize(fontCharacterSize);
@@ -386,6 +444,15 @@ osg::Group* create3DText(const osg::Vec3& center,float radius)
osg::Geode* geode = new osg::Geode;
bool useVBOs = false;
#if defined(OSG_GL3_AVAILABLE) && !defined(OSG_GL2_AVAILABLE) && !defined(OSG_GL1_AVAILABLE)
useVBOs = true;
osg::Program* program = new osg::Program;
program->addShader(new osg::Shader(osg::Shader::VERTEX, gl3TextVertexShader));
program->addShader(new osg::Shader(osg::Shader::FRAGMENT, gl3TextFragmentShader));
geode->getOrCreateStateSet()->setAttributeAndModes(program, osg::StateAttribute::ON);
#endif
////////////////////////////////////////////////////////////////////////////////////////////////////////
//
@@ -398,6 +465,7 @@ osg::Group* create3DText(const osg::Vec3& center,float radius)
osg::Vec3 pos(center.x()-radius*.5f,center.y()-radius*.5f,center.z()-radius*.5f);
osgText::Text* text1 = new osgText::Text;
text1->setUseVertexBufferObjects(useVBOs);
text1->setFont("fonts/times.ttf");
text1->setCharacterSize(characterSize);
text1->setPosition(pos);
@@ -406,6 +474,7 @@ osg::Group* create3DText(const osg::Vec3& center,float radius)
geode->addDrawable(text1);
osgText::Text* text2 = new osgText::Text;
text2->setUseVertexBufferObjects(useVBOs);
text2->setFont("fonts/times.ttf");
text2->setCharacterSize(characterSize);
text2->setPosition(pos);
@@ -414,6 +483,7 @@ osg::Group* create3DText(const osg::Vec3& center,float radius)
geode->addDrawable(text2);
osgText::Text* text3 = new osgText::Text;
text3->setUseVertexBufferObjects(useVBOs);
text3->setFont("fonts/times.ttf");
text3->setCharacterSize(characterSize);
text3->setPosition(pos);
@@ -424,6 +494,7 @@ osg::Group* create3DText(const osg::Vec3& center,float radius)
osg::Vec4 characterSizeModeColor(1.0f,0.0f,0.5f,1.0f);
osgText::Text* text4 = new osgText::Text;
text4->setUseVertexBufferObjects(useVBOs);
text4->setFont("fonts/times.ttf");
text4->setCharacterSize(characterSize);
text4->setPosition(center);
@@ -437,6 +508,7 @@ osg::Group* create3DText(const osg::Vec3& center,float radius)
geode->addDrawable(text4);
osgText::Text* text5 = new osgText::Text;
text5->setUseVertexBufferObjects(useVBOs);
text5->setColor(characterSizeModeColor);
text5->setFont("fonts/times.ttf");
//text5->setCharacterSize(characterSize);
@@ -449,6 +521,7 @@ osg::Group* create3DText(const osg::Vec3& center,float radius)
geode->addDrawable(text5);
osgText::Text* text6 = new osgText::Text;
text6->setUseVertexBufferObjects(useVBOs);
text6->setColor(characterSizeModeColor);
text6->setFont("fonts/times.ttf");
text6->setCharacterSize(characterSize);
@@ -459,6 +532,7 @@ osg::Group* create3DText(const osg::Vec3& center,float radius)
geode->addDrawable(text6);
osgText::Text* text7 = new osgText::Text;
text7->setUseVertexBufferObjects(useVBOs);
text7->setColor(characterSizeModeColor);
text7->setFont("fonts/times.ttf");
text7->setCharacterSize(characterSize);