add options to conrol presentation and clear behaviour
authorRalf Jung <post@ralfj.de>
Sun, 31 Mar 2013 13:55:29 +0000 (15:55 +0200)
committerRalf Jung <post@ralfj.de>
Sun, 31 Mar 2013 13:55:29 +0000 (15:55 +0200)
gltest.cpp

index 8d296a3991965e965e0c3ac1e7e0c03eba4a6c53..0cae93287a6f021270c0f739a3f9a1a3ad519f9a 100644 (file)
@@ -15,13 +15,13 @@ namespace po = boost::program_options;
 #include "glwindow.h"
 #if defined(USE_GLX)
 #include "glxbackend.h"
 #include "glwindow.h"
 #if defined(USE_GLX)
 #include "glxbackend.h"
-GLBackend *createGLBackend()
+static GLBackend *createGLBackend()
 {
        return new GLXBackend();
 }
 #elif defined(USE_EGL)
 #include "eglbackend.h"
 {
        return new GLXBackend();
 }
 #elif defined(USE_EGL)
 #include "eglbackend.h"
-GLBackend *createGLBackend()
+static GLBackend *createGLBackend()
 {
        return new EGLBackend();
 }
 {
        return new EGLBackend();
 }
@@ -30,12 +30,12 @@ GLBackend *createGLBackend()
 #endif
 
 // configuration
 #endif
 
 // configuration
-const GLfloat boxWidth = 0.045f;
-const GLfloat boxSpeed = 1.25f; // per second
+static const GLfloat boxWidth = 0.045f;
+static const GLfloat boxSpeed = 1.25f; // per second
 
 // profiler
 
 // profiler
-const int numProfilerStates = 5;
-const char *profilerStateNames[numProfilerStates] = { "Pre-Render", "Drawing", "Swapping", "Post-Render", "Outside renderer"};
+enum ProfilerState { statePreRender, stateDraw, statePresent, statePostRender, stateOutsideRender, numProfilerStates };
+static const char *profilerStateNames[numProfilerStates] = { "Pre-Render", "Drawing", "Presenting", "Post-Render", "Outside renderer"};
 
 // utility functions
 static double getTime()
 
 // utility functions
 static double getTime()
@@ -53,7 +53,7 @@ static void drawRect(GLfloat x1, GLfloat y1, GLfloat x2, GLfloat y2)
 // the window
 class TearTestWindow : public GLWindow {
 public:
 // the window
 class TearTestWindow : public GLWindow {
 public:
-       TearTestWindow() : GLWindow(XOpenDisplay(0), createGLBackend()), boxPos(0), boxDirection(1)
+       TearTestWindow(bool overdraw, bool copy) : GLWindow(XOpenDisplay(0), createGLBackend()), overdraw(overdraw), copy(copy), boxPos(0), boxDirection(1)
        {}
 
        void setSwapInterval(int i) {
        {}
 
        void setSwapInterval(int i) {
@@ -89,7 +89,7 @@ protected:
                glFlush();
        }
        
                glFlush();
        }
        
-       void profilerTick(int nextState)
+       void profilerTick(ProfilerState nextState)
        {
                assert (nextState >= 0 && nextState < numProfilerStates);
                double time = getTime();
        {
                assert (nextState >= 0 && nextState < numProfilerStates);
                double time = getTime();
@@ -113,8 +113,9 @@ protected:
        }
        
        void renderGL()
        }
        
        void renderGL()
-       {                            
-               profilerTick(0);
+       {              
+               //////////////////////////////////////////////
+               profilerTick(statePreRender);
                double time = getTime();
                // anim
                double passedTime = time-lastFrame;
                double time = getTime();
                // anim
                double passedTime = time-lastFrame;
@@ -130,37 +131,37 @@ protected:
                        }
                }
                lastFrame = time;
                        }
                }
                lastFrame = time;
-               // draw
-               //glFlush();
-               profilerTick(1);
-               //glClear(GL_COLOR_BUFFER_BIT);
-               glBegin(GL_QUADS);
-               // clear manually
-               glColor3f(0.0f, 0.0f, 0.0f);
-               drawRect(0, 0, 1, 1);
+               //////////////////////////////////////////////
+               profilerTick(stateDraw);
+               if (overdraw) {
+                       glBegin(GL_QUADS);
+                       // clear manually
+                       glColor3f(0.0f, 0.0f, 0.0f);
+                       drawRect(0, 0, 1, 1);
+               }
+               else {
+                       glClear(GL_COLOR_BUFFER_BIT);
+                       glBegin(GL_QUADS);
+               }
                glColor3f(0.8f, 1.0f, 0.75f);
                drawRect(boxPos, 0, boxPos+boxWidth, 1);
                glEnd();
                glColor3f(0.8f, 1.0f, 0.75f);
                drawRect(boxPos, 0, boxPos+boxWidth, 1);
                glEnd();
-               usleep(20*1000);
-               profilerTick(2);
-               getBackend()->swapBuffers();
-//             glDrawBuffer(GL_FRONT);
-//             int xpos = 0;
-//             int ypos = 0;
-               //foreach (const QRect &r, region.rects()) {
-                       // convert to OpenGL coordinates
-                       //int y = displayHeight() - 0 - r.height();
-//                     glBitmap(0, 0, 0, 0, 0 - xpos, 0 - ypos, NULL); // not glRasterPos2f, see glxbackend.cpp
-//                     xpos = 0;
-//                     ypos = 0;
-//                     glCopyPixels(0, 0, getWidth(), getHeight(), GL_COLOR);
-               //}
-//             glBitmap(0, 0, 0, 0, -xpos, -ypos, NULL); // move position back to 0,0
-//             glDrawBuffer(GL_BACK);
-               profilerTick(3);
+               //////////////////////////////////////////////
+               profilerTick(statePresent);
+               if (copy) {
+                       glDrawBuffer(GL_FRONT);
+                       glCopyPixels(0, 0, getWidth(), getHeight(), GL_COLOR);
+                       glDrawBuffer(GL_BACK);
+               }
+               else {
+                       getBackend()->swapBuffers();
+               }
+               //////////////////////////////////////////////
+               profilerTick(statePostRender);
                glFlush();
                ++framect;
                glFlush();
                ++framect;
-               profilerTick(4);
+               //////////////////////////////////////////////
+               profilerTick(stateOutsideRender);
        }
        
        virtual void handleKeyPress(KeySym key)
        }
        
        virtual void handleKeyPress(KeySym key)
@@ -173,6 +174,7 @@ protected:
        }
 
 private:
        }
 
 private:
+       bool overdraw, copy;
        double lastFrame;
        GLfloat boxPos, boxDirection;
        // FPS
        double lastFrame;
        GLfloat boxPos, boxDirection;
        // FPS
@@ -190,6 +192,8 @@ int main(int argc, char ** argv)
        desc.add_options()
                ("help,h", "produce help message")
                ("swap-interval,i", po::value<int>(), "set swap interval")
        desc.add_options()
                ("help,h", "produce help message")
                ("swap-interval,i", po::value<int>(), "set swap interval")
+               ("copy,c", "copy to front buffer (instead of performing a buffer swap)")
+               ("overdraw,o", "overdraw previous image (instead of calling glClear)")
        ;
        po::variables_map vm;
        po::store(po::parse_command_line(argc, argv, desc), vm);
        ;
        po::variables_map vm;
        po::store(po::parse_command_line(argc, argv, desc), vm);
@@ -201,7 +205,7 @@ int main(int argc, char ** argv)
        }
 
        // actual program
        }
 
        // actual program
-       TearTestWindow w;
+       TearTestWindow w(vm.count("overdraw"), vm.count("copy"));
        w.open(800, 600);
        if (vm.count("swap-interval"))
                w.setSwapInterval(vm["swap-interval"].as<int>());
        w.open(800, 600);
        if (vm.count("swap-interval"))
                w.setSwapInterval(vm["swap-interval"].as<int>());