aboutsummaryrefslogtreecommitdiff
path: root/src/texture.c
blob: 9476d75f8956319f27df1a8dfe8a58a3ea932f64 (plain)
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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
/*
 * texture.c
 *
 * Handle textures
 *
 * (c) 2008 Thomas White <taw27@cam.ac.uk>
 *
 *  thrust3d - a silly game
 *
 */

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include <png.h>
#include <stdlib.h>
#include <glew.h>
#include <glu.h>

#include "render.h"

static int texture_load_png(char *filename, GLuint *name) {

	FILE *fh;
	png_bytep header;
	png_structp png_ptr;
	png_infop info_ptr;
	png_infop end_info;
	unsigned int width;
	unsigned int height;
	unsigned int bit_depth;
	unsigned int channels;
	png_bytep *row_pointers;
	unsigned int x;
	unsigned int y;
	uint8_t *texels;
	
	/* Open file */
	fh = fopen(filename, "rb");
	if ( !fh ) {
		return 1;
	}
	
	/* Check it's actually a PNG file */
	header = malloc(8);
	fread(header, 1, 8, fh);
	if ( png_sig_cmp(header, 0, 8)) {
		fprintf(stderr, "Texture file '%s' is not a PNG file.\n", filename);
		free(header);
		fclose(fh);
		return 1;
	}
	free(header);
	
	png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
	if ( !png_ptr ) {
		fprintf(stderr, "Couldn't create PNG read structure.\n");
		fclose(fh);
		return 1;
	}
	
	info_ptr = png_create_info_struct(png_ptr);
	if ( !info_ptr ) {
		png_destroy_read_struct(&png_ptr, (png_infopp)NULL, (png_infopp)NULL);
		fprintf(stderr, "Couldn't create PNG info structure.\n");
		fclose(fh);
		return 1;
	}
	
	end_info = png_create_info_struct(png_ptr);
	if ( !end_info ) {
		png_destroy_read_struct(&png_ptr, &info_ptr, (png_infopp)NULL);
		printf("Couldn't create PNG end info structure.\n");
		fclose(fh);
		return 1;
	}
	
	if ( setjmp(png_jmpbuf(png_ptr)) ) {
		png_destroy_read_struct(&png_ptr, &info_ptr, &end_info);
		fclose(fh);
		fprintf(stderr, "PNG read failed.\n");
		return 1;
	}
	
	png_init_io(png_ptr, fh);
	png_set_sig_bytes(png_ptr, 8);
	
	/* Read! */
	png_read_png(png_ptr, info_ptr, PNG_TRANSFORM_IDENTITY, NULL);
	
	width = png_get_image_width(png_ptr, info_ptr);
	height = png_get_image_height(png_ptr, info_ptr);
	bit_depth = png_get_bit_depth(png_ptr, info_ptr);
	channels = png_get_channels(png_ptr, info_ptr);
	if ( bit_depth != 8 ) {
		fprintf(stderr, "Texture image '%s' doesn't have 8 bits per channel per pixel.\n", filename);
		png_destroy_read_struct(&png_ptr, &info_ptr, &end_info);
		fclose(fh);
		return 1;
	}
	if ( channels != 4 ) {
		fprintf(stderr, "Texture image '%s' doesn't have 4 channels.\n", filename);
		png_destroy_read_struct(&png_ptr, &info_ptr, &end_info);
		fclose(fh);
		return 1;
	}
	
	/* Get image data */
	row_pointers = png_get_rows(png_ptr, info_ptr);
	
	texels = malloc(4*width*height);
	for ( y=0; y<height; y++ ) {
		for ( x=0; x<width; x++ ) {
		
			unsigned int r, g, b, a;

			r = row_pointers[y][(channels*x)+0];
			g = row_pointers[y][(channels*x)+1];
			b = row_pointers[y][(channels*x)+2];
			a = row_pointers[y][(channels*x)+3];
			
			texels[4*(x + width*(height-1-y)) + 0] = r;
			texels[4*(x + width*(height-1-y)) + 1] = g;
			texels[4*(x + width*(height-1-y)) + 2] = b;
			texels[4*(x + width*(height-1-y)) + 3] = a;
			
		}
	}
	
	glGenTextures(1, name);
	glBindTexture(GL_TEXTURE_2D, *name);
	glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
	glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
	glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
	glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
	gluBuild2DMipmaps(GL_TEXTURE_2D, GL_RGBA8, width, height, GL_RGBA, GL_UNSIGNED_BYTE, texels);
	free(texels);
	
	png_destroy_read_struct(&png_ptr, &info_ptr, &end_info);
	fclose(fh);
	
	return 0;

}

void texture_load(RenderContext *ctx, char *name) {

	GLuint colourmap, normalmap;
	char colourmap_filename[256];
	char normalmap_filename[256];
	
	snprintf(colourmap_filename, 255, "%s/textures/%s.png", DATADIR, name);
	snprintf(normalmap_filename, 255, "%s/textures/%s-normals.png", DATADIR, name);
	
	if ( texture_load_png(colourmap_filename, &colourmap) != 0 ) {
		fprintf(stderr, "Couldn't load texture '%s'\n", name);
		return;
	}
	ctx->textures[ctx->num_textures].texname = colourmap;
	
	if ( texture_load_png(normalmap_filename, &normalmap) == 0 ) {
		ctx->textures[ctx->num_textures].normalmap = normalmap;
		ctx->textures[ctx->num_textures].has_normals = 1;
	} else {
		ctx->textures[ctx->num_textures].has_normals = 0;
	}
	
	ctx->textures[ctx->num_textures].name = strdup(name);
	ctx->num_textures++;

}

Texture *texture_lookup(RenderContext *ctx, const char *name) {
	
	int i, found;
	
	found = 0;
	for ( i=0; i<ctx->num_textures; i++ ) {
		if ( strcmp(ctx->textures[i].name, name) == 0 ) {
			found = 1;
			break;
		}
	}
	
	if ( found == 0 ) {
		return NULL;
	}
	
	return &(ctx->textures[i]);
	
}

void texture_free_all(RenderContext *ctx) {
	
	int i;
	
	for ( i=0; i<ctx->num_textures; i++ ) {
		glDeleteTextures(1, &(ctx->textures[ctx->num_textures].texname));
	}
	
}