-
Notifications
You must be signed in to change notification settings - Fork 4
/
Skybox.cpp
74 lines (65 loc) · 2.14 KB
/
Skybox.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
#include "Skybox.h"
#include "Camera.h"
#include "mat.h"
#include "program.h"
#define STB_IMAGE_IMPLEMENTATION
#include "stb_image.h"
void Skybox::CreateCubeMap(
const char* front,
const char* back,
const char* top,
const char* bottom,
const char* left,
const char* right)
{
// generate a cube-map texture to hold all the sides
glActiveTexture(GL_TEXTURE0);
glGenTextures(1, &texCube);
// load each image and copy into a side of the cube-map texture
load_cube_map_side(texCube, GL_TEXTURE_CUBE_MAP_NEGATIVE_Z, front);
load_cube_map_side(texCube, GL_TEXTURE_CUBE_MAP_POSITIVE_Z, back);
load_cube_map_side(texCube, GL_TEXTURE_CUBE_MAP_POSITIVE_Y, top);
load_cube_map_side(texCube, GL_TEXTURE_CUBE_MAP_NEGATIVE_Y, bottom);
load_cube_map_side(texCube, GL_TEXTURE_CUBE_MAP_NEGATIVE_X, left);
load_cube_map_side(texCube, GL_TEXTURE_CUBE_MAP_POSITIVE_X, right);
// format cube map texture
glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MAG_FILTER, GL_LINEAR_MIPMAP_LINEAR);
glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_R, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_CUBE_MAP, texCube);
glGenerateMipmap(GL_TEXTURE_CUBE_MAP);
glBindTexture(GL_TEXTURE_CUBE_MAP, 0);
}
bool Skybox::load_cube_map_side(GLuint texture, GLenum side_target, const char* file_name)
{
glBindTexture(GL_TEXTURE_CUBE_MAP, texture);
int x, y, n;
int force_channels = 4;
unsigned char* image_data = stbi_load(file_name, &x, &y, &n, force_channels);
if (!image_data) {
fprintf(stderr, "ERROR: could not load %s\n", file_name);
return false;
}
// non-power-of-2 dimensions check
if ((x & (x - 1)) != 0 || (y & (y - 1)) != 0) {
fprintf(stderr,
"WARNING: image %s is not power-of-2 dimensions\n",
file_name);
}
// copy image data into 'target' side of cube map
glTexImage2D(
side_target,
0,
GL_RGBA,
x,
y,
0,
GL_RGBA,
GL_UNSIGNED_BYTE,
image_data);
free(image_data);
return true;
}