summaryrefslogtreecommitdiffstats
path: root/dev/a6-harold/billboards.h
blob: 7de2799e1d8c82bc56613827282963695a2b5458 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
/** CSci-4611 Assignment 6: Harold
 */

#ifndef BILLBOARDS_H_
#define BILLBOARDS_H_

#include <mingfx.h>
using namespace mingfx;

#include "ground.h"

/** Billboards are strokes planted in the ground that rotate automatically to
 face the viewer.  This class holds the entire collection of billboard strokes.
 */
class Billboards {
public:
    Billboards();
    virtual ~Billboards();
    
    void Init(ShaderProgram *stroke3d_shaderprog);
    
    /// Projects a 2D normalized screen point (e.g., the mouse position in normalized
    /// device coordinates) to a 3D point on a plane defined by an "origin", which can
    /// really be any point coincident with the plane, and the plane normal.  Returns
    /// true if the screen point projects onto the plane and stores the result in
    /// plane_point.  Returns false if the screen point does not project onto the plane.
    bool ScreenPtHitsPlane(const Matrix4 &view_matrix, const Matrix4 &proj_matrix,
                         const Point3 &plane_origin, const Vector3 &plane_normal,
                         const Point2 &normalized_screen_pt, Point3 *plane_point);
    
    
    /// Checks to see if a ray starting at the eye point and passing through 2D
    /// normalized screen point projects onto any of the billboards in the scene. If
    /// so, returns the id of the closest billboard intersected.  If not, returns -1.
    int IntersectBillboard(const Matrix4 &view_matrix, const Matrix4 &proj_matrix,
                           const Point2 &normalized_screen_pt);
    
    
    /// Adds a new stroke as a billboard by projecting it onto a plane parallel
    /// to the filmplane that intersects with the anchor point, which should lie
    /// on the ground.
    void AddBillboardStroke(const Matrix4 &view_matrix, const Matrix4 &proj_matrix,
                            const std::vector<Point2> &stroke2d, const Mesh &stroke2d_mesh,
                            const Color &stroke_color, Ground *ground_ptr);
    
    /// Edits an existing billboard by adding an additional stroke to it.
    void AddToBillboard(const Matrix4 &view_matrix, const Matrix4 &proj_matrix,
                        int billboard_id, const Mesh &stroke2d_mesh,
                        const Color &stroke_color);
    
    
    /// Adjusts the transformation matrix used to draw each billboard so that the
    /// billboard will face the camera.  Needs to be called each time the camera
    /// is moved within the scene.
    void UpdateBillboardRotations(const Point3 &current_eye_point);
    
    
    /// Draws all of the billboards
    void Draw(const Matrix4 &view_matrix, const Matrix4 &proj_matrix);
    
private:

    // each billboard stores the following
    struct Billboard {
        Mesh mesh;
        Color color;
        Point3 anchor_pt;
        AABB bounding_box;
        Matrix4 transform;
    };

    // the array of all active billboards
    std::vector<Billboard> billboards_;
    
    ShaderProgram *stroke3d_shaderprog_;
};

#endif