Mesh of Box

21:16
Mon
21
Jan 2013

Mesh of Box

Too many times I had to come up with triangle mesh of a box to hardcode it in my program, written just from memory or with help of a sheet of paper. It's easy to make a mistake and have a box with one face missing or something like that. So in case me or somebody in the future will need it, here it is. Parameters:

Box spanning from (-1, -1, -1) to (+1, +1, +1). Contains 3D positions and normals. Topology is triangle strip, using strip-cut index. Backface culling can be used, front faces are clockwise (using Direct3D coordinate system).

// H file

struct SVertex {
    vec3 Position;
    vec3 Normal;
};

const size_t BOX_VERTEX_COUNT = 6 * 4;
const size_t BOX_INDEX_COUNT  = 6 * 5;
extern const SVertex BOX_VERTICES[];
extern const uint16_t BOX_INDICES[];

// CPP file

const SVertex BOX_VERTICES[] = {
    // -X
    { vec3(-1.f, -1.f,  1.f), vec3(-1.f,  0.f,  0.f) },
    { vec3(-1.f,  1.f,  1.f), vec3(-1.f,  0.f,  0.f) },
    { vec3(-1.f, -1.f, -1.f), vec3(-1.f,  0.f,  0.f) },
    { vec3(-1.f,  1.f, -1.f), vec3(-1.f,  0.f,  0.f) },
    // -Z
    { vec3(-1.f, -1.f, -1.f), vec3( 0.f,  0.f, -1.f) },
    { vec3(-1.f,  1.f, -1.f), vec3( 0.f,  0.f, -1.f) },
    { vec3( 1.f, -1.f, -1.f), vec3( 0.f,  0.f, -1.f) },
    { vec3( 1.f,  1.f, -1.f), vec3( 0.f,  0.f, -1.f) },
    // +X
    { vec3( 1.f, -1.f, -1.f), vec3( 1.f,  0.f,  0.f) },
    { vec3( 1.f,  1.f, -1.f), vec3( 1.f,  0.f,  0.f) },
    { vec3( 1.f, -1.f,  1.f), vec3( 1.f,  0.f,  0.f) },
    { vec3( 1.f,  1.f,  1.f), vec3( 1.f,  0.f,  0.f) },
    // +Z
    { vec3( 1.f, -1.f,  1.f), vec3( 0.f,  0.f,  1.f) },
    { vec3( 1.f,  1.f,  1.f), vec3( 0.f,  0.f,  1.f) },
    { vec3(-1.f, -1.f,  1.f), vec3( 0.f,  0.f,  1.f) },
    { vec3(-1.f,  1.f,  1.f), vec3( 0.f,  0.f,  1.f) },
    // -Y
    { vec3(-1.f, -1.f,  1.f), vec3( 0.f, -1.f,  0.f) },
    { vec3(-1.f, -1.f, -1.f), vec3( 0.f, -1.f,  0.f) },
    { vec3( 1.f, -1.f,  1.f), vec3( 0.f, -1.f,  0.f) },
    { vec3( 1.f, -1.f, -1.f), vec3( 0.f, -1.f,  0.f) },
    // +Y
    { vec3(-1.f,  1.f, -1.f), vec3( 0.f,  1.f,  0.f) },
    { vec3(-1.f,  1.f,  1.f), vec3( 0.f,  1.f,  0.f) },
    { vec3( 1.f,  1.f, -1.f), vec3( 0.f,  1.f,  0.f) },
    { vec3( 1.f,  1.f,  1.f), vec3( 0.f,  1.f,  0.f) },
};

const uint16_t BOX_INDICES[] = {
     0,  1,  2,  3, 0xFFFF, // -X
     4,  5,  6,  7, 0xFFFF, // -Z
     8,  9, 10, 11, 0xFFFF, // +X
    12, 13, 14, 15, 0xFFFF, // +Z
    16, 17, 18, 19, 0xFFFF, // -Y
    20, 21, 22, 23, 0xFFFF, // +Y
};

Comments (1) | Tags: rendering | Author: Adam Sawicki | Share

Comments

yll
2016-03-25 03:32:07
https://www.google.co.jp/webhp?sourceid=chrome-instant&ion=1&espv=2&ie=UTF-8#q=Comments+site:http://asawicki.info/&start=20
http://asawicki.info/news_1340_a_reminder_about_my_links.html?x=viewnews&id=1340
http://asawicki.info/news_1435_visual_studio_2010_service_pack_1_released_yesterday.html?x=viewnews&id=1435
http://asawicki.info/news_1585_my_lecture_on_careercon.html?x=viewnews&id=1585
http://asawicki.info/news_1606_unicode_w_visual_c_-_my_old_article.html?x=viewnews&id=1606
http://asawicki.info/news_1526_sbx_-_scale-bias_transform.html?x=viewnews&id=1526
http://asawicki.info/news_1611_type_safe_ptr_-_idea_for_type-safe_void_pointer.html?x=viewnews&id=1611
http://asawicki.info/news_1487_noconsole_-_my_c_project.html?x=viewnews&id=1487
http://asawicki.info/news_1370_origami_sonobe_module.html?x=viewnews&id=1370
http://asawicki.info/news_1336_generating_and_compressing_avi_video_files.html?x=viewnews&id=1336
http://asawicki.info/news_1322_fractals_ifs_and_a_fern.html?x=viewnews&id=1322
http://asawicki.info/news_1465_handling_ctrlc_in_windows_console_application.html?x=viewnews&id=1465
http://asawicki.info/news_1612_printstream_20_-_polymorphic_printf.html?x=viewnews&id=1612
http://asawicki.info/news_1579_writing_ofx_video_processing_plugin.html?x=viewnews&id=1579
http://asawicki.info/news_1584_psychill_evening_-_my_first_music_visualizations.html?x=viewnews&id=1584
http://asawicki.info/news_1538_type_visualization_in_visual_studio_2012_debugger.html?x=viewnews&id=1538
http://asawicki.info/news_1609_poznan_game_arena_game_industry_conference_2015.html?x=viewnews&id=1609

Post comment

Nick *
Your name or nickname
E-mail
Your contact information (optional, will not be shown)
Text *
Content of your comment
Calculate *
(* - required field)
STAT NO AD [Stat] [Admin] [STAT NO AD] [pub] [Mirror] Copyright © 2004-2017 Adam Sawicki
Copyright © 2004-2017 Adam Sawicki