20 #include <QtGui/QImage> 21 #include <QFontMetrics> 22 #include <unordered_map> 43 (j = _num & 0xFFFF0000) || (j = _num);
44 (k = j & 0xFF00FF00) || (k = j);
45 (j = k & 0xF0F0F0F0) || (j = k);
46 (k = j & 0xCCCCCCCC) || (k = j);
47 (j = k & 0xAAAAAAAA) || (j = k);
57 QFontMetrics metric(_f);
60 int fontHeight=metric.height();
64 const static char startChar=
' ';
65 const static char endChar=
'~';
75 std::unordered_map <int,AbstractVAO *> widthVAO;
77 for(
char c=startChar;
c<=endChar; ++
c)
95 Real s1=width*1.0f/widthPow2;
99 Real t1=metric.height()*-1.0f/heightPow2;
107 finalImage.fill(Qt::transparent);
110 painter.begin(&finalImage);
112 painter.setRenderHints(QPainter::HighQualityAntialiasing
113 | QPainter::TextAntialiasing);
118 painter.setPen(Qt::black);
120 painter.drawText(0, metric.ascent(), QString(
c));
140 finalImage=finalImage.convertToFormat(QImage::Format_ARGB32_Premultiplied);
142 int widthTexture=finalImage.width();
143 int heightTexture=finalImage.height();
144 std::unique_ptr<unsigned char []>
data(
new unsigned char[ widthTexture*heightTexture * 4]);
145 unsigned int index=0;
147 for(
int y=heightTexture-1;
y>0; --
y)
149 for(
int x=0;
x<widthTexture; ++
x)
151 colour=finalImage.pixel(
x,
y);
152 data[index++]=
static_cast<unsigned char>(qRed(colour));
153 data[index++]=
static_cast<unsigned char>(qGreen(colour));
154 data[index++]=
static_cast<unsigned char>(qBlue(colour));
155 data[index++]=
static_cast<unsigned char>(qAlpha(colour));
164 if (widthVAO.find(width) ==widthVAO.end())
178 std::array<textVertData,6> d;
242 std::cout<<
"created "<<widthVAO.size()<<
" unique billboards\n";
244 this->setColour(0,0,0);
245 this->setTransform(1.0,1.0);
253 for(
auto &
m : m_characters)
273 (*shader)[
"nglTextShader"]->use();
283 int textLength=text.length();
285 for (
int i = 0; i < textLength; ++i)
292 FontChar f = m_characters[text[i].toLatin1()];
317 float scaleX=2.0f/_w;
318 float scaleY=-2.0f/_h;
325 (*shader)[
"nglTextShader"]->use();
345 (*shader)[
"nglTextShader"]->use();
356 (*shader)[
"nglTextShader"]->use();
365 (*shader)[
"nglTextShader"]->use();
GLAPI void GLAPIENTRY glDeleteTextures(GLsizei n, const GLuint *textures)
simple class to hold colour information and set the basic opengl colour state. also has overloaded me...
GLAPI void GLAPIENTRY glEnable(GLenum cap)
static AbstractVAO * createVAO(const std::string &_type, GLenum _mode=GL_TRIANGLES)
GLuint textureID
the width of the font
GLAPI void GLAPIENTRY glTexImage2D(GLenum target, GLint level, GLint internalformat, GLsizei width, GLsizei height, GLint border, GLenum format, GLenum type, const void *pixels)
Singleton Class to init and Use GLSL Shaders the class stores the shaders as a map of shader objects ...
a structure to hold the font char texture id and the vao. The vao for each font will be a different s...
GLuint GLfloat GLfloat GLfloat GLfloat GLfloat GLfloat GLfloat GLfloat GLfloat t1
Text(const QFont &_f) noexcept
ctor must pass in a ready constructed QFont make sure the size and emphasis is set before doing this ...
void setColour(const Colour &_c) noexcept
set the colour of the font from an Colour
#define GL_ONE_MINUS_SRC_ALPHA
GLint GLint GLint GLint GLint GLint y
void setRegisteredUniform2f(const std::string &_paramName, float _p1, float _p2) noexcept
set the pre-registered uniform
GLint GLenum GLsizei GLint GLsizei const void * data
void setScreenSize(int _w, int _h) noexcept
set the size of the screen to scale our font to fit correctly this basically creates the orthographic...
main shader loader / manager class for GLSL shaders
implementation files for RibExport class
void setVertexAttributePointer(GLuint _id, GLint _size, GLenum _type, GLsizei _stride, unsigned int _dataOffset, bool _normalise=false)
set the generic vertex attribute pointer data usually this method will do however the user may occasi...
GLuint GLfloat GLfloat GLfloat GLfloat GLfloat GLfloat GLfloat GLfloat s1
GLAPI void GLAPIENTRY glBlendFunc(GLenum sfactor, GLenum dfactor)
PRECISION Real
create a variable called Real which is the main data type we use (GLfloat for most cases) ...
GLAPI void GLAPIENTRY glBindTexture(GLenum target, GLuint texture)
GLuint GLfloat GLfloat GLfloat GLfloat GLfloat GLfloat s0
void setRegisteredUniform1f(const std::string &_paramName, float _p1) noexcept
set the pre-registered uniform
base class for all VAO from the VAOFactory this defines the base class type with simple draw / bind b...
fmt::BufferedFile & move(fmt::BufferedFile &f)
void setTransform(float _x, float _y) noexcept
void setNumIndices(size_t _s)
the number of indices to draw in the array. It may be that the draw routine can overide this at anoth...
GLAPI void GLAPIENTRY glTexParameteri(GLenum target, GLenum pname, GLint param)
AbstractVAO * vao
the texture id of the font billboard
void unbind()
unbind the VAO by binding default 0
void bind()
bind the VAO so it can be used.
virtual void setData(const VertexData &_data)=0
this method is used to set the data in the VAO, we have a base data type of VertexData above...
Basic text rendering for OpenGL 3.x.
#define GL_TEXTURE_MIN_FILTER
void renderText(float _x, float _y, const QString &_text) const noexcept
render the text to the screen at _x,_y where 0,0 is top left of the screen all positioning is relativ...
#define GL_TEXTURE_MAG_FILTER
GLint GLint GLint GLint GLint x
static ShaderLib * instance()
Get the instance.
GLAPI void GLAPIENTRY glGenTextures(GLsizei n, GLuint *textures)
void setRegisteredUniform3f(const std::string &_paramName, float _p1, float _p2, float _p3) noexcept
set the pre-registered uniform
virtual void draw() const =0
this is the draw method for the VAO the user must implement this per VAO data, usually this will be a...
GLuint GLfloat GLfloat GLfloat GLfloat GLfloat GLfloat GLfloat t0
unsigned int nearestPowerOfTwo(unsigned int _num)
code taken from here http://jeffreystedfast.blogspot.com/2008/06/calculating-nearest-power-of-2.html
GLint GLint GLint GLint GLint GLint GLsizei width
GLAPI void GLAPIENTRY glDisable(GLenum cap)
~Text()
dtor will clean / remove textures and VAO's for the class
#define GL_NEAREST_MIPMAP_LINEAR