-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTexture.cpp
101 lines (91 loc) · 2.3 KB
/
Texture.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
/* ----------------------------------------------------
Tecture.cpp
Clase que interpreta texturas en formato BMP para
aplicarlas a un modelo OBJ.
Marco Fuentes - 18188
Gráficas por computadora - Segundo Semestre 2020 - UVG
---------------------------------------------------- */
#include "Texture.hpp"
#include <iostream>
#include <sstream>
#include <fstream>
#include <string>
#include <stdlib.h>
using namespace std;
Texture::Texture(){
path = "";
}
void Texture::openFile(string path){
this->path = path;
file.open(this->path ,ios::in | ios::binary);
}
void Texture::read(){
string line;
if (file.is_open()){
int headerSize;
file.seekg(10);
file.read((char *)&headerSize, 4);
file.seekg(14+4);
file.read((char *)&width , 4);
file.read((char *)&height, 4);
file.seekg(headerSize);
pixels = new int**[height];
for (int i = 0 ; i < height ; i++){
pixels[i] = new int*[width];
for (int j = 0 ; j < width ; j++){
pixels[i][j] = new int[3];
file.read((char *)&pixels[i][j][0] , 1);
file.read((char *)&pixels[i][j][1] , 1);
file.read((char *)&pixels[i][j][2] , 1);
}
}
file.close();
for (int i = 0 ; i < height ; i++){
for (int j = 0 ; j < width ; j++){
for(int k = 0 ; k < 3 ; k++){
if (pixels[i][j][k] > 255){
pixels[i][j][k] = 255;
}else if(pixels[i][j][k] <0){
pixels[i][j][k] = 0;
}
}
}
}
}else{
cout << "Imposible encontrar archivo: " << path << endl;
cout << "Aniquilando ejecución" << endl;
exit(1);
}
}
int* Texture::getColor(double tx, double ty){
int* result = new int[3];
result[0] = 0;
result[1] = 0;
result[2] = 0;
if (tx >= 0 && tx <=1 && ty >= 0 && ty <= 1){
int x = int(tx*(width-1));
int y = int(ty*(height-1));
result[2] = pixels[y][x][2];//r
result[1] = pixels[y][x][1];//g
result[0] = pixels[y][x][0];//b
}
return result;
};
string Texture::getPath(){
return path;
}
Texture::~Texture(){
for (int i = 0 ; i < height ; i++){
for (int j = 0 ; j < width ; j++){
delete[] pixels[i][j];
}
delete[] pixels[i];
}
if(height > 0 && width > 0) {delete[] pixels;};
};
int Texture::getWidth(){
return width;
}
int Texture::getHeight(){
return height;
}