void subdivide (std::vector<vector>& vertices, std::vector<triangle>& triangles) { std::vector<triangle> new_tris; for (const triangle& tri : triangles) { unsigned int a(tri[0]), b(tri[1]), c(tri[2]); unsigned int d, e, f; vector n1 (normalize(halfway(vertices[a], vertices[b]))); auto f1 (std::find (vertices.begin(), vertices.end(), n1)); if (f1 == vertices.end()) { d = vertices.size(); vertices.push_back(n1); } else { d = std::distance(vertices.begin(), f1); } vector n2 (normalize(halfway(vertices[b], vertices[c]))); auto f2 (std::find (vertices.begin(), vertices.end(), n2)); if (f2 == vertices.end()) { e = vertices.size(); vertices.push_back(n2); } else { e = std::distance(vertices.begin(), f2); } vector n3 (normalize(halfway(vertices[c], vertices[a]))); auto f3 (std::find (vertices.begin(), vertices.end(), n3)); if (f3 == vertices.end()) { f = vertices.size(); vertices.push_back(n3); } else { f = std::distance(vertices.begin(), f3); } new_tris.push_back(make_triangle(a, d, f)); new_tris.push_back(make_triangle(d, b, e)); new_tris.push_back(make_triangle(f, e, c)); new_tris.push_back(make_triangle(d, e, f)); } triangles.swap(new_tris); }
/** Create a list of surfaces. */ list356_t* get_surfaces() { debug("get_surfaces()") ; // Spheres along the negative x, y, and z axes. list356_t* surfaces = make_list() ; list356_t* bbt_surfaces = make_list() ; surface_t* z_sphere ; surface_t* green_sphere; for (float x=0.0; x>=-400.0; x-=1.0) { lst_add(bbt_surfaces, (green_sphere = make_sphere(x, 0.0, 0.0f, 1.0f, &GREEN, &GREEN, &WHITE, 100.0f))) ; // green_sphere->refl_color = &LIGHT_GREY; lst_add(bbt_surfaces, make_sphere(0.0f, x, 0.0f, .25, &PURPLE, &PURPLE, &WHITE, 100.0f)) ; lst_add(bbt_surfaces, (z_sphere = make_sphere(0.0f, 0.0f, x, .25, &PURPLE, &PURPLE, &WHITE, 100.0f))) ; z_sphere->refl_color = &LIGHT_GREY ; } lst_add(bbt_surfaces, make_sphere(-9.0f, 1.0f, -9.0f, 2.0f, &WHITE, &WHITE, &WHITE, 90.0f)); lst_add(bbt_surfaces, make_sphere(-9.0f, 1.0f, -8.0f, 2.0f, &WHITE, &WHITE, &WHITE, 90.0f)); lst_add(bbt_surfaces, make_sphere(-9.0f, 1.0f, -6.0f, 2.0f, &WHITE, &WHITE, &WHITE, 90.0f)); // A rectangle in the y=-1 plane. surface_t* tri1 = make_triangle( (point3_t){-40, -1, 2}, (point3_t){2, -1, 2}, (point3_t){2, -1, -20}, &LIGHT_GREY, &LIGHT_GREY, &BLACK, 10.0f) ; surface_t* tri2 = make_triangle( (point3_t){-40, -1, 2}, (point3_t){2, -1, -20}, (point3_t){-40, -1, -20}, &LIGHT_GREY, &LIGHT_GREY, &BLACK, 10.0f) ; tri1->refl_color = &DARK_GREY ; tri2->refl_color = &DARK_GREY ; lst_add(surfaces, tri1) ; lst_add(surfaces, tri2) ; // An infinite plane in the y=-2 plane. /* surface_t* plane = make_plane( (point3_t){-40, -2, 2}, (point3_t){2, -2, 2}, (point3_t){2, -2, -20}, &RED, &RED, &BLACK, 10.0f); plane->refl_color = &DARK_GREY; lst_add(surfaces, plane); */ lst_add(surfaces, make_bbt_node(bbt_surfaces)); return surfaces ; }
Dart_handle make_tetrahedral(CMap_4& amap) { Dart_handle d1 = make_triangle(amap); Dart_handle d2 = make_triangle(amap); Dart_handle d3 = make_triangle(amap); Dart_handle d4 = make_triangle(amap); amap.link_beta<2>(d1, d2); amap.link_beta<2>(d3, d2->beta(0)); amap.link_beta<2>(d1->beta(1), d3->beta(0)); amap.link_beta<2>(d4, d2->beta(1)); amap.link_beta<2>(d4->beta(0), d3->beta(1)); amap.link_beta<2>(d4->beta(1), d1->beta(0)); return d1; }
/** Create a list of surfaces. */ list356_t* get_surfaces() { debug("get_surfaces()") ; // Spheres along the negative x, y, and z axes. list356_t* surfaces = make_list() ; surface_t* z_sphere ; for (float x=0.0; x>=-40.0; x-=3.0) { lst_add(surfaces, make_sphere(x, 0.0, 0.0f, 1.0f, &GREEN, &GREEN, &WHITE, 100.0f)) ; lst_add(surfaces, make_sphere(0.0f, x, 0.0f, .25, &PURPLE, &PURPLE, &WHITE, 100.0f)) ; lst_add(surfaces, (z_sphere = make_sphere(0.0f, 0.0f, x, .25, &PURPLE, &PURPLE, &WHITE, 100.0f))) ; z_sphere->refl_color = &LIGHT_GREY ; } // A rectangle in the y=-1 plane. surface_t* tri1 = make_triangle( (point3_t){-40, -1, 2}, (point3_t){2, -1, 2}, (point3_t){2, -1, -20}, &LIGHT_GREY, &LIGHT_GREY, &BLACK, 10.0f) ; surface_t* tri2 = make_triangle( (point3_t){-40, -1, 2}, (point3_t){2, -1, -20}, (point3_t){-40, -1, -20}, &LIGHT_GREY, &LIGHT_GREY, &BLACK, 10.0f) ; tri1->refl_color = &DARK_GREY ; tri2->refl_color = &DARK_GREY ; lst_add(surfaces, tri1) ; lst_add(surfaces, tri2) ; return surfaces ; }
void draw_triangles() { // ----> 向右是 x轴的正方向 // 向上是 y 轴正方向 auto v1 = make_vertex(0, 0, 0.f); auto v2 = make_vertex(640, 0, 0.f); auto v3 = make_vertex(0, 480, 0.f); auto triangle = make_triangle(v1,v2,v3); glEnableClientState(GL_VERTEX_ARRAY); glColor4f(1,0,0,1); glVertexPointer(3,GL_FLOAT,0,&triangle); glDrawArrays(GL_TRIANGLES,0,3); glDisableClientState(GL_VERTEX_ARRAY); }
skydome (float size = 1, unsigned int subdiv = 2) { vertices_.push_back(vector(0, 0, size)); vertices_.push_back(vector(size , 0 , 0)); vertices_.push_back(vector(0 , size, 0)); vertices_.push_back(vector(-size, 0 , 0)); vertices_.push_back(vector(0 ,-size, 0)); vertices_.push_back(vector(0, 0, -size)); triangles_.push_back(make_triangle(0, 1, 2)); triangles_.push_back(make_triangle(0, 2, 3)); triangles_.push_back(make_triangle(0, 3, 4)); triangles_.push_back(make_triangle(0, 4, 1)); triangles_.push_back(make_triangle(5, 1, 2)); triangles_.push_back(make_triangle(5, 2, 3)); triangles_.push_back(make_triangle(5, 3, 4)); triangles_.push_back(make_triangle(5, 4, 1)); for (unsigned int i (0); i < subdiv; ++i) subdivide(vertices_, triangles_); }
/** Create a list of surfaces. */ list356_t* get_surfaces() { list356_t* surfaces = make_list() ; // Table. point3_t vertices[8] = { {0, 0, 1}, {8, 0, 1}, {0, 8, 1}, {8, 8, 1}, {0, 0, -1}, {8, 0, -1}, {0, 8, -1}, {8, 8, -1}, } ; int indices[] = { 0, 1, 3, 0, 3, 2, // top 0, 2, 6, 0, 6, 4, // left 4, 6, 7, 4, 7, 5, // bottom 1, 5, 7, 1, 7, 3, // right 2, 3, 7, 2, 7, 6, // back 0, 4, 5, 0, 5, 1 // front } ; int top_offset = 6 ; int offset = 36 ; list356_t* table_surfaces = make_list() ; for (int i=0; i<top_offset/3; ++i) { lst_add(table_surfaces, make_triangle( vertices[indices[3*i]], vertices[indices[3*i+1]], vertices[indices[3*i+2]], &RED, &RED, &WHITE, 10.0f)) ; } for (int i=top_offset/3; i<offset/3; ++i) { lst_add(table_surfaces, make_triangle( vertices[indices[3*i]], vertices[indices[3*i+1]], vertices[indices[3*i+2]], &GREEN, &GREEN, &WHITE, 10.0f)) ; } // Two purple spheres. lst_add(table_surfaces, make_sphere(6, 6, 1.75+.01, .75, &PURPLE, &PURPLE, &WHITE, 100.0f)) ; lst_add(table_surfaces, make_sphere(5, 2, 1.75+.01, .75, &PURPLE, &PURPLE, &WHITE, 100.0f)) ; // Transparent cube. point3_t cube_vertices[] = { {4, 0, 3}, {5, 0, 3}, {4, 1, 3}, {5, 1, 3}, {4, 0, 1.01}, {5, 0, 1.01}, {4, 1, 1.01}, {5, 1, 1.01}, } ; for (int i=0; i<offset/3; ++i) { surface_t* t = make_triangle( cube_vertices[indices[3*i]], cube_vertices[indices[3*i+1]], cube_vertices[indices[3*i+2]], &BLACK, &BLACK, &WHITE, 10.0f) ; t->refr_index = 1.1f ; t->atten = &GREENISH ; lst_add(surfaces, t) ; } list356_itr_t* itr = lst_iterator(table_surfaces) ; while (lst_has_next(itr)) lst_add(surfaces, lst_next(itr)) ; lst_free(table_surfaces) ; // Plane at z=-1. surface_t* plane = make_plane( (point3_t){0, 0, -1}, (point3_t){1, 0, -1}, (point3_t){1, 1, -1}, &LIGHT_GREY, &LIGHT_GREY, &BLACK, 10.0f) ; plane->refl_color = &LIGHT_GREY ; lst_add(surfaces, plane) ; return surfaces ; }
void rg(list356_t* surfaces, point3_t* eye, point3_t* look_at) { //update eye and look_at positions point3_t eye_position = {4.0f, -4.0f, 8.0f} ; point3_t look_at_point = {4.0f, 4.0f, 1.0f} ; *eye = eye_position; *look_at = look_at_point; // Chess board. list356_t* board_surfaces = make_list() ; // All the vertices. point3_t vertices[85] ; for (int x=0; x<9; ++x) { for (int y=0; y<9; ++y) { vertices[9*y+x] = (point3_t){x, y, 1} ; } } vertices[81] = (point3_t){0, 0, -1} ; vertices[82] = (point3_t){8, 0, -1} ; vertices[83] = (point3_t){0, 8, -1} ; vertices[84] = (point3_t){8, 8, -1} ; int indices[138*3] ; int offset = 0 ; // Top of chess board. for (int x=0; x<8; ++x) { for (int y=0; y<8; ++y) { indices[offset++] = 9*y+x ; indices[offset++] = 9*y+x+1 ; indices[offset++] = 9*(y+1)+x+1 ; indices[offset++] = 9*y+x ; indices[offset++] = 9*(y+1)+x+1 ; indices[offset++] = 9*(y+1)+x ; } } int top_offset = offset ; // Sides and bottom. indices[offset++] = 81 ; indices[offset++] = 82; indices[offset++] = 8 ; indices[offset++] = 81 ; indices[offset++] = 8; indices[offset++] = 0 ; indices[offset++] = 8 ; indices[offset++] = 82 ; indices[offset++] = 84 ; indices[offset++] = 8 ; indices[offset++] = 84 ; indices[offset++] = 80 ; indices[offset++] = 72 ; indices[offset++] = 80 ; indices[offset++] = 84 ; indices[offset++] = 72 ; indices[offset++ ] = 84 ; indices[offset++] = 83 ; indices[offset++] = 0 ; indices[offset++] = 72 ; indices[offset++] = 83 ; indices[offset++] = 0 ; indices[offset++] = 83 ; indices[offset++] = 81 ; indices[offset++] = 81 ; indices[offset++] = 83 ; indices[offset++] =84 ; indices[offset++] = 81 ; indices[offset++] = 84 ; indices[offset++] = 82 ; debug("get_surfaces(): final offset = %d", offset) ; // Top as triangles. for (int i=0; i<top_offset/3; ++i) { int c = (i/2)%8 ; int r = (i/2)/8 ; int j = r+c ; color_t* color = j%2 == 0 ? &RED : &BLACK ; lst_add(board_surfaces, make_triangle( vertices[indices[3*i]], vertices[indices[3*i+1]], vertices[indices[3*i+2]], color, color, &WHITE, 10.0f)) ; } // Sides and bottom at triangles. for (int i=top_offset/3; i<offset/3; ++i) { lst_add(board_surfaces, make_triangle( vertices[indices[3*i]], vertices[indices[3*i+1]], vertices[indices[3*i+2]], &LIGHT_GREY, &LIGHT_GREY, &WHITE, 10.0f)) ; } //// "Pieces" //for (int c=0; c<8; ++c) { // for (int r=0; r<2; ++r) { // surface_t* s = make_sphere(c+.5, r+.5, 1.375+.01, .375, // &BLACK, &BLACK, &WHITE, 100.0f) ; // lst_add(board_surfaces, s) ; // } // //for (int r=6; r<8; ++r) { // // lst_add(board_surfaces, make_sphere(c+.5, r+.5, 1.25, .25, // // &WHITE, &WHITE, &WHITE, 100.0f)) ; // //} //} // Draw R manually // Define squares to place spheres on void add_sphere(c,r) { lst_add(board_surfaces, make_sphere(c+.5, r+.5, 1.75, .50, &GREEN, &GREEN, &BLACK, 150.0f)) ; }