This repository has been archived on 2020-03-08. You can view files and clone it, but cannot push or open issues or pull requests.
pg-tp3/cylinder.cpp

69 lines
1.4 KiB
C++

#include <cylinder.h>
#include <stdio.h>
void VertexBuf::push(GLfloat x, GLfloat y, GLfloat z, Color &c)
{
vertices[i] = x;
vertices[i+1] = y;
vertices[i+2] = z;
colors[i] = c.r;
colors[i+1] = c.g;
colors[i+2] = c.b;
i += 3;
}
void Cylinder::build_face(float z)
{
Color color(c);
for (unsigned j = 0; j < nb_fac; j++) {
vb.push(0, 0, z, color);
vb.push(cos(angle*j) * r, sin(angle*j) * r, z, color);
vb.push(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()
{
build_face(-ep/2);
build_face(ep/2);
}
void Cylinder::build_side()
{
Color color(c.r*.7, c.g*.7, c.b*.7);
for (unsigned i = 0; i < nb_fac+1; i++) {
vb.push(cos(angle*i) * r, sin(angle*i) * r, -ep/2, color);
vb.push(cos(angle*i) * r, sin(angle*i) * r, ep/2, color);
}
}
void Cylinder::draw(QOpenGLFunctions &f, int pos, int col)
{
f.glVertexAttribPointer(pos, 3, GL_FLOAT, GL_FALSE, 0, vb.vertices);
f.glVertexAttribPointer(col, 3, GL_FLOAT, GL_FALSE, 0, vb.colors);
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);
}