diff options
author | chai <chaifix@163.com> | 2018-09-17 08:08:17 +0800 |
---|---|---|
committer | chai <chaifix@163.com> | 2018-09-17 08:08:17 +0800 |
commit | 8a2970103ed4facafee30a7653ee3b6cb6df902a (patch) | |
tree | 997cf9d7eef1830fb2087f2f5829c9177bbeb29d | |
parent | 2250248b97d31c15f3f4c0381bff8d52e88a8c9e (diff) |
*update
-rw-r--r-- | libjin/Common/Array.hpp | 2 | ||||
-rw-r--r-- | libjin/Graphics/Canvas.cpp | 14 | ||||
-rw-r--r-- | libjin/Graphics/Color.h | 8 | ||||
-rw-r--r-- | libjin/Graphics/Drawable.cpp | 16 | ||||
-rw-r--r-- | libjin/Graphics/Drawable.h | 4 | ||||
-rw-r--r-- | libjin/Graphics/Font.cpp | 204 | ||||
-rw-r--r-- | libjin/Graphics/Font.h | 90 | ||||
-rw-r--r-- | libjin/Graphics/FontData.cpp | 86 | ||||
-rw-r--r-- | libjin/Graphics/FontData.h | 44 | ||||
-rw-r--r-- | libjin/Graphics/Texture.cpp | 67 | ||||
-rw-r--r-- | libjin/Graphics/Texture.h | 2 |
11 files changed, 386 insertions, 151 deletions
diff --git a/libjin/Common/Array.hpp b/libjin/Common/Array.hpp index 7c0f058..45082db 100644 --- a/libjin/Common/Array.hpp +++ b/libjin/Common/Array.hpp @@ -28,7 +28,7 @@ namespace jin return data; } - T operator[](int index) + T& operator[](int index) { return data[index]; } diff --git a/libjin/Graphics/Canvas.cpp b/libjin/Graphics/Canvas.cpp index d5f7729..6164ffe 100644 --- a/libjin/Graphics/Canvas.cpp +++ b/libjin/Graphics/Canvas.cpp @@ -26,16 +26,6 @@ namespace graphics Canvas::Canvas(int w, int h) : Drawable(w, h) { - vertCoord[0] = 0; vertCoord[1] = 0; - vertCoord[2] = 0; vertCoord[3] = h; - vertCoord[4] = w; vertCoord[5] = h; - vertCoord[6] = w; vertCoord[7] = 0; - - textCoord[0] = 0; textCoord[1] = 1; - textCoord[2] = 0; textCoord[3] = 0; - textCoord[4] = 1; textCoord[5] = 0; - textCoord[6] = 1; textCoord[7] = 1; - GLint current_fbo; glGetIntegerv(GL_DRAW_FRAMEBUFFER_BINDING, ¤t_fbo); @@ -85,7 +75,7 @@ namespace graphics glMatrixMode(GL_PROJECTION); glPushMatrix(); glLoadIdentity(); - glOrtho(0, w, h, 0, -1, 1); + glOrtho(0, w, 0, h, -1, 1); /* set (model*view) matrix */ glMatrixMode(GL_MODELVIEW); @@ -112,12 +102,14 @@ namespace graphics glBindFramebuffer(GL_FRAMEBUFFER, DEFAULT_CANVAS->fbo); + /* set viewport on screen */ glViewport(0, 0, ww, wh); /* set projection matrix */ glMatrixMode(GL_PROJECTION); glPushMatrix(); glLoadIdentity(); + /* flip bottom and top */ glOrtho(0, ww, wh, 0, -1, 1); /* set (model*view) matrix */ diff --git a/libjin/Graphics/Color.h b/libjin/Graphics/Color.h index 97a87d6..3a9c54a 100644 --- a/libjin/Graphics/Color.h +++ b/libjin/Graphics/Color.h @@ -46,6 +46,14 @@ namespace graphics a = c.a; } + void set(unsigned char _r, unsigned char _g, unsigned char _b, unsigned char _a) + { + r = _r; + g = _g; + b = _b; + a = _a; + } + void operator = (const Color& c) { r = c.r; diff --git a/libjin/Graphics/Drawable.cpp b/libjin/Graphics/Drawable.cpp index ad87841..848880c 100644 --- a/libjin/Graphics/Drawable.cpp +++ b/libjin/Graphics/Drawable.cpp @@ -15,6 +15,15 @@ namespace graphics , size(w, h) , anchor(0, 0) { + vertex_coords[0] = 0; vertex_coords[1] = 0; + vertex_coords[2] = 0; vertex_coords[3] = h; + vertex_coords[4] = w; vertex_coords[5] = h; + vertex_coords[6] = w; vertex_coords[7] = 0; + + texture_coords[0] = 0; texture_coords[1] = 0; + texture_coords[2] = 0; texture_coords[3] = 1; + texture_coords[4] = 1; texture_coords[5] = 1; + texture_coords[6] = 1; texture_coords[7] = 0; } Drawable::~Drawable() @@ -30,9 +39,6 @@ namespace graphics void Drawable::draw(int x, int y, float sx, float sy, float r) { - /* Must set textCoord and vertCoord before renderring */ - if (! textCoord||! vertCoord) return; - static jin::math::Matrix t; t.setTransformation(x, y, r, sx, sy, anchor.x, anchor.y); @@ -44,8 +50,8 @@ namespace graphics glEnableClientState(GL_VERTEX_ARRAY); glEnableClientState(GL_TEXTURE_COORD_ARRAY); - glTexCoordPointer(2, GL_FLOAT, 0, textCoord); - glVertexPointer(2, GL_FLOAT, 0, vertCoord); + glVertexPointer(2, GL_FLOAT, 0, vertex_coords); + glTexCoordPointer(2, GL_FLOAT, 0, texture_coords); glDrawArrays(GL_QUADS, 0, 4); glDisableClientState(GL_VERTEX_ARRAY); glDisableClientState(GL_TEXTURE_COORD_ARRAY); diff --git a/libjin/Graphics/Drawable.h b/libjin/Graphics/Drawable.h index 3aee208..08614da 100644 --- a/libjin/Graphics/Drawable.h +++ b/libjin/Graphics/Drawable.h @@ -31,8 +31,8 @@ namespace graphics /* GLuint vbo; */ jin::math::Vector2<unsigned int> size; jin::math::Vector2<int> anchor; - float vertCoord[DRAWABLE_V_SIZE]; - float textCoord[DRAWABLE_V_SIZE]; + float vertex_coords[DRAWABLE_V_SIZE]; + float texture_coords[DRAWABLE_V_SIZE]; }; diff --git a/libjin/Graphics/Font.cpp b/libjin/Graphics/Font.cpp index 9b7d45b..e5caeac 100644 --- a/libjin/Graphics/Font.cpp +++ b/libjin/Graphics/Font.cpp @@ -3,9 +3,8 @@ #include "font.h" #include <stdio.h> -#define STB_TRUETYPE_IMPLEMENTATION -#include "../3rdparty/stb/stb_truetype.h" #include "color.h" +#include "../Common/Array.hpp" namespace jin { @@ -14,64 +13,66 @@ namespace graphics using namespace std; using namespace jin::math; - - const int Font::TEXTURE_WIDTHS[] = { 128, 256, 256, 512, 512, 1024, 1024 }; - const int Font::TEXTURE_HEIGHTS[] = { 128, 128, 256, 256, 512, 512, 1024 }; - /** - * 每个10开头的字节都只有6位有效,11开头的表明是code units的开头 - * 0000 0000-0000 007F | 0xxx xxxx 7 - * 0000 0080-0000 07FF | 110x xxxx 10xxxxxx 5 + 6 - * 0000 0800-0000 FFFF | 1110 xxxx 10xxxxxx 10xxxxxx 4 + 6*2 - * 0001 0000-0010 FFFF | 1111 0xxx 10xxxxxx 10xxxxxx 10xxxxxx 3 + 6*3 - */ - /* https://www.zhihu.com/question/23374078 */ - /* https://blog.csdn.net/carrie0728/article/details/17286043 */ - /* https://wenku.baidu.com/view/a6fddd07bed5b9f3f90f1ceb.html */ - /* https://stackoverflow.com/questions/27331819/whats-the-difference-between-a-character-a-code-point-a-glyph-and-a-grapheme */ - /* utf8 to unicode */ - static const char *ttf_utf8toCodepoint(const char *p, unsigned *res) { + //const int Font::TEXTURE_WIDTHS[] = { 128, 256, 256, 512, 512, 1024, 1024 }; + //const int Font::TEXTURE_HEIGHTS[] = { 128, 128, 256, 256, 512, 512, 1024 }; + + /* utf8 byte string to unicode codepoint */ + static const char *utf8toCodepoint(const char *p, unsigned *res) { unsigned x, mask, shift; - /* 处理code units的第一个字节 */ - switch (*p & 0xf0) { // 1111 0000 - case 0xf0 /*1111 0000*/ : mask = 0x07 /*0111*/; shift = 18; break; - case 0xe0 /*1110 0000*/: mask = 0x0f /*1111*/; shift = 12; break; - /* 110x */ - case 0xc0 /*1100 0000*/: - case 0xd0 /*1101 0000*/: mask = 0x1f /*0001 1111*/; shift = 6; break; + switch (*p & 0xf0) { + case 0xf0: mask = 0x07; shift = 18; break; + case 0xe0: mask = 0x0f; shift = 12; break; + case 0xc0: + case 0xd0: mask = 0x1f; shift = 6; break; default: - /* 0xxx */ *res = *p; return p + 1; } x = (*p & mask) << shift; do { - /* Return early if we reach an unexpected NULL */ if (*(++p) == '\0') { *res = x; return p; } shift -= 6; - x |= (*p & 0x3f) /*0011 1111*/<< shift; + x |= (*p & 0x3f) << shift; } while (shift); *res = x; return p + 1; } - /*static*/ Font* Font::createFont(const char* font, size_t size) + /*static*/ Font* Font::createFont(const FontData* fontData, unsigned int fontSzie) { - + Font* font; + try + { + font = new Font(fontData, fontSzie); + } + catch (...) + { + return nullptr; + } + return font; } - /*static*/ Font* Font::createFont(const char* file) + Font::Font(const FontData* f, unsigned int fontSize) + : xoffset(0) + , yoffset(0) + , font(f) + , fontsize(fontSize) { - + baseline = f->getBaseline(fontsize); + createTexture(); } - Font::Font() - : textureLevel(TEXTURE_SIZE_LEVEL_MAX) + Font::~Font() { - + map<unsigned int, Glyph*>::iterator it = glyphs.begin(); + for (; it != glyphs.end(); ++it) + { + delete it->second; + } } bool Font::createTexture() @@ -83,25 +84,12 @@ 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); - /*Initialize the texture, attempting smaller sizes if initialization fails.*/ - bool initialized = false; - while (textureLevel >= 0) - { - /*clear errors before initializing*/ - while (glGetError() != GL_NO_ERROR); - textureWidth = TEXTURE_WIDTHS[textureLevel]; - textureHeight = TEXTURE_HEIGHTS[textureLevel]; - glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, textureWidth, textureHeight, 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL); - initialized = (glGetError() == GL_NO_ERROR); - if (initialized || textureLevel <= 0) - break; - --textureLevel; - } - if (!initialized) + glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 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; + return false; } textures.push_back(t); glBindTexture(GL_TEXTURE_2D, 0); @@ -110,27 +98,127 @@ namespace graphics void Font::print(const char* text, int x, int y) { - int len = strlen(text); + const char* p = text; /* xy and uv list */ - vector<GlyphVertex> glyphvertices(len*4); + vector<GlyphVertex> glyphvertices; /* texture binded along with glyphvertices */ vector<GlyphArrayDrawInfo> glyphinfolist; - float dx = 0; - float dy = 0; - //float lineheihgt = ; + float dx = 50; + float dy = 50; + unsigned int c; + GLuint texture = 0; + int start = 0, count = 0; + do { + p = utf8toCodepoint(p, &c); + Glyph* glyph = findGlyph(c); + if (texture == 0) + texture = glyph->texture; + float xoff = glyph->box.xoff; + float yoff = glyph->box.yoff; + float width = glyph->box.width; + float height = glyph->box.height; + float mx = glyph->box.width * TEXTURE_SIZE; + float my = glyph->box.height * TEXTURE_SIZE; + glyphvertices.push_back(GlyphVertex(dx, dy, xoff, yoff)); + glyphvertices.push_back(GlyphVertex(dx, dy + my, xoff, yoff + height)); + 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) + { + GlyphArrayDrawInfo textureInfo; + textureInfo.texture = texture; + textureInfo.startvertex = start; + textureInfo.vertexcount = (*p == NULL) ? count + 4 : count; + glyphinfolist.push_back(textureInfo); + count = 0; + start = count; + } + count += 4; + } while (*p != NULL); + glEnableClientState(GL_VERTEX_ARRAY); + glEnableClientState(GL_TEXTURE_COORD_ARRAY); + for (int i = 0; i < glyphinfolist.size(); ++i) + { + GlyphArrayDrawInfo& info = glyphinfolist[i]; + glBindTexture(GL_TEXTURE_2D, info.texture); + int s = sizeof(GlyphVertex); + glVertexPointer(2, GL_FLOAT, s, &glyphvertices[info.startvertex].x); + glTexCoordPointer(2, GL_FLOAT, s, &glyphvertices[info.startvertex].u); + glDrawArrays(GL_QUADS, 0, info.vertexcount); + glBindTexture(GL_TEXTURE_2D, 0); + + } + glDisableClientState(GL_VERTEX_ARRAY); + glDisableClientState(GL_TEXTURE_COORD_ARRAY); } + //float Font::getCharWidth(int c, int last) { + // int res = 0; + // int width, lsb; + // stbtt_GetCodepointHMetrics(&fontData.font, c, &width, &lsb); + // res = width; + // if (last) { + // int kerning = stbtt_GetCodepointKernAdvance(&fontData.font, last, c); + // res += kerning; + // } + // return res * fontData.scale; + //} + + //int Font::getTextWidth(const char* text) + //{ + // float res = 0; + // int last = 0; + // const char *p = text; + // while (*p) { + // unsigned c; + // p = utf8toCodepoint(p, &c); + // res += getCharWidth(c, last); + // last = c; + // } + // return ceil(res); + //} + /** * 根据unicode渲染字体文件到texture上,并更新glyphs */ Glyph* Font::addGlyph(unsigned int character) { - + Glyph* glyph = (Glyph*)malloc(sizeof(Glyph)); + int w, h; + const Color* bitmap = font->getCodepointBitmap(character, fontsize, &w, &h); + GLuint texture = textures.back(); + glBindTexture(GL_TEXTURE_2D, texture); + glTexSubImage2D(GL_TEXTURE_2D, 0, xoffset, yoffset, w, h, GL_RGBA, 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].a >> 5]); + } + putchar('\n'); + } + float s = TEXTURE_SIZE; + glyph->texture = texture; + glyph->box.set(xoffset / s, yoffset / s, w / s, h / s); + glyphs.insert(std::pair<unsigned int, Glyph*>(character, glyph)); + xoffset += w; + return glyph; } Glyph* Font::findGlyph(unsigned int character) { - + map<unsigned int, Glyph*>::iterator it = glyphs.find(character); + if (it != glyphs.end()) + { + return it->second; + } + else + { + Glyph* glyph = addGlyph(character); + return glyph; + } } } // graphics diff --git a/libjin/Graphics/Font.h b/libjin/Graphics/Font.h index fa54337..b71c2cc 100644 --- a/libjin/Graphics/Font.h +++ b/libjin/Graphics/Font.h @@ -6,28 +6,31 @@ #include <vector> #include <map> #include "drawable.h" -#include "../3rdparty/stb/stb_truetype.h" +#include "FontData.h" #include "../math/quad.h" namespace jin { namespace graphics { - /** - * original from love2d font and graphics modules - * the basic idea is storing glyphs in several mipmap - * http://www.ruanyifeng.com/blog/2007/10/ascii_unicode_and_utf-8.html - */ - + struct GlyphVertex { - float x, y; // screen coordinates - float u, v; // texture coordinates + float x, y; + float u, v; + GlyphVertex(float _x, float _y, float _u, float _v) + { + set(_x, _y, _u, _v); + } + void set(float _x, float _y, float _u, float _v) + { + x = _x; + y = _y; + u = _u; + v = _v; + } }; - /* track when to change texutre binded in render array */ - /* casue switch texture is expensive */ - /* std::vector<GlyphVertex> list */ struct GlyphArrayDrawInfo { GLuint texture; @@ -35,45 +38,64 @@ namespace graphics int vertexcount; }; - /* glyph texture */ + struct GlyphBox + { + float xoff, yoff; + float width, height; + void set(float x, float y, float w, float h) + { + xoff = x; + yoff = y; + width = w; + height = h; + } + }; + struct Glyph { - GLuint texture; // texture where this glyph rendered - int spacing; // spacing of glyph - GlyphVertex vertices[4]; // quad of glyph render region + GLuint texture; // texture where this glyph rendered + GlyphBox box; // glyph box }; class Font { - public: - static Font* createFont(const char* file); - static Font* createFont(const char* data, size_t size); + private: + static const int DEFAULT_FONT_SIZE = 12; + + public: + static Font* createFont(const FontData* fontData, unsigned int fontSzie = DEFAULT_FONT_SIZE); void print(const char* text, int x, int y); 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]; - static const int TEXTURE_HEIGHTS[TEXTURE_SIZE_LEVELS_COUNT]; - static const int SPACES_PER_TAB = 4; + //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]; + //static const int TEXTURE_HEIGHTS[TEXTURE_SIZE_LEVELS_COUNT]; + //static const int SPACES_PER_TAB = 4; + static const int TEXTURE_SIZE = 512; + + Font(const FontData* font, unsigned int fontSize); + ~Font(); - /* create a new mipmap to render glyph and push it on textures */ bool createTexture(); - /* create a glyph for a unicode and return it */ Glyph* addGlyph(unsigned int character); - /* find glyph by unicode */ Glyph* findGlyph(unsigned int character); - /* list of textures where glyphs rendered, always operate the last one */ - /* map character to its render area */ + //float getCharWidth(int c, int last); + //int getTextWidth(const char* text); + std::vector<GLuint> textures; - std::map<unsigned int, Glyph*> glyphs; - /* mipmap size level */ - int textureLevel; - int textureWidth; - int textureHeight; + std::map<unsigned int, Glyph*> glyphs; // map glyph codepoint to Glyph + const FontData* font; + const unsigned int fontsize; + unsigned int baseline; + + /* cursor helped render to texture */ + float xoffset; + float yoffset; }; diff --git a/libjin/Graphics/FontData.cpp b/libjin/Graphics/FontData.cpp new file mode 100644 index 0000000..68aa417 --- /dev/null +++ b/libjin/Graphics/FontData.cpp @@ -0,0 +1,86 @@ +#include "FontData.h" +#define STB_TRUETYPE_IMPLEMENTATION +#include "../3rdparty/stb/stb_truetype.h" +#include <stdio.h> + +namespace jin +{ +namespace graphics +{ + + FontData* FontData::createFontData(const unsigned char* data, unsigned int size) + { + FontData* font = nullptr; + try + { + font = new FontData(data, size); + return font; + } + catch (...) + { + return nullptr; + } + } + + FontData::FontData(const unsigned char* d, unsigned int s) + { + raw.size = s; + raw.data = (unsigned char*)malloc(s); + memcpy(raw.data, d, s); + if (!stbtt_InitFont(&info, (const unsigned char*)raw.data, 0)) + { + delete raw.data; + throw 0; + } + } + + FontData::~FontData() + { + free(raw.data); + } + + int FontData::getBaseline(unsigned int px) const + { + float scale = stbtt_ScaleForPixelHeight(&info, px); + int ascent; + stbtt_GetFontVMetrics(&info, &ascent, 0, 0); + int baseline = (int)(ascent*scale); + return baseline; + } + + unsigned char* FontData::getCodepointBitmapAlpha(unsigned int codepoint, int px, int* width, int* height) const + { + float scale = stbtt_ScaleForPixelHeight(&info, px); + unsigned char* bitmap = stbtt_GetCodepointBitmap(&info, scale, scale, codepoint, width, height, NULL, NULL); + return bitmap; + } + + unsigned char* FontData::getCodepointBox(unsigned int codepoint, int px, int*x, int* y, int* x1, int*y1) + { + + } + + unsigned char* FontData::getCodepointBitmapBox(unsigned int codepoint, int px, int*x, int* y, int* x1, int*y1) + { + + } + + Color* FontData::getCodepointBitmap(unsigned int codepoint, int px, int* width, int* height) const + { + float scale = stbtt_ScaleForPixelHeight(&info, px); + unsigned char* bitmap = stbtt_GetCodepointBitmap(&info, scale, scale, codepoint, width, height, NULL, NULL); + int w = *width, h = *height; + Color* bitmap32 = new Color[w*h]; + for (int y = 0; y < h; ++y) + { + for (int x = 0; x < w; ++x) + { + bitmap32[x + y * w].set(0xff, 0xff, 0xff, bitmap[x + y * w]); + } + } + free(bitmap); + return bitmap32; + } + +} +}
\ No newline at end of file diff --git a/libjin/Graphics/FontData.h b/libjin/Graphics/FontData.h new file mode 100644 index 0000000..1bfaceb --- /dev/null +++ b/libjin/Graphics/FontData.h @@ -0,0 +1,44 @@ +#ifndef __LIBJIN_FONTDATA_H +#define __LIBJIN_FONTDATA_H +#include "../3rdparty/stb/stb_truetype.h" +#include "Color.h" + +namespace jin +{ +namespace graphics +{ + + class FontData + { + public: + static FontData* createFontData(const unsigned char* data, unsigned int size); + + ~FontData(); + + void pushFontsize(int f) { fontsize = f; scale = 0; }; + void popFontsize() { fontsize = 0; scale = 0; }; + + unsigned char* getCodepointBitmapAlpha(unsigned int codepoint, int px, int* width, int* height) const; + Color* getCodepointBitmap(unsigned int codepoint, int px, int* width, int* height) const; + int getBaseline(unsigned int px) const; + + unsigned char* getCodepointBox(unsigned int codepoint, int px, int*x, int* y, int* x1, int*y1); + unsigned char* getCodepointBitmapBox(unsigned int codepoint, int px, int*x, int* y, int* x1, int*y1); + + private: + FontData(const unsigned char* data, unsigned int size); + + stbtt_fontinfo info; + struct + { + unsigned char* data; + unsigned int size; + } raw; + int fontsize; + float scale; + }; + +} +} + +#endif
\ No newline at end of file diff --git a/libjin/Graphics/Texture.cpp b/libjin/Graphics/Texture.cpp index 45aedfb..dbc7b70 100644 --- a/libjin/Graphics/Texture.cpp +++ b/libjin/Graphics/Texture.cpp @@ -8,49 +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(); - const Color* pixels = bitmap->getPixels(); - tex->size.w = bitmap->getWidth(); - tex->size.h = bitmap->getHeight(); - unsigned int w = tex->size.w; - unsigned int h = tex->size.h; - - glGenTextures(1, &tex->texture); - glBindTexture(GL_TEXTURE_2D, tex->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_RGBA8, w, h, 0, GL_RGBA, GL_UNSIGNED_BYTE, pixels); - - tex->vertCoord[0] = 0; tex->vertCoord[1] = 1; - tex->vertCoord[2] = 0; tex->vertCoord[3] = h; - tex->vertCoord[4] = w; tex->vertCoord[5] = h; - tex->vertCoord[6] = w; tex->vertCoord[7] = 1; - - tex->textCoord[0] = 0; tex->textCoord[1] = 0; - tex->textCoord[2] = 0; tex->textCoord[3] = 1; - tex->textCoord[4] = 1; tex->textCoord[5] = 1; - tex->textCoord[6] = 1; tex->textCoord[7] = 0; - - return tex; - } - - Texture::Texture() - : Drawable() - { - } + /*static*/ Texture* Texture::createTexture(Bitmap* bitmap) + { + Texture* tex = new Texture(bitmap); + return tex; + } - Texture::~Texture() - { - } + 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); + + glBindTexture(GL_TEXTURE_2D, 0); + } + + Texture::~Texture() + { + } -} // graphics + } // graphics } // jin #endif // LIBJIN_MODULES_RENDER
\ No newline at end of file diff --git a/libjin/Graphics/Texture.h b/libjin/Graphics/Texture.h index d088928..8498666 100644 --- a/libjin/Graphics/Texture.h +++ b/libjin/Graphics/Texture.h @@ -20,7 +20,7 @@ namespace graphics ~Texture(); private: - Texture(); + Texture(const Bitmap* bitmap); }; |