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 project_volume_onto_image_a (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[i] *= scale;
}
xip = (double*) malloc (3*vol->dim[0]*sizeof(double));
yip = (double*) malloc (3*vol->dim[1]*sizeof(double));
zip = (double*) malloc (3*vol->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[i*3+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->pix_spacing[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[k*3+0] = a * cbi->matrix[2];
zip[k*3+1] = a * cbi->matrix[6];
zip[k*3+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++) {
rawvec3_add3 (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++] += get_pixel_value (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[i] = v2[i] + v3[i];
}
}
float get_pixel_value (CB_Image* cbi, double r, double c)
{
int rr, cc;
rr = round_int (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];
}
void project_volume_onto_image_a (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[i] *= scale;
}
xip = (double*) malloc (3*vol->dim[0]*sizeof(double));
yip = (double*) malloc (3*vol->dim[1]*sizeof(double));
zip = (double*) malloc (3*vol->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[i*3+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->pix_spacing[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[k*3+0] = a * cbi->matrix[2];
zip[k*3+1] = a * cbi->matrix[6];
zip[k*3+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++) {
rawvec3_add3 (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++] += get_pixel_value (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[i] = v2[i] + v3[i];
}
}
float get_pixel_value (CB_Image* cbi, double r, double c)
{
int rr, cc;
rr = round_int (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];
}