-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLoader.cpp
More file actions
87 lines (61 loc) · 1.77 KB
/
Loader.cpp
File metadata and controls
87 lines (61 loc) · 1.77 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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
#include "Loader.h"
#include <GL/glew.h>
#include <vector>
#include <fstream>
Loader::Loader()
{
}
Texture Loader::loadPNG(const std::string& filePath)
{
Texture texture;
std::vector<unsigned char> in;
std::vector<unsigned char> out;
unsigned long width, height;
if (loadFile(filePath, in) == false) {
printf("Failed to load file to buffer");
}
printf("Loading texture...\n");
int errorCode = decodePNG(out, width, height, in.data(), in.size());
if (errorCode != 0) {
printf("decodePNG failed with error: %s", std::to_string(errorCode).c_str());
}
glGenTextures(1, &(texture.id));
glBindTexture(GL_TEXTURE_2D, texture.id);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, out.data());
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);
glGenerateMipmap(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, 0);
texture.width = width;
texture.height = height;
texture.filePath = filePath;
printf("Texture %s loaded\n", filePath.c_str());
return texture;
}
bool Loader::loadFile(const std::string& fileName, std::vector<unsigned char>& buffer)
{
//open file
std::ifstream file(fileName, std::ios::binary);
if (file.fail()) {
printf("Failed to read in file: %s", fileName.c_str());
return false;
}
//get size of file
long size;
file.seekg(0, std::ios::end);
size = file.tellg();
//reset to beginning
file.seekg(0, std::ios::beg);
//resize buffer
size -= file.tellg();
buffer.resize(size);
//read file to buffer
file.read((char*)&(buffer[0]), size);
file.close();
return true;
}
Loader::~Loader()
{
}