progrès
This commit is contained in:
parent
8281c6f6db
commit
ba711d53fc
22
cylinder.cpp
22
cylinder.cpp
@ -25,7 +25,6 @@ Cylinder::Cylinder(float ep, float r, unsigned nb_fac, Color c)
|
|||||||
face_size(nb_fac * 3),
|
face_size(nb_fac * 3),
|
||||||
side_size((nb_fac+1) * 2)
|
side_size((nb_fac+1) * 2)
|
||||||
{
|
{
|
||||||
vector<GLfloat> verts((2*face_size + side_size) * 3 * 2);
|
|
||||||
build_faces(verts);
|
build_faces(verts);
|
||||||
build_side(verts);
|
build_side(verts);
|
||||||
vbo.create();
|
vbo.create();
|
||||||
@ -36,6 +35,12 @@ Cylinder::Cylinder(float ep, float r, unsigned nb_fac, Color c)
|
|||||||
vbo.bind();
|
vbo.bind();
|
||||||
vbo.allocate(verts.data(), verts.size() * sizeof (GLfloat));
|
vbo.allocate(verts.data(), verts.size() * sizeof (GLfloat));
|
||||||
vbo.release();
|
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)
|
void Cylinder::draw(QOpenGLFunctions &f, int pos, int col)
|
||||||
{
|
{
|
||||||
vbo.bind();
|
// vbo.bind();
|
||||||
f.glEnableVertexAttribArray(pos);
|
f.glBindBuffer(GL_ARRAY_BUFFER, vbo.bufferId());
|
||||||
f.glVertexAttribPointer(pos, 3, GL_FLOAT, GL_FALSE,
|
f.glVertexAttribPointer(pos, 3, GL_FLOAT, GL_FALSE,
|
||||||
3 * sizeof (GLfloat),
|
6 * sizeof (GLfloat), verts.data());
|
||||||
nullptr);
|
|
||||||
f.glEnableVertexAttribArray(col);
|
|
||||||
f.glVertexAttribPointer(col, 3, GL_FLOAT, GL_FALSE,
|
f.glVertexAttribPointer(col, 3, GL_FLOAT, GL_FALSE,
|
||||||
3 * sizeof (GLfloat),
|
6 * sizeof (GLfloat), verts.data() + 3);
|
||||||
(void *) (3 * sizeof (GLfloat)));
|
f.glEnableVertexAttribArray(pos);
|
||||||
|
f.glEnableVertexAttribArray(col);
|
||||||
f.glDrawArrays(GL_TRIANGLES, 0, face_size);
|
f.glDrawArrays(GL_TRIANGLES, 0, face_size);
|
||||||
f.glDrawArrays(GL_TRIANGLES, face_size, face_size);
|
f.glDrawArrays(GL_TRIANGLES, face_size, face_size);
|
||||||
f.glDrawArrays(GL_QUAD_STRIP,
|
f.glDrawArrays(GL_QUAD_STRIP,
|
||||||
@ -96,5 +100,5 @@ void Cylinder::draw(QOpenGLFunctions &f, int pos, int col)
|
|||||||
side_size);
|
side_size);
|
||||||
f.glDisableVertexAttribArray(pos);
|
f.glDisableVertexAttribArray(pos);
|
||||||
f.glDisableVertexAttribArray(col);
|
f.glDisableVertexAttribArray(col);
|
||||||
vbo.release();
|
// vbo.release();
|
||||||
}
|
}
|
||||||
|
@ -30,9 +30,11 @@ struct Cylinder
|
|||||||
size_t face_size;
|
size_t face_size;
|
||||||
size_t side_size;
|
size_t side_size;
|
||||||
|
|
||||||
|
std::vector<GLfloat> verts;
|
||||||
QOpenGLBuffer vbo;
|
QOpenGLBuffer vbo;
|
||||||
|
|
||||||
Cylinder(float ep, float r, unsigned nb_fac, Color c);
|
Cylinder(float ep, float r, unsigned nb_fac, Color c);
|
||||||
|
~Cylinder();
|
||||||
|
|
||||||
void build_face(float z, std::vector<GLfloat> &verts);
|
void build_face(float z, std::vector<GLfloat> &verts);
|
||||||
void build_faces(std::vector<GLfloat> &verts);
|
void build_faces(std::vector<GLfloat> &verts);
|
||||||
|
@ -76,7 +76,7 @@ void GLArea::resizeGL(int w, int h)
|
|||||||
void GLArea::paintGL()
|
void GLArea::paintGL()
|
||||||
{
|
{
|
||||||
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
|
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
|
||||||
program->bind(); // active le shader program
|
program->bind();
|
||||||
|
|
||||||
QMatrix4x4 matrix;
|
QMatrix4x4 matrix;
|
||||||
GLfloat hr = focal_length;
|
GLfloat hr = focal_length;
|
||||||
|
78
scene.cpp
78
scene.cpp
@ -6,86 +6,14 @@ using namespace std;
|
|||||||
|
|
||||||
|
|
||||||
Scene::Scene()
|
Scene::Scene()
|
||||||
:volant(.2, .55, 8, Color(.522, .816, 1)),
|
: cylindre(1, 1, 4, Color(.702, .424, .286)) {}
|
||||||
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)) {}
|
|
||||||
|
|
||||||
|
|
||||||
void Scene::draw(QOpenGLFunctions &f, QOpenGLShaderProgram *prog, int mat_uni,
|
void Scene::draw(QOpenGLFunctions &f, QOpenGLShaderProgram *prog, int mat_uni,
|
||||||
float alpha, QMatrix4x4 &matrix, int pos, int col)
|
float alpha, QMatrix4x4 &matrix, int pos, int col)
|
||||||
{
|
{
|
||||||
float gh = .45;
|
// matrix.rotate(70, 1, 0, 0);
|
||||||
float xh = gh*cos(alpha);
|
// matrix.rotate(15, 0, 0, 1);
|
||||||
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);
|
prog->setUniformValue(mat_uni, matrix);
|
||||||
cylindre.draw(f, pos, col);
|
cylindre.draw(f, pos, col);
|
||||||
matrix = m;
|
|
||||||
}
|
}
|
||||||
|
9
scene.h
9
scene.h
@ -15,15 +15,6 @@ public:
|
|||||||
float alpha, QMatrix4x4 &matrix, int vertices, int colors);
|
float alpha, QMatrix4x4 &matrix, int vertices, int colors);
|
||||||
|
|
||||||
private:
|
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;
|
Cylinder cylindre;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user