UltraGrid
UltraGrid Low-Latency High-Resolution Transmission System
Loading...
Searching...
No Matches
opengl_panorama.cpp
Go to the documentation of this file.
1
5/*
6 * Copyright (c) 2023 CESNET, z. s. p. o.
7 * All rights reserved.
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, is permitted provided that the following conditions
11 * are met:
12 *
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 *
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 *
20 * 3. Neither the name of CESNET nor the names of its contributors may be
21 * used to endorse or promote products derived from this software without
22 * specific prior written permission.
23 *
24 * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS
25 * "AS IS" AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING,
26 * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
27 * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
28 * EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
29 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
30 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
31 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
32 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
33 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
34 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
35 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36 */
37
38#include <glm/glm.hpp>
39#include <glm/gtc/matrix_transform.hpp>
40#include <glm/gtc/type_ptr.hpp>
41#include <glm/gtc/quaternion.hpp>
42#include "opengl_utils.hpp"
44#include "video_frame.h"
45#include "debug.h"
46
47#include "opengl_panorama.hpp"
49
50static const char *persp_vert_src = R"END(
51#version 330 core
52layout(location = 0) in vec3 vert_pos;
53layout(location = 1) in vec2 vert_uv;
54
55out vec2 UV;
56
57uniform mat4 pv_mat;
58
59void main(){
60 gl_Position = pv_mat * vec4(vert_pos, 1.0f);
61 UV = vert_uv;
62}
63)END";
64
65static const char *persp_frag_src = R"END(
66#version 330 core
67in vec2 UV;
68out vec3 color;
69uniform sampler2D tex;
70void main(){
71 color = texture(tex, UV).rgb;
72}
73)END";
74
75
78
79PanoramaScene::PanoramaScene(GlProgram program, Model model): program(std::move(program)),
80 model(std::move(model))
81{
83}
84
85void PanoramaScene::render(int width, int height){
86 float aspect_ratio = static_cast<float>(width) / height;
87 glm::mat4 projMat = glm::perspective(glm::radians(fov),
88 aspect_ratio,
89 0.1f,
90 300.0f);
91 glm::mat4 viewMat(1.f);
92 viewMat = glm::rotate(viewMat, glm::radians(rot_y), {1.f, 0, 0});
93 viewMat = glm::rotate(viewMat, glm::radians(rot_x), {0.f, 1, 0});
94 glm::mat4 pvMat = projMat * viewMat;
95
96 render(width, height, pvMat);
97}
98
99void PanoramaScene::render(int width, int height, const glm::mat4& pvMat){
101
102 glUseProgram(program.get());
103 glViewport(0, 0, width, height);
104 GLuint pvLoc;
105 pvLoc = glGetUniformLocation(program.get(), "pv_mat");
106 glUniformMatrix4fv(pvLoc, 1, GL_FALSE, glm::value_ptr(pvMat));
107
108 GLuint tex_id;
109 {//lock
110 std::lock_guard<std::mutex> lock(tex_mut);
112 tex.swap_front();
113 }
114 tex_id = tex.get_front().get();
115 }
116
117 glBindTexture(GL_TEXTURE_2D, tex_id);
118 model.render();
119}
120
121void PanoramaScene::put_frame(video_frame *f, bool pbo_frame){
123
124 Texture *back_texture;
125 {
126 std::lock_guard<std::mutex> lock(tex_mut);
127 back_texture = &tex.get_back();
128 }
129
130 uploader.attach_dst_texture(back_texture);
131 uploader.put_frame(f, pbo_frame);
132
133 glBindTexture(GL_TEXTURE_2D, back_texture->get());
134 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
135
136 glFinish();
137
138 {
139 std::lock_guard<std::mutex> lock(tex_mut);
140 tex.swap_back();
141 }
142}
143
144void PanoramaScene::rotate(float dx, float dy){
145 rot_x -= dx;
146 rot_y -= dy;
147
148 if(rot_y > 90) rot_y = 90;
149 if(rot_y < -90) rot_y = -90;
150}
151
void put_frame(video_frame *f, bool pbo_frame=false)
void attach_dst_texture(Texture *tex)
GLuint get()
void render()
GLuint get() const
bool is_new_front_available() const
static const char * persp_frag_src
static const char * persp_vert_src
#define PROFILE_FUNC
void rotate(float dx, float dy)
FrameUploader uploader
TripleBufferTexture tex
void render(int width, int height)
void put_frame(video_frame *f, bool pbo_frame=false)
Struct video_frame represents a video frame and contains video description.
Definition types.h:305