fixed font xadvance, added color to text, updated test scene

This commit is contained in:
Anselme 2016-07-22 13:06:15 +02:00
parent a2219c0982
commit 0e560c2eef
5 changed files with 56 additions and 28 deletions

View File

@ -7,6 +7,7 @@
#include <resourcemanager.h>
#include <sparrowrenderer.h>
#include <scenetree.h>
#include <texture.h>
#include <tools/graph.h>
#include <tools/pathfinder.h>
@ -50,8 +51,19 @@ int main(){
RESOURCE_ADD(fonte_des_neiges,Font,"shellfont");
MeshNode* mnode = new MeshNode(fonte_des_neiges->getTextMesh("Hello World!"));
MeshNode* mnode/* = new MeshNode(fonte_des_neiges->getTextMesh("Hello World!"));
scene.addObject(scene.getRootObject(),mnode)*/;
mnode = new MeshNode(fonte_des_neiges->getTextMesh("WOW!", glm::vec3(0.5, 0.7, 0.2)));
mnode->setTransform(glm::rotate(glm::translate(glm::mat4(), glm::vec3(70, 30, 0)), 0.4f, glm::vec3(0, 0, 1)));
scene.addObject(scene.getRootObject(),mnode);
mnode = new MeshNode(fonte_des_neiges->getTextMesh("Such Text!", glm::vec3(0.7, 0.4, 0.2)));
mnode->setTransform(glm::rotate(glm::translate(glm::mat4(), glm::vec3(200, 170, 0)), -0.5f, glm::vec3(0, 0, 1)));
scene.addObject(scene.getRootObject(),mnode);
mnode = new MeshNode(fonte_des_neiges->getTextMesh("Very font!", glm::vec3(0.7, 0.2, 0.8)));
mnode->setTransform(glm::rotate(glm::translate(glm::mat4(), glm::vec3(260, 300, 0)), 0.1f, glm::vec3(0, 0, 1)));
scene.addObject(scene.getRootObject(),mnode);
engine.setScene(&scene);

View File

@ -1,24 +1,36 @@
#include "font.h"
#include <iostream>
#include <phongmaterial.h>
Font::Font()
{
}
Mesh* Font::getTextMesh(std::string s)
Mesh* Font::getTextMesh(std::string s, glm::vec3 color)
{
Mesh* text = new Mesh();
glm::vec2 current_pos(50,50);
for(auto carac : s){
CharInfo chinf = m_charTable[carac];
current_pos += chinf.offset;
text->addRectangle2D(current_pos,chinf.dim,chinf.pos,chinf.dim);
current_pos -= chinf.offset;
current_pos.x += (chinf.dim.x + chinf.offset.x);
glm::ivec2 current_pos;
for(char c : s){
if(c == '\n')
{
current_pos.x = 0; // left alignment -> TODO : be able to center or align right
current_pos.y += m_lineHeight;
}
else
{
CharInfo charInfo = m_charTable[c];
text->addRectangle2D(current_pos + charInfo.offset,
charInfo.dim,
glm::vec2(charInfo.pos)/m_scale,
glm::vec2(charInfo.dim)/m_scale);
current_pos.x += charInfo.xadvance;
}
}
text->setMaterial((Material*)m_mat);
text->setDepth(0.5);
PhongMaterial *mat = new PhongMaterial();
// TODO : delete this material somewhere (garbage collector ?)
mat->setTexture(PhongMaterial::ALPHA_SLOT, m_tex, "font_texture");
mat->diffuse = color;
text->setMaterial((Material*)mat);
text->initGL();
return text;
}

View File

@ -4,9 +4,8 @@
#include <map>
#include <glm/vec2.hpp>
#include "mesh.h"
//#include "scenetree.h"
class PhongMaterial;
class Texture;
class Font
{
@ -19,22 +18,27 @@ public:
int xadvance;
};
Font();
// setters
void setName(std::string name){m_name = name;}
void addCharInfo(wchar_t character,CharInfo character_info){ m_charTable[character] = character_info; }
void addCharInfo(wchar_t character, CharInfo character_info){ m_charTable[character] = character_info; }
void setNbChar(int nbchar){m_nbChar =nbchar;}
void setLineHeight(int line_h){m_lineHeight = line_h;}
void setBase(int base){m_base = base;}
void setAntiAnliasing(bool antialiasing){m_antiAliasing = antialiasing;}
void setScale(glm::vec2 scale){m_scale = scale;}
void setMaterial(PhongMaterial *mat){m_mat = mat;}
Mesh* getTextMesh(std::string s);
void setTexture(Texture *tex){m_tex = tex;}
Mesh* getTextMesh(std::string s, glm::vec3 color = glm::vec3(1));
private:
std::string m_name;
PhongMaterial *m_mat;
std::map<wchar_t,CharInfo> m_charTable;
Texture *m_tex;
std::map<wchar_t, CharInfo> m_charTable;
glm::vec2 m_scale;
bool m_antiAliasing;
int m_nbChar, m_lineHeight,m_base;
int m_nbChar;
int m_lineHeight;
int m_base;
};
#endif // FONT_H

View File

@ -34,7 +34,9 @@ std::string* Loader::loadTextFile(const std::string &filename)
Image* Loader::loadImage(const std::string &filename, bool hasAlpha)
{
sf::Image sfImg;
sfImg.loadFromFile(tex_directory+filename);
bool ok = sfImg.loadFromFile(tex_directory+filename);
if(!ok)
return NULL;
Image* img = new Image();
img->depth = hasAlpha ? 32 : 24;
img->width = sfImg.getSize().x;
@ -74,12 +76,12 @@ Font* Loader::loadFont(const std::string &description_file, const std::string &t
std::getline(file, line);
int line_h, base;
glm::vec2 scale;
glm::ivec2 scale;
std::sscanf(line.c_str(),"common lineHeight=%d base=%d scaleW=%d scaleH=%d",
&line_h,&base,&(scale.x),&(scale.y));
font->setLineHeight(line_h);
font->setBase(base);
font->setScale(scale);
font->setScale(glm::vec2(scale));
std::getline(file, line);//ignore 3rd line for now => only use 1 page.
std::getline(file, line);
@ -99,10 +101,7 @@ Font* Loader::loadFont(const std::string &description_file, const std::string &t
Image* fucking_image_of_doom = loadImage(texture_file);
Texture* texture = new Texture(fucking_image_of_doom);
delete fucking_image_of_doom;
PhongMaterial* mat = new PhongMaterial();
mat->setTexture(PhongMaterial::DIFFUSE_SLOT,texture,font_name);
// mat->diffuse = glm::vec3(1,1,1);
font->setMaterial(mat);
font->setTexture(texture);
return font;
}

View File

@ -3,6 +3,7 @@
#include <string>
#include <vector>
#include <glm/vec3.hpp>
class Image;
class Mesh;