-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBitMap.h
More file actions
157 lines (123 loc) · 3.5 KB
/
BitMap.h
File metadata and controls
157 lines (123 loc) · 3.5 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
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
#ifndef __BITMAP_H_INCLUDE__
#define __BITMAP_H_INCLUDE__
#include <string>
#include <deque>
#include <iostream>
//#include <cstdlib>
#include <fstream>
#ifdef __APPLE__
# include <GLUT/glut.h>
#else
# include <GL/glut.h>
#endif
class BitMap{
public:
int sizeX;
int sizeY;
unsigned char *data;
// Texture binding variables
unsigned int *texture; // Array of texture indices.
public:
std::deque<std::string> sources; // filenames
int selectedSource;
BitMap();
BitMap(std::string filename);
void reload();
BitMap* loadBMP(std::string filename); // Load 24-bit uncompressed bitmap file
// Texture functions
void setupTexture();
private:
void loadProceduralTextures(); // Load image data as a texture. Called by setupTexture()
};
BitMap::BitMap()
{
sizeX = sizeY = -1;
data = nullptr;
texture = new unsigned int[1];
selectedSource = 0;
// Get the file names
std::ifstream in("gliderTextureFilenames.txt");
while (!in.eof())
{
std::string st;
in >> st;
sources.push_back(st);
}
if (sources.size() > 0)
loadBMP(sources[selectedSource]);
else
{
std::cout << "No files given" << std::endl;
return;
}
if (sizeX == 0 && sizeY == 0)
std::cout << "Image file: " << sources[selectedSource] << " wasn't found" << std::endl;
}
BitMap::BitMap(std::string filename)
{
texture = new unsigned int[1];
loadBMP(filename);
// Note if the image wasn't loaded
if (sizeX == 0 && sizeY == 0)
std::cout << "Image file: " << filename << " wasn't found" << std::endl;
}
// Load the currently selected file
void BitMap::reload()
{
if (selectedSource < sources.size() && selectedSource >= 0)
loadBMP(sources[selectedSource]);
}
// Load the BitMap data
// Returns this BitMap object
BitMap* BitMap::loadBMP(std::string filename)
{
unsigned int size, offset, headerSize;
// Read input file name.
std::ifstream infile(filename.c_str(), std::ios::binary);
// Get the starting point of the image data.
infile.seekg(10);
infile.read((char *)&offset, 4);
// Get the header size of the bitmap.
infile.read((char *)&headerSize, 4);
// Get width and height values in the bitmap header.
infile.seekg(18);
infile.read((char *)&this->sizeX, 4);
infile.read((char *)&this->sizeY, 4);
// Allocate buffer for the image.
size = this->sizeX * this->sizeY * 24;
this->data = new unsigned char[size];
// Read bitmap data.
infile.seekg(offset);
infile.read((char *)this->data, size);
// Reverse color from bgr to rgb.
int temp;
for (int i = 0; i < size; i += 3)
{
temp = this->data[i];
this->data[i] = this->data[i + 2];
this->data[i + 2] = temp;
}
return this;
}
void BitMap::setupTexture()
{
glEnableClientState(GL_VERTEX_ARRAY);
glEnableClientState(GL_TEXTURE_COORD_ARRAY);
glGenTextures(1, texture); // Create texture index array.
loadProceduralTextures(); // Load image data as a texture.
//glEnable(GL_TEXTURE_2D); // Enable OpenGL texturing
//// Specify how texture values combine with current surface color values.
//glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
}
// Load image data as a texture.
void BitMap::loadProceduralTextures()
{
// Bind chessboard image to texture index[0].
glBindTexture(GL_TEXTURE_2D, texture[0]);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
gluBuild2DMipmaps(GL_TEXTURE_2D, GL_RGB, 64, 64, GL_RGB, GL_UNSIGNED_BYTE, this->data);
}
#endif