-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtexture.c
137 lines (100 loc) · 2.99 KB
/
texture.c
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
#define PNG_DEBUG 3
#include <stdlib.h>
#include <png.h>
#include "types.h"
#include "texture.h"
rgba* load_png(char* filename, unsigned* width,
unsigned* height)
{
unsigned char header[8];
FILE *fp = fopen(filename, "rb");
if (!fp) {
printf("can't open file\n");
return 0;
}
fread(header, 1, 8, fp);
if (!png_check_sig(header, 8)) {
fclose(fp);
printf("signature error\n");
return 0;
}
png_structp png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING,
NULL, NULL, NULL);
if (!png_ptr) {
fclose(fp);
printf("no png ptr\n");
return 0;
}
png_infop info_ptr = png_create_info_struct(png_ptr);
if (!info_ptr) {
printf("no info ptr\n");
png_destroy_read_struct(&png_ptr, NULL, NULL);
fclose(fp);
return 0;
}
if (setjmp(png_jmpbuf(png_ptr))) {
png_destroy_read_struct(&png_ptr, &info_ptr, NULL);
fclose(fp);
printf("png error really rs\n");
return 0;
}
png_init_io(png_ptr, fp);
png_set_sig_bytes(png_ptr, 8);
png_read_info(png_ptr, info_ptr);
png_uint_32 w, h;
w = png_get_image_width(png_ptr, info_ptr);
h = png_get_image_height(png_ptr, info_ptr);
*width = w;
*height = h;
png_bytep *row_pointers =
(png_bytep*) malloc(sizeof(png_bytep) * h);
for (int y=0; y<h; y++) {
row_pointers[y] = (png_byte*)
malloc(png_get_rowbytes(png_ptr,info_ptr));
}
png_read_image(png_ptr, row_pointers);
fclose(fp);
rgba* image_data = malloc(4 * sizeof(rgba) * w*h);
for (int y=0; y<h; y++) {
png_byte* row = row_pointers[y];
for (int x=0; x<w; x++) {
png_byte* ptr = &(row[x*4]);
rgba* ptr2 = &(image_data[y*h]);
ptr2[x].r = ptr[0];
ptr2[x].g = ptr[1];
ptr2[x].b = ptr[2];
ptr2[x].a = ptr[3];
}
}
free(row_pointers);
return image_data;
}
GLuint setup_texture(rgba* image_data, unsigned w,
unsigned h)
{
GLuint texture;
glGenTextures(1, &texture);
glBindTexture(GL_TEXTURE_2D, texture);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER,
GL_LINEAR_MIPMAP_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
gluBuild2DMipmaps( GL_TEXTURE_2D, 4, w, h,
GL_RGBA, GL_UNSIGNED_BYTE, (GLvoid*) image_data );
return texture;
}
GLuint png_texture(char* filename)
{
unsigned w, h;
rgba* t = load_png(filename, &w, &h);
GLuint tx = setup_texture(t, w, h);
free(t);
return tx;
}
GLuint png_loadmap(char* filename, rgba** image,
unsigned* w, unsigned* h)
{
*image = load_png(filename, w, h);
return setup_texture(*image, *w, *h);
}