forked from Barnold1953/GraphicsTutorials
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathImageLoader.cpp
More file actions
57 lines (43 loc) · 1.91 KB
/
ImageLoader.cpp
File metadata and controls
57 lines (43 loc) · 1.91 KB
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
#include "ImageLoader.h"
#include "picoPNG.h"
#include "IOManager.h"
#include "BengineErrors.h"
namespace Bengine {
GLTexture ImageLoader::loadPNG(std::string filePath) {
//Create a GLTexture and initialize all its fields to 0
GLTexture texture = {};
//This is the input data to decodePNG, which we load from a file
std::vector<unsigned char> in;
//This is the output data from decodePNG, which is the pixel data for our texture
std::vector<unsigned char> out;
unsigned long width, height;
//Read in the image file contents into a buffer
if (IOManager::readFileToBuffer(filePath, in) == false) {
fatalError("Failed to load PNG file to buffer!");
}
//Decode the .png format into an array of pixels
int errorCode = decodePNG(out, width, height, &(in[0]), in.size());
if (errorCode != 0) {
fatalError("decodePNG failed with error: " + std::to_string(errorCode));
}
//Generate the openGL texture object
glGenTextures(1, &(texture.id));
//Bind the texture object
glBindTexture(GL_TEXTURE_2D, texture.id);
//Upload the pixels to the texture
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, &(out[0]));
//Set some texture parameters
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
//Generate the mipmaps
glGenerateMipmap(GL_TEXTURE_2D);
//Unbind the texture
glBindTexture(GL_TEXTURE_2D, 0);
texture.width = width;
texture.height = height;
//Return a copy of the texture data
return texture;
}
}