Subversion Repositories gelsvn

Rev

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

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