-
Notifications
You must be signed in to change notification settings - Fork 1
/
ParticleTexture.cpp
167 lines (135 loc) · 4.28 KB
/
ParticleTexture.cpp
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
#include "ParticleTexture.h"
/* BMP file loader loads a 24-bit bmp file only */
/*
* getint and getshort are help functions to load the bitmap byte by byte
*/
static unsigned int getint(FILE *fp) {
int c, c1, c2, c3;
/* get 4 bytes */
c = getc(fp);
c1 = getc(fp);
c2 = getc(fp);
c3 = getc(fp);
return ((unsigned int) c) +
(((unsigned int) c1) << 8) +
(((unsigned int) c2) << 16) +
(((unsigned int) c3) << 24);
}
static unsigned int getshort(FILE *fp){
int c, c1;
/* get 2 bytes*/
c = getc(fp);
c1 = getc(fp);
return ((unsigned int) c) + (((unsigned int) c1) << 8);
}
/* quick and dirty bitmap loader...for 24 bit bitmaps with 1 plane only. */
int ParticleTexture::ImageLoad(const char *filename, Image *image) {
printf("loading image\n");
FILE *file;
unsigned long size; /* size of the image in bytes. */
unsigned long i; /* standard counter. */
unsigned short int planes; /* number of planes in image (must be 1) */
unsigned short int bpp; /* number of bits per pixel (must be 24) */
char temp; /* used to convert bgr to rgb color. */
/* make sure the file is there. */
if ((file = fopen(filename, "rb"))==NULL) {
printf("File Not Found : %s\n",filename);
return 0;
}
/* seek through the bmp header, up to the width height: */
fseek(file, 18, SEEK_CUR);
/* No 100% errorchecking anymore!!! */
/* read the width */ image->sizeX = getint (file);
/* read the height */
image->sizeY = getint (file);
/* calculate the size (assuming 24 bits or 3 bytes per pixel). */
size = image->sizeX * image->sizeY * 3;
/* read the planes */
planes = getshort(file);
if (planes != 1) {
printf("Planes from %s is not 1: %u\n", filename, planes);
return 0;
}
/* read the bpp */
bpp = getshort(file);
if (bpp != 24) {
printf("Bpp from %s is not 24: %u\n", filename, bpp);
return 0;
}
/* seek past the rest of the bitmap header. */
fseek(file, 24, SEEK_CUR);
/* read the data. */
image->data = (char *) malloc(size);
if (image->data == NULL) {
printf("Error allocating memory for color-corrected image data");
return 0;
}
if ((i = fread(image->data, size, 1, file)) != 1) {
printf("Error reading image data from %s.\n", filename);
return 0;
}
for (i=0;i<size;i+=3) { /* reverse all of the colors. (bgr -> rgb) */
temp = image->data[i];
image->data[i] = image->data[i+2];
image->data[i+2] = temp;
}
fclose(file); /* Close the file and release the filedes */
/* we're done. */
return 1;
}
ParticleTexture::ParticleTexture() :
filename(""),
name(""),
tid(0),
unit(0),
handle(0)
{
}
ParticleTexture::~ParticleTexture()
{
}
void ParticleTexture::init()
{
printf("initing texture\n");
ParticleTexture::Image *image0 = (ParticleTexture::Image *)malloc(sizeof(ParticleTexture::Image));
if(image0 == NULL) {
printf("Error allocating space for image");
}
if(!ParticleTexture::ImageLoad(filename.c_str(), image0)) {
printf("Error loading texture image\n");
}
// Set active texture to texture unit 0
glActiveTexture(GL_TEXTURE0);
// Generate a texture buffer object
glGenTextures(1, &tid);
// Bind the current texture to be the newly generated texture object
glBindTexture(GL_TEXTURE_2D, tid);
// Load the actual texture data
// Base level is 0, number of channels is 3, and border is 0.
glTexImage2D(GL_TEXTURE_2D, 0, 3, image0->sizeX, image0->sizeY,
0, GL_RGB, GL_UNSIGNED_BYTE, image0->data);
// Generate image pyramid
glGenerateMipmap(GL_TEXTURE_2D);
// Set texture wrap modes for the S and T directions
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
// Set filtering mode for magnification and minimification
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
// Unbind
glBindTexture(GL_TEXTURE_2D, 0);
// Free image, since the data is now on the GPU
free(image0);
}
void ParticleTexture::bind()
{
//printf("binding texture\n");
glActiveTexture(GL_TEXTURE0 + unit);
glBindTexture(GL_TEXTURE_2D, tid);
glUniform1i(handle, unit);
}
void ParticleTexture::unbind()
{
glActiveTexture(GL_TEXTURE0 + unit);
glBindTexture(GL_TEXTURE_2D, 0);
}