示例#1
0
/* -------------------------------------------------------------------------- */
void compute_plane( Complex *c_max, 
                    Complex *c_min, 
                    Complex *c_factor)
{
    unsigned int x, y;
    Complex c_cur;      /* used as the current z value */
    
    static char plane[HEIGHT][WIDTH];  /* array to hold the generated image */

    /* classic nested for loop approach */
    for(y = 0; y < HEIGHT; y++)    
    {
        c_cur.im = convert_y_coord( c_max->im, c_factor->im, y);
        
        for(x = 0; x < WIDTH; x++)
        {
            c_cur.re = convert_x_coord( c_min->re, c_factor->re, x);
            
            if( is_outside_rad2( &c_cur)){
                plane[y][x] = MAX_ITERATIONS / 2;  /* make the outer grey */
            }
            else{
                /* compute c_cur checking if it is in the set */
                plane[y][x] = is_member( c_cur);
            }
        }
    }
    
    /* write it out to a PPM file */
    write_to_ppm( plane);
}
示例#2
0
int main()
{
    printf("pid: %d\n", getpid());
    uint8_t *pixels;
    light_node lights = NULL;
    rectangular_node rectangulars = NULL;
    sphere_node spheres = NULL;
    color background = { 0.0, 0.1, 0.1 };
    struct timespec start, end;

#include "use-models.h"

    /* allocate by the given resolution */
    pixels = malloc(sizeof(unsigned char) * ROWS * COLS * 3);
    if (!pixels) exit(-1);

    printf("# Rendering scene\n");
    /* do the ray tracing with the given geometry */
    clock_gettime(CLOCK_REALTIME, &start);
    raytracing(pixels, background,
               rectangulars, spheres, lights, &view, ROWS, COLS);
    clock_gettime(CLOCK_REALTIME, &end);
    {
        FILE *outfile = fopen(OUT_FILENAME, "wb");
        write_to_ppm(outfile, pixels, ROWS, COLS);
        fclose(outfile);
    }

    delete_rectangular_list(&rectangulars);
    delete_sphere_list(&spheres);
    delete_light_list(&lights);
    free(pixels);
    printf("Done!\n");
    printf("Execution time of raytracing() : %lf sec\n", diff_in_second(start, end));
    return 0;
}
示例#3
0
文件: main.c 项目: wwjd228/raytracing
int main()
{
    uint8_t *pixels;
    light_node lights = NULL;
    rectangular_node rectangulars = NULL;
    sphere_node spheres = NULL;
    color background = { 0.0, 0.1, 0.1 };
    struct timespec start, end;
    pthread_attr_t attr;
    void *status;
#include "use-models.h"
    pthread_attr_init(&attr);
    pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE);

    /* allocate by the given resolution */
    pixels = malloc(sizeof(unsigned char) * ROWS * COLS * 3);
    thread_arg *arg_lefttop = setThread_arg(pixels, background,
                                            rectangulars, spheres, lights,
                                            &view, ROWS/2, COLS/2, 0, 0);
    thread_arg *arg_leftdown = setThread_arg(pixels, background,
                               rectangulars, spheres, lights,
                               &view, ROWS, COLS/2, ROWS/2, 0);
    thread_arg *arg_righttop = setThread_arg(pixels, background,
                               rectangulars, spheres, lights,
                               &view, ROWS/2, COLS, 0, COLS/2);
    thread_arg *arg_rightdown = setThread_arg(pixels, background,
                                rectangulars, spheres, lights,
                                &view, ROWS, COLS, ROWS/2, COLS/2);

    pthread_attr_destroy(&attr);
    if (!pixels) exit(-1);

    printf("# Rendering scene\n");
    /* do the ray tracing with the given geometry */
    clock_gettime(CLOCK_REALTIME, &start);
    pthread_create(&callThd[0], &attr, raytracing, (void *)arg_lefttop);
    pthread_create(&callThd[1], &attr, raytracing, (void *)arg_leftdown);
    pthread_create(&callThd[2], &attr, raytracing, (void *)arg_righttop);
    pthread_create(&callThd[3], &attr, raytracing, (void *)arg_rightdown);

    //raytracing(pixels, background,
    //           rectangulars, spheres, lights, &view, ROWS, COLS);
    pthread_attr_destroy(&attr);

    pthread_join(callThd[0], &status);
    pthread_join(callThd[1], &status);
    pthread_join(callThd[2], &status);
    pthread_join(callThd[3], &status);

    clock_gettime(CLOCK_REALTIME, &end);
    {
        FILE *outfile = fopen(OUT_FILENAME, "wb");
        write_to_ppm(outfile, pixels, ROWS, COLS);
        fclose(outfile);
    }

    delete_rectangular_list(&rectangulars);
    delete_sphere_list(&spheres);
    delete_light_list(&lights);
    free(pixels);

    printf("Done!\n");
    printf("Execution time of raytracing() : %lf sec\n", diff_in_second(start, end));
    return 0;
}
示例#4
0
文件: main.c 项目: ktvexe/raytracing
int main()
{
    /*thread variable*/
    pthread_t *threadx;
    struct parameter *threadpara;

    uint8_t *pixels;
    light_node lights = NULL;
    rectangular_node rectangulars = NULL;
    sphere_node spheres = NULL;
    color background = { 0.0, 0.1, 0.1 };
    struct timespec start, end;

#include "use-models.h"

    /* allocate by the given resolution */
    pixels = malloc(sizeof(unsigned char) * ROWS * COLS * 3);
    if (!pixels) exit(-1);

    printf("# Rendering scene\n");
    /* do the ray tracing with the given geometry */
    clock_gettime(CLOCK_REALTIME, &start);

    /*****create parameter which thread used****/
    threadpara =(struct parameter*)malloc(THREAD_NUM*sizeof(struct parameter));
    for(int i =0; i<THREAD_NUM; i++) {
        threadpara[i].begin_col =COLS*((double)i/THREAD_NUM);
        threadpara[i].finish_col =COLS*((double)(i+1.0)/THREAD_NUM);
        threadpara[i].pixels =pixels;
        threadpara[i].lights =lights;
        threadpara[i].rectangulars = rectangulars;
        threadpara[i].spheres =spheres;
        memcpy( threadpara[i].background,background,sizeof(color));
        threadpara[i].view = &view;
        threadpara[i].width = ROWS;
        threadpara[i].height = COLS;
    }
    threadx=(pthread_t*)malloc(THREAD_NUM*sizeof(pthread_t));

    /*
    pthread_create(&threadx[0],NULL,&raytracing,&threadpara[0]);
    pthread_create(&threadx[1],NULL,&raytracing,&threadpara[1]);
    pthread_create(&threadx[2],NULL,&raytracing,&threadpara[2]);
    pthread_create(&threadx[3],NULL,&raytracing,&threadpara[3]);


    pthread_join(threadx[0],NULL);
    pthread_join(threadx[1],NULL);
    pthread_join(threadx[2],NULL);
    pthread_join(threadx[3],NULL);
     */

    for(int i=0; i<THREAD_NUM; i++) {
        pthread_create(&threadx[i],NULL,&raytracing,&threadpara[i]);
    }
    for(int i=0; i<THREAD_NUM; i++) {
        pthread_join(threadx[i],NULL);
    }

    clock_gettime(CLOCK_REALTIME, &end);
    {
        FILE *outfile = fopen(OUT_FILENAME, "wb");
        write_to_ppm(outfile, pixels, ROWS, COLS);
        fclose(outfile);
    }

    delete_rectangular_list(&rectangulars);
    delete_sphere_list(&spheres);
    delete_light_list(&lights);
    free(pixels);
    FILE *output;
    output= fopen("opt.txt","a");
    fprintf(output,"raytracing(): %lf \n", diff_in_second(start, end));
    fclose(output);
    printf("Done!\n");
    printf("Execution time of raytracing() : %lf sec\n", diff_in_second(start, end));
    free(threadx);
    return 0;
}