c++ - 3D cube appears stretched when rendered -
i have basic opengl project draws 3d cube screen. resolution 1600 x 900. no matter how far or close camera cube, cube appears stretched horizontally triangle. i've loaded same cube java project , draws perfectly. same code in c++, stretched effect. suggestions may causing this?
glvoid rendersettings::init() { const glfloat fov = 60.0f; const glfloat aspect_ratio = getscreenwidth() / getscreenheight(); const glfloat nearest_view = 1.0f; const glfloat farthest_view = 1000.0f; glviewport(0, 0, getscreenwidth(), getscreenheight()); glmatrixmode(gl_projection); glloadidentity(); gluperspective(fov, aspect_ratio, nearest_view, farthest_view); glclearcolor(0.0, 0.0, 0.0, 1.0); glenable(gl_depth_test); glenable(gl_cull_face); gldepthfunc(gl_lequal); glmatrixmode(gl_modelview); glloadidentity(); }
this
const glfloat aspect_ratio = getscreenwidth() / getscreenheight(); has problem. presume getscreenwidth , getscreenheight return integers. in c/c++ when dividing integers happens integer operation, means result rounded down (always) next smaller integer. type assign result doesn't matter. c/c++ @ types of l-values , r-values independently. can fix typecasting 1 side of division float (usually cast both), i.e.
glfloat const aspect_ratio = (float)getscreenwidth() / (float)getscreenheight(); also note const qualifier left associative, i.e. acts on what's left of it. if it's very first token in variable definition statement acts toward right. many language purists (including me) consider ugly. writing const after type caters more natural language.
Comments
Post a Comment