-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathProject3s16-texture.cpp
92 lines (77 loc) · 2.7 KB
/
Project3s16-texture.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
#define _CRT_SECURE_NO_DEPRECATE
#include <stdlib.h>
#include <stdio.h>
#include <math.h> // include math functions, such as sin, cos, M_PI
#include <iostream> // allow c++ style console printouts
using namespace std; // allow console printouts without std::
/** 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* loadPPM(const char* filename, int& width, int& height)
{
const int BUFSIZE = 128;
FILE* fp;
unsigned int read;
unsigned char* rawData;
char buf[3][BUFSIZE];
char* retval_fgets;
size_t retval_sscanf;
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] == '#');
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 (read != 1)
{
std::cerr << "error parsing ppm file, incomplete data" << std::endl;
delete[] rawData;
width = 0;
height = 0;
return NULL;
}
return rawData;
}
// load image file into texture object
void loadTexture()
{
GLuint texture[1]; // storage for one texture
int twidth, theight; // texture width/height [pixels]
unsigned char* tdata; // texture pixel data
// Load image file
tdata = loadPPM("image.ppm", twidth, theight);
if (tdata==NULL) return;
// Create ID for texture
glGenTextures(1, &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);
// Set bi-linear filtering for both minification and magnification
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
}