]> git.jsancho.org Git - lugaru.git/blob - Source/Stereo.cpp
Moved stereo initialization code to a separate file.
[lugaru.git] / Source / Stereo.cpp
1
2 #include "Game.h"
3 #include "Stereo.h"
4
5
6 extern int kContextWidth;
7 extern int kContextHeight;
8
9 bool CanInitStereo(StereoMode mode) {
10         GLint stencilbits = 0;
11         
12         switch(mode) {
13                 case stereoNone:
14                         return true;
15                         break;
16                 case stereoAnaglyph:
17                         return true;
18                         break;
19                 case stereoHorizontalInterlaced:
20                 case stereoVerticalInterlaced:
21                         glGetIntegerv(GL_STENCIL_BITS, &stencilbits);
22                         if ( stencilbits < 1 ) {
23                                 fprintf(stderr, "Failed to get a stencil buffer, interlaced stereo not available.\n");
24                                 return false;
25                         } else {
26                                 fprintf(stderr, "Stencil buffer has %i bits, good.\n", stencilbits);
27                         }
28                         
29                 default:
30                         return false;
31         }
32
33 }
34
35 void InitStereo(StereoMode mode) {
36         switch(mode) {
37                 case stereoNone:
38                         return;
39                 case stereoAnaglyph:
40                         return;
41                 case stereoHorizontalInterlaced:
42                 case stereoVerticalInterlaced:
43                         
44                         fprintf(stderr, "Screen width is %i, height is %i\n", kContextWidth, kContextHeight);
45                         
46                         glEnable( GL_STENCIL_TEST);
47                         glClearStencil(0);
48                         glClear(  GL_STENCIL_BUFFER_BIT );
49                         glStencilFunc(GL_ALWAYS, 0x1, 0x1);
50                         glStencilOp(GL_REPLACE, GL_REPLACE, GL_REPLACE);
51                         
52                         
53                         glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
54                         glPixelStorei(GL_UNPACK_ROW_LENGTH, 3);
55                         glPixelStorei(GL_UNPACK_SKIP_ROWS, 0);
56                         glPixelStorei(GL_UNPACK_SKIP_PIXELS, 0);
57                         glColorMask( 1.0, 1.0, 1.0, 1.0 );
58                         char stencil[] = {64,127,255};
59                         
60                         glViewport(0,0, kContextWidth, kContextHeight);
61                         glMatrixMode(GL_PROJECTION);
62                         glLoadIdentity();
63                         glOrtho((GLdouble)0, (GLdouble)kContextWidth, (GLdouble)kContextHeight, 0, -1, 1);
64                         glMatrixMode(GL_MODELVIEW);
65                         glLoadIdentity();
66                         
67                         for(int y=0;y<kContextHeight;y+=2) {
68                                 
69                                 for(int x=0;x<kContextWidth;x++) {
70                                         glRasterPos2i(x, y);
71                                         glDrawPixels(1, 1, GL_RGB, GL_UNSIGNED_BYTE, &stencil);
72                                 }
73                         }
74                         
75                         glStencilFunc(GL_NOTEQUAL, 0x01, 0x01);
76                         glStencilOp(GL_KEEP, GL_KEEP, GL_KEEP);
77
78                         // Something gets screwed up due to the changes above
79                         // revert to default.
80                         glPixelStorei(GL_UNPACK_ALIGNMENT, 4);
81                         glPixelStorei(GL_UNPACK_ROW_LENGTH, 0);
82                         glPixelStorei(GL_UNPACK_SKIP_ROWS, 0);
83                         glPixelStorei(GL_UNPACK_SKIP_PIXELS, 0);
84         }
85         
86 }