-
Notifications
You must be signed in to change notification settings - Fork 3
/
hello.c
281 lines (219 loc) · 6.68 KB
/
hello.c
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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
#include "hello.h"
GLuint vertex_shader, fragment_shader, program;
CvVideoWriter *writer = 0;
int counter = 0;
int image_width = 512;
int image_height = 512;
static struct {
struct {
GLint position;
} attributes;
GLuint vertex_buffer;
GLuint textures[2];
struct {
GLint textures[2];
} uniforms;
} g_resources;
static GLuint makeBuffer(
GLenum target,
const void *buffer_data,
GLsizei buffer_size
) {
GLuint buffer;
glGenBuffers(1, &buffer);
glBindBuffer(target, buffer);
glBufferData(target, buffer_size, buffer_data, GL_STATIC_DRAW);
return buffer;
}
static GLuint makeTexture(const char *filename)
{
int width, height;
unsigned char *pixels = read_jpeg(filename, &width, &height);
GLuint texture;
if (!pixels)
return 0;
glGenTextures(1, &texture);
glBindTexture(GL_TEXTURE_2D, texture);
glTexImage2D(
GL_TEXTURE_2D, 0, /* target, level */
GL_RGB8, /* internal format */
width, height, 0, /* width, height, border */
GL_BGR, GL_UNSIGNED_BYTE, /* external format, type */
pixels /* pixels */
);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
free(pixels);
return texture;
}
static GLuint makeShader(GLenum type, const char *filename)
{
GLint length;
GLchar *source = (GLchar *)file_contents(filename, &length);
GLuint shader;
GLint shader_ok;
if (!source)
return 0;
shader = glCreateShader(type);
glShaderSource(shader, 1, (const GLchar**)&source, &length);
free(source);
glCompileShader(shader);
glGetShaderiv(shader, GL_COMPILE_STATUS, &shader_ok);
if (!shader_ok) {
fprintf(stderr, "Failed to compile %s:\n", filename);
glDeleteShader(shader);
return 0;
}
return shader;
}
static GLuint makeProgram(GLuint vertex_shader, GLuint fragment_shader)
{
GLint program_ok;
GLuint program = glCreateProgram();
glAttachShader(program, vertex_shader);
glAttachShader(program, fragment_shader);
glLinkProgram(program);
glGetProgramiv(program, GL_LINK_STATUS, &program_ok);
if (!program_ok) {
fprintf(stderr, "Failed to link shader program:\n");
glDeleteProgram(program);
return 0;
}
return program;
}
static void setupScreen(int w, int h){
glClearColor(1.0f, 1.0f, 1.0f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT);
glViewport(0, 0, w, h);
}
static int initShaders(void){
vertex_shader = makeShader( GL_VERTEX_SHADER, "noop.v.glsl");
if (vertex_shader == 0)
return 0;
fragment_shader = makeShader( GL_FRAGMENT_SHADER, "noop.f.glsl");
if (fragment_shader == 0)
return 0;
program = makeProgram(vertex_shader, fragment_shader);
if (program == 0)
return 0;
glUseProgram(program);
}
static void loadTexture(void){
g_resources.textures[0] = makeTexture("images/01-qwiki-reference.jpg");
g_resources.uniforms.textures[0] = glGetUniformLocation(program, "textures[0]");
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, g_resources.textures[0]);
glUniform1f(g_resources.uniforms.textures[0], 0);
}
static void setupTexture(void){
// makeTexture already calls texImage2D
}
static void setupBuffers(void){
GLfloat g_vertex_buffer_data[] = {
-0.5f, -0.5f, 0.0f,
0.5, -0.5, 0.0f,
0.5f, 0.5f, 0.0f,
0.5f, 0.5f, 0.0f,
-0.5f, 0.5f, 0.0f,
-0.5f, -0.5f, 0.0f
};
g_resources.vertex_buffer = makeBuffer(
GL_ARRAY_BUFFER,
g_vertex_buffer_data,
sizeof(g_vertex_buffer_data)
);
}
static float angle = 0.0;
static void setupDynamicBuffers(){
GLfloat x_translation = sin(angle)/2.0f;
GLfloat triangleVertices[] = {
//left triangle
-0.5 + x_translation, 0.5, 0.0,
0.0 + x_translation, 0.5, 0.0,
-0.5 + x_translation, -0.5, 0.0,
//right triangle
0.0 + x_translation, 0.5, 0.0,
0.0 + x_translation, -0.5, 0.0,
-0.5 + x_translation, -0.5, 0.0
};
angle += 0.01;
g_resources.vertex_buffer = makeBuffer(
GL_ARRAY_BUFFER,
triangleVertices,
sizeof(triangleVertices)
);
}
static void drawScene(void)
{
int width = image_width;
int height = image_height;
setupScreen(width, height);
int numOfVertices = 6;
g_resources.attributes.position
= glGetAttribLocation(program, "position");
glEnableVertexAttribArray(g_resources.attributes.position);
glBindBuffer(GL_ARRAY_BUFFER, g_resources.vertex_buffer);
glVertexAttribPointer(
g_resources.attributes.position, /* attribute */
3, /* size */
GL_FLOAT, /* type */
GL_FALSE, /* normalized? */
0,//sizeof(GLfloat)*3, /* stride */
(void*)0 /* array buffer offset */
);
glDrawArrays(GL_TRIANGLES, 0, numOfVertices);
glutSwapBuffers();
}
static void screenCapture(int width, int height){
unsigned char *raw_image = (unsigned char*) calloc(width * height * 3, sizeof(char));
glReadPixels(0, 0, width, height, GL_RGB, GL_UNSIGNED_BYTE, raw_image);
IplImage* img = cvCreateImage(cvSize(width, height), IPL_DEPTH_8U, 3);
img->imageData = (char *)raw_image;
cvWriteFrame(writer, img); // add the frame to the file
cvReleaseImage(&img);
}
static void replay(){
int width = image_width;
int height = image_height;
setupDynamicBuffers();
drawScene();
screenCapture(width, height);
}
static int setupBoard(int width, int height, int argc, char** argv){
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE);
glutInitWindowSize(width, height);
glutCreateWindow("Hello World");
glewInit();
if (!GLEW_VERSION_2_0) {
fprintf(stderr, "OpenGL 2.0 not available\n");
return -1;
}
return 1;
}
static int setupVideoWriting(){
int isColor = 1;
int fps = 30;
int width = image_width;
int height = image_height;
// You can change CV_FOURCC('' to CV_FOURCC('X', '2', '6', '4') for x264 output
// Make sure you compiled ffmpeg with x264 support and OpenCV with x264
writer = cvCreateVideoWriter("out.avi", CV_FOURCC('j', 'p', 'e', 'g'), fps, cvSize(width, height), isColor);
return 0;
}
int main(int argc, char** argv){
setupVideoWriting();
int width = image_width;
int height = image_height;
setupBoard(width, height, argc, argv);
setupScreen(width, height);
initShaders();
loadTexture();
setupTexture();
setupBuffers();
glutDisplayFunc(&drawScene);
glutIdleFunc(&replay);
glutMainLoop();
cvReleaseVideoWriter(&writer);
return 0;
}