Easy3D 2.6.1
Loading...
Searching...
No Matches
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
117 void draw_quad(unsigned int position_attrib, unsigned int texcoord_attrib, int x, int y, int w, int h, int vpw,
118 int vph, float depth);
119
128 void draw_full_screen_quad(unsigned int position_attrib, unsigned int texcoord_attrib, float depth);
129
139 void draw_polygon_wire(const Polygon2 &polygon, const vec4 &color, int width, int height, float depth);
140
158 void draw_polygon_filled(const Polygon2 &polygon, const vec4 &color, int width, int height, float depth);
159
167 void draw_sphere_big_circles(LinesDrawable *drawable, const mat4 &mvp, const mat4 &m, bool axes = true);
168
176 void draw_box_wire(LinesDrawable *drawable, const mat4 &mvp, const mat4 &m, bool abstracted = false);
177 // @}
178
179
180
182 // @{
183
193 void create_grid(int x_steps, int y_steps, std::vector<vec3> &points, float depth = 0.0f, float scale = 0.5f);
194
202 void create_circle(int slices, std::vector<vec3> &points, std::vector<unsigned int> &indices);
203
211 void create_box(std::vector<vec3> &points, std::vector<vec3> &colors, bool abstracted = false);
212
213
225 void create_sphere(
226 const vec3 &center, double radius, int slices, int stacks, const vec3 &color,
227 std::vector<vec3> &points, std::vector<vec3> &normals, std::vector<vec3> &colors
228 );
229
244 const vec3 &center, double radius, int slices, int stacks, int checker_size,
245 const vec3 &color1, const vec3 &color2,
246 std::vector<vec3> &points, std::vector<vec3> &normals, std::vector<vec3> &colors
247 );
248
260 void create_cylinder(
261 double radius, int slices, const vec3 &s, const vec3 &t, const vec3 &color,
262 std::vector<vec3> &points, std::vector<vec3> &normals, std::vector<vec3> &colors
263 );
264
276 void create_cone(
277 double radius, int slices, const vec3 &s, const vec3 &t, const vec3 &color,
278 std::vector<vec3> &points, std::vector<vec3> &normals, std::vector<vec3> &colors
279 );
280
290 void create_torus(double major_radius, double minor_radius, int major_slices, int minor_slices,
291 std::vector<vec3> &points, std::vector<vec3> &normals
292 );
293
302 void create_camera(std::vector<vec3> &points, float width, float fov, float hw_ratio = 0.6f);
303
313 void create_camera(std::vector<vec3> &points, std::vector<unsigned int> &indices, float width, float fov,
314 float hw_ratio = 0.6f);
315 // @}
316
317
318 // create an image plane (useful for rendering images in 3D using camera extrinsic parameters)
319// void create_image_plane(const easy3d::mat3 &R, const easy3d::vec3 &t) {
320// if (!image_plane_) {
321// image_plane_ = new TrianglesDrawable("image_plane");
322// image_plane_->set_visible(show_images_);
323// image_plane_->update_texcoord_buffer({vec2(0, 0), vec2(1, 0), vec2(1, 1), vec2(0, 1)});
324// image_plane_->update_index_buffer({0, 2, 1, 0, 3, 2});
325// add_drawable(image_plane_);
326//
327// if (!texture_1_)
328// texture_1_ = Texture::create(resource::directory() + "/data/image_1.png");
329//
330// if (texture_1_) {
331// image_plane_->set_texture(texture_1_);
332// image_plane_->set_use_texture(true);
333// }
334// }
335//
336// if (!view_frustum_) {
337// view_frustum_ = new LinesDrawable("view_frustum");
338// view_frustum_->set_visible(show_images_);
339// add_drawable(view_frustum_);
340// }
341//
342// const float fov = camera()->fieldOfView();
343// const float hw_ratio = static_cast<float>(height()) / width();
344// const float halfWidth = camera()->sceneRadius() * 2; // large enough to put the image plane behind the object
345// const float halfHeight = halfWidth * hw_ratio;
346// const float dist = halfHeight / std::tan(fov * 0.5);
347//
348// // coordinates in camera frame
349// const vec3 c(0.0f, 0.0f, 0.0f); // camera center
350// std::vector<vec3> corners = {
351// vec3(-halfWidth, -halfHeight, -dist),
352// vec3(halfWidth, -halfHeight, -dist),
353// vec3(halfWidth, halfHeight, -dist),
354// vec3(-halfWidth, halfHeight, -dist)
355// };
356//
357// Box3 box = current_model()->bounding_box();
358// // convert to world coordinate system
359// for (auto &p : corners) {
360// p = camera()->worldCoordinatesOf(p);
361// box.grow(p);
362// }
363// box.grow(camera()->position());
364// camera()->setSceneBoundingBox(box);
365//
366// image_plane_->update_vertex_buffer(corners);
367//
368// corners.push_back(camera()->position());
369// view_frustum_->update_vertex_buffer(corners);
370// view_frustum_->update_index_buffer({0, 4, 1, 4, 2, 4, 3, 4});
371// }
372
373 }
374
375}
376
377#endif // EASY3D_RENDERER_SHAPE_H
The drawable for rendering a set of line segments, e.g., edges of a mesh, vector fields.
Definition drawable_lines.h:40
A collection of functions for rendering basic shapes.
Definition shape.cpp:40
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:44
GenericPolygon< float > Polygon2
A 2D polygon of float type.
Definition types.h:116
GenericRect< float > Rect
A 2D axis-aligned rectangle of float type.
Definition types.h:111
Mat4< float > mat4
A 4 by 4 matrix of float type.
Definition types.h:67
Vec< 4, float > vec4
A 4D point/vector of float type.
Definition types.h:46