Panagiotis Christopoulos Charitos 14 anni fa
parent
commit
2f3660e9e3

File diff suppressed because it is too large
+ 0 - 1
build/debug/Makefile


+ 5 - 1
shaders/Final.glsl

@@ -13,7 +13,11 @@ layout(location = 0) out vec3 fFragColor;
 
 void main()
 {
-	//if( gl_FragCoord.x > 0.5 ) discard;
+	/*if(vTexCoords.y > 0.5)
+	{
+		fFragColor = vec3(1.0, 0.0, 1.0);
+		return;
+	}*/
 
 	fFragColor = texture2D(rasterImage, vTexCoords).rgb;
 	//fFragColor = vec3(texture2D(rasterImage, vTexCoords).b);

+ 3 - 4
shaders/SimpleVert.glsl

@@ -3,15 +3,14 @@
 /// get the Normalized Display Coordinates ([-1,1]) simply by looking in the vertex
 /// position. The vertex positions of the quad are from 0.0 to 1.0 for both axis.
 
-layout(location = 0) in vec2 position;
+layout(location = 0) in vec2 position; // the vert coords are {1.0,1.0}, {0.0,1.0}, {0.0,0.0}, {1.0,0.0}
 
 out vec2 vTexCoords;
 
 void main()
 {
-	vec2 vertPos = position; // the vert coords are {1.0,1.0}, {0.0,1.0}, {0.0,0.0}, {1.0,0.0}
-	vTexCoords = vertPos;
-	vec2 vertPosNdc = vertPos * 2.0 - 1.0;
+	vTexCoords = position;
+	vec2 vertPosNdc = position * 2.0 - 1.0;
 	gl_Position = vec4(vertPosNdc, 0.0, 1.0);
 }
 

+ 3 - 2
shaders/UiText.glsl

@@ -27,8 +27,9 @@ layout(location = 0) out vec4 fColor;
 
 void main()
 {
-	vec4 texCol = texture2D(texture, vTexCoords).rgba * color;
+	float r = texture2D(texture, vTexCoords).r;
 
-	fColor = texCol;
+	fColor = vec4(r) * color;
 	//fColor = texCol - texCol + vec4(1.0, 0.0, 1.0, 0.1);
+	//fColor = texCol - texCol + vec4(texture2D(texture, vTexCoords).r);
 }

+ 2 - 2
src/Core/AsyncLoader.cpp

@@ -34,11 +34,11 @@ void AsyncLoader::load(const char* filename, LoadCallback loadCallback, void* st
 //======================================================================================================================
 void AsyncLoader::workingFunc()
 {
-	glContext = SDL_GL_CreateContext(AppSingleton::getInstance().getWindowId());
+	/*glContext = SDL_GL_CreateContext(AppSingleton::getInstance().getWindowId());
 	if(SDL_GL_MakeCurrent(AppSingleton::getInstance().getWindowId(), glContext) != 0)
 	{
 		throw EXCEPTION("Cannot select GL context");
-	}
+	}*/
 
 
 	while(1)

+ 8 - 5
src/Main.cpp

@@ -41,6 +41,7 @@
 #include "MaterialRuntime.h"
 #include "Globals.h"
 #include "UiFtFontLoader.h"
+#include "UiFont.h"
 
 
 // map (hard coded)
@@ -123,7 +124,8 @@ void init()
 
 	srand(unsigned(time(NULL)));
 
-	painter = new Ui::Painter;
+	painter = new Ui::Painter(AppSingleton::getInstance().getWindowWidth(), AppSingleton::getInstance().getWindowHeight());
+	painter->setFont("engine-rsrc/ModernAntiqua.ttf", 25, 25);
 
 	// camera
 	PerspectiveCamera* cam = new PerspectiveCamera(false, NULL);
@@ -368,11 +370,12 @@ void mainLoop()
 
 		MainRendererSingleton::getInstance().render(*AppSingleton::getInstance().getActiveCam());
 
-		painter->setPosition(Vec2(0.0, 0.0));
+		painter->setPosition(Vec2(0.0, 0.5));
 		painter->setFontSize(Vec2(0.03, 0.03 * MainRendererSingleton::getInstance().getAspectRatio()));
 		painter->setColor(Vec4(1.0));
 
-		painter->drawText("Once upon a time in a place\ncalled \"Kickapoo\"");
+		//painter->drawText("A");
+		painter->drawText("Once uppon a time in a place called Kickapoo.");
 
 		if(InputSingleton::getInstance().getKey(SDL_SCANCODE_ESCAPE))
 		{
@@ -432,11 +435,11 @@ void mainLoop()
 //======================================================================================================================
 int main(int argc, char* argv[])
 {
-	FT_Vector s = {100, 100};
+	/*FT_Vector s = {100, 100};
 	Ui::FtFontLoader fnt("engine-rsrc/ModernAntiqua.ttf", s);
 	fnt.saveImage("/tmp/test.tga");
 
-	return 0;
+	return 0;*/
 
 	try
 	{

+ 0 - 1
src/Renderer/Hdr.cpp

@@ -103,7 +103,6 @@ void Hdr::run()
 	toneSProg->findUniVar("fai")->set(r.getPps().getPrePassFai(), 0);
 	r.drawQuad();
 
-
 	// blurring passes
 	hblurFai.setRepeat(false);
 	fai.setRepeat(false);

+ 1 - 1
src/Renderer/Renderer.cpp

@@ -28,7 +28,7 @@ void Renderer::init(const RendererInitializer& initializer)
 	width = initializer.width;
 	height = initializer.height;
 
-	aspectRatio = float(width)/height;
+	aspectRatio = float(width) / height;
 	framesNum = 0;
 
 	// a few sanity checks

+ 7 - 3
src/Ui/UiFont.cpp

@@ -18,6 +18,8 @@ void Font::create(const char* fontFilename, uint nominalWidth, uint NominalHeigh
 	FT_Vector ftSize = {nominalWidth, NominalHeight};
 	FtFontLoader ft(fontFilename, ftSize);
 
+	ft.saveImage("/tmp/test.tga");
+
 	// - Create glyphs
 	// - Get metrics
 	BOOST_FOREACH(const FtFontLoader::Glyph& ftGlyph, ft.getGlyphs())
@@ -60,13 +62,13 @@ void Font::create(const char* fontFilename, uint nominalWidth, uint NominalHeigh
 			glyph.textureMat(0, 2) = tslX;
 			glyph.textureMat(1, 2) = tslY;
 
-			std::cout << glyph.textureMat << std::endl;
+			//std::cout << glyph.textureMat << std::endl;
 
 			posX += glyph.width;
 		}
 
 		posX = 0;
-		posY += ft.getImageSize().y / FtFontLoader::GLYPH_ROWS;
+		posY += ft.getLineHeight();
 	}
 
 	//
@@ -77,14 +79,16 @@ void Font::create(const char* fontFilename, uint nominalWidth, uint NominalHeigh
 	tinit.height = ft.getImageSize().y;
 	tinit.internalFormat = GL_RED;
 	tinit.format = GL_RED;
-	tinit.type = GL_UNSIGNED_INT;
+	tinit.type = GL_UNSIGNED_BYTE;
 	tinit.data = &ft.getImage()[0];
 	tinit.mipmapping = false;
 	tinit.filteringType = Texture::TFT_NEAREST;
 	tinit.anisotropyLevel = 0;
+	tinit.dataCompression = Texture::DC_NONE;
 
 	map.reset(new Texture());
 	map->create(tinit);
+	map->setRepeat(false);
 }
 
 

+ 5 - 1
src/Ui/UiFont.h

@@ -25,6 +25,10 @@ class Font
 		const Mat3& getGlyphTextureMatrix(char c) const {return glyphs[c - ' '].textureMat;}
 		uint getGlyphWidth(char c) const {return glyphs[c - ' '].width;}
 		uint getGlyphHeight(char c) const {return glyphs[c - ' '].height;}
+		int getGlyphAdvance(char c) const {return glyphs[c - ' '].horizAdvance;}
+		int getGlyphBearingX(char c) const {return glyphs[c - ' '].horizBearingX;}
+		int getGlyphBearingY(char c) const {return glyphs[c - ' '].horizBearingY;}
+		const Texture& getMap() const {return *map;}
 		/// @}
 
 	private:
@@ -46,7 +50,7 @@ class Font
 		/// @param[in] fontFilename The filename of the font to load
 		/// @param[in] nominalWidth The nominal glyph width in pixels
 		/// @param[in] nominalHeight The nominal glyph height in pixels
-		void create(const char* fontFilename, uint nominalWidth, uint NominalHeight);
+		void create(const char* fontFilename, uint nominalWidth, uint nominalHeight);
 };
 
 

+ 23 - 7
src/Ui/UiFtFontLoader.cpp

@@ -36,7 +36,7 @@ void FtFontLoader::getAllGlyphs()
 		}
 	}
 
-	//glyphs[0].metrics.width = glyphs['_' - ' '].metrics.width;
+	glyphs[0].metrics = glyphs['_' - ' '].metrics;
 }
 
 
@@ -64,19 +64,21 @@ void FtFontLoader::computeImageSize()
 {
 	imgSize.x = 0;
 	imgSize.y = 0;
+	lineHeight = 0;
 
 	//
 	// Get img height
 	//
 	BOOST_FOREACH(const Glyph& glyph, glyphs)
 	{
-		if(toPixels(glyph.metrics.height) > imgSize.y)
+		if(toPixels(glyph.metrics.height) > int(lineHeight))
 		{
-			imgSize.y = toPixels(glyph.metrics.height);
+			lineHeight = toPixels(glyph.metrics.height);
 		}
 	}
 
-	imgSize.y = GLYPH_ROWS * imgSize.y;
+	float exp = ceil(log2(GLYPH_ROWS * lineHeight));
+	imgSize.y = pow(2, exp);
 
 	//
 	// Get img width
@@ -99,6 +101,9 @@ void FtFontLoader::computeImageSize()
 			imgSize.x = rowSize;
 		}
 	}
+
+	exp = ceil(log2(imgSize.x));
+	imgSize.x = pow(2, exp);
 }
 
 
@@ -136,7 +141,7 @@ void FtFontLoader::createImage(const char* filename, const FT_Vector& fontSize)
 	// Get final image size and create image buffer
 	computeImageSize();
 
-	size_t size = imgSize.x * imgSize.y * 2 * sizeof(uchar);
+	size_t size = imgSize.x * imgSize.y * 1 * sizeof(uchar);
 	img.resize(size, 128);
 
 	// Draw all glyphs to the image
@@ -164,7 +169,18 @@ void FtFontLoader::createImage(const char* filename, const FT_Vector& fontSize)
 		}
 
 		pos.x = 0;
-		pos.y += imgSize.y / (GLYPH_ROWS);
+		pos.y += lineHeight;
+	}
+
+	// Flip image y
+	for(int i = 0; i < imgSize.y / 2; i++)
+	{
+		for(int j = 0; j < imgSize.x; j++)
+		{
+			uchar tmp = img[i * imgSize.x + j];
+			img[i * imgSize.x + j] = img[(imgSize.y - i - 1) * imgSize.x + j];
+			img[(imgSize.y - i - 1) * imgSize.x + j] = tmp;
+		}
 	}
 
 	// Clean
@@ -200,7 +216,7 @@ void FtFontLoader::saveImage(const char* filename) const
 
 	fwrite(header6, 1, sizeof(header6), fp);
 
-	for(int i = imgSize.y - 1; i > -1; i--)
+	for(int i = 0; i < imgSize.y; i++)
 	{
 		for(int j = 0; j < imgSize.x; j++)
 		{

+ 2 - 0
src/Ui/UiFtFontLoader.h

@@ -45,6 +45,7 @@ class FtFontLoader
 		GETTER_R(Vec<uchar>, img, getImage)
 		GETTER_R(FT_Vector, imgSize, getImageSize)
 		GETTER_R(Vec<Glyph>, glyphs, getGlyphs)
+		GETTER_R_BY_VAL(uint, lineHeight, getLineHeight)
 		/// @}
 
 		/// Save the image (img) to TGA. Its for debugging purposes
@@ -60,6 +61,7 @@ class FtFontLoader
 		Vec<Glyph> glyphs;
 		Vec<uchar> img;
 		FT_Vector imgSize;
+		uint lineHeight; ///< Calculated as the max height among all glyphs
 		/// @}
 
 		/// Reads the face and extracts the glyphs

+ 45 - 37
src/Ui/UiPainter.cpp

@@ -3,6 +3,7 @@
 #include "GlStateMachine.h"
 #include "Texture.h"
 #include "Logger.h"
+#include "UiFont.h"
 
 
 namespace Ui {
@@ -11,19 +12,30 @@ namespace Ui {
 //======================================================================================================================
 // Constructor                                                                                                         =
 //======================================================================================================================
-Painter::Painter()
+Painter::Painter(uint deviceWidth, uint deviceHeight):
+	deviceWidth(deviceWidth),
+	deviceHeight(deviceHeight)
 {
 	init();
 }
 
 
+//======================================================================================================================
+// setFont                                                                                                             =
+//======================================================================================================================
+void Painter::setFont(const char* fontFilename, uint nominalWidth, uint nominalHeight)
+{
+	font.reset(new Font(fontFilename, nominalWidth, nominalHeight));
+}
+
+
 //======================================================================================================================
 // init                                                                                                                =
 //======================================================================================================================
 void Painter::init()
 {
 	// Map
-	fontMap.loadRsrc("engine-rsrc/fontmap-big.png");
+	fontMap.loadRsrc("engine-rsrc/fontmap.png");
 	columns = 16;
 	rows = 8;
 	tabSize = 4;
@@ -32,7 +44,8 @@ void Painter::init()
 	sProg.loadRsrc("shaders/UiText.glsl");
 
 	// Geom
-	float quadVertCoords[][2] = {{1.0, 0.0}, {0.0, 0.0}, {0.0, -1.0}, {1.0, -1.0}};
+	//float quadVertCoords[][2] = {{1.0, 0.0}, {0.0, 0.0}, {0.0, -1.0}, {1.0, -1.0}};
+	float quadVertCoords[][2] = {{1.0, 1.0}, {0.0, 1.0}, {0.0, 0.0}, {1.0, 0.0}};
 	qPositionsVbo.create(GL_ARRAY_BUFFER, sizeof(quadVertCoords), quadVertCoords, GL_STATIC_DRAW);
 
 	ushort quadVertIndeces[2][3] = {{0, 1, 3}, {1, 2, 3}}; // 2 triangles
@@ -51,74 +64,69 @@ void Painter::drawText(const char* text)
 {
 	// Set GL
 	GlStateMachineSingleton::getInstance().enable(GL_BLEND);
+	//GlStateMachineSingleton::getInstance().disable(GL_BLEND);
 	glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
 	GlStateMachineSingleton::getInstance().disable(GL_DEPTH_TEST);
 
 	// SProg (some)
 	sProg->bind();
 
-	sProg->findUniVar("texture")->set(*fontMap, 0);
+	sProg->findUniVar("texture")->set(font->getMap(), 0);
 	sProg->findUniVar("color")->set(&col);
 
 	// Vao
 	qVao.bind();
 
 	// Iterate
-	float horizontalMoving = fontSize.x() * 0.65;
-	float verticalMoving = fontSize.y() /* * 0.7*/;
-	Vec2 p = pos;
+	Vec2 p; // in NDC
+	p.x() = 2.0 * pos.x() - 1.0;
+	p.y() = 2.0 * -pos.y() - 1.0;
 	const char* c = text;
 	while(*c != '\0')
 	{
 		char cc = *c;
 		// Check if special
-		if(cc == '\n')
+		if(cc == ' ')
 		{
-			p.y() += verticalMoving;
+			// do nothing
+		}
+		else if(cc == '\n')
+		{
+			/*p.y() += verticalMoving;
 			p.x() = pos.x();
 			++c;
-			continue;
+			continue;*/
 		}
-		if(cc == '\t') // tab
+		else if(cc == '\t') // tab
 		{
-			p.x() += horizontalMoving * (tabSize - 1);
+			p.x() +=  font->getGlyphWidth(' ') * (tabSize - 1);
 			cc = ' ';
 		}
-		if(cc < ' ' || cc > '~') // out of range
+		else if(cc < ' ' || cc > '~') // out of range
 		{
 			cc = '~' + 1;
 		}
+		else
+		{
+			Mat3 trfM = Mat3::getIdentity();
+			trfM(0, 0) = 2.0 * float(font->getGlyphWidth(cc)) / deviceWidth;
+			trfM(1, 1) = 2.0 * float(font->getGlyphHeight(cc)) / deviceHeight;
+			trfM(0, 2) = p.x() /*+ (font->getGlyphBearingX(cc) / float(deviceWidth))*/;
 
-		// Pos trf
-		Mat3 trf = Mat3::getIdentity();
-		trf(0, 0) = fontSize.x() * 2.0;
-		trf(1, 1) = fontSize.y() * 2.0;
-		trf(0, 2) = p.x() * 2.0 - 1.0;
-		trf(1, 2) = -p.y() * 2.0 + 1.0;
-
-		sProg->findUniVar("transformation")->set(&trf);
-
-		// Tex trf
-		uint i = cc - 32;
-		uint crntRow = i / columns;
-		uint crntColumn = i % columns;
-
-		//INFO(crntRow << " " << crntColumn);
+			trfM(1, 2) = 2 * (font->getGlyphBearingY(cc) - int(font->getGlyphHeight(cc))) / float(deviceHeight);
 
-		Mat3 texTrf = Mat3::getIdentity();
-		texTrf(0, 0) = 1.0 / columns;
-		texTrf(1, 1) = 1.0 / rows;
-		texTrf(0, 2) = crntColumn / float(columns);
-		texTrf(1, 2) = 1.0 - crntRow / float(rows);
+			sProg->findUniVar("transformation")->set(&trfM);
+			sProg->findUniVar("textureTranformation")->set(&font->getGlyphTextureMatrix(cc));
 
-		sProg->findUniVar("textureTranformation")->set(&texTrf);
 
-		// Render
-		glDrawElements(GL_TRIANGLES, 2 * 3, GL_UNSIGNED_SHORT, 0);
+			// Render
+			glDrawElements(GL_TRIANGLES, 2 * 3, GL_UNSIGNED_SHORT, 0);
+		}
 
 		// Inc
 		++c;
-		p.x() += horizontalMoving;
+		//p.x() += 2 * font->getGlyphAdvance(cc) / float(deviceWidth);
+		p.x() += 2 * font->getGlyphWidth(cc) / float(deviceWidth);
 	}
 }
 

+ 13 - 1
src/Ui/UiPainter.h

@@ -1,6 +1,7 @@
 #ifndef UI_PAINTER_H
 #define UI_PAINTER_H
 
+#include <memory>
 #include "RsrcPtr.h"
 #include "Math.h"
 #include "Accessors.h"
@@ -11,16 +12,22 @@
 namespace Ui {
 
 
+class Font;
+
+
 class Painter
 {
 	public:
-		Painter();
+		Painter(uint deviceWidth = 0, uint deviceHeight = 0);
 
 		/// @name Accessors
 		/// @{
 		GETTER_SETTER(Vec2, pos, getPosition, setPosition)
 		GETTER_SETTER(Vec2, fontSize, getFontSize, setFontSize)
 		GETTER_SETTER(Vec4, col, getColor, setColor)
+		//GETTER_SETTER(boost::array<uint, 2>, deviceSize, getDeviceSize, setDeviceSize)
+		void setFont(const char* fontFilename, uint nominalWidth, uint nominalHeight);
+		const Font& getFont() const {return *font;}
 		/// @}
 
 		void drawText(const char* text);
@@ -29,6 +36,8 @@ class Painter
 	private:
 		/// @name Data
 		/// @{
+		std::auto_ptr<Font> font;
+
 		RsrcPtr<Texture> fontMap;
 		uint columns;
 		uint rows;
@@ -42,6 +51,9 @@ class Painter
 		Vbo qPositionsVbo;
 		Vbo qIndecesVbo;
 		Vao qVao;
+
+		uint deviceWidth; ///< The size of the device in pixels
+		uint deviceHeight;
 		/// @}
 
 		void init();

Some files were not shown because too many files changed in this diff