terravisor/source/load_texture.cpp

102 lines
3.6 KiB
C++
Raw Normal View History

#include "load_texture.h"
#include <FreeImage.h>
2024-08-20 00:47:03 -04:00
GLuint LoadTexture(const std::string& fname)
{
GLuint tex_id;
FREE_IMAGE_FORMAT format = FreeImage_GetFileType(fname.c_str(), 0);
FIBITMAP* tempImg = FreeImage_Load(format, fname.c_str());
FIBITMAP* img;
if(format == FIF_EXR)
img = FreeImage_ConvertToRGBF(tempImg);
else
img = FreeImage_ConvertTo32Bits(tempImg);
FreeImage_Unload(tempImg);
GLuint w = FreeImage_GetWidth(img);
GLuint h = FreeImage_GetHeight(img);
GLuint scanW = FreeImage_GetPitch(img);
GLubyte* byteImg = new GLubyte[h * scanW];;
GLfloat* floatImg = new GLfloat[h * scanW];;
if (format == FIF_EXR)
FreeImage_ConvertToRawBits((BYTE*)floatImg, img, scanW, 96, FI_RGBA_RED_MASK, FI_RGBA_GREEN_MASK, FI_RGBA_BLUE_MASK, TRUE);
else
FreeImage_ConvertToRawBits(byteImg, img, scanW, 32, FI_RGBA_RED_MASK, FI_RGBA_GREEN_MASK, FI_RGBA_BLUE_MASK, FALSE);
FreeImage_Unload(img);
glGenTextures(1, &tex_id);
glBindTexture(GL_TEXTURE_2D, tex_id);
if (format == FIF_EXR)
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB32F, w, h, 0, GL_RGB, GL_FLOAT, floatImg);
else
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, w, h, 0, GL_BGRA, GL_UNSIGNED_BYTE, byteImg);
glGenerateMipmap(GL_TEXTURE_2D);
if (format == FIF_EXR)
{
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
}
else
{
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
}
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
delete[] byteImg;
delete[] floatImg;
return tex_id;
}
GLuint LoadSkybox(const std::vector<std::string>& faces)
{
GLuint tex_id;
glGenTextures(1, &tex_id);
glBindTexture(GL_TEXTURE_CUBE_MAP, tex_id);
for (GLuint i = 0; i < faces.size(); i++)
{
FREE_IMAGE_FORMAT format = FreeImage_GetFileType(faces[i].c_str(), 0);
FIBITMAP* tempImg = FreeImage_Load(format, faces[i].c_str());
FIBITMAP* img = (format == FIF_EXR) ? FreeImage_ConvertToRGBF(tempImg) : FreeImage_ConvertTo32Bits(tempImg);
FreeImage_Unload(tempImg);
GLuint w = FreeImage_GetWidth(img);
GLuint h = FreeImage_GetHeight(img);
GLuint scanW = FreeImage_GetPitch(img);
if (format == FIF_EXR)
{
GLfloat* floatImg = new GLfloat[h * scanW];
FreeImage_ConvertToRawBits((BYTE*)floatImg, img, scanW, 96, FI_RGBA_RED_MASK, FI_RGBA_GREEN_MASK, FI_RGBA_BLUE_MASK, TRUE);
glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_X + i, 0, GL_RGB32F, w, h, 0, GL_RGB, GL_FLOAT, floatImg);
delete[] floatImg;
}
else
{
GLubyte* byteImg = new GLubyte[h * scanW];
FreeImage_ConvertToRawBits(byteImg, img, scanW, 32, FI_RGBA_RED_MASK, FI_RGBA_GREEN_MASK, FI_RGBA_BLUE_MASK, FALSE);
glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_X + i, 0, GL_RGBA, w, h, 0, GL_BGRA, GL_UNSIGNED_BYTE, byteImg);
delete[] byteImg;
}
FreeImage_Unload(img);
}
glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_R, GL_CLAMP_TO_EDGE);
return tex_id;
}