1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289
|
/* redblue_stereo.c - demo of stereo for red/blue filter stereo glasses */
/* by Walter Vannini (walterv@jps.net, waltervannini@hotmail.com) */
/* In stereo mode, the object is drawn in red for the left eye
and blue for the right eye. Viewing the scene with red/blue
filter stereo glasses should give a sense of stereo 3D.
glColorMask is used to control update of the red and blue
channel. glFrustum is used to setup two different view frustums
for each eye based on eye separation. */
/* Copyright (c) Walter Vannini, 1998. */
/* This program is freely distributable without licensing fees and is
provided without guarantee or warrantee expressed or implied. This
program is -not- in the public domain. */
#include <stdlib.h>
#include <math.h>
#include <GL/glut.h>
#ifndef M_PI
#define M_PI 3.14159265358979323846
#endif
void init(void);
void KeyboardFunc(unsigned char key, int x, int y);
void MenuFunc(int value);
void IdleFunc(void);
void ReshapeFunc(int w, int h);
void DisplayFunc(void);
struct ProgramState
{
int w;
int h;
GLdouble RotationY;
double eye;
double zscreen;
double znear;
double zfar;
double RotationIncrement;
int solidmode;
};
struct ProgramState ps;
const double PIXELS_PER_INCH = 100.0;
void init(void)
{
GLfloat mat_ambient[] = {0.2,0.0,0.2,1.0} ;
GLfloat mat_diffuse[] = {0.7,0.0,0.7,1.0} ;
GLfloat mat_specular[] = {0.1,0.0,0.1,1.0} ;
GLfloat mat_shininess[]={20.0};
GLfloat light_position[]={0.0,5.0,20.0,1.0};
GLfloat light_ambient0[]= {1.0,0.0,0.0,1.0};
GLfloat light_diffuse0[]= {1.0,0.0,0.0,1.0};
GLfloat light_specular0[]={1.0,0.0,0.0,1.0};
GLfloat light_ambient1[]= {0.0,0.0,1.0,1.0};
GLfloat light_diffuse1[]= {0.0,0.0,1.0,1.0};
GLfloat light_specular1[]={0.0,0.0,1.0,1.0};
glDisable(GL_DITHER);
glClearColor(0.0, 0.0, 0.0, 1.0);
glShadeModel(GL_SMOOTH);
glEnable(GL_DEPTH_TEST);
glEnable(GL_NORMALIZE);
glEnable(GL_CULL_FACE);
glLightModeli(GL_LIGHT_MODEL_LOCAL_VIEWER, 1);
glMaterialfv(GL_FRONT, GL_AMBIENT, mat_ambient);
glMaterialfv(GL_FRONT, GL_DIFFUSE, mat_diffuse);
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_AMBIENT, light_ambient0);
glLightfv(GL_LIGHT0, GL_DIFFUSE, light_diffuse0);
glLightfv(GL_LIGHT0, GL_SPECULAR, light_specular0);
glLightfv(GL_LIGHT1, GL_POSITION, light_position);
glLightfv(GL_LIGHT1, GL_AMBIENT, light_ambient1);
glLightfv(GL_LIGHT1, GL_DIFFUSE, light_diffuse1);
glLightfv(GL_LIGHT1, GL_SPECULAR, light_specular1);
glEnable(GL_LIGHTING);
ps.eye=0.80;
ps.zscreen = 10.0;
ps.znear = 7.0;
ps.zfar = 13.0;
ps.RotationY = 0.0;
ps.RotationIncrement = 4.0;
ps.solidmode = 1;
}
void KeyboardFunc(unsigned char key, int x, int y)
{
switch(key)
{
case 27: /* escape */
case 'q':
case 'Q':
exit(0);
break;
case 's': /* stereo */
ps.eye = 0.80;
break;
case '1':
ps.solidmode = 1;
glFrontFace(GL_CCW);
break;
case '2':
ps.solidmode = 2;
glFrontFace(GL_CCW);
break;
case '3':
ps.solidmode = 3;
/* The teapot polygons face the wrong way. Sigh. */
glFrontFace(GL_CW);
break;
case '4':
ps.solidmode = 4;
glFrontFace(GL_CCW);
break;
case 'm': /* mono */
ps.eye = 0.0;
break;
}
}
void MenuFunc(int value)
{
KeyboardFunc(value, 0, 0);
}
void IdleFunc(void)
{
ps.RotationY += ps.RotationIncrement;
glutPostRedisplay();
}
void ReshapeFunc(int w, int h)
{
glViewport(0,0, w, h);
ps.w = w;
ps.h = h;
}
void DisplayFunc(void)
{
double xfactor=1.0, yfactor=1.0;
double Eye =0.0;
int i;
if(ps.w < ps.h)
{
xfactor = 1.0;
yfactor = ps.h/ps.w;
}
else if(ps.h < ps.w)
{
xfactor = ps.w/ps.h;
yfactor = 1.0;
}
glClear(GL_COLOR_BUFFER_BIT);
for(i=0;i<2;i++)
{
glEnable(GL_LIGHT0 + i);
glClear(GL_DEPTH_BUFFER_BIT);
if(i==0) /* left eye - RED */
{
Eye = ps.eye;
glColorMask(GL_TRUE,GL_FALSE,GL_FALSE,GL_TRUE);
}
else /* if(i==1) right eye - BLUE */
{
Eye = -ps.eye;
glColorMask(GL_FALSE,GL_FALSE,GL_TRUE,GL_TRUE);
}
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glFrustum(
(-(ps.w/(2.0*PIXELS_PER_INCH))+Eye) *(ps.znear/ps.zscreen)*xfactor,
(ps.w/(2.0*PIXELS_PER_INCH)+Eye) *(ps.znear/ps.zscreen)*xfactor,
-(ps.h/(2.0*PIXELS_PER_INCH))*(ps.znear/ps.zscreen)*yfactor,
(ps.h/(2.0*PIXELS_PER_INCH))*(ps.znear/ps.zscreen)*yfactor,
ps.znear, ps.zfar);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glTranslatef(Eye,0.0,0.0);
glTranslated(0,0,-ps.zscreen);
switch(ps.solidmode)
{
case 1:
{
glTranslated(cos(ps.RotationY*M_PI/180.0),
0, -sin(ps.RotationY*M_PI/180.0) );
glRotated(ps.RotationY, 0.0,0.1,0.0);
glPushMatrix();
glScalef(0.5,0.5,0.5);
glutSolidDodecahedron();
glPopMatrix();
break;
}
case 2:
{
glTranslated(cos(ps.RotationY*M_PI/180.0),
0, -sin(ps.RotationY*M_PI/180.0) );
glRotated(ps.RotationY, 0.0,0.1,0.0);
glutSolidIcosahedron();
break;
}
case 3:
{
glRotated(60.0, 1 , 0, 0);
glTranslated(cos(ps.RotationY*M_PI/180.0),
0, -sin(ps.RotationY*M_PI/180.0) );
glRotated(ps.RotationY, 0.0,0.1,0.0);
glutSolidTeapot(1.0);
break;
}
case 4:
{
double SunRadius = 0.2;
double SunToEarth = 1.0;
double EarthRadius = 0.15;
glutSolidSphere(SunRadius, 20, 16); /* draw sun */
glRotated(60.0, 1 , 0, 0);
glPushMatrix();
glRotated(90.0, 1 , 0, 0);
glutSolidTorus(0.01,SunToEarth, 10,50);
glPopMatrix();
glRotated(ps.RotationY, 0.0,0.1,0.0);
glTranslatef (SunToEarth, 0.0, 0.0);
glutSolidSphere(EarthRadius, 10, 8); /* draw earth */
break;
}
}
glDisable(GL_LIGHT0 + i);
}
glColorMask(GL_TRUE,GL_TRUE,GL_TRUE,GL_TRUE);
glutSwapBuffers();
}
void
VisibilityFunc(int vis)
{
if (vis == GLUT_VISIBLE) {
glutIdleFunc(IdleFunc);
} else {
glutIdleFunc(NULL);
}
}
int main(int argc, char **argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA );
ps.w = 512;
ps.h = 512;
glutInitWindowSize(ps.w, ps.h);
glutInitWindowPosition(100,100);
glutCreateWindow(argv[0]);
init();
glutVisibilityFunc(VisibilityFunc);
glutDisplayFunc(DisplayFunc);
glutReshapeFunc(ReshapeFunc);
glutKeyboardFunc(KeyboardFunc);
glutCreateMenu(MenuFunc);
glutAddMenuEntry("Stereo", 's');
glutAddMenuEntry("Mono", 'm');
glutAddMenuEntry("Dodecahedron", '1');
glutAddMenuEntry("Icosahedron", '2');
glutAddMenuEntry("Teapot", '3');
glutAddMenuEntry("Solar system", '4');
glutAttachMenu(GLUT_RIGHT_BUTTON);
glutMainLoop();
return 0;
}
|