Расчет касательного и бикасательного векторов при загрузке модели
This commit is contained in:
		
							parent
							
								
									cffa5087d2
								
							
						
					
					
						commit
						477293a4e8
					
				| @ -13,6 +13,8 @@ | |||||||
| 
 | 
 | ||||||
| class Model genShpere(float radius, int sectorsCount, class Node* parent = NULL); // Генерирует сферу заданного радиуса с определенным количеством сегментов
 | class Model genShpere(float radius, int sectorsCount, class Node* parent = NULL); // Генерирует сферу заданного радиуса с определенным количеством сегментов
 | ||||||
| 
 | 
 | ||||||
|  | void calc_tb(const GLuint* indices, const int indices_count, const glm::vec3* verteces, const glm::vec2* texCords, glm::vec3* tangent, glm::vec3* bitangent); // Расчет касательных и бикасательных векторов
 | ||||||
|  | 
 | ||||||
| // Класс узла сцены
 | // Класс узла сцены
 | ||||||
| class Node  | class Node  | ||||||
| { | { | ||||||
|  | |||||||
| @ -537,3 +537,44 @@ Model genShpere(float radius, int sectorsCount, Node* parent) | |||||||
| 
 | 
 | ||||||
|     return result; |     return result; | ||||||
| } | } | ||||||
|  | 
 | ||||||
|  | // Расчет касательных и бикасательных векторов
 | ||||||
|  | void calc_tb(const GLuint* indices, const int indices_count, const glm::vec3* verteces, const glm::vec2* texCords, glm::vec3* tangent, glm::vec3* bitangent) | ||||||
|  | { | ||||||
|  |     glm::vec2 dTex1, dTex2; // Разница по текстурным координатам
 | ||||||
|  |     glm::vec3 dPos1, dPos2; // Разница по координатам вершин
 | ||||||
|  |     float f; // Разность произведений
 | ||||||
|  |     glm::vec3 tmp; // Для вычислений вектора
 | ||||||
|  | 
 | ||||||
|  |     for (int i = 0; i < indices_count; i+=3) | ||||||
|  |     { | ||||||
|  |         // Разности векторов
 | ||||||
|  |         dTex1 = texCords[indices[i+1]] - texCords[indices[i]]; | ||||||
|  |         dTex2 = texCords[indices[i+2]] - texCords[indices[i]]; | ||||||
|  |         dPos1 = verteces[indices[i+1]] - verteces[indices[i]]; | ||||||
|  |         dPos2 = verteces[indices[i+2]] - verteces[indices[i]]; | ||||||
|  |         f = dTex1.x * dTex2.y - dTex2.x * dTex1.y; | ||||||
|  |          | ||||||
|  |         // Покомпонентное вычисление касательного вектора
 | ||||||
|  |         tmp.x = (dTex2.y * dPos1.x - dTex1.y * dPos2.x) / f; | ||||||
|  |         tmp.y = (dTex2.y * dPos1.y - dTex1.y * dPos2.y) / f; | ||||||
|  |         tmp.z = (dTex2.y * dPos1.z - dTex1.y * dPos2.z) / f; | ||||||
|  |         // Нормируем значение
 | ||||||
|  |         tmp = glm::normalize(tmp); | ||||||
|  |         // Добавим вектор в контейнер        
 | ||||||
|  |         tangent[indices[i  ]] = tmp; // Для каждого индекса полигона
 | ||||||
|  |         tangent[indices[i+1]] = tmp; // значение вектора
 | ||||||
|  |         tangent[indices[i+2]] = tmp; // одинаковое
 | ||||||
|  | 
 | ||||||
|  |         // Покомпонентное вычисление бикасательного вектора
 | ||||||
|  |         tmp.x = (-dTex2.x * dPos1.x + dTex1.x * dPos2.x) / f; | ||||||
|  |         tmp.y = (-dTex2.x * dPos1.y + dTex1.x * dPos2.y) / f; | ||||||
|  |         tmp.z = (-dTex2.x * dPos1.z + dTex1.x * dPos2.z) / f; | ||||||
|  |         // Нормируем значение
 | ||||||
|  |         tmp = glm::normalize(tmp); | ||||||
|  |         // Добавим вектор в контейнер        
 | ||||||
|  |         bitangent[indices[i  ]] = tmp; // Для каждого индекса полигона
 | ||||||
|  |         bitangent[indices[i+1]] = tmp; // значение вектора
 | ||||||
|  |         bitangent[indices[i+2]] = tmp; // одинаковое
 | ||||||
|  |     } | ||||||
|  | } | ||||||
|  | |||||||
| @ -126,6 +126,7 @@ Scene loadOBJtoScene(const char* filename, const char* mtl_directory, const char | |||||||
|     std::vector<glm::vec3> verteces; // вершины
 |     std::vector<glm::vec3> verteces; // вершины
 | ||||||
|     std::vector<glm::vec3> normals; // нормали
 |     std::vector<glm::vec3> normals; // нормали
 | ||||||
|     std::vector<glm::vec2> texCords; // текстурные координаты
 |     std::vector<glm::vec2> texCords; // текстурные координаты
 | ||||||
|  |     std::vector<glm::vec3> tangent, bitangent; // касательный и бикасательный веткоры
 | ||||||
|     size_t hash; // Для уникальных вершин
 |     size_t hash; // Для уникальных вершин
 | ||||||
|     std::map <int, int> uniqueVerteces; // словарь для уникальных вершин: ключ - хеш, значение - индекс вершины
 |     std::map <int, int> uniqueVerteces; // словарь для уникальных вершин: ключ - хеш, значение - индекс вершины
 | ||||||
|      |      | ||||||
| @ -190,12 +191,18 @@ Scene loadOBJtoScene(const char* filename, const char* mtl_directory, const char | |||||||
|         } |         } | ||||||
|     } // for (const auto& shape : shapes) 
 |     } // for (const auto& shape : shapes) 
 | ||||||
| 
 | 
 | ||||||
|      |     // Изменим размер массивов
 | ||||||
|  |     tangent.resize(verteces.size()); | ||||||
|  |     bitangent.resize(verteces.size()); | ||||||
|  |     // Расчет касательных и бикасательных векторов
 | ||||||
|  |     calc_tb(indices.data(), indices.size(), verteces.data(), texCords.data(), tangent.data(), bitangent.data()); | ||||||
| 
 | 
 | ||||||
|     // Загрузка в буферы
 |     // Загрузка в буферы
 | ||||||
|     model.load_verteces (&verteces[0], verteces.size()); |     model.load_verteces (&verteces[0], verteces.size()); | ||||||
|     model.load_normals  (&normals[0],  normals.size()); |     model.load_normals  (&normals[0],  normals.size()); | ||||||
|     model.load_texCoords(&texCords[0], texCords.size()); |     model.load_texCoords(&texCords[0], texCords.size()); | ||||||
|  |     model.load_tangent(&tangent[0], tangent.size()); | ||||||
|  |     model.load_bitangent(&bitangent[0], bitangent.size()); | ||||||
|     // Загрузка индексного буфера
 |     // Загрузка индексного буфера
 | ||||||
|     model.load_indices  (&indices[0],  indices.size()); |     model.load_indices  (&indices[0],  indices.size()); | ||||||
| 
 | 
 | ||||||
|  | |||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user