-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
386 lines (316 loc) · 10.9 KB
/
Copy pathmain.cpp
File metadata and controls
386 lines (316 loc) · 10.9 KB
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
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
// Example rendering loop using OpenGL and GLFW
// Captures video frames from a V4L2 device and applies hyperbolic transformations
// to create a kaleidoscopic effect using shaders.
// Generated with help of chat GPT
// It uses the c_mcp project to expose some
// functions on MCP
#include "video.h"
#include <GL/glew.h>
#include <GLFW/glfw3.h>
#include <iostream>
#include <fstream>
#include <string>
#include <signal.h>
#include <cstdlib>
#include <cstring>
#include "hyperbolic.h"
extern "C"
{
#include "cJSON.h"
#include "config.h"
#include "http.h"
#include "stdio_transport.h"
}
#include "tools.h"
std::string readShaderFile(const std::string &filename)
{
std::ifstream file(filename);
if (!file.is_open())
{
std::cerr << "Error opening shader file: " << filename << std::endl;
return "";
}
std::string content((std::istreambuf_iterator<char>(file)), std::istreambuf_iterator<char>());
file.close();
return content;
}
GLuint createShaderProgram(const char *vertexPath, const char *fragmentPath)
{
std::string vertexCode = readShaderFile(vertexPath);
std::string fragmentCode = readShaderFile(fragmentPath);
const char *vShaderCode = vertexCode.c_str();
const char *fShaderCode = fragmentCode.c_str();
GLuint vertex, fragment;
vertex = glCreateShader(GL_VERTEX_SHADER);
glShaderSource(vertex, 1, &vShaderCode, nullptr);
glCompileShader(vertex);
GLint success;
glGetShaderiv(vertex, GL_COMPILE_STATUS, &success);
if (!success)
{
char infoLog[512];
glGetShaderInfoLog(vertex, 512, nullptr, infoLog);
std::cerr << "Error compiling vertex shader: " << infoLog << std::endl;
return 0;
}
fragment = glCreateShader(GL_FRAGMENT_SHADER);
glShaderSource(fragment, 1, &fShaderCode, nullptr);
glCompileShader(fragment);
glGetShaderiv(fragment, GL_COMPILE_STATUS, &success);
if (!success)
{
char infoLog[512];
glGetShaderInfoLog(fragment, 512, nullptr, infoLog);
std::cerr << "Error compiling fragment shader: " << infoLog << std::endl;
return 0;
}
GLuint shaderProgram = glCreateProgram();
glAttachShader(shaderProgram, vertex);
glAttachShader(shaderProgram, fragment);
glLinkProgram(shaderProgram);
glGetProgramiv(shaderProgram, GL_LINK_STATUS, &success);
if (!success)
{
char infoLog[512];
glGetProgramInfoLog(shaderProgram, 512, nullptr, infoLog);
std::cerr << "Error linking shader program: " << infoLog << std::endl;
return 0;
}
glDeleteShader(vertex);
glDeleteShader(fragment);
return shaderProgram;
}
GLuint mk_texture(int width, int height)
{
GLuint tex = 0;
glGenTextures(1, &tex);
glBindTexture(GL_TEXTURE_2D, tex);
// For raw pixel buffers, nearest avoids filtering blur:
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
// Clamp (or use GL_REPEAT if you want wraparound)
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
// If your rows aren’t 4-byte aligned (common with 3-channel RGB),
// set unpack alignment to 1 to avoid row padding issues:
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
// Allocate storage once; no data yet. Replace width/height.
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB8, width, height, 0,
GL_RGB, GL_UNSIGNED_BYTE, nullptr);
glBindTexture(GL_TEXTURE_2D, 0);
return tex;
}
void gl_error_callback(int c, const char *d)
{
fprintf(stderr, "GLFW error %d: %s\n", c, d);
}
static volatile sig_atomic_t g_stop = 0;
void on_sigint(int sig)
{
(void)sig;
g_stop = 1;
}
void key_callback(GLFWwindow *window, int key, int scancode, int action, int mods)
{
if (action == GLFW_PRESS)
{
if (key == GLFW_KEY_ESCAPE)
{
glfwSetWindowShouldClose(window, GLFW_TRUE);
}
}
}
int width, height;
int main(int argc, char **argv)
{
signal(SIGINT, on_sigint);
const char *dev_name = (argc > 1) ? argv[1] : "/dev/video5";
int video_width = (argc > 2) ? atoi(argv[2]) : 640;
int video_height = (argc > 3) ? atoi(argv[3]) : 480;
define_tools();
int err = 0;
#if defined(MCP_STDIO)
err = init_stdio();
#else
err = init_http();
#endif
if (err != 0)
{
fprintf(stderr, "Failed to initialize MCP client\n");
return 1;
}
enum v4l2_buf_type type;
size_t rgb_size = (size_t)video_width * video_height * 3;
unsigned char *rgb = (unsigned char *)malloc(rgb_size);
if (!rgb)
{
fprintf(stderr, "malloc rgb failed\n");
return 1;
}
memset(rgb, 0, rgb_size);
err = init_video(dev_name, video_width, video_height, type);
if (err != 0)
return err;
glfwSetErrorCallback(gl_error_callback);
if (!glfwInit())
{
std::cerr << "Failed to initialize GLFW" << std::endl;
return -1;
}
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
GLFWmonitor *monitor = glfwGetPrimaryMonitor();
const GLFWvidmode *mode = glfwGetVideoMode(monitor);
GLFWwindow *window = glfwCreateWindow(mode->width, mode->height, "Fullscreen Example", monitor, NULL);
if (!window)
{
std::cerr << "Failed to create GLFW window" << std::endl;
glfwTerminate();
return -1;
}
glfwMakeContextCurrent(window);
glfwSwapInterval(1);
if (glewInit() != GLEW_OK)
{
std::cerr << "Failed to initialize GLEW" << std::endl;
glfwDestroyWindow(window);
glfwTerminate();
return -1;
}
// Set key callback
glfwSetKeyCallback(window, key_callback);
glfwGetFramebufferSize(window, &width, &height);
glViewport(0, 0, width, height);
glfwSetFramebufferSizeCallback(window, [](GLFWwindow *, int w, int h)
{ glViewport(0, 0, w, h);
width = w; height = h; });
GLuint shaderProgram = createShaderProgram("vertex_shader.glsl", "fragment_shader.glsl");
if (shaderProgram == 0)
{
glfwDestroyWindow(window);
glfwTerminate();
return -1;
}
glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_HIDDEN);
GLuint tex = mk_texture(video_width, video_height);
GLint uResolutionLoc = glGetUniformLocation(shaderProgram, "uResolution");
GLint uTimeLoc = glGetUniformLocation(shaderProgram, "uTime");
GLint uN1Loc = glGetUniformLocation(shaderProgram, "uN1");
GLint uN2Loc = glGetUniformLocation(shaderProgram, "uN2");
GLint uN3Loc = glGetUniformLocation(shaderProgram, "uN3");
GLint uAALoc = glGetUniformLocation(shaderProgram, "uAA");
GLint uTextureZoomLoc = glGetUniformLocation(shaderProgram, "uTextureZoom");
GLint uEdgeColorLoc = glGetUniformLocation(shaderProgram, "uEdgeColor");
GLint uBackgroundColor = glGetUniformLocation(shaderProgram, "uBackgroundColor");
GLint uGeometryType = glGetUniformLocation(shaderProgram, "uGeometryType");
GLint uAnimationOn = glGetUniformLocation(shaderProgram, "uAnimationOn");
vec3 n1, n2, n3;
float zoom = 1.0f;
float vertices[] = {
// positions
-1.0f, -1.0f, 0.0f,
1.0f, -1.0f, 0.0f,
1.0f, 1.0f, 0.0f,
-1.0f, 1.0f, 0.0f};
unsigned int indices[] = {
0, 1, 2,
2, 3, 0};
GLuint VBO, VAO, EBO;
glGenVertexArrays(1, &VAO);
glGenBuffers(1, &VBO);
glGenBuffers(1, &EBO);
glBindVertexArray(VAO);
glBindBuffer(GL_ARRAY_BUFFER, VBO);
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indices), indices, GL_STATIC_DRAW);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(float), (void *)0);
glEnableVertexAttribArray(0);
glBindVertexArray(0);
float c = 0.0f;
float dc = 0.1;
double last = glfwGetTime();
while (!glfwWindowShouldClose(window) && !g_stop)
{
double now = glfwGetTime();
double dt = now - last;
err = try_get_buffer(rgb, video_width, video_height);
if (err != 0)
{
glfwSetWindowShouldClose(window, GLFW_TRUE);
continue;
}
#if defined(MCP_STDIO)
process_stdio();
#else
process_http();
#endif
glClear(GL_COLOR_BUFFER_BIT);
// Update texture if the buffer changed (fast path: glTexSubImage2D)
glBindTexture(GL_TEXTURE_2D, tex);
glPixelStorei(GL_UNPACK_ALIGNMENT, 1); // keep safe
glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, video_width, video_height,
GL_RGB, GL_UNSIGNED_BYTE, rgb);
// Bind texture unit 0 and set the sampler uniform
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, tex);
glUseProgram(shaderProgram);
glUniform1i(glGetUniformLocation(shaderProgram, "uTex"), 0);
glUniform2f(uResolutionLoc, (float)width, (float)height);
GLint uVideoWidthLoc = glGetUniformLocation(shaderProgram, "uVideoWidth");
glUniform1f(uVideoWidthLoc, (float)video_width);
glUniform1f(uTimeLoc, (float)now);
switch (symmetry)
{
case 1:
compute_triangle(2, 4, 7, n1, n2, n3);
zoom = 0.5;
break;
case 2:
compute_triangle(4, 4, 4, n1, n2, n3);
zoom = 0.5;
break;
default:
compute_triangle(2, 4, 5, n1, n2, n3);
zoom = 1.0;
}
glUniform3f(uN1Loc, (float)n1.x, (float)n1.y, (float)n1.z);
glUniform3f(uN2Loc, (float)n2.x, (float)n2.y, (float)n2.z);
glUniform3f(uN3Loc, (float)n3.x, (float)n3.y, (float)n3.z);
glUniform1i(uAALoc, 4);
glUniform1f(uTextureZoomLoc, (float)zoom);
glUniform3f(uEdgeColorLoc, (float)edgeColor.x, (float)edgeColor.y, (float)edgeColor.z);
glUniform3f(uBackgroundColor, (float)backgroundColor.x, (float)backgroundColor.y, (float)backgroundColor.z);
glUniform1i(uGeometryType, geometryType);
glUniform1i(uAnimationOn, animationOn);
/*
if (dt > 0.1) // update color every 0.1 seconds
{
c += dc;
if (c > 1.0f || c < 0.0f)
dc = -dc; // reverse direction
last = now;
}
*/
glBindVertexArray(VAO);
glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0);
glBindVertexArray(0);
glfwSwapBuffers(window);
glfwPollEvents();
}
stop_video(type);
#if defined(MCP_STDIO)
end_stdio();
#else
end_http();
#endif
glDeleteVertexArrays(1, &VAO);
glDeleteBuffers(1, &VBO);
glDeleteBuffers(1, &EBO);
glDeleteProgram(shaderProgram);
glfwDestroyWindow(window);
glfwTerminate();
free(rgb);
return 0;
}