Browse Source

new rasterization refactor, resize reworks, pointToViewport moved out

pull/2/head
Odilitime 5 years ago
parent
commit
253d484204
  1. 101
      src/graphics/components/TextComponent.cpp

101
src/graphics/components/TextComponent.cpp

@ -7,6 +7,10 @@ @@ -7,6 +7,10 @@
extern TextRasterizerCache *rasterizerCache;
TextComponent::TextComponent(const std::string &rawText, const int rawX, const int rawY, const unsigned int size, const bool bolded, const unsigned int hexColor, const int passedWindowWidth, const int passedWindowHeight) {
windowWidth = passedWindowWidth;
windowHeight = passedWindowHeight;
//const std::clock_t begin = clock();
text = rawText;
//x = rawX;
@ -92,44 +96,61 @@ inline void setVerticesColor(std::unique_ptr<float[]> &vertices, int p, unsigned @@ -92,44 +96,61 @@ inline void setVerticesColor(std::unique_ptr<float[]> &vertices, int p, unsigned
vertices[static_cast<size_t>(posMac(p) + 6)] = (static_cast<float>((color >> 0) & 0xFF)) / 255;
}
void TextComponent::rasterize(const int rawX, const int rawY, const int passedWindowWidth, const int passedWindowHeight) {
void TextComponent::rasterize(const int rawX, const int rawY) {
//const std::clock_t begin = clock();
const std::shared_ptr<TextRasterizer> textRasterizer=rasterizerCache->loadFont(fontSize, bold);
unsigned int glyphCount;
//unsigned int glyphCount;
// we need to know how much width we have between x and windowWidth
// and preferrable where the inline starts
// my inline start is parent->children adding width from start to me
// actually the run of all inline before me, starting at parent x as a minimum
//std::cout << "TextComponent::rasterize pre-height: " << (int)height << std::endl;
int wrapToX = 0; // windowWidth - rawX;
bool wrapped;
glyphs = textRasterizer->rasterize(text, rawX, windowWidth, wrapToX, width, height, glyphCount, endingX, endingY, wrapped);
//std::cout << "TextComponent::rasterize done [" << text << "] - start x: " << rawX << " width: " << (int)width << " wrapWidth: " << windowWidth << " wrapTo: " << wrapToX << std::endl;
//std::cout << "TextComponent::rasterize post-height: " << (int)height << std::endl;
if (glyphs == nullptr) {
//bool wrapped;
//glyphs = textRasterizer->rasterize(text, rawX, windowWidth, wrapToX, width, height, glyphCount, endingX, endingY, wrapped);
rasterizationRequest request;
request.text = text;
request.startX = rawX;
request.availableWidth = availableWidth;
request.sourceStartX = rasterStartX;
request.sourceStartY = rasterStartY;
request.noWrap = noWrap;
std::shared_ptr<rasterizationResponse> response = textRasterizer->rasterize(request);
if (response.get() == nullptr) {
std::cout << "TextComponent::rasterize - got nullptr from rasterizer" << std::endl;
return;
}
width = response->width;
height = response->height;
endingX = response->endingX;
endingY = response->endingY;
//std::cout << "TextComponent::rasterize done [" << text << "] - start x: " << rawX << " width: " << (int)width << " wrapWidth: " << windowWidth << " wrapTo: " << wrapToX << std::endl;
//std::cout << "TextComponent::rasterize post-height: " << (int)height << std::endl;
//if (glyphs == nullptr) {
// return;
//}
// did we wrap
int startX = rawX;
// if we didn't we have a nice little texture starting at rawX potentially up to windowWidth
// if we did wrap, then we start at wrapToX (0) and we're a big square texture
if (wrapped) {
if (response->wrapped) {
//std::cout << "it's wrapped starting at " << wrapToX << std::endl;
startX = wrapToX;
}
//std::cout << "startX: " << startX << std::endl;
glyphVertices.clear();
for (unsigned int i = 0; i < glyphCount; i++) {
//for (unsigned int i = 0; i < glyphCount; i++) {
//for(std::vector<Glyph>::iterator it=glyphs->begin(); it!=glyphs->end(); ++it) {
const Glyph &glyph = glyphs[i];
//const Glyph &glyph = glyphs[i];
//Glyph &glyph=*it;
float vx0 = startX;
float vy0 = glyph.y0 + rawY;
float vy0 = response->y0 + rawY;
//std::cout << "glyph x from: " << (int)glyph.x0 << " to " << (int)glyph.x1 << std::endl;
float vx1 = startX + (glyph.x1 - glyph.x0);
float vy1 = glyph.y1 + rawY;
float vx1 = startX + (response->x1 - response->x0);
float vy1 = response->y1 + rawY;
//std::cout << "textcomponent at " << (int)vx0 << "," << (int)vy0 << " to " << (int)vx1 << "," << (int)vy1 << std::endl;
// convert our local x,y,w,h into actual ogl coords
@ -138,37 +159,43 @@ void TextComponent::rasterize(const int rawX, const int rawY, const int passedWi @@ -138,37 +159,43 @@ void TextComponent::rasterize(const int rawX, const int rawY, const int passedWi
//vx0 = ((vx0 / windowWidth) * 2) - 1;
//vy0 = ((vy0 / windowHeight) * 2) - 1;
pointToViewport(vx0, vy0, windowWidth, windowHeight);
pointToViewport(vx1, vy1, windowWidth, windowHeight);
pointToViewport(vx0, vy0);
pointToViewport(vx1, vy1);
//std::cout << "textcomponent at GL" << (int)vx0 << "," << (int)vy0 << " to GL" << (int)vx1 << "," << (int)vy1 << std::endl;
std::unique_ptr<float[]> vertices = std::make_unique<float[]>(36);
vertices[posMac(0) + 0] = vx0;
vertices[posMac(0) + 1] = vy0;
setVerticesColor(vertices, 0, color);
vertices[posMac(0) + 7] = glyph.s0;
vertices[posMac(0) + 8] = glyph.t0;
vertices[posMac(0) + 7] = response->s0;
vertices[posMac(0) + 8] = response->t0;
vertices[posMac(1) + 0] = vx0;
vertices[posMac(1) + 1] = vy1;
setVerticesColor(vertices, 1, color);
vertices[posMac(1) + 7] = glyph.s0;
vertices[posMac(1) + 8] = glyph.t1;
vertices[posMac(1) + 7] = response->s0;
vertices[posMac(1) + 8] = response->t1;
vertices[posMac(2) + 0] = vx1;
vertices[posMac(2) + 1] = vy1;
setVerticesColor(vertices, 2, color);
vertices[posMac(2) + 7] = glyph.s1;
vertices[posMac(2) + 8] = glyph.t1;
vertices[posMac(2) + 7] = response->s1;
vertices[posMac(2) + 8] = response->t1;
vertices[posMac(3) + 0] = vx1;
vertices[posMac(3) + 1] = vy0;
setVerticesColor(vertices, 3, color);
vertices[posMac(3) + 7] = glyph.s1;
vertices[posMac(3) + 8] = glyph.t0;
vertices[posMac(3) + 7] = response->s1;
vertices[posMac(3) + 8] = response->t0;
glyphVertices.push_back(std::move(vertices));
}
//std::cout << "TextComponent::rasterize - glyphVertices count: " << glyphVertices.size() << std::endl;
textureWidth = response->textureWidth;
textureHeight = response->textureHeight;
textureData = std::move(response->textureData);
//}
//const std::clock_t end = clock();
//std::cout << "rasterize text [" << text << "] in: " << std::fixed << ((static_cast<double>(end - begin)) / CLOCKS_PER_SEC) << std::scientific << " seconds" << std::endl;
}
@ -199,10 +226,19 @@ void TextComponent::render() { @@ -199,10 +226,19 @@ void TextComponent::render() {
}
}
void TextComponent::resize() {
// compatible adapter
void TextComponent::resize(const int passedWindowWidth, const int passedWindowHeight) {
resize(passedWindowWidth, passedWindowHeight, passedWindowWidth);
}
// more detailed control
void TextComponent::resize(const int passedWindowWidth, const int passedWindowHeight, const int passedAvailableWidth) {
windowWidth = passedWindowWidth;
windowHeight = passedWindowHeight;
availableWidth = passedAvailableWidth;
//std::cout << "TextComponent::resize" << std::endl;
//std::cout << "TextComponent::resize - rasterizing at " << (int)x << "x" << (int)y << std::endl;
rasterize(x, y, windowWidth, windowHeight);
rasterize(x, y);
//std::cout << "TextComponent::resize - rasterizing at " << (int)x << "x" << (int)y << " size: " << (int)width << "x" << (int)height << std::endl;
// make sure we have glyphs
if (!glyphVertices.size()) {
@ -211,27 +247,24 @@ void TextComponent::resize() { @@ -211,27 +247,24 @@ void TextComponent::resize() {
}
// reupload NEW texture to video card
/*
const Glyph &glyph = glyphs[0];
if (!glyphs) {
std::cout << "TextComponent::resize - glyph points no where" << std::endl;
return;
}
*/
if (!textures.size()) {
textures.push_back(0);
glGenTextures(1, &textures.back());
}
glBindTexture(GL_TEXTURE_2D, textures.back()); // select texture
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, glyph.textureWidth, glyph.textureHeight, 0, GL_RED, GL_UNSIGNED_BYTE, glyph.textureData.get()); // update texture
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, textureWidth, textureHeight, 0, GL_RED, GL_UNSIGNED_BYTE, textureData.get()); // update texture
glGenerateMipmap(GL_TEXTURE_2D);
verticesDirty = true;
}
void TextComponent::pointToViewport(float &rawX, float &rawY, const int passedWindowWidth, const int passedWindowHeight) const {
rawX = ((rawX / windowWidth) * 2) - 1;
rawY = ((rawY / windowHeight) * 2) - 1;
}
void TextComponent::sanitize(std::string &str) {
size_t found = 0; // position
while ((found = str.find("&", found)) != std::string::npos) {

Loading…
Cancel
Save