Program code for 3D 3D display using OpenGL

Source: Internet
Author: User

Because the angle between the left eye and the right eye is different from that of the monitor, the image can be allocated to the right eye or the left eye using this angle difference to cover the light, after the brain merges the two images from different images into a pair of images with spatial depth and dimension information, we can see 3D images.
The complete implementation code is as follows:
Copy codeThe Code is as follows: # include "stdafx. h"
# Include "GL/glut. h"
# Include "stdlib. h"
# Include "stdio. h"
# Include "math. h"
Static int big = 0;
Static bool isLeftEye = false;
# Define PI 3.1415926
Const GLfloat R = 8.0;
Static GLfloat leftMatrix [16] = {1.0, 0.0, 0.0, 0.0,
0.0, 1.0, 0.0, 0.0,
0.0, 0.0, 1.0, 0.0,
0.0, 0.0, 0.0, 1.0 };
Static GLfloat rightMatrix [16] = {1.0, 0.0, 0.0, 0.0,
0.0, 1.0, 0.0, 0.0,
0.0, 0.0, 1.0, 0.0,
0.0, 0.0, 0.0, 1.0 };
Static GLfloat leftPersMatrix [16] = {1.0, 0.0, 0.0, 0.0,
0.0, 1.0, 0.0, 0.0,
0.0, 0.0, 1.0, 0.0,
0.0, 0.0, 0.0, 1.0 };
Static GLfloat rightPersMatrix [16] = {1.0, 0.0, 0.0, 0.0,
0.0, 1.0, 0.0, 0.0,
0.0, 0.0, 1.0, 0.0,
0.0, 0.0, 0.0, 1.0 };
Void init (void)
{
Const GLfloat SD = 0.06;
GLfloat n = SD * R/2.0;
// If it is a transpose rank
// N = 0;
LeftMatrix [12] = n;
RightMatrix [12] =-n;
// Assume that the eye reaches the screen in meters.
GLfloat p = SD/(2*1 * tan (PI/6) * 1 );
// P = 0.0;
LeftPersMatrix [12] =-p;
RightPersMatrix [12] = p;
GLfloat mat_specular [] = {0.8, 0.8, 0.0, 1.0 };
GLfloat mat_shininess [] ={ 50.0 };
GLfloat light_position [] = {1.0, 1.0, 1.0, 0.0 };
GLfloat white_light [] = {1.0, 1.0, 1.0, 1.0 };
GLfloat yellow_light [] = {1.0, 1.0, 0.0, 1.0 };
GLfloat lmodel_ambient [] = {0.0, 0.7, 0.5, 1.0 };
GlClearColor (1.0, 1.0, 1.0, 0.0 );
GlShadeModel (GL_SMOOTH );
GlMaterialfv (GL_FRONT, GL_SPECULAR, mat_specular );
GlMaterialfv (GL_FRONT, GL_SHININESS, mat_shininess );
GlLightfv (GL_LIGHT0, GL_POSITION, light_position );
GlLightfv (GL_LIGHT0, GL_DIFFUSE, yellow_light); // subject color
GlLightfv (GL_LIGHT0, GL_SPECULAR, white_light); // The highlight color.
GlLightModelfv (GL_LIGHT_MODEL_AMBIENT, lmodel_ambient );
Glable (GL_LIGHTING );
Glable (GL_LIGHT0 );
GlEnable (GL_DEPTH_TEST );
}
Void display (void)
{
GlColorMask (1.0, 1.0, 1.0, 1.0 );
GlClearColor (0.0, 0.0, 0.0, 1.0 );
GlClearDepth (1.0 );
GlClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
GlColor3f (1.0, 1.0, 1.0 );
// Draw the left eye
GlMatrixMode (GL_PROJECTION );
GlPushMatrix ();
Float mat [16];
GlGetFloatv (GL_PROJECTION_MATRIX, mat );
GlLoadIdentity ();
GlMultMatrixf (leftPersMatrix );
GlMultMatrixf (mat );
GlMatrixMode (GL_MODELVIEW );
GlPushMatrix ();
GlGetFloatv (GL_MODELVIEW_MATRIX, mat );
GlLoadIdentity ();
GlMultMatrixf (leftMatrix );
GlMultMatrixf (mat );
GlColorMask (1.0, 0.0, 0.0, 1.0 );
GlRotatef (GLfloat) big, 0.0, 1.0, 0.0 );
Fig (2.0 );
GlPopMatrix ();
GlMatrixMode (GL_PROJECTION );
GlPopMatrix ();
GlFlush ();
// Draw the right eye
GlClearDepth (1.0 );
GlClear (GL_DEPTH_BUFFER_BIT );
GlMatrixMode (GL_PROJECTION );
GlPushMatrix ();
GlGetFloatv (GL_PROJECTION_MATRIX, mat );
GlLoadIdentity ();
GlMultMatrixf (rightPersMatrix );
GlMultMatrixf (mat );
GlMatrixMode (GL_MODELVIEW );
GlPushMatrix ();
GlGetFloatv (GL_MODELVIEW_MATRIX, mat );
GlLoadIdentity ();
GlMultMatrixf (rightMatrix );
GlMultMatrixf (mat );
GlColorMask (0.0, 1.0, 1.0, 1.0 );
GlRotatef (GLfloat) big, 0.0, 1.0, 0.0 );
Fig (2.0 );
GlPopMatrix ();
GlMatrixMode (GL_PROJECTION );
GlPopMatrix ();
GlFlush ();
// GlPopMatrix ();
// If (isLeftEye)
//{
// GlMatrixMode (GL_PROJECTION );
// GlMultMatrixf (leftPersMatrix );
// GlMatrixMode (GL_MODELVIEW );
// GlMultMatrixf (leftMatrix );
// GlColorMask (1.0, 0.0, 0.0, 1.0 );
//
//
//
// IsLeftEye = false;
//} Else
//{
//
// GlMatrixMode (GL_PROJECTION );
// GlMultMatrixf (rightPersMatrix );
// GlMatrixMode (GL_MODELVIEW );
// GlMultMatrixf (rightMatrix );
// GlColorMask (0.0, 1.0, 1.0, 1.0 );
// IsLeftEye = true;
//}
// GlRotatef (GLfloat) big, 0.0, 1.0, 0.0 );
// Maid (1.0 );
// GlRotatef (GLfloat) big, 0.0, 1.0, 0.0 );
// GlTranslatef (3.0, 0.0, 0.0 );
// Maid (0.5 );
Gluswapbuffers ();
}
Void reshape (int w, int h)
{
GlViewport (0, 0, (GLsizei) w, (GLsizei) h );
GlMatrixMode (GL_PROJECTION );
GlLoadIdentity ();
GluPerspective (60, (GLfloat) w/(GLfloat) h, 0.01, 20.0 );
GlMatrixMode (GL_MODELVIEW );
GlLoadIdentity ();
GluLookAt (0.0, 0.0, R, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0 );
}
Void keyboard (unsigned char key, int x, int y)
{
Switch (key)
{
Case 'B ':
Big = (big + 1) % 360;
Glupostredisplay ();
Break;
Case 'B ':
Big = (big-1) %360;
Glupostredisplay ();
Break;
Case 27: // exit the program when you press ESC
Exit (0 );
Break;
Default:
Break;
}
}
Void spinDisplay (void)
{
Big = (big + 1) % 360;
Glupostredisplay ();
}
Int main (int argc, char ** argv)
{
Gluinit (& argc, argv );
Fig );
Gluinitwindowsize (500,500 );
Gluinitwindowposition (100,100 );
Ngcreatewindow (argv [0]);
Init ();
Gludisplayfunc (display );
Glureshapefunc (reshape );
Glukeyboardfunc (keyboard );
GlutIdleFunc (spinDisplay );
Glumainloop ();
Return 0;
}

The final result is as follows:

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.