-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtga.cpp
More file actions
104 lines (94 loc) · 3.6 KB
/
Copy pathtga.cpp
File metadata and controls
104 lines (94 loc) · 3.6 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
#include "tga.h"
#include <cstdio>
#include <cstdlib>
#include <Windows.h>
#include <gl\GL.h>
#include <gl\GLU.h>
#pragma pack(1)
struct TGAHeader{
char id_length;
char map_type;
char type;
short map_start;
short map_length;
char map_depth;
short x_origin;
short y_origin;
short width;
short height;
char bpp;
char descriptor_bits;
};
#pragma pack()
TGA::TGA(char* imagePath) {
FILE* file = NULL;
TGAHeader header;
char* pixels, *buffer;
int c, r, i, bytespp = 4;
char n, packet_header;
char pixel[4];
file = fopen(imagePath, "rb");
fread(&header, 18, 1, file);
bytespp = header.bpp / 8;
pixels = (char*)malloc(bytespp * header.width * header.height);
if (header.type == 2) {
fseek(file, header.map_start + header.map_length * bytespp + 18, SEEK_SET);
buffer = (char*)malloc(bytespp * header.width * header.height);
fread(buffer, bytespp, header.width * header.height, file);
for (c = 0; c < header.width; c ++) {
for (r = 0; r < header.height; r ++) {
if (bytespp == 4) {
pixels[(c + (header.height - r - 1) * header.width) * bytespp + 0] = buffer[(c + r * header.width) * bytespp + 2];
pixels[(c + (header.height - r - 1) * header.width) * bytespp + 1] = buffer[(c + r * header.width) * bytespp + 1];
pixels[(c + (header.height - r - 1) * header.width) * bytespp + 2] = buffer[(c + r * header.width) * bytespp + 0];
pixels[(c + (header.height - r - 1) * header.width) * bytespp + 3] = buffer[(c + r * header.width) * bytespp + 3];
} else if (bytespp == 3) {
pixels[(c + (header.height - r - 1) * header.width) * bytespp + 0] = buffer[(c + r * header.width) * bytespp + 2];
pixels[(c + (header.height - r - 1) * header.width) * bytespp + 1] = buffer[(c + r * header.width) * bytespp + 1];
pixels[(c + (header.height - r - 1) * header.width) * bytespp + 2] = buffer[(c + r * header.width) * bytespp + 0];
}
}
}
free(buffer);
} else if (header.type == 10) {
fseek(file, header.map_start + header.map_length * bytespp + 18, SEEK_SET);
c = 0; r = header.height - 1;
while (r >= 0) {
fread(&packet_header, sizeof(packet_header), 1, file);
n = packet_header & 0x7F;
*((int*)pixel) = 0;
if (n != packet_header) fread(&pixel, bytespp, 1, file);
for (i = 0; i < n + 1; i++) {
if (n == packet_header) fread(&pixel, bytespp, 1, file);
if (bytespp == 4) {
pixels[(c + r * header.width) * bytespp + 0] = pixel[2];
pixels[(c + r * header.width) * bytespp + 1] = pixel[1];
pixels[(c + r * header.width) * bytespp + 2] = pixel[0];
pixels[(c + r * header.width) * bytespp + 3] = pixel[3];
} else if (bytespp == 3) {
pixels[(c + r * header.width) * bytespp + 0] = pixel[2];
pixels[(c + r * header.width) * bytespp + 1] = pixel[1];
pixels[(c + r * header.width) * bytespp + 2] = pixel[0];
}
c += 1;
if (c >= header.width) {
c = 0;
r -= 1;
}
}
}
}
fclose(file);
glGenTextures(1, &textureHandle);
glBindTexture(GL_TEXTURE_2D, textureHandle);
glTexEnvf( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE );
glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_NEAREST );
glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR );
glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT );
glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT );
gluBuild2DMipmaps( GL_TEXTURE_2D, 3, header.width, header.height, GL_RGB, GL_UNSIGNED_BYTE, pixels);
free(pixels);
}
GLuint TGA::getTextureHandle(void) {
return textureHandle;
}