From c208d64e225a80085c2634a67d558c05929fd8d7 Mon Sep 17 00:00:00 2001 From: chai Date: Wed, 19 Sep 2018 16:29:02 +0800 Subject: *update --- build/05Font/05Font.vcxproj | 2 +- build/libjin/libjin.vcxproj | 3 +- build/libjin/libjin.vcxproj.filters | 10 ++++-- libjin/3rdparty/ogl/OpenGL.h | 71 +++++++++++++++++++++++++++++++++++++ libjin/Graphics/Drawable.cpp | 2 +- libjin/Graphics/Drawable.h | 2 +- libjin/Graphics/Font.cpp | 40 ++++++++++++--------- libjin/Graphics/Font.h | 14 ++++---- libjin/Graphics/OpenGL.cpp | 11 +----- libjin/Graphics/OpenGL.h | 33 +---------------- libjin/Graphics/Texture.cpp | 50 +++++++++++++------------- libjin/Graphics/Window.cpp | 2 +- test/05Font/main.cpp | 30 +++++++++++++--- 13 files changed, 167 insertions(+), 103 deletions(-) create mode 100644 libjin/3rdparty/ogl/OpenGL.h diff --git a/build/05Font/05Font.vcxproj b/build/05Font/05Font.vcxproj index 046472d..5827205 100644 --- a/build/05Font/05Font.vcxproj +++ b/build/05Font/05Font.vcxproj @@ -22,7 +22,7 @@ 15.0 {D1953718-E728-4A86-9CCF-8BEC1F5C5F97} My05Font - 10.0.14393.0 + 8.1 diff --git a/build/libjin/libjin.vcxproj b/build/libjin/libjin.vcxproj index 9dac72a..6cd4b76 100644 --- a/build/libjin/libjin.vcxproj +++ b/build/libjin/libjin.vcxproj @@ -58,6 +58,7 @@ + @@ -126,7 +127,7 @@ {407E9199-D39C-4460-B218-0C29AB42483B} Win32Proj libjin - 10.0.14393.0 + 8.1 diff --git a/build/libjin/libjin.vcxproj.filters b/build/libjin/libjin.vcxproj.filters index 8a02ed1..c84c01f 100644 --- a/build/libjin/libjin.vcxproj.filters +++ b/build/libjin/libjin.vcxproj.filters @@ -55,6 +55,9 @@ {0de74e8c-939d-4275-a4d6-a1c075b8eeb2} + + {01c41441-9b31-4a63-b3dd-18b4bfdf61ac} + @@ -159,10 +162,10 @@ Graphics - + Graphics - + Graphics @@ -346,6 +349,9 @@ Graphics + + 3rdparty\ogl2d + diff --git a/libjin/3rdparty/ogl/OpenGL.h b/libjin/3rdparty/ogl/OpenGL.h new file mode 100644 index 0000000..c4bc78a --- /dev/null +++ b/libjin/3rdparty/ogl/OpenGL.h @@ -0,0 +1,71 @@ +#ifndef __OGL2D_H +#define __OGL2D_H +#include +//#include "../GLee/GLee.h" + +/* 2d wrap of opengl 3.0 */ +class OpenGL +{ +public: + OpenGL() {}; + ~OpenGL() {}; + + struct Texture + { + GLuint texture; + GLint level; + GLint internalformat; + }; + + struct Color8i { unsigned char r, g, b, a; }; + struct Color32f { float r, g, b, a; }; + + void flushError(); + GLuint genTexture(); + void bindTexture(GLuint texture); + void setTexParameter(GLenum pname, GLint param); + + Texture genTexturex(); + void bindTexture(const Texture& texture); + + void drawColor(const Color8i color); + void drawColor(const Color32f color); + +private: + std::vector _color; + +}; + +/* OpenGL instance singleton */ +extern OpenGL gl; + +#if defined(OGL2D_IMPLEMENT) + +OpenGL gl; + +void OpenGL::flushError() +{ + while (glGetError() != GL_NO_ERROR); +} + +GLuint OpenGL::genTexture() +{ + GLuint t; + glGenTextures(1, &t); + return t; +} + +void OpenGL::bindTexture(GLuint texture) +{ + glBindTexture(GL_TEXTURE_2D, texture); +} + +void OpenGL::setTexParameter(GLenum pname, GLint param) +{ + glTexParameteri(GL_TEXTURE_2D, pname, param); +} + + +#endif // OGL2D_IMPLEMENT + +#endif \ No newline at end of file diff --git a/libjin/Graphics/Drawable.cpp b/libjin/Graphics/Drawable.cpp index 848880c..3ba4717 100644 --- a/libjin/Graphics/Drawable.cpp +++ b/libjin/Graphics/Drawable.cpp @@ -42,7 +42,7 @@ namespace graphics static jin::math::Matrix t; t.setTransformation(x, y, r, sx, sy, anchor.x, anchor.y); - glBindTexture(GL_TEXTURE_2D, texture); + gl.bindTexture(texture); /* push modle matrix */ glPushMatrix(); diff --git a/libjin/Graphics/Drawable.h b/libjin/Graphics/Drawable.h index 08614da..4f7d760 100644 --- a/libjin/Graphics/Drawable.h +++ b/libjin/Graphics/Drawable.h @@ -4,7 +4,7 @@ #if LIBJIN_MODULES_RENDER #include "../math/Vector2.hpp" -#include "../3rdparty/GLee/GLee.h" +#include "OpenGL.h" namespace jin { diff --git a/libjin/Graphics/Font.cpp b/libjin/Graphics/Font.cpp index c1afd53..d401a97 100644 --- a/libjin/Graphics/Font.cpp +++ b/libjin/Graphics/Font.cpp @@ -1,6 +1,7 @@ #include "../jin_configuration.h" #if LIBJIN_MODULES_RENDER +#include "OpenGL.h" #include "font.h" #include #include "color.h" @@ -95,35 +96,36 @@ namespace graphics } } - bool Font::createAtlas() + GLuint Font::createAtlas() { GLuint t; - glGenTextures(1, &t); - glBindTexture(GL_TEXTURE_2D, t); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); + gl.flushError(); + t = gl.genTexture(); + gl.bindTexture(t); + gl.setTexParameter(GL_TEXTURE_MAG_FILTER, GL_LINEAR); + gl.setTexParameter(GL_TEXTURE_MIN_FILTER, GL_LINEAR); + gl.setTexParameter(GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); + gl.setTexParameter(GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, textureWidth, textureHeight, 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL); if (glGetError() != GL_NO_ERROR) { glDeleteTextures(1, &t); glBindTexture(GL_TEXTURE_2D, 0); - return false; + return 0; } atlases.push_back(t); glBindTexture(GL_TEXTURE_2D, 0); - return true; + return t; } - void Font::print(const char* t, int x, int y) + void Font::print(const char* t, int x, int y, int lineheight, int spacing) { - Page* page = typeset(t, x, y); + Page* page = typeset(t, x, y, lineheight, spacing); render(page); delete page; } - Page* Font::typeset(const char* t, int x, int y) + Page* Font::typeset(const char* t, int x, int y, int lineheight, int spacing) { // typesetting Page* page = new Page(); @@ -137,13 +139,17 @@ namespace graphics for (int i = 0; *t != NULL; i += 4) { t = utf8toCodepoint(t, &c); - if (c == 0x0D) + if (c == 0x0D) + { + i -= 4; continue; + } /* new line */ if (c == 0x0A) { - p.y += descent; + p.y += lineheight; p.x = x; + i -= 4; continue; } glyph = findGlyph(c); @@ -175,7 +181,7 @@ namespace graphics vertex.y = p.y; vertex.v = bbox.y; glyphvertices.push_back(vertex); - p.x += glyph->width; + p.x += glyph->width + spacing; } return page; } @@ -289,8 +295,8 @@ namespace graphics cursor.y += descent; if (cursor.y + descent * 2 > textureHeight) { - /* create another atlas */ - createAtlas(); + /* create new atlas */ + atlas = createAtlas(); cursor.y = 0; } } diff --git a/libjin/Graphics/Font.h b/libjin/Graphics/Font.h index e944b41..b09d393 100644 --- a/libjin/Graphics/Font.h +++ b/libjin/Graphics/Font.h @@ -22,9 +22,9 @@ namespace graphics struct GlyphArrayDrawInfo { - GLuint texture; - unsigned int start; - unsigned int count; + GLuint texture; // atlas + unsigned int start; // glyph vertex indecies + unsigned int count; // glyph vertex count }; struct Glyph @@ -53,15 +53,15 @@ namespace graphics static Font* createFont(FontData* fontData, unsigned int fontSzie); - void print(const char* text, int x, int y); - Page* typeset(const char* text, int x, int y); + Page* typeset(const char* text, int x, int y, int lineheight, int spacing); void render(const Page* page); + void print(const char* text, int x, int y, int lineheight, int spacing); + //Bitmap* bake(const char* text); #if defined(font_debug) void drawAtlas(); #endif private: - /* font atlas levels */ static const int TEXTURE_SIZE_LEVELS_COUNT = 7; static const int TEXTURE_SIZE_LEVEL_MAX = TEXTURE_SIZE_LEVELS_COUNT - 1; static const int TEXTURE_WIDTHS[TEXTURE_SIZE_LEVELS_COUNT]; @@ -71,7 +71,7 @@ namespace graphics ~Font(); void estimateSize(); - bool createAtlas(); + GLuint createAtlas(); Glyph* bakeGlyph(Codepoint character); Glyph* findGlyph(Codepoint character); diff --git a/libjin/Graphics/OpenGL.cpp b/libjin/Graphics/OpenGL.cpp index d89f721..ff9d307 100644 --- a/libjin/Graphics/OpenGL.cpp +++ b/libjin/Graphics/OpenGL.cpp @@ -1,11 +1,2 @@ +#define OGL2D_IMPLEMENT #include "OpenGL.h" - -namespace jin -{ -namespace graphics -{ - - - -} -} \ No newline at end of file diff --git a/libjin/Graphics/OpenGL.h b/libjin/Graphics/OpenGL.h index 2f087be..f03cea5 100644 --- a/libjin/Graphics/OpenGL.h +++ b/libjin/Graphics/OpenGL.h @@ -1,33 +1,2 @@ -#ifndef __LIBJIN_OPENGL_H -#define __LIBJIN_OPENGL_H -#include #include "../3rdparty/GLee/GLee.h" -#include "Color.h" - -namespace jin -{ -namespace graphics -{ - - /* 2d wrap of opengl 3.0 */ - class OpenGL - { - public: - OpenGL(); - - void bindTexture(GLuint texture); - - private: - std::vector color; - - ~OpenGL(); - - }; - - /* OpenGL instance singleton */ - extern OpenGL gl; - -} -} - -#endif \ No newline at end of file +#include "../3rdparty/ogl/OpenGL.h" diff --git a/libjin/Graphics/Texture.cpp b/libjin/Graphics/Texture.cpp index dbc7b70..4d73f76 100644 --- a/libjin/Graphics/Texture.cpp +++ b/libjin/Graphics/Texture.cpp @@ -8,38 +8,38 @@ namespace jin { - namespace graphics - { +namespace graphics +{ - using namespace jin::math; + using namespace jin::math; - /*static*/ Texture* Texture::createTexture(Bitmap* bitmap) - { - Texture* tex = new Texture(bitmap); - return tex; - } + /*static*/ Texture* Texture::createTexture(Bitmap* bitmap) + { + Texture* tex = new Texture(bitmap); + return tex; + } - Texture::Texture(const Bitmap* bitmap) - : Drawable(bitmap->getWidth(), bitmap->getHeight()) - { - unsigned int w = size.w; - unsigned int h = size.h; - const Color* pixels = bitmap->getPixels(); + Texture::Texture(const Bitmap* bitmap) + : Drawable(bitmap->getWidth(), bitmap->getHeight()) + { + unsigned int w = size.w; + unsigned int h = size.h; + const Color* pixels = bitmap->getPixels(); - glGenTextures(1, &texture); - glBindTexture(GL_TEXTURE_2D, texture); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); - glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, w, h, 0, GL_RGBA, GL_UNSIGNED_BYTE, pixels); + texture = gl.genTexture(); + gl.bindTexture(texture); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); + glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, w, h, 0, GL_RGBA, GL_UNSIGNED_BYTE, pixels); - glBindTexture(GL_TEXTURE_2D, 0); - } + glBindTexture(GL_TEXTURE_2D, 0); + } - Texture::~Texture() - { - } + Texture::~Texture() + { + } - } // graphics +} // graphics } // jin #endif // LIBJIN_MODULES_RENDER \ No newline at end of file diff --git a/libjin/Graphics/Window.cpp b/libjin/Graphics/Window.cpp index bafc9ea..c3743d3 100644 --- a/libjin/Graphics/Window.cpp +++ b/libjin/Graphics/Window.cpp @@ -3,7 +3,7 @@ #include #include "window.h" -#include "../3rdparty/GLee/GLee.h" +#include "OpenGL.h" #include "canvas.h" #include "../utils/utils.h" #include "../audio/sdl/SDLAudio.h" diff --git a/test/05Font/main.cpp b/test/05Font/main.cpp index 78a1142..da65c2e 100644 --- a/test/05Font/main.cpp +++ b/test/05Font/main.cpp @@ -10,6 +10,7 @@ Font* font = nullptr; Canvas* canvas; FontData* data = nullptr; JSLProgram* shader = nullptr; +Page* page = nullptr; float dt; void onLoad() { @@ -17,18 +18,18 @@ void onLoad() uniform float dt; Color effect(Color col, Texture tex, vec2 uv, vec2 screen) { - float a = Texel(tex, uv).a; - return Color(col.rgb, a/5); + return Texel(tex, uv); } )"; shader = JSLProgram::createJSLProgram(program); Filesystem* fs = Filesystem::get(); fs->mount("../Debug"); Buffer buffer; - fs->read("simhei.ttf", &buffer); + fs->read("font.ttf", &buffer); data = FontData::createFontData((const unsigned char*)buffer.data, buffer.size); font = Font::createFont(data, 14); //canvas = Canvas::createCanvas(100, 100); + //page = font->typeset("こんにちは世界!", 120, 20); } void onEvent(jin::input::Event* e) @@ -54,10 +55,29 @@ void onDraw() if (font != nullptr) { glColor4f(1, 1, 1, 1); - font->print(u8"Hello,你好\n啊 world!", 10, 10); + //font->print(u8"Hello,你好\n啊 world!", 10, 10); //font->print(u8"Привет мир!", 10, 10 + 15 * 1); - //font->print(u8"こんにちは世界!", 10, 10 + 15 * 2); + font->print(u8R"( +abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ +平安時代中期の物語。紫式部著。ただし,そのすべてが紫式部の筆に成るのでは +ないとする説もある。 54帖。寛弘 (1004~12) 頃成立か。物語は3部に分けてみ +ることができる。第1部は,容貌,才能などすべてにすぐれた主人公光源氏が,多 +啊哈噶科膜卡して広く迎えられている。貴族社会の苦悩を摘出したところに磁瓷得 +ることができる。第1部は,容貌,才能などすべてにすぐれた主人公光源氏が,多 +くの女性と関係をもちながら,運命に導かれて栄華をきわめる姿を描く。これに対 +して第2部は苦悩の世界であって,光源氏は最愛の紫の上を失い,栄華は内側から崩 +壊する。第3部 (宇治十帖) は光源氏没後の物語で,不義によって生れた薫大将を主 +人公として,不安に満ちた暗い世界が展開される。さまざまな恋愛と運命的な人生の +うちに,貴族社会の苦悩を摘出したところに価値があり,現代では,世界的な文学と +して広く迎えられている。abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ +漫画自1999年开始在日本集英社旗下的少年漫画杂志《周刊少年Jump》上连载。2002年 +,由日本动画工作室Studio Pierrot根据漫画原作所改编制作的电视动画版《火影忍者 +》开始在日本东京电视台播出。2004年,漫画进而改编成电影。2006年,漩涡鸣人入选 +美国《新闻周刊》日文版于10月18日发行的特集中选出的“全世界最受尊敬的100位日本 +人”。[2] +)", 10, 10 + 15 * 2, 15, 0); //font->print(u8"你好世界!", 10, 10 + 15*3); + //font->render(page); glColor4f(1, 1, 1, 1); } shader->unuse(); -- cgit v1.1-26-g67d0