-
Notifications
You must be signed in to change notification settings - Fork 1
/
load_and_bind_texture.h
47 lines (30 loc) · 1.22 KB
/
load_and_bind_texture.h
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
#ifndef LOAD_AND_BIND_TEXTURE_H
#define LOAD_AND_BIND_TEXTURE_H
#include <GL/glut.h>
unsigned int load_and_bind_texture(const char* filename)
{
char* image_buffer = NULL; // the image data
int width = 0, height = 0;
// read in the PNG image data into image_buffer
if (png_load(filename, &width, &height, &image_buffer)==0)
{
fprintf(stderr, "Failed to read image texture from %s\n", filename);
exit(1);
}
// request one texture handle
unsigned int tex_handle = 0;
glGenTextures(1, &tex_handle);
// create a new texture object and bind it to tex_handle
glBindTexture(GL_TEXTURE_2D, tex_handle);
// so we can blend it
glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, (GLvoid*)image_buffer);
glBindTexture(GL_TEXTURE_2D,0);
free(image_buffer); // free the image buffer memory
return tex_handle;
}
#endif