MRPT  2.0.4
CRenderizableShaderTriangles.cpp
Go to the documentation of this file.
1 /* +------------------------------------------------------------------------+
2  | Mobile Robot Programming Toolkit (MRPT) |
3  | https://www.mrpt.org/ |
4  | |
5  | Copyright (c) 2005-2020, Individual contributors, see AUTHORS file |
6  | See: https://www.mrpt.org/Authors - All rights reserved. |
7  | Released under BSD License. See: https://www.mrpt.org/License |
8  +------------------------------------------------------------------------+ */
9 
10 #include "opengl-precomp.h" // Precompiled header
11 
13 #include <mrpt/opengl/Shader.h>
15 #include <mrpt/opengl/opengl_api.h>
16 
17 using namespace mrpt;
18 using namespace mrpt::opengl;
19 
22 
23 // Dtor:
25 
27 {
28 #if MRPT_HAS_OPENGL_GLUT
29  // Generate vertices & colors into m_triangles
30  const_cast<CRenderizableShaderTriangles&>(*this)
32 
33  const auto n = m_triangles.size();
34 
35  // Define OpenGL buffers:
38  m_trianglesBuffer.allocate(m_triangles.data(), sizeof(m_triangles[0]) * n);
39 
40  // VAO: required to use glEnableVertexAttribArray()
41  m_vao.createOnce();
42 #endif
43 }
44 
46 {
47 #if MRPT_HAS_OPENGL_GLUT
48 
49  if (rc.lights && rc.shader->hasUniform("light_diffuse"))
50  {
51  const Program& s = *rc.shader;
52 
53  glUniform4fv(s.uniformId("light_diffuse"), 1, &rc.lights->diffuse.R);
54  glUniform4fv(s.uniformId("light_ambient"), 1, &rc.lights->ambient.R);
55  glUniform4fv(s.uniformId("light_specular"), 1, &rc.lights->specular.R);
56  glUniform3fv(
57  s.uniformId("light_direction"), 1, &rc.lights->direction.x);
58  CHECK_OPENGL_ERROR();
59  }
60 
61  // Set up the vertex array:
62  const GLuint attr_position = rc.shader->attributeId("position");
63  m_vao.bind();
64  glEnableVertexAttribArray(attr_position);
66  glVertexAttribPointer(
67  attr_position, /* attribute */
68  3, /* size */
69  GL_FLOAT, /* type */
70  GL_FALSE, /* normalized? */
71  sizeof(TTriangle::Vertex), /* stride */
72  BUFFER_OFFSET(offsetof(TTriangle::Vertex, xyzrgba.pt.x)));
73  CHECK_OPENGL_ERROR();
74 
75  // Set up the color array:
76  const GLuint attr_color = rc.shader->attributeId("vertexColor");
77  glEnableVertexAttribArray(attr_color);
79  glVertexAttribPointer(
80  attr_color, /* attribute */
81  4, /* size */
82  GL_UNSIGNED_BYTE, /* type */
83  GL_TRUE, /* normalized? */
84  sizeof(TTriangle::Vertex), /* stride */
85  BUFFER_OFFSET(offsetof(TTriangle::Vertex, xyzrgba.r)));
86  CHECK_OPENGL_ERROR();
87 
88  // Set up the normals array:
89  const GLuint attr_normals = rc.shader->attributeId("vertexNormal");
90  glEnableVertexAttribArray(attr_normals);
92  glVertexAttribPointer(
93  attr_normals, /* attribute */
94  3, /* size */
95  GL_FLOAT, /* type */
96  GL_FALSE, /* normalized? */
97  sizeof(TTriangle::Vertex), /* stride */
98  BUFFER_OFFSET(offsetof(TTriangle::Vertex, normal.x)));
99  CHECK_OPENGL_ERROR();
100 
101  glDrawArrays(GL_TRIANGLES, 0, 3 * m_triangles.size());
102  CHECK_OPENGL_ERROR();
103 
104  glDisableVertexAttribArray(attr_position);
105  glDisableVertexAttribArray(attr_color);
106  glDisableVertexAttribArray(attr_normals);
107 
108 #endif
109 }
void createOnce()
Calls create() only if the buffer has not been created yet.
Definition: COpenGLBuffer.h:60
#define IMPLEMENTS_VIRTUAL_SERIALIZABLE(class_name, base_class, NS)
This must be inserted as implementation of some required members for virtual CSerializable classes: ...
const mrpt::opengl::Program * shader
void allocate(const void *data, int byteCount)
Reserves byteCount bytes in the buffer and copy to it the provided data.
Definition: COpenGLBuffer.h:78
The base class of 3D objects that can be directly rendered through OpenGL.
Definition: CRenderizable.h:48
const mrpt::opengl::TLightParameters * lights
Renderizable generic renderer for objects using the triangles shader.
void renderUpdateBuffers() const override
Called whenever m_outdatedBuffers is true: used to re-generate OpenGL vertex buffers, etc.
int attributeId(const char *name) const
Definition: Shader.h:114
Context for calls to render()
virtual void onUpdateBuffers_Triangles()=0
Must be implemented in derived classes to update the geometric entities to be drawn in "m_*_buffer" f...
bool hasUniform(const char *name) const
Definition: Shader.h:116
T x
X,Y,Z coordinates.
Definition: TPoint3D.h:29
This is the global namespace for all Mobile Robot Programming Toolkit (MRPT) libraries.
std::vector< mrpt::opengl::TTriangle > m_triangles
List of triangles.
mrpt::math::TVector3Df direction
Light direction (must be normalized)
The namespace for 3D scene representation and rendering.
Definition: CGlCanvasBase.h:13
void createOnce()
Calls create() only if the buffer has not been created yet.
int uniformId(const char *name) const
Definition: Shader.h:113
A resource handling helper for OpenGL Shader "programs".
Definition: Shader.h:77
#define BUFFER_OFFSET(offset)
void render(const RenderContext &rc) const override
Implements the rendering of 3D objects in each class derived from CRenderizable.



Page generated by Doxygen 1.8.14 for MRPT 2.0.4 Git: 5711e29ae Wed May 27 14:29:47 2020 +0200 at miƩ may 27 14:30:10 CEST 2020