-
Notifications
You must be signed in to change notification settings - Fork 0
/
EstruturasBasicas.cpp
154 lines (135 loc) · 4.16 KB
/
EstruturasBasicas.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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
#include "EstruturasBasicas.h"
using namespace std;
Ponto::Ponto()
{
z = 0;
}
void Ponto::print()
{
cout << "Ponto: " << x << ", " << y << ", " << z << endl;
}
Cor::Cor()
{
}
Cor::Cor(std::string color_name)
{
// padrão é a cor preta
this->r = 0;
this->g = 0;
this->b = 0;
// preenche caso seja outra cor
if (color_name == "white") {
this->r = this->g = this->b = 255.0;
} else if (color_name == "gray") {
this->r = this->g = this->b = 127.0;
} else if (color_name == "lightgray") {
this->r = this->g = this->b = 210.0;
} else if (color_name == "green") {
this->g = 255.0;
} else if (color_name == "red") {
this->r = 255.0;
} else if (color_name == "blue") {
this->b = 255.0;
} else if (color_name == "lightgreen") {
this->r = 155.0;
this->g = 187.0;
this->b = 89.0;
} else if (color_name == "darkgreen") {
this->r = 99.0;
this->g = 169.0;
this->b = 117.0;
} else if (color_name == "lightblue") {
this->r = 79.0;
this->g = 129.0;
this->b = 189.0;
} else if (color_name == "darkred") {
this->r = 170;
this->g = 0.0;
this->b = 0.0;
}
// normaliza para o intervalo [0,1]
this->r /= 255.0;
this->g /= 255.0;
this->b /= 255.0;
}
Cor::Cor(int _r, int _g, int _b)
{
this->r = (float)_r/255.0;
this->g = (float)_g/255.0;
this->b = (float)_b/255.0;
}
double calculaDistancia(Ponto p1, Ponto p2)
{
return sqrt((p2.x - p1.x) * (p2.x - p1.x) + (p2.y - p1.y) * (p2.y - p1.y) + (p2.z - p1.z) * (p2.z - p1.z));
}
Textura::Textura()
{
}
Textura::Textura(std::string texture_name)
{
string arquivo = "images/" + texture_name;
this->t = this->LoadTextureRAW(arquivo.c_str());
}
GLuint Textura::LoadTextureRAW(const char * filename)
{
GLuint texture;
Image* image = loadBMP(filename);
glGenTextures( 1, &texture );
glBindTexture( GL_TEXTURE_2D, texture );
glTexEnvf( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE,GL_MODULATE );
glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER,GL_LINEAR );
glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER,GL_LINEAR );
glTexImage2D(GL_TEXTURE_2D, //Always GL_TEXTURE_2D
0, //0 for now
GL_RGB, //Format OpenGL uses for image
image->width, image->height, //Width and height
0, //The border of the image
GL_RGB, //GL_RGB, because pixels are stored in RGB format
GL_UNSIGNED_BYTE, //GL_UNSIGNED_BYTE, because pixels are stored
//as unsigned numbers
image->pixels); //The actual pixel data
delete image;
return texture;
}
void drawBox(GLfloat tamanho, GLfloat textureS)
{
static GLfloat n[6][3] =
{
{-1.0, 0.0, 0.0},
{0.0, 1.0, 0.0},
{1.0, 0.0, 0.0},
{0.0, -1.0, 0.0},
{0.0, 0.0, 1.0},
{0.0, 0.0, -1.0}
};
static GLint faces[6][4] =
{
{0, 1, 2, 3},
{3, 2, 6, 7},
{7, 6, 5, 4},
{4, 5, 1, 0},
{5, 6, 2, 1},
{7, 4, 0, 3}
};
GLfloat v[8][3];
GLint i;
v[0][0] = v[1][0] = v[2][0] = v[3][0] = -tamanho / 2;
v[4][0] = v[5][0] = v[6][0] = v[7][0] = tamanho / 2;
v[0][1] = v[1][1] = v[4][1] = v[5][1] = -tamanho / 2;
v[2][1] = v[3][1] = v[6][1] = v[7][1] = tamanho / 2;
v[0][2] = v[3][2] = v[4][2] = v[7][2] = -tamanho / 2;
v[1][2] = v[2][2] = v[5][2] = v[6][2] = tamanho / 2;
for (i = 5; i >= 0; i--) {
glBegin(GL_POLYGON);
glNormal3fv(&n[i][0]);
glTexCoord2f (textureS, 0);
glVertex3fv(&v[faces[i][0]][0]);
glTexCoord2f (textureS, textureS);
glVertex3fv(&v[faces[i][1]][0]);
glTexCoord2f (0, textureS);
glVertex3fv(&v[faces[i][2]][0]);
glTexCoord2f (0, 0);
glVertex3fv(&v[faces[i][3]][0]);
glEnd();
}
}