-
Notifications
You must be signed in to change notification settings - Fork 0
/
GLTexture.h
132 lines (114 loc) · 3.19 KB
/
GLTexture.h
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
/*
* Copyright (c) 2005-2015 Michael Shafae.
* All rights reserved.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY, to the extent permitted by law; without even the
* implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
*
* $Id: GLTexture.h 5640 2015-04-01 16:26:14Z mshafae $
*
* Utility functions for loading a texture.
*
* STUDENTS _MAY_ NEED TO MAKE CHANGES TO ACCOMMODATE IMAGE TYPES USED.
*
*/
#ifdef _WIN32
#pragma warning(disable : 4996)
#endif
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cassert>
#ifdef _WIN32
#include <Windows.h>
#endif
#ifdef __APPLE__
/* Apple's weird location of their OpenGL & GLUT implementation */
#include <OpenGL/OpenGL.h>
#include <OpenGL/glu.h>
#else
#include <GL/gl.h>
#include <GL/glu.h>
#endif
#include <FreeImage.h>
#ifndef _GLTEXTURE_H_
#define _GLTEXTURE_H_
// An almost-all-purpose image loader using FreeImage
FIBITMAP* FreeImageGenericLoader(const char* filename, int flag){
FIBITMAP* dib = NULL;
FREE_IMAGE_FORMAT fif = FreeImage_GetFileType(filename, 0);
if( fif == FIF_UNKNOWN ){
fif = FreeImage_GetFIFFromFilename(filename);
}
if( (fif != FIF_UNKNOWN) && FreeImage_FIFSupportsReading(fif)){
dib = FreeImage_Load(fif, filename, flag);
assert(dib);
}
return dib;
}
class Texture2D{
private:
char *_filename;
GLuint _id;
GLint _minFilter;
GLint _magFilter;
public:
Texture2D(char *filename, GLint minFilter = GL_NEAREST, GLint magFilter = GL_NEAREST){
_filename = filename;
_minFilter = minFilter;
_magFilter = magFilter;
FIBITMAP *bitmap = FreeImageGenericLoader(_filename, 0);
assert(bitmap);
FREE_IMAGE_COLOR_TYPE fic = FreeImage_GetColorType(bitmap);
assert( fic == FIC_RGB || fic == FIC_RGBALPHA );
unsigned int bpp = FreeImage_GetBPP(bitmap);
assert(bpp == 24 || bpp == 32);
GLenum pixelFormat;
if(bpp == 24){
pixelFormat = GL_RGB;
}else{
pixelFormat = GL_RGBA;
}
int width = FreeImage_GetWidth(bitmap);
int height = FreeImage_GetHeight(bitmap);
glGenTextures(1, &_id);
bind( );
// glTexEnv is deprecated in Core profile
//glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, _minFilter);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, _magFilter);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, pixelFormat, GL_UNSIGNED_BYTE, (void*)FreeImage_GetBits(bitmap));
// Sanity check
/*
fprintf(stderr, "Width: %d, Height: %d\n", width, height);
if( pixelFormat == GL_RGB ){
fprintf(stderr, "Pixel format is RGB\n");
}else{
fprintf(stderr, "Pixel format is RGBA\n");
}
GLubyte *p = (GLubyte*)FreeImage_GetBits(bitmap);
for(int i = 0; i < (width * height); i++){
printf("%d ", p[i]);
}
*/
}
~Texture2D( ){
glDeleteTextures(1, &_id);
}
void bind( ){
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, _minFilter);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, _magFilter);
glBindTexture(GL_TEXTURE_2D, _id);
}
GLuint id( ){
return _id;
}
GLint minFilter( ){
return _minFilter;
}
GLint magFilter( ){
return _magFilter;
}
};
#endif