UltraGrid
UltraGrid Low-Latency High-Resolution Transmission System
Loading...
Searching...
No Matches
src
video_display
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
"
43
#include "
opengl_conversions.hpp
"
44
#include "
video_frame.h
"
45
#include "
debug.h
"
46
47
#include "
opengl_panorama.hpp
"
48
#include "
utils/profile_timer.hpp
"
49
50
static
const
char
*
persp_vert_src
= R
"END(
51
#version 330 core
52
layout(location = 0) in vec3 vert_pos;
53
layout(location = 1) in vec2 vert_uv;
54
55
out vec2 UV;
56
57
uniform mat4 pv_mat;
58
59
void main(){
60
gl_Position = pv_mat * vec4(vert_pos, 1.0f);
61
UV = vert_uv;
62
}
63
)END";
64
65
static
const
char
*
persp_frag_src
= R
"END(
66
#version 330 core
67
in vec2 UV;
68
out vec3 color;
69
uniform sampler2D tex;
70
void main(){
71
color = texture(tex, UV).rgb;
72
}
73
)END";
74
75
76
PanoramaScene::PanoramaScene
():
PanoramaScene
(
GlProgram
(
persp_vert_src
,
persp_frag_src
),
77
Model
::get_sphere()) { }
78
79
PanoramaScene::PanoramaScene
(
GlProgram
program,
Model
model): program(std::move(program)),
80
model(std::move(model))
81
{
82
tex
.
allocateTextures
();
83
}
84
85
void
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
99
void
PanoramaScene::render
(
int
width,
int
height,
const
glm::mat4& pvMat){
100
PROFILE_FUNC
;
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
);
111
if
(
tex
.
is_new_front_available
()){
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
121
void
PanoramaScene::put_frame
(
video_frame
*f,
bool
pbo_frame){
122
PROFILE_FUNC
;
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
144
void
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
FrameUploader::put_frame
void put_frame(video_frame *f, bool pbo_frame=false)
Definition
opengl_utils.cpp:321
FrameUploader::attach_dst_texture
void attach_dst_texture(Texture *tex)
Definition
opengl_utils.hpp:327
GlProgram
Definition
opengl_utils.hpp:70
GlProgram::get
GLuint get()
Definition
opengl_utils.hpp:85
Model
Definition
opengl_utils.hpp:102
Model::render
void render()
Definition
opengl_utils.cpp:142
Texture
Definition
opengl_utils.hpp:147
Texture::get
GLuint get() const
Definition
opengl_utils.hpp:159
TripleBufferTexture::swap_front
void swap_front()
Definition
opengl_utils.hpp:379
TripleBufferTexture::get_back
Texture & get_back()
Definition
opengl_utils.hpp:359
TripleBufferTexture::is_new_front_available
bool is_new_front_available() const
Definition
opengl_utils.hpp:387
TripleBufferTexture::allocateTextures
void allocateTextures()
Definition
opengl_utils.hpp:350
TripleBufferTexture::swap_back
void swap_back()
Definition
opengl_utils.hpp:371
TripleBufferTexture::get_front
Texture & get_front()
Definition
opengl_utils.hpp:364
lock
Definition
resource_manager.cpp:106
debug.h
opengl_conversions.hpp
persp_frag_src
static const char * persp_frag_src
Definition
opengl_panorama.cpp:65
persp_vert_src
static const char * persp_vert_src
Definition
opengl_panorama.cpp:50
opengl_panorama.hpp
opengl_utils.hpp
profile_timer.hpp
PROFILE_FUNC
#define PROFILE_FUNC
Definition
profile_timer.hpp:139
PanoramaScene
Definition
opengl_panorama.hpp:48
PanoramaScene::rotate
void rotate(float dx, float dy)
Definition
opengl_panorama.cpp:144
PanoramaScene::uploader
FrameUploader uploader
Definition
opengl_panorama.hpp:96
PanoramaScene::tex
TripleBufferTexture tex
Definition
opengl_panorama.hpp:93
PanoramaScene::fov
float fov
Definition
opengl_panorama.hpp:99
PanoramaScene::rot_y
float rot_y
Definition
opengl_panorama.hpp:98
PanoramaScene::rot_x
float rot_x
Definition
opengl_panorama.hpp:97
PanoramaScene::model
Model model
Definition
opengl_panorama.hpp:92
PanoramaScene::render
void render(int width, int height)
Definition
opengl_panorama.cpp:85
PanoramaScene::tex_mut
std::mutex tex_mut
Definition
opengl_panorama.hpp:94
PanoramaScene::PanoramaScene
PanoramaScene()
Definition
opengl_panorama.cpp:76
PanoramaScene::program
GlProgram program
Definition
opengl_panorama.hpp:91
PanoramaScene::put_frame
void put_frame(video_frame *f, bool pbo_frame=false)
Definition
opengl_panorama.cpp:121
video_frame
Struct video_frame represents a video frame and contains video description.
Definition
types.h:305
video_frame.h
Generated on Wed Sep 25 2024 14:27:24 for UltraGrid by
1.12.0