-
Notifications
You must be signed in to change notification settings - Fork 0
/
texture_loader.cpp
131 lines (104 loc) · 4.54 KB
/
texture_loader.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
/*
Copyright (C) 2016 Meritxell Jordana
Copyright (C) 2016 Marc Sanchez
*/
#include "texture_loader.h"
using namespace std;
const char* const TextureLoader::waterTextureFile = "textures/water.jpeg";
const char* const TextureLoader::grassTextureFile = "textures/grass.jpeg";
const char* const TextureLoader::edgeTextureFile = "textures/edge.jpeg";
const int TextureLoader::textureDimension = 64;
#ifdef ARDUINO
const char* const TextureLoader::waterPurpleTextureFile = "textures/water_purple.jpeg";
const char* const TextureLoader::waterBlueTextureFile = "textures/water_blue.jpeg";
const char* const TextureLoader::waterYellowTextureFile = "textures/water_yellow.jpeg";
const char* const TextureLoader::waterOrangeTextureFile = "textures/water_orange.jpeg";
const char* const TextureLoader::waterRedTextureFile = "textures/water_red.jpeg";
#endif
void TextureLoader::LoadTextures() {
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, Water);
TextureLoader::LoadTexture(TextureLoader::waterTextureFile,
TextureLoader::textureDimension);
glBindTexture(GL_TEXTURE_2D, Grass);
TextureLoader::LoadTexture(TextureLoader::grassTextureFile,
TextureLoader::textureDimension);
glBindTexture(GL_TEXTURE_2D, Edge);
TextureLoader::LoadTexture(TextureLoader::edgeTextureFile,
TextureLoader::textureDimension);
#ifdef ARDUINO
glBindTexture(GL_TEXTURE_2D, WaterPurple);
TextureLoader::LoadTexture(TextureLoader::waterPurpleTextureFile,
TextureLoader::textureDimension);
glBindTexture(GL_TEXTURE_2D, WaterBlue);
TextureLoader::LoadTexture(TextureLoader::waterBlueTextureFile,
TextureLoader::textureDimension);
glBindTexture(GL_TEXTURE_2D, WaterYellow);
TextureLoader::LoadTexture(TextureLoader::waterYellowTextureFile,
TextureLoader::textureDimension);
glBindTexture(GL_TEXTURE_2D, WaterOrange);
TextureLoader::LoadTexture(TextureLoader::waterOrangeTextureFile,
TextureLoader::textureDimension);
glBindTexture(GL_TEXTURE_2D, WaterRed);
TextureLoader::LoadTexture(TextureLoader::waterRedTextureFile,
TextureLoader::textureDimension);
#endif
glDisable(GL_TEXTURE_2D);
}
void TextureLoader::ReadJPEG(const char *filename, unsigned char **image,
int *width, int *height) {
struct jpeg_decompress_struct cinfo;
struct jpeg_error_mgr jerr;
FILE * infile;
unsigned char **buffer;
unsigned int i, j;
cinfo.err = jpeg_std_error(&jerr);
jpeg_create_decompress(&cinfo);
if ((infile = fopen(filename, "rb")) == NULL) {
printf("Unable to open file %s\n", filename);
exit(1);
}
jpeg_stdio_src(&cinfo, infile);
jpeg_read_header(&cinfo, TRUE);
jpeg_calc_output_dimensions(&cinfo);
jpeg_start_decompress(&cinfo);
*width = cinfo.output_width;
*height = cinfo.output_height;
*image = (unsigned char*) malloc(cinfo.output_width * cinfo.output_height * cinfo.output_components);
buffer = (unsigned char **) malloc(1 * sizeof (unsigned char **));
buffer[0] = (unsigned char *) malloc(cinfo.output_width * cinfo.output_components);
i = 0;
while (cinfo.output_scanline < cinfo.output_height) {
jpeg_read_scanlines(&cinfo, buffer, 1);
for (j = 0; j < cinfo.output_width * cinfo.output_components; j++) {
(*image)[i] = buffer[0][j];
i++;
}
}
free(buffer);
jpeg_finish_decompress(&cinfo);
}
void TextureLoader::LoadTexture(const char *filename, int dim) {
unsigned char *buffer;
unsigned char *buffer2;
int width, height;
long i, j;
long k, h;
ReadJPEG(filename, &buffer, &width, &height);
buffer2 = (unsigned char*) malloc(dim * dim * 3);
//-- The texture pattern is subsampled so that its dimensions become dim x dim --
for (i = 0; i < dim; i++)
for (j = 0; j < dim; j++) {
k = i * height / dim;
h = j * width / dim;
buffer2[3 * (i * dim + j)] = buffer[3 * (k * width + h)];
buffer2[3 * (i * dim + j) + 1] = buffer[3 * (k * width + h) + 1];
buffer2[3 * (i * dim + j) + 2] = buffer[3 * (k * width + h) + 2];
}
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, dim, dim, 0, GL_RGB, GL_UNSIGNED_BYTE, buffer2);
free(buffer);
free(buffer2);
}