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 <resourcemanager.h>
#include <sparrowrenderer.h> #include <sparrowrenderer.h>
#include <scenetree.h> #include <scenetree.h>
#include <texture.h>
#include <tools/graph.h> #include <tools/graph.h>
#include <tools/pathfinder.h> #include <tools/pathfinder.h>
@ -50,8 +51,19 @@ int main(){
RESOURCE_ADD(fonte_des_neiges,Font,"shellfont"); 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); scene.addObject(scene.getRootObject(),mnode);
engine.setScene(&scene); engine.setScene(&scene);

View File

@ -1,24 +1,36 @@
#include "font.h" #include "font.h"
#include <iostream> #include <phongmaterial.h>
Font::Font() Font::Font()
{ {
} }
Mesh* Font::getTextMesh(std::string s) Mesh* Font::getTextMesh(std::string s, glm::vec3 color)
{ {
Mesh* text = new Mesh(); Mesh* text = new Mesh();
glm::vec2 current_pos(50,50); glm::ivec2 current_pos;
for(auto carac : s){ for(char c : s){
CharInfo chinf = m_charTable[carac]; if(c == '\n')
current_pos += chinf.offset; {
text->addRectangle2D(current_pos,chinf.dim,chinf.pos,chinf.dim); current_pos.x = 0; // left alignment -> TODO : be able to center or align right
current_pos -= chinf.offset; current_pos.y += m_lineHeight;
current_pos.x += (chinf.dim.x + chinf.offset.x);
} }
text->setMaterial((Material*)m_mat); else
text->setDepth(0.5); {
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;
}
}
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(); text->initGL();
return text; return text;
} }

View File

@ -4,9 +4,8 @@
#include <map> #include <map>
#include <glm/vec2.hpp> #include <glm/vec2.hpp>
#include "mesh.h" #include "mesh.h"
//#include "scenetree.h"
class PhongMaterial; class Texture;
class Font class Font
{ {
@ -19,6 +18,8 @@ public:
int xadvance; int xadvance;
}; };
Font(); Font();
// setters
void setName(std::string name){m_name = name;} 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 setNbChar(int nbchar){m_nbChar =nbchar;}
@ -26,15 +27,18 @@ public:
void setBase(int base){m_base = base;} void setBase(int base){m_base = base;}
void setAntiAnliasing(bool antialiasing){m_antiAliasing = antialiasing;} void setAntiAnliasing(bool antialiasing){m_antiAliasing = antialiasing;}
void setScale(glm::vec2 scale){m_scale = scale;} void setScale(glm::vec2 scale){m_scale = scale;}
void setMaterial(PhongMaterial *mat){m_mat = mat;} void setTexture(Texture *tex){m_tex = tex;}
Mesh* getTextMesh(std::string s);
Mesh* getTextMesh(std::string s, glm::vec3 color = glm::vec3(1));
private: private:
std::string m_name; std::string m_name;
PhongMaterial *m_mat; Texture *m_tex;
std::map<wchar_t, CharInfo> m_charTable; std::map<wchar_t, CharInfo> m_charTable;
glm::vec2 m_scale; glm::vec2 m_scale;
bool m_antiAliasing; bool m_antiAliasing;
int m_nbChar, m_lineHeight,m_base; int m_nbChar;
int m_lineHeight;
int m_base;
}; };
#endif // FONT_H #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) Image* Loader::loadImage(const std::string &filename, bool hasAlpha)
{ {
sf::Image sfImg; sf::Image sfImg;
sfImg.loadFromFile(tex_directory+filename); bool ok = sfImg.loadFromFile(tex_directory+filename);
if(!ok)
return NULL;
Image* img = new Image(); Image* img = new Image();
img->depth = hasAlpha ? 32 : 24; img->depth = hasAlpha ? 32 : 24;
img->width = sfImg.getSize().x; 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); std::getline(file, line);
int line_h, base; int line_h, base;
glm::vec2 scale; glm::ivec2 scale;
std::sscanf(line.c_str(),"common lineHeight=%d base=%d scaleW=%d scaleH=%d", std::sscanf(line.c_str(),"common lineHeight=%d base=%d scaleW=%d scaleH=%d",
&line_h,&base,&(scale.x),&(scale.y)); &line_h,&base,&(scale.x),&(scale.y));
font->setLineHeight(line_h); font->setLineHeight(line_h);
font->setBase(base); 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);//ignore 3rd line for now => only use 1 page.
std::getline(file, line); 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); Image* fucking_image_of_doom = loadImage(texture_file);
Texture* texture = new Texture(fucking_image_of_doom); Texture* texture = new Texture(fucking_image_of_doom);
delete fucking_image_of_doom; delete fucking_image_of_doom;
PhongMaterial* mat = new PhongMaterial(); font->setTexture(texture);
mat->setTexture(PhongMaterial::DIFFUSE_SLOT,texture,font_name);
// mat->diffuse = glm::vec3(1,1,1);
font->setMaterial(mat);
return font; return font;
} }

View File

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