Пример #1
0
static void
set_clip_planes (CoglFramebuffer *framebuffer,
                 CoglMatrixEntry *modelview_entry,
                 float x_1,
                 float y_1,
                 float x_2,
                 float y_2)
{
  CoglMatrix modelview_matrix;
  CoglMatrixStack *projection_stack =
    _cogl_framebuffer_get_projection_stack (framebuffer);
  CoglMatrix projection_matrix;
  CoglMatrix modelview_projection;
  float signed_area;

  float vertex_tl[4] = { x_1, y_1, 0, 1.0 };
  float vertex_tr[4] = { x_2, y_1, 0, 1.0 };
  float vertex_bl[4] = { x_1, y_2, 0, 1.0 };
  float vertex_br[4] = { x_2, y_2, 0, 1.0 };

  cogl_matrix_stack_get (projection_stack, &projection_matrix);
  cogl_matrix_entry_get (modelview_entry, &modelview_matrix);

  cogl_matrix_multiply (&modelview_projection,
                        &projection_matrix,
                        &modelview_matrix);

  project_vertex (&modelview_projection, vertex_tl);
  project_vertex (&modelview_projection, vertex_tr);
  project_vertex (&modelview_projection, vertex_bl);
  project_vertex (&modelview_projection, vertex_br);

  /* Calculate the signed area of the polygon formed by the four
     vertices so that we can know its orientation */
  signed_area = (vertex_tl[0] * (vertex_tr[1] - vertex_bl[1])
                 + vertex_tr[0] * (vertex_br[1] - vertex_tl[1])
                 + vertex_br[0] * (vertex_bl[1] - vertex_tr[1])
                 + vertex_bl[0] * (vertex_tl[1] - vertex_br[1]));

  /* Set the clip planes to form lines between all of the vertices
     using the same orientation as we calculated */
  if (signed_area > 0.0f)
    {
      /* counter-clockwise */
      set_clip_plane (framebuffer, GL_CLIP_PLANE0, vertex_tl, vertex_bl);
      set_clip_plane (framebuffer, GL_CLIP_PLANE1, vertex_bl, vertex_br);
      set_clip_plane (framebuffer, GL_CLIP_PLANE2, vertex_br, vertex_tr);
      set_clip_plane (framebuffer, GL_CLIP_PLANE3, vertex_tr, vertex_tl);
    }
  else
    {
      /* clockwise */
      set_clip_plane (framebuffer, GL_CLIP_PLANE0, vertex_tl, vertex_tr);
      set_clip_plane (framebuffer, GL_CLIP_PLANE1, vertex_tr, vertex_br);
      set_clip_plane (framebuffer, GL_CLIP_PLANE2, vertex_br, vertex_bl);
      set_clip_plane (framebuffer, GL_CLIP_PLANE3, vertex_bl, vertex_tl);
    }
}
Пример #2
0
int main()
{
 int exit_flag = 0, i;

 VERTEX cube[8];
 cube[0].local = (_3D){-10.0, -10.0, -10.0};
 cube[1].local = (_3D){10.0, -10.0, -10.0};
 cube[2].local = (_3D){10.0, 10.0, -10.0};
 cube[3].local = (_3D){-10.0, 10.0, -10.0};
 cube[4].local = (_3D){-10.0, -10.0, 10.0};
 cube[5].local = (_3D){10.0, -10.0, 10.0};
 cube[6].local = (_3D){10.0, 10.0, 10.0};
 cube[7].local = (_3D){-10.0, 10.0, 10.0};

 _3D world_pos = {0.0, 0.0, 100.0};

 init();

 while(!exit_flag)
  {
   rest(10);

   if(keypressed())
    {
     if(key[KEY_ESC]) { exit_flag = 1; }
     if(key[KEY_LEFT]) { world_pos.x -= 1.0; }
     if(key[KEY_RIGHT]) { world_pos.x += 1.0; }
     if(key[KEY_UP]) { world_pos.z += 3.0; }
     if(key[KEY_DOWN]) { world_pos.z -= 3.0; }
    }

   clear_to_color(buffer, 0);

   for(i = 0; i < 8; i++)
    {
     rotate_vertex(&cube[i], 1, 2, 3);
     project_vertex(&cube[i], world_pos);
    }

   _2d_line(cube[0].screen[0], cube[1].screen[0], RED);
   _2d_line(cube[1].screen[0], cube[2].screen[0], RED);
   _2d_line(cube[2].screen[0], cube[3].screen[0], RED);
   _2d_line(cube[3].screen[0], cube[0].screen[0], RED);

   _2d_line(cube[4].screen[0], cube[5].screen[0], RED);
   _2d_line(cube[5].screen[0], cube[6].screen[0], RED);
   _2d_line(cube[6].screen[0], cube[7].screen[0], RED);
   _2d_line(cube[7].screen[0], cube[4].screen[0], RED);

   _2d_line(cube[0].screen[0], cube[4].screen[0], RED);
   _2d_line(cube[1].screen[0], cube[5].screen[0], RED);
   _2d_line(cube[2].screen[0], cube[6].screen[0], RED);
   _2d_line(cube[3].screen[0], cube[7].screen[0], RED);

   _2d_line(cube[0].screen[1], cube[1].screen[1], BLUE);
   _2d_line(cube[1].screen[1], cube[2].screen[1], BLUE);
   _2d_line(cube[2].screen[1], cube[3].screen[1], BLUE);
   _2d_line(cube[3].screen[1], cube[0].screen[1], BLUE);

   _2d_line(cube[4].screen[1], cube[5].screen[1], BLUE);
   _2d_line(cube[5].screen[1], cube[6].screen[1], BLUE);
   _2d_line(cube[6].screen[1], cube[7].screen[1], BLUE);
   _2d_line(cube[7].screen[1], cube[4].screen[1], BLUE);

   _2d_line(cube[0].screen[1], cube[4].screen[1], BLUE);
   _2d_line(cube[1].screen[1], cube[5].screen[1], BLUE);
   _2d_line(cube[2].screen[1], cube[6].screen[1], BLUE);
   _2d_line(cube[3].screen[1], cube[7].screen[1], BLUE);

   blit(buffer, screen, 0, 0, 0, 0, SCREEN_W, SCREEN_H);
  }

 destroy_bitmap(buffer);
 return 0;
}
Пример #3
0
int main()
{
    int exit_flag = 0, i;

    VERTEX cube[8];
    cube[0].local = (_3D) {
        -10.0, -10.0, 0.0
    };
    cube[1].local = (_3D) {
        10.0, -10.0, 0.0
    };
    cube[2].local = (_3D) {
        10.0, 10.0, 0.0
    };
    cube[3].local = (_3D) {
        -10.0, 10.0, -10.0
    };

    cube[4].local = (_3D) {
        -10.0, -10.0, 10.0
    };
    cube[5].local = (_3D) {
        10.0, -10.0, 10.0
    };
    cube[6].local = (_3D) {
        10.0, 10.0, 10.0
    };
    cube[7].local = (_3D) {
        -10.0, 10.0, 10.0
    };

    _3D world_pos = {0.0, 0.0, 100.0};

    init();


    printf("D1:=%f;\nD2:=%f;\nD3:=%f;\nD4:=%f;\n", dist_3d(cube[0].local, cube[1].local),
           dist_3d(cube[1].local, cube[2].local),
           dist_3d(cube[2].local, cube[3].local),
           dist_3d(cube[3].local, cube[0].local));

    readkey();

    int xang, yang, zang;

    while(!exit_flag)
    {
        rest(10);

        xang = 0;
        yang = 0;
        zang = 0;

        if(keypressed())
        {
            if(key[KEY_ESC]) {
                exit_flag = 1;
            }
            if(key[KEY_A]) {
                world_pos.x -= 1.0;
            }
            if(key[KEY_D]) {
                world_pos.x += 1.0;
            }
            if(key[KEY_W]) {
                world_pos.z += 3.0;
            }
            if(key[KEY_S]) {
                world_pos.z -= 3.0;
            }
            if(key[KEY_UP]) {
                xang = 3;
            }
            if(key[KEY_DOWN]) {
                xang = -3;
            }
            if(key[KEY_RIGHT]) {
                yang = 3;
            }
            if(key[KEY_LEFT]) {
                yang = -3;
            }
        }

        clear_to_color(buffer, 0);

        for(i = 0; i < 8; i++)
        {
            rotate_vertex(&cube[i], xang, yang, zang);
            project_vertex(&cube[i], world_pos);
        }
        /*
            printf("%f, %f, %f\n", dist_3d(cube[0].local, cube[1].local),
                                 dist_3d(cube[1].local, cube[2].local),
                                 dist_3d(cube[2].local, cube[0].local));
        */

        printf("Sx1:=%f;\nSy1:=%f;\nSx2:=%f;\nSy2:=%f;\nSx3:=%f;\nSy3:=%f;\nSx4:=%f;\nSy4:=%f;\n", cube[0].screen[0].x, cube[0].screen[0].y,
               cube[1].screen[0].x, cube[1].screen[0].y,
               cube[2].screen[0].x, cube[2].screen[0].y, cube[3].screen[0].x, cube[3].screen[0].y);

        printf("t = %f\n\n\n\n\n\n", (cube[0].local.x + world_pos.x) / cube[0].screen[0].x);

        /*printf("{%f %f %f}\n", cube[0].local.x + world_pos.x,
         cube[0].local.y + world_pos.y, cube[0].local.z + world_pos.z);
        */

        _2d_line(cube[0].screen[0], cube[1].screen[0], RED);
        _2d_line(cube[1].screen[0], cube[2].screen[0], RED);
        _2d_line(cube[2].screen[0], cube[3].screen[0], RED);
        _2d_line(cube[3].screen[0], cube[0].screen[0], RED);

        /*
           _2d_line(cube[4].screen[0], cube[5].screen[0], RED);
           _2d_line(cube[5].screen[0], cube[6].screen[0], RED);
           _2d_line(cube[6].screen[0], cube[7].screen[0], RED);
           _2d_line(cube[7].screen[0], cube[4].screen[0], RED);

           _2d_line(cube[0].screen[0], cube[4].screen[0], RED);
           _2d_line(cube[1].screen[0], cube[5].screen[0], RED);
           _2d_line(cube[2].screen[0], cube[6].screen[0], RED);
           _2d_line(cube[3].screen[0], cube[7].screen[0], RED);
        */

        /*
           _2d_line(cube[0].screen[1], cube[1].screen[1], BLUE);
           _2d_line(cube[1].screen[1], cube[2].screen[1], BLUE);
           _2d_line(cube[2].screen[1], cube[3].screen[1], BLUE);
           _2d_line(cube[3].screen[1], cube[0].screen[1], BLUE);

           _2d_line(cube[4].screen[1], cube[5].screen[1], BLUE);
           _2d_line(cube[5].screen[1], cube[6].screen[1], BLUE);
           _2d_line(cube[6].screen[1], cube[7].screen[1], BLUE);
           _2d_line(cube[7].screen[1], cube[4].screen[1], BLUE);

           _2d_line(cube[0].screen[1], cube[4].screen[1], BLUE);
           _2d_line(cube[1].screen[1], cube[5].screen[1], BLUE);
           _2d_line(cube[2].screen[1], cube[6].screen[1], BLUE);
           _2d_line(cube[3].screen[1], cube[7].screen[1], BLUE);
        */

        blit(buffer, screen, 0, 0, 0, 0, SCREEN_W, SCREEN_H);
    }

    destroy_bitmap(buffer);
    return 0;
}
Пример #4
0
int main()
{
    int exit_flag = 0, i, j;
    MAT16F tmat;

    int vnum, node_num;
    VERTEX *map_vertex;
    STATIC_BSP_NODE *tree;

    init();

    load_static_bsp("map.bsp", &map_vertex, &vnum, &tree, &node_num);
    printf("Vertex Number: %d.\nNode Number: %d.\n", vnum, node_num);

    LOCK_VARIABLE(fps);
    LOCK_VARIABLE(frame_count);
    LOCK_FUNCTION(update_fps);
    install_int(update_fps, 1000);

    float frame = 0.0;

    VEC3F view_volume[5];
    VEC3F cam_pos = vec3f(0.0, 3.0, 0.0), cam_dir, cam_dir_normal, cam_ang = vec3f(0.0, 0.0, 0.0);

    while(!exit_flag)
    {
        int mx, my;
        get_mouse_mickeys(&mx, &my);
        position_mouse(SCREEN_W / 2, SCREEN_H / 2);

        cam_ang.x += my * 0.001;
        cam_ang.y -= mx * 0.001;
        cam_dir.x = CAM_SPEED * cos(0.5 * M_PI + cam_ang.y);
        cam_dir.y = CAM_SPEED * cos(0.5 * M_PI + cam_ang.x);
        cam_dir.z = CAM_SPEED * sin(0.5 * M_PI + cam_ang.y);
        cam_dir_normal = vec3f(-cam_dir.z, 0.0, cam_dir.x);

        if(key[KEY_ESC]) {
            exit_flag = 1;
        }
        if(key[KEY_W]) {
            cam_pos = VEC3F_SUM(cam_pos, cam_dir);
        }
        if(key[KEY_S]) {
            cam_pos = VEC3F_DIFF(cam_pos, cam_dir);
        }
        if(key[KEY_A]) {
            cam_pos = VEC3F_SUM(cam_pos, cam_dir_normal);
        }
        if(key[KEY_D]) {
            cam_pos = VEC3F_DIFF(cam_pos, cam_dir_normal);
        }

        set_fov(90.0);
        if(mouse_b > 1)
            set_fov(30.0);

        build_view_volume(view_volume);

        reset_mat16f(tmat);
        rotate_x_mat16f(tmat, cam_ang.x);
        rotate_y_mat16f(tmat, cam_ang.y);
        rotate_z_mat16f(tmat, 0.0);
        translate_mat16f(tmat, cam_pos.x, cam_pos.y, cam_pos.z);

        for(i = 0; i < 5; i++)
            transform_vec3f(&view_volume[i], view_volume[i], tmat);

        reset_mat16f(tmat);
        translate_mat16f(tmat, -cam_pos.x, -cam_pos.y, -cam_pos.z);
        rotate_z_mat16f(tmat, 0.0);
        rotate_y_mat16f(tmat, -cam_ang.y);
        rotate_x_mat16f(tmat, -cam_ang.x);

        for(i = 0; i < vnum; i++)
        {
            transform_vec3f(&map_vertex[i].trans, map_vertex[i].object, tmat);
            project_vertex(&map_vertex[i]);
        }

        for(i = 0; i < node_num; i++)
            tree[i].flag = 0;
        int checks = 0;
        mark_nodes_inside_volume(tree, 0, map_vertex, view_volume, 5, &checks);
        int count = 0;
        for(i = 0; i < node_num; i++)
            if(tree[i].flag == 1)
                count++;

        clear_to_color(buffer, 0);
        clear_to_color(BASE_INT_z_buffer, BASE_INT_z_buffer_precision);
        traverse_tree(tree, 0, cam_pos, map_vertex, -1);

        textprintf_ex(buffer, font, 10, 10, makecol(255, 255, 255), 0, "FPS: %d", fps);
        textprintf_ex(buffer, font, 10, 10, makecol(255, 255, 255), 0, "FPS: %d", fps);
        textprintf_ex(buffer, font, 10, 20, makecol(255, 255, 255), 0, "Rendered: %d of nodes, %d of faces.",
                      (int)((float)checks / (float)node_num * 100.0), (int)((float)count / (float)node_num * 100.0));
        //textprintf_ex(buffer, font, 10, 20, makecol(255, 255, 255), 0, "%d", checks);
        blit(buffer, screen, 0, 0, 0, 0, SCREEN_W, SCREEN_H);
        frame_count++;
    }

    deinit_engine();

    for(i = 0; i < node_num; i++)
        free(tree[i].poly.vind);
    free(tree);
    free(map_vertex);

    return 0;
}
Пример #5
0
int main()
{
 int exit_flag = 0, i;
 MAT16F tmat;
 VEC3F pos = vec3f(0.0, 0.0, 0.5), ang = vec3f(0.0, 0.0, 0.0);
 MD2_MODEL mdl;
 VERTEX *model_verts;
 TRI *model_tris;

 if(load_md2("data/babe.md2", &mdl))
  {
   allegro_message("Error: I need the MD2 model, stupid!");
   exit(1);
  }

 convert_md2_to_base(&model_verts, &model_tris, &mdl, 0.02);

 init();
 
 LOCK_VARIABLE(fps);
 LOCK_VARIABLE(frame_count);
 LOCK_FUNCTION(update_fps);
 install_int(update_fps, 1000);

 float frame = 0.0;
 int flag = 0, flag2 = 0;
 int anim_flag = 0, point_flag = 0;

 while(!exit_flag)
  {
   set_texture_mapping_mode(0);

   if(key[KEY_ESC]) { exit_flag = 1; }
   if(key[KEY_LEFT]) { pos.x -= 0.01; }
   if(key[KEY_RIGHT]) { pos.x += 0.01; }
   if(key[KEY_UP]) { pos.z += 0.02; }
   if(key[KEY_DOWN]) { pos.z -= 0.02; }
   if(key[KEY_SPACE]) { frame += 0.1; }
   if(key[KEY_P]) { anim_flag = anim_flag ? 0 : 1; key[KEY_P] = 0; }
   if(key[KEY_O]) { point_flag = point_flag ? 0 : 1; key[KEY_O] = 0; }

   VEC2F mp, cp;
   cp = vec2f(mouse_x, mouse_y);

   if(mouse_b == 1)
    {
     if(flag == 0)
       mp = cp;
     flag = 1;

     ang.y -= ((cp.x - mp.x) * 0.0002);
    }

   else
    flag = 0;

   if(mouse_b == 2)
    {
     if(flag2 == 0)
       mp = cp;
     flag2 = 1;

     pos.z -= ((cp.y - mp.y) * 0.0002);
    }

   else
    flag2 = 0;

   ang.x = M_PI * 1.5;

   convert_md2_frame_to_base(model_verts, &mdl, frame, 0.02);
   
   if(anim_flag)
   frame += 0.1 * (60.0 / (fps + 1.0));

   reset_mat16f(tmat);
   rotate_mat16f(tmat, ang.x, ang.y, ang.z);
   translate_mat16f(tmat, pos.x, pos.y, pos.z);

   for(i = 0; i < mdl.header.num_vertices; i++)
    {
     transform_vec3f(&model_verts[i].world, model_verts[i].local, tmat);
     project_vertex(&model_verts[i]);
    }

   clear_bitmap(buffer);
   clear_to_color(zbuffer, ZBUFFER_PRECISION);

   for(i = 0; i < mdl.header.num_tris; i++)
    {
     update_tri_normal(&model_tris[i], model_verts);
     frustum_cull(&model_tris[i], model_verts, -0.95, 20.0);
     cull_backface(&model_tris[i], model_verts);
    }

   for(i = 0; i < mdl.header.num_tris; i++)
    if(model_tris[i].vis)
     my_triangle(buffer, model_verts, model_tris[i]);

   if(point_flag)
   for(i = 0; i < mdl.header.num_tris; i++)
    if(model_tris[i].vis)
     {
      int x1 = model_verts[model_tris[i].a].sx, y1 = model_verts[model_tris[i].a].sy,
          x2 = model_verts[model_tris[i].b].sx, y2 = model_verts[model_tris[i].b].sy,
          x3 = model_verts[model_tris[i].c].sx, y3 = model_verts[model_tris[i].c].sy;

      float z, z1 = model_verts[model_tris[i].a].screen.z,
               z2 = model_verts[model_tris[i].b].screen.z,
               z3 = model_verts[model_tris[i].c].screen.z;

      if(on_bitmap(x1, y1, SCREEN_W, SCREEN_H))
       {
        z = (float)RI_PIXEL_Z(x1, y1) / (float)(ZBUFFER_PRECISION);
        if(z1 < z + 0.001) circlefill(buffer, x1, y1, 1, makecol(255, 0, 0));
       }

      if(on_bitmap(x2, y2, SCREEN_W, SCREEN_H))
       {
        z = (float)RI_PIXEL_Z(x2, y2) / (float)(ZBUFFER_PRECISION);
        if(z2 < z + 0.001) circlefill(buffer, x2, y2, 1, makecol(255, 0, 0));
       }

      if(on_bitmap(x3, y3, SCREEN_W, SCREEN_H))
       {
        z = (float)RI_PIXEL_Z(x3, y3) / (float)(ZBUFFER_PRECISION);
        if(z3 < z + 0.001) circlefill(buffer, x3, y3, 1, makecol(255, 0, 0));
       }
     }

   textprintf_ex(buffer, font, 10, 10, makecol(255, 255, 255), 0, "FPS: %d", fps);
   blit(buffer, screen, 0, 0, 0, 0, SCREEN_W, SCREEN_H);
   frame_count++;
  }

 free_md2(&mdl);
 free(model_tris);
 free(model_verts);
 destroy_bitmap(texture);
 destroy_bitmap(zbuffer);
 destroy_bitmap(buffer);
 deinit_renderer();

 return 0;
}
Пример #6
0
int main()
{
 int exit = 0, i, j;
 VEC3F pos = vec3f(0.0, 0.0, 1.0), ang = ZERO_VEC3F;
 MAT16F mat;

 int vnum, pnum;
 VERTEX *vertex;
 POLY3D *poly;

 init();

 load_map("quake_map.txt", &vertex, &poly, &vnum, &pnum, (1.0 / 1000.0));
 //printf("%d", poly[144].vind[0]);

 while(!exit)
  {
   if(key[KEY_ESC]) { exit = 1; }
   if(key[KEY_LEFT]) { pos.x -= MOTION_SPEED; }
   if(key[KEY_RIGHT]) { pos.x += MOTION_SPEED; }
   if(key[KEY_UP]) { pos.z += MOTION_SPEED; }
   if(key[KEY_DOWN]) { pos.z -= MOTION_SPEED; }

   ang.x += 0.01;
   ang.y += 0.01;
   ang.z += 0.01;

   reset_mat16f(mat);
   rotate_x_mat16f(mat, ang.x);
   rotate_y_mat16f(mat, ang.y);
   rotate_z_mat16f(mat, ang.z);
   translate_mat16f(mat, pos.x, pos.y, pos.z);

   for(i = 0; i < vnum; i++)
    {
     transform_vec3f(&vertex[i].trans, vertex[i].object, mat);
     project_vertex(&vertex[i]);
    }

   clear_to_color(buffer, 0);

/*
   for(i = 0; i < vnum; i++)
    if(vertex[i].trans.z > 0.01)
     putpixel(buffer, vertex[i].sx, vertex[i].sy, makecol(255, 255, 255));
*/

   for(i = 0; i < pnum; i++)
    {
     for(j = 0; j < poly[i].vnum / 3; j++)
      {
     line(buffer, vertex[poly[i].vind[j * 3]].sx, vertex[poly[i].vind[j * 3]].sy,
                  vertex[poly[i].vind[j * 3 + 1]].sx, vertex[poly[i].vind[j * 3 + 1]].sy,
                  makecol(255, 255, 255));
     line(buffer, vertex[poly[i].vind[j * 3 + 1]].sx, vertex[poly[i].vind[j * 3 + 1]].sy,
                  vertex[poly[i].vind[j * 3 + 2]].sx, vertex[poly[i].vind[j * 3 + 2]].sy,
                  makecol(255, 255, 255));
     line(buffer, vertex[poly[i].vind[j * 3 + 2]].sx, vertex[poly[i].vind[j * 3 + 2]].sy,
                  vertex[poly[i].vind[j * 3]].sx, vertex[poly[i].vind[j * 3]].sy,
                  makecol(255, 255, 255));
      }
    }

   blit(buffer, screen, 0, 0, 0, 0, SCREEN_W, SCREEN_H);
  }

 for(i = 0; i < pnum; i++)
  free(poly[i].vind);
 free(poly);
 free(vertex);
 deinit_engine();
 return 0;
}
Пример #7
0
int main()
{
 int exit = 0, i, vnum, pnum;
 VEC3F pos = vec3f(0.0, 0.0, 0.3), ang = ZERO_VEC3F;
 MAT16F tmat;
 VERTEX *vertex;
 POLY3D *poly;

 init();

 load_map("quake_map2.txt", &vertex, &poly, &vnum, &pnum, 0.2);

 LOCK_VARIABLE(fps);
 LOCK_VARIABLE(frame_count);
 LOCK_FUNCTION(update_fps);
 install_int(update_fps, 1000);

 VEC3F cam_pos = vec3f(0.0, 3.0, 0.0), cam_dir, cam_dir_normal, cam_ang = vec3f(0.0, 0.0, 0.0);

 while(!exit)
  {
   int mx, my;
   get_mouse_mickeys(&mx, &my);
   position_mouse(SCREEN_W / 2, SCREEN_H / 2);

   cam_ang.x += my * 0.001;
   cam_ang.y -= mx * 0.001;
   cam_dir.x = CAM_SPEED * cos(0.5 * M_PI + cam_ang.y);
   cam_dir.y = CAM_SPEED * cos(0.5 * M_PI + cam_ang.x);
   cam_dir.z = CAM_SPEED * sin(0.5 * M_PI + cam_ang.y);
   cam_dir_normal = vec3f(-cam_dir.z, 0.0, cam_dir.x);

   if(key[KEY_ESC]) { exit = 1; }
   if(key[KEY_W]) { cam_pos = VEC3F_SUM(cam_pos, cam_dir); }
   if(key[KEY_S]) { cam_pos = VEC3F_DIFF(cam_pos, cam_dir); }
   if(key[KEY_A]) { cam_pos = VEC3F_SUM(cam_pos, cam_dir_normal); }
   if(key[KEY_D]) { cam_pos = VEC3F_DIFF(cam_pos, cam_dir_normal); }

   set_fov(90.0);
   if(mouse_b > 1)
    set_fov(30.0);

   reset_mat16f(tmat);
   translate_mat16f(tmat, -cam_pos.x, -cam_pos.y, -cam_pos.z);
   rotate_z_mat16f(tmat, 0.0);
   rotate_y_mat16f(tmat, -cam_ang.y);
   rotate_x_mat16f(tmat, -cam_ang.x);

   for(i = 0; i < vnum; i++)
    {
     transform_vec3f(&vertex[i].trans, vertex[i].object, tmat);
     project_vertex(&vertex[i]);
    }

   clear_to_color(buffer, 0);
   clear_to_color(BASE_INT_z_buffer, BASE_INT_z_buffer_precision);

   for(i = 0; i < pnum; i++)
    {
     update_poly3d_normal(&poly[i], vertex);
     V0 = vertex[poly[i].vind[0]].trans;
     global_n = USCALE_VEC3F(NORMALIZED_VEC3F(poly[i].normal), -1.0);
     VEC3F N = global_n;
     DIST = -VEC3F_DOT_PRODUCT(V0, N);

     get_axes(vertex[poly[i].vind[0]].trans,
              vertex[poly[i].vind[1]].trans,
              vertex[poly[i].vind[2]].trans,
              to_vec3f(poly[i].texcoord[0]),
              to_vec3f(poly[i].texcoord[1]),
              to_vec3f(poly[i].texcoord[2]), &A, &B, &C);
     T0 = to_vec3f(poly[i].texcoord[0]);

     if(cull_backface(&poly[i], vertex))
     render_poly3d(&poly[i], vertex);
    }

   textprintf_ex(buffer, font, 10, 10, makecol(255, 255, 255), 0, "FPS: %d", fps);
   blit(buffer, screen, 0, 0, 0, 0, SCREEN_W, SCREEN_H);
   frame_count++;
  }

 for(i = 0; i < pnum; i++)
  destroy_poly3d(&poly[i]);
 free(vertex);

 deinit_engine();
 return 0;
}
Пример #8
0
int main()
{
 int exit = 0, i;
 VEC3F pos = vec3f(0.0, 0.0, 0.3), ang = ZERO_VEC3F;
 MAT16F mat;
 VERTEX cube_vertex[8];
 POLY3D cube_poly[12];

 init();

 cube_vertex[0].object = vec3f(-CUBE_SIZE, CUBE_SIZE, -CUBE_SIZE);
 cube_vertex[1].object = vec3f(CUBE_SIZE, CUBE_SIZE, -CUBE_SIZE);
 cube_vertex[2].object = vec3f(CUBE_SIZE, -CUBE_SIZE, -CUBE_SIZE);
 cube_vertex[3].object = vec3f(-CUBE_SIZE, -CUBE_SIZE, -CUBE_SIZE);
 cube_vertex[4].object = vec3f(-CUBE_SIZE, CUBE_SIZE, CUBE_SIZE);
 cube_vertex[5].object = vec3f(CUBE_SIZE, CUBE_SIZE, CUBE_SIZE);
 cube_vertex[6].object = vec3f(CUBE_SIZE, -CUBE_SIZE, CUBE_SIZE);
 cube_vertex[7].object = vec3f(-CUBE_SIZE, -CUBE_SIZE, CUBE_SIZE);

 create_poly3d(&cube_poly[0], random_color(), 3, 0, 1, 2);
 create_poly3d(&cube_poly[1], random_color(), 3, 2, 3, 0);
 create_poly3d(&cube_poly[2], random_color(), 3, 7, 6, 5);
 create_poly3d(&cube_poly[3], random_color(), 3, 5, 4, 7);


/*
 create_poly3d(&cube_poly[1], random_color(), 4, 7, 6, 5, 4);
 create_poly3d(&cube_poly[2], random_color(), 4, 4, 5, 1, 0);
 create_poly3d(&cube_poly[3], random_color(), 4, 6, 7, 3, 2);
 create_poly3d(&cube_poly[4], random_color(), 4, 0, 3, 7, 4);
 create_poly3d(&cube_poly[5], random_color(), 4, 5, 6, 2, 1);
*/
 LOCK_VARIABLE(fps);
 LOCK_VARIABLE(frame_count);
 LOCK_FUNCTION(update_fps);
 install_int(update_fps, 1000);

 while(!exit)
  {
   if(key[KEY_ESC]) { exit = 1; }
   if(key[KEY_LEFT]) { pos.x -= MOTION_SPEED; }
   if(key[KEY_RIGHT]) { pos.x += MOTION_SPEED; }
   if(key[KEY_UP]) { pos.z += MOTION_SPEED; }
   if(key[KEY_DOWN]) { pos.z -= MOTION_SPEED; }

   ang.x += 0.01;
   ang.y += 0.01;
   ang.z += 0.01;

   reset_mat16f(mat);
   rotate_x_mat16f(mat, ang.x);
   rotate_y_mat16f(mat, ang.y);
   rotate_z_mat16f(mat, ang.z);
   translate_mat16f(mat, pos.x, pos.y, pos.z);

   for(i = 0; i < 8; i++)
    {
     transform_vec3f(&cube_vertex[i].trans, cube_vertex[i].object, mat);
     project_vertex(&cube_vertex[i]);
    }

   clear_to_color(buffer, 0);
   clear_to_color(BASE_INT_z_buffer, BASE_INT_z_buffer_precision);

   for(i = 0; i < 4; i++)
    {
     update_poly3d_normal(&cube_poly[i], cube_vertex);
     if(!cull_backface(&cube_poly[i], cube_vertex))
     render_poly3d(&cube_poly[i], cube_vertex);
    }

   textprintf_ex(buffer, font, 10, 10, makecol(255, 255, 255), 0, "FPS: %d", fps);
   blit(buffer, screen, 0, 0, 0, 0, SCREEN_W, SCREEN_H);
   frame_count++;
  }

 for(i = 0; i < 6; i++)
  destroy_poly3d(&cube_poly[i]);

 deinit_engine();
 return 0;
}