quad.cpp
Go to the documentation of this file.
1 /*
2  * Copyright 2014 Google Inc. 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 #include "tango-gl/quad.h"
18 #include "tango-gl/util.h"
19 
20 namespace tango_gl {
21 
22 static const char kVertexShader[] =
23  "attribute vec4 vertex;\n"
24  "attribute vec2 inputTextureCoordinate;\n"
25  "varying vec2 textureCoordinate;\n"
26  "uniform mat4 mvp;\n"
27  "void main() {\n"
28  " gl_Position = mvp*vertex;\n"
29  " textureCoordinate = inputTextureCoordinate.xy;\n"
30  "}\n";
31 
32 static const char kFragmentShader[] =
33  "varying vec2 textureCoordinate;\n"
34  "uniform sampler2D inputTexture;\n"
35  "void main() {\n"
36  " gl_FragColor = texture2D(inputTexture, textureCoordinate);\n"
37  "}\n";
38 
39 static const float vertices[] = {-0.5f, -0.5f, 0.5f, -0.5f,
40  -0.5f, 0.5f, 0.5f, 0.5f};
41 
42 static const GLfloat texture_coords[] = {0.0f, 1.0f, 1.0f, 1.0f,
43  0.0f, 0.0f, 1.0f, 0.0f, };
44 
46  shader_program_ = util::CreateProgram(kVertexShader, kFragmentShader);
47  if (!shader_program_) {
48  LOGE("Could not create program.");
49  }
50  uniform_mvp_mat_ = glGetUniformLocation(shader_program_, "mvp");
51  attrib_vertices_ = glGetAttribLocation(shader_program_, "vertex");
53  glGetAttribLocation(shader_program_, "inputTextureCoordinate");
54  texture_handle = glGetUniformLocation(shader_program_, "inputTexture");
55  glGenBuffers(1, &vertex_buffer_);
56 }
57 
58 Quad::~Quad() { glDeleteShader(shader_program_); }
59 
60 void Quat::SetTextureId(GLuint texture_id) { texture_id_ = texture_id; }
61 
62 void Quad::Render(const glm::mat4& projection_mat,
63  const glm::mat4& view_mat) const {
64  glEnable(GL_CULL_FACE);
65  glEnable(GL_BLEND);
66  glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
67  glUseProgram(shader_program_);
68 
69  glBindTexture(GL_TEXTURE_2D, texture_id_);
70  glUniform1i(texture_handle, 0);
71 
72  // Calculate MVP matrix and pass it to shader.
73  glm::mat4 model_mat = GetTransformationMatrix();
74  glm::mat4 mvp_mat = projection_mat * view_mat * model_mat;
76 
77  // Vertice binding
78  glEnableVertexAttribArray(attrib_vertices_);
79  glVertexAttribPointer(attrib_vertices_, 2, GL_FLOAT, GL_FALSE, 0, vertices);
80  glBindBuffer(GL_ARRAY_BUFFER, 0);
81 
82  glEnableVertexAttribArray(texture_coords_);
83  glVertexAttribPointer(texture_coords_, 2, GL_FLOAT, GL_FALSE, 0,
84  texture_coords);
85 
86  glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
87  glUseProgram(0);
88  glBindTexture(GL_TEXTURE_2D, 0);
89 }
90 
91 } // namespace tango_gl
GLuint uniform_mvp_mat_
Definition: quad.h:39
glm::mat4 GetTransformationMatrix() const
Definition: transform.cpp:67
static const float vertices[]
Definition: quad.cpp:39
static const char kFragmentShader[]
Definition: quad.cpp:32
GLuint vertex_buffer_
Definition: quad.h:34
static const GLfloat texture_coords[]
Definition: quad.cpp:42
GLuint CreateProgram(const char *vertex_source, const char *fragment_source)
Definition: util.cpp:75
GLuint texture_id_
Definition: quad.h:41
#define LOGE(...)
void Render(const glm::mat4 &projection_mat, const glm::mat4 &view_mat) const
Definition: quad.cpp:62
unsigned int GLuint
Definition: dummy.cpp:78
GLM_FUNC_DECL genType::value_type const * value_ptr(genType const &vec)
GLuint shader_program_
Definition: quad.h:35
#define GL_FALSE
Definition: dummy.cpp:79
GLuint texture_coords_
Definition: quad.h:37
GLuint texture_handle
Definition: quad.h:38
void glUniformMatrix4fv(GLuint, int, int, float *)
Definition: dummy.cpp:80
static const char kVertexShader[]
Definition: quad.cpp:22
GLuint attrib_vertices_
Definition: quad.h:36


rtabmap
Author(s): Mathieu Labbe
autogenerated on Wed Jun 5 2019 22:41:32