added 3dengfx into the repo, probably not the correct version for this
[summerhack] / src / 3dengfx / src / 3dengfx / object.hpp
1 /*
2 This file is part of the 3dengfx, realtime visualization system.
3
4 Copyright (c) 2004, 2005 John Tsiombikas <nuclear@siggraph.org>
5
6 3dengfx is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
10
11 3dengfx is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with 3dengfx; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19 */
20
21 /* higher level 3d object abstraction
22  *
23  * Author: John Tsiombikas 2004
24  * Modified: John Tsiombikas 2005
25  */
26
27 #ifndef _OBJECT_HPP_
28 #define _OBJECT_HPP_
29
30 #include <string>
31 #include "gfx/3dgeom.hpp"
32 #include "gfx/animation.hpp"
33 #include "n3dmath2/n3dmath2.hpp"
34 #include "material.hpp"
35 #include "3denginefx.hpp"
36 #include "gfx/bvol.hpp"
37
38 struct RenderParams {
39         bool billboarded;
40         bool zwrite;
41         bool blending;
42         BlendingFactor src_blend, dest_blend;
43         bool handle_blending;
44         GfxProg *gfxprog;
45         bool auto_cube_maps;
46         bool hidden;
47         bool show_normals;
48         scalar_t show_normals_scale;
49         bool highlight;
50         Color highlight_color;
51         scalar_t highlight_line_width;
52         bool two_sided;
53         bool use_vertex_color;
54         TextureAddressing taddr;
55         bool auto_normalize;
56         bool cast_shadows;
57         
58         RenderParams();
59 };
60
61 enum {
62         RMODE_COLOR                     = 1,            // unused
63         RMODE_LIGHTING          = 2,            // unused
64         RMODE_TEXTURES          = 4,
65         RMODE_BLENDING          = 8,
66         RMODE_SHADERS           = 16,
67         RMODE_ALL                       = 0xffff
68 };
69
70 // this bitfield determines which aspects of rendering will actually take place
71 // it overrides all render parameters.
72 extern unsigned long master_render_mode;
73
74 class Object : public XFormNode {
75 private:
76         Matrix4x4 world_mat;
77         RenderParams render_params;
78         BoundingVolume *bvol;
79         bool bvol_valid;
80         
81         void render_hack(unsigned long time);
82
83         void draw_normals();
84         void draw_highlight();
85         
86         void setup_bump_light(unsigned long time);
87         void update_bounding_volume();
88         
89 public:
90         TriMesh mesh;
91         Material mat;
92         
93         Object();
94         Object(const TriMesh &mesh);
95         ~Object();
96         
97         void set_mesh(const TriMesh &mesh);
98         TriMesh *get_mesh_ptr();
99         TriMesh &get_mesh();
100         const TriMesh &get_mesh() const;
101
102         // shortcut functions for accessing the geometry easily
103         unsigned long get_vertex_count() const;
104         const Vertex *get_vertex_data() const;
105         Vertex *get_mod_vertex_data();
106         unsigned long get_triangle_count() const;
107         const Triangle *get_triangle_data() const;
108         Triangle *get_mod_triangle_data();
109
110         void set_dynamic(bool enable);
111         bool get_dynamic() const;
112         
113         void set_material(const Material &mat);
114         Material *get_material_ptr();
115         Material get_material() const;
116         
117         void set_render_params(const RenderParams &rp);
118         RenderParams get_render_params() const;
119         
120         void set_shading(ShadeMode shading_mode);
121         void set_billboarding(bool enable);
122         void set_zwrite(bool enable);
123         void set_blending(bool enable);
124         void set_blending_mode(BlendingFactor sblend, BlendingFactor dblend);
125         void set_handle_blending(bool enable);
126         void set_wireframe(bool enable);
127         void set_gfx_program(GfxProg *prog);
128         void set_auto_cube_maps(bool enable);
129         void set_hidden(bool enable);
130         void set_show_normals(bool enable);
131         void set_show_normals_scale(scalar_t scale);
132         void set_highlight(bool enable);
133         void set_highlight_color(const Color &color);
134         void set_highlight_line_width(scalar_t width);
135         void set_auto_global(bool enable);
136         void set_use_vertex_color(bool enable);
137         void set_texture_addressing(TextureAddressing taddr);
138         void set_auto_normalize(bool enable);
139         void set_shadow_casting(bool enable);
140
141         void apply_xform(unsigned long time = XFORM_LOCAL_PRS);
142
143         void calculate_normals();
144         void normalize_normals();
145         
146         bool render(unsigned long time = XFORM_LOCAL_PRS);
147 };
148
149
150 // --- some convinient derived objects for geom. generation ---
151
152 class ObjCube : public Object {
153 public:
154         ObjCube(scalar_t sz, int subdiv);
155 };
156
157 class ObjPlane : public Object {
158 public:
159         ObjPlane(const Vector3 &normal = Vector3(0, 1, 0), const Vector2 &size = Vector2(1, 1), int subdiv = 0);
160 };
161
162 class ObjCylinder : public Object {
163 public:
164         ObjCylinder(scalar_t rad = 1.0, scalar_t len = 1.0, bool caps = true, int udiv = 12, int vdiv = 0);
165 };
166
167 class ObjSphere : public Object {
168 public:
169         ObjSphere(scalar_t radius = 1.0, int subdiv = 5);
170 };
171
172 class ObjTorus : public Object {
173 public:
174         ObjTorus(scalar_t circle_rad = 0.5, scalar_t revolv_rad = 1.0, int subdiv = 5);
175 };
176
177 class ObjTeapot : public Object {
178 public:
179         ObjTeapot(scalar_t size = 1.0, int subdiv = 5);
180 };
181
182 #endif  // _OBJECT_HPP_