diff options
author | chai <chaifix@163.com> | 2018-09-18 08:26:07 +0800 |
---|---|---|
committer | chai <chaifix@163.com> | 2018-09-18 08:26:07 +0800 |
commit | 746290f4fc003311ec43b47584a065e6e8afa3a5 (patch) | |
tree | f37a4659ce713b698fad34ed2284d53806a288d5 | |
parent | be4b87560dd82068c99948d5d45badd2c42369af (diff) |
*update
-rw-r--r-- | build/05Font/05Font.vcxproj | 4 | ||||
-rw-r--r-- | build/libjin/libjin.vcxproj | 2 | ||||
-rw-r--r-- | libjin/Graphics/Font.cpp | 55 | ||||
-rw-r--r-- | libjin/Graphics/Font.h | 6 | ||||
-rw-r--r-- | libjin/Graphics/FontData.cpp | 8 | ||||
-rw-r--r-- | libjin/Graphics/FontData.h | 4 | ||||
-rw-r--r-- | libjin/Graphics/font.shader.h | 3 | ||||
-rw-r--r-- | test/05Font/main.cpp | 27 |
8 files changed, 55 insertions, 54 deletions
diff --git a/build/05Font/05Font.vcxproj b/build/05Font/05Font.vcxproj index 2fe9b4f..046472d 100644 --- a/build/05Font/05Font.vcxproj +++ b/build/05Font/05Font.vcxproj @@ -22,7 +22,7 @@ <VCProjectVersion>15.0</VCProjectVersion> <ProjectGuid>{D1953718-E728-4A86-9CCF-8BEC1F5C5F97}</ProjectGuid> <RootNamespace>My05Font</RootNamespace> - <WindowsTargetPlatformVersion>8.1</WindowsTargetPlatformVersion> + <WindowsTargetPlatformVersion>10.0.14393.0</WindowsTargetPlatformVersion> </PropertyGroup> <Import Project="$(VCTargetsPath)\Microsoft.Cpp.Default.props" /> <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'" Label="Configuration"> @@ -104,7 +104,7 @@ <OptimizeReferences>true</OptimizeReferences> <AdditionalLibraryDirectories>$(SolutionDir)\lib\SDL2-2.0.5\lib\x86\</AdditionalLibraryDirectories> <AdditionalDependencies>SDL2main.lib;SDL2.lib;opengl32.lib;glu32.lib;%(AdditionalDependencies)</AdditionalDependencies> - <SubSystem>Console</SubSystem> + <SubSystem>Windows</SubSystem> </Link> </ItemDefinitionGroup> <ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'"> diff --git a/build/libjin/libjin.vcxproj b/build/libjin/libjin.vcxproj index fbc7640..6f3f11b 100644 --- a/build/libjin/libjin.vcxproj +++ b/build/libjin/libjin.vcxproj @@ -123,7 +123,7 @@ <ProjectGuid>{407E9199-D39C-4460-B218-0C29AB42483B}</ProjectGuid> <Keyword>Win32Proj</Keyword> <RootNamespace>libjin</RootNamespace> - <WindowsTargetPlatformVersion>8.1</WindowsTargetPlatformVersion> + <WindowsTargetPlatformVersion>10.0.14393.0</WindowsTargetPlatformVersion> </PropertyGroup> <Import Project="$(VCTargetsPath)\Microsoft.Cpp.Default.props" /> <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'" Label="Configuration"> diff --git a/libjin/Graphics/Font.cpp b/libjin/Graphics/Font.cpp index f0cbeb2..c8cc56d 100644 --- a/libjin/Graphics/Font.cpp +++ b/libjin/Graphics/Font.cpp @@ -68,7 +68,7 @@ namespace graphics font->pushFontsize(fontsize); baseline = font->getBaseline(); font->popFontsize(); - createTexture(); + createAtlas(); } Font::~Font() @@ -80,7 +80,7 @@ namespace graphics } } - bool Font::createTexture() + bool Font::createAtlas() { GLuint t; glGenTextures(1, &t); @@ -89,14 +89,14 @@ namespace graphics 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); - glTexImage2D(GL_TEXTURE_2D, 0, GL_R8, TEXTURE_SIZE, TEXTURE_SIZE, 0, GL_RED, GL_UNSIGNED_BYTE, NULL); + glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, TEXTURE_SIZE, TEXTURE_SIZE, 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL); if (glGetError() != GL_NO_ERROR) { glDeleteTextures(1, &t); glBindTexture(GL_TEXTURE_2D, 0); return false; } - textures.push_back(t); + atlases.push_back(t); glBindTexture(GL_TEXTURE_2D, 0); return true; } @@ -117,7 +117,7 @@ namespace graphics p = utf8toCodepoint(p, &c); Glyph* glyph = findGlyph(c); if (texture == 0) - texture = glyph->texture; + texture = glyph->atlas; float xoff = glyph->box.xoff; float yoff = glyph->box.yoff; float width = glyph->box.width; @@ -129,7 +129,7 @@ namespace graphics glyphvertices.push_back(GlyphVertex(dx + mx, dy + my, xoff + width, yoff + height)); glyphvertices.push_back(GlyphVertex(dx + mx, dy, xoff + width, yoff)); dx += glyph->box.width * TEXTURE_SIZE; - if (glyph->texture != texture && texture != 0 || *p == NULL) + if (glyph->atlas != texture && texture != 0 || *p == NULL) { GlyphArrayDrawInfo textureInfo; textureInfo.texture = texture; @@ -143,8 +143,6 @@ namespace graphics } while (*p != NULL); glEnableClientState(GL_VERTEX_ARRAY); glEnableClientState(GL_TEXTURE_COORD_ARRAY); - static JSLProgram* shader = JSLProgram::createJSLProgram(font_shader); - shader->use(); for (int i = 0; i < glyphinfolist.size(); ++i) { GlyphArrayDrawInfo& info = glyphinfolist[i]; @@ -158,9 +156,9 @@ namespace graphics glBindTexture(GL_TEXTURE_2D, 1); float xy[] = { 0,0, - 0,500, - 500, 500, - 500, 0 + 0,TEXTURE_SIZE, + TEXTURE_SIZE, TEXTURE_SIZE, + TEXTURE_SIZE, 0 }; float uv[] = { 0, 0, @@ -174,7 +172,6 @@ namespace graphics glBindTexture(GL_TEXTURE_2D, 0); glDisableClientState(GL_VERTEX_ARRAY); glDisableClientState(GL_TEXTURE_COORD_ARRAY); - shader->unuse(); } //float Font::getCharWidth(int c, int last) { @@ -203,33 +200,23 @@ namespace graphics // return ceil(res); //} - /** - * 烘焙glyph到贴图上的特定位置,确保不会重叠 - */ Glyph* Font::bakeGlyph(unsigned int character) { Glyph* glyph = (Glyph*)malloc(sizeof(Glyph)); - int w, h; + int w, h, xoff, yoff; font->pushFontsize(fontsize); - const Channel* bitmap = font->getCodepointBitmapAlpha(character, &w, &h); - //const Color* bitmap32 = font->getCodepointBitmap(character, &w, &h); - font->popFontsize(); - GLuint texture = textures.back(); - glBindTexture(GL_TEXTURE_2D, texture); - glTexSubImage2D(GL_TEXTURE_2D, 0, xoffset, yoffset, w, h, GL_RED, GL_UNSIGNED_BYTE, bitmap); - glBindTexture(GL_TEXTURE_2D, 0); - //for (int y = 0; y < h; ++y) - //{ - // for (int x = 0; x < w; ++x) - // { - // putchar(" .:ioVM@"[bitmap[y*w + x] >> 5]); - // } - // putchar('\n'); - //} - free((void*)bitmap); + GLuint atlas = atlases.back(); + const Color* bitmap = font->getCodepointBitmap(character, &w, &h, &xoff, &yoff); + { + font->popFontsize(); + glBindTexture(GL_TEXTURE_2D, atlas); + glTexSubImage2D(GL_TEXTURE_2D, 0, xoffset + xoff, yoffset + (-yoff), w, h, GL_RGBA, GL_UNSIGNED_BYTE, bitmap); + glBindTexture(GL_TEXTURE_2D, 0); + delete[] bitmap; + } float s = TEXTURE_SIZE; - glyph->texture = texture; - glyph->box.set(xoffset / s, yoffset / s, w / s, h / s); + glyph->atlas = atlas; + glyph->box.set(xoffset / s, yoffset / s, (w -xoff)/ s, (h - yoff) / s); glyphs.insert(std::pair<unsigned int, Glyph*>(character, glyph)); xoffset += w; return glyph; diff --git a/libjin/Graphics/Font.h b/libjin/Graphics/Font.h index 4b0a850..c04d64a 100644 --- a/libjin/Graphics/Font.h +++ b/libjin/Graphics/Font.h @@ -53,7 +53,7 @@ namespace graphics struct Glyph { - GLuint texture; // texture where this glyph rendered + GLuint atlas; // texture where this glyph rendered GlyphBox box; // glyph box }; @@ -84,14 +84,14 @@ namespace graphics Font(FontData* font, unsigned int fontSize); ~Font(); - bool createTexture(); + bool createAtlas(); Glyph* bakeGlyph(unsigned int character); Glyph* findGlyph(unsigned int character); //float getCharWidth(int c, int last); //int getTextWidth(const char* text); - std::vector<GLuint> textures; + std::vector<GLuint> atlases; std::map<unsigned int, Glyph*> glyphs; // map glyph codepoint to Glyph FontData* font; const unsigned int fontsize; diff --git a/libjin/Graphics/FontData.cpp b/libjin/Graphics/FontData.cpp index 33d1875..513b5fa 100644 --- a/libjin/Graphics/FontData.cpp +++ b/libjin/Graphics/FontData.cpp @@ -73,17 +73,17 @@ namespace graphics scales.pop_back(); } - Channel* FontData::getCodepointBitmapAlpha(unsigned int codepoint, int* width, int* height) const + Channel* FontData::getCodepointBitmapAlpha(unsigned int codepoint, int* width, int* height, int* xoff, int* yoff) const { float scale = scales.back(); - Channel* bitmap = stbtt_GetCodepointBitmap(&info, scale, scale, codepoint, width, height, NULL, NULL); + Channel* bitmap = stbtt_GetCodepointBitmap(&info, scale, scale, codepoint, width, height, xoff, yoff); return bitmap; } - Color* FontData::getCodepointBitmap(unsigned int codepoint, int* width, int* height) const + Color* FontData::getCodepointBitmap(unsigned int codepoint, int* width, int* height, int* xoff, int* yoff) const { float scale = scales.back(); - Channel* bitmap = stbtt_GetCodepointBitmap(&info, scale, scale, codepoint, width, height, NULL, NULL); + Channel* bitmap = stbtt_GetCodepointBitmap(&info, scale, scale, codepoint, width, height, xoff, yoff); int w = *width, h = *height; Color* bitmap32 = new Color[w*h]; for (int y = 0; y < h; ++y) diff --git a/libjin/Graphics/FontData.h b/libjin/Graphics/FontData.h index 7d48adb..838f575 100644 --- a/libjin/Graphics/FontData.h +++ b/libjin/Graphics/FontData.h @@ -19,8 +19,8 @@ namespace graphics void pushFontsize(unsigned int fontsize); void popFontsize(); - Channel* getCodepointBitmapAlpha(unsigned int codepoint, int* width, int* height) const; - Color* getCodepointBitmap(unsigned int codepoint, int* width, int* height) const; + Channel* getCodepointBitmapAlpha(unsigned int codepoint, int* width, int* height, int* xoff, int* yoff) const; + Color* getCodepointBitmap(unsigned int codepoint, int* width, int* height, int* xoff, int* yoff) const; int getBaseline() const; unsigned char* getCodepointBox(unsigned int codepoint, int*x, int* y, int* x1, int*y1); diff --git a/libjin/Graphics/font.shader.h b/libjin/Graphics/font.shader.h index 04db2f0..27a9354 100644 --- a/libjin/Graphics/font.shader.h +++ b/libjin/Graphics/font.shader.h @@ -4,7 +4,6 @@ Color effect(Color col, Texture tex, vec2 uv, vec2 xy) { // r Color c = Texel(tex, uv); - float alpha = c.r; - return Vec4(col.rgb, alpha); + return Vec4(col.rgb, c.r); } )font_shader";
\ No newline at end of file diff --git a/test/05Font/main.cpp b/test/05Font/main.cpp index 58c13ac..1b0ae0a 100644 --- a/test/05Font/main.cpp +++ b/test/05Font/main.cpp @@ -9,14 +9,26 @@ using namespace jin::filesystem; Font* font = nullptr; Canvas* canvas; FontData* data = nullptr; +JSLProgram* shader = nullptr; +float dt; void onLoad() { + const char* program = R"( +uniform float dt; +Color effect(Color col, Texture tex, vec2 uv, vec2 screen) +{ + float d = 150; + float f = abs((screen.x - d * sin(dt))/20); + return vec4(col.rgb*(1-f), Texel(tex, uv).a); +} + )"; + shader = JSLProgram::createJSLProgram(program); Filesystem* fs = Filesystem::get(); - fs->mount("D:/Jin/libjin/min/build/Debug"); + fs->mount("../Debug"); Buffer buffer; - fs->read("font.ttf", &buffer); + fs->read("SIMYOU.TTF", &buffer); data = FontData::createFontData((const unsigned char*)buffer.data, buffer.size); - font = Font::createFont(data, 50); + font = Font::createFont(data, 14); //canvas = Canvas::createCanvas(100, 100); } @@ -29,7 +41,7 @@ void onEvent(jin::input::Event* e) void onUpdate(int ms) { - + dt += ms / (float)1000; } void onDraw() @@ -37,14 +49,17 @@ void onDraw() glColor4f(32 / 255.f, 32 / 255.f, 32 / 255.f, 1); rect(FILL, 0, 0, 500, 500); glColor4f(1, 1, 1, 1); + //shader->use(); + //shader->sendFloat("dt", dt); //Canvas::bind(canvas); if (font != nullptr) { - glColor4f(1, 1, 0, 1); + glColor4f(1, 1, 1, 1); //font->print(u8"ԴZΤ", 0, 0); - font->print(u8"Դ", 0, 0); + font->print(u8"hello, worldԴZ", 0, 0); glColor4f(1, 1, 1, 1); } + //shader->unuse(); //Canvas::unbind(); //canvas->draw(0, 0, 2, 2, 0); } |