-
Notifications
You must be signed in to change notification settings - Fork 0
/
Cylinder.cpp
76 lines (67 loc) · 2.11 KB
/
Cylinder.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
#include "Cylinder.h"
#include <math.h>
Cylinder::Cylinder(int slices, int stacks, bool smooth) {
this->slices = slices;
this->stacks = stacks;
this->smooth = smooth;
}
void Cylinder::draw()
{
long float pi = acos(-1.0);
long float angulo = 360.0/ slices;
float ki,kf;
//Base z-
glPushMatrix();
glTranslatef(0,0,-0.5);
glRotated(180,1,0,0);
glBegin(GL_POLYGON);
for(int i = 0; i < slices; ++i) {
glNormal3f(0,0,1);
glTexCoord2f((cos(angulo*(i)*pi/180)+1)/2,(sin(angulo*(i)*pi/180)+1)/2);
glVertex3f(cos(angulo*(i)*pi/180),sin(angulo*(i)*pi/180),0);
}
glEnd();
glPopMatrix();
glPushMatrix();
glTranslatef(0,0,-0.5);
glBegin(GL_QUADS);
for(int i = 0; i < slices; i++) {
ki=0.0;
kf=1.0/(float)stacks;
for(int j=0; j < stacks; j++){
if(!smooth)
glNormal3f((cos(angulo*(i)*pi/180)+cos(angulo*(i+1)*pi/180))/2.0,(sin(angulo*(i)*pi/180)+sin(angulo*(i+1)*pi/180))/2.0,0);
if(smooth)
glNormal3f(cos(angulo*(i)*pi/180),sin(angulo*(i)*pi/180),0);
glTexCoord2f(cos(angulo*(i)*pi/180), ki);
glVertex3f(cos(angulo*(i)*pi/180),sin(angulo*(i)*pi/180),ki);
if(smooth)
glNormal3f(cos(angulo*(i+1)*pi/180),sin(angulo*(i+1)*pi/180),0);
glTexCoord2f(cos(angulo*(i+1)*pi/180), ki);
glVertex3f(cos(angulo*(i+1)*pi/180),sin(angulo*(i+1)*pi/180),ki);
if(smooth)
glNormal3f(cos(angulo*(i+1)*pi/180),sin(angulo*(i+1)*pi/180),0);
glTexCoord2f(cos(angulo*(i+1)*pi/180), kf);
glVertex3f(cos(angulo*(i+1)*pi/180),sin(angulo*(i+1)*pi/180),kf);
if(smooth)
glNormal3f(cos(angulo*(i)*pi/180),sin(angulo*(i)*pi/180),0);
glTexCoord2f(cos(angulo*(i)*pi/180), kf);
glVertex3f(cos(angulo*(i)*pi/180),sin(angulo*(i)*pi/180),kf);
ki=kf;
kf+=1.0/(float)stacks;
}
}
glEnd();
glPopMatrix();
//Base z+
glPushMatrix();
glTranslatef(0,0,0.5);
glBegin(GL_POLYGON);
for(int i = 0; i < slices; ++i) {
glNormal3f(0,0,1);
glTexCoord2f((cos(angulo*(i)*pi/180)+1.0)/2.0,(sin(angulo*(i)*pi/180)+1.0)/2.0);
glVertex3f(cos(angulo*(i)*pi/180),sin(angulo*(i)*pi/180),0);
}
glEnd();
glPopMatrix();
}