Merging r58475 through r58700 from trunk into soc-2013-depsgraph_mt
[blender.git] / intern / cycles / util / util_view.cpp
1 /*
2  * Copyright 2011, Blender Foundation.
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License
6  * as published by the Free Software Foundation; either version 2
7  * of the License, or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software Foundation,
16  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17  */
18
19 #include <stdio.h>
20 #include <stdlib.h>
21 #include <string.h>
22
23 #include "util_opengl.h"
24 #include "util_time.h"
25 #include "util_view.h"
26
27 #ifdef __APPLE__
28 #include <GLUT/glut.h>
29 #else
30 #include <GL/glut.h>
31 #endif
32
33 CCL_NAMESPACE_BEGIN
34
35 /* structs */
36
37 struct View {
38         ViewInitFunc initf;
39         ViewExitFunc exitf;
40         ViewResizeFunc resize;
41         ViewDisplayFunc display;
42         ViewKeyboardFunc keyboard;
43
44         bool first_display;
45         bool redraw;
46
47         int width, height;
48 } V;
49
50 /* public */
51
52 static void view_display_text(int x, int y, const char *text)
53 {
54         const char *c;
55
56         glRasterPos3f(x, y, 0);
57
58         for(c = text; *c != '\0'; c++)
59                 glutBitmapCharacter(GLUT_BITMAP_HELVETICA_10, *c);
60 }
61
62 void view_display_info(const char *info)
63 {
64         const int height = 20;
65
66         glEnable(GL_BLEND);
67         glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
68         glColor4f(0.1f, 0.1f, 0.1f, 0.8f);
69         glRectf(0.0f, V.height - height, V.width, V.height);
70         glDisable(GL_BLEND);
71
72         glColor3f(0.5f, 0.5f, 0.5f);
73
74         view_display_text(10, 7 + V.height - height, info);
75
76         glColor3f(1.0f, 1.0f, 1.0f);
77 }
78
79 static void view_display()
80 {
81         if(V.first_display) {
82                 if(V.initf) V.initf();
83                 if(V.exitf) atexit(V.exitf);
84
85                 V.first_display = false;
86         }
87
88         glClearColor(0.05f, 0.05f, 0.05f, 0.0f);
89         glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
90
91         glMatrixMode(GL_PROJECTION);
92         glLoadIdentity();
93         gluOrtho2D(0, V.width, 0, V.height);
94
95         glMatrixMode(GL_MODELVIEW);
96         glLoadIdentity();
97
98         glRasterPos3f(0, 0, 0);
99
100         if(V.display)
101                 V.display();
102
103         glutSwapBuffers();
104 }
105
106 static void view_reshape(int width, int height)
107 {
108         if(width <= 0 || height <= 0)
109                 return;
110         
111         V.width = width;
112         V.height = height;
113
114         glViewport(0, 0, width, height);
115
116         glMatrixMode(GL_PROJECTION);
117         glLoadIdentity();
118
119         glMatrixMode(GL_MODELVIEW);
120         glLoadIdentity();
121
122         if(V.resize)
123                 V.resize(width, height);
124 }
125
126 static void view_keyboard(unsigned char key, int x, int y)
127 {
128         if(V.keyboard)
129                 V.keyboard(key);
130
131         if(key == 'm')
132                 printf("mouse %d %d\n", x, y);
133         if(key == 'q') {
134                 if(V.exitf) V.exitf();
135                 exit(0);
136         }
137 }
138
139 static void view_idle(void)
140 {
141         if(V.redraw) {
142                 V.redraw = false;
143                 glutPostRedisplay();
144         }
145
146         time_sleep(0.1f);
147 }
148
149 void view_main_loop(const char *title, int width, int height,
150         ViewInitFunc initf, ViewExitFunc exitf,
151         ViewResizeFunc resize, ViewDisplayFunc display,
152         ViewKeyboardFunc keyboard)
153 {
154         const char *name = "app";
155         char *argv = (char*)name;
156         int argc = 1;
157
158         memset(&V, 0, sizeof(V));
159         V.width = width;
160         V.height = height;
161         V.first_display = true;
162         V.redraw = false;
163         V.initf = initf;
164         V.exitf = exitf;
165         V.resize = resize;
166         V.display = display;
167         V.keyboard = keyboard;
168
169         glutInit(&argc, &argv);
170         glutInitWindowSize(width, height);
171         glutInitWindowPosition(0, 0);
172         glutInitDisplayMode(GLUT_RGB|GLUT_DOUBLE|GLUT_DEPTH);
173         glutCreateWindow(title);
174
175 #ifndef __APPLE__
176         glewInit();
177 #endif
178
179         view_reshape(width, height);
180
181         glutDisplayFunc(view_display);
182         glutIdleFunc(view_idle);
183         glutReshapeFunc(view_reshape);
184         glutKeyboardFunc(view_keyboard);
185
186         glutMainLoop();
187 }
188
189 void view_redraw()
190 {
191         V.redraw = true;
192 }
193
194 CCL_NAMESPACE_END
195