Apollo 11.0
自动驾驶开放平台
plane.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
19std::shared_ptr<Texture> Plane::NullTextureObj;
20
21Plane::Plane(const std::shared_ptr<Texture>& t)
22 : RenderableObject(4, 4), texture_id_(0), texture_(t) {}
23
24bool Plane::FillVertexBuffer(GLfloat* pBuffer) {
25 if (texture_ == nullptr || !texture_->isDirty()) {
26 return false;
27 }
28 glGenTextures(1, &texture_id_);
29
30 glBindTexture(GL_TEXTURE_2D, texture_id_);
31 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
32 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
33 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_BORDER);
34 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_BORDER);
35
36 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, texture_->width(),
37 texture_->height(), 0, texture_->texture_format(),
38 GL_UNSIGNED_BYTE, texture_->data());
39
40 glBindTexture(GL_TEXTURE_2D, 0);
41
42 texture_->removeDirty();
43
44 pBuffer[0] = -1.0f;
45 pBuffer[1] = -1.0f;
46 pBuffer[2] = 0.0f;
47 pBuffer[3] = 0.0f;
48 pBuffer[4] = 1.0f;
49 pBuffer[5] = -1.0f;
50 pBuffer[6] = 1.0f;
51 pBuffer[7] = 0.0f;
52 pBuffer[8] = 1.0f;
53 pBuffer[9] = 1.0f;
54 pBuffer[10] = 1.0f;
55 pBuffer[11] = 1.0f;
56 pBuffer[12] = -1.0f;
57 pBuffer[13] = 1.0f;
58 pBuffer[14] = 0.0f;
59 pBuffer[15] = 1.0f;
60
61 return true;
62}
63
65 glEnableVertexAttribArray(0);
66 glVertexAttribPointer(
67 0, 2, GL_FLOAT, GL_FALSE,
68 static_cast<int>(sizeof(GLfloat)) * vertex_element_count(), 0);
69
70 glEnableVertexAttribArray(1);
71 glVertexAttribPointer(
72 1, 2, GL_FLOAT, GL_FALSE,
73 static_cast<int>(sizeof(GLfloat)) * vertex_element_count(),
74 reinterpret_cast<void*>(sizeof(GLfloat) * 2));
75}
76
77void Plane::Draw(void) {
78 if (texture_->data()) {
79 if (texture_->isSizeChanged()) {
80 glGenTextures(1, &texture_id_);
81
82 glBindTexture(GL_TEXTURE_2D, texture_id_);
83 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
84 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
85 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_BORDER);
86 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_BORDER);
87
88 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, texture_->width(),
89 texture_->height(), 0, texture_->texture_format(),
90 GL_UNSIGNED_BYTE, texture_->data());
91
92 glBindTexture(GL_TEXTURE_2D, 0);
93
94 texture_->removeDirty();
95 } else if (texture_->isDirty()) {
96 glBindTexture(GL_TEXTURE_2D, texture_id_);
97 glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, texture_->width(),
98 texture_->height(), texture_->texture_format(),
99 GL_UNSIGNED_BYTE, texture_->data());
100 glBindTexture(GL_TEXTURE_2D, 0);
101 texture_->removeDirty();
102 }
103 }
104
105 glActiveTexture(GL_TEXTURE0);
106 glBindTexture(GL_TEXTURE_2D, texture_id_);
108 glBindTexture(GL_TEXTURE_2D, 0);
109}
void SetupAllAttrPointer(void) override
Definition plane.cc:64
static std::shared_ptr< Texture > NullTextureObj
Definition plane.h:35
bool FillVertexBuffer(GLfloat *pBuffer) override
Definition plane.cc:24
Plane(const std::shared_ptr< Texture > &t=NullTextureObj)
Definition plane.cc:21
void Draw(void) override
Definition plane.cc:77
int vertex_element_count(void) const
virtual void Draw(void)