178 lines
5.6 KiB
C++
178 lines
5.6 KiB
C++
/*
|
|
QGLView
|
|
Copyright (C) 2018 Ivan Pelipenko peri4ko@yandex.ru
|
|
|
|
This program is free software: you can redistribute it and/or modify
|
|
it under the terms of the GNU 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 General Public License for more details.
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
*/
|
|
|
|
#include "glprimitives.h"
|
|
|
|
|
|
void GLPrimitivePoint::draw(__GLShaderProgram__ * prog, bool simplest) {
|
|
glPointSize(sz);
|
|
glColor3f(material_.color_diffuse.redF(), material_.color_diffuse.greenF(), material_.color_diffuse.blueF());
|
|
glBegin(GL_POINTS);
|
|
glVertex3d(0., 0., 0.);
|
|
glEnd();
|
|
}
|
|
|
|
|
|
|
|
void GLPrimitiveLine::draw(__GLShaderProgram__ * prog, bool simplest) {
|
|
glColor3f(material_.color_diffuse.redF(), material_.color_diffuse.greenF(), material_.color_diffuse.blueF());
|
|
glBegin(GL_LINES);
|
|
glVertex3d(p0.x(), p0.y(), p0.z());
|
|
glVertex3d(p1.x(), p1.y(), p1.z());
|
|
glEnd();
|
|
}
|
|
|
|
|
|
|
|
GLPrimitiveCube::GLPrimitiveCube(double width, double length, double height, QVector3D pos): GLObjectBase() {
|
|
geom_prim = Quads;
|
|
w = width;
|
|
l = length;
|
|
h = height;
|
|
moveTo(pos);
|
|
init();
|
|
}
|
|
|
|
|
|
void GLPrimitiveCube::init() {
|
|
double hw = w / 2., hl = l / 2., hh = h / 2.;
|
|
//list = glGenLists(1);
|
|
//glNewList(list, GL_COMPILE);
|
|
//glColor4d(material_.color_diffuse.redF(), material_.color_diffuse.greenF(), material_.color_diffuse.blueF(), material_.color_diffuse.alphaF());
|
|
vbo.init();
|
|
QVector<GLfloat> & d_vertices(vbo.vertices()), & d_normals(vbo.normals()), & d_uvs(vbo.texcoords());
|
|
d_vertices.clear();
|
|
d_normals.clear();
|
|
d_uvs.clear();
|
|
for (int i = 0; i < 4; ++i)
|
|
d_normals << 0. << -1. << 0.;
|
|
d_vertices << -hw << -hl << -hh;
|
|
d_vertices << hw << -hl << -hh;
|
|
d_vertices << hw << -hl << hh;
|
|
d_vertices << -hw << -hl << hh;
|
|
d_uvs << 0. << 0. << 1. << 0. << 1. << 1. << 0. << 1.;
|
|
for (int i = 0; i < 4; ++i)
|
|
d_normals << 0. << 1. << 0.;
|
|
d_vertices << -hw << hl << -hh;
|
|
d_vertices << -hw << hl << hh;
|
|
d_vertices << hw << hl << hh;
|
|
d_vertices << hw << hl << -hh;
|
|
d_uvs << 0. << 0. << 1. << 0. << 1. << 1. << 0. << 1.;
|
|
for (int i = 0; i < 4; ++i)
|
|
d_normals << -1. << 0. << 0.;
|
|
d_vertices << -hw << -hl << -hh;
|
|
d_vertices << -hw << -hl << hh;
|
|
d_vertices << -hw << hl << hh;
|
|
d_vertices << -hw << hl << -hh;
|
|
d_uvs << 0. << 0. << 1. << 0. << 1. << 1. << 0. << 1.;
|
|
for (int i = 0; i < 4; ++i)
|
|
d_normals << 1. << 0. << 0.;
|
|
d_vertices << hw << -hl << -hh;
|
|
d_vertices << hw << hl << -hh;
|
|
d_vertices << hw << hl << hh;
|
|
d_vertices << hw << -hl << hh;
|
|
d_uvs << 0. << 0. << 1. << 0. << 1. << 1. << 0. << 1.;
|
|
for (int i = 0; i < 4; ++i)
|
|
d_normals << 0. << 0. << -1.;
|
|
d_vertices << -hw << -hl << -hh;
|
|
d_vertices << -hw << hl << -hh;
|
|
d_vertices << hw << hl << -hh;
|
|
d_vertices << hw << -hl << -hh;
|
|
d_uvs << 0. << 0. << 1. << 0. << 1. << 1. << 0. << 1.;
|
|
for (int i = 0; i < 4; ++i)
|
|
d_normals << 0. << 0. << 1.;
|
|
d_vertices << -hw << -hl << hh;
|
|
d_vertices << hw << -hl << hh;
|
|
d_vertices << hw << hl << hh;
|
|
d_vertices << -hw << hl << hh;
|
|
d_uvs << 0. << 0. << 1. << 0. << 1. << 1. << 0. << 1.;
|
|
is_init = true;
|
|
vbo.rebuffer();
|
|
}
|
|
|
|
|
|
GLPrimitiveEllipsoid::GLPrimitiveEllipsoid(double width, double length, double height, int seg_wl, int seg_h, QVector3D pos) {
|
|
geom_prim = GLObjectBase::Triangles;
|
|
w = width;
|
|
l = length;
|
|
h = height;
|
|
swl = seg_wl;
|
|
sh = seg_h;
|
|
moveTo(pos);
|
|
init();
|
|
}
|
|
|
|
|
|
void GLPrimitiveEllipsoid::putTriangle(const QVector3D & v0, const QVector3D & v1, const QVector3D & v2) {
|
|
vbo.vertices() << v0.x() << v0.y() << v0.z() << v1.x() << v1.y() << v1.z() << v2.x() << v2.y() << v2.z();
|
|
QVector3D n = QVector3D::normal(v1 - v0, v2 - v0);
|
|
for (int i = 0; i < 3; ++i)
|
|
vbo.normals() << n.x() << n.y() << n.z();
|
|
return;
|
|
QVector3D s(w, l, h);
|
|
n = (v0 * s).normalized(); vbo.normals() << n.x() << n.y() << n.z();
|
|
n = (v1 * s).normalized(); vbo.normals() << n.x() << n.y() << n.z();
|
|
n = (v2 * s).normalized(); vbo.normals() << n.x() << n.y() << n.z();
|
|
}
|
|
|
|
|
|
void GLPrimitiveEllipsoid::init() {
|
|
QVector<QVector3D> points;
|
|
vbo.clear();
|
|
vbo.init();
|
|
uint ret = 0;
|
|
int hseg = sh + 1, wlseg = swl + 1;
|
|
double crw, crl, a, ch, twl;
|
|
QVector3D cp(0., 0., -h / 2.);
|
|
points << cp;
|
|
for (int i = 1; i < hseg; i++) {
|
|
ch = -cos((double)i / hseg * M_PI);
|
|
cp.setZ(ch * h / 2.);
|
|
twl = sqrt(1. - ch * ch) / 2.;
|
|
crw = twl * w;
|
|
crl = twl * l;
|
|
for (int j = 0; j < wlseg * 2; j++) {
|
|
a = (double)j / wlseg * M_PI;
|
|
cp.setY(crw * sin(a));
|
|
cp.setX(crl * cos(a));
|
|
points << cp;
|
|
ret = points.size() - 1;
|
|
if (i == 1)
|
|
if (j > 0) putTriangle(points[0], points[ret], points[ret - 1]);
|
|
if (j > 0) {
|
|
if (i > 1) {
|
|
putTriangle(points[ret - wlseg * 2 - 1], points[ret], points[ret - 1]);
|
|
putTriangle(points[ret - wlseg * 2], points[ret], points[ret - wlseg * 2 - 1]);
|
|
}
|
|
}
|
|
}
|
|
if (i == 1) putTriangle(points[0], points[ret - wlseg * 2 + 1], points[ret]);
|
|
else {
|
|
putTriangle(points[ret - wlseg * 2 + 1], points[ret], points[ret - wlseg * 2]);
|
|
putTriangle(points[ret - wlseg * 2 + 1], points[ret - wlseg * 2], points[ret - wlseg * 4 + 1]);
|
|
}
|
|
}
|
|
points << QVector3D(0., 0., h / 2.);
|
|
ret = points.size() - 1;
|
|
putTriangle(points[ret - 1], points[ret - wlseg * 2], points[ret]);
|
|
for (int j = 1; j < wlseg * 2; j++)
|
|
if (j > 0) putTriangle(points[ret - wlseg * 2 + j - 1], points[ret - wlseg * 2 + j], points[ret]);
|
|
is_init = true;
|
|
vbo.rebuffer();
|
|
}
|