added 3dengfx into the repo, probably not the correct version for this
[summerhack] / src / 3dengfx / src / 3dengfx / rend_curve.cpp
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 /* renderable curves
22  *
23  * Author: John Tsiombikas 2005
24  */
25
26 #include "3denginefx.hpp"
27 #include "rend_curve.hpp"
28
29 RendCurve::RendCurve(Curve *curve) {
30         this->curve = curve;
31         stroke = false;
32         width = 1.0;
33         detail = 5;
34         set_blending_mode(BLEND_SRC_ALPHA, BLEND_ONE_MINUS_SRC_ALPHA);
35 }
36
37 void RendCurve::set_curve(Curve *curve) {
38         this->curve = curve;
39 }
40
41 Curve *RendCurve::get_curve() {
42         return curve;
43 }
44
45 void RendCurve::set_width(scalar_t width) {
46         this->width = width;
47 }
48
49 scalar_t RendCurve::get_width() const {
50         return width;
51 }
52
53 void RendCurve::set_blending_mode(BlendingFactor sblend, BlendingFactor dblend) {
54         src_blend = sblend;
55         dst_blend = dblend;
56 }
57
58 void RendCurve::set_stroke(bool enable) {
59         stroke = enable;
60 }
61
62 void RendCurve::set_detail(int detail) {
63         this->detail = detail;
64 }
65
66 void RendCurve::set_material(const Material &mat) {
67         this->mat = mat;
68 }
69
70 Material *RendCurve::get_material_ptr() {
71         return &mat;
72 }
73
74 Material RendCurve::get_material() const {
75         return mat;
76 }
77
78 bool RendCurve::render(unsigned long time) {
79         if(!curve) return false;
80         
81         set_matrix(XFORM_WORLD, get_prs(time).get_xform_matrix());
82         mat.set_glmaterial();
83
84         if(mat.tex[TEXTYPE_DIFFUSE]) {
85                 set_texture(0, mat.tex[TEXTYPE_DIFFUSE]);
86                 enable_texture_unit(0);
87                 set_texture_coord_index(0, 0);
88                 set_texture_unit_color(0, TOP_MODULATE, TARG_TEXTURE, TARG_PREV);
89                 set_texture_unit_alpha(0, TOP_MODULATE, TARG_TEXTURE, TARG_PREV);
90         }
91
92         set_alpha_blending(true);
93         set_zwrite(false);
94         set_blend_func(src_blend, dst_blend);
95
96         int line_count = curve->get_segment_count() * detail;
97         scalar_t dx = 1.0 / (scalar_t)line_count;
98         scalar_t t = dx;
99         Vector3 prev_pos = (*curve)(0.0);
100         for(int i=1; i<line_count; i++) {
101                 Vector3 pos = (*curve)(t);
102                 draw_line(Vertex(prev_pos, 0.0), Vertex(pos, 1.0), width, width);
103                 prev_pos = pos;
104                 t += dx;
105         }
106         
107         set_alpha_blending(false);
108         set_zwrite(true);
109         disable_texture_unit(0);
110
111         return true;
112 }
113
114 bool RendCurve::render_segm(float start, float end, unsigned long time) {
115         if(!curve) return false;
116         
117         set_matrix(XFORM_WORLD, get_prs(time).get_xform_matrix());
118         mat.set_glmaterial();
119
120         if(mat.tex[TEXTYPE_DIFFUSE]) {
121                 set_texture(0, mat.tex[TEXTYPE_DIFFUSE]);
122                 enable_texture_unit(0);
123                 set_texture_coord_index(0, 0);
124                 set_texture_unit_color(0, TOP_MODULATE, TARG_TEXTURE, TARG_PREV);
125                 set_texture_unit_alpha(0, TOP_MODULATE, TARG_TEXTURE, TARG_PREV);
126         }
127
128         set_alpha_blending(true);
129         set_zwrite(false);
130         set_blend_func(src_blend, dst_blend);
131
132         int line_count = curve->get_segment_count() * detail;
133         scalar_t dx = (end - start) / (scalar_t)line_count;
134         scalar_t t = start + dx;
135         Vector3 prev_pos = (*curve)(start);
136         for(int i=1; i<line_count; i++) {
137                 Vector3 pos = (*curve)(t);
138                 draw_line(Vertex(prev_pos, (float)i / (float)line_count), Vertex(pos, (float)(i + 1) / line_count), width, width, mat.diffuse_color);
139                 prev_pos = pos;
140                 t += dx;
141         }
142         
143         set_alpha_blending(false);
144         set_zwrite(true);
145         disable_texture_unit(0);
146
147         return true;
148 }