Easy3D 2.5.3
shape.h
1/********************************************************************
2 * Copyright (C) 2015 Liangliang Nan <liangliang.nan@gmail.com>
3 * https://3d.bk.tudelft.nl/liangliang/
4 *
5 * This file is part of Easy3D. If it is useful in your research/work,
6 * I would be grateful if you show your appreciation by citing it:
7 * ------------------------------------------------------------------
8 * Liangliang Nan.
9 * Easy3D: a lightweight, easy-to-use, and efficient C++ library
10 * for processing and rendering 3D data.
11 * Journal of Open Source Software, 6(64), 3255, 2021.
12 * ------------------------------------------------------------------
13 *
14 * Easy3D is free software; you can redistribute it and/or modify
15 * it under the terms of the GNU General Public License Version 3
16 * as published by the Free Software Foundation.
17 *
18 * Easy3D is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU General Public License for more details.
22 *
23 * You should have received a copy of the GNU General Public License
24 * along with this program. If not, see <http://www.gnu.org/licenses/>.
25 ********************************************************************/
26
27#ifndef EASY3D_RENDERER_SHAPE_H
28#define EASY3D_RENDERER_SHAPE_H
29
30#include <vector>
31
32#include <easy3d/core/types.h>
33
34
35namespace easy3d {
36
37 class LinesDrawable;
38
43 namespace shape {
44
46 // @{
47
57 void draw_quad_wire(const Rect &rect, const vec4 &color, int width, int height, float depth);
58
68 void draw_quad_filled(const Rect &rect, const vec4 &color, int width, int height, float depth);
69
79 void draw_quad_filled(const Rect &rect, unsigned int texture, int width, int height, float depth);
80
87 void draw_full_screen_quad(unsigned int texture, float depth);
88
101 void draw_depth_texture(const Rect &rect, unsigned int texture, int width, int height, float depth);
102
114 void draw_quad(unsigned int position_attrib, unsigned int texcoord_attrib, int x, int y, int w, int h, int vpw,
115 int vph, float depth);
116
125 void draw_full_screen_quad(unsigned int position_attrib, unsigned int texcoord_attrib, float depth);
126
136 void draw_polygon_wire(const Polygon2 &polygon, const vec4 &color, int width, int height, float depth);
137
155 void draw_polygon_filled(const Polygon2 &polygon, const vec4 &color, int width, int height, float depth);
156
164 void draw_sphere_big_circles(LinesDrawable *drawable, const mat4 &mvp, const mat4 &m, bool axes = true);
165
173 void draw_box_wire(LinesDrawable *drawable, const mat4 &mvp, const mat4 &m, bool abstracted = false);
174 // @}
175
176
177
179 // @{
180
189 void create_grid(int x_steps, int y_steps, std::vector<vec3> &points, float depth = 0.0f, float scale = 0.5f);
190
196 void create_circle(int slices, std::vector<vec3> &points, std::vector<unsigned int> &indices);
197
203 void create_box(std::vector<vec3> &points, std::vector<vec3> &colors, bool abstracted = false);
204
205
212 void create_sphere(
213 const vec3 &center, double radius, int slices, int stacks, const vec3 &color,
214 std::vector<vec3> &points, std::vector<vec3> &normals, std::vector<vec3> &colors
215 );
216
226 const vec3 &center, double radius, int slices, int stacks, int checker_size,
227 const vec3 &color1, const vec3 &color2,
228 std::vector<vec3> &points, std::vector<vec3> &normals, std::vector<vec3> &colors
229 );
230
234 void create_cylinder(
235 double radius, int slices, const vec3 &s, const vec3 &t, const vec3 &color,
236 std::vector<vec3> &points, std::vector<vec3> &normals, std::vector<vec3> &colors
237 );
238
243 void create_cone(
244 double radius, int slices, const vec3 &s, const vec3 &t, const vec3 &color,
245 std::vector<vec3> &points, std::vector<vec3> &normals, std::vector<vec3> &colors
246 );
247
257 void create_torus(double major_radius, double minor_radius, int major_slices, int minor_slices,
258 std::vector<vec3> &points, std::vector<vec3> &normals
259 );
260
268 void create_camera(std::vector<vec3> &points, float width, float fov, float hw_ratio = 0.6f);
269
277 void create_camera(std::vector<vec3> &points, std::vector<unsigned int> &indices, float width, float fov,
278 float hw_ratio = 0.6f);
279 // @}
280
281
282 // create an image plane (useful for rendering images in 3D using camera extrinsic parameters)
283// void create_image_plane(const easy3d::mat3 &R, const easy3d::vec3 &t) {
284// if (!image_plane_) {
285// image_plane_ = new TrianglesDrawable("image_plane");
286// image_plane_->set_visible(show_images_);
287// image_plane_->update_texcoord_buffer({vec2(0, 0), vec2(1, 0), vec2(1, 1), vec2(0, 1)});
288// image_plane_->update_index_buffer({0, 2, 1, 0, 3, 2});
289// add_drawable(image_plane_);
290//
291// if (!texture_1_)
292// texture_1_ = Texture::create(resource::directory() + "/data/image_1.png");
293//
294// if (texture_1_) {
295// image_plane_->set_texture(texture_1_);
296// image_plane_->set_use_texture(true);
297// }
298// }
299//
300// if (!view_frustum_) {
301// view_frustum_ = new LinesDrawable("view_frustum");
302// view_frustum_->set_visible(show_images_);
303// add_drawable(view_frustum_);
304// }
305//
306// const float fov = camera()->fieldOfView();
307// const float hw_ratio = static_cast<float>(height()) / width();
308// const float halfWidth = camera()->sceneRadius() * 2; // large enough to put the image plane behind the object
309// const float halfHeight = halfWidth * hw_ratio;
310// const float dist = halfHeight / std::tan(fov * 0.5);
311//
312// // coordinates in camera frame
313// const vec3 c(0.0f, 0.0f, 0.0f); // camera center
314// std::vector<vec3> corners = {
315// vec3(-halfWidth, -halfHeight, -dist),
316// vec3(halfWidth, -halfHeight, -dist),
317// vec3(halfWidth, halfHeight, -dist),
318// vec3(-halfWidth, halfHeight, -dist)
319// };
320//
321// Box3 box = current_model()->bounding_box();
322// // convert to world coordinate system
323// for (auto &p : corners) {
324// p = camera()->worldCoordinatesOf(p);
325// box.grow(p);
326// }
327// box.grow(camera()->position());
328// camera()->setSceneBoundingBox(box);
329//
330// image_plane_->update_vertex_buffer(corners);
331//
332// corners.push_back(camera()->position());
333// view_frustum_->update_vertex_buffer(corners);
334// view_frustum_->update_index_buffer({0, 4, 1, 4, 2, 4, 3, 4});
335// }
336
337 }
338
339}
340
341#endif // EASY3D_RENDERER_SHAPE_H
void create_sphere(const vec3 &center, double radius, int slices, int stacks, const vec3 &color, std::vector< vec3 > &points, std::vector< vec3 > &normals, std::vector< vec3 > &colors)
Generates data (points, normals, and colors) for a 3D sphere.
Definition: shape.cpp:767
void draw_quad_filled(const Rect &rect, const vec4 &color, int width, int height, float depth)
Draws a solid quad defined in the screen space.
Definition: shape.cpp:90
void draw_polygon_wire(const Polygon2 &polygon, const vec4 &color, int width, int height, float depth)
Draws a polygon (line loop) in the screen space.
Definition: shape.cpp:451
void draw_polygon_filled(const Polygon2 &polygon, const vec4 &color, int width, int height, float depth)
Draws a filled polygon in the screen space.
Definition: shape.cpp:492
void create_camera(std::vector< vec3 > &points, float width, float fov, float hw_ratio)
Generates data (points) for representing a camera in the 3D world as a set of lines.
Definition: shape.cpp:985
void create_grid(int x_steps, int y_steps, std::vector< vec3 > &points, float depth, float scale)
Generates data for a grid as a set of line segments.
Definition: shape.cpp:677
void create_checker_sphere(const vec3 &center, double radius, int slices, int stacks, int checker_size, const vec3 &color1, const vec3 &color2, std::vector< vec3 > &points, std::vector< vec3 > &normals, std::vector< vec3 > &colors)
Generates data (points, normals, and colors) for a 3D checker sphere.
Definition: shape.cpp:774
void draw_quad(unsigned int positionAttrib, unsigned int texcoordAttrib, int x, int y, int w, int h, int vpw, int vph, float depth)
Draws a quad defined in the screen space using a bound shader.
Definition: shape.cpp:336
void draw_sphere_big_circles(LinesDrawable *drawable, const mat4 &mvp, const mat4 &m, bool axes)
Draws the outline (the 3 big circles) of a sphere.
Definition: shape.cpp:553
void create_torus(double major_radius, double minor_radius, int major_slices, int minor_slices, std::vector< vec3 > &points, std::vector< vec3 > &normals)
Prepares data for representing a torus.
Definition: shape.cpp:930
void create_circle(int slices, std::vector< vec3 > &points, std::vector< unsigned int > &indices)
Generates data for a unit circle as a set of line segments.
Definition: shape.cpp:749
void create_cylinder(double radius, int slices, const vec3 &s, const vec3 &t, const vec3 &color, std::vector< vec3 > &points, std::vector< vec3 > &normals, std::vector< vec3 > &colors)
Prepares data (points, normals, and colors) for a 3D cylinder defined by two 3D points s and t.
Definition: shape.cpp:839
void create_box(std::vector< vec3 > &points, std::vector< vec3 > &colors, bool abstracted)
Generates data for a unit box as a set of line segments.
Definition: shape.cpp:698
void create_cone(double radius, int slices, const vec3 &s, const vec3 &t, const vec3 &color, std::vector< vec3 > &points, std::vector< vec3 > &normals, std::vector< vec3 > &colors)
Prepares data (points, normals, and colors) for a 3D cone defined by two 3D points b and t....
Definition: shape.cpp:884
void draw_box_wire(LinesDrawable *drawable, const mat4 &mvp, const mat4 &m, bool abstracted)
Draws a box.
Definition: shape.cpp:641
void draw_quad_wire(const Rect &rect, const vec4 &color, int width, int height, float depth)
Draws a wire quad defined in the screen space.
Definition: shape.cpp:42
void draw_full_screen_quad(unsigned int texture, float depth)
Draws a full screen textured quad.
Definition: shape.cpp:211
void draw_depth_texture(const Rect &rect, unsigned int texture, int width, int height, float depth)
Draws a quad visualizing a depth texture in a region.
Definition: shape.cpp:269
Definition: collider.cpp:182
Vec< 3, float > vec3
A 3D point/vector of float type.
Definition: types.h:45
GenericPolygon< float > Polygon2
A 2D polygon of float type.
Definition: types.h:117
GenericRect< float > Rect
A 2D axis-aligned rectangle of float type.
Definition: types.h:112
Mat4< float > mat4
A 4 by 4 matrix of float type.
Definition: types.h:68
Vec< 4, float > vec4
A 4D point/vector of float type.
Definition: types.h:47