Subversion Repositories gelsvn

Rev

Rev 401 | 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>
15
 
403 jab 16
/** Ancestral class for Manifold rendering. Do not use directly. Its only purpose is to
17
	create a display list and remove it when the object is destroyed. This is an example
18
	of the RAII "resource acquisition is initialization" idiom */
399 jab 19
class ManifoldRenderer
20
	{
21
	protected:
22
		GLuint display_list;
23
	public:
24
		ManifoldRenderer(): display_list(glGenLists(1))	{}
25
		virtual ~ManifoldRenderer()
26
		{
27
			glDeleteLists(display_list, 1);
28
		}
29
		virtual void draw()
30
		{
31
			glCallList(display_list);
32
		}
33
	};
34
 
403 jab 35
/** Ugly basic gouraud rendering. This class uses OpenGL's fixed function pipeline. */
399 jab 36
class NormalRenderer: public ManifoldRenderer
37
	{
38
	public:
403 jab 39
		NormalRenderer(HMesh::Manifold& m, bool smooth)
399 jab 40
		{
41
			glNewList(display_list,GL_COMPILE);
403 jab 42
			GLGraphics::draw(m,smooth);
399 jab 43
			glEndList();
44
		}
45
	};
46
 
403 jab 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. */
399 jab 50
class WireframeRenderer: public ManifoldRenderer
51
	{
52
		GLGraphics::IDBufferWireframeRenderer* idbuff_renderer;
53
 
54
		int maximum_face_valency(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
 
403 jab 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
	protected:
86
		GLuint prog,vs,fs;
399 jab 87
 
403 jab 88
		/// Compile the vertex and fragment shaders and link to form shader program.
89
		void init_shaders(const std::string& vss, 
90
						  const std::string& fss);
399 jab 91
 
403 jab 92
		/// Produce a display list containing geometry and normals (which may be smooth or per face).
93
		void compile_display_list(HMesh::Manifold& m, bool smooth);
399 jab 94
	public:
403 jab 95
 
96
		/// This constructor simply calls init_shaders and then compile_display_list.
97
		SimpleShaderRenderer(HMesh::Manifold& m, 
98
							bool smooth, 
99
							const std::string& vss, 
100
							const std::string& fss)
399 jab 101
		{
403 jab 102
			init_shaders(vss,fss);
103
			compile_display_list(m, smooth);
399 jab 104
		}
403 jab 105
 
106
		/// This constructor does nothing. Use of you want to control shader program creation or display list generation
107
		SimpleShaderRenderer() {}
108
 
109
		/// Releases the program and shaders.
399 jab 110
		~SimpleShaderRenderer()
111
		{
112
			glDeleteProgram(prog);
113
			glDeleteShader(vs);
114
			glDeleteShader(fs);
115
		}
403 jab 116
 
117
		/// Do the actual drawing. Simply calls the display list if this function is not overloaded.
399 jab 118
		virtual void draw();
119
 
120
	};
121
 
403 jab 122
/** Render reflection lines. This class renders the object as if it is specular and inside
123
	an infinitely long, vertical cylinder with white strips (also vertical). Useful if you
124
	want to see whether the surface is smooth or has kinks. */
399 jab 125
class ReflectionLineRenderer: public SimpleShaderRenderer
126
	{
127
		const static std::string vss;
128
		const static std::string fss;
129
	public:
403 jab 130
		ReflectionLineRenderer(HMesh::Manifold& m, bool smooth):
131
			SimpleShaderRenderer(m, smooth, vss, fss) {}
399 jab 132
 
133
	};
134
 
403 jab 135
/** Render isophotes with respect to a lightsource in the eye. Useful if you
136
 want to see whether the surface is smooth or has kinks. */
399 jab 137
class IsophoteLineRenderer: public SimpleShaderRenderer
138
	{
139
		const static std::string vss;
140
		const static std::string fss;
141
	public:
403 jab 142
		IsophoteLineRenderer(HMesh::Manifold& m, bool smooth):
143
		SimpleShaderRenderer(m, smooth, vss, fss) {}
399 jab 144
 
145
	};
146
 
403 jab 147
/** The toon renderer simply quantizes the shading to give a toonish appearance
148
	with a fat black silhouette. */
149
 
150
class ToonRenderer: public SimpleShaderRenderer
399 jab 151
	{
152
		const static std::string vss;
153
		const static std::string fss;
154
	public:
403 jab 155
		ToonRenderer(HMesh::Manifold& m, bool smooth):
156
		SimpleShaderRenderer(m, smooth, vss, fss) {}
399 jab 157
 
158
	};
159
 
403 jab 160
/** Render like glazed ceramics. Looks cool. I will add more to this. */
401 jab 161
class GlazedRenderer: public SimpleShaderRenderer
162
	{
403 jab 163
		float bsphere_rad;
401 jab 164
		const static std::string vss;
165
		const static std::string fss;
166
	public:
403 jab 167
		GlazedRenderer(HMesh::Manifold& m, bool smooth, float _bsphere_rad=1.0):
168
		SimpleShaderRenderer(m, smooth, vss, fss), bsphere_rad(_bsphere_rad) {}
169
		void draw();
401 jab 170
	};
399 jab 171
 
403 jab 172
/** Render a scalar field. Positive scalars are mapped to blue and negative to red.
173
	This class also has controls for gamma correction which is highly useful if the 
174
	scalars are mostly small or large and simply scaling to the 0-1 range does not 
175
	produce a good result. */
176
class ScalarFieldRenderer: public SimpleShaderRenderer
177
	{
178
		const static std::string vss;
179
		const static std::string fss;
180
	public:
181
		ScalarFieldRenderer(HMesh::Manifold& m, bool smooth,
182
							std::vector<double>& field, double max_val);
183
	};
399 jab 184
 
403 jab 185
/** Line fields are rendered by convolving a noise function in the direction of the line.
186
	This is useful, for instance, for curvature rendering. */
187
class LineFieldRenderer: public SimpleShaderRenderer
188
	{
189
		const static std::string vss;
190
		const static std::string fss;
191
		float r;
192
	public:
193
		LineFieldRenderer(HMesh::Manifold& m, bool smooth, std::vector<CGLA::Vec3d>& lines, float _r);
194
		void draw();
195
	};
401 jab 196
 
403 jab 197
 
198
 
399 jab 199
#endif