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

This commit is contained in:
2019-05-27 16:36:51 +00:00
parent 09e7268779
commit bb75525a3b
37 changed files with 926 additions and 683 deletions

View File

@@ -27,7 +27,7 @@ GLObjectBase::GLObjectBase() {
pass_ = Solid;
geom_prim = Triangles;
scale_ = QVector3D(1., 1., 1.);
parent_ = 0;
parent_ = nullptr;
is_root = is_init = is_tex_loaded = selected_ = false;
visible_ = accept_fog = accept_light = cast_shadow = rec_shadow = select_ = true;
line_width = -1.;
@@ -36,7 +36,7 @@ GLObjectBase::GLObjectBase() {
type_ = glMesh;
raw_matrix = false;
mat_.setToIdentity();
view_ = 0;
view_ = nullptr;
}
@@ -45,7 +45,7 @@ GLObjectBase::~GLObjectBase() {
if (parent_) parent_->children_.removeAll(this);
if (view_) ((QGLView*)view_)->objectDeleted(this);
foreach (GLObjectBase * c, children_) {
c->parent_ = 0;
c->parent_ = nullptr;
delete c;
}
}
@@ -81,7 +81,7 @@ GLObjectBase * GLObjectBase::clone(bool withChildren) {
o->vbo.normals_ = vbo.normals_;
o->vbo.texcoords_ = vbo.texcoords_;
o->vbo.colors_ = vbo.colors_;
o->view_ = 0;
o->view_ = nullptr;
o->children_.clear();
if (withChildren) {
for (int i = 0; i < children_.size(); ++i)
@@ -90,6 +90,17 @@ GLObjectBase * GLObjectBase::clone(bool withChildren) {
return o;
}
void GLObjectBase::init() {
calculateBoundingBox();
vbo.init();
vbo.rebuffer();
//material_.reflection.create();
//qDebug() << "init" << vbo.buffer_;
is_init = true;
}
void GLObjectBase::draw(QOpenGLShaderProgram * prog, bool simplest) {
vbo.draw(geom_prim, prog, simplest);
/*if (!d_vertices.isEmpty()) {
@@ -134,21 +145,65 @@ void GLObjectBase::addChild(GLObjectBase * o) {
}
void GLObjectBase::removeChild(GLObjectBase * o) {
if (o == this) return;
children_.removeAll(o);
o->parent_ = nullptr;
o->buildTransform();
if (view_ != nullptr) view_->collectLights();
}
void GLObjectBase::removeChild(int index) {
children_[index]->parent_ = nullptr;
children_[index]->buildTransform();
children_.removeAt(index);
if (view_ != nullptr) view_->collectLights();
}
void GLObjectBase::clearChildren(bool deleteAll) {
foreach (GLObjectBase * i, children_) {
i->view_ = 0;
i->parent_ = 0;
i->view_ = nullptr;
i->parent_ = nullptr;
i->clearChildren(deleteAll);
if (deleteAll) {
delete i;
} else {
i->buildTransform();
}
i->buildTransform();
}
children_.clear();
if (view_) view_->collectLights();
}
GLObjectBase * GLObjectBase::child(int index) {
if (index < 0 || index >= children_.size()) return nullptr;
return children_[index];
}
GLObjectBase * GLObjectBase::child(const QString & name) {
foreach (GLObjectBase * i, children_)
if (i->name_ == name) return i;
return nullptr;
}
const GLObjectBase * GLObjectBase::child(int index) const {
if (index < 0 || index >= children_.size()) return nullptr;
return children_[index];
}
const GLObjectBase * GLObjectBase::child(const QString & name) const {
foreach (GLObjectBase * i, children_)
if (i->name_ == name) return i;
return nullptr;
}
QList<GLObjectBase * > GLObjectBase::children(bool all_) {
if (!all_) return children_;
QList<GLObjectBase * > cl;
@@ -157,6 +212,66 @@ QList<GLObjectBase * > GLObjectBase::children(bool all_) {
}
void GLObjectBase::rotateX(GLfloat a) {
raw_matrix = false;
angles_.setX(angles_.x() + a);
buildTransform();
}
void GLObjectBase::rotateY(GLfloat a) {
raw_matrix = false;
angles_.setY(angles_.y() + a);
buildTransform();
}
void GLObjectBase::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 GLObjectBase::setRotationX(GLfloat a) {
raw_matrix = false;
angles_.setX(a);
buildTransform();
}
void GLObjectBase::setRotationY(GLfloat a) {
raw_matrix = false;
angles_.setY(a);
buildTransform();
}
void GLObjectBase::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);
buildTransform();
}
void GLObjectBase::setRotation(const QVector3D & a) {
raw_matrix = false;
angles_= a;
buildTransform();
}
void GLObjectBase::resetRotation() {
raw_matrix = false;
angles_ = QVector3D(0., 0., 0.);
buildTransform();
}
void GLObjectBase::addChildren(QList<GLObjectBase * > & list, GLObjectBase * where) {
foreach (GLObjectBase * i, where->children_) {
list << i;
@@ -165,6 +280,15 @@ void GLObjectBase::addChildren(QList<GLObjectBase * > & list, GLObjectBase * whe
}
void GLObjectBase::loadTextures(bool with_children) {
material_.loadTextures(view_->textureManager());
if (with_children)
foreach (GLObjectBase * i, children_) i->loadTextures();
is_tex_loaded = true;
checkPass();
}
void GLObjectBase::calculateBoundingBox() {
bound = vbo.boundingBox();
QVector<QVector3D> c = bound.corners(), tc;
@@ -218,6 +342,15 @@ void GLObjectBase::select() {
}
void GLObjectBase::setMaterial(const Material & m, bool with_children) {
material_ = m;
if (with_children)
foreach (GLObjectBase * i, children_) i->setMaterial(m, true);
checkPass();
is_tex_loaded = false;
}
void GLObjectBase::buildTransform() {
itransform_.setToIdentity();
GLObjectBase * p = parent_;
@@ -235,6 +368,13 @@ void GLObjectBase::buildTransform() {
}
void GLObjectBase::initInternal() {
init();
loadTextures();
foreach (GLObjectBase * i, children_) i->initInternal();
}
void GLObjectBase::localTransform(QMatrix4x4 & m) {
m.translate(pos_);
m.rotate(angles_.z(), 0., 0., 1.);
@@ -245,7 +385,7 @@ void GLObjectBase::localTransform(QMatrix4x4 & m) {
void GLObjectBase::checkPass() {
if (material_.color_diffuse.alphaF() * (1.f - material_.transparency) < 1.f) pass_ = Transparent;
if (float(material_.color_diffuse.alphaF()) * (1.f - material_.transparency) < 1.f) pass_ = Transparent;
else pass_ = Solid;
}
@@ -256,9 +396,9 @@ QMatrix4x4 GLObjectBase::worldMatrix(QMatrix4x4 parent) const {
if (raw_matrix) {
mat *= mat_;
} else {
if (angles_.z() != 0.) mat.rotate(angles_.z(), 0., 0., 1.);
if (angles_.y() != 0.) mat.rotate(angles_.y(), 0., 1., 0.);
if (angles_.x() != 0.) mat.rotate(angles_.x(), 1., 0., 0.);
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_);
}
return parent * mat;
@@ -269,14 +409,14 @@ void GLObjectBase::render(int * id, QMap<int, GLObjectBase * > * ids, int sh_id_
if (!visible_) return;
//glPushMatrix();
///qglMultMatrix TODO
material_.apply(0);
if (id != 0) {
material_.apply(nullptr);
if (id != nullptr) {
++(*id);
ids->insert(*id, this);
//glVertexAttrib1f(sh_id_loc, (*id) / 255.f);
//qDebug() << "assign to" << sh_id_loc << (*id) / 255.f;
}
draw(0);
draw(nullptr);
foreach (GLObjectBase * i, children_)
i->render(id, ids, sh_id_loc);
//glPopMatrix();
@@ -296,7 +436,7 @@ Light::Light(): GLObjectBase(), shadow_map(0, true, GL_R16F) {
}
Light::Light(const QVector3D & p, const QColor & c, GLdouble i): GLObjectBase(), shadow_map(0, true, GL_R16F) {
Light::Light(const QVector3D & p, const QColor & c, float i): GLObjectBase(), shadow_map(0, true, GL_R16F) {
type_ = glLight;
light_type = Omni;
pos_ = p;
@@ -314,7 +454,7 @@ GLObjectBase * Light::clone(bool withChildren) {
//GLObjectBase::clone(withChildren);
o->is_init = false;
o->name_ = name_ + "_copy";
o->view_ = 0;
o->view_ = nullptr;
o->children_.clear();
if (withChildren) {
for (int i = 0; i < children_.size(); ++i)
@@ -340,7 +480,7 @@ void Light::draw(QOpenGLShaderProgram * prog, bool simplest) {
glBegin(GL_POINTS);
glVertex3f(0., 0., 0.);
glEnd();
double s = 10;
float s = 10;
if (light_type != Omni) {
glBegin(GL_LINES);
QVector4D dir = QVector4D(direction);
@@ -365,14 +505,14 @@ QDataStream & operator <<(QDataStream & s, const GLObjectBase * p) {
//qDebug() << "place self done";
if (p->type_ == GLObjectBase::glLight) {
//qDebug() << "place light ...";
Light * l = (Light*)p;
const Light * l = (const Light*)p;
cs << cs.chunk(100, l->direction) << cs.chunk(101, l->angle_start) << cs.chunk(102, l->angle_end) << cs.chunk(103, l->intensity)
<< cs.chunk(104, l->decay_const) << cs.chunk(105, l->decay_linear) << cs.chunk(106, l->decay_quadratic)
<< cs.chunk(107, l->decay_start) << cs.chunk(108, l->decay_end) << cs.chunk(109, int(l->light_type));
}
if (p->type_ == GLObjectBase::glCamera) {
//qDebug() << "place camera ...";
Camera * c = (Camera*)p;
const Camera * c = (const Camera*)p;
cs << cs.chunk(200, c->aim_) << cs.chunk(201, c->fov_) << cs.chunk(202, c->depth_start) << cs.chunk(203, c->depth_end)
<< cs.chunk(204, c->angle_limit_lower_xy) << cs.chunk(205, c->angle_limit_upper_xy)
<< cs.chunk(206, c->mirror_x) << cs.chunk(207, c->mirror_y);
@@ -385,10 +525,10 @@ QDataStream & operator <<(QDataStream & s, const GLObjectBase * p) {
}
QDataStream & operator >>(QDataStream & s, GLObjectBase *& p) {
ChunkStream cs(s);
p = 0;
p = nullptr;
int ccnt = 0;
Light * l = 0;
Camera * c = 0;
Light * l = nullptr;
Camera * c = nullptr;
//qDebug() << "read obj ...";
while (!cs.atEnd()) {
switch (cs.read()) {
@@ -410,7 +550,7 @@ QDataStream & operator >>(QDataStream & s, GLObjectBase *& p) {
case 5: if (p) p->cast_shadow = cs.getData<bool>(); break;
case 6: if (p) p->rec_shadow = cs.getData<bool>(); break;
case 7: if (p) p->raw_matrix = cs.getData<bool>(); break;
case 8: if (p) p->line_width = cs.getData<double>(); break;
case 8: if (p) p->line_width = cs.getData<float>(); break;
case 9: if (p) p->render_mode = (GLObjectBase::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;
@@ -422,28 +562,28 @@ QDataStream & operator >>(QDataStream & s, GLObjectBase *& p) {
case 17: if (p) p->name_ = cs.getData<QString>(); break;
case 18: if (p) p->meta = cs.getData<QVariantMap>(); break;
case 100: if (l) l->direction = cs.getData<QVector3D>(); break;
case 101: if (l) l->angle_start = cs.getData<GLdouble>(); break;
case 102: if (l) l->angle_end = cs.getData<GLdouble>(); break;
case 103: if (l) l->intensity = cs.getData<GLdouble>(); break;
case 104: if (l) l->decay_const = cs.getData<GLdouble>(); break;
case 105: if (l) l->decay_linear = cs.getData<GLdouble>(); break;
case 106: if (l) l->decay_quadratic = cs.getData<GLdouble>(); break;
case 107: if (l) l->decay_start = cs.getData<GLdouble>(); break;
case 108: if (l) l->decay_end = cs.getData<GLdouble>(); 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;
case 104: if (l) l->decay_const = cs.getData<GLfloat>(); break;
case 105: if (l) l->decay_linear = cs.getData<GLfloat>(); break;
case 106: if (l) l->decay_quadratic = cs.getData<GLfloat>(); break;
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 200: if (c) c->setAim(cs.getData<QVector3D>()); break;
case 201: if (c) c->setFOV(cs.getData<GLdouble>()); break;
case 202: if (c) c->setDepthStart(cs.getData<GLdouble>()); break;
case 203: if (c) c->setDepthEnd(cs.getData<GLdouble>()); break;
case 204: if (c) c->setAngleLowerLimitXY(cs.getData<GLdouble>()); break;
case 205: if (c) c->setAngleUpperLimitXY(cs.getData<GLdouble>()); break;
case 201: if (c) c->setFOV(cs.getData<GLfloat>()); break;
case 202: if (c) c->setDepthStart(cs.getData<GLfloat>()); break;
case 203: if (c) c->setDepthEnd(cs.getData<GLfloat>()); break;
case 204: if (c) c->setAngleLowerLimitXY(cs.getData<GLfloat>()); break;
case 205: if (c) c->setAngleUpperLimitXY(cs.getData<GLfloat>()); break;
case 206: if (c) c->mirror_x = cs.getData<bool>(); break;
case 207: if (c) c->mirror_y = cs.getData<bool>(); break;
}
}
//qDebug() << p->name() << ccnt;
for (int i = 0; i < ccnt; ++i) {
GLObjectBase * c = 0;
GLObjectBase * c = nullptr;
s >> c;
if (!c) continue;
c->parent_ = p;