Subversion Repositories gelsvn

Rev

Rev 647 | Rev 663 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
647 janba 1
/*
2
 *  VisObj.cpp
3
 *  GEL
4
 *
5
 *  Created by J. Andreas Bærentzen on 20/09/08.
6
 *  Copyright 2008 __MyCompanyName__. All rights reserved.
7
 *
8
 */
9
 
10
#include "VisObj.h"
11
 
12
#include <GLGraphics/Console.h>
13
#include <HMesh/Manifold.h>
14
#include <HMesh/AttributeVector.h>
15
#include <HMesh/load.h>
16
#include <HMesh/curvature.h>
17
 
18
#include <CGLA/Mat3x3d.h>
19
#include <CGLA/Vec3d.h>
20
#include <CGLA/Vec4d.h>
21
 
22
using namespace std;
23
using namespace CGLA;
24
using namespace HMesh;
25
using namespace GLGraphics;
26
 
27
int WINX=800, WINY=800;
28
 
29
namespace GLGraphics {
30
 
31
    void VisObj::refit()
32
    {
33
        bsphere(mani, bsphere_center, bsphere_radius);
34
 
35
        view_ctrl.set_centre(Vec3f(bsphere_center));
36
        view_ctrl.set_eye_dist(2*bsphere_radius);
37
    }
38
 
39
    bool VisObj::reload(string _file)
40
    {
41
        if(_file != "") file = _file;
42
        mani.clear();
43
        if(!load(file, mani))
44
            return false;
45
        refit();
46
        return true;
47
    }
48
 
49
    bool VisObj::add_mesh(string file)
50
    {
51
        if(!load(file, mani))
52
            return false;
53
        refit();
54
        return true;
55
    }
56
 
57
 
657 janba 58
    bool VisObj::select_vertex(const CGLA::Vec2i& pos)
647 janba 59
    {
657 janba 60
        float d;
61
        if(depth_pick(pos[0], pos[1], d))
62
        {
63
            Vec3d c;
64
            float r;
65
            bsphere(mani, c, r);
66
            VertexID closest = InvalidVertexID;
67
            double min_dist = DBL_MAX;
68
            for(auto vid : mani.vertices())
69
            {
70
                Vec3d wp = world2screen(mani.pos(vid));
71
                if(sqr_length(Vec2d(wp[0],wp[1])-Vec2d(pos))<100)
72
                {
73
                    double dist = sqr_length(screen2world(pos[0], pos[1], d)-mani.pos(vid));
74
                    if(dist < min_dist)
75
                    {
76
                        min_dist = dist;
77
                        closest = vid;
78
                    }
79
                }
80
            }
81
            if(closest != InvalidVertexID) {
82
                vertex_selection.resize(mani.allocated_vertices(),0);
83
                vertex_selection[closest] = 1;
84
                active_selection = true;
85
                post_create_display_list();
86
                return true;
87
            }
88
        }
89
        return false;
90
 
91
    }
92
 
93
    void VisObj::produce_renderer(const std::string& display_method , Console& cs, bool smooth, float gamma)
94
    {
95
        delete renderer;
96
 
97
        string short_name = display_method.substr(0,3);
98
        if(short_name== "wir")
99
            renderer = new WireframeRenderer(mani, smooth);
100
 
101
        else if(short_name == "har")
102
            renderer = new HarmonicsRenderer(mani, harm, cs);
103
 
104
        else if(short_name == "iso") {
105
            renderer = new IsophoteLineRenderer();
106
            renderer->compile_display_list(mani,smooth);
107
        }
108
        else if(short_name == "ref") {
109
            renderer = new ReflectionLineRenderer();
110
            renderer->compile_display_list(mani,smooth);
111
        }
112
        else if(short_name == "gla") {
113
            renderer = new GlazedRenderer();
114
            dynamic_cast<GlazedRenderer*>(renderer)->compile_display_list(mani,smooth);
115
        }
116
 
117
 
118
        else if(short_name == "too") {
119
            renderer = new ToonRenderer();
120
            renderer->compile_display_list(mani,smooth);
121
        }
122
 
123
 
124
        else if(short_name == "cur"){
125
            static Console::variable<string> line_direction("min");
126
            static Console::variable<string> method("tensors");
127
            static Console::variable<int> smoothing_iter(1);
647 janba 128
 
657 janba 129
            line_direction.reg(cs,"display.curvature_lines.direction", "");
130
            method.reg(cs, "display.curvature_lines.method", "");
131
            smoothing_iter.reg(cs, "display.curvature_lines.smoothing_iter", "");
647 janba 132
 
657 janba 133
            VertexAttributeVector<Mat3x3d> curvature_tensors(mani.allocated_vertices());
134
            VertexAttributeVector<Vec3d> min_curv_direction(mani.allocated_vertices());
135
            VertexAttributeVector<Vec3d> max_curv_direction(mani.allocated_vertices());
136
            string _line_direction = line_direction;
137
            VertexAttributeVector<Vec3d>& lines = (_line_direction == "min") ? min_curv_direction : max_curv_direction;
138
            VertexAttributeVector<Vec2d> curvature(mani.allocated_vertices());
647 janba 139
 
657 janba 140
            if(string(method) == "tensors")
141
            {
142
                curvature_tensors_from_edges(mani, curvature_tensors);
143
                for(int i=0;i<smoothing_iter; ++i)
144
                    smooth_curvature_tensors(mani,curvature_tensors);
145
 
146
                curvature_from_tensors(mani, curvature_tensors,
147
                                       min_curv_direction,
148
                                       max_curv_direction,
149
                                       curvature);
647 janba 150
            }
657 janba 151
            else
152
                curvature_paraboloids(mani,
153
                                      min_curv_direction,
154
                                      max_curv_direction,
155
                                      curvature);
647 janba 156
 
657 janba 157
            renderer = new LineFieldRenderer();
158
            dynamic_cast<LineFieldRenderer*>(renderer)->compile_display_list(mani, lines);
159
        }
160
        else if(short_name == "gau"){
161
            static Console::variable<float> smoothing(2.0f);
162
            smoothing.reg(cs, "display.gaussian_curvature_renderer.smoothing", "");
163
            VertexAttributeVector<double> scalars(mani.allocated_vertices());
164
            gaussian_curvature_angle_defects(mani, scalars, smoothing);
165
            double max_G = 0;
647 janba 166
 
657 janba 167
            for(VertexIDIterator v = mani.vertices_begin(); v != mani.vertices_end(); ++v)
168
                max_G = max(abs(scalars[*v]), max_G);
647 janba 169
 
657 janba 170
            renderer = new ScalarFieldRenderer();
171
            dynamic_cast<ScalarFieldRenderer*>(renderer)->compile_display_list(mani, smooth, scalars, max_G, gamma);
647 janba 172
 
657 janba 173
        }
174
        else if(short_name == "mea"){
175
            static Console::variable<int> smoothing(2);
176
            smoothing.reg(cs, "display.mean_curvature_renderer.smoothing", "");
177
 
178
            VertexAttributeVector<double> scalars(mani.allocated_vertices());
179
            mean_curvatures(mani, scalars, smoothing);
180
            double max_G = 0;
181
            double mean = 0;
182
 
183
            for(VertexIDIterator v = mani.vertices_begin(); v != mani.vertices_end(); ++v){
184
                max_G = max(abs(scalars[*v]), max_G);
185
                mean += scalars[*v];
647 janba 186
            }
187
 
657 janba 188
            renderer = new ScalarFieldRenderer();
189
            dynamic_cast<ScalarFieldRenderer*>(renderer)->compile_display_list(mani, smooth, scalars, max_G, gamma);
190
        }
191
        else if(short_name == "amb"){
192
            static Console::variable<int> smoothing(2);
193
            smoothing.reg(cs, "display.ambient_occlusion_renderer.smoothing", "");
647 janba 194
 
657 janba 195
            VertexAttributeVector<double> scalars(mani.allocated_vertices());
196
            mean_curvatures(mani, scalars, smoothing);
197
            double max_G = 0;
198
 
199
            for(VertexIDIterator v = mani.vertices_begin(); v != mani.vertices_end(); ++v)
200
                max_G = max(abs(scalars[*v]), max_G);
201
 
202
            renderer = new AmbientOcclusionRenderer();
203
            dynamic_cast<AmbientOcclusionRenderer*>(renderer)->compile_display_list(mani, scalars, max_G);
204
 
647 janba 205
        }
657 janba 206
        else if(short_name == "deb")
207
        {
208
            renderer = new DebugRenderer;
209
            renderer->compile_display_list(mani, smooth);
210
        }
211
        else {
212
            renderer = new NormalRenderer();
213
            renderer->compile_display_list(mani, smooth);
214
        }
215
 
216
 
217
    }
218
 
219
    void VisObj::draw_selection()
220
    {
221
        Vec3d c;
222
        float r;
223
        bsphere(mani, c, r);
224
        r *= 0.003;
225
        glDisable(GL_LIGHTING);
226
        for(auto vid : mani.vertices())
227
        {
228
            Vec3d p = mani.pos(vid);
229
            if(vertex_selection[vid])
230
                glColor3f(1,1,0);
231
            else
232
                glColor3f(0, 0, 0.3);
233
            glPushMatrix();
234
            glTranslated(p[0], p[1], p[2]);
235
            glScalef(r, r, r);
236
            draw_ball();
237
            glPopMatrix();
238
        }
239
        glEnable(GL_LIGHTING);
240
    }
241
 
242
 
243
    void VisObj::display(const std::string& display_method , Console& cs, bool smooth, float gamma)
244
    {
245
        if(create_display_list){
246
            create_display_list = false;
247
            produce_renderer(display_method, cs, smooth, gamma);
248
        }
647 janba 249
        view_ctrl.set_gl_modelview();
250
        renderer->draw();
657 janba 251
        if(active_selection)
252
            draw_selection();
647 janba 253
    }
254
}