Moved Primitive2DList into 2dutils, fixed coding style of 2dutils.

This commit is contained in:
hiker 2015-05-12 16:24:40 +10:00
parent ee26611759
commit fbad97a4a7
4 changed files with 191 additions and 128 deletions

View File

@ -24,9 +24,27 @@
#include "../../lib/irrlicht/source/Irrlicht/COpenGLTexture.h"
static void drawTexColoredQuad(const video::ITexture *texture, const video::SColor *col, float width, float height,
float center_pos_x, float center_pos_y, float tex_center_pos_x, float tex_center_pos_y,
float tex_width, float tex_height)
// ============================================================================
class Primitive2DList : public Shader<Primitive2DList>,
public TextureReadNew<ST_BILINEAR_FILTERED >
{
public:
Primitive2DList()
{
loadProgram(OBJECT, GL_VERTEX_SHADER, "primitive2dlist.vert",
GL_FRAGMENT_SHADER, "transparent.frag");
assignUniforms();
assignSamplerNames(m_program, 0, "tex", ST_BILINEAR_FILTERED);
} // Primitive2DList
}; //Primitive2DList
// ============================================================================
static void drawTexColoredQuad(const video::ITexture *texture,
const video::SColor *col, float width,
float height, float center_pos_x,
float center_pos_y, float tex_center_pos_x,
float tex_center_pos_y, float tex_width,
float tex_height)
{
unsigned colors[] = {
col[0].getRed(), col[0].getGreen(), col[0].getBlue(), col[0].getAlpha(),
@ -51,38 +69,40 @@ static void drawTexColoredQuad(const video::ITexture *texture, const video::SCol
glBindBuffer(GL_ARRAY_BUFFER, 0);
glGetError();
}
} // drawTexColoredQuad
static
void drawTexQuad(GLuint texture, float width, float height,
float center_pos_x, float center_pos_y, float tex_center_pos_x, float tex_center_pos_y,
float tex_width, float tex_height)
// ----------------------------------------------------------------------------
static void drawTexQuad(GLuint texture, float width, float height,
float center_pos_x, float center_pos_y,
float tex_center_pos_x, float tex_center_pos_y,
float tex_width, float tex_height)
{
UIShader::TextureRectShader::getInstance()->use();
glBindVertexArray(SharedObject::UIVAO);
UIShader::TextureRectShader::getInstance()->setTextureUnits(texture);
UIShader::TextureRectShader::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));
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) );
glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
glBindVertexArray(0);
glBindBuffer(GL_ARRAY_BUFFER, 0);
glGetError();
}
} // drawTexQuad
static void
getSize(unsigned texture_width, unsigned texture_height, bool textureisRTT,
const core::rect<s32>& 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
)
// ----------------------------------------------------------------------------
static void getSize(unsigned texture_width, unsigned texture_height,
bool textureisRTT, const core::rect<s32>& 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;
@ -112,35 +132,36 @@ float &tex_center_pos_x, float &tex_center_pos_y
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);
}
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>* clipRect,
const video::SColor &colors, bool useAlphaChannelOfTexture)
// ----------------------------------------------------------------------------
void draw2DImage(const video::ITexture* texture,
const core::rect<s32>& 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()) {
video::SColor duplicatedArray[4] = {
colors, colors, colors, colors
};
draw2DImage(texture, destRect, sourceRect, clipRect, duplicatedArray, useAlphaChannelOfTexture);
if (!CVS->isGLSL())
{
video::SColor duplicatedArray[4] = { colors, colors, colors, colors };
draw2DImage(texture, destRect, sourceRect, clip_rect, duplicatedArray,
use_alpha_channel_of_texture);
return;
}
float width, height,
center_pos_x, center_pos_y,
tex_width, tex_height,
tex_center_pos_x, tex_center_pos_y;
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);
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 (useAlphaChannelOfTexture)
if (use_alpha_channel_of_texture)
{
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
@ -149,40 +170,54 @@ void draw2DImage(const video::ITexture* texture, const core::rect<s32>& destRect
{
glDisable(GL_BLEND);
}
if (clipRect)
if (clip_rect)
{
if (!clipRect->isValid())
if (!clip_rect->isValid())
return;
glEnable(GL_SCISSOR_TEST);
const core::dimension2d<u32>& renderTargetSize = irr_driver->getActualScreenSize();
glScissor(clipRect->UpperLeftCorner.X, renderTargetSize.Height - clipRect->LowerRightCorner.Y,
clipRect->getWidth(), clipRect->getHeight());
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());
}
UIShader::UniformColoredTextureRectShader::getInstance()->use();
glBindVertexArray(SharedObject::UIVAO);
UIShader::UniformColoredTextureRectShader::getInstance()->setTextureUnits(static_cast<const irr::video::COpenGLTexture*>(texture)->getOpenGLTextureName());
UIShader::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);
const video::COpenGLTexture *c_texture =
static_cast<const video::COpenGLTexture*>(texture);
UIShader::UniformColoredTextureRectShader::getInstance()
->setTextureUnits(c_texture->getOpenGLTextureName());
UIShader::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 (clipRect)
if (clip_rect)
glDisable(GL_SCISSOR_TEST);
glUseProgram(0);
glGetError();
}
} // draw2DImage
// ----------------------------------------------------------------------------
void draw2DImageFromRTT(GLuint texture, size_t texture_w, size_t texture_h,
const core::rect<s32>& destRect,
const core::rect<s32>& sourceRect, const core::rect<s32>* clipRect,
const video::SColor &colors, bool useAlphaChannelOfTexture)
const core::rect<s32>& destRect,
const core::rect<s32>& sourceRect,
const core::rect<s32>* clip_rect,
const video::SColor &colors,
bool use_alpha_channel_of_texture)
{
if (useAlphaChannelOfTexture)
if (use_alpha_channel_of_texture)
{
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
@ -200,36 +235,42 @@ void draw2DImageFromRTT(GLuint texture, size_t texture_w, size_t texture_h,
glBindVertexArray(SharedObject::UIVAO);
UIShader::UniformColoredTextureRectShader::getInstance()->setTextureUnits(texture);
UIShader::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);
UIShader::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);
}
} // draw2DImageFromRTT
void draw2DImage(const video::ITexture* texture, const core::rect<s32>& destRect,
const core::rect<s32>& sourceRect, const core::rect<s32>* clipRect,
const video::SColor* const colors, bool useAlphaChannelOfTexture)
// ----------------------------------------------------------------------------
void draw2DImage(const video::ITexture* texture,
const core::rect<s32>& destRect,
const core::rect<s32>& sourceRect,
const core::rect<s32>* clip_rect,
const video::SColor* const colors,
bool use_alpha_channel_of_texture)
{
if (!CVS->isGLSL())
{
irr_driver->getVideoDriver()->draw2DImage(texture, destRect, sourceRect, clipRect, colors, useAlphaChannelOfTexture);
irr_driver->getVideoDriver()->draw2DImage(texture, destRect, sourceRect,
clip_rect, colors,
use_alpha_channel_of_texture);
return;
}
float width, height,
center_pos_x, center_pos_y,
tex_width, tex_height,
tex_center_pos_x, tex_center_pos_y;
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);
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 (useAlphaChannelOfTexture)
if (use_alpha_channel_of_texture)
{
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
@ -238,66 +279,86 @@ void draw2DImage(const video::ITexture* texture, const core::rect<s32>& destRect
{
glDisable(GL_BLEND);
}
if (clipRect)
if (clip_rect)
{
if (!clipRect->isValid())
if (!clip_rect->isValid())
return;
glEnable(GL_SCISSOR_TEST);
const core::dimension2d<u32>& renderTargetSize = irr_driver->getActualScreenSize();
glScissor(clipRect->UpperLeftCorner.X, renderTargetSize.Height - clipRect->LowerRightCorner.Y,
clipRect->getWidth(), clipRect->getHeight());
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);
{
drawTexColoredQuad(texture, colors, width, height, center_pos_x,
center_pos_y, tex_center_pos_x, tex_center_pos_y,
tex_width, tex_height);
}
else
drawTexQuad(static_cast<const irr::video::COpenGLTexture*>(texture)->getOpenGLTextureName(), width, height, center_pos_x, center_pos_y,
tex_center_pos_x, tex_center_pos_y, tex_width, tex_height);
if (clipRect)
{
const video::COpenGLTexture *c_texture =
static_cast<const video::COpenGLTexture*>(texture);
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, u32 primitiveCount,
video::E_VERTEX_TYPE vType, scene::E_PRIMITIVE_TYPE pType, video::E_INDEX_TYPE iType)
u32 vertexCount, const void* indexList,
u32 primitiveCount, video::E_VERTEX_TYPE vType,
scene::E_PRIMITIVE_TYPE pType,
video::E_INDEX_TYPE iType)
{
if (!CVS->isGLSL())
{
irr_driver->getVideoDriver()->draw2DVertexPrimitiveList(vertices, vertexCount, indexList, primitiveCount, vType, pType, iType);
irr_driver->getVideoDriver()
->draw2DVertexPrimitiveList(vertices, vertexCount, indexList,
primitiveCount, vType, pType, iType);
return;
}
GLuint tmpvao, tmpvbo, tmpibo;
primitiveCount += 2;
glGenVertexArrays(1, &tmpvao);
glBindVertexArray(tmpvao);
glGenBuffers(1, &tmpvbo);
glBindBuffer(GL_ARRAY_BUFFER, tmpvbo);
glBufferData(GL_ARRAY_BUFFER, vertexCount * getVertexPitchFromType(vType), vertices, GL_STREAM_DRAW);
glBufferData(GL_ARRAY_BUFFER, vertexCount * getVertexPitchFromType(vType),
vertices, GL_STREAM_DRAW);
glGenBuffers(1, &tmpibo);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, tmpibo);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, primitiveCount * sizeof(u16), indexList, GL_STREAM_DRAW);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, primitiveCount * sizeof(u16),
indexList, GL_STREAM_DRAW);
VertexUtils::bindVertexArrayAttrib(vType);
UIShader::Primitive2DList::getInstance()->use();
UIShader::Primitive2DList::getInstance()->setUniforms();
const video::SOverrideMaterial &m = irr_driver->getVideoDriver()->getOverrideMaterial();
Primitive2DList::getInstance()->use();
Primitive2DList::getInstance()->setUniforms();
const video::SOverrideMaterial &m = irr_driver->getVideoDriver()
->getOverrideMaterial();
compressTexture(tex, false);
UIShader::Primitive2DList::getInstance()->setTextureUnits(getTextureGLuint(tex));
Primitive2DList::getInstance()->setTextureUnits(getTextureGLuint(tex));
glDrawElements(GL_TRIANGLE_FAN, primitiveCount, GL_UNSIGNED_SHORT, 0);
glDeleteVertexArrays(1, &tmpvao);
glDeleteBuffers(1, &tmpvbo);
glDeleteBuffers(1, &tmpibo);
}
} // draw2DVertexPrimitiveList
// ----------------------------------------------------------------------------
void GL32_draw2DRectangle(video::SColor color, const core::rect<s32>& position,
const core::rect<s32>* clip)
const core::rect<s32>* clip)
{
if (!CVS->isGLSL())
@ -336,14 +397,18 @@ void GL32_draw2DRectangle(video::SColor color, const core::rect<s32>& position,
return;
glEnable(GL_SCISSOR_TEST);
const core::dimension2d<u32>& renderTargetSize = irr_driver->getActualScreenSize();
glScissor(clip->UpperLeftCorner.X, renderTargetSize.Height - clip->LowerRightCorner.Y,
clip->getWidth(), clip->getHeight());
const core::dimension2d<u32>& render_target_size =
irr_driver->getActualScreenSize();
glScissor(clip->UpperLeftCorner.X,
render_target_size.Height - clip->LowerRightCorner.Y,
clip->getWidth(), clip->getHeight());
}
UIShader::ColoredRectShader::getInstance()->use();
glBindVertexArray(SharedObject::UIVAO);
UIShader::ColoredRectShader::getInstance()->setUniforms(core::vector2df(center_pos_x, center_pos_y), core::vector2df(width, height), color);
UIShader::ColoredRectShader::getInstance()
->setUniforms(core::vector2df(center_pos_x, center_pos_y),
core::vector2df(width, height), color );
glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
glBindBuffer(GL_ARRAY_BUFFER, 0);
@ -353,4 +418,4 @@ void GL32_draw2DRectangle(video::SColor color, const core::rect<s32>& position,
glUseProgram(0);
glGetError();
}
} // GL32_draw2DRectangle

View File

@ -28,23 +28,35 @@
#include <irrTypes.h>
void draw2DImageFromRTT(GLuint texture, size_t texture_w, size_t texture_h,
const irr::core::rect<irr::s32>& destRect,
const irr::core::rect<irr::s32>& sourceRect, const irr::core::rect<irr::s32>* clipRect,
const irr::video::SColor &colors, bool useAlphaChannelOfTexture);
const irr::core::rect<irr::s32>& destRect,
const irr::core::rect<irr::s32>& sourceRect,
const irr::core::rect<irr::s32>* clipRect,
const irr::video::SColor &colors,
bool useAlphaChannelOfTexture);
void draw2DImage(const irr::video::ITexture* texture, const irr::core::rect<irr::s32>& 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,
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, const irr::core::rect<irr::s32>* clipRect,
const irr::video::SColor* const colors, bool useAlphaChannelOfTexture);
void draw2DImage(const irr::video::ITexture* texture,
const irr::core::rect<irr::s32>& destRect,
const irr::core::rect<irr::s32>& sourceRect,
const irr::core::rect<irr::s32>* clipRect,
const irr::video::SColor* const colors,
bool useAlphaChannelOfTexture);
void draw2DVertexPrimitiveList(irr::video::ITexture *t, const void* vertices,
irr::u32 vertexCount, const void* indexList, irr::u32 primitiveCount,
irr::video::E_VERTEX_TYPE vType = irr::video::EVT_STANDARD, irr::scene::E_PRIMITIVE_TYPE pType = irr::scene::EPT_TRIANGLES, irr::video::E_INDEX_TYPE iType = irr::video::EIT_16BIT);
irr::u32 vertexCount, const void* indexList,
irr::u32 primitiveCount,
irr::video::E_VERTEX_TYPE vType = irr::video::EVT_STANDARD,
irr::scene::E_PRIMITIVE_TYPE pType = irr::scene::EPT_TRIANGLES,
irr::video::E_INDEX_TYPE iType = irr::video::EIT_16BIT );
void GL32_draw2DRectangle(irr::video::SColor color, const irr::core::rect<irr::s32>& position,
const irr::core::rect<irr::s32>* clip = 0);
void GL32_draw2DRectangle(irr::video::SColor color,
const irr::core::rect<irr::s32>& position,
const irr::core::rect<irr::s32>* clip = 0);
#endif

View File

@ -1326,14 +1326,6 @@ namespace FullScreenShader
namespace UIShader
{
Primitive2DList::Primitive2DList()
{
loadProgram(OBJECT,
GL_VERTEX_SHADER, "primitive2dlist.vert",
GL_FRAGMENT_SHADER, "transparent.frag");
assignUniforms();
assignSamplerNames(m_program, 0, "tex", ST_BILINEAR_FILTERED);
}
TextureRectShader::TextureRectShader()
{

View File

@ -524,12 +524,6 @@ public:
namespace UIShader
{
class Primitive2DList : public Shader<Primitive2DList>,
public TextureReadNew<ST_BILINEAR_FILTERED >
{
public:
Primitive2DList();
};
class TextureRectShader : public Shader<TextureRectShader, core::vector2df,
core::vector2df, core::vector2df,