105 lines
2.3 KiB
C++
105 lines
2.3 KiB
C++
#include <cylinder.h>
|
|
#include <QDebug>
|
|
|
|
using namespace std;
|
|
|
|
|
|
static void push_vertex(vector<GLfloat> &verts,
|
|
GLfloat x, GLfloat y, GLfloat z,
|
|
Color c) {
|
|
verts.push_back(x);
|
|
verts.push_back(y);
|
|
verts.push_back(z);
|
|
verts.push_back(c.r);
|
|
verts.push_back(c.g);
|
|
verts.push_back(c.b);
|
|
}
|
|
|
|
|
|
Cylinder::Cylinder(float ep, float r, unsigned nb_fac, Color c)
|
|
:ep(ep),
|
|
r(r),
|
|
nb_fac(nb_fac),
|
|
c(c),
|
|
angle(2*M_PI/nb_fac),
|
|
face_size(nb_fac * 3),
|
|
side_size((nb_fac+1) * 2)
|
|
{
|
|
build_faces(verts);
|
|
build_side(verts);
|
|
vbo.create();
|
|
qDebug() << "verts.size() " << verts.size()
|
|
<< "nb_fac:" << nb_fac
|
|
<< "face_size:" << face_size
|
|
<< "side_size:" << side_size;
|
|
vbo.bind();
|
|
vbo.allocate(verts.data(), verts.size() * sizeof (GLfloat));
|
|
vbo.release();
|
|
qDebug() << verts;
|
|
}
|
|
|
|
|
|
Cylinder::~Cylinder() {
|
|
vbo.destroy();
|
|
}
|
|
|
|
|
|
|
|
void Cylinder::build_face(float z, vector<GLfloat> &verts)
|
|
{
|
|
Color color(c);
|
|
for (unsigned j = 0; j < nb_fac; j++) {
|
|
push_vertex(verts, 0, 0, z, color);
|
|
push_vertex(verts, cos(angle*j) * r, sin(angle*j) * r, z, color);
|
|
push_vertex(verts, cos(angle*(j+1)) * r, sin(angle*(j+1)) * r, z, color);
|
|
if (j % (nb_fac/4) == 0) {
|
|
if (j % (nb_fac/2) == 0) {
|
|
color.r *= .8;
|
|
color.g *= .8;
|
|
color.b *= .8;
|
|
}
|
|
else {
|
|
color = c;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
void Cylinder::build_faces(vector<GLfloat> &verts)
|
|
{
|
|
build_face(-ep/2, verts);
|
|
build_face(ep/2, verts);
|
|
}
|
|
|
|
|
|
void Cylinder::build_side(vector<GLfloat> &verts)
|
|
{
|
|
Color color(c.r*.7, c.g*.7, c.b*.7);
|
|
for (unsigned i = 0; i < nb_fac+1; i++) {
|
|
push_vertex(verts, cos(angle*i) * r, sin(angle*i) * r, -ep/2, color);
|
|
push_vertex(verts, cos(angle*i) * r, sin(angle*i) * r, ep/2, color);
|
|
}
|
|
}
|
|
|
|
|
|
void Cylinder::draw(QOpenGLFunctions &f, int pos, int col)
|
|
{
|
|
// vbo.bind();
|
|
f.glBindBuffer(GL_ARRAY_BUFFER, vbo.bufferId());
|
|
f.glVertexAttribPointer(pos, 3, GL_FLOAT, GL_FALSE,
|
|
6 * sizeof (GLfloat), verts.data());
|
|
f.glVertexAttribPointer(col, 3, GL_FLOAT, GL_FALSE,
|
|
6 * sizeof (GLfloat), verts.data() + 3);
|
|
f.glEnableVertexAttribArray(pos);
|
|
f.glEnableVertexAttribArray(col);
|
|
f.glDrawArrays(GL_TRIANGLES, 0, face_size);
|
|
f.glDrawArrays(GL_TRIANGLES, face_size, face_size);
|
|
f.glDrawArrays(GL_QUAD_STRIP,
|
|
face_size * 2,
|
|
side_size);
|
|
f.glDisableVertexAttribArray(pos);
|
|
f.glDisableVertexAttribArray(col);
|
|
// vbo.release();
|
|
}
|