Subversion Repositories gelsvn

Rev

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

Rev Author Line No. Line
572 jab 1
/* ----------------------------------------------------------------------- *
2
 * This file is part of GEL, http://www.imm.dtu.dk/GEL
3
 * Copyright (C) the authors and DTU Informatics
4
 * For license and list of authors, see ../../doc/intro.pdf
5
 * ----------------------------------------------------------------------- */
578 jab 6
/**
7
 * @file ManifoldRenderer.h
8
 * @brief Tool for rendering a HMesh Manifold.
9
 */
572 jab 10
 
557 jab 11
#ifndef __MESHEDIT_RENDERER_H__
12
#define __MESHEDIT_RENDERER_H__
13
 
601 jab 14
#include "../GL/glew.h"
15
#include "../GLGraphics/draw.h"
16
#include "../GLGraphics/IDBufferWireFrameRenderer.h"
17
#include "../CGLA/Vec4d.h"
557 jab 18
 
19
namespace HMesh
20
{
21
    template<typename ITEM>
22
    class VertexAttributeVector;
23
}
24
 
25
namespace GLGraphics {
26
 
27
/** Ancestral class for Manifold rendering. Do not use directly. Its only purpose is to
28
	create a display list and remove it when the object is destroyed. This is an example
29
	of the RAII "resource acquisition is initialization" idiom */
30
class ManifoldRenderer
31
	{
32
	protected:
33
		GLuint display_list;
34
	public:
35
		ManifoldRenderer(): display_list(glGenLists(1))	{}
36
		virtual ~ManifoldRenderer()
37
		{
38
			glDeleteLists(display_list, 1);
39
		}
40
		virtual void draw()
41
		{
42
			glCallList(display_list);
43
		}
44
	};
45
 
46
 
47
/** Wireframe rendering. This is a nasty complex class that relies on other classes. The trouble
48
	is that for non-triangle meshes, we need to use another approach than for triangle meshes.
49
	This class is really a front end for a couple of other classes. */
50
class WireframeRenderer: public ManifoldRenderer
51
	{
52
		GLGraphics::IDBufferWireframeRenderer* idbuff_renderer;
53
 
54
		int maximum_face_valency(const HMesh::Manifold& m);
55
 
56
	public:
57
		~WireframeRenderer()
58
		{
59
			delete idbuff_renderer;
60
		}
61
 
62
		WireframeRenderer(HMesh::Manifold& m, bool flat);
63
 
64
		void draw();
65
	};
66
 
67
/** SimpleShaderRenderer is a very basic class for drawing a Manifold with shading.
68
	It is a convenient way to draw a surface using vertex and fragment shaders since it takes
69
	care of initializing the shaders and producing a display list for the geometry. 
70
 
71
	Geometry shaders typically add more complexity and are left out of this class, so you cannot add a
72
	geometry shader.
73
 
74
	While this class can be used directly, the normal procedure is to inherit from SimpleShaderRenderer
75
	and then pass the shaders to the constructor. The strings defining the shaders would fit
76
	nicely as static constant strings (see e.g. ToonRenderer or GlazedRenderer) in your inherited class.
77
 
78
	If you need to define more attributes or uniforms, you need to take charge. Your inherited class's 
79
	constructor should then use the default constructor of SimpleShaderRenderer. You can call init_shaders
80
	to initialize the shaders and then compile the display list yourself with the needed uniforms and
81
	attributes - rather than calling compile_display_list which only puts vertices and normals in the list. 
82
 */
83
class SimpleShaderRenderer: public ManifoldRenderer
84
	{
85
		/// Compile the vertex and fragment shaders and link to form shader program.
86
		void init_shaders(const std::string& vss, 
87
						  const std::string& fss);
88
 
89
		/// Produce a display list containing geometry and normals (which may be smooth or per face).
90
		void compile_display_list(const HMesh::Manifold& m, bool smooth);
91
 
92
	protected:
93
 
94
		GLuint prog,vs,fs;
95
 
96
	public:
97
 
98
		/// This constructor simply calls init_shaders and then compile_display_list.
99
		SimpleShaderRenderer(const HMesh::Manifold& m, 
100
							bool smooth, 
101
							const std::string& vss, 
102
							const std::string& fss)
103
		{
104
			init_shaders(vss,fss);
105
			compile_display_list(m, smooth);
106
		}
107
 
108
		/** This constructor simply initializes the shaders. It does not create the display list.
109
			Use if you shader has extra attributes. */
110
		SimpleShaderRenderer(const std::string& vss, 
111
							 const std::string& fss) {init_shaders(vss,fss);}
112
 
113
		/// Releases the program and shaders.
114
		~SimpleShaderRenderer()
115
		{
116
			glDeleteProgram(prog);
117
			glDeleteShader(vs);
118
			glDeleteShader(fs);
119
		}
120
 
121
		/// Do the actual drawing. Simply calls the display list if this function is not overloaded.
122
		virtual void draw();
123
 
124
	};
125
 
126
/** Ugly basic gouraud rendering. This class uses OpenGL's fixed function pipeline. */
127
class NormalRenderer: public SimpleShaderRenderer
128
{
129
    const static std::string vss;
130
    const static std::string fss;
131
 
132
public:
133
    NormalRenderer(const HMesh::Manifold& m, bool smooth):
134
    SimpleShaderRenderer(m, smooth, vss, fss) {}
135
};
136
 
137
 
138
/** Render reflection lines. This class renders the object as if it is specular and inside
139
	an infinitely long, vertical cylinder with white strips (also vertical). Useful if you
140
	want to see whether the surface is smooth or has kinks. */
141
class ReflectionLineRenderer: public SimpleShaderRenderer
142
	{
143
		const static std::string vss;
144
		const static std::string fss;
145
	public:
146
		ReflectionLineRenderer(const HMesh::Manifold& m, bool smooth):
147
			SimpleShaderRenderer(m, smooth, vss, fss) {}
148
 
149
	};
150
 
151
/** Render isophotes with respect to a lightsource in the eye. Useful if you
152
 want to see whether the surface is smooth or has kinks. */
153
class IsophoteLineRenderer: public SimpleShaderRenderer
154
	{
155
		const static std::string vss;
156
		const static std::string fss;
157
	public:
158
		IsophoteLineRenderer(const HMesh::Manifold& m, bool smooth):
159
		SimpleShaderRenderer(m, smooth, vss, fss) {}
160
 
161
	};
162
 
163
/** The toon renderer simply quantizes the shading to give a toonish appearance
164
	with a fat black silhouette. */
165
 
166
class ToonRenderer: public SimpleShaderRenderer
167
	{
168
		const static std::string vss;
169
		const static std::string fss;
170
	public:
171
		ToonRenderer(const HMesh::Manifold& m, bool smooth):
172
		SimpleShaderRenderer(m, smooth, vss, fss) {}
173
 
174
	};
175
 
176
/** Render like glazed ceramics. Looks cool. I will add more to this. */
177
class GlazedRenderer: public SimpleShaderRenderer
178
	{
179
		float bsphere_rad;
180
		const static std::string vss;
181
		const static std::string fss;
182
	public:
183
		GlazedRenderer(const HMesh::Manifold& m, bool smooth, float _bsphere_rad=1.0):
184
		SimpleShaderRenderer(m, smooth, vss, fss), bsphere_rad(_bsphere_rad) {}
185
		void draw();
186
	};
187
 
188
/** Render a scalar field. Positive scalars are mapped to blue and negative to red.
189
	This class also has controls for gamma correction which is highly useful if the 
190
	scalars are mostly small or large and simply scaling to the 0-1 range does not 
191
	produce a good result. */
192
class ScalarFieldRenderer: public SimpleShaderRenderer
193
	{
194
		const static std::string vss;
195
		const static std::string fss;
196
	public:
197
		ScalarFieldRenderer(const HMesh::Manifold& m, bool smooth, HMesh::VertexAttributeVector<double>& field, double max_val, float gamma = 2.2);
198
	};
199
 
572 jab 200
    /** Render a scalar field. Positive scalars are mapped to blue and negative to red.
201
     This class also has controls for gamma correction which is highly useful if the 
202
     scalars are mostly small or large and simply scaling to the 0-1 range does not 
203
     produce a good result. */
204
    class PeriodicScalarFieldRenderer: public SimpleShaderRenderer
205
	{
206
		const static std::string vss;
207
		const static std::string fss;
208
	public:
209
		PeriodicScalarFieldRenderer(const HMesh::Manifold& m, bool smooth, HMesh::VertexAttributeVector<double>& field, float gamma = 2.2);
210
	};
211
 
557 jab 212
/** Ambient occlusion renderer. Very similar to ScalarFieldRender. Simply assumes that the input values are
213
	mean curvatures which in some sense indicate how concave the surface is.*/
214
class AmbientOcclusionRenderer: public SimpleShaderRenderer
215
	{
216
		const static std::string vss;
217
		const static std::string fss;
218
	public:
219
		AmbientOcclusionRenderer(const HMesh::Manifold& m, bool smooth, HMesh::VertexAttributeVector<double>& field, double max_val);
220
	};
221
 
222
 
223
/** Line fields are rendered by convolving a noise function in the direction of the line.
224
	This is useful, for instance, for curvature rendering. */
225
class LineFieldRenderer: public SimpleShaderRenderer
226
	{
227
		const static std::string vss;
228
		const static std::string fss;
229
		float r;
230
	public:
566 jab 231
		LineFieldRenderer(const HMesh::Manifold& m, bool smooth, HMesh::VertexAttributeVector<CGLA::Vec3d>& lines, float _r);
557 jab 232
		void draw();
233
	};
234
 
235
/** Class for wireframe rendering. Enable it will case all triangles to be drawn to be drawn as
236
 wireframe. Only triangles are supported, but it is fast. */
237
class DualVertexRenderer: public ManifoldRenderer
238
	{
239
		const static std::string vss;
240
		const static std::string gss;
241
		const static std::string fss;
242
	public:
243
		/// The constructor creates the wireframe shader program. It is static so the program is shared.
244
		DualVertexRenderer(const HMesh::Manifold& m, HMesh::VertexAttributeVector<CGLA::Vec4d>& field);
245
	};
246
}
247
 
248
#endif