-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtexture.cpp
More file actions
129 lines (97 loc) · 3.36 KB
/
texture.cpp
File metadata and controls
129 lines (97 loc) · 3.36 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
124
125
126
127
128
129
/*
Copyright 2011 Etay Meiri
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 <iostream>
#include "common.h"
#include "texture.h"
Texture::Texture(GLenum TextureTarget, const std::string& FileName)
{
m_textureTarget = TextureTarget;
m_fileName = FileName;
}
bool Texture::Load()
{
char *ImgData = NULL;
int ImgWidth;
int ImgHeight;
LoadBMP(m_fileName.c_str(),ImgWidth,ImgHeight,&ImgData);
glGenTextures(1, &m_textureObj);
glBindTexture(m_textureTarget, m_textureObj);
glTexImage2D(m_textureTarget, 0, GL_RGB, ImgWidth, ImgHeight, 0, GL_BGR, GL_UNSIGNED_BYTE, ImgData);
free(ImgData);
glTexParameterf(m_textureTarget, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameterf(m_textureTarget, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
return true;
}
void Texture::Bind(GLenum TextureUnit)
{
glActiveTexture(TextureUnit);
glBindTexture(m_textureTarget, m_textureObj);
}
void Texture::LoadBMP(const char *filename,int &_width, int &_height, char **pixels)
{
char buf[100]; // 100B temporary buffer
// some essential header fields
// to be compared against the assumed bmp format
int file_actual_size;
int file_size;
int bmp_compression;
// file handle
FILE *fil = NULL;
fopen_s(&fil,filename,"rb");
// check if file is opened
if (fil == NULL) ThrowException("Brak pliku BMP");
fseek (fil, 0, SEEK_END);
file_actual_size=ftell (fil); // obtain real filesize
rewind(fil); // make sure the file pointer is set to the beginning
//*********** READ THE HEADER ***************
// read the signature
fread(buf,1,2,fil);
if ((buf[0] !=0x42) || (buf[1] != 0x4d)) {fclose(fil);
//ThrowException("Invalid signature");
}
// read the filesize
fread(&file_size,sizeof(file_size),1,fil);
if (file_size != file_actual_size) {
//ThrowException("Invalid file size");
}
// 12 dummies
fread(buf,1,12,fil);
fread(&_width,sizeof(_width),1,fil);
fread(&_height,sizeof(_height),1,fil);
// 2 dummies
fread(buf,1,2,fil);
// bits per pixel
fread(buf,1,1,fil);
if (buf[0] != 24) ;//ThrowException("Invalid BPP");
// 1 dummy
fread(buf,1,1,fil);
//compression mode
fread(&bmp_compression,sizeof(bmp_compression),1,fil);
if (bmp_compression != 0) ;//ThrowException("Invalid Compression");; // invalid compression
// 20 dummies
fread(buf,1,20,fil);
//********************* END OF THE HEADER ***********************
// determine padding constant
int padding = 0;
if (_width*3 % 4) padding = 4 - (_width*3) % 4;
// allocate memory for pixeltable
*pixels = (char *)calloc((_width*3+padding)*_height,sizeof(char));
// read file content into pixeltable
for (int h = 0; h < _height; h++)
{
fread(*pixels+h*(_width*3+padding),1,_width*3+padding,fil);
}
fclose(fil);
}