diff --git a/qglengine/core/glprimitives.cpp b/qglengine/core/glprimitives.cpp index d05eb5f..01d054f 100644 --- a/qglengine/core/glprimitives.cpp +++ b/qglengine/core/glprimitives.cpp @@ -87,13 +87,12 @@ Mesh * Primitive::cube(float width, float length, float height) { } -Mesh * Primitive::ellipsoid(int segments_wl, int segments_h, float width, float length, float height, float end_angle) { +Mesh * Primitive::ellipsoid(int segments_wl, int segments_h, float radius, float end_angle) { Mesh * ret = new Mesh(); QVector & v(ret->vertices ()); QVector & n(ret->normals ()); QVector & t(ret->texcoords()); QVector< Vector3i> & ind(ret->indicesTriangles()); - double hh = height / 2.f; int hseg = segments_h + 1, wlseg = segments_wl + 1; double crw, crl, a, ch, twl; double eang = deg2rad * end_angle; @@ -101,10 +100,10 @@ Mesh * Primitive::ellipsoid(int segments_wl, int segments_h, float width, float QVector3D cp; for (int i = 0; i <= hseg; i++) { ch = -cos((double)i / hseg * M_PI); - cp.setZ(ch * hh); - twl = sqrt(1. - ch * ch) / 2.; - crw = twl * width; - crl = twl * length; + cp.setZ(ch * radius); + twl = sqrt(1. - ch * ch); + crw = twl * radius; + crl = twl * radius; int cvcnt = wlseg * 2; for (int j = 0; j < cvcnt; j++) { a = (double)j / (cvcnt - 1) * eang; @@ -112,7 +111,7 @@ Mesh * Primitive::ellipsoid(int segments_wl, int segments_h, float width, float cp.setY(crw * sin(a)); v << cp; t << QVector2D((double)j / (cvcnt - 1), ch/2.f + 0.5f); - n << (cp / QVector3D(length * length, width * width, height * height)).normalized(); + n << cp.normalized(); int si = v.size() - 1; if (j > 0 && i > 0) { ind << Vector3i(si - cvcnt - 1, si, si - 1); @@ -121,13 +120,12 @@ Mesh * Primitive::ellipsoid(int segments_wl, int segments_h, float width, float } } if (end_angle < 360.) { - Mesh * cap = Primitive::disc(segments_h+1, length, height, 180); + Mesh * cap = Primitive::disc(segments_h+1, radius, 180); cap->rotatePoints(90, 0, 1, 0); cap->rotatePoints(-90, 0, 0, 1); ret->append(cap); cap->flipNormals(); cap->rotatePoints(end_angle, 0, 0, 1); - cap->scalePoints(QVector3D((cos(eang)*cos(eang)*width+sin(eang)*sin(eang)*length)/width, (cos(eang)*cos(eang)*length+sin(eang)*sin(eang)*width)/length, 1)); ret->append(cap); delete cap; } @@ -135,7 +133,7 @@ Mesh * Primitive::ellipsoid(int segments_wl, int segments_h, float width, float } -Mesh * Primitive::disc(int segments, float width, float length, float end_angle) { +Mesh * Primitive::disc(int segments, float radius, float end_angle) { Mesh * ret = new Mesh(); QVector & v(ret->vertices ()); QVector & n(ret->normals ()); @@ -150,11 +148,11 @@ Mesh * Primitive::disc(int segments, float width, float length, float end_angle) end_angle *= deg2rad; for (int i = 0; i < segments; i++) { double a = (double)i / (segments - 1) * end_angle; - cp.setX(length / 2. * cos(a)); - cp.setY(width / 2. * sin(a)); + cp.setX(radius * cos(a)); + cp.setY(radius * sin(a)); v << cp; n << QVector3D(0, 0, 1); - t << QVector2D(cp.x() / width + 0.5f, cp.y() / length + 0.5f); + t << QVector2D(cp.x() / radius + 1, cp.y() / radius + 1); int si = v.size() - 1; if (i > 0) ind << Vector3i(si - 1, si, 0); } @@ -162,10 +160,10 @@ Mesh * Primitive::disc(int segments, float width, float length, float end_angle) } -QVector3D coneNormal(double rx, double ry, double height, double ang) { +QVector3D coneNormal(double r, double height, double ang) { QVector3D norm; - norm.setX(rx * cos(ang)); - norm.setY(ry * sin(ang)); + norm.setX(r * cos(ang)); + norm.setY(r * sin(ang)); norm.setZ(0.); double rl = norm.length(); double ca = atan2(rl, height); @@ -175,7 +173,7 @@ QVector3D coneNormal(double rx, double ry, double height, double ang) { } -Mesh * Primitive::cone(int segments, float width, float length, float height) { +Mesh * Primitive::cone(int segments, float radius, float height) { Mesh * ret = new Mesh(); QVector & v(ret->vertices ()); QVector & n(ret->normals ()); @@ -183,27 +181,26 @@ Mesh * Primitive::cone(int segments, float width, float length, float height) { QVector< Vector3i> & ind(ret->indicesTriangles()); int seg = qMax(segments + 1, 4); - double rx = width / 2., ry = length / 2.; QVector3D cp; for (int i = 0; i < seg; i++) { double a = (double)i / (seg - 1) * M_2PI; - cp.setX(ry * cos(a)); - cp.setY(rx * sin(a)); + cp.setX(radius * cos(a)); + cp.setY(radius * sin(a)); if (i > 0) { v << QVector3D(0, 0, height); t << QVector2D((double)(i - 1) / (seg - 1), 1.f); double ta = ((double)i - 0.5) / (seg - 1) * M_2PI; - n << coneNormal(rx, ry, height, ta); + n << coneNormal(radius, height, ta); } v << cp; t << QVector2D((double)i / (seg - 1), 0.f); - n << coneNormal(rx, ry, height, a); + n << coneNormal(radius, height, a); int si = v.size() - 1; if (i > 0) ind << Vector3i(si - 1, si - 2, si); } - Mesh * cap = Primitive::disc(segments, width, length); + Mesh * cap = Primitive::disc(segments, radius); cap->flipNormals(); ret->append(cap); delete cap; @@ -212,7 +209,7 @@ Mesh * Primitive::cone(int segments, float width, float length, float height) { } -Mesh * Primitive::cylinder(int segments, float width, float length, float height, float end_angle) { +Mesh * Primitive::cylinder(int segments, float radius, float height, float end_angle) { Mesh * ret = new Mesh(); QVector & v(ret->vertices ()); QVector & n(ret->normals ()); @@ -220,16 +217,15 @@ Mesh * Primitive::cylinder(int segments, float width, float length, float height QVector< Vector3i> & ind(ret->indicesTriangles()); int seg = qMax(segments + 1, 4); - double rx = width / 2., ry = length / 2.; QVector3D cp, norm; double eang = deg2rad * end_angle; for (int i = 0; i < seg; i++) { double a = (double)i / (seg - 1) * eang; - cp.setX(ry * cos(a)); - cp.setY(rx * sin(a)); + cp.setX(radius * cos(a)); + cp.setY(radius * sin(a)); cp.setZ(0.); - norm = (cp / QVector3D(length * length, width * width, 1)).normalized(); + norm = cp.normalized(); v << cp; cp.setZ(height); v << cp; @@ -243,7 +239,7 @@ Mesh * Primitive::cylinder(int segments, float width, float length, float height } } - Mesh * cap = Primitive::disc(segments, width, length, end_angle); + Mesh * cap = Primitive::disc(segments, radius, end_angle); cap->flipNormals(); ret->append(cap); cap->translatePoints(QVector3D(0., 0., height)); @@ -252,14 +248,13 @@ Mesh * Primitive::cylinder(int segments, float width, float length, float height delete cap; if (end_angle < 360.) { - Mesh * cap = Primitive::plane(length/2, height); + Mesh * cap = Primitive::plane(radius, height); cap->rotatePoints(90, 1, 0, 0); //cap->rotatePoints(-90, 0, 0, 1); - cap->translatePoints(length/4, 0, height/2); + cap->translatePoints(radius/2, 0, height/2); ret->append(cap); cap->flipNormals(); cap->rotatePoints(end_angle, 0, 0, 1); - cap->scalePoints(QVector3D((cos(eang)*cos(eang)*width+sin(eang)*sin(eang)*length)/width, (cos(eang)*cos(eang)*length+sin(eang)*sin(eang)*width)/length, 1)); ret->append(cap); delete cap; } @@ -269,13 +264,13 @@ Mesh * Primitive::cylinder(int segments, float width, float length, float height Mesh * Primitive::arrow(int segments, float thick, float angle) { - double cone_d = 3. * thick; - double cone_h = cone_d / tan(angle * deg2rad); + double cone_r = 2. * thick; + double cone_h = 1.5 * cone_r / tan(angle * deg2rad); Mesh * ret = new Mesh(); - Mesh * m = Primitive::cylinder(segments, thick, thick, 1. - cone_h); + Mesh * m = Primitive::cylinder(segments, thick, 1. - cone_h); ret->append(m); delete m; - m = Primitive::cone(segments, cone_d, cone_d, cone_h); + m = Primitive::cone(segments, cone_r, cone_h); m->translatePoints(QVector3D(0., 0., 1. - cone_h)); ret->append(m); delete m; @@ -323,7 +318,7 @@ Mesh * Primitive::torus(int segments_main, int segments_second, float radius_mai pcnt = v.size(); } if (end_angle < 360.) { - Mesh * cap = Primitive::disc(segments_second-1, radius_second * 2, radius_second * 2); + Mesh * cap = Primitive::disc(segments_second-1, radius_second); cap->rotatePoints(90, 1, 0, 0); cap->translatePoints(radius_main, 0, 0); ret->append(cap); @@ -362,29 +357,30 @@ Mesh * Primitive::cubeFrame(float width, float length, float height) { } -Mesh * Primitive::ellipsoidFrame(int segments_wl, int segments_h, float width, float length, float height) { +Mesh * Primitive::ellipsoidFrame(int segments_wl, int segments_h, float radius) { Mesh * ret = new Mesh(GL_LINES); QVector & v(ret->vertices ()); QVector & n(ret->normals ()); QVector & t(ret->texcoords()); QVector< Vector2i> & ind(ret->indicesLines()); - double hh = height / 2.f; int hseg = segments_h + 1, wlseg = segments_wl + 1; double crw, crl, a, ch, twl; QVector3D cp; for (int i = 0; i <= hseg; i++) { ch = -cos((double)i / hseg * M_PI); - cp.setZ(ch * hh); - twl = sqrt(1. - ch * ch) / 2.; - crw = twl * width; - crl = twl * length; + cp.setZ(ch * radius); + twl = sqrt(1. - ch * ch); + crw = twl * radius / 2; + crl = twl * radius / 2; int cvcnt = wlseg * 2; for (int j = 0; j < cvcnt; j++) { a = (double)j / (cvcnt - 1) * M_2PI; cp.setX(crl * cos(a)); cp.setY(crw * sin(a)); - v << cp; t << QVector2D((double)j / (cvcnt - 1), ch/2.f + 0.5f); + v << cp; + t << QVector2D((double)j / (cvcnt - 1), ch/2.f + 0.5f); + n << cp.normalized(); int si = v.size() - 1; if (j > 0 && i > 0) { ind << Vector2i(si, si - 1); @@ -392,16 +388,11 @@ Mesh * Primitive::ellipsoidFrame(int segments_wl, int segments_h, float width, f } } } - - n.resize(v.size()); - for (int i = 0; i < v.size(); i++) - n[i] = v[i].normalized(); - return ret; } -Mesh * Primitive::coneFrame(int segments, float width, float length, float height) { +Mesh * Primitive::coneFrame(int segments, float radius, float height) { Mesh * ret = new Mesh(GL_LINES); QVector & v(ret->vertices ()); QVector & n(ret->normals ()); @@ -409,21 +400,20 @@ Mesh * Primitive::coneFrame(int segments, float width, float length, float heigh QVector< Vector2i> & ind(ret->indicesLines()); int seg = qMax(segments + 1, 4); - double rx = width / 2., ry = length / 2.; QVector3D cp; for (int i = 0; i < seg; i++) { double a = (double)i / (seg - 1) * M_2PI; - cp.setX(ry * cos(a)); - cp.setY(rx * sin(a)); + cp.setX(radius * cos(a)); + cp.setY(radius * sin(a)); if (i > 0) { v << QVector3D(0, 0, height); t << QVector2D((double)(i - 1) / (seg - 1), 1.f); double ta = ((double)i - 0.5) / (seg - 1) * M_2PI; - n << coneNormal(rx, ry, height, ta); + n << coneNormal(radius, height, ta); } v << cp; t << QVector2D((double)i / (seg - 1), 0.f); - n << coneNormal(rx, ry, height, a); + n << coneNormal(radius, height, a); int si = v.size() - 1; if (i > 0) { ind << Vector2i(si - 1, si); diff --git a/qglengine/core/glprimitives.h b/qglengine/core/glprimitives.h index fb6597b..6a6b601 100644 --- a/qglengine/core/glprimitives.h +++ b/qglengine/core/glprimitives.h @@ -29,13 +29,13 @@ Mesh * plane(float width = 1., float length = 1.); Mesh * cube(float width = 1., float length = 1., float height = 1.); -Mesh * ellipsoid(int segments_wl, int segments_h, float width = 1., float length = 1., float height = 1., float end_angle = 360.); +Mesh * ellipsoid(int segments_wl, int segments_h, float radius = 1., float end_angle = 360.); -Mesh * disc(int segments, float width = 1., float length = 1., float end_angle = 360.); +Mesh * disc(int segments, float radius = 1., float end_angle = 360.); -Mesh * cone(int segments, float width = 1., float length = 1., float height = 1.); +Mesh * cone(int segments, float radius = 1., float height = 1.); -Mesh * cylinder(int segments, float width = 1., float length = 1., float height = 1., float end_angle = 360.); +Mesh * cylinder(int segments, float radius = 1., float height = 1., float end_angle = 360.); Mesh * arrow(int segments = 16, float thick = 0.04, float angle = 30.); // length = 1 @@ -46,9 +46,9 @@ Mesh * lineFrame(QVector3D p0, QVector3D p1); Mesh * cubeFrame(float width = 1., float length = 1., float height = 1.); -Mesh * ellipsoidFrame(int segments_wl, int segments_h, float width = 1., float length = 1., float height = 1.); +Mesh * ellipsoidFrame(int segments_wl, int segments_h, float radius = 1.); -Mesh * coneFrame(int segments, float width = 1., float length = 1., float height = 1.); +Mesh * coneFrame(int segments, float radius = 1., float height = 1.); } diff --git a/qglengine/renderer_service.cpp b/qglengine/renderer_service.cpp index f6756ab..520236a 100644 --- a/qglengine/renderer_service.cpp +++ b/qglengine/renderer_service.cpp @@ -60,8 +60,8 @@ RendererService::RendererService(Renderer * r_): r(r_) { omni_mesh_f = Primitive::ellipsoidFrame(2, 1); omni_mesh ->scalePoints(1.5); omni_mesh_f->scalePoints(1.5); - cone_mesh = Primitive::cone(8, 1., 1., 1.); - cone_mesh_f = Primitive::coneFrame(8, 1., 1., 1.); + cone_mesh = Primitive::cone(8, 0.5, 1.); + cone_mesh_f = Primitive::coneFrame(8, 0.5, 1.); QMatrix4x4 mat; mat.translate(0,0,-1); cone_mesh ->transformPoints(mat); @@ -79,10 +79,8 @@ RendererService::RendererService(Renderer * r_): r(r_) { handle_move_mesh = Primitive::arrow(12, 0.06); handle_ms_2_mesh = Primitive::torus(8, 12, 0.5, 0.02, 90); - Mesh * m = Primitive::disc(8, 1., 1., 90); + Mesh * m = Primitive::disc(8, 0.5, 90); handle_ms_2_mesh->append(m); - delete m; - m = Primitive::disc(8, 1., 1., 90); m->flipNormals(); handle_ms_2_mesh->append(m); delete m; @@ -93,12 +91,12 @@ RendererService::RendererService(Renderer * r_): r(r_) { handle_rotate_mesh->append(m); delete m; - handle_scale_mesh = Primitive::cylinder(12, 0.06, 0.06, 0.85); - m = Primitive::ellipsoid(12, 12, 0.3, 0.3, 0.3); + handle_scale_mesh = Primitive::cylinder(12, 0.06, 0.85); + m = Primitive::ellipsoid(12, 12, 0.3); m->translatePoints(QVector3D(0., 0., 0.85)); handle_scale_mesh->append(m); delete m; - handle_scale_3_mesh = Primitive::ellipsoid(12, 12, 0.4, 0.4, 0.4); + handle_scale_3_mesh = Primitive::ellipsoid(12, 12, 0.4); handle_move_mesh ->scalePoints(7.5); handle_ms_2_mesh ->scalePoints(7.5); diff --git a/qglengine/widgets/primitiveeditor.cpp b/qglengine/widgets/primitiveeditor.cpp index 2cbc629..d05ec53 100644 --- a/qglengine/widgets/primitiveeditor.cpp +++ b/qglengine/widgets/primitiveeditor.cpp @@ -14,22 +14,17 @@ PrimitiveEditor::PrimitiveEditor(QWidget *parent) : QWidget(parent), ui(new Ui:: editors[Cube] << ui->widgetWidth; editors[Cube] << ui->widgetLength; editors[Cube] << ui->widgetHeight; - editors[Ellipsoid] << ui->widgetWidth; - editors[Ellipsoid] << ui->widgetLength; - editors[Ellipsoid] << ui->widgetHeight; + editors[Ellipsoid] << ui->widgetRadius1; editors[Ellipsoid] << ui->widgetSegments; editors[Ellipsoid] << ui->widgetSegments2; editors[Ellipsoid] << ui->widgetAngle; - editors[Disc] << ui->widgetWidth; - editors[Disc] << ui->widgetLength; + editors[Disc] << ui->widgetRadius1; editors[Disc] << ui->widgetSegments; editors[Disc] << ui->widgetAngle; - editors[Cone] << ui->widgetWidth; - editors[Cone] << ui->widgetLength; + editors[Cone] << ui->widgetRadius1; editors[Cone] << ui->widgetHeight; editors[Cone] << ui->widgetSegments; - editors[Cylinder] << ui->widgetWidth; - editors[Cylinder] << ui->widgetLength; + editors[Cylinder] << ui->widgetRadius1; editors[Cylinder] << ui->widgetHeight; editors[Cylinder] << ui->widgetSegments; editors[Cylinder] << ui->widgetAngle; @@ -88,46 +83,36 @@ Mesh * PrimitiveEditor::createMesh(QVariantList & params) { case Ellipsoid: m = Primitive::ellipsoid(ui->spinSegments->value(), ui->spinSegments2->value(), - ui->spinWidth->value(), - ui->spinLength->value(), - ui->spinHeight->value(), + ui->spinRadius->value(), ui->spinAngle->value()); params << ui->spinSegments->value() << ui->spinSegments2->value() - << ui->spinWidth->value() - << ui->spinLength->value() - << ui->spinHeight->value() + << ui->spinRadius->value() << ui->spinAngle->value(); break; case Disc: m = Primitive::disc(ui->spinSegments->value(), - ui->spinWidth->value(), - ui->spinLength->value(), + ui->spinRadius->value(), ui->spinAngle->value()); params << ui->spinSegments->value() - << ui->spinWidth->value() - << ui->spinLength->value() + << ui->spinRadius->value() << ui->spinAngle->value(); break; case Cone: m = Primitive::cone(ui->spinSegments->value(), - ui->spinWidth->value(), - ui->spinLength->value(), + ui->spinRadius->value(), ui->spinHeight->value()); params << ui->spinSegments->value() - << ui->spinWidth->value() - << ui->spinLength->value() + << ui->spinRadius->value() << ui->spinHeight->value(); break; case Cylinder: m = Primitive::cylinder(ui->spinSegments->value(), - ui->spinWidth->value(), - ui->spinLength->value(), + ui->spinRadius->value(), ui->spinHeight->value(), ui->spinAngle->value()); params << ui->spinSegments->value() - << ui->spinWidth->value() - << ui->spinLength->value() + << ui->spinRadius->value() << ui->spinHeight->value() << ui->spinAngle->value(); break; @@ -182,27 +167,22 @@ void PrimitiveEditor::selectionChanged() { case Ellipsoid: ui->spinSegments->setValue(vl.takeFirst().toDouble()); ui->spinSegments2->setValue(vl.takeFirst().toDouble()); - ui->spinWidth->setValue(vl.takeFirst().toDouble()); - ui->spinLength->setValue(vl.takeFirst().toDouble()); - ui->spinHeight->setValue(vl.takeFirst().toDouble()); + ui->spinRadius->setValue(vl.takeFirst().toDouble()); ui->spinAngle->setValue(vl.takeFirst().toDouble()); break; case Disc: ui->spinSegments->setValue(vl.takeFirst().toDouble()); - ui->spinWidth->setValue(vl.takeFirst().toDouble()); - ui->spinLength->setValue(vl.takeFirst().toDouble()); + ui->spinRadius->setValue(vl.takeFirst().toDouble()); ui->spinAngle->setValue(vl.takeFirst().toDouble()); break; case Cone: ui->spinSegments->setValue(vl.takeFirst().toDouble()); - ui->spinWidth->setValue(vl.takeFirst().toDouble()); - ui->spinLength->setValue(vl.takeFirst().toDouble()); + ui->spinRadius->setValue(vl.takeFirst().toDouble()); ui->spinHeight->setValue(vl.takeFirst().toDouble()); break; case Cylinder: ui->spinSegments->setValue(vl.takeFirst().toDouble()); - ui->spinWidth->setValue(vl.takeFirst().toDouble()); - ui->spinLength->setValue(vl.takeFirst().toDouble()); + ui->spinRadius->setValue(vl.takeFirst().toDouble()); ui->spinHeight->setValue(vl.takeFirst().toDouble()); ui->spinAngle->setValue(vl.takeFirst().toDouble()); break;