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 
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 Quad::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,
85 
86  glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
87  glUseProgram(0);
88  glBindTexture(GL_TEXTURE_2D, 0);
89 }
90 
91 } // namespace tango_gl
tango_gl::Quad::uniform_mvp_mat_
GLuint uniform_mvp_mat_
Definition: quad.h:39
tango_gl::Quad::vertex_buffer_
GLuint vertex_buffer_
Definition: quad.h:34
tango_gl::vertices
static const float vertices[]
Definition: quad.cpp:39
util.h
glm::value_ptr
GLM_FUNC_DECL const genType::value_type * value_ptr(genType const &vec)
tango_gl::Quad::shader_program_
GLuint shader_program_
Definition: quad.h:35
glm::detail::tmat4x4
Definition: type_mat.hpp:47
LOGE
#define LOGE(...)
Definition: tango-gl/include/tango-gl/util.h:61
tango_gl::Quad::texture_id_
GLuint texture_id_
Definition: quad.h:41
tango_gl::Quad::SetTextureId
void SetTextureId(GLuint texture_id)
Definition: quad.cpp:60
GL_FALSE
#define GL_FALSE
Definition: dummy.cpp:79
GLuint
unsigned int GLuint
Definition: dummy.cpp:78
tango_gl::util::CreateProgram
GLuint CreateProgram(const char *vertex_source, const char *fragment_source)
Definition: util.cpp:75
tango_gl::kFragmentShader
static const char kFragmentShader[]
Definition: quad.cpp:32
tango_gl::kVertexShader
static const char kVertexShader[]
Definition: quad.cpp:22
tango_gl::Quad::texture_coords_
GLuint texture_coords_
Definition: quad.h:37
tango_gl::Quad::texture_handle
GLuint texture_handle
Definition: quad.h:38
tango_gl::Quad::~Quad
~Quad()
Definition: quad.cpp:58
tango_gl::Quad::Render
void Render(const glm::mat4 &projection_mat, const glm::mat4 &view_mat) const
Definition: quad.cpp:62
tango_gl::Quad::attrib_vertices_
GLuint attrib_vertices_
Definition: quad.h:36
glUniformMatrix4fv
void glUniformMatrix4fv(GLuint, int, int, float *)
Definition: dummy.cpp:80
tango_gl
Definition: axis.cpp:20
tango_gl::texture_coords
static const GLfloat texture_coords[]
Definition: quad.cpp:42
tango_gl::Quad::Quad
Quad()
Definition: quad.cpp:45
tango_gl::Transform::GetTransformationMatrix
glm::mat4 GetTransformationMatrix() const
Definition: transform.cpp:67
quad.h


rtabmap
Author(s): Mathieu Labbe
autogenerated on Thu Jul 25 2024 02:50:15