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