]> git.jsancho.org Git - lugaru.git/blob - Source/Stereo.cpp
c5b8075b4b235e8de47cd220ea621fb74c6f4098
[lugaru.git] / Source / Stereo.cpp
1 /*
2 Copyright (C) 2003, 2010 - Wolfire Games
3
4 This file is part of Lugaru.
5
6 Lugaru is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
10
11 Lugaru is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with Lugaru.  If not, see <http://www.gnu.org/licenses/>.
18 */
19
20 #include "Game.h"
21 #include "Stereo.h"
22
23
24 extern int kContextWidth;
25 extern int kContextHeight;
26
27 bool CanInitStereo(StereoMode mode)
28 {
29     GLint stencilbits = 0;
30
31     switch (mode) {
32     case stereoNone:
33         return true;
34         break;
35     case stereoAnaglyph:
36         return true;
37         break;
38     case stereoHorizontalInterlaced:
39     case stereoVerticalInterlaced:
40         glGetIntegerv(GL_STENCIL_BITS, &stencilbits);
41         if ( stencilbits < 1 ) {
42             fprintf(stderr, "Failed to get a stencil buffer, interlaced stereo not available.\n");
43             return false;
44         } else {
45             fprintf(stderr, "Stencil buffer has %i bits, good.\n", stencilbits);
46         }
47         return true;
48         break;
49     default:
50         return false;
51     }
52
53 }
54
55 void InitStereo(StereoMode mode)
56 {
57     switch (mode) {
58     default:
59     case stereoNone:
60     case stereoAnaglyph:
61         glDisable(GL_STENCIL_TEST);
62         return;
63     case stereoHorizontalInterlaced:
64     case stereoVerticalInterlaced:
65         fprintf(stderr, "Screen width is %i, height is %i\n", kContextWidth, kContextHeight);
66
67         // Setup stencil buffer
68         glDisable( GL_DEPTH_TEST);
69         glDisable(GL_CULL_FACE);
70         glDisable(GL_LIGHTING);
71         glDisable(GL_TEXTURE_2D);
72
73         glEnable( GL_STENCIL_TEST);
74         glClearStencil(0);
75         glClear(  GL_STENCIL_BUFFER_BIT );
76         glStencilFunc(GL_ALWAYS, 0x1, 0x1);
77         glStencilOp(GL_REPLACE, GL_REPLACE, GL_REPLACE);
78
79         // Setup viewport
80         glViewport(0, 0, kContextWidth, kContextHeight);
81         glMatrixMode(GL_PROJECTION);
82         glPushMatrix();
83         glLoadIdentity();
84         glOrtho((GLdouble)0, (GLdouble)kContextWidth, (GLdouble)kContextHeight, 0, -1, 1);
85         glMatrixMode(GL_MODELVIEW);
86         glPushMatrix();
87         glLoadIdentity();
88         glColorMask( GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE );
89         glDisable(GL_LINE_SMOOTH);
90
91         // Add 0.5 to the coordinates, because OpenGL considers a pixel should be
92         // turned on when a line passes through the center of it.
93         if ( mode == stereoHorizontalInterlaced ) {
94             for (int y = 0; y < kContextHeight; y += 2) {
95                 glBegin(GL_LINES);
96                 glVertex3f(0.5, y + 0.5, 0);
97                 glVertex3f(kContextWidth + 0.5, y + 0.5, 0);
98                 glEnd();
99             }
100         } else {
101             for (int x = 0; x < kContextWidth; x += 2) {
102                 glBegin(GL_LINES);
103                 glVertex3f(x + 0.5, 0.5, 0);
104                 glVertex3f(x + 0.5, kContextHeight + 0.5, 0);
105                 glEnd();
106             }
107         }
108
109         glColorMask( GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE );
110
111         glPopMatrix();
112         glMatrixMode(GL_PROJECTION);
113         glPopMatrix();
114
115         glStencilFunc(GL_NOTEQUAL, 0x01, 0x01);
116         glStencilOp(GL_KEEP, GL_KEEP, GL_KEEP);
117         glEnable( GL_DEPTH_TEST);
118         glEnable(GL_CULL_FACE);
119         glEnable(GL_LIGHTING);
120         glEnable(GL_TEXTURE_2D);
121     }
122
123 }
124
125 const char* StereoModeName(StereoMode mode)
126 {
127     switch (mode) {
128     case stereoNone:
129         return "None";
130         break;
131     case stereoAnaglyph:
132         return "Anaglyph";
133         break;
134     case stereoHorizontalInterlaced:
135         return "Horizontal interlacing";
136         break;
137     case stereoVerticalInterlaced:
138         return "Vertical interlacing";
139         break;
140     case stereoHorizontalSplit:
141         return "Horizontal split";
142         break;
143     case stereoVerticalSplit:
144         return "Vertical split";
145         break;
146     case stereoOpenGL:
147         return "OpenGL";
148         break;
149     default:
150         return "(error)";
151         break;
152     }
153 }