-
Notifications
You must be signed in to change notification settings - Fork 0
/
Material.h
112 lines (92 loc) · 3.77 KB
/
Material.h
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
//
// Created by Marrony Neris on 11/25/15.
//
#ifndef MATERIAL_H
#define MATERIAL_H
struct MaterialBumpedDiffuse {
Program program;
int mainUnit;
Texture2D mainTex;
Sampler mainSampler;
int bumpUnit;
Texture2D bumpMap;
Sampler bumpSampler;
};
struct MaterialTransparency {
Program program;
int mainUnit;
Texture2D mainTex;
Sampler mainSampler;
int bumpUnit;
Texture2D bumpMap;
Sampler bumpSampler;
float alpha;
};
struct Material {
int passCount;
CommandBuffer* state[4];
static Material* create(HeapAllocator& allocator, MaterialBumpedDiffuse* diffuse) {
Material* material = (Material*) allocator.allocate(sizeof(Material));
material->passCount = 1;
material->state[0] = CommandBuffer::create(allocator, 10);
BindProgram::create(material->state[0], diffuse->program);
#if RIGHT_HANDED
SetDepthTest::create(material->state[0], true, GL_LEQUAL);
#else
SetDepthTest::create(material->state[0], true, GL_GEQUAL);
#endif
SetBlend::disable(material->state[0], 0);
SetBlend::disable(material->state[0], 1);
SetBlend::disable(material->state[0], 2);
if(diffuse->mainUnit != -1) {
BindTexture::create(material->state[0], diffuse->mainTex, diffuse->mainSampler, diffuse->mainUnit);
}
if(diffuse->bumpUnit != -1) {
BindTexture::create(material->state[0], diffuse->bumpMap, diffuse->bumpSampler, diffuse->bumpUnit);
}
return material;
}
static Material* create(HeapAllocator& allocator, MaterialTransparency* transparency) {
Material* material = (Material*) allocator.allocate(sizeof(Material));
material->passCount = 2;
material->state[0] = CommandBuffer::create(allocator, 10);
BindProgram::create(material->state[0], transparency->program);
#if RIGHT_HANDED
SetDepthTest::create(material->state[0], true, GL_LEQUAL);
SetCullFace::create(material->state[0], true, GL_FRONT, GL_CCW);
#else
SetDepthTest::create(material->state[0], true, GL_GEQUAL);
SetCullFace::create(material->state[0], true, GL_FRONT, GL_CW);
#endif
SetBlend::create(material->state[0], true, 0, GL_FUNC_ADD, GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
if(transparency->mainUnit != -1) {
BindTexture::create(material->state[0], transparency->mainTex, transparency->mainSampler, transparency->mainUnit);
}
if(transparency->bumpUnit != -1) {
BindTexture::create(material->state[0], transparency->bumpMap, transparency->bumpSampler, transparency->bumpUnit);
}
material->state[1] = CommandBuffer::create(allocator, 10);
BindProgram::create(material->state[1], transparency->program);
#if RIGHT_HANDED
SetDepthTest::create(material->state[1], true, GL_LEQUAL);
SetCullFace::create(material->state[1], true, GL_BACK, GL_CCW);
#else
SetDepthTest::create(material->state[1], true, GL_GEQUAL);
SetCullFace::create(material->state[1], true, GL_BACK, GL_CW);
#endif
SetBlend::create(material->state[1], true, 0, GL_FUNC_ADD, GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
if(transparency->mainUnit != -1) {
BindTexture::create(material->state[1], transparency->mainTex, transparency->mainSampler, transparency->mainUnit);
}
if(transparency->bumpUnit != -1) {
BindTexture::create(material->state[1], transparency->bumpMap, transparency->bumpSampler, transparency->bumpUnit);
}
return material;
}
static void destroy(HeapAllocator& allocator, Material* material) {
for(int i = 0; i < material->passCount; i++)
allocator.deallocate(material->state[i]);
allocator.deallocate(material);
}
};
#endif //MATERIAL_H