From ba711d53fcab7876083f850595936da5d4262c74 Mon Sep 17 00:00:00 2001 From: ccolin Date: Mon, 9 Mar 2020 08:45:43 +0100 Subject: [PATCH] =?UTF-8?q?progr=C3=A8s?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- cylinder.cpp | 22 ++++++++------ cylinder.h | 2 ++ glarea.cpp | 2 +- scene.cpp | 82 ++++------------------------------------------------ scene.h | 9 ------ 5 files changed, 21 insertions(+), 96 deletions(-) diff --git a/cylinder.cpp b/cylinder.cpp index 370b145..988b11a 100644 --- a/cylinder.cpp +++ b/cylinder.cpp @@ -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 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 &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(); } diff --git a/cylinder.h b/cylinder.h index 643b109..97f5119 100644 --- a/cylinder.h +++ b/cylinder.h @@ -30,9 +30,11 @@ struct Cylinder size_t face_size; size_t side_size; + std::vector verts; QOpenGLBuffer vbo; Cylinder(float ep, float r, unsigned nb_fac, Color c); + ~Cylinder(); void build_face(float z, std::vector &verts); void build_faces(std::vector &verts); diff --git a/glarea.cpp b/glarea.cpp index 2356791..e7e6431 100644 --- a/glarea.cpp +++ b/glarea.cpp @@ -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; diff --git a/scene.cpp b/scene.cpp index 87a75c4..adb3ad0 100644 --- a/scene.cpp +++ b/scene.cpp @@ -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); } diff --git a/scene.h b/scene.h index 6aa5687..439312e 100644 --- a/scene.h +++ b/scene.h @@ -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; };