1
0
Fork 0
mirror of https://github.com/AquariaOSE/Aquaria.git synced 2025-05-10 19:13:44 +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,glTexCoord2d,(GLdouble s, GLdouble t),(s,t),)
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,glDrawBuffer,(GLenum mode),(mode),)

View file

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

View file

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

View file

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

View file

@ -10,6 +10,11 @@
#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
//*******************************************************************
@ -42,6 +47,8 @@ private:
GLFontChar *chars;
} header;
mutable DynamicGPUBuffer vbo;
public:
//Constructor
@ -85,54 +92,78 @@ public:
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
{
unsigned int i;
GLFontChar *glfont_char;
float width, height;
const size_t sz = text.size();
const size_t bytes = sz * 4 * (2+2+4) * sizeof(float);
//Begin rendering quads
glBegin(GL_QUADS);
const float tw = header.tex_width * scalar;
const float th = header.tex_height * scalar;
unsigned int sz = text.size();
float a = 0;
//Loop through characters
for (i = 0; i < sz; i++)
do
{
//Make sure character is in range
unsigned int c = (unsigned int)text[i];
if (c < header.start_char || c > header.end_char)
continue;
float *p = (float*)vbo.beginWrite(GPUBUFTYPE_VEC2_TC_RGBA, bytes, GPUACCESS_DEFAULT);
//Get pointer to glFont character
glfont_char = &header.chars[c - header.start_char];
for(size_t i = 0; i < text.size(); ++i)
{
//Make sure character is in range
unsigned int c = (unsigned int)text[i];
if (c < header.start_char || c > header.end_char)
continue;
//Get width and height
width = (glfont_char->dx * header.tex_width) * scalar;
height = (glfont_char->dy * header.tex_height) * scalar;
const GLFontChar &fc = header.chars[c - header.start_char];
if (i == (sz-1))
a = alpha*lastAlpha;
else
a = alpha;
const float width = fc.dx * tw;
const float height = fc.dy * th;
//Specify colors, vertices, and texture coordinates
glColor4f(top_color[0], top_color[1], top_color[2], a);
glTexCoord2f(glfont_char->tx1, glfont_char->ty1);
glVertex3f(x, y, 0.0F);
glTexCoord2f(glfont_char->tx2, glfont_char->ty1);
glVertex3f(x + width, y, 0.0F);
glColor4f(bottom_color[0], bottom_color[1], bottom_color[2], a);
glTexCoord2f(glfont_char->tx2, glfont_char->ty2);
glVertex3f(x + width, y + height, 0.0F);
glTexCoord2f(glfont_char->tx1, glfont_char->ty2);
glVertex3f(x, y + height, 0.0F);
float a = alpha;
if (i == (sz-1))
a *= lastAlpha;
//Move to next character
x += width;
*p++ = x; // x
*p++ = y; // y
*p++ = fc.tx1; // 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; // 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;
x += width;
}
}
while(!vbo.commitWrite());
//Stop rendering quads
glEnd();
vbo.apply();
size_t last = sz * 4;
for(size_t i = 0; i < last; i += 4)
glDrawArrays(GL_TRIANGLE_FAN, i, 4);
}
};