Subversion Repositories gelsvn

Rev

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