1 | #include "images.h"
|
2 |
|
3 | #include "../soil/SOIL.h"
|
4 |
|
5 | #include "config.h"
|
6 |
|
7 | #ifdef USE_GLES
|
8 | #include <GLES2/gl2.h>
|
9 | #include <GLES2/gl2ext.h>
|
10 | #else
|
11 | #include <GL/glew.h>
|
12 | #endif
|
13 |
|
14 | #include <stdio.h>
|
15 | #include <stdlib.h>
|
16 | #include <string.h>
|
17 |
|
18 | GLuint loadOpenGLTexture(const char *filename)
|
19 | {
|
20 | int width;
|
21 | int height;
|
22 | unsigned char *data;
|
23 | unsigned char *temp;
|
24 | int i;
|
25 | GLuint result = 0;
|
26 |
|
27 | // much faster than using SOIL_load_OGL_texture(), plus this doesn't use any deprecated functionality that is illegal in 3.2 core contexts
|
28 | data = SOIL_load_image(filename, &width, &height, 0, SOIL_LOAD_RGBA);
|
29 |
|
30 | // make sure the load was successful
|
31 | if(data)
|
32 | {
|
33 | // the pixel data is flipped vertically, so we need to flip it back; this uses an in-place reversal
|
34 | temp = (unsigned char*)malloc(sizeof(unsigned char) * width * 4); // enough space for one row of RGBA pixels
|
35 | for(i = 0; i < height / 2; i ++)
|
36 | {
|
37 | memcpy(temp, &data[i * width * 4], (width * 4)); // copy row into temp array
|
38 | memcpy(&data[i * width * 4], &data[(height - i - 1) * width * 4], (width * 4)); // copy other side of array into this row
|
39 | memcpy(&data[(height - i - 1) * width * 4], temp, (width * 4)); // copy temp into other side of array
|
40 | }
|
41 | free(temp);
|
42 |
|
43 | // we can generate a texture object since we had a successful load
|
44 | glGenTextures(1, &result);
|
45 | glActiveTexture(GL_TEXTURE0);
|
46 | glBindTexture(GL_TEXTURE_2D, result);
|
47 | glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, data);
|
48 |
|
49 | // we want textures to be wrappable
|
50 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
|
51 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
|
52 |
|
53 | // we also want mipmapping on for maximum prettiness
|
54 | glGenerateMipmap(GL_TEXTURE_2D);
|
55 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
|
56 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
|
57 |
|
58 | // release the memory used to perform the loading
|
59 | SOIL_free_image_data(data);
|
60 | }
|
61 | else
|
62 | {
|
63 | fprintf(stderr, "loadOpenGLTexture() could not load '%s'\n" , filename);
|
64 | exit(1);
|
65 | }
|
66 |
|
67 | return result;
|
68 | }
|
69 | |