-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodel.cpp
101 lines (88 loc) · 2.25 KB
/
model.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
#pragma once
#include <iostream>
#include <vector>
#include <string>
#include "model.h"
#include <array>
#include <boost/algorithm/string.hpp>
Model::Model() {
wireframe_pts = new std::vector<std::array<float, 3>>;
texture_pts = new std::vector<std::array<float, 3>>;
normal_pts = new std::vector<std::array<float, 3>>;
faces = new std::vector<std::array<std::string, 3>>;
texture = new Image(1, 1, Image::RGBA);
normal_map = new Image(1, 1, Image::RGBA);
}
bool Model::load_model(const char* filename) {
float coord;
std::string line;
std::vector<std::string> split;
std::array<float, 3> vertex;
std::array<std::string, 3> face;
std::ifstream file(filename);
if (file.is_open()) {
while (std::getline(file, line)) {
boost::split(split, line, [](char c) {return c == ' '; });
if (split[0] == "v") {
for (unsigned int i = 1; i < split.size(); i++) {
coord = std::stof(split[i]);
vertex[i - 1] = coord;
}
(*wireframe_pts).push_back(vertex);
}
else if (split[0] == "vt") {
for (unsigned int i = 2; i < split.size(); i++) {
coord = std::stof(split[i]);
vertex[i - 2] = coord;
}
(*texture_pts).push_back(vertex);
}
else if (split[0] == "vn") {
for (unsigned int i = 2; i < split.size(); i++) {
coord = std::stof(split[i]);
vertex[i - 2] = coord;
}
(*normal_pts).push_back(vertex);
}
else if (split[0] == "f") {
for (unsigned int i = 1; i < split.size(); i++) {
face[i - 1] = split[i];
}
(*faces).push_back(face);
}
}
file.close();
}
return true;
}
bool Model::load_texture(const char* filename) {
texture->read_png_file(filename);
texture->flip_vertically();
return true;
}
bool Model::load_normal_map(const char* filename) {
normal_map->read_png_file(filename);
normal_map->flip_vertically();
return true;
}
int Model::get_size() {
return faces->size();
}
std::array<std::string, 3> Model::face(int i) {
return (*faces)[i];
}
std::array<float, 3> Model::vertex(int i) {
return (*wireframe_pts)[i];
}
std::array<float, 3> Model::tx_vertex(int i) {
return (*texture_pts)[i];
}
std::array<float, 3> Model::norm_vertex(int i) {
return (*normal_pts)[i];
}
Image* Model::get_texture() {
return texture;
}
Image* Model::get_normal_map() {
return normal_map;
}