Advertisement
Guest User

modified_openvg_demo_rpi

a guest
Aug 16th, 2012
519
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 8.68 KB | None | 0 0
  1. // shapes: minimal program to explore OpenVG
  2. // Anthony Starks (ajstarks@gmail.com)
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include <assert.h>
  6. #include <time.h>
  7.  
  8. #include "VG/openvg.h"
  9. #include "VG/vgu.h"
  10. #include "EGL/egl.h"
  11. #include "GLES/gl.h"
  12.  
  13. typedef struct
  14. {
  15.     uint32_t screen_width;
  16.     uint32_t screen_height;
  17. // OpenGL|ES objects
  18.     EGLDisplay display;
  19.     EGLSurface surface;
  20.     EGLContext context;
  21. } STATE_T;
  22.  
  23. static void exit_func(void);
  24. static STATE_T _state, *state=&_state;
  25.  
  26. // init_ogl sets the display, OpenGL|ES context and screen information
  27. // state holds the OGLES model information
  28. static void init_ogl(STATE_T *state) {
  29.     int32_t success = 0;
  30.     EGLBoolean result;
  31.     EGLint num_config;
  32.  
  33.     static EGL_DISPMANX_WINDOW_T nativewindow;
  34.  
  35.     DISPMANX_ELEMENT_HANDLE_T dispman_element;
  36.     DISPMANX_DISPLAY_HANDLE_T dispman_display;
  37.     DISPMANX_UPDATE_HANDLE_T dispman_update;
  38.     VC_RECT_T dst_rect;
  39.     VC_RECT_T src_rect;
  40.  
  41.     static const EGLint attribute_list[] = {
  42.         EGL_RED_SIZE, 8,
  43.         EGL_GREEN_SIZE, 8,
  44.         EGL_BLUE_SIZE, 8,
  45.         EGL_ALPHA_SIZE, 8,
  46.         EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
  47.         EGL_NONE
  48.     };
  49.  
  50.     EGLConfig config;
  51.  
  52. // get an EGL display connection
  53.     state->display = eglGetDisplay(EGL_DEFAULT_DISPLAY);
  54.     assert(state->display!=EGL_NO_DISPLAY);
  55.  
  56. // initialize the EGL display connection
  57.     result = eglInitialize(state->display, NULL, NULL);
  58.     assert(EGL_FALSE != result);
  59.  
  60. // bind OpenVG API
  61.     eglBindAPI(EGL_OPENVG_API);
  62.  
  63. // get an appropriate EGL frame buffer configuration
  64.     result = eglChooseConfig(state->display, attribute_list, &config, 1, &num_config);
  65.     assert(EGL_FALSE != result);
  66.  
  67. // create an EGL rendering context
  68.     state->context = eglCreateContext(state->display, config, EGL_NO_CONTEXT, NULL);
  69.     assert(state->context!=EGL_NO_CONTEXT);
  70.  
  71. // create an EGL window surface
  72.     success = graphics_get_display_size(0 /* LCD */, &state->screen_width, &state->screen_height);
  73.     assert( success >= 0 );
  74.  
  75.     dst_rect.x = 0;
  76.     dst_rect.y = 0;
  77.     dst_rect.width = state->screen_width;
  78.     dst_rect.height = state->screen_height;
  79.  
  80.     src_rect.x = 0;
  81.     src_rect.y = 0;
  82.     src_rect.width = state->screen_width << 16;
  83.     src_rect.height = state->screen_height << 16;
  84.  
  85.     dispman_display = vc_dispmanx_display_open( 0 /* LCD */);
  86.     dispman_update = vc_dispmanx_update_start( 0 );
  87.  
  88.     dispman_element = vc_dispmanx_element_add ( dispman_update, dispman_display,
  89.         0/*layer*/, &dst_rect, 0/*src*/,
  90.         &src_rect, DISPMANX_PROTECTION_NONE, 0 /*alpha*/, 0/*clamp*/, 0/*transform*/);
  91.  
  92.     nativewindow.element = dispman_element;
  93.     nativewindow.width = state->screen_width;
  94.     nativewindow.height = state->screen_height;
  95.     vc_dispmanx_update_submit_sync( dispman_update );
  96.  
  97.     state->surface = eglCreateWindowSurface( state->display, config, &nativewindow, NULL );
  98.     assert(state->surface != EGL_NO_SURFACE);
  99.  
  100. // connect the context to the surface
  101.     result = eglMakeCurrent(state->display, state->surface, state->surface, state->context);
  102.     assert(EGL_FALSE != result);
  103.  
  104. //DAVE - Set up screen ratio
  105.     glViewport(0, 0, (GLsizei)state->screen_width, (GLsizei)state->screen_height);
  106.  
  107.     glMatrixMode(GL_PROJECTION);
  108.     glLoadIdentity();
  109.  
  110.     float ratio = (float)state->screen_width / (float)state->screen_height;
  111.     glFrustumf(-ratio, ratio, -1.0f, 1.0f, 1.0f, 10.0f);
  112. }
  113.  
  114.  
  115. // setfill sets the fill color
  116. void setfill(float color[4]) {
  117.     VGPaint fillPaint = vgCreatePaint();
  118.     vgSetParameteri(fillPaint, VG_PAINT_TYPE, VG_PAINT_TYPE_COLOR);
  119.     vgSetParameterfv(fillPaint, VG_PAINT_COLOR, 4, color);
  120.     vgSetPaint(fillPaint, VG_FILL_PATH);
  121.     vgDestroyPaint(fillPaint);
  122. }
  123.  
  124.  
  125. // setstroke sets the stroke color and width
  126. void setstroke(float color[4], float width) {
  127.     VGPaint strokePaint = vgCreatePaint();
  128.     vgSetParameteri(strokePaint, VG_PAINT_TYPE, VG_PAINT_TYPE_COLOR);
  129.     vgSetParameterfv(strokePaint, VG_PAINT_COLOR, 4, color);
  130.     vgSetPaint(strokePaint, VG_STROKE_PATH);
  131.     vgSetf(VG_STROKE_LINE_WIDTH, width);
  132.     vgSeti(VG_STROKE_CAP_STYLE, VG_CAP_BUTT);
  133.     vgSeti(VG_STROKE_JOIN_STYLE, VG_JOIN_MITER);
  134.     vgDestroyPaint(strokePaint);
  135. }
  136.  
  137.  
  138. // rect makes a rectangle at the specified location and dimensions, applying style
  139. void Rect(float x, float y, float w, float h, float sw, float fill[4], float stroke[4]) {
  140.     VGPath path = vgCreatePath(VG_PATH_FORMAT_STANDARD, VG_PATH_DATATYPE_F, 1.0f, 0.0f, 0, 0, VG_PATH_CAPABILITY_ALL);
  141.     vguRect(path, x, y, w, h);
  142.     setfill(fill);
  143.     setstroke(stroke, sw);
  144.     vgDrawPath(path, VG_FILL_PATH | VG_STROKE_PATH);
  145.     vgDestroyPath(path);
  146. }
  147.  
  148.  
  149. // Line makes a line, applying style
  150. void Line(float x1, float y1, float x2, float y2, float sw, float stroke[4]) {
  151.     VGPath path = vgCreatePath(VG_PATH_FORMAT_STANDARD, VG_PATH_DATATYPE_F, 1.0f, 0.0f, 0, 0, VG_PATH_CAPABILITY_ALL);
  152.     vguLine(path, x1, y1, x2, y2);
  153.     setstroke(stroke, sw);
  154.     vgDrawPath(path, VG_STROKE_PATH);
  155.     vgDestroyPath(path);
  156.  
  157. }
  158.  
  159.  
  160. // Roundrect makes an rounded rectangle at the specified location and dimensions, applying style
  161. void Roundrect(float x, float y, float w, float h, float rw, float rh, float sw, float fill[4], float stroke[4]) {
  162.     VGPath path = vgCreatePath(VG_PATH_FORMAT_STANDARD, VG_PATH_DATATYPE_F, 1.0f, 0.0f, 0, 0, VG_PATH_CAPABILITY_ALL);
  163.     vguRoundRect(path, x, y, w, h, rw, rh);
  164.     setfill(fill);
  165.     setstroke(stroke, sw);
  166.     vgDrawPath(path, VG_FILL_PATH | VG_STROKE_PATH);
  167.     vgDestroyPath(path);
  168. }
  169.  
  170.  
  171. // Ellipse makes an ellipse at the specified location and dimensions, applying style
  172. void Ellipse(float x, float y, float w, float h, float sw, float fill[4], float stroke[4]) {
  173.     VGPath path = vgCreatePath(VG_PATH_FORMAT_STANDARD, VG_PATH_DATATYPE_F, 1.0f, 0.0f, 0, 0, VG_PATH_CAPABILITY_ALL);
  174.     vguEllipse(path, x, y, w, h);
  175.     setfill(fill);
  176.     setstroke(stroke, sw);
  177.     vgDrawPath(path, VG_FILL_PATH | VG_STROKE_PATH);
  178.     vgDestroyPath(path);
  179. }
  180.  
  181.  
  182. // Start begins the picture, clearing a rectangluar region with a specified color
  183. void Start(int width, int height, float fill[4]) {
  184.     vgSetfv(VG_CLEAR_COLOR, 4, fill);
  185.     vgClear(0, 0, width, height);
  186.     vgLoadIdentity();
  187. }
  188.  
  189.  
  190. // End checks for errors, and renders to the display
  191. void End() {
  192.     assert(vgGetError() == VG_NO_ERROR);
  193.     eglSwapBuffers(state->display, state->surface);
  194.     assert(eglGetError() == EGL_SUCCESS);
  195. }
  196.  
  197.  
  198. // randcolor returns a fraction of 255
  199. VGfloat randcolor() {
  200.     return (VGfloat)(rand() % 256) / 255.0;
  201. }
  202.  
  203.  
  204. // randf returns a floating point number bounded by n
  205. VGfloat randf(n) {
  206.     return (VGfloat)(rand() % n);
  207. }
  208.  
  209.  
  210. // rshapes draws shapes (rect and ellipse) with random colors, strokes, and sizes.
  211. void rshapes(int width, int height, int n) {
  212.     float rcolor[4], scolor[4], bgcolor[4] = {0,0,0,1};
  213.     scolor[3] = 1; // strokes are solid
  214.     Start(width, height, bgcolor);
  215.     int i;
  216.     for (i=0; i < n; i++) {
  217.         rcolor[0] = randcolor();
  218.         rcolor[1] = randcolor();
  219.         rcolor[2] = randcolor();
  220.         rcolor[3] = randcolor();
  221.  
  222.         scolor[1] = randcolor();
  223.         scolor[2] = randcolor();
  224.         scolor[3] = randcolor();
  225.         if (i%2 == 0) {
  226.             Ellipse(randf(width), randf(height), randf(200), randf(100), randf(10), rcolor, scolor);
  227.         }
  228.         else {
  229.             Rect(randf(width), randf(height), randf(200), randf(100), randf(10), rcolor, scolor);
  230.         }
  231.     }
  232.     End();
  233. }
  234.  
  235.  
  236. // exit_func cleans up
  237. static void exit_func(void) {
  238. // clear screen
  239.     glClear( GL_COLOR_BUFFER_BIT );
  240.     eglSwapBuffers(state->display, state->surface);
  241.  
  242. // Release OpenGL resources
  243.     eglMakeCurrent( state->display, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT );
  244.     eglDestroySurface( state->display, state->surface );
  245.     eglDestroyContext( state->display, state->context );
  246.     eglTerminate( state->display );
  247. }
  248.  
  249.  
  250. // main initializes the system and shows the picture,
  251. // exit and clean up when you hit [RETURN].
  252. int main (int argc, char **argv) {
  253.     int w, h, n;
  254.     bcm_host_init();
  255.     memset( state, 0, sizeof( *state ) ); // clear application state
  256.     init_ogl(state); // Start OGLES
  257.     w = state->screen_width;
  258.     h = state->screen_height;
  259.     n = 500;
  260.     if (argc > 1) {
  261.         n = atoi(argv[1]);
  262.     }
  263.     srand ( time(NULL) );
  264.  
  265.     rshapes(w, h, n);
  266.     time_t exit_at = time(NULL) + 10;
  267.     int count = 0;
  268.     while (1) {
  269.     count++;
  270.     rshapes(w,h,n);;
  271.     if (time(NULL) > exit_at) {
  272.         break;
  273.     }
  274.     }
  275.     printf("Rendered %d frames in 10 seconds of %d shapes, fps: %f \n", (count), (n), (float) count / 10.0f);
  276.     exit_func();
  277.  
  278.     return 0;
  279. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement