code format
This commit is contained in:
@@ -1,29 +1,30 @@
|
||||
/*
|
||||
QGL TextureManager
|
||||
Ivan Pelipenko peri4ko@yandex.ru
|
||||
QGL TextureManager
|
||||
Ivan Pelipenko peri4ko@yandex.ru
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU Lesser General Public License as published by
|
||||
the Free Software Foundation, either version 3 of the License, or
|
||||
(at your option) any later version.
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU Lesser General Public License as published by
|
||||
the Free Software Foundation, either version 3 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU Lesser General Public License for more details.
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU Lesser General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Lesser General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
You should have received a copy of the GNU Lesser General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#include "gltexture_manager.h"
|
||||
|
||||
#include "gltypes.h"
|
||||
|
||||
QStringList TextureManager::search_pathes(".");
|
||||
|
||||
|
||||
QVector3D colorVector(QRgb c) {
|
||||
return QVector3D(((uchar*)(&c))[0] / 255.f, ((uchar*)(&c))[1] / 255.f, ((uchar*)(&c))[2] / 255.f);
|
||||
return QVector3D(((uchar *)(&c))[0] / 255.f, ((uchar *)(&c))[1] / 255.f, ((uchar *)(&c))[2] / 255.f);
|
||||
}
|
||||
|
||||
|
||||
@@ -37,12 +38,12 @@ GLuint TextureManager::loadTexture(const QString & path, bool ownership, bool bu
|
||||
if (p.isEmpty()) return 0;
|
||||
int tid = textureID(p, bump);
|
||||
if (tid > 0) {
|
||||
//qDebug() << "[TextureManager] Found" << path << "as" << tid;
|
||||
// qDebug() << "[TextureManager] Found" << path << "as" << tid;
|
||||
return tid;
|
||||
}
|
||||
QImage image(p);
|
||||
if (bump) convertToNormal(image);
|
||||
//qDebug() << p << image.width() << image.height() << image.format() << bump;
|
||||
// qDebug() << p << image.width() << image.height() << image.format() << bump;
|
||||
GLuint tid_ = tid;
|
||||
createGLTexture(f, tid_, image);
|
||||
tid = tid_;
|
||||
@@ -53,7 +54,7 @@ GLuint TextureManager::loadTexture(const QString & path, bool ownership, bool bu
|
||||
qDebug() << "[TextureManager] Loaded" << p << "as" << tid;
|
||||
if (ownership) {
|
||||
tex_ids[bump ? 1 : 0].insert(p, tid);
|
||||
tex_im [bump ? 1 : 0].insert(p, image);
|
||||
tex_im[bump ? 1 : 0].insert(p, image);
|
||||
}
|
||||
return tid;
|
||||
}
|
||||
@@ -69,7 +70,7 @@ GLuint TextureManager::loadTexture(const QImage & im, bool ownership, bool bump)
|
||||
qDebug() << "[TextureManager] Can`t load image";
|
||||
return tid;
|
||||
}
|
||||
//qDebug() << "[TextureManager] Loaded image as" << tid;
|
||||
// qDebug() << "[TextureManager] Loaded image as" << tid;
|
||||
return tid;
|
||||
}
|
||||
|
||||
@@ -109,15 +110,18 @@ void TextureManager::reloadTexture(GLuint tid, const QImage & im) {
|
||||
|
||||
void TextureManager::convertToNormal(QImage & im) {
|
||||
if (im.isNull()) return;
|
||||
QImage sim = im.convertToFormat(QImage::Format_ARGB32);
|
||||
float sum[3] = {0., 0., 0.};
|
||||
llong a = 0;
|
||||
QImage sim = im.convertToFormat(QImage::Format_ARGB32);
|
||||
float sum[3] = {0., 0., 0.};
|
||||
llong a = 0;
|
||||
const uchar * sd = sim.constBits();
|
||||
for (int i = 0; i < sim.height(); i++) {
|
||||
for (int j = 0; j < sim.width(); j++) {
|
||||
sum[2] += sd[a] / 255.f - 0.5f; ++a;
|
||||
sum[1] += sd[a] / 255.f - 0.5f; ++a;
|
||||
sum[0] += sd[a] / 255.f - 0.5f; ++a;
|
||||
sum[2] += sd[a] / 255.f - 0.5f;
|
||||
++a;
|
||||
sum[1] += sd[a] / 255.f - 0.5f;
|
||||
++a;
|
||||
sum[0] += sd[a] / 255.f - 0.5f;
|
||||
++a;
|
||||
++a;
|
||||
}
|
||||
}
|
||||
@@ -125,13 +129,13 @@ void TextureManager::convertToNormal(QImage & im) {
|
||||
sum[0] /= wh;
|
||||
sum[1] /= wh;
|
||||
sum[2] /= wh;
|
||||
//qDebug() << sum[0] << sum[1] << sum[2];
|
||||
// qDebug() << sum[0] << sum[1] << sum[2];
|
||||
if ((qAbs(sum[0]) <= 0.05f) && (qAbs(sum[1]) <= 0.05f) && (sum[2] >= 0.25f)) /// already normal
|
||||
return;
|
||||
//qDebug() << "convert to normal";
|
||||
// qDebug() << "convert to normal";
|
||||
QImage dim = QImage(sim.width(), sim.height(), QImage::Format_ARGB32);
|
||||
int tx, ty, w = sim.width(), h = sim.height();
|
||||
a = 0;
|
||||
a = 0;
|
||||
uchar * dd = dim.bits();
|
||||
for (int i = 0; i < sim.height(); i++) {
|
||||
for (int j = 0; j < sim.width(); j++) {
|
||||
@@ -145,27 +149,31 @@ void TextureManager::convertToNormal(QImage & im) {
|
||||
p[2] = colorVector(sim.pixel(tx, i));
|
||||
res.setY(piClamp(0.5f + (p[0].length() - p[1].length()) / 2.f, 0.f, 1.f));
|
||||
res.setX(piClamp(0.5f - (p[0].length() - p[2].length()) / 2.f, 0.f, 1.f));
|
||||
tx = (j + 1) % w;
|
||||
ty = (i + 1) % h;
|
||||
tx = (j + 1) % w;
|
||||
ty = (i + 1) % h;
|
||||
p[1] = colorVector(sim.pixel(j, ty));
|
||||
p[2] = colorVector(sim.pixel(tx, i));
|
||||
res.setY(piClamp(0.5f + (p[0].length() - p[1].length()) / 2.f, 0.f, 1.f));
|
||||
res.setX(piClamp(0.5f - (p[0].length() - p[2].length()) / 2.f, 0.f, 1.f));
|
||||
res.setZ(1.f);
|
||||
dd[a] = res.z() * 255; ++a;
|
||||
dd[a] = res.y() * 255; ++a;
|
||||
dd[a] = res.x() * 255; ++a;
|
||||
dd[a] = 255; ++a;
|
||||
dd[a] = res.z() * 255;
|
||||
++a;
|
||||
dd[a] = res.y() * 255;
|
||||
++a;
|
||||
dd[a] = res.x() * 255;
|
||||
++a;
|
||||
dd[a] = 255;
|
||||
++a;
|
||||
}
|
||||
}
|
||||
im = dim;
|
||||
//im.save("_normal.png");
|
||||
// im.save("_normal.png");
|
||||
}
|
||||
|
||||
|
||||
bool TextureManager::loadTextures() {
|
||||
QFileInfoList fil;
|
||||
foreach (const QString & i, tex_pathes)
|
||||
foreach(const QString & i, tex_pathes)
|
||||
loadTexture(i, true);
|
||||
tex_pathes.clear();
|
||||
return true;
|
||||
@@ -178,7 +186,7 @@ void TextureManager::deleteTextures() {
|
||||
qDebug() << "[TextureManager] Delete" << texs.size() << "textures";
|
||||
if (!texs.isEmpty()) f->glDeleteTextures(texs.size(), &texs[0]);
|
||||
tex_ids[i].clear();
|
||||
tex_im [i].clear();
|
||||
tex_im[i].clear();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -189,7 +197,7 @@ void TextureManager::deleteTexture(const QString & name) {
|
||||
GLuint id = tex_ids[i][name];
|
||||
f->glDeleteTextures(1, &id);
|
||||
tex_ids[i].remove(name);
|
||||
tex_im [i].remove(name);
|
||||
tex_im[i].remove(name);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user