OpenGL for three-dimensional display

Source: Internet
Author: User

Stereoscopic display principle: if the model view matrix without stereoscopic display is set to mV and the projection matrix is MP, P indicates any point in the object space, then, convert it to the screen coordinate p * = Mp × MV × P. Note that the coordinate columns in OpenGL are prioritized, so the matrices are all right multiplication.

Both the left and right eyes are transformed from the intermediate transformation matrix. The transformation matrix formula of the left eye in the three-dimensional display is as follows:

P (l) * = MS (l) × MP (l) × MT (l) × mV (l) × P;

The right-eye matrix transformation formula is:

P (r) * = MS (r) × MP (r) × MT (r) × mV (r) × P;

Among them, MS and MT are the transformations required for three-dimensional display.

There are several parameters in the program: the distance from the eyes to the screen in the real world FD, the distance between the eyes SD, the scale R,

For example, if there is no stereoscopic display, the viewpoint is in the blue middle, And the stereoscopic display is to separate the left eye (red) and right eye (green) views.

In the program, the left eye is painted in red, and the right eye is painted in green and blue.

 

Code:

/* <Br/> * @ (#) $ ID: steroshow. CPP [4/24/2008 renyafei] dizuo@126.com $ <br/> * @ (#) OpenGL-based three-dimensional display implementation <br/> * Author: dizuo. hangzhou. <br/> * All Rights Reserved. <br/> */<br/> # include <Gl/glut. h> <br/> # include <math. h> <br/> void Init (void) <br/>{< br/> glfloat mat_diffuse [] = {1.0, 1.0, 0.0 }; <br/> glfloat mat_specular [] = {0.8, 0.8, 0.0, 1.0}; <br/> glfloat mat_shininess [] = {300 .}; <br/> glfloat light_position [] = {1.0, 1.0, 1.0, 0.0}; <br/> glfloat light_diffuse [] = {1.0, 1.0, 0.0 }; <br/> glfloat light_ambient [] = {0.7, 0.2, 0.2, 1.0}; </P> <p> glclearcolor (0.0, 0.0, 0.0, 0.0 ); <br/> glshademodel (gl_smooth); </P> <p> glmaterialfv (gl_front, gl_specular, mat_specular); <br/> glmaterialfv (gl_front, gl_diffuse, mat_diffuse ); <br/> latency (gl_front, gl_shininess, mat_shininess); <br/> gllightfv (gl_light0, gl_position, light_position); <br/> gllightfv (gl_light0, latency, light_ambient ); <br/> gllightfv (gl_light0, gl_diffuse, light_diffuse); </P> <p> gl_lighting (gl_lighting); <br/> glable (gl_light0 ); <br/> glable (gl_depth_test ); <br/>}< br/>/** // * initialize <br/> * initialization parameters <br/> */<br/> glfloat Pi = 3.1415926; <br/> glfloat FD = 5.0; // fusion distance <br/> glfloat realscreentoeyedistance = 1.0; <br/> glfloat r = FD/realscreentoeyedistance; // scale R = FD/realscreentoeyedistance <br/> glfloat SD = 0.05; // The distance between two eyes <br/> glfloat aspect = 1.0; // parameters in the glulookat function <br/> glfloat fovy = 60.0; // Zhang Jiao <br/> glfloat F = 1/TAN (fovy * PI) /(2*180); // F = CTG (fovy/2); </P> <p> // Matrix Model View matrix with column priority, projection Matrix <br/> glfloat leftmodelviewmatrix [16] ={< br/> 1.0, 0.0, 0.0, 0.0, <br/> 0.0, 1.0, 0.0, 0.0, <br/> 0.0, 0.0, 1.0, 0.0, <br/> SD * r/2.0, 0.0, 0.0, 1.0 <br/> }; </P> <p> glfloat leftprojectmatrix [16] ={< br/> 1.0, 0.0, 0.0, 0.0, <br/> 0.0, 1.0, 0.0, 0.0, <br/> 0.0, 0.0, 1.0, 0.0, <br/>-(SD * f)/(2.0 * FD * aspect), 0.0, 0.0, 1.0 <br/>}; </P> <p> glfloat rightmodelviewmatrix [16] ={< br/> 1.0, 0.0, 0.0, 0.0, <br/> 0.0, 1.0, 0.0, 0.0, <br/> 0.0, 0.0, 1.0, 0.0, <br/>-SD * r/2.0, 0.0, 0.0, 1.0 <br/> }; </P> <p> glfloat rightprojectmatrix [16] ={< br/> 1.0, 0.0, 0.0, 0.0, <br/> 0.0, 1.0, 0.0, 0.0, <br/> 0.0, 0.0, 1.0, 0.0, <br/> (SD * f)/(2.0 * FD * aspect), 0.0, 0.0, 1.0 <br/>}; </P> <p> // For the use of rotating <br/> static glfloat spin = 0.0; </P> <p> void display (void) <br/>{< br/> glfloat matrix [16] = {0 .}; </P> <p> glcolormask (1.0, 1.0, 1.0, 1.0); <br/> glclearcolor (0.0, 0.0, 0.0, 1.0 ); <br/> glcleardepth (1.0); </P> <p> glclear (gl_color_buffer_bit | gl_depth_buffer_bit); <br/> glcolor3f (1.0, 1.0, 1.0 ); </P> <p> // comment <br/> // left viewport <br/> glmatrixmode (gl_projection); <br/> glpushmatrix (); <br/> glgetfloatv (gl_projection_matrix, matrix); <br/> glloadidentity (); <br/> glmultmatrixf (leftprojectmatrix); <br/> glmultmatrixf (matrix ); </P> <p> glmatrixmode (gl_modelview); <br/> glloadidentity (); <br/> gltranslated (0.0, 0.0,-FD ); <br/> glpushmatrix (); <br/>{< br/> glgetfloatv (gl_modelview_matrix, matrix); <br/> glloadidentity (); <br/> glmultmatrixf (leftmodelviewmatrix); <br/> glmultmatrixf (matrix); <br/> glcolormask (1.0, 0.0, 0.0, 1.0 ); <br/>/** // * <br/> * The coordinate VP of the object <br/> * is changed to the screen coordinate: VP '= leftprojectmatrix × MP × leftmodelviewmatrix × MV × Mr × VP <br/> */<br/> glpushmatrix (); <br/>{< br/> glrotatef (spin, 0.0, 1.0, 0.0); fig (1.0); <br/>}< br/> glpopmatrix (); <br/>}< br/> glpopmatrix (); <br/> glmatrixmode (gl_projection); <br/> glpopmatrix (); <br/> glflush (); </P> <p> // comment <br/> // right viewport <br/> glmatrixmode (gl_projection); <br/> glpushmatrix (); <br/> glgetfloatv (gl_projection_matrix, matrix); <br/> glloadidentity (); <br/> glmultmatrixf (rightprojectmatrix); <br/> glmultmatrixf (matrix ); </P> <p> glmatrixmode (gl_modelview); <br/> glpushmatrix (); <br/>{< br/> glgetfloatv (gl_modelview_matrix, matrix ); <br/> glloadidentity (); <br/> glmultmatrixf (rightmodelviewmatrix); <br/> glmultmatrixf (matrix); </P> <p> glcolormask (0.0, 1.0, 1.0, 1.0); <br/> glcleardepth (1.0); <br/> glclear (gl_depth_buffer_bit ); <br/>/** // * <br/> * The coordinate VP of the object <br/> * is changed to the screen coordinate: VP '= rightprojectmatrix × MP × rightmodelviewmatrix × MV × Mr × VP <br/> */<br/> glpushmatrix (); <br/>{< br/> glrotatef (spin, 0.0, 1.0, 0.0); <br/> fig (1.0); <br/> // glusolidsphere (1.0, 20, 5); <br/>}< br/> glpopmatrix (); </P> <p> glmatrixmode (gl_projection ); <br/> glpopmatrix (); <br/> glflush (); </P> <p> gluswapbuffers (); <br/>}</P> <p> void reshape (int w, int h) <br/> {<br/> glviewport (0, 0, (glsizei) w, (glsizei) H); <br/> glmatrixmode (gl_projection); <br/> glloadidentity (); <br/> // projection matrix: MP <br/> gluperspective (fovy, (glfloat) W/(glfloat) h, 1.0, 20.0); <br/>}< br/> void spindisplay (void) <br/>{< br/> spin = spin + 1.0; <br/> If (spin> 360.0) <br/> spin = spin-360.0; <br/> glupostredisplay (); <br/>}</P> <p> int main (INT argc, char ** argv) <br/>{< br/> gluinit (& argc, argv); <br/> gluinitdisplaymode (glu_double | glu_rgb | glu_depth); <br/> gluinitwindowsize (500,500 ); <br/> gluinitwindowposition (100,100); <br/> glucreatewindow (argv [0]); <br/> Init (); <br/> gludisplayfunc (Display ); <br/> maid (reshape); <br/> glutidlefunc (spindisplay); <br/> fig (); <br/> return 0; <br/>}

Related three-dimensional display link: http://local.wasp.uwa.edu.au /~ Pbourke/projection/stereorender/

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.