-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathtexture.cpp
More file actions
123 lines (94 loc) · 2.95 KB
/
texture.cpp
File metadata and controls
123 lines (94 loc) · 2.95 KB
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
#include "texture.h"
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <png.h>
#include <iostream>
using namespace std;
bool loadPngImage(const char *name, int &outWidth, int &outHeight, bool &outHasAlpha, int &nbChanels, GLubyte **outData)
{
png_structp png_ptr;
png_infop info_ptr;
unsigned int sig_read = 0;
int color_type, interlace_type;
bool gray = false;
outHasAlpha = false;
FILE *fp;
if ((fp = fopen(name, "rb")) == NULL)
return false;
png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
if (png_ptr == NULL)
{
fclose(fp);
return false;
}
info_ptr = png_create_info_struct(png_ptr);
if (info_ptr == NULL) {
fclose(fp);
png_destroy_read_struct(&png_ptr, NULL, NULL);
return false;
}
if (setjmp(png_jmpbuf(png_ptr))) {
png_destroy_read_struct(&png_ptr, &info_ptr, NULL);
fclose(fp);
return false;
}
png_init_io(png_ptr, fp);
png_set_sig_bytes(png_ptr, sig_read);
png_read_png(png_ptr, info_ptr, PNG_TRANSFORM_STRIP_16 | PNG_TRANSFORM_PACKING | PNG_TRANSFORM_EXPAND, NULL);
png_uint_32 width, height;
int bit_depth;
png_get_IHDR(png_ptr, info_ptr, &width, &height, &bit_depth, &color_type,
&interlace_type, NULL, NULL);
outWidth = width;
outHeight = height;
if(color_type == PNG_COLOR_TYPE_GRAY || color_type == PNG_COLOR_TYPE_GRAY_ALPHA) gray = true;
if(color_type == PNG_COLOR_TYPE_RGB_ALPHA || color_type == PNG_COLOR_TYPE_GRAY_ALPHA) outHasAlpha = true;
nbChanels = (gray ? 1 : 3) + (outHasAlpha ? 1 : 0);
unsigned int row_bytes = png_get_rowbytes(png_ptr, info_ptr);
*outData = (unsigned char*) malloc(row_bytes * outHeight);
png_bytepp row_pointers = png_get_rows(png_ptr, info_ptr);
for (int i = 0; i < outHeight; i++) {
memcpy(*outData+(row_bytes * (outHeight-1-i)), row_pointers[i], row_bytes);
}
png_destroy_read_struct(&png_ptr, &info_ptr, NULL);
fclose(fp);
cout << "Image ("<< name <<") loaded " << width << "x" << height << " color(" << nbChanels << " chanels)" << endl;
return true;
}
GLuint createTexture(const char *filename)
{
GLubyte *textureImage;
int width, height, nbChanels;
bool hasAlpha;
bool success = loadPngImage(filename, width, height, hasAlpha, nbChanels, &textureImage);
if (!success) {
cout << "Unable to load png file" << endl;
return 0;
}
GLuint texture;
glGenTextures(1, &texture);
glBindTexture(GL_TEXTURE_2D, texture);
GLint format;
switch(nbChanels)
{
case 1:
format = GL_RED;
break;
case 2:
format = GL_RG;
case 3:
format = GL_RGB;
break;
case 4:
format = GL_RGBA;
break;
}
glTexImage2D(GL_TEXTURE_2D, 0, format, width, height, 0, format, GL_UNSIGNED_BYTE, textureImage);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glBindTexture(GL_TEXTURE_2D, 0);
return texture;
}