-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTexture.cpp
More file actions
134 lines (107 loc) · 3.36 KB
/
Texture.cpp
File metadata and controls
134 lines (107 loc) · 3.36 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
#include "Texture.h"
#ifdef __APPLE__
#include <GLUT/glut.h>
#else
#include <GL/glut.h>
#endif
Texture* Texture::emptyTexture = new Texture();
Texture::Texture()
{
id = 0;
}
Texture::Texture(const char* fname)
{
filename = fname;
GLuint texture[1]; // storage for one texture
int twidth = 1024;
int theight = 1024; // texture width/height [pixels]
unsigned char* tdata; // texture pixel data
//Load image file
tdata = loadPPM(filename, twidth, theight);
//If the image wasn't loaded, can't continue
if(tdata == NULL)
return;
//Create ID for texture
glGenTextures(1, &texture[0]);
id=texture[0];
//Set this texture to be the one we are working with
glBindTexture(GL_TEXTURE_2D, texture[0]);
//Generate the texture
glTexImage2D(GL_TEXTURE_2D, 0, 3, twidth, theight, 0, GL_RGB, GL_UNSIGNED_BYTE, tdata);
//Make sure no bytes are padded:
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
//Select GL_MODULATE to mix texture with quad color for shading:
glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
//Use bilinear interpolation:
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
//And unbind it!
glBindTexture(GL_TEXTURE_2D, 0);
}
Texture::~Texture()
{
//
}
void Texture::bind(void)
{
glBindTexture(GL_TEXTURE_2D, id);
}
void Texture::unbind(void)
{
glBindTexture(GL_TEXTURE_2D, 0);
}
/** Load a ppm file from disk.
@input filename The location of the PPM file. If the file is not found, an error message
will be printed and this function will return 0
@input width This will be modified to contain the width of the loaded image, or 0 if file not found
@input height This will be modified to contain the height of the loaded image, or 0 if file not found
@return Returns the RGB pixel data as interleaved unsigned chars (R0 G0 B0 R1 G1 B1 R2 G2 B2 .... etc) or 0 if an error ocured
**/
unsigned char* Texture::loadPPM(const char* filename, int& width, int& height)
{
const int BUFSIZE = 128;
FILE* fp;
size_t read;
unsigned char* rawData;
char buf[3][BUFSIZE];
char* retval_fgets;
size_t retval_sscanf;
//Open the texture file
if((fp=fopen(filename, "rb")) == NULL)
{
std::cerr << "error reading ppm file, could not locate " << filename << std::endl;
width = 0;
height = 0;
return NULL;
}
//Read magic number:
retval_fgets = fgets(buf[0], BUFSIZE, fp);
//Read width and height:
do
{
retval_fgets=fgets(buf[0], BUFSIZE, fp);
} while(buf[0][0] == '#');
//Set the width and height
retval_sscanf=sscanf(buf[0], "%s %s", buf[1], buf[2]);
width = atoi(buf[1]);
height = atoi(buf[2]);
//Read maxval:
do
{
retval_fgets=fgets(buf[0], BUFSIZE, fp);
} while(buf[0][0] == '#');
//Read image data:
rawData = new unsigned char[width * height * 3];
read = fread(rawData, width * height * 3, 1, fp);
fclose(fp);
//If the read was a failure, error
if(read != 1)
{
std::cerr << "error parsing ppm file, incomplete data" << std::endl;
delete[] rawData;
width = 0;
height = 0;
return NULL;
}
return rawData;
}