Hello and good morning guys,
I tryied to compile this simple Ray tracer code from CUDA By Example book and i get a whole bunch of errors can you please help me sort this out…?
The code is this
#include <cuda.h>
#include <GL\gl.h>
#include <cpu_bitmap.h>
#include <stdio.h>
#include <math.h>
#define rnd( x ) (x * rand() / RAND_MAX)
#define SPHERES 20
#define INF 2e10f
__global__ void kernel (unsigned char *ptr)
{
int x = threadIdx.x + blockIdx.x * blockDim.x;
int y = threadIdx.y + blockIdx.y * blockDim.y;
int offset = x + y * blockDim.x * gridDim.x;
float ox = (x - DIM/2);
float oy = (y - DIM/2);
float r=0, g=0, b=0;
float maxz = -INF;
for(int i=0; i<SPHERES; i++)
{
float n;
float t = s[i].hit( ox, oy, &n);
if(t > maxz)
{
float fscale = n;
r = s[i].r * fscale;
g = s[i].g * fscale;
b = s[i].b * fscale;
}
}
ptr[offset*4 + 0] = (int)(r * 255);
ptr[offset*4 + 1] = (int)(g * 255);
ptr[offset*4 + 2] = (int)(b * 255);
ptr[offset*4 + 3] = 255;
}
struct Sphere {
float x,y,z;
float r,g,b;
float radius;
__device__ float hit(float ox, float oy, float *n)
{
float dx = ox - x;
float dy = oy - y;
if((dx*dx + dy*dy) < (radius*radius))
{
float dz = sqrtf( radius*radius - dx*dx - dy*dy );
*n = dz / sqrtf(radius*radius);
return dz + z;
}
return -INF;
}
}
int main(void)
{
Sphere *s;
cudaEvent_t start,stop;
cudaEventCreate( &start );
cudaEventCreate( &stop );
cudaEventRecord( start , 0 );
CPUBitmap bitmap( DIM,DIM);
unsigned char *dev_bitmap;
cudaMalloc(&dev_bitmap,bitmap.image_size());
cudaMalloc(&s,sizeof(Sphere * SPHERES));
Sphere *temp_s = (Sphere*)malloc( sizeof(Sphere) * SPHERES );
for (int i=0; i<SPHERES; i++)
{
temp_s[i].r = rnd( 1.0f );
temp_s[i].g = rnd( 1.0f );
temp_s[i].b = rnd( 1.0f );
temp_s[i].x = rnd( 1000.0f ) - 500;
temp_s[i].y = rnd( 1000.0f ) - 500;
temp_s[i].z = rnd( 1000.0f ) - 500;
temp_s[i].radius = rnd( 100.0f ) + 20;
}
cudaMemcpy(s, temp_s, sizeof((Sphere * SPHERES)), cudaMemcpyHostToDevice);
free(temp_s);
dim3 grids(DIM/16,DIM/16);
dim3 threads(16,16);
kernel<<<grids,threads>>>( dev_bitmap );
cudaMemcpy(bitmap.get_bitmap_ptr(), dev_bitmap, bitmap.image_size(), cudaMemcpyDeviceToHost);
bitmap.display_and_exit();
cudaFree( dev_bitmap );
cudaFree( s );
return 0;
}
and the list of errors i get is this
- C:/Users/Angelos/Documents/Visual Studio 2010/Projects/CUDA/RayTracing/RayTracing/main.cu(18): error : identifier "DIM" is undefined
C:/Users/Angelos/Documents/Visual Studio 2010/Projects/CUDA/RayTracing/RayTracing/main.cu(27): error : identifier “s” is undefined
C:/Users/Angelos/Documents/Visual Studio 2010/Projects/CUDA/RayTracing/RayTracing/main.cu(74): error : expected a “;”
C:/Users/Angelos/Documents/Visual Studio 2010/Projects/CUDA/RayTracing/RayTracing/main.cu(84): error : identifier “DIM” is undefined
C:/Users/Angelos/Documents/Visual Studio 2010/Projects/CUDA/RayTracing/RayTracing/main.cu(89): error : expected a “)”
1>C:/Users/Angelos/Documents/Visual Studio 2010/Projects/CUDA/RayTracing/RayTracing/main.cu(104): error : expected a “)”
C:/Users/Angelos/Documents/Visual Studio 2010/Projects/CUDA/RayTracing/RayTracing/main.cu(104): error : expected an expression