-
Notifications
You must be signed in to change notification settings - Fork 0
/
texture.cpp
66 lines (57 loc) · 2.4 KB
/
texture.cpp
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
#pragma once
#include <string>
#include "texture.h"
texture CreateTexture(std::string Filename, texture_type TextureType = TextureType_Null)
{
texture Result = {};
Result.Filename = Filename;
Result.Type = TextureType;
i32 RequestedChannelCount = 0;
i32 FlipVertically = 1;
stbi_set_flip_vertically_on_load(FlipVertically);
u8 *Data = stbi_load(Filename.c_str(), &Result.Width, &Result.Height, &Result.ChannelCount, RequestedChannelCount);
if(Data)
{
glGenTextures(1, &Result.Handle);
glBindTexture(GL_TEXTURE_2D, Result.Handle);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
if(Result.ChannelCount == 3)
{
Result.Format = GL_RGB;
Result.InternalFormat = GL_SRGB;
}
else if(Result.ChannelCount == 4)
{
Result.Format = GL_RGBA;
// NOTE: If anything looks weird when drawing textures,
// maybe toggle between GL_SRGB and GL_SRGB_ALPHA. I'm too
// lazy to check opengl docs right now.
// Result->InternalFormat = GL_SRGB;
Result.InternalFormat = GL_SRGB_ALPHA;
}
else
{
fprintf(stderr, "%s Texture format is not GL_RGB or GL_RGBA\n", Filename.c_str());
stbi_image_free(Data);
Result.Type = TextureType_Null;
}
i32 MipMapDetailLevel = 0;
// REMINDER: Textures to be used for data should not be uploaded as GL_SRGB!
// NOTE: InternalFormat is the format we want to store the data, Format is the input format
glTexImage2D(GL_TEXTURE_2D, MipMapDetailLevel, Result.InternalFormat, Result.Width, Result.Height, 0, Result.Format, GL_UNSIGNED_BYTE, Data);
// NOTE(Jorge): Set custom MipMaps filtering values here!
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glGenerateMipmap(GL_TEXTURE_2D);
stbi_image_free(Data);
}
else
{
printf("Could not load image file: %s\n", Filename.c_str());
Result.Type = TextureType_Null;
}
return Result;
}