![]() |
GLC_lib | |||||||
| OpenGL Library Class | ||||||||
From example02 (Point of view handling) replace the circle by a cylinder GLC_Cylinder and add a light GLC_Light to the scene.
Download Source and binaries of this example.
Only the difference compared to the example02 is developed.
#ifndef GLWIDGET_H_ #define GLWIDGET_H_ #include <QGLWidget> //////////////////////////// GLC specific/////////////////////////////////////// // The factory #include <GLC_Factory> // Light #include <GLC_Light> // The Viewport with a default camera #include <GLC_Viewport> // The Mover controller is used to change the point of view #include <GLC_MoverController> //////////////////////////End GLC specific///////////////////////////////////// class GLWidget : public QGLWidget { public: GLWidget(QWidget *p_parent); ~GLWidget(); private: void initializeGL(); void paintGL(); void resizeGL(int width, int height); //Mouse events void mousePressEvent(QMouseEvent * e); void mouseMoveEvent(QMouseEvent * e); void mouseReleaseEvent(QMouseEvent * e); private: //////////////////////////// GLC specific/////////////////////////////////////// GLC_Factory* m_pFactory; GLC_3DViewInstance m_Cylinder; GLC_Light m_light; GLC_Viewport m_GlView; GLC_MoverController m_MoverController; //////////////////////////End GLC specific///////////////////////////////////// }; #endif /*GLWIDGET_H_*/
GLC_Cylinder : The cylinder to display.
GLC_Light : The light of the scene.
#include <QtDebug> #include "glwidget.h" // GLC_Lib Exception #include <GLC_Exception> // For VSYNC problem under Mac OS X #if defined(Q_OS_MAC) #include <OpenGL.h> #endif GLWidget::GLWidget(QWidget *p_parent) : QGLWidget(p_parent) , m_pFactory(GLC_Factory::instance(this->context())) , m_Cylinder(m_pFactory->createCylinder(1.0, 2.0)) // Cylinder definition. , m_light() , m_GlView(this) , m_MoverController() { // Set cylinder material QColor matBlue; matBlue.setRgbF(0.5, 0.8, 1.0, 1.0); m_Cylinder.geomAt(0)->addMaterial(new GLC_Material(matBlue)); // Set up mover controller QColor repColor; repColor.setRgbF(1.0, 0.11372, 0.11372, 0.0); m_MoverController= m_pFactory->createDefaultMoverController(repColor, &m_GlView); // Set iso view m_GlView.cameraHandle()->setIsoView(); } GLWidget::~GLWidget() { delete m_pFactory; } void GLWidget::initializeGL() { // For VSYNC problem under Mac OS X #if defined(Q_OS_MAC) const GLint swapInterval = 1; CGLSetParameter(CGLGetCurrentContext(), kCGLCPSwapInterval, &swapInterval); #endif // OpenGL initialisation m_GlView.initGl(); // Reframe to the cylinder bounding Box m_GlView.reframe(m_Cylinder.boundingBox()); // Set the opengl clipping plane m_GlView.setDistMinAndMax(m_Cylinder.boundingBox()); } void GLWidget::paintGL() { // Clear screen glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // Load identity matrix glLoadIdentity(); //////////////////////////// GLC specific/////////////////////////////////////// try { // define the light m_light.enable(); m_light.glExecute(); // define view matrix m_GlView.glExecuteCam(); // Display the cylinder m_Cylinder.glExecute(); // Display UI Info (orbit circle) m_MoverController.drawActiveMoverRep(); } catch (GLC_Exception &e) { qDebug() << e.what(); } //////////////////////////End GLC specific///////////////////////////////////// } void GLWidget::resizeGL(int width, int height) { //////////////////////////// GLC specific/////////////////////////////////////// m_GlView.setWinGLSize(width, height); // Compute window aspect ratio //////////////////////////End GLC specific///////////////////////////////////// } void GLWidget::mousePressEvent(QMouseEvent *e) { if (m_MoverController.hasActiveMover()) return; switch (e->button()) { case (Qt::RightButton): m_MoverController.setActiveMover(GLC_MoverController::TrackBall, e->x(), e->y()); updateGL(); break; case (Qt::LeftButton): m_MoverController.setActiveMover(GLC_MoverController::Pan, e->x(), e->y()); updateGL(); break; case (Qt::MidButton): m_MoverController.setActiveMover(GLC_MoverController::Zoom, e->x(), e->y()); updateGL(); break; default: break; } } void GLWidget::mouseMoveEvent(QMouseEvent * e) { if (not m_MoverController.hasActiveMover()) return; m_MoverController.move(e->x(), e->y()); // Update opengl clipping plane m_GlView.setDistMinAndMax(m_Cylinder.boundingBox()); updateGL(); } void GLWidget::mouseReleaseEvent(QMouseEvent*) { if (not m_MoverController.hasActiveMover()) return; m_MoverController.setNoMover(); updateGL(); }
Initialization :
Body :
| ©2005-2010 Laurent Ribon |