opengl - How to give a 2D structure 3D depth -
i begging learn opengl part of molecular modeling project, , trying render 7 helices placed spatially close each other , move, tilt , interact each other in ways. question how give 2d scene 3-dimensional depth geometric structures true helices in 3 dimensions?
i have tried playing around projection matrices (gulperspective, glfrustum) without luck, using gldepthrange function.
i include code rendering helices, simplicity insert code rendering 1 helix (the other 6 helices same except translation matrix , color function parameters) , reshaping when mapping object coordinates clip coordinates:
any appreciated.
void init() { glclearcolor(0.0f, 0.0f, 0.0f, 1.0f); // black background gllinewidth(8.0); } /* called draw helices */ void renderhelix() { glfloat x,y,z; glfloat c = 2.5f; //helical pitch glfloat theta; //constant angle between tangent , x-axis glfloat r = 4.5f; //radius //glint = 1; //loop through code render 7 helices glclear(gl_color_buffer_bit | gl_depth_buffer_bit); /* green helix */ glbegin(gl_line_strip); glcolor3f(0.0, 1.0, 0.0); for(theta = 0; theta <= 360; ++theta) { x = r*(cos(theta)); y = r*(sin(theta)); z = c*theta; glvertex3f(x,y,z); } glend(); glloadidentity(); glscalef(1.0,1.0,12.0); gltranslatef(50.0, 100.0, 0.0); //move position glrotatef(90.0, 0.0, 0.0, 0.0); /* other 6 helices .... */ glflush(); glutswapbuffers(); } void reshape(glint w, glint h) { if(h==0) h=1; glviewport(0,0,w,h); glmatrixmode(gl_projection); glloadidentity(); glfloat aspectratio = (glfloat)w/(glfloat)h; if(w<=h) glortho(-100,100,-100/aspectratio,100/aspectratio, -100,310); else glortho(-100*aspectratio,100*aspectratio,-100,100,-100,310); glmatrixmode(gl_modelview); glloadidentity(); }
i edited code , put comment here , there:
void init() { /* 1 time opengl initialization textures , * other static stuff. nothing here */ } /* called draw helices */ void renderhelix() { glfloat x,y,z; float aspectratio; glfloat c = 2.5f; //helical pitch glfloat theta; //constant angle between tangent , x-axis glfloat r = 4.5f; //radius //glint = 1; //loop through code render 7 helices /* assuming w , h globals or somehow else accessible */ aspectratio = (float)w/(float)h; glviewport(0,0,w,h); glclearcolor(0.0f, 0.0f, 0.0f, 1.0f); // black background glclear(gl_color_buffer_bit | gl_depth_buffer_bit); glmatrixmode(gl_projection); glloadidentity(); glfloat aspectratio = (glfloat)w/(glfloat)h; if(w<=h) glfrustum(-10.f,10.f, -10.f/aspectratio, 10.f/aspectratio, 1.0f, 10.f); else glfrustum(-10.f*aspectratio,10.f*aspectratio,-10.f,10.f, 1.0f,10.f); /* enable depth testing, make sure right depth func used */ glenable(gl_depth_test); gldepthfunc(gl_less); /* green helix */ gllinewidth(8.0); glmatrixmode(gl_modelview); glloadidentity(); gltranslatef(0.f, 0.f, -4.5f); glpushmatrix(); /* helix drawn in center, it's not moved anywhere. */ glbegin(gl_line_strip); glcolor3f(0.0, 1.0, 0.0); /* trigonometric functions take radians, not degrees */ for(theta = 0; theta <= m_pi2; theta += m_pi2*0.01;) { /* instead of calculating helices new each rendering * pass should store them vertex array. */ x = r*cosf(theta); y = r*sinf(theta); z = c*theta; glvertex3f(x,y,z); } glend(); glpopmatrix(); /* other 6 helices .... */ glscalef(1.0,1.0,12.0); gltranslatef(50.0, 100.0, 0.0); //move position of next helix, may want use glpushmatrix/glpopmatrix glrotatef(90.0, 0.0, 0.0, 0.0); // null rotation, what's that's supposed do? glflush(); // redundant, glflush implied swapbuffers glutswapbuffers(); } void reshape(glint w, glint h) { /* pleast don't set opengl stuff in window reshape handler. * causes confusion, 1 uses multiple projections * in single rendering */ }
Comments
Post a Comment