c++ - Missing faces when displaying STL data -
i wrote simple parser ascii stl format. when try render triangles supplied normals, resulting object missing many faces:
this how should like:
what tried:
- explicitly disabled backface culling (though shouldn't have been active before)
- ensured depth buffer enabled
here minimal sample program reproduces error:
#include <sdl2/sdl.h> #include <sdl2/sdl_main.h> #include <sdl2/sdl_render.h> #include <sdl2/sdl_opengl.h> int main(int argc, char **argv) { sdl_init(sdl_init_video); int screen_w=1280,screen_h=720; sdl_window * win = sdl_createwindow("test", 20, 20, screen_w, screen_h, sdl_window_opengl); sdl_glcontext glcontext = sdl_gl_createcontext(win); stlparser stlparser; std::ifstream file(".\\logo.stl"); stlparser.parseascii(file); const auto& ndata = stlparser.getndata(); const auto& vdata = stlparser.getvdata(); std::cout << "number of facets: " << ndata.size() << std::endl; sdl_gl_setattribute(sdl_gl_doublebuffer, 1); glmatrixmode(gl_projection | gl_modelview); glloadidentity(); glscalef(1.f, -1.f, 1.f); glortho(0, screen_w, 0, screen_h, -screen_w, screen_w); glcleardepth(1.0f); gldepthfunc(gl_lequal); glenable(gl_depth_test); gldisable(gl_cull_face); glenableclientstate(gl_normal_array); glenableclientstate(gl_vertex_array); glnormalpointer(gl_float, 0, ndata.data()); glvertexpointer(3, gl_float, 0, vdata.data()); sdl_event event; bool quit = false; while (!quit) { while (sdl_pollevent(&event)) switch(event.type) { case sdl_quit: quit = true; break; } ; // drawing glclearcolor(255,255,255,255); glclear(gl_color_buffer_bit | gl_depth_buffer_bit); gltranslatef(screen_w/2,0,0); glrotatef(0.5,0,1,0); gltranslatef(-screen_w/2,0,0); glpushmatrix(); gltranslatef(screen_w/2,screen_h/2,0); glcolor3f(0.5,0.5,0); gldrawarrays(gl_triangles, 0, vdata.size()); glpopmatrix(); sdl_gl_swapwindow(win); sdl_delay(10); } sdl_destroywindow(win); sdl_quit(); return 0; }
the stlparser methods getndata()
, getvdata()
have following signatures:
const std::vector<std::array<float,3>>& getndata() const; const std::vector<std::array<std::array<float,3>,3>>& getvdata() const;
stlparser output should correct, can provide sources if needed.
what doing wrong?
you should change
gldrawarrays(gl_triangles, 0, vdata.size());
to
gldrawarrays(gl_triangles, 0, 3 * vdata.size());
i.e. count
should vertex count, not triangle count.
Comments
Post a Comment