-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathComponentMaterial.cpp
More file actions
135 lines (87 loc) · 3.15 KB
/
ComponentMaterial.cpp
File metadata and controls
135 lines (87 loc) · 3.15 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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
#include "ComponentMaterial.h"
#include "glew-2.1.0\include\GL\glew.h"
#include "Devil/include/il.h"
#include "Devil/include/ilu.h"
#include "Devil/include/ilut.h"
ComponentMaterial::ComponentMaterial()
{
type = Component_type::COMPONENT_MATERIAL;
CheckeredTexture();
}
ComponentMaterial::ComponentMaterial(const char* tex_full_path) {
Texture* tex = new Texture();
tex->gl_binding = ilutGLLoadImage((char*)tex_full_path);
tex->width = ilGetInteger(IL_IMAGE_WIDTH);
tex->height = ilGetInteger(IL_IMAGE_HEIGHT);
tex->path = tex_full_path;
textures.push_back(tex);
}
ComponentMaterial::ComponentMaterial(aiMaterial& mat) {
type = Component_type::COMPONENT_MATERIAL;
uint numTextures = mat.GetTextureCount(aiTextureType_DIFFUSE);
for (int i = 0; i < numTextures; i++) {
Texture* tex = new Texture();
mat.GetTexture(aiTextureType_DIFFUSE, i, &tex->path);
aiString tex_path("Assets/");
tex_path.Append(tex->path.C_Str());
tex->gl_binding = ilutGLLoadImage((char*)tex_path.C_Str());
if (tex->gl_binding != 0) {
tex->width = ilGetInteger(IL_IMAGE_WIDTH);
tex->height = ilGetInteger(IL_IMAGE_HEIGHT);
textures.push_back(tex);
}
else
CheckeredTexture();
}
if (textures.empty())
CheckeredTexture();
}
ComponentMaterial::ComponentMaterial(ComponentMaterial& component) {
type = Component_type::COMPONENT_MATERIAL;
for (std::vector<Texture*>::iterator it = component.textures.begin(); it != component.textures.end(); it++) {
Texture* tex = new Texture();
tex->gl_binding = ilutGLLoadImage((char*) (*it)->path.C_Str());
if (tex->gl_binding != 0) {
tex->width = ilGetInteger(IL_IMAGE_WIDTH);
tex->height = ilGetInteger(IL_IMAGE_HEIGHT);
textures.push_back(tex);
}
else
CheckeredTexture();
}
}
ComponentMaterial::~ComponentMaterial() {
for (std::vector<Texture*>::iterator it = textures.begin(); it != textures.end(); it++)
glDeleteTextures(1, &(*it)->gl_binding);
textures.clear();
}
Component* ComponentMaterial::Duplicate()
{
return (Component*) new ComponentMaterial(*this);
}
void ComponentMaterial::CheckeredTexture() {
GLubyte checkImage[CHECKERS_HEIGHT][CHECKERS_WIDTH][4];
for (int i = 0; i < CHECKERS_HEIGHT; i++) {
for (int j = 0; j < CHECKERS_WIDTH; j++) {
int c = ((((i & 0x8) == 0) ^ (((j & 0x8)) == 0))) * 255;
checkImage[i][j][0] = (GLubyte)c;
checkImage[i][j][1] = (GLubyte)c;
checkImage[i][j][2] = (GLubyte)c;
checkImage[i][j][3] = (GLubyte)255;
}
}
Texture* checkered_tex = new Texture();
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
glGenTextures(1, &checkered_tex->gl_binding);
glBindTexture(GL_TEXTURE_2D, checkered_tex->gl_binding);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, CHECKERS_WIDTH, CHECKERS_HEIGHT,
0, GL_RGBA, GL_UNSIGNED_BYTE, checkImage);
checkered_tex->width = CHECKERS_WIDTH;
checkered_tex->height = CHECKERS_HEIGHT;
glBindTexture(GL_TEXTURE_2D, 0);
textures.push_back(checkered_tex);
}