-
Notifications
You must be signed in to change notification settings - Fork 0
/
texture.cpp
172 lines (151 loc) · 8.71 KB
/
texture.cpp
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
#include "texture.h"
unsigned Texture::texture_array;
std::unordered_map<std::string, unsigned int> Texture::str_to_texid;
void Texture::setup()
{
IMG_Init(IMG_INIT_PNG);
const int textureSize = 16;
const int numTextures = 17;
int texid = 0;
glGenTextures(1, &texture_array);
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D_ARRAY, texture_array);
glTexImage3D(GL_TEXTURE_2D_ARRAY, 0, GL_RGBA, textureSize, textureSize,
numTextures, 0, GL_RGBA, GL_UNSIGNED_BYTE, nullptr);
SDL_Surface* surface = IMG_Load("texture_packs/default/voxels/grass.png");
assert(surface->w == 16);
assert(surface->h == 16);
glTexSubImage3D(GL_TEXTURE_2D_ARRAY, 0, 0, 0, texid, textureSize, textureSize, 1, GL_RGBA, GL_UNSIGNED_BYTE, surface->pixels);
glTexParameteri(GL_TEXTURE_2D_ARRAY, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D_ARRAY, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
str_to_texid["grass"] = texid++;
SDL_FreeSurface(surface);
surface = IMG_Load("texture_packs/default/voxels/grass_side.png");
assert(surface->w == 16);
assert(surface->h == 16);
glTexSubImage3D(GL_TEXTURE_2D_ARRAY, 0, 0, 0, texid, textureSize, textureSize, 1, GL_RGBA, GL_UNSIGNED_BYTE, surface->pixels);
glTexParameteri(GL_TEXTURE_2D_ARRAY, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D_ARRAY, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
str_to_texid["grass_side"] = texid++;
SDL_FreeSurface(surface);
surface = IMG_Load("texture_packs/default/voxels/sand.png");
assert(surface->w == 16);
assert(surface->h == 16);
glTexSubImage3D(GL_TEXTURE_2D_ARRAY, 0, 0, 0, texid, textureSize, textureSize, 1, GL_RGB, GL_UNSIGNED_BYTE, surface->pixels); // even though sand.png is a png, it's GL_RGB instead of GL_RGBA
glTexParameteri(GL_TEXTURE_2D_ARRAY, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D_ARRAY, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
str_to_texid["sand"] = texid++;
SDL_FreeSurface(surface);
surface = IMG_Load("texture_packs/default/voxels/dirt.png");
assert(surface->w == 16);
assert(surface->h == 16);
glTexSubImage3D(GL_TEXTURE_2D_ARRAY, 0, 0, 0, texid, textureSize, textureSize, 1, GL_RGBA, GL_UNSIGNED_BYTE, surface->pixels);
glTexParameteri(GL_TEXTURE_2D_ARRAY, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D_ARRAY, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
str_to_texid["dirt"] = texid++;
SDL_FreeSurface(surface);
surface = IMG_Load("texture_packs/default/voxels/stone.png");
assert(surface->w == 16);
assert(surface->h == 16);
glTexSubImage3D(GL_TEXTURE_2D_ARRAY, 0, 0, 0, texid, textureSize, textureSize, 1, GL_RGB, GL_UNSIGNED_BYTE, surface->pixels); // even though stone.png is a png, it's GL_RGB instead of GL_RGBA
glTexParameteri(GL_TEXTURE_2D_ARRAY, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D_ARRAY, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
str_to_texid["stone"] = texid++;
SDL_FreeSurface(surface);
surface = IMG_Load("texture_packs/default/voxels/logtop.png");
assert(surface->w == 16);
assert(surface->h == 16);
glTexSubImage3D(GL_TEXTURE_2D_ARRAY, 0, 0, 0, texid, textureSize, textureSize, 1, GL_RGBA, GL_UNSIGNED_BYTE, surface->pixels);
glTexParameteri(GL_TEXTURE_2D_ARRAY, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D_ARRAY, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
str_to_texid["logtop"] = texid++;
SDL_FreeSurface(surface);
surface = IMG_Load("texture_packs/default/voxels/log.png");
assert(surface->w == 16);
assert(surface->h == 16);
glTexSubImage3D(GL_TEXTURE_2D_ARRAY, 0, 0, 0, texid, textureSize, textureSize, 1, GL_RGBA, GL_UNSIGNED_BYTE, surface->pixels);
glTexParameteri(GL_TEXTURE_2D_ARRAY, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D_ARRAY, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
str_to_texid["log"] = texid++;
SDL_FreeSurface(surface);
surface = IMG_Load("texture_packs/default/voxels/water.png");
assert(surface->w == 16);
assert(surface->h == 16);
glTexSubImage3D(GL_TEXTURE_2D_ARRAY, 0, 0, 0, texid, textureSize, textureSize, 1, GL_RGBA, GL_UNSIGNED_BYTE, surface->pixels);
glTexParameteri(GL_TEXTURE_2D_ARRAY, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D_ARRAY, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
str_to_texid["water"] = texid++;
SDL_FreeSurface(surface);
surface = IMG_Load("texture_packs/default/voxels/iron.png");
assert(surface->w == 16);
assert(surface->h == 16);
glTexSubImage3D(GL_TEXTURE_2D_ARRAY, 0, 0, 0, texid, textureSize, textureSize, 1, GL_RGBA, GL_UNSIGNED_BYTE, surface->pixels);
glTexParameteri(GL_TEXTURE_2D_ARRAY, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D_ARRAY, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
str_to_texid["iron"] = texid++;
SDL_FreeSurface(surface);
surface = IMG_Load("texture_packs/default/voxels/coal.png");
assert(surface->w == 16);
assert(surface->h == 16);
glTexSubImage3D(GL_TEXTURE_2D_ARRAY, 0, 0, 0, texid, textureSize, textureSize, 1, GL_RGBA, GL_UNSIGNED_BYTE, surface->pixels);
glTexParameteri(GL_TEXTURE_2D_ARRAY, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D_ARRAY, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
str_to_texid["coal"] = texid++;
SDL_FreeSurface(surface);
surface = IMG_Load("texture_packs/default/voxels/common_cactus_top.png");
assert(surface->w == 16);
assert(surface->h == 16);
glTexSubImage3D(GL_TEXTURE_2D_ARRAY, 0, 0, 0, texid, textureSize, textureSize, 1, GL_RGBA, GL_UNSIGNED_BYTE, surface->pixels);
glTexParameteri(GL_TEXTURE_2D_ARRAY, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D_ARRAY, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
str_to_texid["common_cactus_top"] = texid++;
SDL_FreeSurface(surface);
surface = IMG_Load("texture_packs/default/voxels/common_cactus_side.png");
assert(surface->w == 16);
assert(surface->h == 16);
glTexSubImage3D(GL_TEXTURE_2D_ARRAY, 0, 0, 0, texid, textureSize, textureSize, 1, GL_RGBA, GL_UNSIGNED_BYTE, surface->pixels);
glTexParameteri(GL_TEXTURE_2D_ARRAY, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D_ARRAY, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
str_to_texid["common_cactus_side"] = texid++;
SDL_FreeSurface(surface);
surface = IMG_Load("texture_packs/default/voxels/common_dead_shrub.png");
assert(surface->w == 16);
assert(surface->h == 16);
glTexSubImage3D(GL_TEXTURE_2D_ARRAY, 0, 0, 0, texid, textureSize, textureSize, 1, GL_RGBA, GL_UNSIGNED_BYTE, surface->pixels);
glTexParameteri(GL_TEXTURE_2D_ARRAY, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D_ARRAY, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
str_to_texid["common_dead_shrub"] = texid++;
SDL_FreeSurface(surface);
surface = IMG_Load("texture_packs/default/voxels/common_tall_grass.png");
assert(surface->w == 16);
assert(surface->h == 16);
glTexSubImage3D(GL_TEXTURE_2D_ARRAY, 0, 0, 0, texid, textureSize, textureSize, 1, GL_RGBA, GL_UNSIGNED_BYTE, surface->pixels);
glTexParameteri(GL_TEXTURE_2D_ARRAY, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D_ARRAY, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
str_to_texid["common_tall_grass"] = texid++;
SDL_FreeSurface(surface);
surface = IMG_Load("texture_packs/default/voxels/diamond.png");
assert(surface->w == 16);
assert(surface->h == 16);
glTexSubImage3D(GL_TEXTURE_2D_ARRAY, 0, 0, 0, texid, textureSize, textureSize, 1, GL_RGBA, GL_UNSIGNED_BYTE, surface->pixels);
glTexParameteri(GL_TEXTURE_2D_ARRAY, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D_ARRAY, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
str_to_texid["diamond"] = texid++;
SDL_FreeSurface(surface);
surface = IMG_Load("texture_packs/default/voxels/error.png");
assert(surface->w == 16);
assert(surface->h == 16);
glTexSubImage3D(GL_TEXTURE_2D_ARRAY, 0, 0, 0, texid, textureSize, textureSize, 1, GL_RGBA, GL_UNSIGNED_BYTE, surface->pixels);
glTexParameteri(GL_TEXTURE_2D_ARRAY, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D_ARRAY, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
str_to_texid["error"] = texid++;
SDL_FreeSurface(surface);
surface = IMG_Load("texture_packs/default/voxels/leaves.png");
assert(surface->w == 16);
assert(surface->h == 16);
glTexSubImage3D(GL_TEXTURE_2D_ARRAY, 0, 0, 0, texid, textureSize, textureSize, 1, GL_RGBA, GL_UNSIGNED_BYTE, surface->pixels);
glTexParameteri(GL_TEXTURE_2D_ARRAY, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D_ARRAY, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
str_to_texid["leaves"] = texid++;
SDL_FreeSurface(surface);
}