-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUtils.cpp
More file actions
62 lines (55 loc) · 2.1 KB
/
Utils.cpp
File metadata and controls
62 lines (55 loc) · 2.1 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
#include "Utils.h"
#include "targa.h"
#include <fstream>
#include <string>
#define _USE_MATH_DEFINES
#include <math.h>
void Utils::loadTexture(const char* filename, GLuint &texture)
{
TargaImage textureImage;
const std::string filename_str(filename);
//Try open a file containing the serialized object.
//const std::string bin_filename = filename_str.substr(0, filename_str.find_last_of('.')) + ".bin";
//std::ifstream binFileIn(bin_filename.c_str(), std::ios::binary);
//if (!binFileIn.good())
//{
if (!textureImage.load(filename))
{
MessageBox(NULL, "Could not load texture image", "Texture error", MB_ICONERROR | MB_OK);
}
else
{
// Serialize the TargaImage to disk to faciliate faster loading next time.
// std::ofstream binFileOut(bin_filename.c_str(), std::ios::binary);
// binFileOut.write(reinterpret_cast<char*>(&textureImage), sizeof(textureImage));
// binFileOut.write(reinterpret_cast<char*>(textureImage.getImageData()), textureImage.getImageSize());
// binFileOut.close();
}
//}
//else
//{
// Read the serialized TargaImage from disk.
// binFileIn.read(reinterpret_cast<char*>(&textureImage), sizeof(TargaImage));
// binFileIn.read(reinterpret_cast<char*>(textureImage.getImageData()), textureImage.getImageSize());
//}
//binFileIn.close();
glGenTextures(1, &texture);
glBindTexture(GL_TEXTURE_2D, texture);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
if (textureImage.getType() == GL_RGB)
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB8, textureImage.getWidth(), textureImage.getHeight(),
0, GL_RGB, GL_UNSIGNED_BYTE ,textureImage.getImageData());
else
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, textureImage.getWidth(), textureImage.getHeight(),
0, GL_RGBA, GL_UNSIGNED_BYTE ,textureImage.getImageData());
textureImage.unload();
}
GLfloat Utils::degreesToRadians(GLfloat degrees)
{
return degrees * (GLfloat)M_PI / 180.0f;
}
GLfloat Utils::radiansToDegrees(GLfloat radians)
{
return radians * 180.0f / (GLfloat)M_PI;
}