git-svn-id: svn://db.shs.com.ru/libs@540 a8b55f48-bf90-11e4-a774-851b48703e85
This commit is contained in:
@@ -19,7 +19,6 @@
|
||||
#include "gltypes.h"
|
||||
#include "qglview.h"
|
||||
|
||||
GLTextureManager * currentGLTextureManager;
|
||||
QStringList GLTextureManagerBase::search_pathes(".");
|
||||
|
||||
|
||||
@@ -35,12 +34,12 @@ bool GLCubeTexture::create() {
|
||||
glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
|
||||
//glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_GENERATE_MIPMAP_SGIS, GL_TRUE);
|
||||
//glClearError();
|
||||
glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_X, 0, format_, size, size, 0, GL_RGBA, GL_UNSIGNED_BYTE, 0);
|
||||
glTexImage2D(GL_TEXTURE_CUBE_MAP_NEGATIVE_X, 0, format_, size, size, 0, GL_RGBA, GL_UNSIGNED_BYTE, 0);
|
||||
glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_Y, 0, format_, size, size, 0, GL_RGBA, GL_UNSIGNED_BYTE, 0);
|
||||
glTexImage2D(GL_TEXTURE_CUBE_MAP_NEGATIVE_Y, 0, format_, size, size, 0, GL_RGBA, GL_UNSIGNED_BYTE, 0);
|
||||
glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_Z, 0, format_, size, size, 0, GL_RGBA, GL_UNSIGNED_BYTE, 0);
|
||||
glTexImage2D(GL_TEXTURE_CUBE_MAP_NEGATIVE_Z, 0, format_, size, size, 0, GL_RGBA, GL_UNSIGNED_BYTE, 0);
|
||||
glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_X, 0, format_, size, size, 0, GL_RGBA, GL_UNSIGNED_BYTE, nullptr);
|
||||
glTexImage2D(GL_TEXTURE_CUBE_MAP_NEGATIVE_X, 0, format_, size, size, 0, GL_RGBA, GL_UNSIGNED_BYTE, nullptr);
|
||||
glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_Y, 0, format_, size, size, 0, GL_RGBA, GL_UNSIGNED_BYTE, nullptr);
|
||||
glTexImage2D(GL_TEXTURE_CUBE_MAP_NEGATIVE_Y, 0, format_, size, size, 0, GL_RGBA, GL_UNSIGNED_BYTE, nullptr);
|
||||
glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_Z, 0, format_, size, size, 0, GL_RGBA, GL_UNSIGNED_BYTE, nullptr);
|
||||
glTexImage2D(GL_TEXTURE_CUBE_MAP_NEGATIVE_Z, 0, format_, size, size, 0, GL_RGBA, GL_UNSIGNED_BYTE, nullptr);
|
||||
//qDebug() << glGetError();
|
||||
changed_ = false;
|
||||
return id_ > 0;
|
||||
@@ -83,16 +82,14 @@ void GLCubeTexture::loadPathesFromDirectory(const QString & dir) {
|
||||
|
||||
|
||||
QString GLTextureManagerBase::findFile(const QString & path) {
|
||||
if (!currentGLTextureManager) return QString();
|
||||
return ::findFile(path, currentGLTextureManager->search_pathes);
|
||||
return ::findFile(path, search_pathes);
|
||||
}
|
||||
|
||||
|
||||
GLuint GLTextureManagerBase::loadTexture(const QString & path, bool ownership, bool bump) {
|
||||
if (!currentGLTextureManager) return 0;
|
||||
QString p = findFile(path);
|
||||
if (p.isEmpty()) return 0;
|
||||
int tid = ((GLTextureManagerBase*)currentGLTextureManager)->textureID(p, bump);
|
||||
int tid = textureID(p, bump);
|
||||
if (tid > 0) {
|
||||
//qDebug() << "[TextureManager] Found" << path << "as" << tid;
|
||||
return tid;
|
||||
@@ -110,7 +107,7 @@ GLuint GLTextureManagerBase::loadTexture(const QString & path, bool ownership, b
|
||||
return tid;
|
||||
}
|
||||
qDebug() << "[TextureManager] Loaded" << p << "as" << tid;
|
||||
if (ownership) ((GLTextureManagerBase*)currentGLTextureManager)->tex_ids[bump ? 1 : 0].insert(p, tid);
|
||||
if (ownership) tex_ids[bump ? 1 : 0].insert(p, tid);
|
||||
return tid;
|
||||
}
|
||||
|
||||
@@ -126,13 +123,12 @@ GLuint GLTextureManagerBase::loadTexture(const QImage & im, bool ownership, bool
|
||||
return tid;
|
||||
}
|
||||
//qDebug() << "[TextureManager] Loaded image as" << tid;
|
||||
if (ownership) ((GLTextureManagerBase*)currentGLTextureManager)->tex_ids[bump ? 1 : 0].insert(QString(), tid);
|
||||
if (ownership) tex_ids[bump ? 1 : 0].insert(QString(), tid);
|
||||
return tid;
|
||||
}
|
||||
|
||||
|
||||
void GLTextureManagerBase::reloadTexture(GLuint tid, const QString & path) {
|
||||
if (!currentGLTextureManager) return;
|
||||
QString p = findFile(path);
|
||||
if (p.isEmpty() || (tid == 0)) return;
|
||||
QImage image(p);
|
||||
@@ -146,7 +142,6 @@ void GLTextureManagerBase::reloadTexture(GLuint tid, const QString & path) {
|
||||
|
||||
|
||||
void GLTextureManagerBase::reloadTexture(GLuint tid, const QImage & im) {
|
||||
if (!currentGLTextureManager) return;
|
||||
if (im.isNull() || (tid == 0)) return;
|
||||
QImage image(im);
|
||||
createGLTexture(tid, image);
|
||||
@@ -154,28 +149,31 @@ void GLTextureManagerBase::reloadTexture(GLuint tid, const QImage & im) {
|
||||
}
|
||||
|
||||
|
||||
Vector3d colorVector(QRgb c) {return Vector3d(((uchar*)(&c))[0] / 255., ((uchar*)(&c))[1] / 255., ((uchar*)(&c))[2] / 255.);}
|
||||
Vector3d colorVector(QRgb c) {
|
||||
return Vector3d(((uchar*)(&c))[0] / 255., ((uchar*)(&c))[1] / 255., ((uchar*)(&c))[2] / 255.);
|
||||
}
|
||||
|
||||
|
||||
void GLTextureManagerBase::convertToNormal(QImage & im) {
|
||||
if (im.isNull()) return;
|
||||
QImage sim = im.convertToFormat(QImage::Format_ARGB32);
|
||||
double sum[3] = {0., 0., 0.};
|
||||
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] += double(sd[a]) / 255. - 0.5; ++a;
|
||||
sum[1] += double(sd[a]) / 255. - 0.5; ++a;
|
||||
sum[0] += double(sd[a]) / 255. - 0.5; ++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;
|
||||
}
|
||||
}
|
||||
double wh = sim.width() * sim.height();
|
||||
float wh = sim.width() * sim.height();
|
||||
sum[0] /= wh;
|
||||
sum[1] /= wh;
|
||||
sum[2] /= wh;
|
||||
qDebug() << sum[0] << sum[1] << sum[2];
|
||||
if ((qAbs(sum[0]) <= 0.05) && (qAbs(sum[1]) <= 0.05) && (sum[2] >= 0.4)) /// already normal
|
||||
if ((qAbs(sum[0]) <= 0.05f) && (qAbs(sum[1]) <= 0.05f) && (sum[2] >= 0.4f)) /// already normal
|
||||
return;
|
||||
qDebug() << "convert to bump";
|
||||
QImage dim = QImage(sim.width(), sim.height(), QImage::Format_ARGB32);
|
||||
@@ -192,14 +190,14 @@ void GLTextureManagerBase::convertToNormal(QImage & im) {
|
||||
p[0] = colorVector(sim.pixel(j, i));
|
||||
p[1] = colorVector(sim.pixel(j, ty));
|
||||
p[2] = colorVector(sim.pixel(tx, i));
|
||||
res.y = piClamp(0.5 + (p[0].length() - p[1].length()) / 2., 0., 1.);
|
||||
res.x = piClamp(0.5 + (p[0].length() - p[2].length()) / 2., 0., 1.);
|
||||
res.y = piClamp(0.5f + (p[0].length() - p[1].length()) / 2.f, 0.f, 1.f);
|
||||
res.x = piClamp(0.5f + (p[0].length() - p[2].length()) / 2.f, 0.f, 1.f);
|
||||
tx = (j + 1) % w;
|
||||
ty = (i + 1) % h;
|
||||
p[1] = colorVector(sim.pixel(j, ty));
|
||||
p[2] = colorVector(sim.pixel(tx, i));
|
||||
res.y = piClamp(0.5 + (p[0].length() - p[1].length()) / 2., 0., 1.);
|
||||
res.x = piClamp(0.5 + (p[0].length() - p[2].length()) / 2., 0., 1.);
|
||||
res.y = piClamp(0.5f + (p[0].length() - p[1].length()) / 2.f, 0.f, 1.f);
|
||||
res.x = piClamp(0.5f + (p[0].length() - p[2].length()) / 2.f, 0.f, 1.f);
|
||||
res.z = 1.;
|
||||
dd[a] = res.z * 255; ++a;
|
||||
dd[a] = res.x * 255; ++a;
|
||||
@@ -247,7 +245,7 @@ void Material::apply(QOpenGLShaderProgram * prog) {
|
||||
glMaterialfv(GL_FRONT, GL_DIFFUSE, mat_diffuse);
|
||||
glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular);
|
||||
//qDebug() << (map_specularity.color_amount)*128.;
|
||||
glMaterialf(GL_FRONT, GL_SHININESS, (map_specularity.color_amount)*128.);
|
||||
glMaterialf(GL_FRONT, GL_SHININESS, (map_specularity.color_amount)*128.f);
|
||||
glMaterialfv(GL_FRONT, GL_EMISSION, mat_emission);
|
||||
glMaterialfv(GL_FRONT, GL_AMBIENT, mat_diffuse);
|
||||
}
|
||||
@@ -256,7 +254,6 @@ void Material::apply(QOpenGLShaderProgram * prog) {
|
||||
|
||||
void Material::loadTextures(GLTextureManagerBase * tm) {
|
||||
//qDebug() << "load textures";
|
||||
if (!tm) tm = (GLTextureManagerBase*)currentGLTextureManager;
|
||||
if (!tm) return;
|
||||
if (!map_diffuse.bitmap_path.isEmpty()) map_diffuse.bitmap_id = tm->loadTexture(map_diffuse.bitmap_path);
|
||||
if (!map_normal.bitmap_path.isEmpty()) map_normal.bitmap_id = tm->loadTexture(map_normal.bitmap_path, true, true);
|
||||
|
||||
Reference in New Issue
Block a user