Saturday 15 May 2010

Scaling 3D shapes (OpenGL and C++) -



Scaling 3D shapes (OpenGL and C++) -

i've created programme in opengl draws shapes. want user able zoom in on shapes if want to. code draws shapes:

/*initialise required opengl functions*/ glclear(gl_color_buffer_bit | gl_depth_buffer_bit); glmatrixmode(gl_projection); glpushmatrix(); glloadidentity(); glortho(0.0, screenwidth, screenheight, 0.0, -1.0, 10.0); glmatrixmode(gl_modelview); glpushmatrix(); glloadidentity(); gldisable(gl_cull_face); glclear(gl_depth_buffer_bit); /*draw square*/ glcolor3f(1, 0, 0); glbegin(gl_quads); glvertex2f(screenwidth * 0.75, screenheight * 0.08333); glvertex2f(screenwidth * 0.75, screenheight * 0.16666); glvertex2f(screenwidth * 0.86666, screenheight * 0.16666); glvertex2f(screenwidth * 0.86666, screenheight * 0.08333); glend(); glcolor3f(0, 0, 0); /*let user zoom*/ if (getasynckeystate(vk_up)) { /*"zoom" global variable*/ zoom += 0.005; } glscaled(1 + zoom, 1 + zoom, 1); /*everything drawn point on (a sphere , cube) should scaled*/ glmatrixmode(gl_projection); glloadidentity(); glmatrixmode(gl_modelview); glloadidentity(); gltranslatef(-0.3, 0, 0); glutsolidsphere(3, 20, 20); glmatrixmode(gl_modelview); glloadidentity(); gltranslatef(0.55, 0.36, 0); glutsolidcube(0.05); glmatrixmode(gl_projection); glpopmatrix(); glmatrixmode(gl_modelview); glpopmatrix(); glutswapbuffers();

the code draws shapes properly, shapes can't scaled. i've used similar code in other functions, believe may because using 3d shapes or may have me calling "glmatrixmode" multiple times. either way, how should alter code cube , sphere scaled based on user input, first square not affected?

glscaled() changes current matrix. phone call glloadidentity() undoing scaling. doing lots of unnecessary calls glmatrixmode() , glloadidentity() should eliminated. seek more this:

// don't need these, if do, 1 time top. glmatrixmode(gl_projection); glloadidentity(); glmatrixmode(gl_modelview); glloadidentity(); glpushmatrix(); // save current matrix glscaled(1 + zoom, 1 + zoom, 1); // scale /*everything drawn point on (a sphere , cube) should scaled*/ gltranslatef(-0.3, 0, 0); glutsolidsphere(3, 20, 20); gltranslatef(0.55, 0.36, 0); glutsolidcube(0.05); glpopmatrix(); // undo glscaled() phone call above glutswapbuffers();

c++ opengl 3d scale shape

No comments:

Post a Comment