-
Notifications
You must be signed in to change notification settings - Fork 0
/
texture.cpp
240 lines (194 loc) · 8.23 KB
/
texture.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
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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
/*
* graphic depictions, a visual workbench for graphs
*
* Copyright (C) 2016 Matvey Soloviev
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "stdafx.h"
#include "texture.h"
void CSTexture::BuildPOT(BMPImage *img)
{
int w,h;
float sw,sh;
w = 1 << (int)ceil((log((double)img->width)/log(2.0f)));
h = 1 << (int)ceil((log((double)img->height)/log(2.0f)));
sw=(float)img->width/w;
sh=(float)img->height/h;
unsigned char *nbuf = new unsigned char[w*h*4];
for(int y=0; y<h;++y) for(int x=0;x<w;++x) {
nbuf[y*w*4+x*4]=img->data[(int)(sh*y)*img->width*4+(int)(sw*x)*4];
nbuf[y*w*4+x*4+1]=img->data[(int)(sh*y)*img->width*4+(int)(sw*x)*4+1];
nbuf[y*w*4+x*4+2]=img->data[(int)(sh*y)*img->width*4+(int)(sw*x)*4+2];
nbuf[y*w*4+x*4+3]=img->data[(int)(sh*y)*img->width*4+(int)(sw*x)*4+3];
}
//gluBuild2DMipmaps ( GL_TEXTURE_2D, GL_RGBA, w, h, GL_RGBA, GL_UNSIGNED_BYTE, nbuf);
glTexImage2D(GL_TEXTURE_2D,0,GL_RGBA,w,h,0,GL_RGBA,GL_UNSIGNED_BYTE,nbuf);
delete[] nbuf;
}
void CSTexture::LoadBitmap(char *filename)
{
BMPImage *pImage=new BMPImage;
FILE *pFile = NULL;
unsigned short nNumPlanes;
unsigned short nNumBPP;
int i;
if( (pFile = fopen(filename, "rb") ) == NULL )
printf("ERROR: getBitmapImageData - %s not found\n",filename);
// Seek forward to width and height info
fseek( pFile, 18, SEEK_CUR );
if( (i = fread(&pImage->width, 4, 1, pFile) ) != 1 )
printf("ERROR: getBitmapImageData - Couldn't read width from %s.\n", filename);
if( (i = fread(&pImage->height, 4, 1, pFile) ) != 1 )
printf("ERROR: getBitmapImageData - Couldn't read height from %s.\n", filename);
if( (fread(&nNumPlanes, 2, 1, pFile) ) != 1 )
printf("ERROR: getBitmapImageData - Couldn't read plane count from %s.\n", filename);
if( nNumPlanes != 1 )
printf( "ERROR: getBitmapImageData - Plane count from %s is not 1: %u\n", filename, nNumPlanes );
if( (i = fread(&nNumBPP, 2, 1, pFile)) != 1 )
printf( "ERROR: getBitmapImageData - Couldn't read BPP from %s.\n", filename );
// Seek forward to image data
fseek( pFile, 24, SEEK_CUR );
if( nNumBPP != 24 ) {
//BGRA
int nTotalImagesize = (pImage->width * pImage->height) * 4;
xs=pImage->width;
ys=pImage->height;
pImage->data = (char*) malloc( nTotalImagesize );
data=(unsigned char*)pImage->data;
if( (i = fread(pImage->data, nTotalImagesize, 1, pFile) ) != 1 )
printf("ERROR: getBitmapImageData - Couldn't read image data from %s.\n", filename);
bool hasalpha=false;
alpha=false;
for(int i = 0; i < xs*ys*4;i+=4)
{
unsigned char temp = pImage->data[i];
pImage->data[i ] = pImage->data[i+2];
pImage->data[i+2] = temp;
if(pImage->data[i+3]!=0) {
hasalpha=true;
if(pImage->data[i+3]!=0xFF) alpha=true;
}
//data[i+3] = 0xFF;
}
if(!hasalpha) for(int i=0;i<xs*ys*4;i+=4) pImage->data[i+3]=0xFF;
glGenTextures(1, &tid); // Create The Texture
// Typical Texture Generation Using Data From The Bitmap
glBindTexture(GL_TEXTURE_2D, tid); // Bind To The Texture ID
glTexParameteri(GL_TEXTURE_2D, 0x8191, GL_FALSE);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR); // (Modify This For The Type Of Filtering You Want)
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR); // (Modify This For The Type Of Filtering You Want)
// (Modify This If You Want Mipmaps)
BuildPOT(pImage);
//gluBuild2DMipmaps ( GL_TEXTURE_2D, GL_RGBA, pImage->width, pImage->height, GL_RGBA, GL_UNSIGNED_BYTE, pImage->data);
//glTexImage2D( GL_TEXTURE_2D, 0, GL_RGBA, pImage->width, pImage->height, 0, GL_RGBA, GL_UNSIGNED_BYTE, pImage->data);
/*int i,j;
glGetTexLevelParameteriv(GL_TEXTURE_2D,0,GL_TEXTURE_WIDTH,&i);
glGetTexLevelParameteriv(GL_TEXTURE_2D,0,GL_TEXTURE_HEIGHT,&j);
char buf[32];
sprintf(buf,"%d,%d",i,j);
MessageBox(NULL,buf,"",0);*/
} else {
//BGR
// Calculate the image's total size in bytes. Note how we multiply the
// result of (width * height) by 3. This is becuase a 24 bit color BMP
// file will give you 3 bytes per pixel.
int nTotalImagesize = (pImage->width * pImage->height) * 3;
xs=pImage->width;
ys=pImage->height;
pImage->data = (char*) malloc( xs*ys*4 );
data=(unsigned char*)pImage->data;
if( (i = fread(pImage->data, nTotalImagesize, 1, pFile) ) != 1 )
printf("ERROR: getBitmapImageData - Couldn't read image data from %s.\n", filename);
//
// Finally, rearrange BGR to RGBA
//
char charTemp;
for( i = nTotalImagesize-3; i >= 0; i -= 3 )
{
charTemp = pImage->data[i];
pImage->data[(i*4)/3] = pImage->data[i+2];
pImage->data[(i*4)/3+1] = pImage->data[i+1];
pImage->data[(i*4)/3+2] = charTemp;
pImage->data[(i*4)/3+3] = 0xFF;
}
glGenTextures(1, &tid); // Create The Texture
// Typical Texture Generation Using Data From The Bitmap
glBindTexture(GL_TEXTURE_2D, tid); // Bind To The Texture ID
glTexParameteri(GL_TEXTURE_2D, 0x8191, GL_FALSE);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR); // (Modify This For The Type Of Filtering You Want)
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR); // (Modify This For The Type Of Filtering You Want)
// (Modify This If You Want Mipmaps)
BuildPOT(pImage);
//gluBuild2DMipmaps ( GL_TEXTURE_2D, GL_RGBA, pImage->width, pImage->height, GL_RGBA, GL_UNSIGNED_BYTE, pImage->data);
}
delete pImage;
return;
}
void CSTexture::CKCopy(CSTexture *src,int r,int g,int b)
{
xs=src->xs;
ys=src->ys;
alpha=src->alpha;
data=(unsigned char*)malloc(xs*ys*4);
for(int i=0;i<xs*ys*4;i+=4) {
if(src->data[i]==r && src->data[i+1]==g && src->data[i+2]==b) {
memset(data+i,0,4);
} else
memcpy(data+i,src->data+i,4);
}
glGenTextures(1, &tid); // Create The Texture
// Typical Texture Generation Using Data From The Bitmap
glBindTexture(GL_TEXTURE_2D, tid); // Bind To The Texture ID
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR); // (Modify This For The Type Of Filtering You Want)
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR); // (Modify This For The Type Of Filtering You Want)
// (Modify This If You Want Mipmaps)
gluBuild2DMipmaps ( GL_TEXTURE_2D, GL_RGBA, xs, ys, GL_RGBA, GL_UNSIGNED_BYTE, data);
return;
}
void CSTexture::SubareaCopy(CSTexture *src,int x1,int y1,int x2,int y2)
{
xs=x2-x1;
ys=y2-y1;
alpha=src->alpha;
data=(unsigned char*)malloc(xs*ys*4);
for(int y=y1;y<y2;++y) {
memcpy(data+(y-y1)*xs*4,src->data+(y*src->xs*4+x1*4),(x2-x1)*4);
}
glGenTextures(1, &tid); // Create The Texture
// Typical Texture Generation Using Data From The Bitmap
glBindTexture(GL_TEXTURE_2D, tid); // Bind To The Texture ID
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_NEAREST); // (Modify This For The Type Of Filtering You Want)
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_NEAREST); // (Modify This For The Type Of Filtering You Want)
BMPImage img;
img.height=ys;
img.width=xs;
img.data=(char*)data;
BuildPOT(&img);
// (Modify This If You Want Mipmaps)
//gluBuild2DMipmaps ( GL_TEXTURE_2D, GL_RGBA, xs, ys, GL_RGBA, GL_UNSIGNED_BYTE, data);
return;
}
void CSTexture::Bind()
{
/*if(alpha) {
glEnable(GL_BLEND);
glDisable(GL_ALPHA_TEST);
} else {
glDisable(GL_BLEND);
glEnable(GL_ALPHA_TEST);
glAlphaFunc(GL_GREATER,0.5);
}*/
glBindTexture(GL_TEXTURE_2D,tid);
}