52#define MOD_NAME "[Opengl utils] "
54static const float PI_F=3.14159265358979f;
57 1.0f, 1.0f, 1.0f, 1.0f,
58 -1.0f, 1.0f, 0.0f, 1.0f,
59 -1.0f, -1.0f, 0.0f, 0.0f,
61 1.0f, 1.0f, 1.0f, 1.0f,
62 -1.0f, -1.0f, 0.0f, 0.0f,
63 1.0f, -1.0f, 1.0f, 0.0f
67 255, 0, 0, 0, 255, 0, 0, 0, 255, 255, 255, 255,
68 255, 0, 0, 0, 255, 0, 0, 0, 255, 255, 255, 255,
69 255, 0, 0, 0, 255, 0, 0, 0, 255, 255, 255, 255,
70 255, 0, 0, 0, 255, 0, 0, 0, 255, 255, 255, 255
74 glCompileShader(shaderId);
79 glGetShaderiv(shaderId, GL_COMPILE_STATUS, &ret);
80 glGetShaderiv(shaderId, GL_INFO_LOG_LENGTH, &
len);
82 std::vector<char> errorMsg(
len+1);
83 glGetShaderInfoLog(shaderId,
len, NULL, &errorMsg[0]);
84 printf(
"%s\n", errorMsg.data());
93 glGetIntegerv(GL_NUM_EXTENSIONS, &
count);
95 for(
int i = 0; i <
count; i++){
96 const char *
item = (
const char *) glGetStringi(GL_EXTENSIONS, i);
97 if(strcmp(
item, ext) == 0)
105 GLuint vertexShader = glCreateShader(GL_VERTEX_SHADER);
106 GLuint fragShader = glCreateShader(GL_FRAGMENT_SHADER);
108 glShaderSource(vertexShader, 1, &
vert_src, NULL);
110 glShaderSource(fragShader, 1, &
frag_src, NULL);
114 glAttachShader(
program, vertexShader);
115 glAttachShader(
program, fragShader);
119 glDetachShader(
program, vertexShader);
120 glDetachShader(
program, fragShader);
121 glDeleteShader(vertexShader);
122 glDeleteShader(fragShader);
137 glDeleteBuffers(1, &
vbo);
139 glDeleteVertexArrays(1, &
vao);
145 glBindVertexArray(
vao);
147 glDrawElements(GL_TRIANGLES,
indices_num, GL_UNSIGNED_INT, (
void *) 0);
152 glBindVertexArray(0);
157 glGenVertexArrays(1, &model.
vao);
158 glBindVertexArray(model.
vao);
163 glGenBuffers(1, &model.
vbo);
164 glBindBuffer(GL_ARRAY_BUFFER, model.
vbo);
165 glBufferData(GL_ARRAY_BUFFER, vertices.size() *
sizeof(
float), vertices.data(), GL_STATIC_DRAW);
168 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, model.
elem_buf);
169 glBufferData(GL_ELEMENT_ARRAY_BUFFER, indices.size() *
sizeof(
unsigned), indices.data(), GL_STATIC_DRAW);
171 glEnableVertexAttribArray(0);
172 glBindBuffer(GL_ARRAY_BUFFER, model.
vbo);
173 glVertexAttribPointer(
181 glEnableVertexAttribArray(1);
182 glBindBuffer(GL_ARRAY_BUFFER, model.
vbo);
183 glVertexAttribPointer(
189 (
void*)(3 *
sizeof(
float))
192 glBindVertexArray(0);
200 glGenVertexArrays(1, &model.
vao);
201 glBindVertexArray(model.
vao);
203 glGenBuffers(1, &model.
vbo);
204 glBindBuffer(GL_ARRAY_BUFFER, model.
vbo);
207 glEnableVertexAttribArray(0);
208 glBindBuffer(GL_ARRAY_BUFFER, model.
vbo);
209 glVertexAttribPointer(
217 glEnableVertexAttribArray(1);
218 glBindBuffer(GL_ARRAY_BUFFER, model.
vbo);
219 glVertexAttribPointer(
225 (
void*)(2 *
sizeof(
float))
228 glBindVertexArray(0);
238 glDeleteTextures(1, &
tex_id);
239 glDeleteBuffers(1, &
pbo);
249 glBindTexture(GL_TEXTURE_2D,
tex_id);
256 GL_RGBA, GL_UNSIGNED_BYTE,
262 GLint internal_format,
270 glBindTexture(GL_TEXTURE_2D,
tex_id);
275 glBindBuffer(GL_PIXEL_UNPACK_BUFFER,
pbo->get());
276 glUnmapBuffer(GL_PIXEL_UNPACK_BUFFER);
287 glBindBuffer(GL_PIXEL_UNPACK_BUFFER, 0);
295 glGenTextures(1, &
tex_id);
296 glBindTexture(GL_TEXTURE_2D,
tex_id);
297 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, 4, 4, 0, GL_RGB, GL_UNSIGNED_BYTE,
pixels);
298 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
299 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
300 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
301 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
302 glGenBuffers(1, &
pbo);
307 glBindTexture(GL_TEXTURE_2D, tex);
308 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
309 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
310 glBindTexture(GL_TEXTURE_2D, 0);
313 glBindFramebuffer(GL_FRAMEBUFFER,
fbo);
314 glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, tex, 0);
315 if (glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE){
316 printf(
"Error %d\n", glGetError());
318 glBindFramebuffer(GL_FRAMEBUFFER, 0);
324 glBindTexture(GL_TEXTURE_2D,
tex->
get());
325 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
326 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
327 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
328 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
335 assert(
conv &&
"Frame uploader for codec not A;");
337 conv->put_frame(f, pbo_frame);
341 std::vector<codec_t> ret;
347 ret.insert(ret.end(), cvt.begin(), cvt.end());
355layout(location = 0) in vec2 vert_pos;
356layout(location = 1) in vec2 vert_uv;
358uniform vec2 scale_vec;
363 gl_Position = vec4(vert_pos * scale_vec, 0.0f, 1.0f);
372uniform sampler2D tex;
373uniform bool deinterlace = false;
375 color = texture(tex, UV).rgb;
378 float lineOff = 1.0f / textureSize(tex, 0).y;
379 vec3 pix_down = texture(tex, vec2(UV.x, UV.y + lineOff)).rgb;
380 color = (color + pix_down) / 2.0f;
417 double screen_aspect = (double) width / height;
419 if(screen_aspect > video_aspect) {
420 x = (double) height * video_aspect / width;
422 y = (double) -width / (height * video_aspect);
428 pvLoc = glGetUniformLocation(
program.
get(),
"scale_vec");
429 glUniform2f(pvLoc, x, y);
436 pvLoc = glGetUniformLocation(
program.
get(),
"deinterlace");
437 glUniform1i(pvLoc, enable);
441 std::vector<float> verts;
443 float lat_step =
PI_F / latitude_n;
444 float long_step = 2 *
PI_F / longtitude_n;
446 for(
int i = 0; i < latitude_n + 1; i++){
447 float y = std::cos(i * lat_step) * r;
448 float y_slice_r = std::sin(i * lat_step) * r;
451 for(
int j = 0; j < longtitude_n + 1; j++){
452 float x = std::sin(j * long_step) * y_slice_r;
453 float z = std::cos(j * long_step) * y_slice_r;
458 float u = 1 -
static_cast<float>(j) / longtitude_n;
459 float v =
static_cast<float>(i) / latitude_n;
471 std::vector<unsigned int> indices;
473 for(
int i = 0; i < latitude_n; i++){
474 int slice_idx = i * (latitude_n + 1);
475 int next_slice_idx = (i + 1) * (latitude_n + 1);
477 for(
int j = 0; j < longtitude_n; j++){
480 if(i != latitude_n - 1){
481 indices.push_back(slice_idx + j + 1);
482 indices.push_back(next_slice_idx + j);
483 indices.push_back(next_slice_idx + j + 1);
487 indices.push_back(slice_idx + j + 1);
488 indices.push_back(slice_idx + j);
489 indices.push_back(next_slice_idx + j);
struct video_desc video_desc_from_frame(const struct video_frame *frame)
Returns struct video_desc from video frame.
void resize(int width, int height)
void enableDeinterlacing(bool enable)
void put_frame(video_frame *f)
std::unique_ptr< GlBuffer > upload_pbo
std::vector< codec_t > get_supported_codecs()
void enable_pbo(bool enable)
void put_frame(video_frame *f, bool pbo_frame=false)
std::unique_ptr< Frame_convertor > conv
void attach_dst_texture(Texture *tex)
static Model get_sphere()
void load_frame(int w, int h, GLint internal_format, GLenum src_format, GLenum type, video_frame *f, bool pbo_frame)
void allocate(int w, int h, GLint internal_fmt)
std::vector< codec_t > get_convertor_supported_codecs()
std::unique_ptr< Frame_convertor > get_convertor_for_codec(codec_t codec)
bool check_gl_extension_present(const char *ext)
static const GLfloat rectangle[]
static void compileShader(GLuint shaderId)
static std::vector< unsigned > gen_sphere_indices(int latitude_n, int longtitude_n)
static unsigned char pixels[]
static std::vector< float > gen_sphere_vertices(int r, int latitude_n, int longtitude_n)
void attach_texture(GLuint tex)
char * data
Raw tile data. Pointer must be at least 4B aligned.
unsigned int width
width of every tile (!)
unsigned int height
height of every tile (!)
Struct video_frame represents a video frame and contains video description.
struct tile tiles[FLEXIBLE_ARRAY_MEMBER]
struct video_frame_callbacks callbacks
@ RGB
RGB 8-bit (packed into 24-bit word)
@ RGBA
RGBA 8-bit, big-endian.
bool video_desc_eq(struct video_desc a, struct video_desc b)
Compares two video descriptions.