git-svn-id: svn://db.shs.com.ru/libs@638 a8b55f48-bf90-11e4-a774-851b48703e85

This commit is contained in:
2019-12-02 20:09:18 +00:00
parent 385070f70f
commit 35668c13fc
15 changed files with 792 additions and 263 deletions

View File

@@ -27,7 +27,6 @@ ObjectBase::ObjectBase(Mesh * geom, Material * mat) {
type_ = glMesh;
render_mode = View;
pass_ = Solid;
scale_ = QVector3D(1., 1., 1.);
parent_ = nullptr;
color_ = Qt::white;
is_root = is_init = is_tex_loaded = selected_ = false;
@@ -67,12 +66,11 @@ ObjectBase * ObjectBase::clone(bool withChildren) {
o->accept_light = accept_light;
o->accept_fog = accept_fog;
o->visible_ = visible_;
o->color_ = color_;
o->type_ = type_;
o->raw_matrix = raw_matrix;
o->mat_ = mat_;
o->pos_ = pos_;
o->angles_ = angles_;
o->scale_ = scale_;
o->trans = trans;
o->itransform_ = itransform_;
o->bound = bound;
o->name_ = name_;// + "_copy";
@@ -215,62 +213,28 @@ void ObjectBase::setVisible(bool v) {
}
void ObjectBase::rotateX(GLfloat a) {
raw_matrix = false;
angles_.setX(angles_.x() + a);
buildTransform();
}
void ObjectBase::rotateY(GLfloat a) {
raw_matrix = false;
angles_.setY(angles_.y() + a);
buildTransform();
}
void ObjectBase::rotateZ(GLfloat a) {
raw_matrix = false;
angles_.setZ(angles_.z() + a);
while (angles_.z() < -360.f) angles_.setZ(angles_.z() + 360.f);
while (angles_.z() > 360.f) angles_.setZ(angles_.z() - 360.f);
buildTransform();
}
void ObjectBase::setRotationX(GLfloat a) {
raw_matrix = false;
angles_.setX(a);
buildTransform();
}
void ObjectBase::setRotationY(GLfloat a) {
raw_matrix = false;
angles_.setY(a);
trans.setRotationZ(trans.rotationZ() + a);
//angles_.setZ(angles_.z() + a);
//while (angles_.z() < -360.f) angles_.setZ(angles_.z() + 360.f);
//while (angles_.z() > 360.f) angles_.setZ(angles_.z() - 360.f);
buildTransform();
}
void ObjectBase::setRotationZ(GLfloat a) {
raw_matrix = false;
angles_.setZ(a);
while (angles_.z() < -360.f) angles_.setZ(angles_.z() + 360.f);
while (angles_.z() > 360.f) angles_.setZ(angles_.z() - 360.f);
trans.setRotationZ(a);
//angles_.setZ(a);
//while (angles_.z() < -360.f) angles_.setZ(angles_.z() + 360.f);
//while (angles_.z() > 360.f) angles_.setZ(angles_.z() - 360.f);
buildTransform();
}
void ObjectBase::setRotation(const QVector3D & a) {
raw_matrix = false;
angles_= a;
buildTransform();
}
void ObjectBase::resetRotation() {
raw_matrix = false;
angles_ = QVector3D(0., 0., 0.);
void ObjectBase::setTransform(const Transform & t) {
trans = t;
buildTransform();
}
@@ -328,15 +292,27 @@ void ObjectBase::removeProperty(const QString & pn) {
}
void ObjectBase::setTransform(const QMatrix4x4 & t) {
raw_matrix = true;
mat_ = t;
pos_ = mat_.column(3).toVector3D();
mat_.setColumn(3, QVector4D(0., 0., 0., 1.));
void ObjectBase::setMatrix(const QMatrix4x4 & t) {
//raw_matrix = true;
//mat_ = t;
//pos_ = mat_.column(3).toVector3D();
//mat_.setColumn(3, QVector4D(0., 0., 0., 1.));
trans.setMatrix(t);
buildTransform();
}
QMatrix4x4 ObjectBase::matrix() const {
return trans.matrix();
}
QVector3D ObjectBase::inParentSpace(const QVector3D & v) const {
if (!parent_) return v;
return (parent_->matrix() * QVector4D(v, 1)).toVector3D();
}
bool ObjectBase::isSelected(bool check_parents) const {
if (!check_parents) return selected_;
if (selected_) return true;
@@ -397,11 +373,11 @@ void ObjectBase::buildTransform() {
ObjectBase * p = parent_;
if (p)
itransform_ = p->itransform_;
if (raw_matrix) {
itransform_.translate(pos_);
itransform_ *= mat_;
//qDebug() << "raw_matrix" << itransform_;
} else
//if (raw_matrix) {
// itransform_.translate(pos_);
// itransform_ *= mat_;
// //qDebug() << "raw_matrix" << itransform_;
//} else
localTransform(itransform_);
//qDebug() << name_ << itransform_;
foreach (ObjectBase * i, children_)
@@ -417,11 +393,7 @@ void ObjectBase::initInternal() {
void ObjectBase::localTransform(QMatrix4x4 & m) {
m.translate(pos_);
m.rotate(angles_.z(), 0., 0., 1.);
m.rotate(angles_.y(), 0., 1., 0.);
m.rotate(angles_.x(), 1., 0., 0.);
m.scale(scale_);
m *= trans.matrix();
}
@@ -450,15 +422,16 @@ void ObjectBase::setMeshChanged() {
QMatrix4x4 ObjectBase::worldMatrix(QMatrix4x4 parent) const {
QMatrix4x4 mat;
mat.translate(pos_);
if (raw_matrix) {
mat *= mat_;
} else {
if (angles_.z() != 0.f) mat.rotate(angles_.z(), 0., 0., 1.);
if (angles_.y() != 0.f) mat.rotate(angles_.y(), 0., 1., 0.);
if (angles_.x() != 0.f) mat.rotate(angles_.x(), 1., 0., 0.);
mat.scale(scale_);
}
//mat.translate(pos_);
//if (raw_matrix) {
// mat *= mat_;
//} else {
// if (angles_.z() != 0.f) mat.rotate(angles_.z(), 0., 0., 1.);
// if (angles_.y() != 0.f) mat.rotate(angles_.y(), 0., 1., 0.);
// if (angles_.x() != 0.f) mat.rotate(angles_.x(), 1., 0., 0.);
// mat.scale(scale_);
//}
mat = trans.matrix();
return parent * mat;
}
@@ -470,19 +443,56 @@ AimedObject::AimedObject() {
}
QVector3D AimedObject::worldAim() const {
QVector3D ret = worldPos() + worldDirection() * aim_dist;
return ret;
}
void AimedObject::setAim(const QVector3D & p) {
aim_ = p;
QVector3D dir = inParentSpace(p) - pos(), up;
if (!QVector3D::crossProduct(QVector3D(0,0,1), dir).isNull())
up = QVector3D(0,0,1);
trans.setRotation(Transform::fromDirection(dir));
aim_dist = dir.length();
//qDebug() << "setAim" << p << aim();
}
QVector3D AimedObject::direction() const {
return (aim_ - pos_).normalized();
return trans.direction();
}
void AimedObject::setDirection(const QVector3D & d) {
double len = qMax(direction().length(), 0.001f);
aim_ = pos_ + (d.normalized() * len);
//double len = qMax(aim_.length(), 0.001f);
//aim_ = d.normalized() * len;
}
void AimedObject::flyCloser(double s) {
double tl = 1. / (1. + s);
move(direction() * aim_dist * (1. - tl));
aim_dist *= tl;
}
void AimedObject::flyFarer(double s) {
double tl = 1. * (1. + s);
move(direction() * aim_dist * (1. - tl));
aim_dist *= tl;
}
void AimedObject::flyToDistance(double d) {
move(direction() * (aim_dist - d));
aim_dist = d;
//qDebug() << d << (aim() - pos()).length() << aim();
}
void AimedObject::transformChanged() {
}
@@ -502,12 +512,12 @@ Light::Light(): AimedObject(), shadow_map(0, true, GL_R16F) {
Light::Light(const QVector3D & p, const QColor & c, float i): AimedObject(), shadow_map(0, true, GL_R16F) {
type_ = glLight;
light_type = Omni;
pos_ = p;
intensity = i;
color_ = c;
angle_start = angle_end = 180.;
decay_linear = decay_quadratic = decay_start = 0.;
decay_const = decay_end = 1.;
setPos(p);
setDirection(0, 0, -1.);
}
@@ -525,8 +535,8 @@ ObjectBase * Light::clone(bool withChildren) {
}
o->color_ = color_;
o->light_type = light_type;
o->pos_ = pos_;
o->aim_ = aim_;
o->trans = trans;
o->aim_dist = aim_dist;
o->angle_start = angle_start;
o->angle_end = angle_end;
o->intensity = intensity;
@@ -548,21 +558,20 @@ QDataStream & operator <<(QDataStream & s, const ObjectBase * p) {
//qDebug() << "place" << p->name() << "...";
cs.add(1, int(p->type_)).add(2, p->accept_light).add(3, p->accept_fog).add(4, p->visible_)
.add(5, p->cast_shadow).add(6, p->rec_shadow).add(7, p->raw_matrix).add(8, p->line_width)
.add(9, int(p->render_mode)).add(11, p->pos_).add(12, p->angles_)
.add(13, p->scale_).add(14, p->mat_).add(16, p->children_.size())
.add(17, p->name_).add(18, p->meta).add(19, p->color_);
.add(9, int(p->render_mode)).add(14, p->mat_).add(16, p->children_.size())
.add(17, p->name_).add(18, p->meta).add(19, p->color_).add(20, p->trans);
//qDebug() << "place self done";
if (p->type_ == ObjectBase::glLight) {
//qDebug() << "place light ...";
const Light * l = (const Light*)p;
cs.add(100, l->direction()).add(101, l->angle_start).add(102, l->angle_end).add(103, l->intensity)
cs.add(100, l->aim()).add(101, l->angle_start).add(102, l->angle_end).add(103, l->intensity)
.add(104, l->decay_const).add(105, l->decay_linear).add(106, l->decay_quadratic)
.add(107, l->decay_start).add(108, l->decay_end).add(109, int(l->light_type)).add(110, l->aim());
.add(107, l->decay_start).add(108, l->decay_end).add(109, int(l->light_type));
}
if (p->type_ == ObjectBase::glCamera) {
//qDebug() << "place camera ...";
const Camera * c = (const Camera*)p;
cs.add(200, c->aim_).add(201, c->fov_).add(202, c->depth_start).add(203, c->depth_end)
cs.add(200, c->aim()).add(201, c->fov_).add(202, c->depth_start).add(203, c->depth_end)
.add(204, c->angle_limit_lower_xy).add(205, c->angle_limit_upper_xy)
.add(206, c->mirror_x).add(207, c->mirror_y);
}
@@ -601,22 +610,23 @@ QDataStream & operator >>(QDataStream & s, ObjectBase *& p) {
case 8: if (p) p->line_width = cs.getData<float>(); break;
case 9: if (p) p->render_mode = (ObjectBase::RenderMode)cs.getData<int>(); break;
//case 10: if (p) p->material_ = cs.getData<Material>(); break;
case 11: if (p) p->pos_ = cs.getData<QVector3D>(); break;
case 12:
if (p) p->angles_ = cs.getData<QVector3D>();
if (c) {
c->setAngles(cs.getData<QVector3D>());
cam_angles = c->angles();
}
break;
case 13: if (p) p->scale_ = cs.getData<QVector3D>(); break;
//case 11: if (p) p->pos_ = cs.getData<QVector3D>(); break;
//case 12:
// if (p) p->angles_ = cs.getData<QVector3D>();
// if (c) {
// c->setAngles(cs.getData<QVector3D>());
// cam_angles = c->angles();
// }
//break;
//case 13: if (p) p->scale_ = cs.getData<QVector3D>(); break;
case 14: if (p) p->mat_ = cs.getData<QMatrix4x4>(); break;
//case 15: if (p) p->vbo = cs.getData<VBO>(); break;
case 16: if (p) ccnt = cs.getData<int>(); break;
case 17: if (p) p->name_ = cs.getData<QString>(); break;
case 18: if (p) p->meta = cs.getData<QVariantMap>(); break;
case 19: if (p) p->color_ = cs.getData<QColor>(); break;
case 100: if (l) l->setDirection(cs.getData<QVector3D>());break;
case 20: if (p) p->trans = cs.getData<Transform>(); break;
case 100: if (l) l->setAim(cs.getData<QVector3D>()); break;
case 101: if (l) l->angle_start = cs.getData<GLfloat>(); break;
case 102: if (l) l->angle_end = cs.getData<GLfloat>(); break;
case 103: if (l) l->intensity = cs.getData<GLfloat>(); break;
@@ -626,7 +636,6 @@ QDataStream & operator >>(QDataStream & s, ObjectBase *& p) {
case 107: if (l) l->decay_start = cs.getData<GLfloat>(); break;
case 108: if (l) l->decay_end = cs.getData<GLfloat>(); break;
case 109: if (l) l->light_type = (Light::Type)cs.getData<int>(); break;
case 110: if (l) l->setAim(cs.getData<QVector3D>()); if (l->aim().isNull()) l->setAim(l->direction()); break;
case 200: if (c) c->setAim(cs.getData<QVector3D>()); break;
case 201: if (c) c->setFOV(cs.getData<GLfloat>()); break;
case 202: if (c) c->setDepthStart(cs.getData<GLfloat>()); break;
@@ -637,7 +646,7 @@ QDataStream & operator >>(QDataStream & s, ObjectBase *& p) {
case 207: if (c) c->mirror_y = cs.getData<bool>(); break;
}
}
if (c) c->setAngles(cam_angles);
//if (c) c->setAngles(cam_angles);
//qDebug() << p->name() << ccnt;
for (int i = 0; i < ccnt; ++i) {
ObjectBase * c = nullptr;
@@ -646,5 +655,6 @@ QDataStream & operator >>(QDataStream & s, ObjectBase *& p) {
c->parent_ = p;
p->children_ << c;
}
p->buildTransform();
return s;
}