From 8408ddd5521c9ea1b761641a3a153a85572ef0af Mon Sep 17 00:00:00 2001 From: dfighter1985 Date: Sun, 7 Jul 2013 21:46:45 +0200 Subject: [PATCH] Added ViewPortWidget. --HG-- branch : gsoc2013-dfighter --- .../plugins/material_editor/CMakeLists.txt | 1 + .../material_editor_window.cpp | 32 +++++ .../material_editor/material_editor_window.h | 3 + .../material_editor/nel3d_interface.cpp | 42 +++++- .../plugins/material_editor/nel3d_interface.h | 13 ++ .../material_editor/viewport_widget.cpp | 127 ++++++++++++++++++ .../plugins/material_editor/viewport_widget.h | 60 +++++++++ 7 files changed, 277 insertions(+), 1 deletion(-) create mode 100644 code/nel/tools/3d/object_viewer_qt/src/plugins/material_editor/viewport_widget.cpp create mode 100644 code/nel/tools/3d/object_viewer_qt/src/plugins/material_editor/viewport_widget.h diff --git a/code/nel/tools/3d/object_viewer_qt/src/plugins/material_editor/CMakeLists.txt b/code/nel/tools/3d/object_viewer_qt/src/plugins/material_editor/CMakeLists.txt index 36a31f449..86982e606 100644 --- a/code/nel/tools/3d/object_viewer_qt/src/plugins/material_editor/CMakeLists.txt +++ b/code/nel/tools/3d/object_viewer_qt/src/plugins/material_editor/CMakeLists.txt @@ -23,6 +23,7 @@ SET(OVQT_PLUGIN_MATERIAL_EDITOR_HDR shader_editor.h shader_widget.h prop_browser_ctrl.h + viewport_widget.h ) SET(OVQT_PLUGIN_MATERIAL_EDITOR_UIS diff --git a/code/nel/tools/3d/object_viewer_qt/src/plugins/material_editor/material_editor_window.cpp b/code/nel/tools/3d/object_viewer_qt/src/plugins/material_editor/material_editor_window.cpp index 6b5d5ab27..673ac1134 100644 --- a/code/nel/tools/3d/object_viewer_qt/src/plugins/material_editor/material_editor_window.cpp +++ b/code/nel/tools/3d/object_viewer_qt/src/plugins/material_editor/material_editor_window.cpp @@ -20,6 +20,7 @@ #include "shader_widget.h" #include "render_passes.h" #include "nel3d_interface.h" +#include "viewport_widget.h" #include "../core/icore.h" #include "../core/core_constants.h" @@ -42,6 +43,8 @@ namespace MaterialEditor m_ui.setupUi(this); nl3dIface = new CNel3DInterface(); + viewPort = new ViewPortWidget(); + viewPort->setNel3DInterface( nl3dIface ); shaderWidget = new ShaderWidget(); shaderWidget->setNel3DInterface( nl3dIface ); materialSplitter = new MaterialSplitter(); @@ -56,6 +59,8 @@ namespace MaterialEditor createDockWidgets(); setupConnections(); + setCentralWidget( viewPort ); + QTimer::singleShot( 1, this, SLOT( onStartup() ) ); } @@ -65,6 +70,8 @@ namespace MaterialEditor shaderWidget = NULL; delete passesWidget; passesWidget = NULL; + delete viewPort; + viewPort = NULL; delete nl3dIface; nl3dIface = NULL; } @@ -78,7 +85,19 @@ namespace MaterialEditor tr( "Shape files ( *.shape )" ) ); + if( fn.isEmpty() ) + return; + std::string fname = fn.toUtf8().data(); + bool ok = nl3dIface->loadShape( fname ); + if( !ok ) + { + QMessageBox::critical( + this, + tr( "Error loading shape file" ), + tr( "There was an error while loading the shape file." ) + ); + } } void MaterialEditorWindow::onNewMaterialClicked() @@ -154,6 +173,12 @@ namespace MaterialEditor { nl3dIface->loadShaders(); shaderWidget->load(); + viewPort->init(); + } + + void MaterialEditorWindow::onClearSceneClicked() + { + nl3dIface->clearScene(); } void MaterialEditorWindow::createMenus() @@ -181,6 +206,13 @@ namespace MaterialEditor mm->addAction( a ); } + mm = m->addMenu( tr( "Scene" ) ); + { + a = new QAction( tr( "Clear scene" ), NULL ); + connect( a, SIGNAL( triggered( bool ) ), this, SLOT( onClearSceneClicked() ) ); + mm->addAction( a ); + } + a = new QAction( tr( "Shaders" ), NULL ); connect( a, SIGNAL( triggered( bool ) ), this, SLOT( onShadersClicked() ) ); m->addAction( a ); diff --git a/code/nel/tools/3d/object_viewer_qt/src/plugins/material_editor/material_editor_window.h b/code/nel/tools/3d/object_viewer_qt/src/plugins/material_editor/material_editor_window.h index 91f7264e4..44ab19506 100644 --- a/code/nel/tools/3d/object_viewer_qt/src/plugins/material_editor/material_editor_window.h +++ b/code/nel/tools/3d/object_viewer_qt/src/plugins/material_editor/material_editor_window.h @@ -26,6 +26,7 @@ namespace MaterialEditor class RenderPassesWidget; class CNel3DInterface; class MaterialSplitter; + class ViewPortWidget; class MaterialEditorWindow: public QMainWindow { @@ -43,6 +44,7 @@ private Q_SLOTS: void onShadersClicked(); void onPassesClicked(); void onStartup(); + void onClearSceneClicked(); private: void createMenus(); @@ -54,6 +56,7 @@ private: ShaderWidget *shaderWidget; RenderPassesWidget *passesWidget; MaterialSplitter *materialSplitter; + ViewPortWidget *viewPort; Ui::MaterialEditorWindow m_ui; }; diff --git a/code/nel/tools/3d/object_viewer_qt/src/plugins/material_editor/nel3d_interface.cpp b/code/nel/tools/3d/object_viewer_qt/src/plugins/material_editor/nel3d_interface.cpp index b29ef7e2e..95d7d1f7a 100644 --- a/code/nel/tools/3d/object_viewer_qt/src/plugins/material_editor/nel3d_interface.cpp +++ b/code/nel/tools/3d/object_viewer_qt/src/plugins/material_editor/nel3d_interface.cpp @@ -20,6 +20,9 @@ #include "nel/3d/shader_program.h" #include "nel/3d/shader_loader.h" #include "nel/3d/shader_saver.h" +#include "nel/3d/driver_user.h" +#include "nel/3d/scene_user.h" +#include "nel/3d/u_camera.h" #include "nel/misc/i_xml.h" #include "nel/misc/o_xml.h" #include "nel/misc/file.h" @@ -196,13 +199,15 @@ namespace MaterialEditor { mat = new NL3D::CDynMaterial(); shaderManager = new NL3D::CShaderManager(); - + driver = NULL; + scene = NULL; } CNel3DInterface::~CNel3DInterface() { delete shaderManager; shaderManager = NULL; + killViewPort(); } bool CNel3DInterface::loadMaterial( const char *fname ) @@ -332,5 +337,40 @@ namespace MaterialEditor { NLMISC::CFile::deleteFile( "./shaders/" + name + ".nlshdr" ); } + + void CNel3DInterface::initViewPort( unsigned long wnd, unsigned long w, unsigned long h ) + { + driver = NL3D::UDriver::createDriver( 0, false, 0 ); + nlassert( driver != NULL ); + driver->setDisplay( (nlWindow)wnd, NL3D::UDriver::CMode( w, h, 32 ) ); + + scene = driver->createScene( true ); + + } + + void CNel3DInterface::killViewPort() + { + driver->deleteScene( scene ); + delete driver; + driver = NULL; + + } + + void CNel3DInterface::resizeViewPort( unsigned long w, unsigned long h ) + { + if( scene != NULL ) + { + scene->getCam().setPerspective( 90.0f, w / (float)h, 0.1f, 1000.0f ); + } + } + + bool CNel3DInterface::loadShape( const std::string &fileName ) + { + return true; + } + + void CNel3DInterface::clearScene() + { + } } diff --git a/code/nel/tools/3d/object_viewer_qt/src/plugins/material_editor/nel3d_interface.h b/code/nel/tools/3d/object_viewer_qt/src/plugins/material_editor/nel3d_interface.h index 1d38af3fe..04c7c0cb6 100644 --- a/code/nel/tools/3d/object_viewer_qt/src/plugins/material_editor/nel3d_interface.h +++ b/code/nel/tools/3d/object_viewer_qt/src/plugins/material_editor/nel3d_interface.h @@ -26,6 +26,8 @@ namespace NL3D class CDynMaterial; struct SRenderPass; class CShaderManager; + class UDriver; + class UScene; } namespace MaterialEditor @@ -141,9 +143,20 @@ namespace MaterialEditor void saveShader( const std::string &name ); void deleteShader( const std::string &name ); + void initViewPort( unsigned long wnd, unsigned long w, unsigned long h ); + void killViewPort(); + void resizeViewPort( unsigned long w, unsigned long h ); + NL3D::UDriver* getDriver(){ return driver; } + + bool loadShape( const std::string &fileName ); + void clearScene(); + private: NL3D::CDynMaterial *mat; NL3D::CShaderManager *shaderManager; + NL3D::UDriver *driver; + NL3D::UScene *scene; + }; } diff --git a/code/nel/tools/3d/object_viewer_qt/src/plugins/material_editor/viewport_widget.cpp b/code/nel/tools/3d/object_viewer_qt/src/plugins/material_editor/viewport_widget.cpp new file mode 100644 index 000000000..7e4211131 --- /dev/null +++ b/code/nel/tools/3d/object_viewer_qt/src/plugins/material_editor/viewport_widget.cpp @@ -0,0 +1,127 @@ +// Object Viewer Qt Material Editor plugin +// Copyright (C) 2010 Winch Gate Property Limited +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU Affero General Public License as +// published by the Free Software Foundation, either version 3 of the +// License, or (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU Affero General Public License for more details. +// +// You should have received a copy of the GNU Affero General Public License +// along with this program. If not, see . + + +#include "viewport_widget.h" +#include +#include "nel3d_interface.h" +#include "nel/3d/driver.h" +#include "nel/3d/driver_user.h" + +#ifdef NL_OS_WINDOWS +#include +#endif + + +namespace MaterialEditor +{ + ViewPortWidget::ViewPortWidget( QWidget *parent ) : + QWidget( parent ) + { + nl3dIface = NULL; + setAttribute( Qt::WA_PaintOnScreen, true ); + } + + ViewPortWidget::~ViewPortWidget() + { + } + + void ViewPortWidget::init() + { + nl3dIface->initViewPort( (unsigned long)winId(), width(), height() ); + + } + + void ViewPortWidget::resizeEvent( QResizeEvent *evnt ) + { + uint32 w = evnt->size().width(); + uint32 h = evnt->size().height(); + + nl3dIface->resizeViewPort( w, h ); + + QWidget::resizeEvent( evnt ); + } + + +#if defined( NL_OS_WINDOWS ) + +typedef bool ( *winProc )( NL3D::IDriver *driver, HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam ); + +bool ViewPortWidget::winEvent( MSG *message, long *result ) +{ + NL3D::UDriver *udriver = nl3dIface->getDriver(); + if( ( udriver != NULL ) && udriver->isActive() ) + { + NL3D::IDriver *driver = dynamic_cast< NL3D::CDriverUser* >( udriver )->getDriver(); + if( driver != NULL ) + { + winProc proc = (winProc)driver->getWindowProc(); + + return proc( driver, message->hwnd, message->message, message->wParam, message->lParam ); + } + } + + return false; +} + +#elif defined( NL_OS_MAC ) + +typedef bool ( *cocoaProc )( NL3D::IDriver *, const void *e ); + +bool ViewPortWidget::macEvent( EventHandlerCallRef caller, EventRef event ) +{ + if(caller) + nlerror("You are using QtCarbon! Only QtCocoa supported, please upgrade Qt"); + + NL3D::UDriver *udriver = nl3dIface->getDriver(); + if( ( udriver != NULL ) && udriver->isActive() ) + { + NL3D::IDriver *driver = dynamic_cast< NL3D::CDriverUser* >( udriver )->getDriver(); + if( driver != NULL ) + { + cocoaProc proc = ( cocoaProc )driver->getWindowProc(); + proc( driver, event ); + } + } + + return false; +} + +#elif defined( NL_OS_UNIX ) + +typedef bool ( *x11Proc )( NL3D::IDriver *drv, XEvent *e ); + +bool ViewPortWidget::x11Event( XEvent *event ) +{ + + NL3D::UDriver *udriver = nl3dIface->getDriver(); + if( ( udriver != NULL ) && udriver->isActive() ) + { + NL3D::IDriver *driver = dynamic_cast< NL3D::CDriverUser* >( udriver )->getDriver(); + if( driver != NULL ) + { + x11Proc proc = ( x11Proc )driver->getWindowProc(); + proc( driver, event ); + } + } + + return false; +} +#endif + +} + + diff --git a/code/nel/tools/3d/object_viewer_qt/src/plugins/material_editor/viewport_widget.h b/code/nel/tools/3d/object_viewer_qt/src/plugins/material_editor/viewport_widget.h new file mode 100644 index 000000000..154052bae --- /dev/null +++ b/code/nel/tools/3d/object_viewer_qt/src/plugins/material_editor/viewport_widget.h @@ -0,0 +1,60 @@ +// Object Viewer Qt Material Editor plugin +// Copyright (C) 2010 Winch Gate Property Limited +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU Affero General Public License as +// published by the Free Software Foundation, either version 3 of the +// License, or (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU Affero General Public License for more details. +// +// You should have received a copy of the GNU Affero General Public License +// along with this program. If not, see . + + +#ifndef VIEWPORT_WIDGET_H +#define VIEWPORT_WIDGET_H + +#include +#include "nel/misc/types_nl.h" +#include "nel/misc/event_emitter.h" + +namespace MaterialEditor +{ + class CNel3DInterface; + + class ViewPortWidget : public QWidget + { + Q_OBJECT + public: + ViewPortWidget( QWidget *parent = NULL ); + ~ViewPortWidget(); + void setNel3DInterface( CNel3DInterface *iface ){ nl3dIface = iface; } + void init(); + + void resizeEvent( QResizeEvent *evnt ); + + QPaintEngine* paintEngine() const{ return NULL; } + + protected: + +#if defined ( NL_OS_WINDOWS ) + bool winEvent( MSG *message, long *result ); +#elif defined( NL_OS_MAC ) + bool macEvent( EventHandlerCallRef caller, EventRef event ) ; +#elif defined( NL_OS_UNIX ) + bool x11Event( XEvent *event ); +#endif + + private: + CNel3DInterface *nl3dIface; + }; +} + + +#endif + +