This commit is contained in:
ccolin 2020-03-09 08:45:43 +01:00
parent 8281c6f6db
commit ba711d53fc
5 changed files with 21 additions and 96 deletions

View File

@ -25,7 +25,6 @@ Cylinder::Cylinder(float ep, float r, unsigned nb_fac, Color c)
face_size(nb_fac * 3),
side_size((nb_fac+1) * 2)
{
vector<GLfloat> verts((2*face_size + side_size) * 3 * 2);
build_faces(verts);
build_side(verts);
vbo.create();
@ -36,6 +35,12 @@ Cylinder::Cylinder(float ep, float r, unsigned nb_fac, Color c)
vbo.bind();
vbo.allocate(verts.data(), verts.size() * sizeof (GLfloat));
vbo.release();
qDebug() << verts;
}
Cylinder::~Cylinder() {
vbo.destroy();
}
@ -80,15 +85,14 @@ void Cylinder::build_side(vector<GLfloat> &verts)
void Cylinder::draw(QOpenGLFunctions &f, int pos, int col)
{
vbo.bind();
f.glEnableVertexAttribArray(pos);
// vbo.bind();
f.glBindBuffer(GL_ARRAY_BUFFER, vbo.bufferId());
f.glVertexAttribPointer(pos, 3, GL_FLOAT, GL_FALSE,
3 * sizeof (GLfloat),
nullptr);
f.glEnableVertexAttribArray(col);
6 * sizeof (GLfloat), verts.data());
f.glVertexAttribPointer(col, 3, GL_FLOAT, GL_FALSE,
3 * sizeof (GLfloat),
(void *) (3 * sizeof (GLfloat)));
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,
@ -96,5 +100,5 @@ void Cylinder::draw(QOpenGLFunctions &f, int pos, int col)
side_size);
f.glDisableVertexAttribArray(pos);
f.glDisableVertexAttribArray(col);
vbo.release();
// vbo.release();
}

View File

@ -30,9 +30,11 @@ struct Cylinder
size_t face_size;
size_t side_size;
std::vector<GLfloat> verts;
QOpenGLBuffer vbo;
Cylinder(float ep, float r, unsigned nb_fac, Color c);
~Cylinder();
void build_face(float z, std::vector<GLfloat> &verts);
void build_faces(std::vector<GLfloat> &verts);

View File

@ -76,7 +76,7 @@ void GLArea::resizeGL(int w, int h)
void GLArea::paintGL()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
program->bind(); // active le shader program
program->bind();
QMatrix4x4 matrix;
GLfloat hr = focal_length;

View File

@ -6,86 +6,14 @@ using namespace std;
Scene::Scene()
:volant(.2, .55, 8, Color(.522, .816, 1)),
axe_volant(.6, .06, 6, Color(.522*.5, .816*.5, 1.5)),
manivelle(.2, .115, 8, Color(.078, .718, .078)),
axe_manivelle(.35, .06, 6, Color(.078*.5, .718*.5, .078*.5)),
bague_bielle(.2, .14, 8, Color(.078, .718, .078)),
bague_piston(.2, .115, 8, Color(.69, .11, .69)),
axe_bielle_piston(.36, .06, 6, Color(.69*.5, .11*.5, .69*.5)),
bielle(1.5, .05, 6, Color(.173, .824, .173)),
piston(1.1, .065, 6, Color(.847, .318, .847)),
cylindre(1.2, .15, 8, Color(.702, .424, .286)) {}
: cylindre(1, 1, 4, Color(.702, .424, .286)) {}
void Scene::draw(QOpenGLFunctions &f, QOpenGLShaderProgram *prog, int mat_uni,
float alpha, QMatrix4x4 &matrix, int pos, int col)
{
float gh = .45;
float xh = gh*cos(alpha);
float yh = gh*sin(alpha);
QMatrix4x4 m, m2;
/* Volant */
m = matrix;
matrix.rotate(alpha*360/(2*M_PI), 0, 0, 1);
prog->setUniformValue(mat_uni, matrix);
volant.draw(f, pos, col);
m2 = matrix;
matrix.translate(0, 0, -axe_volant.ep/2 + .125);
prog->setUniformValue(mat_uni, matrix);
axe_volant.draw(f, pos, col);
matrix = m2;
matrix.translate(gh, 0, axe_manivelle.ep/2);
prog->setUniformValue(mat_uni, matrix);
axe_manivelle.draw(f, pos, col);
matrix = m;
/* Manivelle */
m = matrix;
matrix.translate(xh, yh, volant.ep/2+manivelle.ep/2);
prog->setUniformValue(mat_uni, matrix);
manivelle.draw(f, pos, col);
matrix = m;
float hj = bielle.ep;
float xj = xh - sqrtf(hj*hj - powf(gh*sin(alpha), 2));
float b = atanf(yh / (xj-xh < 0 ? xj-xh : -xj+xh));
m = matrix;
matrix.translate(xj, 0, volant.ep/2+manivelle.ep/2);
prog->setUniformValue(mat_uni, matrix);
bague_bielle.draw(f, pos, col);
/* Bielle */
m2 = matrix;
matrix.rotate(90, 0, 1, 0);
matrix.rotate(b*360/(2*M_PI), 1, 0, 0);
matrix.translate(0, 0, bielle.ep/2);
prog->setUniformValue(mat_uni, matrix);
bielle.draw(f, pos, col);
matrix = m2;
/* Piston */
m2 = matrix;
matrix.translate(0, 0, axe_bielle_piston.ep/2);
prog->setUniformValue(mat_uni, matrix);
axe_bielle_piston.draw(f, pos, col);
matrix.translate(0, 0, axe_bielle_piston.ep/2 - .15);
prog->setUniformValue(mat_uni, matrix);
bague_piston.draw(f, pos, col);
matrix.rotate(90, 0, 1, 0);
matrix.translate(0, 0, -piston.ep/2);
prog->setUniformValue(mat_uni, matrix);
piston.draw(f, pos, col);
matrix = m2;
matrix = m;
/* Cylindre */
m = matrix;
matrix.translate(-gh-bielle.ep - piston.ep + .3, 0, volant.ep/2
+ bague_bielle.ep
+ bague_piston.ep/2);
matrix.rotate(90, 0, 1, 0);
prog->setUniformValue(mat_uni, matrix);
cylindre.draw(f, pos, col);
matrix = m;
// matrix.rotate(70, 1, 0, 0);
// matrix.rotate(15, 0, 0, 1);
prog->setUniformValue(mat_uni, matrix);
cylindre.draw(f, pos, col);
}

View File

@ -15,15 +15,6 @@ public:
float alpha, QMatrix4x4 &matrix, int vertices, int colors);
private:
Cylinder volant;
Cylinder axe_volant;
Cylinder manivelle;
Cylinder axe_manivelle;
Cylinder bague_bielle;
Cylinder bague_piston;
Cylinder axe_bielle_piston;
Cylinder bielle;
Cylinder piston;
Cylinder cylindre;
};