Beispiel #1
0
static void load_camera()
{
    FILE* fp = fopen("camera.txt", "rt");
    assert(fp);
    for (int i = 0; i < 16; i++)
        fscanf(fp, "%f", &cam_to_view.data()[i]);
    for (int i = 0; i < 16; i++)
        fscanf(fp, "%f", &cam_to_clip.data()[i]);
    fclose(fp);

    fprintf(stderr, "camera loaded from camera.txt\n");
}
Beispiel #2
0
int main()
{
    init();

    if (SDL_Init(SDL_INIT_VIDEO) < 0)
    {
        fprintf(stderr, "SDL_Init failed: %s\n", SDL_GetError());
        return 1;
    }
    atexit(SDL_Quit);

    if (SDL_SetVideoMode(WINDOW_WIDTH, WINDOW_HEIGHT, 32, SDL_OPENGL) == 0)
    {
        fprintf(stderr, "SDL_SetVideoMode failed: %s\n", SDL_GetError());
        return 1;
    }

    cam_to_clip = perspective<float>(45.f / 180.f * 3.14159265f, 1.f, 0.1f, 100.f);

    Vector3f eye = Vector3f(4.f, 1.f, 4.f);
    Vector3f center = Vector3f(0.f, 0.f, 0.f);
    Vector3f up = Vector3f(0.f, 1.f, 0.f);
    cam_to_view = look_at<float>(eye, center, up);

    load_camera();

    int prev_ticks = SDL_GetTicks();

    int last_ticks = prev_ticks;
    int frames = 0;

    while (1)
    {
        int dt = SDL_GetTicks() - prev_ticks;
        prev_ticks += dt;

        // Handle events.

        SDL_Event ev;

        while (SDL_PollEvent(&ev))
        {
            switch (ev.type)
            {
            case SDL_KEYDOWN:
                switch (ev.key.keysym.sym)
                {
                case SDLK_ESCAPE:
                    return 0;

                case SDLK_1:
                    fprintf(stderr, "rendering using OpenGL\n");
                    mode = RENDER_GL;
                    last_ticks = SDL_GetTicks();
                    frames = 0;
                    break;

                case SDLK_2:
                    fprintf(stderr, "rendering using cpu ray tracing\n");
                    mode = RENDER_RT_CPU;
                    last_ticks = SDL_GetTicks();
                    frames = 0;
                    break;

                case SDLK_3:
                    fprintf(stderr, "rendering using cuda ray tracing\n");
                    mode = RENDER_RT_CUDA;
                    last_ticks = SDL_GetTicks();
                    frames = 0;
                    break;

                case SDLK_u:
                    {
                        FILE* fp = fopen("camera.txt", "wt");
                        for (int i = 0; i < 16; i++)
                            fprintf(fp, "%f ", cam_to_view.data()[i]);
                        for (int i = 0; i < 16; i++)
                            fprintf(fp, "%f ", cam_to_clip.data()[i]);
                        fclose(fp);

                        fprintf(stderr, "saved camera to camera.txt\n");
                    }
                    break;

                case SDLK_p:
                    load_camera();
                    break;

                default:
                    break;
                }
                break;

            case SDL_MOUSEMOTION:
                if (ev.motion.state & 1)
                {
                    float fdx = ev.motion.xrel / 400.;
                    float fdy = ev.motion.yrel / 400.;

                    cam_to_view =
                        rotate(Vector3f(1.0f, 0.0, 0.0), fdy) *
                        rotate(Vector3f(0.0, 1.0f, 0.0), fdx) *
                        cam_to_view;
                }
                break;

            case SDL_QUIT:
                return 0;
            }
        }

        // Handle movement.

        float dtf = dt / 1000.f;

        Uint8* keys = SDL_GetKeyState(0);

        if (keys[SDLK_UP] || keys[SDLK_w])
            move(Vector3f(0.f, 0.f, move_speed) * dtf);
        if (keys[SDLK_DOWN] || keys[SDLK_s])
            move(Vector3f(0.f, 0.f, -move_speed) * dtf);
        if (keys[SDLK_LEFT] || keys[SDLK_a])
            move(Vector3f(move_speed, 0.f, 0.f) * dtf);
        if (keys[SDLK_RIGHT] || keys[SDLK_d])
            move(Vector3f(-move_speed, 0.f, 0.f) * dtf);

        // Draw things.

        switch (mode)
        {
        case RENDER_GL:
            draw_gl();
            break;
        case RENDER_RT_CPU:
            draw_rt_cpu();
            break;
        case RENDER_RT_CUDA:
            draw_rt_cuda();
            break;
        };

        SDL_GL_SwapBuffers();

//        frames++;
//        fprintf(stderr, "average %.2f\n", (SDL_GetTicks() - last_ticks) / (double)frames);
    }

    return 0;
}
Beispiel #3
0
        void load(const char* fn)
        {
            FILE* fp = fopen(fn, "r");

            for (;;)
            {
                Matrix4x4f m;
                int n = fscanf(fp, "%f %f %f %f  %f %f %f %f  %f %f %f %f  %f %f %f %f",
                    &m.data()[0], &m.data()[1], &m.data()[2], &m.data()[3],
                    &m.data()[4], &m.data()[5], &m.data()[6], &m.data()[7],
                    &m.data()[8], &m.data()[9], &m.data()[10], &m.data()[11],
                    &m.data()[12], &m.data()[13], &m.data()[14], &m.data()[15]);

                if (n != 16)
                    break;

                frames.push_back(m);
            }

            fclose(fp);
        }