Try to use float for dest_ret

This commit is contained in:
Benau
2016-07-19 12:25:16 +08:00
parent 89f06ed8d3
commit b2ee606e7b
6 changed files with 294 additions and 65 deletions

View File

@@ -275,12 +275,15 @@ void FontWithFace::dumpGlyphPage(const std::string& name)
{
for (unsigned int i = 0; i < m_spritebank->getTextureCount(); i++)
{
video::IImage* image = irr_driver->getVideoDriver()
->createImageFromData (m_spritebank->getTexture(i)
->getColorFormat(), m_spritebank->getTexture(i)->getSize(),
m_spritebank->getTexture(i)->lock(), false/*copy mem*/);
video::ITexture* tex = m_spritebank->getTexture(i);
core::dimension2d<u32> size = tex->getSize();
video::ECOLOR_FORMAT col_format = tex->getColorFormat();
void* data = tex->lock();
m_spritebank->getTexture(i)->unlock();
video::IImage* image = irr_driver->getVideoDriver()
->createImageFromData(col_format, size, data, false/*copy mem*/);
tex->unlock();
irr_driver->getVideoDriver()->writeImageToFile(image, std::string
(name + "_" + StringUtils::toString(i) + ".png").c_str());
image->drop();
@@ -353,8 +356,8 @@ core::dimension2d<u32> FontWithFace::getDimension(const wchar_t* text,
updateCharactersList();
assert(m_character_area_map.size() > 0);
core::dimension2d<u32> dim(0, 0);
core::dimension2d<u32> this_line(0, (int)(m_font_max_height * scale));
core::dimension2d<float> dim(0.0f, 0.0f);
core::dimension2d<float> this_line(0.0f, m_font_max_height * scale);
for (const wchar_t* p = text; *p; ++p)
{
@@ -380,10 +383,11 @@ core::dimension2d<u32> FontWithFace::getDimension(const wchar_t* text,
if (dim.Width < this_line.Width)
dim.Width = this_line.Width;
dim.Width = (int)(dim.Width + 0.9f); // round up
dim.Height = (int)(dim.Height + 0.9f);
core::dimension2d<u32> ret_dim(0, 0);
ret_dim.Width = (u32)(dim.Width + 0.9f); // round up
ret_dim.Height = (u32)(dim.Height + 0.9f);
return dim;
return ret_dim;
} // getDimension
// ----------------------------------------------------------------------------
@@ -391,17 +395,18 @@ int FontWithFace::getCharacterFromPos(const wchar_t* text, int pixel_x,
FontSettings* font_settings) const
{
const float scale = font_settings ? font_settings->getScale() : 1.0f;
int x = 0;
float x = 0;
int idx = 0;
while (text[idx])
{
bool use_fallback_font = false;
const FontArea &a = getAreaFromCharacter(text[idx], &use_fallback_font);
const FontArea &a = getAreaFromCharacter(text[idx],
&use_fallback_font);
x += getCharWidth(a, use_fallback_font, scale);
if (x >= pixel_x)
if (x >= float(pixel_x))
return idx;
++idx;
@@ -441,7 +446,8 @@ void FontWithFace::render(const core::stringw& text,
font_settings->setShadow(true);
}
core::position2d<s32> offset = position.UpperLeftCorner;
core::position2d<float> offset(float(position.UpperLeftCorner.X),
float(position.UpperLeftCorner.Y));
core::dimension2d<s32> text_dimension;
if (rtl || hcenter || vcenter || clip)
@@ -457,7 +463,8 @@ void FontWithFace::render(const core::stringw& text,
offset.Y += (position.getHeight() - text_dimension.Height) / 2;
if (clip)
{
core::rect<s32> clippedRect(offset, text_dimension);
core::rect<s32> clippedRect(core::position2d<s32>
(s32(offset.X), s32(offset.Y)), text_dimension);
clippedRect.clipAgainst(*clip);
if (!clippedRect.isValid()) return;
}
@@ -465,9 +472,9 @@ void FontWithFace::render(const core::stringw& text,
// Collect character locations
const unsigned int text_size = text.size();
core::array<s32> indices(text_size);
core::array<core::position2di> offsets(text_size);
std::vector<bool> fallback(text_size);
core::array<s32> indices(text_size);
core::array<core::position2d<float>> offsets(text_size);
std::vector<bool> fallback(text_size);
// Test again if lazy load char is needed,
// as some text isn't drawn with getDimension
@@ -483,7 +490,7 @@ void FontWithFace::render(const core::stringw& text,
{
if (c==L'\r' && text[i+1]==L'\n')
c = text[++i];
offset.Y += (int)(m_font_max_height * scale);
offset.Y += m_font_max_height * scale;
offset.X = position.UpperLeftCorner.X;
if (hcenter)
offset.X += (position.getWidth() - text_dimension.Width) >> 1;
@@ -495,12 +502,10 @@ void FontWithFace::render(const core::stringw& text,
fallback[i] = use_fallback_font;
if (char_collector == NULL)
{
// Try to use ceil to make offset calculate correctly when scale
// is smaller than 1
s32 glyph_offset_x = (s32)ceil((float) area.bearing_x *
(fallback[i] ? m_fallback_font_scale : scale));
s32 glyph_offset_y = (s32)ceil((float) area.offset_y *
(fallback[i] ? m_fallback_font_scale : scale));
float glyph_offset_x = area.bearing_x *
(fallback[i] ? m_fallback_font_scale : scale);
float glyph_offset_y = area.offset_y *
(fallback[i] ? m_fallback_font_scale : scale);
offset.X += glyph_offset_x;
offset.Y += glyph_offset_y;
offsets.push_back(offset);
@@ -532,10 +537,10 @@ void FontWithFace::render(const core::stringw& text,
}
// Billboard text specific, use offset_y_bt instead
s32 glyph_offset_x = (s32)ceil((float) area.bearing_x *
(fallback[i] ? m_fallback_font_scale : scale));
s32 glyph_offset_y = (s32)ceil((float) area.offset_y_bt *
(fallback[i] ? m_fallback_font_scale : scale));
float glyph_offset_x = area.bearing_x *
(fallback[i] ? m_fallback_font_scale : scale);
float glyph_offset_y = area.offset_y_bt *
(fallback[i] ? m_fallback_font_scale : scale);
offset.X += glyph_offset_x;
offset.Y += glyph_offset_y;
offsets.push_back(offset);
@@ -586,13 +591,13 @@ void FontWithFace::render(const core::stringw& text,
[(*fallback_sprites)[sprite_id].Frames[0].rectNumber] :
positions[sprites[sprite_id].Frames[0].rectNumber]);
core::dimension2d<s32> size = source.getSize();
core::dimension2d<float> size(0.0f, 0.0f);
float cur_scale = (fallback[n] ? m_fallback_font_scale : scale);
size.Width = (int)(size.Width * cur_scale);
size.Height = (int)(size.Height * cur_scale);
size.Width = source.getSize().Width * cur_scale;
size.Height = source.getSize().Height * cur_scale;
core::rect<s32> dest(offsets[n], size);
core::rect<float> dest(offsets[n], size);
video::ITexture* texture = (fallback[n] ?
m_fallback_font->m_spritebank->getTexture(tex_id) :
@@ -603,8 +608,9 @@ void FontWithFace::render(const core::stringw& text,
for (int y_delta = -2; y_delta <= 2; y_delta++)
{
if (x_delta == 0 || y_delta == 0) continue;
draw2DImage(texture, dest + core::position2d<s32>
(x_delta, y_delta), source, clip, black, true);
draw2DImage(texture, dest + core::position2d<float>
(float(x_delta), float(y_delta)), source, clip,
black, true);
}
}
}
@@ -625,13 +631,13 @@ void FontWithFace::render(const core::stringw& text,
(*fallback_positions)[(*fallback_sprites)[sprite_id].Frames[0]
.rectNumber] : positions[sprites[sprite_id].Frames[0].rectNumber]);
core::dimension2d<s32> size = source.getSize();
core::dimension2d<float> size(0.0f, 0.0f);
float cur_scale = (fallback[n] ? m_fallback_font_scale : scale);
size.Width = (int)(size.Width * cur_scale);
size.Height = (int)(size.Height * cur_scale);
size.Width = source.getSize().Width * cur_scale;
size.Height = source.getSize().Height * cur_scale;
core::rect<s32> dest(offsets[n], size);
core::rect<float> dest(offsets[n], size);
video::ITexture* texture = (fallback[n] ?
m_fallback_font->m_spritebank->getTexture(tex_id) :

View File

@@ -36,7 +36,7 @@ public:
{
public:
virtual void collectChar(video::ITexture* texture,
const core::rect<s32>& destRect,
const core::rect<float>& destRect,
const core::rect<s32>& sourceRect,
const video::SColor* const colors) = 0;
};
@@ -125,12 +125,12 @@ private:
std::map<wchar_t, GlyphInfo> m_character_glyph_info_map;
// ------------------------------------------------------------------------
int getCharWidth(const FontArea& area, bool fallback, float scale) const
float getCharWidth(const FontArea& area, bool fallback, float scale) const
{
if (fallback)
return (int)(area.advance_x * m_fallback_font_scale * scale);
return area.advance_x * m_fallback_font_scale;
else
return (int)(area.advance_x * scale);
return area.advance_x * scale;
}
// ------------------------------------------------------------------------
bool loadedChar(wchar_t c) const

View File

@@ -274,7 +274,50 @@ static void getSize(unsigned texture_width, unsigned texture_height,
} // getSize
// ----------------------------------------------------------------------------
void draw2DImage(const video::ITexture* texture,
static void getSize(unsigned texture_width, unsigned texture_height,
bool textureisRTT, const core::rect<float>& destRect,
const core::rect<s32>& sourceRect,
float &width, float &height,
float &center_pos_x, float &center_pos_y,
float &tex_width, float &tex_height,
float &tex_center_pos_x, float &tex_center_pos_y )
{
core::dimension2d<u32> frame_size = irr_driver->getActualScreenSize();
const int screen_w = frame_size.Width;
const int screen_h = frame_size.Height;
center_pos_x = destRect.UpperLeftCorner.X + destRect.LowerRightCorner.X;
center_pos_x /= screen_w;
center_pos_x -= 1.;
center_pos_y = destRect.UpperLeftCorner.Y + destRect.LowerRightCorner.Y;
center_pos_y /= screen_h;
center_pos_y = float(1.f - center_pos_y);
width = destRect.LowerRightCorner.X - destRect.UpperLeftCorner.X;
width /= screen_w;
height = destRect.LowerRightCorner.Y - destRect.UpperLeftCorner.Y;
height /= screen_h;
tex_center_pos_x = float(sourceRect.UpperLeftCorner.X + sourceRect.LowerRightCorner.X);
tex_center_pos_x /= texture_width * 2.f;
tex_center_pos_y = float(sourceRect.UpperLeftCorner.Y + sourceRect.LowerRightCorner.Y);
tex_center_pos_y /= texture_height * 2.f;
tex_width = float(sourceRect.LowerRightCorner.X - sourceRect.UpperLeftCorner.X);
tex_width /= texture_width * 2.f;
tex_height = float(sourceRect.LowerRightCorner.Y - sourceRect.UpperLeftCorner.Y);
tex_height /= texture_height * 2.f;
if (textureisRTT)
tex_height = -tex_height;
const f32 invW = 1.f / static_cast<f32>(texture_width);
const f32 invH = 1.f / static_cast<f32>(texture_height);
const core::rect<f32> tcoords(sourceRect.UpperLeftCorner.X * invW,
sourceRect.UpperLeftCorner.Y * invH,
sourceRect.LowerRightCorner.X * invW,
sourceRect.LowerRightCorner.Y * invH);
} // getSize
// ----------------------------------------------------------------------------
void draw2DImage(const video::ITexture* texture,
const core::rect<s32>& destRect,
const core::rect<s32>& sourceRect,
const core::rect<s32>* clip_rect,
@@ -291,7 +334,90 @@ void draw2DImage(const video::ITexture* texture,
float width, height, center_pos_x, center_pos_y;
float tex_width, tex_height, tex_center_pos_x, tex_center_pos_y;
getSize(texture->getSize().Width, texture->getSize().Height,
getSize(texture->getSize().Width, texture->getSize().Height,
texture->isRenderTarget(), destRect, sourceRect, width, height,
center_pos_x, center_pos_y, tex_width, tex_height,
tex_center_pos_x, tex_center_pos_y);
if (use_alpha_channel_of_texture)
{
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
}
else
{
glDisable(GL_BLEND);
}
if (clip_rect)
{
if (!clip_rect->isValid())
return;
glEnable(GL_SCISSOR_TEST);
const core::dimension2d<u32>& render_target_size =
irr_driver->getActualScreenSize();
glScissor(clip_rect->UpperLeftCorner.X,
render_target_size.Height - clip_rect->LowerRightCorner.Y,
clip_rect->getWidth(), clip_rect->getHeight());
}
UniformColoredTextureRectShader::getInstance()->use();
glBindVertexArray(SharedGPUObjects::getUI_VAO());
#if !defined(USE_GLES2)
const video::COpenGLTexture *c_texture =
static_cast<const video::COpenGLTexture*>(texture);
#else
const video::COGLES2Texture *c_texture =
static_cast<const video::COGLES2Texture*>(texture);
#endif
UniformColoredTextureRectShader::getInstance()
->setTextureUnits(c_texture->getOpenGLTextureName());
UniformColoredTextureRectShader::getInstance()
->setUniforms(core::vector2df(center_pos_x, center_pos_y),
core::vector2df(width, height),
core::vector2df(tex_center_pos_x, tex_center_pos_y),
core::vector2df(tex_width, tex_height),
colors);
glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
glBindVertexArray(0);
glBindBuffer(GL_ARRAY_BUFFER, 0);
if (clip_rect)
glDisable(GL_SCISSOR_TEST);
glUseProgram(0);
glGetError();
} // draw2DImage
// ----------------------------------------------------------------------------
void draw2DImage(const video::ITexture* texture,
const core::rect<float>& destRect,
const core::rect<s32>& sourceRect,
const core::rect<s32>* clip_rect,
const video::SColor &colors,
bool use_alpha_channel_of_texture)
{
if (!CVS->isGLSL())
{
core::rect<irr::s32> dest_rect
(irr::s32(destRect.UpperLeftCorner.X),
irr::s32(destRect.UpperLeftCorner.Y),
irr::s32(destRect.LowerRightCorner.X),
irr::s32(destRect.LowerRightCorner.Y));
video::SColor duplicatedArray[4] = { colors, colors, colors, colors };
draw2DImage(texture, dest_rect, sourceRect, clip_rect, duplicatedArray,
use_alpha_channel_of_texture);
return;
}
float width, height, center_pos_x, center_pos_y;
float tex_width, tex_height, tex_center_pos_x, tex_center_pos_y;
getSize(texture->getSize().Width, texture->getSize().Height,
texture->isRenderTarget(), destRect, sourceRect, width, height,
center_pos_x, center_pos_y, tex_width, tex_height,
tex_center_pos_x, tex_center_pos_y);
@@ -314,7 +440,7 @@ void draw2DImage(const video::ITexture* texture,
glEnable(GL_SCISSOR_TEST);
const core::dimension2d<u32>& render_target_size =
irr_driver->getActualScreenSize();
glScissor(clip_rect->UpperLeftCorner.X,
glScissor(clip_rect->UpperLeftCorner.X,
render_target_size.Height - clip_rect->LowerRightCorner.Y,
clip_rect->getWidth(), clip_rect->getHeight());
}
@@ -323,10 +449,10 @@ void draw2DImage(const video::ITexture* texture,
glBindVertexArray(SharedGPUObjects::getUI_VAO());
#if !defined(USE_GLES2)
const video::COpenGLTexture *c_texture =
const video::COpenGLTexture *c_texture =
static_cast<const video::COpenGLTexture*>(texture);
#else
const video::COGLES2Texture *c_texture =
const video::COGLES2Texture *c_texture =
static_cast<const video::COGLES2Texture*>(texture);
#endif
UniformColoredTextureRectShader::getInstance()
@@ -387,7 +513,7 @@ void draw2DImageFromRTT(GLuint texture, size_t texture_w, size_t texture_h,
} // draw2DImageFromRTT
// ----------------------------------------------------------------------------
void draw2DImage(const video::ITexture* texture,
void draw2DImage(const video::ITexture* texture,
const core::rect<s32>& destRect,
const core::rect<s32>& sourceRect,
const core::rect<s32>* clip_rect,
@@ -463,6 +589,89 @@ void draw2DImage(const video::ITexture* texture,
glGetError();
} // draw2DImage
// ----------------------------------------------------------------------------
void draw2DImage(const video::ITexture* texture,
const core::rect<float>& destRect,
const core::rect<s32>& sourceRect,
const core::rect<s32>* clip_rect,
const video::SColor* const colors,
bool use_alpha_channel_of_texture,
bool draw_translucently)
{
if (!CVS->isGLSL())
{
core::rect<irr::s32> dest_rect
(irr::s32(destRect.UpperLeftCorner.X),
irr::s32(destRect.UpperLeftCorner.Y),
irr::s32(destRect.LowerRightCorner.X),
irr::s32(destRect.LowerRightCorner.Y));
irr_driver->getVideoDriver()->draw2DImage(texture, dest_rect, sourceRect,
clip_rect, colors,
use_alpha_channel_of_texture);
return;
}
float width, height, center_pos_x, center_pos_y, tex_width, tex_height;
float tex_center_pos_x, tex_center_pos_y;
getSize(texture->getSize().Width, texture->getSize().Height,
texture->isRenderTarget(), destRect, sourceRect, width, height,
center_pos_x, center_pos_y, tex_width, tex_height,
tex_center_pos_x, tex_center_pos_y);
if (draw_translucently)
{
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE);
}
else if (use_alpha_channel_of_texture)
{
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
}
else
{
glDisable(GL_BLEND);
}
if (clip_rect)
{
if (!clip_rect->isValid())
return;
glEnable(GL_SCISSOR_TEST);
const core::dimension2d<u32>& render_target_size =
irr_driver->getActualScreenSize();
glScissor(clip_rect->UpperLeftCorner.X,
render_target_size.Height - clip_rect->LowerRightCorner.Y,
clip_rect->getWidth(), clip_rect->getHeight());
}
if (colors)
{
drawTexColoredQuad(texture, colors, width, height, center_pos_x,
center_pos_y, tex_center_pos_x, tex_center_pos_y,
tex_width, tex_height);
}
else
{
#if !defined(USE_GLES2)
const video::COpenGLTexture *c_texture =
static_cast<const video::COpenGLTexture*>(texture);
#else
const video::COGLES2Texture *c_texture =
static_cast<const video::COGLES2Texture*>(texture);
#endif
drawTexQuad(c_texture->getOpenGLTextureName(), width, height,
center_pos_x, center_pos_y, tex_center_pos_x,
tex_center_pos_y, tex_width, tex_height);
}
if (clip_rect)
glDisable(GL_SCISSOR_TEST);
glUseProgram(0);
glGetError();
} // draw2DImage
// ----------------------------------------------------------------------------
void draw2DVertexPrimitiveList(video::ITexture *tex, const void* vertices,
u32 vertexCount, const void* indexList,

View File

@@ -42,6 +42,13 @@ void draw2DImage(const irr::video::ITexture* texture,
const irr::video::SColor &color,
bool useAlphaChannelOfTexture);
void draw2DImage(const irr::video::ITexture* texture,
const irr::core::rect<float>& destRect,
const irr::core::rect<irr::s32>& sourceRect,
const irr::core::rect<irr::s32>* clipRect,
const irr::video::SColor &color,
bool useAlphaChannelOfTexture);
void draw2DImage(const irr::video::ITexture* texture,
const irr::core::rect<irr::s32>& destRect,
const irr::core::rect<irr::s32>& sourceRect,
@@ -49,6 +56,13 @@ void draw2DImage(const irr::video::ITexture* texture,
const irr::video::SColor* const colors,
bool useAlphaChannelOfTexture, bool draw_translucently = false);
void draw2DImage(const irr::video::ITexture* texture,
const irr::core::rect<float>& destRect,
const irr::core::rect<irr::s32>& sourceRect,
const irr::core::rect<irr::s32>* clipRect,
const irr::video::SColor* const colors,
bool useAlphaChannelOfTexture, bool draw_translucently = false);
void draw2DVertexPrimitiveList(irr::video::ITexture *t, const void* vertices,
irr::u32 vertexCount, const void* indexList,
irr::u32 primitiveCount,

View File

@@ -71,17 +71,17 @@ scene::IMesh* STKTextBillboard::getTextMesh(core::stringw text, FontWithFace* fo
//scene::SMesh* mesh = new scene::SMesh();
std::map<video::ITexture*, scene::SMeshBuffer*> buffers;
int max_x = 0;
int min_y = 0;
int max_y = 0;
float max_x = 0;
float min_y = 0;
float max_y = 0;
for (unsigned int i = 0; i < m_chars.size(); i++)
{
int char_x = m_chars[i].m_destRect.LowerRightCorner.X;
float char_x = m_chars[i].m_destRect.LowerRightCorner.X;
if (char_x > max_x)
max_x = char_x;
int char_min_y = m_chars[i].m_destRect.UpperLeftCorner.Y;
int char_max_y = m_chars[i].m_destRect.LowerRightCorner.Y;
float char_min_y = m_chars[i].m_destRect.UpperLeftCorner.Y;
float char_max_y = m_chars[i].m_destRect.LowerRightCorner.Y;
if (char_min_y < min_y)
min_y = char_min_y;
if (char_max_y > min_y)
@@ -92,16 +92,16 @@ scene::IMesh* STKTextBillboard::getTextMesh(core::stringw text, FontWithFace* fo
for (unsigned int i = 0; i < m_chars.size(); i++)
{
core::vector3df char_pos((float) m_chars[i].m_destRect.UpperLeftCorner.X,
(float) m_chars[i].m_destRect.UpperLeftCorner.Y, 0);
core::vector3df char_pos(m_chars[i].m_destRect.UpperLeftCorner.X,
m_chars[i].m_destRect.UpperLeftCorner.Y, 0);
char_pos *= scale;
core::vector3df char_pos2((float)m_chars[i].m_destRect.LowerRightCorner.X,
(float) m_chars[i].m_destRect.LowerRightCorner.Y, 0);
core::vector3df char_pos2(m_chars[i].m_destRect.LowerRightCorner.X,
m_chars[i].m_destRect.LowerRightCorner.Y, 0);
char_pos2 *= scale;
core::dimension2di char_size_i = m_chars[i].m_destRect.getSize();
core::dimension2df char_size(char_size_i.Width*scale, char_size_i.Height*scale);
//core::dimension2di char_size_i = m_chars[i].m_destRect.getSize();
//core::dimension2df char_size(char_size_i.Width*scale, char_size_i.Height*scale);
std::map<video::ITexture*, scene::SMeshBuffer*>::iterator map_itr = buffers.find(m_chars[i].m_texture);
scene::SMeshBuffer* buffer;
@@ -184,7 +184,7 @@ void STKTextBillboard::updateNoGL()
}
void STKTextBillboard::collectChar(video::ITexture* texture,
const core::rect<s32>& destRect,
const core::rect<float>& destRect,
const core::rect<s32>& sourceRect,
const video::SColor* const colors)
{

View File

@@ -31,12 +31,12 @@ class STKTextBillboardChar
{
public:
irr::video::ITexture* m_texture;
irr::core::rect<irr::s32> m_destRect;
irr::core::rect<float> m_destRect;
irr::core::rect<irr::s32> m_sourceRect;
//irr::video::SColor m_colors[4];
STKTextBillboardChar(irr::video::ITexture* texture,
const irr::core::rect<irr::s32>& destRect,
const irr::core::rect<float>& destRect,
const irr::core::rect<irr::s32>& sourceRect,
const irr::video::SColor* const colors)
{
@@ -82,7 +82,7 @@ public:
}
virtual void collectChar(irr::video::ITexture* texture,
const irr::core::rect<irr::s32>& destRect,
const irr::core::rect<float>& destRect,
const irr::core::rect<irr::s32>& sourceRect,
const irr::video::SColor* const colors) OVERRIDE;