Apollo 11.0
自动驾驶开放平台
video_image_viewer.cc
浏览该文件的文档.
1/******************************************************************************
2 * Copyright 2018 The Apollo Authors. All Rights Reserved.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 *****************************************************************************/
16
18
19#include <iostream>
20
22 : QOpenGLWidget(parent),
23 QOpenGLFunctions(),
24 is_init_(false),
25 mvp_id_(),
26 plane_(),
27 ortho_camera_(),
28 default_image_(nullptr),
29 video_image_shader_prog_(nullptr) {}
30
32 if (is_init_) {
33 is_init_ = false;
34
35 makeCurrent();
36
37 plane_.Destroy();
38 video_image_shader_prog_->destroyed();
39 video_image_shader_prog_.reset();
40 default_image_.reset();
41
42 doneCurrent();
43 }
44}
45
47 initializeOpenGLFunctions();
48
49 glPointSize(1.0f);
50 glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
51
52 QImage noImage;
53 if (!noImage.load(tr(":/images/no_image.png"))) {
54 std::cout << "--------can not load the default texture------------"
55 << std::endl;
56 return;
57 }
58
59 default_image_ = std::make_shared<Texture>();
60 if (default_image_ == nullptr || !default_image_->UpdateData(noImage)) {
61 std::cout << "--------can not create the default texture------------"
62 << std::endl;
63 return;
64 }
65
66 video_image_shader_prog_ = RenderableObject::CreateShaderProgram(
67 tr("/apollo/modules/tools/visualizer/shaders/video_image_plane.vert"),
68 tr("/apollo/modules/tools/visualizer/shaders/video_image_plane.frag"));
69 if (video_image_shader_prog_ == nullptr) {
70 return;
71 }
72
73 plane_.set_texture(default_image_);
74 if (!plane_.Init(video_image_shader_prog_)) {
75 return;
76 }
77
78 ortho_camera_.set_near_plane_width(GLfloat(width()));
79 ortho_camera_.set_near_plane_height(GLfloat(height()));
80
81 ortho_camera_.set_fov(static_cast<float>(height()));
83 ortho_camera_.set_position(0.0f, 0.0f, 0.0f);
84
85 ortho_camera_.UpdateWorld();
86 ortho_camera_.UpdateProjection();
87
88 QMatrix4x4 mvp =
89 ortho_camera_.projection_matrix() * ortho_camera_.model_view_matrix();
90
91 video_image_shader_prog_->bind();
92 video_image_shader_prog_->setUniformValue(mvp_id_, mvp);
93 video_image_shader_prog_->release();
94
95 is_init_ = true;
96}
97
98void VideoImgViewer::resizeGL(int width, int height) {
99 glViewport(0, 0, (GLsizei)width, (GLsizei)height);
100
101 if (is_init_) {
102 ortho_camera_.set_fov(static_cast<float>(height));
103 ortho_camera_.set_near_plane_width(GLfloat(width));
104 ortho_camera_.set_near_plane_height(GLfloat(height));
105 ortho_camera_.UpdateProjection();
106
107 QMatrix4x4 mvp =
108 ortho_camera_.projection_matrix() * ortho_camera_.model_view_matrix();
109
110 video_image_shader_prog_->bind();
111 video_image_shader_prog_->setUniformValue(mvp_id_, mvp);
112 video_image_shader_prog_->release();
113 }
114}
115
117 if (is_init_) {
118 plane_.Render();
119 }
120}
void set_near_plane_width(const float npWidth)
void set_position(const QVector3D &pos)
const QMatrix4x4 & model_view_matrix(void) const
void UpdateProjection(void)
const QMatrix4x4 & projection_matrix(void) const
void set_camera_mode(CameraMode cm)
void set_fov(const float fovInDegrees)
void set_near_plane_height(const float npHeight)
virtual void UpdateWorld(void)
void set_texture(const std::shared_ptr< Texture > &t)
Definition plane.h:40
bool Init(std::shared_ptr< QOpenGLShaderProgram > &shaderProgram=NullRenderableObj)
void Render(const QMatrix4x4 *mvp=nullptr)
static std::shared_ptr< QOpenGLShaderProgram > CreateShaderProgram(const QString &vertexShaderFileName, const QString &fragShaderFileName)
void resizeGL(int w, int h) override
VideoImgViewer(QWidget *parent=nullptr)
void initializeGL() override
void paintGL() override