forked from wysaid/CameraCapture
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path4-example_with_glfw.cpp
More file actions
214 lines (174 loc) · 6.63 KB
/
4-example_with_glfw.cpp
File metadata and controls
214 lines (174 loc) · 6.63 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
/**
* @file example_with_glfw.cpp
* @author wysaid (this@wysaid.org)
* @brief GLFW Example with ccap.
* @date 2025-05
*
*/
#include "utils/helper.h"
#include <ccap.h>
#include <chrono>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <string>
#ifdef __GNUC__
#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
#endif
#ifndef M_PI
#define M_PI 3.14159265358979323846
#endif
#define GLAD_GL_IMPLEMENTATION
#include <glad/gl.h>
#define GLFW_INCLUDE_NONE
#include <GLFW/glfw3.h>
constexpr const char* vertexShaderSource = R"(
#version 330 core
layout(location = 0) in vec2 pos;
out vec2 texCoord;
void main() {
gl_Position = vec4(pos, 0.0, 1.0);
texCoord = (pos / 2.0) + 0.5;
}
)";
constexpr const char* fragmentShaderSource = R"(
#version 330 core
in vec2 texCoord;
out vec4 fragColor;
uniform sampler2D tex;
uniform float progress;
const float angle = 10.0;
void main() {
/// Apply a wavy distortion to the texture coordinates based on the progress
vec2 newCoord;
newCoord.x = texCoord.x + 0.01 * sin(progress + texCoord.x * angle);
newCoord.y = texCoord.y + 0.01 * sin(progress + texCoord.y * angle);
/// Avoid sampling too close to the edges
float edge1 = min(texCoord.x, texCoord.y);
float edge2 = max(texCoord.x, texCoord.y);
if (edge1 < 0.05 || edge2 > 0.95)
{
float lengthToEdge = min(edge1, 1.0 - edge2) / 0.05;
newCoord = mix(texCoord, newCoord, vec2(lengthToEdge));
}
fragColor = texture(tex, newCoord);
}
)";
// selectCamera moved to utils/helper.{h,cpp}
int main(int argc, char** argv) {
/// Enable verbose log to see debug information
ccap::setLogLevel(ccap::LogLevel::Verbose);
ccap::Provider cameraProvider;
if (auto deviceNames = cameraProvider.findDeviceNames(); !deviceNames.empty()) {
for (const auto& name : deviceNames) {
std::cout << "## Found video capture device: " << name << std::endl;
}
}
int requestedWidth = 1920;
int requestedHeight = 1080;
double requestedFps = 60;
constexpr ccap::PixelFormat cameraOutputPixelFormat = ccap::PixelFormat::RGBA32;
constexpr GLenum pixelFormatGl = GL_RGBA;
cameraProvider.set(ccap::PropertyName::Width, requestedWidth);
cameraProvider.set(ccap::PropertyName::Height, requestedHeight);
cameraProvider.set(ccap::PropertyName::PixelFormatOutput, cameraOutputPixelFormat);
// cameraProvider.set(ccap::PropertyName::PixelFormatInternal, ccap::PixelFormat::NV12);
cameraProvider.set(ccap::PropertyName::FrameRate, requestedFps);
cameraProvider.set(ccap::PropertyName::FrameOrientation, ccap::FrameOrientation::BottomToTop);
int deviceIndex;
if (argc > 1 && std::isdigit(argv[1][0])) {
deviceIndex = std::stoi(argv[1]);
} else {
deviceIndex = selectCamera(cameraProvider);
}
cameraProvider.open(deviceIndex, true);
if (!cameraProvider.isStarted()) {
std::cerr << "Failed to start camera!" << std::endl;
return -1;
}
int frameWidth{}, frameHeight{};
/// 5s timeout for grab
if (auto frame = cameraProvider.grab(5000)) {
frameWidth = frame->width;
frameHeight = frame->height;
std::cout << "## VideoFrame resolution: " << frameWidth << "x" << frameHeight << std::endl;
} else {
std::cerr << "Failed to grab a frame!" << std::endl;
return -1;
}
glfwInit();
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
GLFWwindow* window = glfwCreateWindow(frameWidth, frameHeight, "ccap gui example", nullptr, nullptr);
if (!window) {
glfwTerminate();
return -1;
}
glfwMakeContextCurrent(window);
gladLoadGL(glfwGetProcAddress);
GLuint vs = glCreateShader(GL_VERTEX_SHADER);
glShaderSource(vs, 1, &vertexShaderSource, nullptr);
glCompileShader(vs);
GLuint fs = glCreateShader(GL_FRAGMENT_SHADER);
glShaderSource(fs, 1, &fragmentShaderSource, nullptr);
glCompileShader(fs);
GLuint prog = glCreateProgram();
glBindAttribLocation(prog, 0, "pos");
glAttachShader(prog, vs);
glAttachShader(prog, fs);
glLinkProgram(prog);
glDeleteShader(vs);
glDeleteShader(fs);
GLint progressUniformLocation = glGetUniformLocation(prog, "progress");
GLint texUniformLocation = glGetUniformLocation(prog, "tex");
glUseProgram(prog);
glUniform1i(texUniformLocation, 0);
GLuint vao, vbo;
glGenVertexArrays(1, &vao);
glGenBuffers(1, &vbo);
glBindVertexArray(vao);
glBindBuffer(GL_ARRAY_BUFFER, vbo);
const float vertData[8] = { -1.0f, -1.0f, 1.0f, -1.0f, -1.0f, 1.0f, 1.0f, 1.0f };
glBufferData(GL_ARRAY_BUFFER, sizeof(vertData), vertData, GL_STATIC_DRAW);
glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 0, nullptr);
glEnableVertexAttribArray(0);
GLuint texture;
glGenTextures(1, &texture);
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, texture);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
for (; !glfwWindowShouldClose(window);) {
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, texture);
if (auto frame = cameraProvider.grab(
30)) { // buffer orphaning: <https://www.khronos.org/opengl/wiki/Buffer_Object_Streaming>, pass nullptr first.
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, frameWidth, frameHeight, 0, pixelFormatGl, GL_UNSIGNED_BYTE, nullptr);
glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, frameWidth, frameHeight, pixelFormatGl, GL_UNSIGNED_BYTE, frame->data[0]);
}
int windowWidth, windowHeight;
glfwGetFramebufferSize(window, &windowWidth, &windowHeight);
glViewport(0, 0, windowWidth, windowHeight);
glClear(GL_COLOR_BUFFER_BIT);
glUseProgram(prog);
float progress = fmod(glfwGetTime(), M_PI * 2.0) * 3.0;
glUniform1f(progressUniformLocation, progress);
glBindVertexArray(vao);
glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
glfwSwapBuffers(window);
glfwPollEvents();
}
// Cleanup, this can be omitted if the program is exiting
glfwDestroyWindow(window);
glDeleteVertexArrays(1, &vao);
glDeleteBuffers(1, &vbo);
glDeleteProgram(prog);
glDeleteTextures(1, &texture);
cameraProvider.close();
glfwTerminate();
return 0;
}