-
Notifications
You must be signed in to change notification settings - Fork 0
/
textureLoader.cpp
192 lines (172 loc) · 4.7 KB
/
textureLoader.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
/**
* File: textureLoader.cpp
*
* Author1: Akshay Tiwari (2018A7PS0201H)
* Author2: Mushkan Surekha (2018B5A70477H)
* Author3: Mahesh Swaminathan (2018A4PS0982H)
* Date: 09-04-2020
* Course: Computer Graphics (IS F311)
*
* Summary of File:
*
* Program to load texture given in a .DDS file using a
* simple parser and then generate a texture ID for
* this texture.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <glad.h>
#include "textureLoader.hpp"
#include <GLFW/glfw3.h>
/**
* GLuint loadBMP(const char* imagepath)
*
* Summary of loadBMP function:
*
* Loads a .bmp texture.
*
* Parameters :
*
* File path of the texture
*
* Return Value :
*
* Texture ID of the read texture
*
* Description :
*
* Loads a .bmp texture.
*/
GLuint loadBMP(const char* imagepath) {
printf("Reading image %s\n", imagepath);
unsigned char header[54];
unsigned int dataPos;
unsigned int imageSize;
unsigned int width, height;
unsigned char* data;
FILE* file = fopen(imagepath, "rb");
if (!file) {
printf("%s could not be opened.\n", imagepath);
getchar();
return 0;
}
if (fread(header, 1, 54, file) != 54) {
printf("Not a correct BMP file\n");
fclose(file);
return 0;
}
if (header[0] != 'B' || header[1] != 'M') {
printf("Not a correct BMP file\n");
fclose(file);
return 0;
}
if (*(int*)&(header[0x1E]) != 0) { printf("Not a correct BMP file\n"); fclose(file); return 0; }
if (*(int*)&(header[0x1C]) != 24) { printf("Not a correct BMP file\n"); fclose(file); return 0; }
dataPos = *(int*)&(header[0x0A]);
imageSize = *(int*)&(header[0x22]);
width = *(int*)&(header[0x12]);
height = *(int*)&(header[0x16]);
if (imageSize == 0) imageSize = width * height * 3;
if (dataPos == 0) dataPos = 54;
data = new unsigned char[imageSize];
fread(data, 1, imageSize, file);
fclose(file);
GLuint textureID;
glGenTextures(1, &textureID);
glBindTexture(GL_TEXTURE_2D, textureID);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_BGR, GL_UNSIGNED_BYTE, data);
delete[] data;
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_MIPMAP_LINEAR);
glGenerateMipmap(GL_TEXTURE_2D);
return textureID;
}
#define FOURCC_DXT1 0x31545844 // Equivalent to "DXT1" in ASCII
#define FOURCC_DXT3 0x33545844 // Equivalent to "DXT3" in ASCII
#define FOURCC_DXT5 0x35545844 // Equivalent to "DXT5" in ASCII
/**
* GLuint loadDDS(const char* imagepath)
*
* Summary of loadDDS function:
*
* Loads a .DDS texture.
*
* Parameters :
*
* File path of the texture
*
* Return Value :
*
* Texture ID of the read texture
*
* Description :
*
* Loads a .DDS texture.
*/
GLuint loadDDS(const char* imagepath) {
unsigned char header[124];
FILE* fp;
fp = fopen(imagepath, "rb");
if (fp == NULL) {
printf("%s could not be opened.\n", imagepath); getchar();
return 0;
}
char filecode[4];
fread(filecode, 1, 4, fp);
if (strncmp(filecode, "DDS ", 4) != 0) {
fclose(fp);
return 0;
}
fread(&header, 124, 1, fp);
unsigned int height = *(unsigned int*)&(header[8]);
unsigned int width = *(unsigned int*)&(header[12]);
unsigned int linearSize = *(unsigned int*)&(header[16]);
unsigned int mipMapCount = *(unsigned int*)&(header[24]);
unsigned int fourCC = *(unsigned int*)&(header[80]);
unsigned char* buffer;
unsigned int bufsize;
bufsize = mipMapCount > 1 ? linearSize * 2 : linearSize;
buffer = (unsigned char*)malloc(bufsize * sizeof(unsigned char));
fread(buffer, 1, bufsize, fp);
fclose(fp);
unsigned int components = (fourCC == FOURCC_DXT1) ? 3 : 4;
unsigned int format;
switch (fourCC)
{
case FOURCC_DXT1:
format = GL_COMPRESSED_RGBA_S3TC_DXT1_EXT;
break;
case FOURCC_DXT3:
format = GL_COMPRESSED_RGBA_S3TC_DXT3_EXT;
break;
case FOURCC_DXT5:
format = GL_COMPRESSED_RGBA_S3TC_DXT5_EXT;
break;
default:
free(buffer);
return 0;
}
GLuint textureID;
glGenTextures(1, &textureID);
glBindTexture(GL_TEXTURE_2D, textureID);
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
unsigned int blockSize = (format == GL_COMPRESSED_RGBA_S3TC_DXT1_EXT) ? 8 : 16;
unsigned int offset = 0;
/* load the mipmaps */
for (unsigned int level = 0; level < mipMapCount && (width || height); ++level)
{
unsigned int size = ((width + 3) / 4) * ((height + 3) / 4) * blockSize;
glCompressedTexImage2D(GL_TEXTURE_2D, level, format, width, height,
0, size, buffer + offset);
offset += size;
width /= 2;
height /= 2;
if (width < 1) width = 1;
if (height < 1) height = 1;
}
free(buffer);
return textureID;
}