aboutsummaryrefslogtreecommitdiff
path: root/src/ObjModel.cpp
blob: 8933da8572e19afae5f05262d14335f2c5bb3b7a (plain) (blame)
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
#include "ObjModel.h"



ObjModel::ObjModel() {}

ObjModel::ObjModel(std::string fileName)
{
	std::chrono::high_resolution_clock::time_point time1 = std::chrono::high_resolution_clock::now();

	int hlp = 0;
	std::ifstream f(fileName);
  if (f.is_open()) {
    std::string s0;
    while (getline(f, s0)) {
      std::stringstream ss0(s0);
      std::string s1 = "";
      ss0 >> s1;
      if (s1.compare(0, s1.length(), "v") == 0) {
        ss0 >> s1;
        float x = std::stof(s1);
        ss0 >> s1;
        float y = std::stof(s1);
        ss0 >> s1;
        float z = std::stof(s1);
        Vertice t(x, y, z, 1.0f, 0.0f, 0.0f);
        vertices.push_back(t);
      }
      else if (s1.compare(0, s1.length(), "vn") == 0) {
        ss0 >> s1;
        float x = std::stof(s1);
        ss0 >> s1;
        float y = std::stof(s1);
        ss0 >> s1;
        float z = std::stof(s1);
        Vector4f v(x, y, z, 0.0f);
        normals.push_back(v);
      }
      else if (s1.compare(0, s1.length(), "vt") == 0) {
        ss0 >> s1;
        float u = std::stof(s1);
        ss0 >> s1;
        float v = std::stof(s1);
        Texture_Coordinates kt;
        kt.u = u;	kt.v = v;
        uvcoo.push_back(kt);
      }
      else if (s1.compare(0, s1.length(), "f") == 0) {
        Triangle pt;
        for (int i = 0; i < 3; i++) {
          ss0 >> s1;
          std::stringstream ss1(s1);
          std::string s2;
          for (int j = 0; std::getline(ss1, s2, '/'); j++) {
            pt.p[i][j] = std::stoi(s2);
          }
        }
        faces.push_back(pt);
      }
    }
    isLoaded = true;
  }
  else {
    isLoaded = false;
  }

	std::chrono::high_resolution_clock::time_point time2 = std::chrono::high_resolution_clock::now();
	loadingTime = std::chrono::duration_cast<std::chrono::duration<double>>(time2 - time1);
}

ObjModel::ObjModel(const ObjModel& a) 
{
	vertices = a.vertices;
	uvcoo = a.uvcoo;
	normals = a.normals;
	faces = a.faces;
}

ObjModel ObjModel::multiplyMatrix(Matrix4f& a) 
{
	ObjModel hlp = *this;
	for (int i = 0; i < vertices.size(); i++)
	{
		hlp.vertices[i] = hlp.vertices[i].multiplyMatrix(a);
	}
	return hlp;
}

void ObjModel::divideW() 
{
	for (int i = 0; i < vertices.size(); i++)
	{
		vertices[i] = vertices[i].divideW();
	}
}

void ObjModel::screenspace(int width, int height)
{
	for (int i = 0; i < vertices.size(); i++)
	{
		vertices[i].screenspace(width, height);
	}
}