forked from SurajSharma90/OpenGL-C---Tutorials
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathOBJLoader.h
More file actions
150 lines (127 loc) · 2.9 KB
/
OBJLoader.h
File metadata and controls
150 lines (127 loc) · 2.9 KB
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
#pragma once
//STD Libs
#include<iostream>
#include<string>
#include<fstream>
#include<vector>
#include<sstream>
#include<algorithm>
//GLEW
#include<glew.h>
//GLFW
#include<glfw3.h>
//OpenGL Math libs
#include<glm.hpp>
#include<vec3.hpp>
#include<vec4.hpp>
#include<mat4x4.hpp>
#include<gtc/matrix_transform.hpp>
#include<gtc/type_ptr.hpp>
//Own libs
#include"Vertex.h"
static std::vector<Vertex> loadOBJ(const char* file_name)
{
//Vertex portions
std::vector<glm::fvec3> vertex_positions;
std::vector<glm::fvec2> vertex_texcoords;
std::vector<glm::fvec3> vertex_normals;
//Face vectors
std::vector<GLint> vertex_position_indicies;
std::vector<GLint> vertex_texcoord_indicies;
std::vector<GLint> vertex_normal_indicies;
//Vertex array
std::vector<Vertex> vertices;
std::stringstream ss;
std::ifstream in_file(file_name);
std::string line = "";
std::string prefix = "";
glm::vec3 temp_vec3;
glm::vec2 temp_vec2;
GLint temp_glint = 0;
//File open error check
if (!in_file.is_open())
{
throw "ERROR::OBJLOADER::Could not open file.";
}
//Read one line at a time
while (std::getline(in_file, line))
{
//Get the prefix of the line
ss.clear();
ss.str(line);
ss >> prefix;
if (prefix == "#")
{
}
else if (prefix == "o")
{
}
else if (prefix == "s")
{
}
else if (prefix == "use_mtl")
{
}
else if (prefix == "v") //Vertex position
{
ss >> temp_vec3.x >> temp_vec3.y >> temp_vec3.z;
vertex_positions.push_back(temp_vec3);
}
else if (prefix == "vt")
{
ss >> temp_vec2.x >> temp_vec2.y;
vertex_texcoords.push_back(temp_vec2);
}
else if (prefix == "vn")
{
ss >> temp_vec3.x >> temp_vec3.y >> temp_vec3.z;
vertex_normals.push_back(temp_vec3);
}
else if (prefix == "f")
{
int counter = 0;
while (ss >> temp_glint)
{
//Pushing indices into correct arrays
if (counter == 0)
vertex_position_indicies.push_back(temp_glint);
else if (counter == 1)
vertex_texcoord_indicies.push_back(temp_glint);
else if (counter == 2)
vertex_normal_indicies.push_back(temp_glint);
//Handling characters
if (ss.peek() == '/')
{
++counter;
ss.ignore(1, '/');
}
else if (ss.peek() == ' ')
{
++counter;
ss.ignore(1, ' ');
}
//Reset the counter
if (counter > 2)
counter = 0;
}
}
else
{
}
}
//Build final vertex array (mesh)
vertices.resize(vertex_position_indicies.size(), Vertex());
//Load in all indices
for (size_t i = 0; i < vertices.size(); ++i)
{
vertices[i].position = vertex_positions[vertex_position_indicies[i] - 1];
vertices[i].texcoord = vertex_texcoords[vertex_texcoord_indicies[i] - 1];
vertices[i].normal = vertex_normals[vertex_normal_indicies[i] - 1];
vertices[i].color = glm::vec3(1.f, 1.f, 1.f);
}
//DEBUG
std::cout << "Nr of vertices: " << vertices.size() << "\n";
//Loaded success
std::cout << "OBJ file loaded!" << "\n";
return vertices;
}