|
| 1 | +/******************************************************* |
| 2 | + * Copyright (c) 2015-2019, ArrayFire |
| 3 | + * All rights reserved. |
| 4 | + * |
| 5 | + * This file is distributed under 3-clause BSD license. |
| 6 | + * The complete license agreement can be obtained at: |
| 7 | + * http://arrayfire.com/licenses/BSD-3-Clause |
| 8 | + ********************************************************/ |
| 9 | + |
| 10 | +#include <forge.h> |
| 11 | +#define USE_FORGE_CPU_COPY_HELPERS |
| 12 | +#include <ComputeCopy.h> |
| 13 | +#include <cmath> |
| 14 | +#include <cstdlib> |
| 15 | +#include <vector> |
| 16 | +#include <iostream> |
| 17 | + |
| 18 | +const unsigned DIMX = 1000; |
| 19 | +const unsigned DIMY = 800; |
| 20 | +const unsigned NSECTORS = 10; |
| 21 | + |
| 22 | +std::vector<float> generateSectors(unsigned count = NSECTORS) |
| 23 | +{ |
| 24 | + std::vector<float> result; |
| 25 | + float prefixSum = 0; |
| 26 | + for (; count > 0; --count) { |
| 27 | + result.push_back(prefixSum); |
| 28 | + result.push_back(std::rand() & 0xffff); |
| 29 | + prefixSum += result.back(); |
| 30 | + } |
| 31 | + return result; |
| 32 | +} |
| 33 | + |
| 34 | +std::vector<float> generateColors(unsigned count = NSECTORS) |
| 35 | +{ |
| 36 | + std::vector<float> result; |
| 37 | + for (; count > 0; --count) { |
| 38 | + for (int channel = 0; channel < 3; ++channel) |
| 39 | + result.push_back(std::rand()/(float)RAND_MAX); |
| 40 | + } |
| 41 | + return result; |
| 42 | +} |
| 43 | + |
| 44 | +int main(int argc, char* argv[]) |
| 45 | +{ |
| 46 | + /* |
| 47 | + * First Forge call should be a window creation call |
| 48 | + * so that necessary OpenGL context is created for any |
| 49 | + * other forge::* object to be created successfully |
| 50 | + */ |
| 51 | + forge::Window wnd(DIMX, DIMY, "Pie Demo"); |
| 52 | + wnd.makeCurrent(); |
| 53 | + |
| 54 | + forge::Chart chart(FG_CHART_2D); |
| 55 | + |
| 56 | + /* |
| 57 | + * Create pie object specifying number of bins |
| 58 | + */ |
| 59 | + forge::Pie pie = chart.pie(NSECTORS, forge::f32); |
| 60 | + |
| 61 | + GfxHandle* handles[2]; |
| 62 | + createGLBuffer(&handles[0], pie.vertices(), FORGE_VERTEX_BUFFER); |
| 63 | + createGLBuffer(&handles[1], pie.colors(), FORGE_VERTEX_BUFFER); |
| 64 | + |
| 65 | + std::vector<float> pieArray = generateSectors(); |
| 66 | + std::vector<float> colArray = generateColors(); |
| 67 | + |
| 68 | + /* set the axes limits to minimum and maximum values of data */ |
| 69 | + float valueRange = pieArray[pieArray.size() - 2] + pieArray[pieArray.size() - 1]; |
| 70 | + chart.setAxesLimits(0, valueRange, 0, valueRange); |
| 71 | + |
| 72 | + copyToGLBuffer(handles[0], (ComputeResourceHandle)pieArray.data(), pie.verticesSize()); |
| 73 | + copyToGLBuffer(handles[1], (ComputeResourceHandle)colArray.data(), pie.colorsSize()); |
| 74 | + |
| 75 | + do { |
| 76 | + wnd.draw(chart); |
| 77 | + } while(!wnd.close()); |
| 78 | + |
| 79 | + releaseGLBuffer(handles[0]); |
| 80 | + releaseGLBuffer(handles[1]); |
| 81 | + |
| 82 | + return 0; |
| 83 | +} |
0 commit comments