Newer
Older
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
#ifdef __APPLE__
#define GL_SILENCE_DEPRECATION
# include <OpenGL/gl.h>
# include <OpenGL/glu.h>
# include <GLUT/glut.h>
#else
# include <GL/gl.h>
# include <GL/glu.h>
# include <GL/freeglut.h>
#endif
#include <iostream>
#include "texture2D.h"
Texture2D::Texture2D(){
this->img = LoadPPM("textures/lava_texture.ppm", &this->width, &this->height, &this->max);
}
Texture2D::Texture2D(char* file){
this->img = LoadPPM(file, &this->width, &this->height, &this->max);
}
void Texture2D::setTexture(){
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, this->width, this->height, 0, GL_RGB, GL_UNSIGNED_BYTE, this->img);
}
// Code from lecture slides
/* LoadPPM -- loads the specified ppm file, and returns the image data as a GLubyte
* (unsigned byte) array. Also returns the width and height of the image, and the
* maximum colour value by way of arguments
* usage: GLubyte myImg = LoadPPM("myImg.ppm", &width, &height, &max);
*/
GLubyte* LoadPPM(char* file, int* width, int* height, int* max)
{
GLubyte* img;
FILE *fd;
int n, m;
int k, nm;
char c;
int i;
char b[100];
float s;
int red, green, blue;
fd = fopen(file, "r");
fscanf(fd,"%[^\n] ",b);
if(b[0]!='P'|| b[1] != '3')
{
printf("%s is not a PPM file!\n",file);
exit(0);
}
printf("%s is a PPM file\n", file);
fscanf(fd, "%c",&c);
while(c == '#')
{
fscanf(fd, "%[^\n] ", b);
printf("%s\n",b);
fscanf(fd, "%c",&c);
}
ungetc(c,fd);
fscanf(fd, "%d %d %d", &n, &m, &k);
printf("%d rows %d columns max value= %d\n",n,m,k);
nm = n*m;
img = (GLubyte*)(malloc(3*sizeof(GLuint)*nm));
s=255.0/k;
for(i=0;i<nm;i++)
{
fscanf(fd,"%d %d %d",&red, &green, &blue );
img[3*nm-3*i-3]=red*s;
img[3*nm-3*i-2]=green*s;
img[3*nm-3*i-1]=blue*s;
}
*width = n;
*height = m;
*max = k;
return img;
}