TerraForge3D  2.3.1
3D Terrain And Landscape Generator

◆ Texture2D() [2/2]

Texture2D::Texture2D ( const std::string  path,
bool  preserveData = true,
bool  readAlpha = false 
)

Definition at line 28 of file Texture2D.cpp.

29 : m_Path(path)
30{
31 int width, height, channels;
32 stbi_set_flip_vertically_on_load(0);
33 unsigned char *data = nullptr;
34
35 if(readAlpha)
36 {
37 data = stbi_load(path.c_str(), &width, &height, &channels, 0);
38 }
39
40 else
41 {
42 data = stbi_load(path.c_str(), &width, &height, &channels, 3);
43 }
44
45 if (data)
46 {
47 m_IsLoaded = true;
48 m_Width = width;
49 m_Height = height;
50 glGenTextures(1, &m_RendererID);
51 glBindTexture(GL_TEXTURE_2D, m_RendererID);
52 glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
53
54 if(readAlpha)
55 {
56 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB8, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, data);
57 }
58
59 else
60 {
61 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB8, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, data);
62 }
63
64 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
65 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
66 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
67 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
68 glGenerateMipmap(GL_TEXTURE_2D);
69
70 if (preserveData)
71 {
72 m_Data = data;
73 }
74
75 else
76 {
77 stbi_image_free(data);
78 }
79 }
80
81 else
82 {
83 std::cout << "Failed to load texture : " << path << std::endl;
84 }
85}