-
Notifications
You must be signed in to change notification settings - Fork 6
/
utils.cpp
77 lines (57 loc) · 1.72 KB
/
utils.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
#include "utils.h"
struct timeval tp; // used to get current time
GLuint createVS(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 createFS(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;
}
long int getCurrentTime()
{
gettimeofday(&tp, NULL);
return (long int)(tp.tv_sec * 10e6 + tp.tv_usec);
}