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