textureOffset & other smaller things

This commit is contained in:
jrkb 2023-04-13 17:16:08 +02:00
parent 604240360e
commit d40db6389b

View file

@ -169,7 +169,11 @@ class Font {
// If hinting is enabled, worldSize must be an integer and defines the font size in pixels used for hinting. // If hinting is enabled, worldSize must be an integer and defines the font size in pixels used for hinting.
// Otherwise, worldSize can be an arbitrary floating-point value. // Otherwise, worldSize can be an arbitrary floating-point value.
Font(FT_Face face, float worldSize = 1.0f, bool hinting = false) : face(face), worldSize(worldSize), hinting(hinting){ Font(FT_Face face, int gpuTextureOffset, float worldSize = 1.0f, bool hinting = false) :
face(face),
gpuTextureOffset(gpuTextureOffset),
worldSize(worldSize),
hinting(hinting){
// TODO: modularize init, so we can initialize with settings and text // TODO: modularize init, so we can initialize with settings and text
if(hinting){ if(hinting){
@ -340,6 +344,9 @@ class Font {
private: private:
bool initializedGlyphsBufferTexture = false; bool initializedGlyphsBufferTexture = false;
bool initializedCurvesBufferTexture = false; bool initializedCurvesBufferTexture = false;
int calcTextureOffset(){
return gpuTextureOffset * 2;
}
void uploadBuffers(){ void uploadBuffers(){
//cout << "bufferGlyphs.size(): " << bufferGlyphs.size() << endl; //cout << "bufferGlyphs.size(): " << bufferGlyphs.size() << endl;
//cout << "bufferCurves.size(): " << bufferCurves.size() << endl; //cout << "bufferCurves.size(): " << bufferCurves.size() << endl;
@ -356,7 +363,7 @@ class Font {
if(!isPowerOfTwo(bufferGlyphsSize)){ if(!isPowerOfTwo(bufferGlyphsSize)){
bufferGlyphsSize = calculateUpperPowerOfTwo(bufferGlyphsSize); bufferGlyphsSize = calculateUpperPowerOfTwo(bufferGlyphsSize);
} }
glActiveTexture(GL_TEXTURE3); glActiveTexture(GL_TEXTURE3 + calcTextureOffset());
glBindTexture(GL_TEXTURE_2D, glyphBuffer); glBindTexture(GL_TEXTURE_2D, glyphBuffer);
if(!initializedGlyphsBufferTexture){ if(!initializedGlyphsBufferTexture){
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
@ -383,7 +390,7 @@ class Font {
if(!isPowerOfTwo(bufferCurvesSize)){ if(!isPowerOfTwo(bufferCurvesSize)){
bufferCurvesSize = calculateUpperPowerOfTwo(bufferCurvesSize); bufferCurvesSize = calculateUpperPowerOfTwo(bufferCurvesSize);
} }
glActiveTexture(GL_TEXTURE4); glActiveTexture(GL_TEXTURE4 + calcTextureOffset());
glBindTexture(GL_TEXTURE_2D, curveBuffer); glBindTexture(GL_TEXTURE_2D, curveBuffer);
if(!initializedCurvesBufferTexture){ if(!initializedCurvesBufferTexture){
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
@ -686,14 +693,14 @@ class Font {
// Important! The GLint for the location needs to stick around // Important! The GLint for the location needs to stick around
// we cannot reuse a location as in Desktop for some reason // we cannot reuse a location as in Desktop for some reason
glyphBufferLocation = glGetUniformLocation(program, "glyphs"); glyphBufferLocation = glGetUniformLocation(program, "glyphs");
glUniform1i(glyphBufferLocation, 3); glUniform1i(glyphBufferLocation, 3 + calcTextureOffset());
curveBufferLocation = glGetUniformLocation(program, "curves"); curveBufferLocation = glGetUniformLocation(program, "curves");
glUniform1i(curveBufferLocation, 4); glUniform1i(curveBufferLocation, 4 + calcTextureOffset());
glActiveTexture(GL_TEXTURE3); glActiveTexture(GL_TEXTURE3 + calcTextureOffset());
glBindTexture(GL_TEXTURE_2D, glyphBuffer); glBindTexture(GL_TEXTURE_2D, glyphBuffer);
glActiveTexture(GL_TEXTURE4); glActiveTexture(GL_TEXTURE4 + calcTextureOffset());
glBindTexture(GL_TEXTURE_2D, curveBuffer); glBindTexture(GL_TEXTURE_2D, curveBuffer);
glActiveTexture(GL_TEXTURE0); glActiveTexture(GL_TEXTURE0);
@ -992,9 +999,14 @@ class Font {
// The glyph quads are expanded by this amount to enable proper // The glyph quads are expanded by this amount to enable proper
// anti-aliasing. Value is relative to emSize. // anti-aliasing. Value is relative to emSize.
float dilation = 0; float dilation = 0;
int gpuTextureOffset = 0;
}; };
static std::shared_ptr <Font> loadFont(FT_Library & library, const std::string & filename, float worldSize = 1.0f, bool hinting = false){ static std::shared_ptr <Font> loadFont(FT_Library & library,
const std::string & filename,
int gpuTextureOffset = 0,
float worldSize = 1.0f,
bool hinting = false){
std::string error; std::string error;
FT_Face face = Font::loadFace(library, filename, error); FT_Face face = Font::loadFace(library, filename, error);
if(error != ""){ if(error != ""){
@ -1002,30 +1014,28 @@ static std::shared_ptr <Font> loadFont(FT_Library & library, const std::string &
return std::shared_ptr <Font>{}; return std::shared_ptr <Font>{};
} }
return std::make_shared <Font>(face, worldSize, hinting); return std::make_shared <Font>(face, gpuTextureOffset, worldSize, hinting);
} }
static void tryUpdateMainFont(FT_Library & library, static void tryUpdateMainFont(FT_Library & library,
const std::string & filename, const std::string & filename,
const string & mainText, const string & mainText,
shared_ptr <Font> & mainFont, shared_ptr <Font> & mainFont,
Font::BoundingBox & bb){ int gpuTextureOffset){
{ {
cout << "tryUpdateMainFont" << endl; cout << "tryUpdateMainFont" << endl;
auto font = loadFont(library, filename, 1.0f); auto font = loadFont(library, filename, gpuTextureOffset);
if(!font){ if(!font){
return; return;
} }
font->dilation = 0.1f; font->dilation = 0.1f;
if(mainText != ""){
font->prepareGlyphsForText(mainText); font->prepareGlyphsForText(mainText);
}
mainFont = std::move(font); mainFont = std::move(font);
bb = mainFont->measure(0, 0, mainText);
cout << "tryUpdateMainFont loaded font" << endl;
cout << "mainText: " << mainText << endl;
cout << bb.minX << "-" << bb.maxX << endl;
} }
} }
} }