Apollo 11.0
自动驾驶开放平台
renderable_object.h
浏览该文件的文档.
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
17#pragma once
18
19#include <QtGui/QOpenGLBuffer>
20#include <QtGui/QOpenGLFunctions>
21#include <QtGui/QOpenGLShaderProgram>
22#include <QtGui/QOpenGLVertexArrayObject>
23#include <memory>
24
25class RenderableObject : protected QOpenGLFunctions {
26 public:
27 static std::shared_ptr<QOpenGLShaderProgram> NullRenderableObj;
28 static std::shared_ptr<QOpenGLShaderProgram> CreateShaderProgram(
29 const QString& vertexShaderFileName, const QString& fragShaderFileName);
30
31 explicit RenderableObject(int vertex_count = 1, int vertex_element_count = 3,
32 const std::shared_ptr<QOpenGLShaderProgram>&
33 shaderProgram = NullRenderableObj);
34 virtual ~RenderableObject(void);
35
36 virtual GLenum GetPrimitiveType(void) const = 0;
37 virtual void SetupExtraUniforms(void) {}
38
39 bool is_renderable(void) const { return is_renderable_; }
40 void set_is_renderable(bool b) { is_renderable_ = b; }
41
42 int vertex_count(void) const { return vertex_count_; }
43 void set_vertex_count(int vertexCount) { vertex_count_ = vertexCount; }
44
45 int vertex_element_count(void) const { return vertex_element_count_; }
46 void set_vertex_element_count(int vertexElementCount) {
47 vertex_element_count_ = vertexElementCount;
48 }
49
50 int VertexBufferSize(void) const {
52 static_cast<int>(sizeof(GLfloat));
53 }
54
56 const std::shared_ptr<QOpenGLShaderProgram>& shaderProgram) {
57 shader_program_ = shaderProgram;
58 }
59
60 bool haveShaderProgram(void) { return shader_program_ != nullptr; }
61
62 bool Init(
63 std::shared_ptr<QOpenGLShaderProgram>& shaderProgram =
64 NullRenderableObj); // initial vao, vbo and call fillVertexBuffer
65 void Destroy(void);
66
67 void Render(const QMatrix4x4* mvp = nullptr);
68
69 protected:
70 virtual bool FillVertexBuffer(GLfloat* pBuffer) = 0;
71 virtual void Draw(void) {
72 glDrawArrays(GetPrimitiveType(), 0, vertex_count());
73 }
74
75 virtual void SetupAllAttrPointer(void) {
76 glEnableVertexAttribArray(0);
77 glVertexAttribPointer(
78 0, vertex_element_count(), GL_FLOAT, GL_FALSE,
79 static_cast<int>(sizeof(GLfloat)) * vertex_element_count(), 0);
80 }
81
84
87
88 std::shared_ptr<QOpenGLShaderProgram> shader_program_;
89 QOpenGLVertexArrayObject vao_;
90 QOpenGLBuffer vbo_;
91};
virtual void SetupAllAttrPointer(void)
int vertex_element_count(void) const
bool Init(std::shared_ptr< QOpenGLShaderProgram > &shaderProgram=NullRenderableObj)
virtual void SetupExtraUniforms(void)
int vertex_count(void) const
int VertexBufferSize(void) const
std::shared_ptr< QOpenGLShaderProgram > shader_program_
virtual ~RenderableObject(void)
void Render(const QMatrix4x4 *mvp=nullptr)
void set_is_renderable(bool b)
void set_shader_program(const std::shared_ptr< QOpenGLShaderProgram > &shaderProgram)
QOpenGLVertexArrayObject vao_
virtual void Draw(void)
void set_vertex_count(int vertexCount)
static std::shared_ptr< QOpenGLShaderProgram > NullRenderableObj
virtual bool FillVertexBuffer(GLfloat *pBuffer)=0
virtual GLenum GetPrimitiveType(void) const =0
void set_vertex_element_count(int vertexElementCount)
static std::shared_ptr< QOpenGLShaderProgram > CreateShaderProgram(const QString &vertexShaderFileName, const QString &fragShaderFileName)
bool is_renderable(void) const
bool haveShaderProgram(void)