#ifndef QGLRENDERER_H#define QGLRENDERER_H#include <QObject>#include <QMap>#include "glheaders.h"#include "qglcamera.h"class QGLObject;class QGLRenderer : public QObject{ Q_OBJECTpublic: QGLRenderer(); void RegisterObject(QGLObject *obj); void UnregisterObject(QGLObject *obj); void Render(); bool HasObjects(); static QGLCamera *camera;public slots: void ToRender(QGLObject *obj);private: QMap<QGLObject*,bool> objects;};#endif // QGLRENDERER_H
#include "qglrenderer.h"#include "qglobject.h"#include <QList>QGLCamera* QGLRenderer::camera;QGLRenderer::QGLRenderer(){ objects.clear();}void QGLRenderer::RegisterObject(QGLObject *obj){ if ( objects.contains(obj) ) return; objects.insert(obj,false); connect(obj,SIGNAL(ToRender(QGLObject*)),this,SLOT(ToRender(QGLObject*)));}void QGLRenderer::UnregisterObject(QGLObject *obj){ if ( objects.contains(obj) ) { objects.remove(obj); disconnect(obj,SIGNAL(ToRender(QGLObject*)),this,SLOT(ToRender(QGLObject*))); }}void QGLRenderer::ToRender(QGLObject *obj){ RegisterObject(obj); objects[obj] = true;}void QGLRenderer::Render(){ QGLObject *obj; QMap<GLfloat,QGLObject* > map; QList<QGLObject* > r_map; QMap<QGLObject*,bool>::const_iterator i = objects.constBegin(); while ( i != objects.constEnd() ) { if ( i.value() == true ) { obj = i.key(); map.insertMulti(obj->DistanceTo(),obj); } i++; } QMap<GLfloat,QGLObject* >::const_iterator j = map.constBegin(); while ( j != map.constEnd() ) { obj = j.value(); r_map.append(obj); j++; objects[obj] = false; } for ( int k=r_map.size()-1;k>=0;k-- ) { r_map.at(k)->Render(); }}bool QGLRenderer::HasObjects(){ if ( objects.isEmpty() ) return false; return true;}
void QGLMesh::Draw(){ glNormalPointer(GL_FLOAT,0,normales); glVertexPointer(3,GL_FLOAT,0,vertexes); glDrawArrays(GL_TRIANGLES,0,numVertex);}
C++ (Qt) QMap<GLfloat,QGLObject* >::const_iterator j = map.constBegin(); while ( j != map.constEnd() ) { obj = j.value(); r_map.append(obj); j++; objects[obj] = false; } for ( int k=r_map.size()-1;k>=0;k-- ) { r_map.at(k)->Render(); }