-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhelper.cpp
153 lines (116 loc) · 4.26 KB
/
helper.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
#include "helper.h"
void initShaders() {
idProgramShader = glCreateProgram();
idVertexShader = initVertexShader("shader.vert");
idFragmentShader = initFragmentShader("shader.frag");
glAttachShader(idProgramShader, idVertexShader);
glAttachShader(idProgramShader, idFragmentShader);
glLinkProgram(idProgramShader);
}
GLuint initVertexShader(const string& filename)
{
string shaderSource;
if (!readDataFromFile(filename, shaderSource)){
cout << "Cannot find file name: " + filename << endl;
exit(-1);
}
GLint length = shaderSource.length();
const GLchar* shader = (const GLchar*) shaderSource.c_str();
GLuint vs = glCreateShader(GL_VERTEX_SHADER);
glShaderSource(vs, 1, &shader, &length);
glCompileShader(vs);
char output[1024] = {0};
glGetShaderInfoLog(vs, 1024, &length, output);
printf("VS compile log: %s\n", output);
return vs;
}
GLuint initFragmentShader(const string& filename)
{
string shaderSource;
if (!readDataFromFile(filename, shaderSource)) {
cout << "Cannot find file name: " + filename << endl;
exit(-1);
}
GLint length = shaderSource.length();
const GLchar* shader = (const GLchar*) shaderSource.c_str();
GLuint fs = glCreateShader(GL_FRAGMENT_SHADER);
glShaderSource(fs, 1, &shader, &length);
glCompileShader(fs);
char output[1024] = {0};
glGetShaderInfoLog(fs, 1024, &length, output);
printf("FS compile log: %s\n", output);
return fs;
}
bool readDataFromFile(const string& fileName, string &data) {
fstream myfile;
myfile.open(fileName.c_str(), std::ios::in);
if (myfile.is_open()) {
string curLine;
while (getline(myfile, curLine)){
data += curLine;
if (!myfile.eof())
data += "\n";
}
myfile.close();
}
else
return false;
return true;
}
void initTexture(char *filename,int *w, int *h)
{
int width, height;
unsigned char *raw_image = NULL;
int bytes_per_pixel = 3; /* or 1 for GRACYSCALE images */
int color_space = JCS_RGB; /* or JCS_GRAYSCALE for grayscale images */
/* these are standard libjpeg structures for reading(decompression) */
struct jpeg_decompress_struct cinfo;
struct jpeg_error_mgr jerr;
/* libjpeg data structure for storing one row, that is, scanline of an image */
JSAMPROW row_pointer[1];
FILE *infile = fopen( filename, "rb" );
unsigned long location = 0;
int i = 0, j = 0;
if ( !infile )
{
printf("Error opening jpeg file %s\n!", filename );
return;
}
printf("Texture filename = %s\n",filename);
/* here we set up the standard libjpeg error handler */
cinfo.err = jpeg_std_error( &jerr );
/* setup decompression process and source, then read JPEG header */
jpeg_create_decompress( &cinfo );
/* this makes the library read from infile */
jpeg_stdio_src( &cinfo, infile );
/* reading the image header which contains image information */
jpeg_read_header( &cinfo, TRUE );
/* Start decompression jpeg here */
jpeg_start_decompress( &cinfo );
/* allocate memory to hold the uncompressed image */
raw_image = (unsigned char*)malloc( cinfo.output_width*cinfo.output_height*cinfo.num_components );
/* now actually read the jpeg into the raw buffer */
row_pointer[0] = (unsigned char *)malloc( cinfo.output_width*cinfo.num_components );
/* read one scan line at a time */
while( cinfo.output_scanline < cinfo.image_height )
{
jpeg_read_scanlines( &cinfo, row_pointer, 1 );
for( i=0; i<cinfo.image_width*cinfo.num_components;i++)
raw_image[location++] = row_pointer[0][i];
}
height = cinfo.image_height;
width = cinfo.image_width;
glGenTextures(1,&idJpegTexture);
glBindTexture(GL_TEXTURE_2D, idJpegTexture);
glActiveTexture(GL_TEXTURE0);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, raw_image);
*w = width;
*h = height;
glGenerateMipmap(GL_TEXTURE_2D);
/* wrap up decompression, destroy objects, free pointers and close open files */
jpeg_finish_decompress( &cinfo );
jpeg_destroy_decompress( &cinfo );
free( row_pointer[0] );
free( raw_image );
fclose( infile );
}