Using OpenGL to achieve 3D stereo display program code _c language

Source: Internet
Author: User
Because the angle of view of the monitor is different between the left eye and the right eye, using this angle to cover the light can be assigned to the right eye or left eye, through the brain of the two images from the difference into a space depth and dimension information, so that the image can be seen 3D.
The complete implementation code looks like this:
Copy Code code 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 you turn the rank
n=0;
LEFTMATRIX[12] = n;
RIGHTMATRIX[12] =-N;
This assumes the eye to the screen for one meter, 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);/The color of the body
GLLIGHTFV (Gl_light0, Gl_specular, white_light);//Highlight Color
GLLIGHTMODELFV (Gl_light_model_ambient, lmodel_ambient);
Glenable (gl_lighting);
Glenable (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 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);
Glutsolidteapot (2.0);
Glpopmatrix ();
Glmatrixmode (gl_projection);
Glpopmatrix ();
Glflush ();
Draw 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);
Glutsolidteapot (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);
Glutsolidteapot (1.0);
Glrotatef ((glfloat) big, 0.0, 1.0, 0.0);
Gltranslatef (3.0, 0.0, 0.0);
Glutsolidteapot (0.5);
Glutswapbuffers ();
}
void reshape (int w, int h)
{
Glviewport (0, 0, (Glsizei) W, (Glsizei) h);
Glmatrixmode (gl_projection);
Glloadidentity ();
Gluperspective (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;
Glutpostredisplay ();
Break
Case ' B ':
Big = (big-1)% 360;
Glutpostredisplay ();
Break
Case 27://Exit program when ESC key is pressed
Exit (0);
Break
Default
Break
}
}
void Spindisplay (void)
{
Big = (big + 1)% 360;
Glutpostredisplay ();
}
int main (int argc, char** argv)
{
Glutinit (&ARGC, argv);
Glutinitdisplaymode (glut_double | Glut_rgb | Glut_depth);
Glutinitwindowsize (500, 500);
Glutinitwindowposition (100, 100);
Glutcreatewindow (Argv[0]);
Init ();
Glutdisplayfunc (display);
Glutreshapefunc (reshape);
Glutkeyboardfunc (keyboard);
Glutidlefunc (Spindisplay);
Glutmainloop ();
return 0;
}

The final effect figure looks like this:

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.