]> git.jsancho.org Git - lugaru.git/blob - Source/Stereo.cpp
Avoid using bool as int
[lugaru.git] / Source / 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 "Game.h"
22 #include "Stereo.h"
23
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         return true;
35         break;
36     case stereoAnaglyph:
37         return true;
38         break;
39     case stereoHorizontalInterlaced:
40     case stereoVerticalInterlaced:
41         glGetIntegerv(GL_STENCIL_BITS, &stencilbits);
42         if ( stencilbits < 1 ) {
43             fprintf(stderr, "Failed to get a stencil buffer, interlaced stereo not available.\n");
44             return false;
45         } else {
46             fprintf(stderr, "Stencil buffer has %i bits, good.\n", stencilbits);
47         }
48         return true;
49         break;
50     default:
51         return false;
52     }
53
54 }
55
56 void InitStereo(StereoMode mode)
57 {
58     switch (mode) {
59     default:
60     case stereoNone:
61     case stereoAnaglyph:
62         glDisable(GL_STENCIL_TEST);
63         return;
64     case stereoHorizontalInterlaced:
65     case stereoVerticalInterlaced:
66         fprintf(stderr, "Screen width is %i, height is %i\n", kContextWidth, kContextHeight);
67
68         // Setup stencil buffer
69         glDisable( GL_DEPTH_TEST);
70         glDisable(GL_CULL_FACE);
71         glDisable(GL_LIGHTING);
72         glDisable(GL_TEXTURE_2D);
73
74         glEnable( GL_STENCIL_TEST);
75         glClearStencil(0);
76         glClear(  GL_STENCIL_BUFFER_BIT );
77         glStencilFunc(GL_ALWAYS, 0x1, 0x1);
78         glStencilOp(GL_REPLACE, GL_REPLACE, GL_REPLACE);
79
80         // Setup viewport
81         glViewport(0, 0, kContextWidth, kContextHeight);
82         glMatrixMode(GL_PROJECTION);
83         glPushMatrix();
84         glLoadIdentity();
85         glOrtho((GLdouble)0, (GLdouble)kContextWidth, (GLdouble)kContextHeight, 0, -1, 1);
86         glMatrixMode(GL_MODELVIEW);
87         glPushMatrix();
88         glLoadIdentity();
89         glColorMask( GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE );
90         glDisable(GL_LINE_SMOOTH);
91
92         // Add 0.5 to the coordinates, because OpenGL considers a pixel should be
93         // turned on when a line passes through the center of it.
94         if ( mode == stereoHorizontalInterlaced ) {
95             for (int y = 0; y < kContextHeight; y += 2) {
96                 glBegin(GL_LINES);
97                 glVertex3f(0.5, y + 0.5, 0);
98                 glVertex3f(kContextWidth + 0.5, y + 0.5, 0);
99                 glEnd();
100             }
101         } else {
102             for (int x = 0; x < kContextWidth; x += 2) {
103                 glBegin(GL_LINES);
104                 glVertex3f(x + 0.5, 0.5, 0);
105                 glVertex3f(x + 0.5, kContextHeight + 0.5, 0);
106                 glEnd();
107             }
108         }
109
110         glColorMask( GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE );
111
112         glPopMatrix();
113         glMatrixMode(GL_PROJECTION);
114         glPopMatrix();
115
116         glStencilFunc(GL_NOTEQUAL, 0x01, 0x01);
117         glStencilOp(GL_KEEP, GL_KEEP, GL_KEEP);
118         glEnable( GL_DEPTH_TEST);
119         glEnable(GL_CULL_FACE);
120         glEnable(GL_LIGHTING);
121         glEnable(GL_TEXTURE_2D);
122     }
123
124 }
125
126 const char* StereoModeName(StereoMode mode)
127 {
128     switch (mode) {
129     case stereoNone:
130         return "None";
131         break;
132     case stereoAnaglyph:
133         return "Anaglyph";
134         break;
135     case stereoHorizontalInterlaced:
136         return "Horizontal interlacing";
137         break;
138     case stereoVerticalInterlaced:
139         return "Vertical interlacing";
140         break;
141     case stereoHorizontalSplit:
142         return "Horizontal split";
143         break;
144     case stereoVerticalSplit:
145         return "Vertical split";
146         break;
147     case stereoOpenGL:
148         return "OpenGL";
149         break;
150     default:
151         return "(error)";
152         break;
153     }
154 }