Redid tiling!

Now faster (always uses 6 verticies thanks to wrapped textures) and precise (properly cuts off).
This commit is contained in:
Aloshi 2012-10-10 10:21:03 -05:00
parent b6264f4858
commit 640c3b52bc
3 changed files with 35 additions and 28 deletions

View file

@ -2,6 +2,7 @@ October 10
-Added a theming tag for the Fast Select box's text. -Added a theming tag for the Fast Select box's text.
-Fixed GuiBox background being positioned wrong. -Fixed GuiBox background being positioned wrong.
-Fixed GuiBox/GuiFastSelect render order. -Fixed GuiBox/GuiFastSelect render order.
-Redid tiling to use only 6 verticies (instead of tilecount-dependent) with wrapped textures. Tiling is also precise now (cuts off when it should).
October 7 October 7
-Fixed borders for GuiBox. The right and bottom borders are flipped, too. -Fixed borders for GuiBox. The right and bottom borders are flipped, too.

View file

@ -154,6 +154,9 @@ void GuiImage::loadImage(std::string path)
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
mWidth = width; mWidth = width;
mHeight = height; mHeight = height;
@ -262,21 +265,15 @@ void GuiImage::onRender()
{ {
if(mTiled) if(mTiled)
{ {
unsigned int xCount = (unsigned int)((float)mResizeWidth/mWidth + 1.5); float xCount = ((float)mResizeWidth/mWidth);
unsigned int yCount = (unsigned int)((float)mResizeHeight/mHeight + 1.5); float yCount = ((float)mResizeHeight/mHeight);
//std::cout << "Array size: " << xCount << "x" << yCount << "\n"; //std::cout << "Array size: " << xCount << "x" << yCount << "\n";
GLfloat* points = new GLfloat[xCount * yCount * 12]; GLfloat* points = new GLfloat[12];
GLfloat* texs = new GLfloat[xCount * yCount * 12]; GLfloat* texs = new GLfloat[12];
for(unsigned int x = 0; x < xCount; x++) buildImageArray(getOffsetX(), getOffsetY(), points, texs, xCount, yCount);
{ drawImageArray(points, texs, 6);
for(unsigned int y = 0; y < yCount; y++)
{
buildImageArray(getOffsetX() + x*mDrawWidth, getOffsetY() + y*mDrawHeight, points + (12 * (x*yCount + y)), texs + (12 * (x*yCount + y)));
}
}
drawImageArray(points, texs, xCount * yCount * 6);
delete[] points; delete[] points;
delete[] texs; delete[] texs;
}else{ }else{
@ -287,35 +284,44 @@ void GuiImage::onRender()
} }
} }
void GuiImage::buildImageArray(int posX, int posY, GLfloat* points, GLfloat* texs) void GuiImage::buildImageArray(int posX, int posY, GLfloat* points, GLfloat* texs, float percentageX, float percentageY)
{ {
points[0] = posX - (mDrawWidth * mOriginX); points[1] = posY - (mDrawHeight * mOriginY); float px = percentageX;
points[2] = posX - (mDrawWidth * mOriginX); points[3] = posY + (mDrawHeight * (1 - mOriginY)); float py = percentageY;
points[4] = posX + (mDrawWidth * (1 - mOriginX)); points[5] = posY - (mDrawHeight * mOriginY);
points[6] = posX + (mDrawWidth * (1 - mOriginX)); points[7] = posY - (mDrawHeight * mOriginY); points[0] = posX - (mDrawWidth * mOriginX) * px; points[1] = posY - (mDrawHeight * mOriginY) * py;
points[8] = posX - (mDrawWidth * mOriginX); points[9] = posY + (mDrawHeight * (1 - mOriginY)); points[2] = posX - (mDrawWidth * mOriginX) * px; points[3] = posY + (mDrawHeight * (1 - mOriginY)) * py;
points[10] = posX + (mDrawWidth * (1 -mOriginX)); points[11] = posY + (mDrawHeight * (1 - mOriginY)); points[4] = posX + (mDrawWidth * (1 - mOriginX)) * px; points[5] = posY - (mDrawHeight * mOriginY) * py;
points[6] = posX + (mDrawWidth * (1 - mOriginX)) * px; points[7] = posY - (mDrawHeight * mOriginY) * py;
points[8] = posX - (mDrawWidth * mOriginX) * px; points[9] = posY + (mDrawHeight * (1 - mOriginY)) * py;
points[10] = posX + (mDrawWidth * (1 -mOriginX)) * px; points[11] = posY + (mDrawHeight * (1 - mOriginY)) * py;
texs[0] = 0; texs[1] = 1; texs[0] = 0; texs[1] = percentageY;
texs[2] = 0; texs[3] = 0; texs[2] = 0; texs[3] = 0;
texs[4] = 1; texs[5] = 1; texs[4] = percentageX; texs[5] = percentageY;
texs[6] = 1; texs[7] = 1; texs[6] = percentageX; texs[7] = percentageY;
texs[8] = 0; texs[9] = 0; texs[8] = 0; texs[9] = 0;
texs[10] = 1; texs[11] = 0; texs[10] = percentageX; texs[11] = 0;
if(mFlipX) if(mFlipX)
{ {
for(int i = 0; i < 11; i += 2) for(int i = 0; i < 11; i += 2)
texs[i] = !texs[i]; if(texs[i] == percentageX)
texs[i] = 0;
else
texs[i] = percentageX;
} }
if(mFlipY) if(mFlipY)
{ {
for(int i = 1; i < 12; i += 2) for(int i = 1; i < 12; i += 2)
texs[i] = !texs[i]; if(texs[i] == percentageY)
texs[i] = 0;
else
texs[i] = percentageY;
} }
} }

View file

@ -42,7 +42,7 @@ private:
void loadImage(std::string path); void loadImage(std::string path);
void resize(); void resize();
void buildImageArray(int x, int y, GLfloat* points, GLfloat* texs); //writes 12 GLfloat points and 12 GLfloat texture coordinates to a given array at a given position void buildImageArray(int x, int y, GLfloat* points, GLfloat* texs, float percentageX = 1, float percentageY = 1); //writes 12 GLfloat points and 12 GLfloat texture coordinates to a given array at a given position
void drawImageArray(GLfloat* points, GLfloat* texs, unsigned int count = 6); //draws the given set of points and texture coordinates, number of coordinate pairs may be specified (default 6) void drawImageArray(GLfloat* points, GLfloat* texs, unsigned int count = 6); //draws the given set of points and texture coordinates, number of coordinate pairs may be specified (default 6)
void unloadImage(); void unloadImage();