summaryrefslogtreecommitdiff
path: root/cad/plugins/NanoVision-1/src/Plugins/RenderingEngines/OpenGL/NXOpenGLRenderingEngine.h
blob: 8d09ccb71125da50bfd42c14bd884c658eef6899 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
// Copyright 2008 Nanorex, Inc.  See LICENSE file for details.

#ifndef NX_OPENGLRENDERINGENGINE_H
#define NX_OPENGLRENDERINGENGINE_H

#include <set>
#include <list>
#include <vector>
#include <functional>

// #include <QtGui>
#include <QtOpenGL>

#include "GLT/glt_light.h"
#include "GLT/glt_lightm.h"
// #include "glt_material.h"
#include "GLT/glt_vector3.h"
#include "GLT/glt_project.h"
#include "GLT/glt_viewport.h"
#include "GLT/glt_bbox.h"

#include <Nanorex/Interface/NXRenderingEngine.h>
#include <Nanorex/Interface/NXMoleculeSet.h>
#include "Nanorex/Utility/NXRGBColor.h"
#include "NXOpenGLSceneGraph.h"
#include "NXOpenGLRendererPlugin.h"
#include "NXOpenGLCamera.h"


/* CLASS: NXOpenGLRenderingEngine */
/**
  *  Renders the molecule set using plain OpenGL. Can be instantiate for
  *  display of molecules or molecule-sets using the template parameter.
  *
  * \internal This class delegates the actual task of creating scenegraphs
  *  and rendering it to an OpenGL context to the NXOpenGLRenderingEngine_delegate
  *  class. The task of the template parameter is to filter what can be
  *  displayed through this window and much of the rendering part is independent
  *  so maintaining a template-less delegate is better code reuse.
  *
  * @ingroup NanorexInterface, PluginArchitecture, GraphicsArchitecture
  */
class NXOpenGLRenderingEngine :
	public QGLWidget,
	public Nanorex::NXRenderingEngine
{
    Q_OBJECT;
	Q_INTERFACES(Nanorex::NXRenderingEngine);

public:
    
    
    NXOpenGLRenderingEngine(QWidget *parent = 0);
    
    virtual ~NXOpenGLRenderingEngine();
    
    // override base-class virtual methods
    
	// RenderingEngineID getID(void) const { return OPENGL; }
    
	QWidget* asQWidget(void) {
		QGLWidget *asQGLWidget = static_cast<QGLWidget*>(this);
		return static_cast<QWidget*>(asQGLWidget);
	}
	
	bool initializePlugins(void);
	
	bool cleanupPlugins(void);
	
	NXRenderingEngine* newInstance(QWidget *parent=0) const;
	
	bool isRendererCompatible(QObject *plugin) const;
	
	// void setCurrentFrame(int frameIdx);
	
	Nanorex::NXRendererPlugin* renderer_cast(QObject *plugin) const;
	
    /// Reset the view based on the atom-bond distribution in the
	/// current frame's molecule-set
	void resetView (void);
	
	void setNamedView(Nanorex::NXNamedView const& view);
	
    // Mouse-event handlers
	void mousePressEvent(QMouseEvent *mouseEvent);
	void mouseReleaseEvent(QMouseEvent *mouseEvent);
	void mouseMoveEvent(QMouseEvent *mouseEvent);
	void wheelEvent(QWheelEvent *event);
    
private:
    
	typedef unsigned int uint;
	
	template<typename T1, typename T2>
		struct PairwiseLess : public binary_function<T1, T2, bool> {
			bool operator () (pair<T1,T2> const& a, pair<T1,T2> const& b) const
			{
				return (a.first < b.first ||
				        (a.first == b.first && a.second < b.second));
			}
		};
    
	NXOpenGLCamera camera;
    
	// bool pluginsInitialized;
    
    // OpenGL settings
	std::vector<GltLight> lights;
	GltLightModel lightModel;
    
    // bool isOrthographicProjection;
    // GltOrtho orthographicProjection;
    // GltFrustum perspectiveProjection;
    // GltViewport viewport;
    
	std::map<uint, Nanorex::NXRGBColor> elementColorMap;
    NXOpenGLMaterial defaultAtomMaterial;
    NXOpenGLMaterial defaultBondMaterial;
	
	typedef set<OBAtom*> RenderedAtomsTableType;
	RenderedAtomsTableType renderedAtoms; // tracks rendered atoms
	
	typedef
		set<pair<OBAtom*,OBAtom*>, PairwiseLess<OBAtom*,OBAtom*> >
		RenderedBondsTableType;
	
	RenderedBondsTableType renderedBonds; // tracks rendered bonds
	
	NXSGOpenGLNode*
		createOpenGLSceneGraph(Nanorex::NXMoleculeSet *const molSetPtr);
	
	NXSGOpenGLNode*
		createOpenGLSceneGraph(OpenBabel::OBMol *const molPtr);
	
	NXSGOpenGLNode*
		createOpenGLSceneGraph(OpenBabel::OBMol *const molPtr,
		                       OpenBabel::OBAtom *const atomPtr,
		                       Vector const& zAxis);
	
#if 0 /// @todo Post-FNANO08
	/// @fixme r1.0.0 hacks
	// -- begin hacks
	/// @todo initialize these in reset()
	bool inDnaGroup, inDnaSegment, inDnaStrand;
	NXMoleculeSet *dnaSegmentMolSetPtr, *dnaStrandMolSetPtr;
	
	NXSGOpenGLNode*
		createOpenGLDnaSegmentSceneGraph(OpenBabel::OBMol *const molPtr);
	
	NXSGOpenGLNode*
		createOpenGLDnaStrandSceneGraph(OpenBabel::OBMol *const molPtr);

	// -- end hacks --
#endif
	
	
	NXSGOpenGLNode* getRotationNode(Vector const& zAxis,
	                                Vector const& newZAxis);
	
    // Implement inherited pure-virtual methods
	Nanorex::NXSGNode*
		createSceneGraph (Nanorex::NXMoleculeSet *const molSetPtr);
	
	bool isRendered(OBAtom *const atomPtr) const;
	bool isRendered(OBBond *const bondPtr) const;
	void markBondRendered(OBBond *const bondPtr);
	
	// NXSGNode* createSceneGraph (OpenBabel::OBMol *const molPtr);
    
    // QGLWidget methods to be overriden
	void initializeGL(void);
	void resizeGL(int width, int height);
	void paintGL(void);
    
	bool initializeElementColorMap(void);
	void initializeDefaultMaterials(void);
	void setupDefaultLights(void);
	void drawSkyBlueBackground(void);
	void drawCompass(void);
	
	// static methods
	BoundingBox GetBoundingBox(Nanorex::NXMoleculeSet *const molSetPtr);
	BoundingBox GetBoundingBox(OpenBabel::OBMol *const molPtr);
	
	friend class NXOpenGLRenderingEngineTest;
	friend class MMPOpenGLRendererTest;
};


inline
Nanorex::NXRenderingEngine*
NXOpenGLRenderingEngine::newInstance(QWidget *parent) const
{
	NXOpenGLRenderingEngine *openGLRenderingEngineInstance =
		new NXOpenGLRenderingEngine(parent);
	return
		static_cast<Nanorex::NXRenderingEngine*>(openGLRenderingEngineInstance);
}


inline
Nanorex::NXRendererPlugin*
NXOpenGLRenderingEngine::renderer_cast(QObject *plugin) const
{
	NXOpenGLRendererPlugin *rendererPlugin =
		qobject_cast<NXOpenGLRendererPlugin*>(plugin);
	return
		static_cast<Nanorex::NXRendererPlugin*>(rendererPlugin);
}

#if 0
inline void NXOpenGLRenderingEngine::setCurrentFrame(int frameIdx)
{
	if(frameIdx < (int)frames.size()) {
		currentFrameIndex = frameIdx;
		resetView();
	}
}
#endif

#endif // NX_OPENGLRENDERINGENGINE_H