From 12158f98d7ddb820a71f176872fa679bce81b35f Mon Sep 17 00:00:00 2001 From: Timo Lotterbach Date: Wed, 28 Sep 2011 11:32:33 +0200 Subject: [PATCH] MockNavi: added command line arguments for surface layer, width and heigth; refactored OpenGLES2App, improved rendering of exactly x fps; --- .../EGLX11MockNavigation/include/MockNavi.h | 2 +- .../EGLX11MockNavigation/include/OpenGLES2App.h | 16 +++- .../EGLX11MockNavigation/src/MockNavi.cpp | 6 +- .../EGLX11MockNavigation/src/OpenGLES2App.cpp | 98 +++++++++++----------- .../EGLX11MockNavigation/src/main.cpp | 40 ++++++--- 5 files changed, 95 insertions(+), 67 deletions(-) diff --git a/LayerManagerExamples/EGLX11MockNavigation/include/MockNavi.h b/LayerManagerExamples/EGLX11MockNavigation/include/MockNavi.h index 10a3ac5..30f9324 100644 --- a/LayerManagerExamples/EGLX11MockNavigation/include/MockNavi.h +++ b/LayerManagerExamples/EGLX11MockNavigation/include/MockNavi.h @@ -32,7 +32,7 @@ class MockNaviHouse; class MockNavi : public OpenGLES2App { public: - MockNavi(float fps, float animationSpeed, const int houseCount); + MockNavi(float fps, float animationSpeed, SurfaceConfiguration* config); virtual void update(int currentTimeInMs, int lastFrameTime); virtual void render(); diff --git a/LayerManagerExamples/EGLX11MockNavigation/include/OpenGLES2App.h b/LayerManagerExamples/EGLX11MockNavigation/include/OpenGLES2App.h index d1feb8e..41fda28 100644 --- a/LayerManagerExamples/EGLX11MockNavigation/include/OpenGLES2App.h +++ b/LayerManagerExamples/EGLX11MockNavigation/include/OpenGLES2App.h @@ -25,10 +25,18 @@ #include "X11/Xutil.h" #include +struct SurfaceConfiguration +{ + unsigned int layerId; + unsigned int surfaceId; + unsigned int surfaceWidth; + unsigned int surfaceHeight; +}; + class OpenGLES2App { public: - OpenGLES2App(float fps, float animationSpeed); + OpenGLES2App(float fps, float animationSpeed, SurfaceConfiguration* config); virtual ~OpenGLES2App(); void mainloop(); @@ -39,12 +47,14 @@ protected: void swapBuffers(); private: - bool createX11Context(int width, int height); + bool createX11Context(SurfaceConfiguration* config); void destroyX11Context(); - bool createEGLContext(int width, int height); + bool createEGLContext(); void destroyEglContext(); + bool setupLayerMangement(SurfaceConfiguration* config); + unsigned int GetTickCount(); protected: diff --git a/LayerManagerExamples/EGLX11MockNavigation/src/MockNavi.cpp b/LayerManagerExamples/EGLX11MockNavigation/src/MockNavi.cpp index 542e78a..ee5f141 100644 --- a/LayerManagerExamples/EGLX11MockNavigation/src/MockNavi.cpp +++ b/LayerManagerExamples/EGLX11MockNavigation/src/MockNavi.cpp @@ -29,10 +29,10 @@ #define CITY_GRID_SIZE 1.0f -MockNavi::MockNavi(float fps, float animationSpeed, const int houseCount) -: OpenGLES2App(fps, animationSpeed) +MockNavi::MockNavi(float fps, float animationSpeed, SurfaceConfiguration* config) +: OpenGLES2App(fps, animationSpeed, config) , m_camera(vec3f(-1.5 * CITY_GRID_SIZE, -0.1, 0.0), vec3f(0.0, 0.0, 0.0)) -, m_houseCount(houseCount) +, m_houseCount(15) { generateCity(); } diff --git a/LayerManagerExamples/EGLX11MockNavigation/src/OpenGLES2App.cpp b/LayerManagerExamples/EGLX11MockNavigation/src/OpenGLES2App.cpp index e435298..b6656c3 100644 --- a/LayerManagerExamples/EGLX11MockNavigation/src/OpenGLES2App.cpp +++ b/LayerManagerExamples/EGLX11MockNavigation/src/OpenGLES2App.cpp @@ -28,34 +28,28 @@ using std::endl; #include #include -// Max width and height of the window -#define SURFACE_WIDTH 800 -#define SURFACE_HEIGHT 480 +#define RUNTIME_IN_MS() (GetTickCount() - startTimeInMS) +// Max width and height of the window #define LAYER_WIDTH 800 #define LAYER_HEIGHT 480 -OpenGLES2App::OpenGLES2App(float fps, float animationSpeed) +OpenGLES2App::OpenGLES2App(float fps, float animationSpeed, SurfaceConfiguration* config) : m_framesPerSecond(fps) , m_animationSpeed(animationSpeed) , m_timerIntervalInMs(1000.0 / m_framesPerSecond) { ilm_init(); - createX11Context(SURFACE_WIDTH, SURFACE_HEIGHT); - createEGLContext(SURFACE_WIDTH, SURFACE_HEIGHT); + createX11Context(config); + createEGLContext(); + setupLayerMangement(config); glClearColor(0.2f, 0.2f, 0.5f, 1.0f); - //glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); - //glEnable(GL_BLEND); glDisable(GL_BLEND); glClearDepthf(1.0f); - glCullFace(GL_BACK); glDisable(GL_CULL_FACE); - // Enable z-buffer test - // We are using a projection matrix optimized for a floating point depth buffer, - // so the depth test and clear value need to be inverted (1 becomes near, 0 becomes far). glEnable(GL_DEPTH_TEST); glDepthFunc(GL_LEQUAL); } @@ -69,27 +63,37 @@ OpenGLES2App::~OpenGLES2App() void OpenGLES2App::mainloop() { - unsigned int startTime = GetTickCount(); - unsigned int currentTime = 0; - unsigned int previousTime = 0; - unsigned int lastFrameTime = 0; + unsigned int startTimeInMS = GetTickCount(); + unsigned int frameStartTimeInMS = 0; + unsigned int renderTimeInMS = 0; + unsigned int frameEndTimeInMS = 0; + unsigned int frameTimeInMS = 0; while (true) { - previousTime = currentTime; - currentTime = GetTickCount() - startTime; - lastFrameTime = currentTime - previousTime; + frameTimeInMS = frameEndTimeInMS - frameStartTimeInMS; + frameStartTimeInMS = RUNTIME_IN_MS(); + + update(m_animationSpeed * frameStartTimeInMS, m_animationSpeed * frameTimeInMS); + render(); + swapBuffers(); - update(m_animationSpeed * currentTime, m_animationSpeed * lastFrameTime); - render(); - swapBuffers(); + renderTimeInMS = RUNTIME_IN_MS() - frameStartTimeInMS; - usleep(m_timerIntervalInMs * 1000); + if (renderTimeInMS < m_timerIntervalInMs) + { + usleep((m_timerIntervalInMs - renderTimeInMS) * 1000); + } + + frameEndTimeInMS = RUNTIME_IN_MS(); } } -bool OpenGLES2App::createX11Context(int width, int height) +bool OpenGLES2App::createX11Context(SurfaceConfiguration* config) { + int width = config->surfaceWidth; + int height = config->surfaceHeight; + t_ilm_bool result = ILM_TRUE; Window rootWindow; XSetWindowAttributes windowAttributes; @@ -167,13 +171,12 @@ bool OpenGLES2App::createX11Context(int width, int height) return result; } -bool OpenGLES2App::createEGLContext(int width, int height) +bool OpenGLES2App::createEGLContext() { t_ilm_bool result = ILM_TRUE; m_eglContextStruct.eglDisplay = NULL; m_eglContextStruct.eglSurface = NULL; m_eglContextStruct.eglContext = NULL; - ilmErrorTypes error = ILM_FAILED; m_eglContextStruct.eglDisplay = eglGetDisplay((EGLNativeDisplayType) m_x11ContextStruct.x11Display); // TODO: remove all C style casts in C++ code; use C++ casts EGLint eglstatus = eglGetError(); @@ -236,43 +239,42 @@ bool OpenGLES2App::createEGLContext(int width, int height) cout << "Error: eglMakeCurrent() failed.\n"; } - // register surfaces to layermanager - t_ilm_layer layerid = (t_ilm_layer)LAYER_EXAMPLE_GLES_APPLICATIONS; - t_ilm_surface surfaceid = (t_ilm_surface)SURFACE_EXAMPLE_EGLX11_APPLICATION; + return result; +} - // TODO: if (error == ILM_FAILED) return ILM_FALSE; +bool OpenGLES2App::setupLayerMangement(SurfaceConfiguration* config) +{ + ilmErrorTypes error = ILM_FAILED; - cout << "create a surface " << (t_ilm_nativehandle) m_x11ContextStruct.x11Window << "\n"; + // register surfaces to layermanager + t_ilm_layer layerid = (t_ilm_layer)config->layerId;//LAYER_EXAMPLE_GLES_APPLICATIONS; + t_ilm_surface surfaceid = (t_ilm_surface)config->surfaceId;//SURFACE_EXAMPLE_EGLX11_APPLICATION; + int width = config->surfaceWidth; + int height = config->surfaceHeight; + + cout << "creating surface " << surfaceid << "\n"; error = ilm_surfaceCreate( (t_ilm_nativehandle) m_x11ContextStruct.x11Window, width, height, ILM_PIXELFORMAT_RGBA_8888, &surfaceid); - // TODO: if (error == ILM_FAILED) return ILM_FALSE; - - cout << "set surface dest region\n"; + cout << "set surface " << surfaceid << " dest region " << 0 << ", " << 0 << ", " << width << ", " << height << "\n"; error = ilm_surfaceSetDestinationRectangle(surfaceid, 0, 0, width, height); - // TODO: if (error == ILM_FAILED) return ILM_FALSE; - - cout << "set surface src region\n"; + cout << "set surface " << surfaceid << " src region " << 0 << ", " << 0 << ", " << width << ", " << height << "\n"; error = ilm_surfaceSetSourceRectangle(surfaceid, 0, 0, width, height); - // TODO: if (error == ILM_FAILED) return ILM_FALSE; - - cout << "add surface to layer\n"; - error = ilm_layerAddSurface(layerid, surfaceid); - cout << "Set surface visible\n"; + cout << "Set surface " << surfaceid << " visible\n"; error = ilm_surfaceSetVisibility(surfaceid, ILM_TRUE); - cout << "Set surface opacity\n"; - error = ilm_surfaceSetOpacity(surfaceid, 0.75f); - //if (error == ILM_FAILED) return ILM_FALSE; + cout << "Set surface " << surfaceid << " opacity 1.0\n"; + error = ilm_surfaceSetOpacity(surfaceid, 1.0f); + + cout << "add surface " << surfaceid << " to layer " << layerid << "\n"; + error = ilm_layerAddSurface(layerid, surfaceid); cout << "commit\n"; error = ilm_commitChanges(); - //if (error == ILM_FAILED) return ILM_FALSE; - - return result; + return ILM_TRUE; } void OpenGLES2App::destroyEglContext() diff --git a/LayerManagerExamples/EGLX11MockNavigation/src/main.cpp b/LayerManagerExamples/EGLX11MockNavigation/src/main.cpp index 835d858..a1e8cbb 100644 --- a/LayerManagerExamples/EGLX11MockNavigation/src/main.cpp +++ b/LayerManagerExamples/EGLX11MockNavigation/src/main.cpp @@ -18,33 +18,49 @@ ****************************************************************************/ #include "Argument.h" #include "MockNavi.h" +#include "LayerScene.h" #include using std::cout; +#define DEFAULT_FPS 30.0 +#define DEFAULT_ANIM 1.0 +#define DEFAULT_LAYER LAYER_EXAMPLE_GLES_APPLICATIONS +#define DEFAULT_SURFACE SURFACE_EXAMPLE_EGLX11_APPLICATION +#define DEFAULT_WIDTH 800 +#define DEFAULT_HEIGHT 480 + int main (int argc, const char * argv[]) { - float defaultFps = 15.0f; - float defaultAnim = 1.5f; - int defaultHouses = 15.0f; - - FloatArgument fps("fps", defaultFps, argc, argv); - FloatArgument animSpeed("anim", defaultAnim, argc, argv); - //IntArgument houses("houses", defaultHouses, argc, argv); + FloatArgument fps("fps", DEFAULT_FPS, argc, argv); + FloatArgument animSpeed("anim", DEFAULT_ANIM, argc, argv); + IntArgument surfaceId("surface", DEFAULT_SURFACE, argc, argv); + IntArgument layerId("layer", DEFAULT_LAYER, argc, argv); + IntArgument width("width", DEFAULT_WIDTH, argc, argv); + IntArgument height("height", DEFAULT_HEIGHT, argc, argv); BoolArgument help("help", false, argc, argv); if (help.get()) { - cout << "Usage: " << argv[0] << " [options]\n" + cout << "\nUsage: " << argv[0] << " [options]\n" << "possible options are:\n" << " -help show this help text\n" - << " -fps x run with x frames per second (default " << defaultFps << ")\n" - << " -anim x set animation speed (default " << defaultAnim << ")\n"; - //<< " -houses x generate x houses (default " << defaultHouses << ")\n"; + << " -fps x limit frames per second to x (default " << DEFAULT_FPS << ")\n" + << " -anim x set animation speed (default " << DEFAULT_ANIM << ")\n" + << " -surface x render to surface id x (default " << DEFAULT_SURFACE << ")\n" + << " -layer x add surface to layer x (default " << DEFAULT_LAYER << ")\n" + << " -width x set surface width to x (default " << DEFAULT_WIDTH << ")\n" + << " -height x set surface height to x (default " << DEFAULT_HEIGHT << ")\n\n"; } else { - MockNavi navi(fps.get(), animSpeed.get(), defaultHouses); + SurfaceConfiguration config; + config.layerId = layerId.get(); + config.surfaceId = surfaceId.get(); + config.surfaceWidth = width.get(); + config.surfaceHeight = height.get(); + + MockNavi navi(fps.get(), animSpeed.get(), &config); navi.mainloop(); } -- 2.7.4