Converting c++ code to open gl

cpp.net India
  • 15 years ago

    How can I run a C code on GPU using openGL?


    I  have this code in C and want it to run on GPU using openGL. I am just beginning to learn openGL.
    My main concern is how to shift the code from running on CPU to GPU. Any help is appreciated.


    void projectvolumeontoimagea (Volume* vol, CB_Image* cbi, float scale)
    {
       int i, j, k, p;
       float* img = (float*) vol->img;
       double wip[3];
       double *xip, *yip, *zip;
       double acc1[3],acc2[3],acc3[3];


       /* Rescale image (destructive rescaling) /
       for (i = 0; i < cbi->dim[0]
    cbi->dim[1]; i++) {
        cbi->img *= scale;
       }


       xip = (double) malloc (3vol->dim[0]sizeof(double));
       yip = (double
    ) malloc (3vol->dim[1]sizeof(double));
       zip = (double) malloc (3vol->dim[2]*sizeof(double));


       /* Precompute stuff here /
       for (i = 0; i < vol->dim[0]; i++) {
        double a = (double) (vol->offset[0] + i * vol->pix_spacing[0]);
        xip[i
    3+0] = a * cbi->matrix[0];
        xip[i3+1] = a * cbi->matrix[4];
        xip[i
    3+2] = a * cbi->matrix[8];
       }
       for (j = 0; j < vol->dim[1]; j++) {
        double a = (double) (vol->offset[1] + j * vol->pixspacing[1]);
        yip[j*3+0] = a * cbi->matrix[1];
        yip[j*3+1] = a * cbi->matrix[5];
        yip[j*3+2] = a * cbi->matrix[9];
       }
       for (k = 0; k < vol->dim[2]; k++) {
        double a = (double) (vol->offset[2 ] + k * vol->pix
    spacing[2]);
        zip[k3+0] = a * cbi->matrix[2];
        zip[k
    3+1] = a * cbi->matrix[6];
        zip[k3+2] = a * cbi->matrix[10];
       }
       wip[0] = cbi->matrix[3];
       wip[1] = cbi->matrix[7];
       wip[2] = cbi->matrix[11];
       
       /
    Main loop /
       p = 0;
       for (k = 0; k < vol->dim[2]; k++) {
        rawvec3_add3 (acc1, wip, &zip[3
    k]);
        for (j = 0; j < vol->dim[1]; j++) {
            rawvec3add3 (acc2, acc1, &yip[3*j]);
            for (i = 0; i < vol->dim[0]; i++) {
             rawvec3
    add3 (acc3, acc2, &xip[3*i]);
             acc3[0] = cbi->ic[0] + acc3[0] / acc3[2];
             acc3[1] = cbi->ic[1] + acc3[1] / acc3[2];
             img[p++] += getpixelvalue (cbi, acc3[0], acc3[1]);
            }
        }
       }
    }


    void
    rawvec3_add3 (double* v1, const double* v2, const double* v3)
    {
       int i;
       for (i = 0; i < 3; i++) {
        v1 = v2 + v3;
       }
    }


    float getpixelvalue (CB_Image* cbi, double r, double c)
    {
       int rr, cc;


       rr = roundint (r);
       if (rr < 0 || rr >= cbi->dim[0]) return 0.0;
       cc = round
    int (c);
       if (cc < 0 || cc >= cbi->dim[1]) return 0.0;
       return cbi->img[rr*cbi-> dim[0] + cc];
    }

Post a reply

No one has replied yet! Why not be the first?

Sign in or Join us (it's free).

Contribute

Why not write for us? Or you could submit an event or a user group in your area. Alternatively just tell us what you think!

Our tools

We've got automatic conversion tools to convert C# to VB.NET, VB.NET to C#. Also you can compress javascript and compress css and generate sql connection strings.

“C++ : Where friends have access to your private members.” - Gavin Russell Baker