Commit 0f454dac by Sebastian Eichelbaum

[FIX] finally runs on android. Only WGECamera not working.

parent 50be3aeb
......@@ -24,6 +24,8 @@
#include <iostream>
#include "core/common/WAssert.h"
#include "WGEGraphicsWindow.h"
#include "exceptions/WGEInitFailed.h"
......@@ -38,7 +40,9 @@ WGEGraphicsWindow::WGEGraphicsWindow( osg::ref_ptr<osg::Referenced>
int width,
int height )
{
#ifdef WGEMODE_MULTITHREADED
// on android, the graphics window is not needed
#ifndef __ANDROID__
#ifdef WGEMODE_MULTITHREADED
// initialize context
m_WindowData = wdata;
try
......@@ -50,9 +54,10 @@ WGEGraphicsWindow::WGEGraphicsWindow( osg::ref_ptr<osg::Referenced>
// use our own exceptions
throw WGEInitFailed( "Initialization of OpenGL graphics context failed." );
}
#else
#else
m_GraphicsWindow = osg::ref_ptr<osgViewer::GraphicsWindow>(
static_cast<osgViewer::GraphicsWindow*>( new osgViewer::GraphicsWindowEmbedded( x, y, width, height ) ) );
#endif
#endif
}
......@@ -63,6 +68,9 @@ WGEGraphicsWindow::~WGEGraphicsWindow()
osg::ref_ptr<osgViewer::GraphicsWindow> WGEGraphicsWindow::getGraphicsWindow()
{
#ifdef __ANDROID__
WAssert( m_GraphicsWindow, "On Android, there is no GraphicsWindow." );
#endif
return m_GraphicsWindow;
}
......@@ -110,49 +118,3 @@ void WGEGraphicsWindow::createContext( int x, int y, int width, int height )
}
#endif
void WGEGraphicsWindow::resize( int width, int height )
{
m_GraphicsWindow->getEventQueue()->windowResize( 0, 0, width, height );
m_GraphicsWindow->resized( 0, 0, width, height );
}
void WGEGraphicsWindow::close()
{
m_GraphicsWindow->getEventQueue()->closeWindow();
}
void WGEGraphicsWindow::keyEvent( KeyEvents eventType, int key )
{
switch( eventType )
{
case KEYPRESS:
m_GraphicsWindow->getEventQueue()->keyPress( static_cast<osgGA::GUIEventAdapter::KeySymbol>( key ) );
break;
case KEYRELEASE:
m_GraphicsWindow->getEventQueue()->keyRelease( static_cast<osgGA::GUIEventAdapter::KeySymbol>( key ) );
break;
}
}
void WGEGraphicsWindow::mouseEvent( MouseEvents eventType, int x, int y, int button )
{
switch( eventType )
{
case MOUSEPRESS:
m_GraphicsWindow->getEventQueue()->mouseButtonPress( x, y, button );
break;
case MOUSERELEASE:
m_GraphicsWindow->getEventQueue()->mouseButtonRelease( x, y, button );
break;
case MOUSEDOUBLECLICK:
m_GraphicsWindow->getEventQueue()->mouseDoubleButtonPress( x, y, button );
break;
case MOUSEMOVE:
m_GraphicsWindow->getEventQueue()->mouseMotion( x, y );
break;
case MOUSESCROLL:
m_GraphicsWindow->getEventQueue()->mouseScroll2D( x, y );
break;
}
}
......@@ -61,54 +61,6 @@ public:
*/
osg::ref_ptr<osgViewer::GraphicsWindow> getGraphicsWindow();
/**
* Event types for the keyEvent() handler.
*/
enum KeyEvents
{
KEYPRESS, KEYRELEASE
};
/**
* Mouse event types for the mouseEvent() handler.
*/
enum MouseEvents
{
MOUSEPRESS, MOUSERELEASE, MOUSEDOUBLECLICK, MOUSEMOVE, MOUSESCROLL
};
/**
* Updates size information.
*
* \param width new width.
* \param height new height.
*/
virtual void resize( int width, int height );
/**
* Initiates a close event for this viewer. It destroys the graphics context and invalidates the viewer.
* This should be called whenever a QT Widget closes to also free its OSG Viewer resources.
*/
virtual void close();
/**
* Handles key events (if forwarded to this Viewer instance).
*
* \param key the key code.
* \param eventType the type of event.
*/
virtual void keyEvent( KeyEvents eventType, int key );
/**
* Handles mouse events forwarded from widget.
*
* \param eventType the event type.
* \param x x coordinate of event.
* \param y y coordinate of event.
* \param button mouse button.
*/
virtual void mouseEvent( MouseEvents eventType, int x, int y, int button );
protected:
/**
* OpenSceneGraph render window.
......
......@@ -25,17 +25,9 @@
#include <string>
#include <iostream>
#include <osg/ShapeDrawable>
#include <osg/Geode>
#include <osg/Camera>
#include <osgGA/FlightManipulator>
#include <osgGA/DriveManipulator>
#include <osgGA/UFOManipulator>
#include <osgGA/KeySwitchMatrixManipulator>
#include <osgGA/StateSetManipulator>
#include <osgGA/AnimationPathManipulator>
#include <osgGA/TerrainManipulator>
#include <osgViewer/ViewerEventHandlers>
#include <osgViewer/View>
......@@ -48,6 +40,7 @@
#include "WGEZoomTrackballManipulator.h"
#include "WPickHandler.h"
#include "../common/WConditionOneShot.h"
#include "../common/WLogger.h"
#include "../common/WThreadedRunner.h"
......@@ -71,30 +64,46 @@ WGEViewer::WGEViewer( std::string name, osg::ref_ptr<osg::Referenced> wdata, int
m_View = osg::ref_ptr<osgViewer::Viewer>( new osgViewer::Viewer );
#endif
#ifdef __ANDROID__
// create a viewer for this new window geometry.
m_View->setUpViewerAsEmbeddedInWindow( x, y, width, height );
m_View->setThreadingModel( osgViewer::ViewerBase::SingleThreaded );
m_View->realize();
#endif
#ifndef __ANDROID__
// FIXME: our camera does not work on android.
m_View->setCamera( new WGECamera( width, height, projectionMode ) );
#endif
m_queryCallback = new QueryCallback( m_View->getCamera(), m_rendered );
m_View->getCamera()->setInitialDrawCallback( m_queryCallback );
m_View->getCamera()->setFinalDrawCallback( m_screenCapture );
// add the stats handler
m_View->addEventHandler( new osgViewer::StatsHandler );
#ifdef WGEMODE_MULTITHREADED
#ifndef __ANDROID__
#ifdef WGEMODE_MULTITHREADED
m_View->getCamera()->setGraphicsContext( m_GraphicsContext.get() );
#else
#else
m_View->getCamera()->setGraphicsContext( m_GraphicsWindow.get() );
#endif
#endif
// force headlight
m_View->setLightingMode( osg::View::HEADLIGHT ); // this is the default anyway
switch( projectionMode )
{
case( WGECamera::ORTHOGRAPHIC ):
m_pickHandler = new WPickHandler( name );
m_View->addEventHandler( m_pickHandler );
// this ensures that the main view has the WGEZoomTrackballManipulator set. Others use the default OSG.
if( name != std::string( "Main View" ) )
break;
case( WGECamera::PERSPECTIVE ):
// camera manipulator
m_View->setCameraManipulator( new WGEZoomTrackballManipulator() );
m_View->setLightingMode( osg::View::HEADLIGHT ); // this is the default anyway
break;
case( WGECamera::TWO_D ):
// no manipulators nor gui handlers
......@@ -106,9 +115,6 @@ WGEViewer::WGEViewer( std::string name, osg::ref_ptr<osg::Referenced> wdata, int
default:
throw WGEInitFailed( std::string( "Unknown projection mode" ) );
}
// add the stats handler
m_View->addEventHandler( new osgViewer::StatsHandler );
}
catch( ... )
{
......@@ -179,27 +185,6 @@ void WGEViewer::paint()
#endif
}
void WGEViewer::resize( int width, int height )
{
m_View->getEventQueue()->windowResize( 0, 0, width, height );
WGEGraphicsWindow::resize( width, height );
// also update the camera
m_View->getCamera()->setViewport( 0, 0, width, height );
WGECamera* camera = dynamic_cast< WGECamera* >( m_View->getCamera() );
if( camera )
{
camera->resize();
}
}
void WGEViewer::close()
{
// forward close event
WGEGraphicsWindow::close();
}
std::string WGEViewer::getName() const
{
return m_name;
......@@ -294,3 +279,56 @@ bool WGEViewer::isAnimationMode() const
return m_inAnimationMode;
}
void WGEViewer::resize( int width, int height )
{
m_View->getEventQueue()->windowResize( 0, 0, width, height );
// also update the camera
m_View->getCamera()->setViewport( 0, 0, width, height );
WGECamera* camera = dynamic_cast< WGECamera* >( m_View->getCamera() );
if( camera )
{
camera->resize();
}
}
void WGEViewer::close()
{
m_View->getEventQueue()->closeWindow();
}
void WGEViewer::keyEvent( KeyEvents eventType, int key )
{
switch( eventType )
{
case KEYPRESS:
m_View->getEventQueue()->keyPress( static_cast<osgGA::GUIEventAdapter::KeySymbol>( key ) );
break;
case KEYRELEASE:
m_View->getEventQueue()->keyRelease( static_cast<osgGA::GUIEventAdapter::KeySymbol>( key ) );
break;
}
}
void WGEViewer::mouseEvent( MouseEvents eventType, int x, int y, int button )
{
switch( eventType )
{
case MOUSEPRESS:
m_View->getEventQueue()->mouseButtonPress( x, y, button );
break;
case MOUSERELEASE:
m_View->getEventQueue()->mouseButtonRelease( x, y, button );
break;
case MOUSEDOUBLECLICK:
m_View->getEventQueue()->mouseDoubleButtonPress( x, y, button );
break;
case MOUSEMOVE:
m_View->getEventQueue()->mouseMotion( x, y );
break;
case MOUSESCROLL:
m_View->getEventQueue()->mouseScroll2D( x, y );
break;
}
}
......@@ -237,6 +237,40 @@ public:
*/
bool isAnimationMode() const;
/**
* Event types for the keyEvent() handler.
*/
enum KeyEvents
{
KEYPRESS, KEYRELEASE
};
/**
* Mouse event types for the mouseEvent() handler.
*/
enum MouseEvents
{
MOUSEPRESS, MOUSERELEASE, MOUSEDOUBLECLICK, MOUSEMOVE, MOUSESCROLL
};
/**
* Handles key events (if forwarded to this Viewer instance).
*
* \param key the key code.
* \param eventType the type of event.
*/
virtual void keyEvent( KeyEvents eventType, int key );
/**
* Handles mouse events forwarded from widget.
*
* \param eventType the event type.
* \param x x coordinate of event.
* \param y y coordinate of event.
* \param button mouse button.
*/
virtual void mouseEvent( MouseEvents eventType, int x, int y, int button );
protected:
/**
* The OpenSceneGraph view used in this (Composite)Viewer.
......
......@@ -28,7 +28,7 @@
#if ( defined( WGEMODE_SINGLETHREADED ) || defined( WGEMODE_MULTITHREADED ))
// should we do something here? No! We assume the user defined it in cmake
#else
#ifdef __APPLE__
#if defined( __APPLE__ ) || ( defined( __ANDROID__ ) )
#define WGEMODE_SINGLETHREADED
#else
#define WGEMODE_MULTITHREADED
......
......@@ -86,7 +86,7 @@ USE_DOTOSGWRAPPER_LIBRARY( osg );
// USE_DOTOSGWRAPPER_LIBRARY( osgFX );
// USE_DOTOSGWRAPPER_LIBRARY( osgParticle );
// USE_DOTOSGWRAPPER_LIBRARY( osgTerrain );
// USE_DOTOSGWRAPPER_LIBRARY( osgText );
USE_DOTOSGWRAPPER_LIBRARY( osgText );
// USE_DOTOSGWRAPPER_LIBRARY( osgVolume );
// NOTE: although we need the osgViewer, uncommenting the below line causes an segfault (at least on Android).
// USE_DOTOSGWRAPPER_LIBRARY( osgViewer );
......
......@@ -81,7 +81,7 @@ void WMAndroidExample::connectors()
void WMAndroidExample::properties()
{
m_propCondition = boost::shared_ptr< WCondition >( new WCondition() );
m_aDouble = m_properties->addProperty( "Shape radii", "Shape radii.", 20.0, m_propCondition );
m_aDouble = m_properties->addProperty( "Shape radii", "Shape radii.", 20.0, m_propCondition );
WModule::properties();
}
......@@ -90,39 +90,85 @@ void WMAndroidExample::requirements()
m_requirements.push_back( new WGERequirement() );
}
static const char vertexShader[] =
"varying vec3 v_normal; \n"
"varying vec3 v_vertex; \n"
"varying vec3 v_light; \n"
"const vec3 lightPos =vec3(0.0, 100.0, 0.0); \n"
"void main() { \n"
" gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex; \n"
" v_vertex = ( gl_ModelViewMatrix * gl_Vertex).xyz; \n"
" v_normal = normalize(gl_NormalMatrix * -gl_Normal); \n"
" v_light = normalize(gl_ModelViewMatrix*vec4(lightPos, 0.0)).xyz ;\n"
"} \n";
static const char fragmentShader[] =
"precision mediump float; \n"
"varying mediump vec3 v_normal; \n"
"varying mediump vec3 v_vertex; \n"
"varying mediump vec3 v_light; \n"
"const vec4 cessnaColor =vec4(1.0, 0.5, 0.0, 1.0); \n"
"const vec4 lightAmbient =vec4(0.1, 0.1, 0.1, 1.0); \n"
"const vec4 lightDiffuse =vec4(0.4, 0.4, 0.4, 1.0); \n"
"const vec4 lightSpecular =vec4(0.8, 0.8, 0.8, 1.0); \n"
"void DirectionalLight(in vec3 normal, \n"
" in vec3 ecPos, \n"
" inout vec4 ambient, \n"
" inout vec4 diffuse, \n"
" inout vec4 specular) \n"
"{ \n"
" float nDotVP; \n"
" vec3 L = v_light; \n"
" nDotVP = max(0.0, dot(normal, L)); \n"
" \n"
" if (nDotVP > 0.0) { \n"
" vec3 E = normalize(-ecPos); \n"
" vec3 R = normalize(reflect( L, normal )); \n"
" specular = pow(max(dot(R, E), 0.0), 16.0) * lightSpecular; \n"
" } \n"
" ambient = lightAmbient; \n"
" diffuse = lightDiffuse * nDotVP; \n"
"} \n"
"void main() { \n"
" vec4 ambiCol = vec4(0.0); \n"
" vec4 diffCol = vec4(0.0); \n"
" vec4 specCol = vec4(0.0); \n"
" vec4 ecPos = vec4( v_vertex, 1.0 ); \n"
" DirectionalLight( v_normal, ecPos.xyz, ambiCol, diffCol, specCol); \n"
" vec4 color = cessnaColor * (ambiCol + diffCol + specCol); \n"
" gl_FragColor = color; \n"
"} \n";
void WMAndroidExample::moduleMain()
{
// debugLog() << "Entering moduleMain()";
debugLog() << "Entering moduleMain()";
m_moduleState.setResetable( true, true );
m_moduleState.add( m_propCondition );
// Signal ready state. Now your module can be connected by the container, which owns the module.
//ready();
// debugLog() << "Module is now ready.";
ready();
debugLog() << "Module is now ready.";
m_rootNode = new WGEManagedGroupNode( m_active );
WKernel::getRunningKernel()->getGraphicsEngine()->getScene()->insert( m_rootNode );
// Normally, you will have a loop which runs as long as the module should not shutdown. In this loop you can react on changing data on input
// connectors or on changed in your properties.
//debugLog() << "Entering main loop";
// while( !m_shutdownFlag() )
// {
//debugLog() << "Waiting ...";
//m_moduleState.wait();
debugLog() << "Entering main loop";
while( !m_shutdownFlag() )
{
// woke up since the module is requested to finish
//if( m_shutdownFlag() )
//{
// break;
// }
if( m_shutdownFlag() )
{
break;
}
//debugLog() << "Creating new OSG node";
debugLog() << "Creating new OSG node";
// You should grab your values at the beginning of such calculation blocks, since the property might change at any time!
int rows = 5;
double radii = 5.0;
int rows = 10;
double radii = 10.0;
osg::ref_ptr< osg::Geode > newGeode = new osg::Geode();
for( int32_t i = 0; i < rows; ++i )
......@@ -139,12 +185,24 @@ void WMAndroidExample::moduleMain()
new osg::ShapeDrawable( new osg::Capsule( osg::Vec3( 225, 128, i * 15 ), radii, radii ) ) );
}
//m_rootNode->remove( m_geode );
// for GLES, we need a shader
osg::Shader* vshader = new osg::Shader( osg::Shader::VERTEX, vertexShader );
osg::Shader* fshader = new osg::Shader( osg::Shader::FRAGMENT, fragmentShader );
osg::Program* prog = new osg::Program;
prog->addShader( vshader );
prog->addShader( fshader );
newGeode->getOrCreateStateSet()->setAttribute( prog );
// remove old node
m_rootNode->remove( m_geode );
m_geode = newGeode;
std::cout << "Teste cndskf vlfndslkgv ldsnfdsngfkldsngflkodshfkldsnfglkds" << std::endl;
// And insert the new node
// m_rootNode->insert( m_geode );
m_rootNode->insert( newGeode );
// }
// WKernel::getRunningKernel()->getGraphicsEngine()->getScene()->remove( m_rootNode );
m_rootNode->insert( m_geode );
// done. wait for events.
debugLog() << "Waiting ...";
m_moduleState.wait();
}
WKernel::getRunningKernel()->getGraphicsEngine()->getScene()->remove( m_rootNode );
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment