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
|
#ifdef G_OS_WIN32
#define WIN32_LEAN_AND_MEAN 1
#include <windows.h>
#define GL_BGR 0x80E0
#define GL_BGRA 0x80E1
#define GL_CLAMP_TO_EDGE 0x812F
#endif
#include <GL/gl.h>
#include <GL/glu.h>
#include <stdio.h>
#include <stdlib.h>
#include "bitmap.h"
#define BITMAP_FILESIZE 0x02
#define BITMAP_OFFSET 0x0a
#define BITMAP_HEADERSIZE 0x0e
#define BITMAP_WIDTH 0x12
#define BITMAP_HEIGHT 0x16
#define BITMAP_DEPTH 0x1c
#define BITMAP_IMAGE_SIZE 0x22
struct BitmapStruct BitmapLoad(char *filename)
{
struct BitmapStruct l_sImage;
GLuint texture;
FILE *bitmap;
// try to open the file
bitmap = fopen(filename, "r");
// does the bitmap exist?
if (bitmap > 0)
{
unsigned int dataoffset, filesize, imagesize;
short depth;
GLsizei width, height;
unsigned char *imagedata;
fseek(bitmap, BITMAP_FILESIZE, SEEK_SET);
fread(&filesize, 4, 1, bitmap);
fseek(bitmap, BITMAP_OFFSET, SEEK_SET);
fread(&dataoffset, 4, 1, bitmap);
fseek(bitmap, BITMAP_WIDTH, SEEK_SET);
fread(&width, 4, 1, bitmap);
fread(&height, 4, 1, bitmap);
fseek(bitmap, BITMAP_DEPTH, SEEK_SET);
fread(&depth, 2, 1, bitmap);
fseek(bitmap, BITMAP_IMAGE_SIZE, SEEK_SET);
fread(&imagesize, 4, 1, bitmap);
imagedata = (unsigned char *)malloc((size_t)imagesize);
fseek(bitmap, dataoffset, SEEK_SET);
fread(imagedata, (size_t)imagesize, 1, bitmap);
glGenTextures(1, &texture);
glBindTexture(GL_TEXTURE_2D, texture);
if (depth == 24) // 24 bits
{
gluBuild2DMipmaps(GL_TEXTURE_2D, GL_RGB, width, height, GL_BGR, GL_UNSIGNED_BYTE, imagedata);
}
else // depth == 32 bits
{
gluBuild2DMipmaps(GL_TEXTURE_2D, GL_RGBA, width, height, GL_BGRA, GL_UNSIGNED_BYTE, imagedata);;
}
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
l_sImage.m_iImageId = texture;
l_sImage.m_iWidth = width;
l_sImage.m_iHeight = height;
free(imagedata);
fclose(bitmap);
return l_sImage;
}
else
{
// couldn't find bitmap
exit(0);
}
}
void BitmapRender(int f_iXPos, int f_iYPos, int f_iWidth, int f_iHeight, int f_iImageId)
{
glBindTexture(GL_TEXTURE_2D, f_iImageId);
glBegin(GL_TRIANGLE_STRIP);
glTexCoord2i(0, 1); glVertex2i(f_iXPos, f_iYPos);
glTexCoord2i(1, 1); glVertex2i(f_iXPos + f_iWidth, f_iYPos);
glTexCoord2i(0, 0); glVertex2i(f_iXPos, f_iYPos + f_iHeight);
glTexCoord2i(1, 0); glVertex2i(f_iXPos + f_iWidth, f_iYPos + f_iHeight);
glEnd();
} // BitmapRender
void BitmapConvertWidth(struct BitmapStruct *f_sImage, double f_dHeight)
{
double l_dRatio;
l_dRatio = (double)f_sImage->m_iHeight / f_dHeight;
f_sImage->m_iWidth = (int)(f_sImage->m_iWidth / l_dRatio);
} // ConvertButton
|