1
0
Fork 0
mirror of https://github.com/AquariaOSE/Aquaria.git synced 2025-07-04 06:54:39 +00:00

Patch glfont to use VBO

Not really, since it's just a buffer that's rendered directly afterward
since font effects have to be considered that make pre-generating the
VBO impossible, but this is about as good as its gets for now.

This gets rid of glVertex3f() for good.
This commit is contained in:
fgenesis 2025-04-17 16:52:08 +02:00
parent b90dcfeec6
commit a44f11b6fd
5 changed files with 76 additions and 40 deletions

View file

@ -86,7 +86,7 @@ GL_FUNC(void,glOrtho,(GLdouble left, GLdouble right, GLdouble bottom, GLdouble t
GL_FUNC(void,glTexCoord2f,(GLfloat s, GLfloat t),(s,t),) GL_FUNC(void,glTexCoord2f,(GLfloat s, GLfloat t),(s,t),)
//GL_FUNC(void,glTexCoord2d,(GLdouble s, GLdouble t),(s,t),) //GL_FUNC(void,glTexCoord2d,(GLdouble s, GLdouble t),(s,t),)
GL_FUNC(void,glVertex2f,(GLfloat x, GLfloat y),(x,y),) GL_FUNC(void,glVertex2f,(GLfloat x, GLfloat y),(x,y),)
GL_FUNC(void,glVertex3f,(GLfloat x, GLfloat y, GLfloat z),(x,y,z),) //GL_FUNC(void,glVertex3f,(GLfloat x, GLfloat y, GLfloat z),(x,y,z),)
GL_FUNC(void,glReadBuffer,(GLenum mode),(mode),) GL_FUNC(void,glReadBuffer,(GLenum mode),(mode),)
GL_FUNC(void,glDrawBuffer,(GLenum mode),(mode),) GL_FUNC(void,glDrawBuffer,(GLenum mode),(mode),)

View file

@ -21,6 +21,8 @@ static unsigned toGlUsage(unsigned usage)
{ {
if(usage & GPUBUF_STATIC) if(usage & GPUBUF_STATIC)
return GL_STATIC_DRAW_ARB; return GL_STATIC_DRAW_ARB;
if(usage & GPUBUF_STREAM)
return GL_STREAM_DRAW_ARB;
return GL_DYNAMIC_DRAW; return GL_DYNAMIC_DRAW;
} }

View file

@ -13,7 +13,8 @@ enum BufUsage
GPUBUF_BINDING_MASK = 0x01, GPUBUF_BINDING_MASK = 0x01,
// usage // usage
GPUBUF_DYNAMIC = 0x00, GPUBUF_DYNAMIC = 0x00,
GPUBUF_STATIC = 0x10 GPUBUF_STATIC = 0x10,
GPUBUF_STREAM = 0x20,
}; };
enum BufDataType enum BufDataType

View file

@ -25,10 +25,12 @@ using namespace std;
#include "glfont2.h" #include "glfont2.h"
using namespace glfont; using namespace glfont;
//******************************************************************* //*******************************************************************
//GLFont Class Implementation //GLFont Class Implementation
//******************************************************************* //*******************************************************************
GLFont::GLFont () GLFont::GLFont ()
: vbo(GPUBUF_STREAM | GPUBUF_VERTEXBUF)
{ {
//Initialize header to safe state //Initialize header to safe state
header.tex = -1; header.tex = -1;

View file

@ -10,6 +10,11 @@
#include <assert.h> #include <assert.h>
// HACK: use engine functions because i can't be bothered to
// write pure opengl functions anymore until this all gets ripped
// out anyway
#include "../BBGE/VertexBuffer.h"
//******************************************************************* //*******************************************************************
//GLFont Interface //GLFont Interface
//******************************************************************* //*******************************************************************
@ -42,6 +47,8 @@ private:
GLFontChar *chars; GLFontChar *chars;
} header; } header;
mutable DynamicGPUBuffer vbo;
public: public:
//Constructor //Constructor
@ -85,54 +92,78 @@ public:
const std::basic_string<T> &text, float scalar, float x, const std::basic_string<T> &text, float scalar, float x,
float y, const float *top_color, const float *bottom_color, float alpha, float lastAlpha) const float y, const float *top_color, const float *bottom_color, float alpha, float lastAlpha) const
{ {
unsigned int i; const size_t sz = text.size();
GLFontChar *glfont_char; const size_t bytes = sz * 4 * (2+2+4) * sizeof(float);
float width, height;
//Begin rendering quads const float tw = header.tex_width * scalar;
glBegin(GL_QUADS); const float th = header.tex_height * scalar;
unsigned int sz = text.size(); do
{
float *p = (float*)vbo.beginWrite(GPUBUFTYPE_VEC2_TC_RGBA, bytes, GPUACCESS_DEFAULT);
float a = 0; for(size_t i = 0; i < text.size(); ++i)
//Loop through characters
for (i = 0; i < sz; i++)
{ {
//Make sure character is in range //Make sure character is in range
unsigned int c = (unsigned int)text[i]; unsigned int c = (unsigned int)text[i];
if (c < header.start_char || c > header.end_char) if (c < header.start_char || c > header.end_char)
continue; continue;
//Get pointer to glFont character const GLFontChar &fc = header.chars[c - header.start_char];
glfont_char = &header.chars[c - header.start_char];
//Get width and height const float width = fc.dx * tw;
width = (glfont_char->dx * header.tex_width) * scalar; const float height = fc.dy * th;
height = (glfont_char->dy * header.tex_height) * scalar;
float a = alpha;
if (i == (sz-1)) if (i == (sz-1))
a = alpha*lastAlpha; a *= lastAlpha;
else
a = alpha;
//Specify colors, vertices, and texture coordinates *p++ = x; // x
glColor4f(top_color[0], top_color[1], top_color[2], a); *p++ = y; // y
glTexCoord2f(glfont_char->tx1, glfont_char->ty1); *p++ = fc.tx1; // u
glVertex3f(x, y, 0.0F); *p++ = fc.ty1; // v
glTexCoord2f(glfont_char->tx2, glfont_char->ty1); *p++ = top_color[0];
glVertex3f(x + width, y, 0.0F); *p++ = top_color[1];
glColor4f(bottom_color[0], bottom_color[1], bottom_color[2], a); *p++ = top_color[2];
glTexCoord2f(glfont_char->tx2, glfont_char->ty2); *p++ = a;
glVertex3f(x + width, y + height, 0.0F);
glTexCoord2f(glfont_char->tx1, glfont_char->ty2); *p++ = x + width; // x
glVertex3f(x, y + height, 0.0F); *p++ = y; // y
*p++ = fc.tx2; // u
*p++ = fc.ty1; // v
*p++ = top_color[0];
*p++ = top_color[1];
*p++ = top_color[2];
*p++ = a;
*p++ = x + width; // x
*p++ = y + height; // y
*p++ = fc.tx2; // u
*p++ = fc.ty2; // v
*p++ = bottom_color[0];
*p++ = bottom_color[1];
*p++ = bottom_color[2];
*p++ = a;
*p++ = x; // x
*p++ = y + height; // y
*p++ = fc.tx1; // u
*p++ = fc.ty2; // v
*p++ = bottom_color[0];
*p++ = bottom_color[1];
*p++ = bottom_color[2];
*p++ = a;
//Move to next character
x += width; x += width;
} }
}
while(!vbo.commitWrite());
//Stop rendering quads vbo.apply();
glEnd();
size_t last = sz * 4;
for(size_t i = 0; i < last; i += 4)
glDrawArrays(GL_TRIANGLE_FAN, i, 4);
} }
}; };