Cleaned up support for GL3 build

This commit is contained in:
Robert Osfield 2017-10-13 17:03:31 +01:00
parent 24bec09b9e
commit 2303d6afc4
5 changed files with 51 additions and 32 deletions

View File

@ -30,15 +30,6 @@
#include <OpenThreads/Mutex>
// GL_ALPHA is deprecated in GL3/GL4 core profile, use GL_RED and a shader in this case. See osgText example.
#if defined(OSG_GL3_AVAILABLE) && !defined(OSG_GL2_AVAILABLE) && !defined(OSG_GL1_AVAILABLE)
#define OSGTEXT_GLYPH_FORMAT GL_RED
#define OSGTEXT_GLYPH_INTERNALFORMAT GL_R8
#else
#define OSGTEXT_GLYPH_FORMAT GL_ALPHA
#define OSGTEXT_GLYPH_INTERNALFORMAT GL_ALPHA
#endif
namespace osgText {
class Font;

View File

@ -348,14 +348,12 @@ osgText::Glyph* FreeTypeFont::getGlyph(const osgText::FontResolution& fontRes, u
for(unsigned char* p=data;p<data+dataSize;) { *p++ = 0; }
glyph->setImage(width,height,1,
OSGTEXT_GLYPH_INTERNALFORMAT,
OSGTEXT_GLYPH_FORMAT, GL_UNSIGNED_BYTE,
GL_ALPHA,
GL_ALPHA, GL_UNSIGNED_BYTE,
data,
osg::Image::USE_NEW_DELETE,
1);
glyph->setInternalTextureFormat(OSGTEXT_GLYPH_INTERNALFORMAT);
// copy image across to osgText::Glyph image.
switch(glyphslot->bitmap.pixel_mode)
{

View File

@ -207,14 +207,12 @@ void DefaultFont::constructGlyphs()
for(unsigned char* p=data;p<data+dataSize;) { *p++ = 0; }
glyph->setImage(sourceWidth,sourceHeight,1,
OSGTEXT_GLYPH_INTERNALFORMAT,
OSGTEXT_GLYPH_FORMAT, GL_UNSIGNED_BYTE,
GL_ALPHA,
GL_ALPHA, GL_UNSIGNED_BYTE,
data,
osg::Image::USE_NEW_DELETE,
1);
glyph->setInternalTextureFormat(OSGTEXT_GLYPH_INTERNALFORMAT);
// now populate data array by converting bitmap into a luminance_alpha map.
unsigned char* ptr = rasters[i-32];
unsigned char value_on = 255;

View File

@ -28,6 +28,22 @@
using namespace osgText;
using namespace std;
// GL_ALPHA and GL_LUMINANCE_ALPHA are deprecated in GL3/GL4 core profile, use GL_RED & GL_RB in this case.
#if defined(OSG_GL3_AVAILABLE) && !defined(OSG_GL2_AVAILABLE) && !defined(OSG_GL1_AVAILABLE)
#define OSGTEXT_GLYPH_ALPHA_FORMAT GL_RED
#define OSGTEXT_GLYPH_ALPHA_INTERNALFORMAT GL_R8
#define OSGTEXT_GLYPH_SDF_FORMAT GL_RG
#define OSGTEXT_GLYPH_SDF_INTERNALFORMAT GL_RG8
#else
#define OSGTEXT_GLYPH_ALPHA_FORMAT GL_ALPHA
#define OSGTEXT_GLYPH_ALPHA_INTERNALFORMAT GL_ALPHA
#define OSGTEXT_GLYPH_SDF_FORMAT GL_LUMINANCE_ALPHA
#define OSGTEXT_GLYPH_SDF_INTERNALFORMAT GL_LUMINANCE_ALPHA
#endif
#if 0
#define TEXTURE_IMAGE_NUM_CHANNELS 1
#define TEXTURE_IMAGE_FORMAT OSGTEXT_GLYPH_FORMAT
@ -160,6 +176,9 @@ void GlyphTexture::copyGlyphImage(Glyph* glyph)
if (_glyphTextureFeatures==GREYSCALE)
{
// OSG_NOTICE<<"GlyphTexture::copyGlyphImage() greyscale copying. glyphTexture="<<this<<", glyph="<<glyph->getGlyphCode()<<std::endl;
// make sure the glyph image settings and the target image are consisent before copying.
glyph->setPixelFormat(_image->getPixelFormat());
glyph->setInternalTextureFormat(_image->getPixelFormat());
_image->copySubImage(glyph->getTexturePositionX(), glyph->getTexturePositionY(), 0, glyph);
return;
}
@ -185,8 +204,6 @@ void GlyphTexture::copyGlyphImage(Glyph* glyph)
float max_distance = sqrtf(float(search_distance)*float(search_distance)*2.0);
int num_channels = TEXTURE_IMAGE_NUM_CHANNELS;
if ((left+glyph->getTexturePositionX())<0) left = -glyph->getTexturePositionX();
if ((right+glyph->getTexturePositionX())>=dest_columns) right = dest_columns-glyph->getTexturePositionX()-1;
@ -194,6 +211,12 @@ void GlyphTexture::copyGlyphImage(Glyph* glyph)
if ((upper+glyph->getTexturePositionY())>=dest_rows) upper = dest_rows-glyph->getTexturePositionY()-1;
int num_components = osg::Image::computeNumComponents(_image->getPixelFormat());
int bytes_per_pixel = osg::Image::computePixelSizeInBits(_image->getPixelFormat(),_image->getDataType())/8;
int alpha_offset = (_image->getPixelFormat()==GL_LUMINANCE_ALPHA) ? 1 : 0;
int sdf_offset = (_image->getPixelFormat()==GL_LUMINANCE_ALPHA) ? 0 : 1;
unsigned char full_on = 255;
unsigned char mid_point = full_on/2;
float mid_point_f = float(mid_point)*multiplier;
@ -343,14 +366,14 @@ void GlyphTexture::copyGlyphImage(Glyph* glyph)
}
unsigned char* dest_ptr = dest_data + (dr*dest_columns + dc)*num_channels;
if (num_channels==2)
unsigned char* dest_ptr = dest_data + (dr*dest_columns + dc)*bytes_per_pixel;
if (num_components==2)
{
// signed distance field value
*(dest_ptr++) = value;
*(dest_ptr+sdf_offset) = value;
// original alpha value from glyph image
*(dest_ptr) = center_value;
*(dest_ptr+alpha_offset) = center_value;
}
else
{
@ -391,13 +414,12 @@ osg::Image* GlyphTexture::createImage()
_image = new osg::Image;
#if defined(OSG_GL3_AVAILABLE) && !defined(OSG_GL2_AVAILABLE) && !defined(OSG_GL1_AVAILABLE)
GLenum imageFormat = (_glyphTextureFeatures==GREYSCALE) ? GL_RED : GL_RG;
#else
GLenum imageFormat = (_glyphTextureFeatures==GREYSCALE) ? GL_ALPHA : GL_LUMINANCE_ALPHA;
#endif
GLenum imageFormat = (_glyphTextureFeatures==GREYSCALE) ? OSGTEXT_GLYPH_ALPHA_FORMAT : OSGTEXT_GLYPH_SDF_FORMAT;
GLenum internalFormat = (_glyphTextureFeatures==GREYSCALE) ? OSGTEXT_GLYPH_ALPHA_INTERNALFORMAT : OSGTEXT_GLYPH_SDF_INTERNALFORMAT;
_image->allocateImage(getTextureWidth(), getTextureHeight(), 1, imageFormat, GL_UNSIGNED_BYTE);
_image->setInternalTextureFormat(internalFormat);
memset(_image->data(), 0, _image->getTotalSizeInBytes());
for(GlyphRefList::iterator itr = _glyphs.begin();

View File

@ -32,6 +32,16 @@ char text_sdf_frag[] = "$OSG_GLSL_VERSION\n"
" #define osg_FragColor gl_FragColor\n"
"#endif\n"
"\n"
"\n"
"#if !defined(GL_ES) && __VERSION__>=130\n"
" #define ALPHA r\n"
" #define SDF g\n"
"#else\n"
" #define ALPHA a\n"
" #define SDF r\n"
"#endif\n"
"\n"
"\n"
"uniform sampler2D glyphTexture;\n"
"\n"
"$OSG_VARYING_IN vec2 texCoord;\n"
@ -49,7 +59,7 @@ char text_sdf_frag[] = "$OSG_GLSL_VERSION\n"
"\n"
"float distanceFromEdge(vec2 tc)\n"
"{\n"
" float center_alpha = TEXTURELOD(glyphTexture, tc, 0.0).r;\n"
" float center_alpha = TEXTURELOD(glyphTexture, tc, 0.0).SDF;\n"
" if (center_alpha==0.0) return -1.0;\n"
"\n"
" //float distance_scale = (1.0/4.0)*1.41;\n"
@ -169,7 +179,7 @@ char text_sdf_frag[] = "$OSG_GLSL_VERSION\n"
"\n"
"#ifdef OUTLINE\n"
"\n"
" float alpha = TEXTURE(glyphTexture, src_texCoord).a;\n"
" float alpha = TEXTURE(glyphTexture, src_texCoord).ALPHA;\n"
" float delta_tc = 1.6*OUTLINE*GLYPH_DIMENSION/TEXTURE_DIMENSION;\n"
"\n"
" float outline_alpha = alpha;\n"
@ -184,7 +194,7 @@ char text_sdf_frag[] = "$OSG_GLSL_VERSION\n"
" {\n"
" for(float j=0.0; j<numSamples; ++j)\n"
" {\n"
" float local_alpha = TEXTURE(glyphTexture, origin + vec2(i*delta_tc, j*delta_tc)).a;\n"
" float local_alpha = TEXTURE(glyphTexture, origin + vec2(i*delta_tc, j*delta_tc)).ALPHA;\n"
" outline_alpha = max(outline_alpha, local_alpha);\n"
" background_alpha = background_alpha * (1.0-local_alpha);\n"
" }\n"
@ -210,7 +220,7 @@ char text_sdf_frag[] = "$OSG_GLSL_VERSION\n"
"\n"
"#else\n"
"\n"
" float alpha = TEXTURE(glyphTexture, src_texCoord).a;\n"
" float alpha = TEXTURE(glyphTexture, src_texCoord).ALPHA;\n"
" if (alpha==0.0) vec4(0.0, 0.0, 0.0, 0.0);\n"
" return vec4(vertexColor.rgb, vertexColor.a * alpha);\n"
"\n"