| 674 | } |
| 675 | |
| 676 | bool ObjectManager::LoadBufferFromTexture(BufferObject* buf, const std::string& str, bool convertToFloat) |
| 677 | { |
| 678 | ed::Logger::Get().Log("Loading buffer data from a texture"); |
| 679 | |
| 680 | std::string path = m_parser->GetProjectPath(str); |
| 681 | int width, height, nrChannels; |
| 682 | unsigned char* data = nullptr; |
| 683 | dds_image_t ddsImage = nullptr; |
| 684 | |
| 685 | bool isDDS = (std::filesystem::path(path).extension().u8string() == ".dds"); |
| 686 | |
| 687 | if (!isDDS) |
| 688 | stbi_load(path.c_str(), &width, &height, &nrChannels, 0); |
| 689 | else { |
| 690 | ddsImage = dds_load_from_file(path.c_str()); |
| 691 | |
| 692 | data = ddsImage->pixels; |
| 693 | width = ddsImage->header.width; |
| 694 | height = ddsImage->header.height; |
| 695 | nrChannels = 4; |
| 696 | } |
| 697 | |
| 698 | if (data != nullptr || (isDDS && ddsImage != nullptr)) { |
| 699 | m_parser->ModifyProject(); |
| 700 | |
| 701 | if (convertToFloat) { |
| 702 | buf->Size = width * height * nrChannels * sizeof(float); |
| 703 | buf->Data = realloc(buf->Data, buf->Size); |
| 704 | float* fData = (float*)buf->Data; |
| 705 | |
| 706 | for (int x = 0; x < width; x++) { |
| 707 | for (int y = 0; y < height; y++) { |
| 708 | for (int z = 0; z < nrChannels; z++) { |
| 709 | int index = ((y * width) + x) * nrChannels + z; |
| 710 | fData[index] = data[index] / 255.0f; |
| 711 | } |
| 712 | } |
| 713 | } |
| 714 | } else { |
| 715 | buf->Size = width * height * nrChannels * sizeof(char); |
| 716 | buf->Data = realloc(buf->Data, buf->Size); |
| 717 | memcpy(buf->Data, data, buf->Size); |
| 718 | } |
| 719 | |
| 720 | if (!isDDS) |
| 721 | stbi_image_free(data); |
| 722 | else |
| 723 | dds_image_free(ddsImage); |
| 724 | |
| 725 | glBindBuffer(GL_UNIFORM_BUFFER, buf->ID); |
| 726 | glBufferData(GL_UNIFORM_BUFFER, buf->Size, buf->Data, GL_STATIC_DRAW); // upload data |
| 727 | glBindBuffer(GL_UNIFORM_BUFFER, 0); |
| 728 | } |
| 729 | |
| 730 | return data != nullptr; |
| 731 | } |
| 732 | bool ObjectManager::LoadBufferFromModel(BufferObject* buf, const std::string& str) |
| 733 | { |
no test coverage detected