Editing SimpleGL example

Warning: You are not logged in. Your IP address will be recorded in this page's edit history.
The edit can be undone. Please check the comparison below to verify that this is what you want to do, and then save the changes below to finish undoing the edit.
Latest revision Your text
Line 1: Line 1:
-
This is a simple program demonstrating how to use [[OpenGL-ES#OpenGL_variants|OpenGL ES 2.0]]. It will output this animated spiral, which can be moved around on the screen:
+
/* Created by exoticorn ( http://talk.maemo.org/showthread.php?t=37356 )
-
 
+
  * edited and commented by André Bergner [endboss]
-
[[Image:Egl-example_output.png]]
+
  *
-
 
+
  * librariries needed:  libx11-dev, libgles2-dev
-
<source lang="cpp">
+
  *
-
/* Created by exoticorn ( http://talk.maemo.org/showthread.php?t=37356 )
+
  * compile with:  g++  -lX11 -lEGL -lGLESv2  egl-example.cpp
-
* edited and commented by André Bergner [endboss]
+
  */
-
*
+
-
* libraries needed:  libx11-dev, libgles2-dev
+
#include  <iostream>
-
*
+
  using namespace std;
-
* compile with:  g++  -lX11 -lEGL -lGLESv2  egl-example.cpp
+
-
*/
+
#include  <cmath>
-
 
+
#include  <sys/time.h>
-
#include  <iostream>
+
-
#include <cstdlib>
+
#include  <X11/Xlib.h>
-
#include  <cstring>
+
#include  <X11/Xatom.h>
-
using namespace std;
+
   
-
 
+
#include  <GLES2/gl2.h>
-
#include  <cmath>
+
#include  <EGL/egl.h>
-
#include  <sys/time.h>
+
-
 
+
-
#include  <X11/Xlib.h>
+
-
#include  <X11/Xatom.h>
+
const char vertex_src [] =
-
#include <X11/Xutil.h>
+
"                                        \
-
 
+
    attribute vec4        position;      \
-
#include  <GLES2/gl2.h>
+
    varying mediump vec2  pos;            \
-
#include  <EGL/egl.h>
+
    uniform vec4          offset;        \
-
 
+
                                          \
-
 
+
    void main()                          \
-
 
+
    {                                    \
-
const char vertex_src [] =
+
      gl_Position = position + offset;  \
-
"                                        \
+
      pos = position.xy;                \
-
  attribute vec4        position;      \
+
    }                                    \
-
  varying mediump vec2  pos;            \
+
";
-
  uniform vec4          offset;        \
+
-
                                        \
+
-
  void main()                          \
+
const char fragment_src [] =
-
  {                                    \
+
"                                                      \
-
      gl_Position = position + offset;  \
+
    varying mediump vec2    pos;                        \
-
      pos = position.xy;                \
+
    uniform mediump float  phase;                      \
-
  }                                    \
+
                                                        \
-
";
+
    void  main()                                        \
-
 
+
    {                                                  \
-
 
+
      gl_FragColor  =  vec4( 1., 0.9, 0.7, 1.0 ) *    \
-
const char fragment_src [] =
+
        cos( 30.*sqrt(pos.x*pos.x + 1.5*pos.y*pos.y)  \
-
"                                                      \
+
              + atan(pos.y,pos.x) - phase );            \
-
  varying mediump vec2    pos;                        \
+
    }                                                  \
-
  uniform mediump float  phase;                      \
+
";
-
                                                      \
+
//  some more formulas to play with...
-
  void  main()                                        \
+
//      cos( 20.*(pos.x*pos.x + pos.y*pos.y) - phase );
-
  {                                                  \
+
//      cos( 20.*sqrt(pos.x*pos.x + pos.y*pos.y) + atan(pos.y,pos.x) - phase );
-
      gl_FragColor  =  vec4( 1., 0.9, 0.7, 1.0 ) *    \
+
//      cos( 30.*sqrt(pos.x*pos.x + 1.5*pos.y*pos.y - 1.8*pos.x*pos.y*pos.y)
-
        cos( 30.*sqrt(pos.x*pos.x + 1.5*pos.y*pos.y)  \
+
//            + atan(pos.y,pos.x) - phase );
-
            + atan(pos.y,pos.x) - phase );            \
+
-
  }                                                  \
+
-
";
+
void
-
//  some more formulas to play with...
+
print_shader_info_log (
-
//      cos( 20.*(pos.x*pos.x + pos.y*pos.y) - phase );
+
    GLuint  shader      // handle to the shader
-
//      cos( 20.*sqrt(pos.x*pos.x + pos.y*pos.y) + atan(pos.y,pos.x) - phase );
+
)
-
//      cos( 30.*sqrt(pos.x*pos.x + 1.5*pos.y*pos.y - 1.8*pos.x*pos.y*pos.y)
+
{
-
//            + atan(pos.y,pos.x) - phase );
+
    GLint  length;
-
 
+
-
 
+
    glGetShaderiv ( shader , GL_INFO_LOG_LENGTH , &length );
-
void
+
-
print_shader_info_log (
+
    if ( length ) {
-
  GLuint  shader      // handle to the shader
+
      char* buffer  =  new char [ length ];
-
)
+
      glGetShaderInfoLog ( shader , length , NULL , buffer );
-
{
+
      cout << "shader info: " <<  buffer << flush;
-
  GLint  length;
+
      delete [] buffer;
-
 
+
-
  glGetShaderiv ( shader , GL_INFO_LOG_LENGTH , &length );
+
      GLint success;
-
 
+
      glGetShaderiv( shader, GL_COMPILE_STATUS, &success );
-
  if ( length ) {
+
      if ( success != GL_TRUE )  exit ( 1 );
-
      char* buffer  =  new char [ length ];
+
    }
-
      glGetShaderInfoLog ( shader , length , NULL , buffer );
+
}
-
      cout << "shader info: " <<  buffer << flush;
+
-
      delete [] buffer;
+
-
 
+
GLuint
-
      GLint success;
+
load_shader (
-
      glGetShaderiv( shader, GL_COMPILE_STATUS, &success );
+
    const char  *shader_source,
-
      if ( success != GL_TRUE )  exit ( 1 );
+
    GLenum      type
-
  }
+
)
-
}
+
{
-
 
+
    GLuint  shader = glCreateShader( type );
-
 
+
-
GLuint
+
    glShaderSource  ( shader , 1 , &shader_source , NULL );
-
load_shader (
+
    glCompileShader ( shader );
-
  const char  *shader_source,
+
-
  GLenum      type
+
    print_shader_info_log ( shader );
-
)
+
-
{
+
    return shader;
-
  GLuint  shader = glCreateShader( type );
+
}
-
 
+
-
  glShaderSource  ( shader , 1 , &shader_source , NULL );
+
-
  glCompileShader ( shader );
+
Display    *x_display;
-
 
+
Window      win;
-
  print_shader_info_log ( shader );
+
EGLDisplay  egl_display;
-
 
+
EGLContext  egl_context;
-
  return shader;
+
   
-
}
+
GLfloat
-
 
+
    norm_x    =  0.0,
-
 
+
    norm_y    =  0.0,
-
Display    *x_display;
+
    offset_x  =  0.0,
-
Window      win;
+
    offset_y  =  0.0,
-
EGLDisplay  egl_display;
+
    p1_pos_x  =  0.0,
-
EGLContext  egl_context;
+
    p1_pos_y  =  0.0;
-
EGLSurface egl_surface;
+
-
 
+
GLint
-
GLfloat
+
    phase_loc,
-
  norm_x    =  0.0,
+
    offset_loc,
-
  norm_y    =  0.0,
+
    position_loc;
-
  offset_x  =  0.0,
+
-
  offset_y  =  0.0,
+
-
  p1_pos_x  =  0.0,
+
EGLSurface  egl_surface;
-
  p1_pos_y  =  0.0;
+
bool        update_pos = false;
-
 
+
-
GLint
+
const float vertexArray[] = {
-
  phase_loc,
+
    0.0,  0.5,  0.0,
-
  offset_loc,
+
  -0.5,  0.0,  0.0,
-
  position_loc;
+
    0.0, -0.5,  0.0,
-
 
+
    0.5,  0.0,  0.0,
-
 
+
    0.0,  0.5,  0.0  
-
bool        update_pos = false;
+
};
-
 
+
-
const float vertexArray[] = {
+
-
  0.0,  0.5,  0.0,
+
void  render()
-
  -0.5,  0.0,  0.0,
+
{
-
  0.0, -0.5,  0.0,
+
    static float  phase = 0;
-
  0.5,  0.0,  0.0,
+
    static int    donesetup = 0;
-
  0.0,  0.5,  0.0  
+
-
};
+
    static XWindowAttributes gwa;
-
 
+
-
 
+
    //// draw
-
void  render()
+
-
{
+
    if ( !donesetup ) {
-
  static float  phase = 0;
+
      XWindowAttributes  gwa;
-
  static int    donesetup = 0;
+
      XGetWindowAttributes ( x_display , win , &gwa );
-
 
+
      glViewport ( 0 , 0 , gwa.width , gwa.height );
-
  static XWindowAttributes gwa;
+
      glClearColor ( 0.08 , 0.06 , 0.07 , 1.);    // background color
-
 
+
      donesetup = 1;
-
  //// draw
+
    }
-
 
+
    glClear ( GL_COLOR_BUFFER_BIT );
-
  if ( !donesetup ) {
+
-
      XWindowAttributes  gwa;
+
    glUniform1f ( phase_loc , phase );  // write the value of phase to the shaders phase
-
      XGetWindowAttributes ( x_display , win , &gwa );
+
    phase  =  fmodf ( phase + 0.5f , 2.f * 3.141f );    // and update the local variable
-
      glViewport ( 0 , 0 , gwa.width , gwa.height );
+
-
      glClearColor ( 0.08 , 0.06 , 0.07 , 1.);    // background color
+
    if ( update_pos ) {  // if the position of the texture has changed due to user action
-
      donesetup = 1;
+
      GLfloat old_offset_x  =  offset_x;
-
  }
+
      GLfloat old_offset_y  =  offset_y;
-
  glClear ( GL_COLOR_BUFFER_BIT );
+
-
 
+
      offset_x  =  norm_x - p1_pos_x;
-
  glUniform1f ( phase_loc , phase );  // write the value of phase to the shaders phase
+
      offset_y  =  norm_y - p1_pos_y;
-
  phase  =  fmodf ( phase + 0.5f , 2.f * 3.141f );    // and update the local variable
+
-
 
+
      p1_pos_x  =  norm_x;
-
  if ( update_pos ) {  // if the position of the texture has changed due to user action
+
      p1_pos_y  =  norm_y;
-
      GLfloat old_offset_x  =  offset_x;
+
-
      GLfloat old_offset_y  =  offset_y;
+
      offset_x  +=  old_offset_x;
-
 
+
      offset_y  +=  old_offset_y;
-
      offset_x  =  norm_x - p1_pos_x;
+
-
      offset_y  =  norm_y - p1_pos_y;
+
      update_pos = false;
-
 
+
    }
-
      p1_pos_x  =  norm_x;
+
-
      p1_pos_y  =  norm_y;
+
    glUniform4f ( offset_loc  ,  offset_x , offset_y , 0.0 , 0.0 );
-
 
+
-
      offset_x  +=  old_offset_x;
+
    glVertexAttribPointer ( position_loc, 3, GL_FLOAT, false, 0, vertexArray );
-
      offset_y  +=  old_offset_y;
+
    glEnableVertexAttribArray ( position_loc );
-
 
+
    glDrawArrays ( GL_TRIANGLE_STRIP, 0, 5 );
-
      update_pos = false;
+
-
  }
+
    eglSwapBuffers ( egl_display, egl_surface );  // get the rendered buffer to the screen
-
 
+
}
-
  glUniform4f ( offset_loc  ,  offset_x , offset_y , 0.0 , 0.0 );
+
-
 
+
-
  glVertexAttribPointer ( position_loc, 3, GL_FLOAT, false, 0, vertexArray );
+
////////////////////////////////////////////////////////////////////////////////////////////
-
  glEnableVertexAttribArray ( position_loc );
+
-
  glDrawArrays ( GL_TRIANGLE_STRIP, 0, 5 );
+
-
 
+
int  main()
-
  eglSwapBuffers ( egl_display, egl_surface );  // get the rendered buffer to the screen
+
{
-
}
+
    ///////  the X11 part  //////////////////////////////////////////////////////////////////
-
 
+
    // in the first part the program opens a connection to the X11 window manager
-
 
+
    //
-
////////////////////////////////////////////////////////////////////////////////////////////
+
-
 
+
    x_display = XOpenDisplay ( NULL );  // open the standard display (the primary screen)
-
 
+
    if ( x_display == NULL ) {
-
int  main()
+
      cerr << "cannot connect to X server" << endl;
-
{
+
      return 1;
-
  ///////  the X11 part  //////////////////////////////////////////////////////////////////
+
    }
-
  // in the first part the program opens a connection to the X11 window manager
+
-
  //
+
    Window root  =  DefaultRootWindow( x_display );  // get the root window (usually the whole screen)
-
 
+
-
  x_display = XOpenDisplay ( NULL );  // open the standard display (the primary screen)
+
    XSetWindowAttributes  swa;
-
  if ( x_display == NULL ) {
+
    swa.event_mask  =  ExposureMask | PointerMotionMask;
-
      cerr << "cannot connect to X server" << endl;
+
-
      return 1;
+
    win  =  XCreateWindow (         // create a window with the provided parameters
-
  }
+
              x_display, root,
-
 
+
              0, 0, 800, 480,  0,
-
  Window root  =  DefaultRootWindow( x_display );  // get the root window (usually the whole screen)
+
              CopyFromParent, InputOutput,
-
 
+
              CopyFromParent, CWEventMask,
-
  XSetWindowAttributes  swa;
+
              &swa );
-
  swa.event_mask  =  ExposureMask | PointerMotionMask | KeyPressMask;
+
-
 
+
    XSetWindowAttributes  xattr;
-
  win  =  XCreateWindow (   // create a window with the provided parameters
+
    Atom  atom;
-
              x_display, root,
+
    int  one = 1;
-
              0, 0, 800, 480,  0,
+
-
              CopyFromParent, InputOutput,
+
    xattr.override_redirect = False;
-
              CopyFromParent, CWEventMask,
+
    XChangeWindowAttributes ( x_display, win, CWOverrideRedirect, &xattr );
-
              &swa );
+
-
 
+
    atom = XInternAtom ( x_display, "_NET_WM_STATE_FULLSCREEN", True );
-
  XSetWindowAttributes  xattr;
+
    XChangeProperty (
-
  Atom  atom;
+
      x_display, win,
-
  int  one = 1;
+
      XInternAtom ( x_display, "_NET_WM_STATE", True ),
-
 
+
      XA_ATOM,  32,  PropModeReplace,
-
  xattr.override_redirect = False;
+
      (unsigned char*) &atom,  1 );
-
  XChangeWindowAttributes ( x_display, win, CWOverrideRedirect, &xattr );
+
-
 
+
    XChangeProperty (
-
  atom = XInternAtom ( x_display, "_NET_WM_STATE_FULLSCREEN", True );
+
      x_display, win,
-
  XChangeProperty (
+
      XInternAtom ( x_display, "_HILDON_NON_COMPOSITED_WINDOW", True ),
-
      x_display, win,
+
      XA_INTEGER,  32,  PropModeReplace,
-
      XInternAtom ( x_display, "_NET_WM_STATE", True ),
+
      (unsigned char*) &one,  1);
-
      XA_ATOM,  32,  PropModeReplace,
+
-
      (unsigned char*) &atom,  1 );
+
    XMapWindow ( x_display , win );            // make the window visible on the screen
-
 
+
    XStoreName ( x_display , win , "GL test" ); // give the window a name
-
  XChangeProperty (
+
-
      x_display, win,
+
    //// get identifiers for the provided atom name strings
-
      XInternAtom ( x_display, "_HILDON_NON_COMPOSITED_WINDOW", False ),
+
    Atom wm_state  = XInternAtom ( x_display, "_NET_WM_STATE", False );
-
      XA_INTEGER,  32,  PropModeReplace,
+
    Atom fullscreen = XInternAtom ( x_display, "_NET_WM_STATE_FULLSCREEN", False );
-
      (unsigned char*) &one,  1);
+
-
 
+
    XEvent xev;
-
  XWMHints hints;
+
    memset ( &xev, 0, sizeof(xev) );
-
  hints.input = True;
+
-
  hints.flags = InputHint;
+
    xev.type                = ClientMessage;
-
  XSetWMHints(x_display, win, &hints);
+
    xev.xclient.window      = win;
-
 
+
    xev.xclient.message_type = wm_state;
-
  XMapWindow ( x_display , win );            // make the window visible on the screen
+
    xev.xclient.format      = 32;
-
  XStoreName ( x_display , win , "GL test" ); // give the window a name
+
    xev.xclient.data.l[0]    = 1;
-
 
+
    xev.xclient.data.l[1]    = fullscreen;
-
  //// get identifiers for the provided atom name strings
+
    XSendEvent (                // send an event mask to the X-server
-
  Atom wm_state  = XInternAtom ( x_display, "_NET_WM_STATE", False );
+
      x_display,
-
  Atom fullscreen = XInternAtom ( x_display, "_NET_WM_STATE_FULLSCREEN", False );
+
      DefaultRootWindow ( x_display ),
-
 
+
      False,
-
  XEvent xev;
+
      SubstructureNotifyMask,
-
  memset ( &xev, 0, sizeof(xev) );
+
      &xev );
-
 
+
-
  xev.type                = ClientMessage;
+
-
  xev.xclient.window      = win;
+
    ///////  the egl part  //////////////////////////////////////////////////////////////////
-
  xev.xclient.message_type = wm_state;
+
    //  egl provides an interface to connect the graphics related functionality of openGL ES
-
  xev.xclient.format      = 32;
+
    //  with the windowing interface and functionality of the native operation system (X11
-
  xev.xclient.data.l[0]    = 1;
+
    //  in our case.
-
  xev.xclient.data.l[1]    = fullscreen;
+
-
  XSendEvent (                // send an event mask to the X-server
+
    egl_display  =  eglGetDisplay( (EGLNativeDisplayType) x_display );
-
      x_display,
+
    if ( egl_display == EGL_NO_DISPLAY ) {
-
      DefaultRootWindow ( x_display ),
+
      cerr << "Got no EGL display." << endl;
-
      False,
+
      return 1;
-
      SubstructureNotifyMask,
+
    }
-
      &xev );
+
-
 
+
    if ( !eglInitialize( egl_display, NULL, NULL ) ) {
-
 
+
      cerr << "Unable to initialize EGL" << endl;
-
  ///////  the egl part  //////////////////////////////////////////////////////////////////
+
      return 1;
-
  //  egl provides an interface to connect the graphics related functionality of openGL ES
+
    }
-
  //  with the windowing interface and functionality of the native operation system (X11
+
-
  //  in our case.
+
    EGLint attr[] = {      // some attributes to set up our egl-interface
-
 
+
      EGL_BUFFER_SIZE, 16,
-
  egl_display  =  eglGetDisplay( (EGLNativeDisplayType) x_display );
+
      EGL_RENDERABLE_TYPE,
-
  if ( egl_display == EGL_NO_DISPLAY ) {
+
      EGL_OPENGL_ES2_BIT,
-
      cerr << "Got no EGL display." << endl;
+
      EGL_NONE
-
      return 1;
+
    };
-
  }
+
-
 
+
    EGLConfig  ecfg;
-
  if ( !eglInitialize( egl_display, NULL, NULL ) ) {
+
    EGLint    num_config;
-
      cerr << "Unable to initialize EGL" << endl;
+
    if ( !eglChooseConfig( egl_display, attr, &ecfg, 1, &num_config ) ) {
-
      return 1;
+
      cerr << "Failed to choose config (eglError: " << eglGetError() << ")" << endl;
-
  }
+
      return 1;
-
 
+
    }
-
  EGLint attr[] = {      // some attributes to set up our egl-interface
+
-
      EGL_BUFFER_SIZE, 16,
+
    if ( num_config != 1 ) {
-
      EGL_RENDERABLE_TYPE,
+
      cerr << "Didn't get exactly one config, but " << num_config << endl;
-
      EGL_OPENGL_ES2_BIT,
+
      return 1;
-
      EGL_NONE
+
    }
-
  };
+
-
 
+
    egl_surface = eglCreateWindowSurface ( egl_display, ecfg, (void*)win, NULL );
-
  EGLConfig  ecfg;
+
    if ( egl_surface == EGL_NO_SURFACE ) {
-
  EGLint    num_config;
+
      cerr << "Unable to create EGL surface (eglError: " << eglGetError() << ")" << endl;
-
  if ( !eglChooseConfig( egl_display, attr, &ecfg, 1, &num_config ) ) {
+
      return 1;
-
      cerr << "Failed to choose config (eglError: " << eglGetError() << ")" << endl;
+
    }
-
      return 1;
+
-
  }
+
    //// egl-contexts collect all state descriptions needed required for operation
-
 
+
    EGLint ctxattr[] = {
-
  if ( num_config != 1 ) {
+
      EGL_CONTEXT_CLIENT_VERSION, 2,
-
      cerr << "Didn't get exactly one config, but " << num_config << endl;
+
      EGL_NONE
-
      return 1;
+
    };
-
  }
+
    egl_context = eglCreateContext ( egl_display, ecfg, EGL_NO_CONTEXT, ctxattr );
-
 
+
    if ( egl_context == EGL_NO_CONTEXT ) {
-
  egl_surface = eglCreateWindowSurface ( egl_display, ecfg, win, NULL );
+
      cerr << "Unable to create EGL context (eglError: " << eglGetError() << ")" << endl;
-
  if ( egl_surface == EGL_NO_SURFACE ) {
+
      return 1;
-
      cerr << "Unable to create EGL surface (eglError: " << eglGetError() << ")" << endl;
+
    }
-
      return 1;
+
-
  }
+
    //// associate the egl-context with the egl-surface
-
 
+
    eglMakeCurrent( egl_display, egl_surface, egl_surface, egl_context );
-
  //// egl-contexts collect all state descriptions needed required for operation
+
-
  EGLint ctxattr[] = {
+
-
      EGL_CONTEXT_CLIENT_VERSION, 2,
+
    ///////  the openGL part  ///////////////////////////////////////////////////////////////
-
      EGL_NONE
+
-
  };
+
    GLuint vertexShader  = load_shader ( vertex_src , GL_VERTEX_SHADER  );    // load vertex shader
-
  egl_context = eglCreateContext ( egl_display, ecfg, EGL_NO_CONTEXT, ctxattr );
+
    GLuint fragmentShader = load_shader ( fragment_src , GL_FRAGMENT_SHADER );  // load fragment shader
-
  if ( egl_context == EGL_NO_CONTEXT ) {
+
-
      cerr << "Unable to create EGL context (eglError: " << eglGetError() << ")" << endl;
+
    GLuint shaderProgram  = glCreateProgram ();                // create program object
-
      return 1;
+
    glAttachShader ( shaderProgram, vertexShader );            // and attach both...
-
  }
+
    glAttachShader ( shaderProgram, fragmentShader );          // ... shaders to it
-
 
+
-
  //// associate the egl-context with the egl-surface
+
    glLinkProgram ( shaderProgram );    // link the program
-
  eglMakeCurrent( egl_display, egl_surface, egl_surface, egl_context );
+
    glUseProgram  ( shaderProgram );    // and select it for usage
-
 
+
-
 
+
    //// now get the locations (kind of handle) of the shaders variables
-
  ///////  the openGL part  ///////////////////////////////////////////////////////////////
+
    position_loc  = glGetAttribLocation  ( shaderProgram , "position" );
-
 
+
    phase_loc    = glGetUniformLocation ( shaderProgram , "phase"    );
-
  GLuint vertexShader  = load_shader ( vertex_src , GL_VERTEX_SHADER  );    // load vertex shader
+
    offset_loc    = glGetUniformLocation ( shaderProgram , "offset"  );
-
  GLuint fragmentShader = load_shader ( fragment_src , GL_FRAGMENT_SHADER );  // load fragment shader
+
    if ( position_loc < 0  ||  phase_loc < 0  ||  offset_loc < 0 ) {
-
 
+
      cerr << "Unable to get uniform location" << endl;
-
  GLuint shaderProgram  = glCreateProgram ();                // create program object
+
      return 1;
-
  glAttachShader ( shaderProgram, vertexShader );            // and attach both...
+
    }
-
  glAttachShader ( shaderProgram, fragmentShader );          // ... shaders to it
+
-
 
+
-
  glLinkProgram ( shaderProgram );    // link the program
+
    const float
-
  glUseProgram  ( shaderProgram );    // and select it for usage
+
      window_width  = 800.0,
-
 
+
      window_height = 480.0;
-
  //// now get the locations (kind of handle) of the shaders variables
+
-
  position_loc  = glGetAttribLocation  ( shaderProgram , "position" );
+
    //// this is needed for time measuring  -->  frames per second
-
  phase_loc    = glGetUniformLocation ( shaderProgram , "phase"    );
+
    struct  timezone  tz;
-
  offset_loc    = glGetUniformLocation ( shaderProgram , "offset"  );
+
    timeval  t1, t2;
-
  if ( position_loc < 0  ||  phase_loc < 0  ||  offset_loc < 0 ) {
+
    gettimeofday ( &t1 , &tz );
-
      cerr << "Unable to get uniform location" << endl;
+
    int  num_frames = 0;
-
      return 1;
+
-
  }
+
    bool quit = false;
-
 
+
    while ( !quit ) {    // the main loop
-
 
+
-
  const float
+
      while ( XPending ( x_display ) ) {  // check for events from the x-server
-
      window_width  = 800.0,
+
-
      window_height = 480.0;
+
          XEvent  xev;
-
 
+
          XNextEvent( x_display, &xev );
-
  //// this is needed for time measuring  -->  frames per second
+
-
  struct  timezone  tz;
+
          if ( xev.type == MotionNotify ) {  // if mouse has moved
-
  timeval  t1, t2;
+
//            cout << "move to: << xev.xmotion.x << "," << xev.xmotion.y << endl;
-
  gettimeofday ( &t1 , &tz );
+
            GLfloat window_y  =  (window_height - xev.xmotion.y) - window_height / 2.0;
-
  int  num_frames = 0;
+
            norm_y            =  window_y / (window_height / 2.0);
-
 
+
            GLfloat window_x  =  xev.xmotion.x - window_width / 2.0;
-
  bool quit = false;
+
            norm_x            =  window_x / (window_width / 2.0);
-
  while ( !quit ) {    // the main loop
+
            update_pos = true;
-
 
+
          }
-
      while ( XPending ( x_display ) ) {  // check for events from the x-server
+
-
 
+
//        if ( xev.type == KeyPress )  quit = true; // doesn't work ???
-
        XEvent  xev;
+
      }
-
        XNextEvent( x_display, &xev );
+
-
 
+
      render();  // now we finally put something on the screen
-
        if ( xev.type == MotionNotify ) {  // if mouse has moved
+
-
//            cout << "move to: << xev.xmotion.x << "," << xev.xmotion.y << endl;
+
      if ( ++num_frames % 100 == 0 ) {
-
            GLfloat window_y  =  (window_height - xev.xmotion.y) - window_height / 2.0;
+
          gettimeofday( &t2, &tz );
-
            norm_y            =  window_y / (window_height / 2.0);
+
          float dt  =  t2.tv_sec - t1.tv_sec + (t2.tv_usec - t1.tv_usec) * 1e-6;
-
            GLfloat window_x  =  xev.xmotion.x - window_width / 2.0;
+
          cout << "fps: " << num_frames / dt << endl;
-
            norm_x            =  window_x / (window_width / 2.0);
+
          num_frames = 0;
-
            update_pos = true;
+
          t1 = t2;
-
        }
+
      }
-
 
+
//      usleep( 1000*10 );
-
        if ( xev.type == KeyPress )  quit = true;
+
    }
-
      }
+
-
 
+
-
      render();  // now we finally put something on the screen
+
    ////  cleaning up...
-
 
+
    eglDestroyContext ( egl_display, egl_context );
-
      if ( ++num_frames % 100 == 0 ) {
+
    eglDestroySurface ( egl_display, egl_surface );
-
        gettimeofday( &t2, &tz );
+
    eglTerminate      ( egl_display );
-
        float dt  =  t2.tv_sec - t1.tv_sec + (t2.tv_usec - t1.tv_usec) * 1e-6;
+
    XDestroyWindow    ( x_display, win );
-
        cout << "fps: " << num_frames / dt << endl;
+
    XCloseDisplay    ( x_display );
-
        num_frames = 0;
+
-
        t1 = t2;
+
    return 0;
-
      }
+
}
-
//      usleep( 1000*10 );
+
-
  }
+
-
 
+
-
 
+
-
  ////  cleaning up...
+
-
  eglDestroyContext ( egl_display, egl_context );
+
-
  eglDestroySurface ( egl_display, egl_surface );
+
-
  eglTerminate      ( egl_display );
+
-
  XDestroyWindow    ( x_display, win );
+
-
  XCloseDisplay    ( x_display );
+
-
 
+
-
  return 0;
+
-
}
+
-
</source>
+
   
   
[[Category:Development]]
[[Category:Development]]
[[Category:Fremantle]]
[[Category:Fremantle]]

Learn more about Contributing to the wiki.


Please note that all contributions to maemo.org wiki may be edited, altered, or removed by other contributors. If you do not want your writing to be edited mercilessly, then do not submit it here.
You are also promising us that you wrote this yourself, or copied it from a public domain or similar free resource (see maemo.org wiki:Copyrights for details). Do not submit copyrighted work without permission!


Cancel | Editing help (opens in new window)