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