]> git.jsancho.org Git - lugaru.git/blob - Source/Graphic/Stereo.cpp
Update copyright year to 2017
[lugaru.git] / Source / Graphic / Stereo.cpp
1 /*
2 Copyright (C) 2003, 2010 - Wolfire Games
3 Copyright (C) 2010-2017 - 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 void InitStereo(StereoMode mode)
54 {
55     switch (mode) {
56         default:
57         case stereoNone:
58         case stereoAnaglyph:
59             glDisable(GL_STENCIL_TEST);
60             return;
61         case stereoHorizontalInterlaced:
62         case stereoVerticalInterlaced:
63             fprintf(stderr, "Screen width is %i, height is %i\n", kContextWidth, kContextHeight);
64
65             // Setup stencil buffer
66             glDisable(GL_DEPTH_TEST);
67             glDisable(GL_CULL_FACE);
68             glDisable(GL_LIGHTING);
69             glDisable(GL_TEXTURE_2D);
70
71             glEnable(GL_STENCIL_TEST);
72             glClearStencil(0);
73             glClear(GL_STENCIL_BUFFER_BIT);
74             glStencilFunc(GL_ALWAYS, 0x1, 0x1);
75             glStencilOp(GL_REPLACE, GL_REPLACE, GL_REPLACE);
76
77             // Setup viewport
78             glViewport(0, 0, kContextWidth, kContextHeight);
79             glMatrixMode(GL_PROJECTION);
80             glPushMatrix();
81             glLoadIdentity();
82             glOrtho((GLdouble)0, (GLdouble)kContextWidth, (GLdouble)kContextHeight, 0, -1, 1);
83             glMatrixMode(GL_MODELVIEW);
84             glPushMatrix();
85             glLoadIdentity();
86             glColorMask(GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE);
87             glDisable(GL_LINE_SMOOTH);
88
89             // Add 0.5 to the coordinates, because OpenGL considers a pixel should be
90             // turned on when a line passes through the center of it.
91             if (mode == stereoHorizontalInterlaced) {
92                 for (int y = 0; y < kContextHeight; y += 2) {
93                     glBegin(GL_LINES);
94                     glVertex3f(0.5, y + 0.5, 0);
95                     glVertex3f(kContextWidth + 0.5, y + 0.5, 0);
96                     glEnd();
97                 }
98             } else {
99                 for (int x = 0; x < kContextWidth; x += 2) {
100                     glBegin(GL_LINES);
101                     glVertex3f(x + 0.5, 0.5, 0);
102                     glVertex3f(x + 0.5, kContextHeight + 0.5, 0);
103                     glEnd();
104                 }
105             }
106
107             glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
108
109             glPopMatrix();
110             glMatrixMode(GL_PROJECTION);
111             glPopMatrix();
112
113             glStencilFunc(GL_NOTEQUAL, 0x01, 0x01);
114             glStencilOp(GL_KEEP, GL_KEEP, GL_KEEP);
115             glEnable(GL_DEPTH_TEST);
116             glEnable(GL_CULL_FACE);
117             glEnable(GL_LIGHTING);
118             glEnable(GL_TEXTURE_2D);
119     }
120 }
121
122 const std::string StereoModeName(StereoMode mode)
123 {
124     switch (mode) {
125         case stereoNone:
126             return "None";
127             break;
128         case stereoAnaglyph:
129             return "Anaglyph";
130             break;
131         case stereoHorizontalInterlaced:
132             return "Horizontal interlacing";
133             break;
134         case stereoVerticalInterlaced:
135             return "Vertical interlacing";
136             break;
137         case stereoHorizontalSplit:
138             return "Horizontal split";
139             break;
140         case stereoVerticalSplit:
141             return "Vertical split";
142             break;
143         case stereoOpenGL:
144             return "OpenGL";
145             break;
146         default:
147             return "(error)";
148             break;
149     }
150 }