opengl - Unexpected projection -
it should rect, takes whole width of window. expecting this, because frustum width -5 5 , rect size 10x10 , rect on same z-axis position near plnane of frustum. result small , doesn't rect, don't know why?
void glwidget::initializegl() { glclearcolor(1.0f, 1.0f, 1.0f, 0.0f); } void glwidget::resizegl(int w, int h) { glviewport(0,0, w, h); glmatrixmode(gl_projection); double ratio = (double)w/(double)h; double size = ((10.0/ratio)/2.0); glfrustum(-5.0, 5.0, -size, size, 10.0, 50.0); } void glwidget::paintgl() { glclear(gl_color_buffer_bit | gl_depth_buffer_bit); glmatrixmode(gl_modelview); glloadidentity(); gltranslatef(0.0f, 0.0f, -10.0f); glcolor3f(0.0f, 0.0f, 0.0f); glbegin(gl_quads); glvertex3f(5.0f, -5.0f, 0.0f); glvertex3f(5.0f, 5.0f, 0.0f); glvertex3f(-5.0f, 5.0f, 0.0f); glvertex3f(-5.0f, -5.0f, 0.0f); glend(); glflush(); }
it should rect, takes whole width of window.
well, switch projection better suited, ortho projection, task when drawing rect. must reset projection matrix identity before applying frustum or ortho.
hint: code in resizegl
should go paintgl
method. see you're using qt, can widget's width , height conveniently using width
, height
getter functions. glclearcolor
goes paingl
well:
void glwidget::initializegl() { } void glwidget::resizegl(int w, int h) { } void glwidget::paintgl() { double const ratio = (double)width()/(double)height(); double const size = ((10.0/ratio)/2.0); glclearcolor(1.0f, 1.0f, 1.0f, 0.0f); glclear(gl_color_buffer_bit | gl_depth_buffer_bit); glviewport(0,0, width(), height()); glmatrixmode(gl_projection); glloadidentity(); glfrustum(-5.0, 5.0, -size, size, 10.0, 50.0); glmatrixmode(gl_modelview); glloadidentity(); gltranslatef(0.0f, 0.0f, -10.0f); draw_perspective_stuff(); glviewport(0,0, width(), height()); glmatrixmode(gl_projection); glloadidentity(); glmatrixmode(gl_modelview); glloadidentity(); // using identity projection, fills viewport glcolor3f(0.0f, 0.0f, 0.0f); glbegin(gl_quads); glvertex3f(-1.f, -1.f, 0.0f); glvertex3f( 1.f, -1.f, 0.0f); glvertex3f( 1.f, 1.f, 0.0f); glvertex3f(-1.f, 1.f, 0.0f); glend(); glflush(); }
Comments
Post a Comment