Cycles: Make all #include statements relative to cycles source directory
[blender.git] / intern / cycles / util / util_view.cpp
1 /*
2  * Copyright 2011-2013 Blender Foundation
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 #include <stdio.h>
18 #include <stdlib.h>
19
20 #include "util/util_opengl.h"
21 #include "util/util_string.h"
22 #include "util/util_time.h"
23 #include "util/util_version.h"
24 #include "util/util_view.h"
25
26 #ifdef __APPLE__
27 #include <GLUT/glut.h>
28 #else
29 #include <GL/glut.h>
30 #endif
31
32 CCL_NAMESPACE_BEGIN
33
34 /* structs */
35
36 struct View {
37         ViewInitFunc initf;
38         ViewExitFunc exitf;
39         ViewResizeFunc resize;
40         ViewDisplayFunc display;
41         ViewKeyboardFunc keyboard;
42         ViewMotionFunc motion;
43
44         bool first_display;
45         bool redraw;
46
47         int mouseX, mouseY;
48         int mouseBut0, mouseBut2;
49
50         int width, height;
51 } V;
52
53 /* public */
54
55 static void view_display_text(int x, int y, const char *text)
56 {
57         const char *c;
58
59         glRasterPos3f(x, y, 0);
60
61         for(c = text; *c != '\0'; c++)
62                 glutBitmapCharacter(GLUT_BITMAP_HELVETICA_10, *c);
63 }
64
65 void view_display_info(const char *info)
66 {
67         const int height = 20;
68
69         glEnable(GL_BLEND);
70         glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
71         glColor4f(0.1f, 0.1f, 0.1f, 0.8f);
72         glRectf(0.0f, V.height - height, V.width, V.height);
73         glDisable(GL_BLEND);
74
75         glColor3f(0.5f, 0.5f, 0.5f);
76
77         view_display_text(10, 7 + V.height - height, info);
78
79         glColor3f(1.0f, 1.0f, 1.0f);
80 }
81
82 void view_display_help()
83 {
84         const int w = (int)((float)V.width  / 1.15f);
85         const int h = (int)((float)V.height / 1.15f);
86
87         const int x1 = (V.width - w) / 2;
88         const int x2 = x1 + w;
89
90         const int y1 = (V.height - h) / 2;
91         const int y2 = y1 + h;
92
93         glEnable(GL_BLEND);
94         glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
95         glColor4f(0.5f, 0.5f, 0.5f, 0.8f);
96         glRectf(x1, y1, x2, y2);
97         glDisable(GL_BLEND);
98
99         glColor3f(0.8f, 0.8f, 0.8f);
100
101         string info = string("Cycles Renderer ") + CYCLES_VERSION_STRING;
102
103         view_display_text(x1+20, y2-20, info.c_str());
104         view_display_text(x1+20, y2-40, "(C) 2011-2016 Blender Foundation");
105         view_display_text(x1+20, y2-80, "Controls:");
106         view_display_text(x1+20, y2-100, "h:  Info/Help");
107         view_display_text(x1+20, y2-120, "r:  Reset");
108         view_display_text(x1+20, y2-140, "p:  Pause");
109         view_display_text(x1+20, y2-160, "esc:  Cancel");
110         view_display_text(x1+20, y2-180, "q:  Quit program");
111
112         view_display_text(x1+20, y2-210, "i:  Interactive mode");
113         view_display_text(x1+20, y2-230, "Left mouse:  Move camera");
114         view_display_text(x1+20, y2-250, "Right mouse:  Rotate camera");
115         view_display_text(x1+20, y2-270, "W/A/S/D:  Move camera");
116         view_display_text(x1+20, y2-290, "0/1/2/3:  Set max bounces");
117
118         glColor3f(1.0f, 1.0f, 1.0f);
119 }
120
121 static void view_display()
122 {
123         if(V.first_display) {
124                 if(V.initf) V.initf();
125                 if(V.exitf) atexit(V.exitf);
126
127                 V.first_display = false;
128         }
129
130         glClearColor(0.05f, 0.05f, 0.05f, 0.0f);
131         glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
132
133         glMatrixMode(GL_PROJECTION);
134         glLoadIdentity();
135         gluOrtho2D(0, V.width, 0, V.height);
136
137         glMatrixMode(GL_MODELVIEW);
138         glLoadIdentity();
139
140         glRasterPos3f(0, 0, 0);
141
142         if(V.display)
143                 V.display();
144
145         glutSwapBuffers();
146 }
147
148 static void view_reshape(int width, int height)
149 {
150         if(width <= 0 || height <= 0)
151                 return;
152
153         V.width = width;
154         V.height = height;
155
156         glViewport(0, 0, width, height);
157
158         glMatrixMode(GL_PROJECTION);
159         glLoadIdentity();
160
161         glMatrixMode(GL_MODELVIEW);
162         glLoadIdentity();
163
164         if(V.resize)
165                 V.resize(width, height);
166 }
167
168 static void view_keyboard(unsigned char key, int x, int y)
169 {
170         if(V.keyboard)
171                 V.keyboard(key);
172
173         if(key == 'm')
174                 printf("mouse %d %d\n", x, y);
175         if(key == 'q') {
176                 if(V.exitf) V.exitf();
177                 exit(0);
178         }
179 }
180
181 static void view_mouse(int button, int state, int x, int y)
182 {
183         if(button == 0) {
184                 if(state == GLUT_DOWN) {
185                         V.mouseX = x;
186                         V.mouseY = y;
187                         V.mouseBut0 = 1;
188                 }
189                 else if(state == GLUT_UP) {
190                         V.mouseBut0 = 0;
191                 }
192         }
193         else if(button == 2) {
194                 if(state == GLUT_DOWN) {
195                         V.mouseX = x;
196                         V.mouseY = y;
197                         V.mouseBut2 = 1;
198                 }
199                 else if(state == GLUT_UP) {
200                         V.mouseBut2 = 0;
201                 }
202         }
203 }
204
205 static void view_motion(int x, int y)
206 {
207         const int but = V.mouseBut0? 0:2;
208         const int distX = x - V.mouseX;
209         const int distY = y - V.mouseY;
210
211         if(V.motion)
212                 V.motion(distX, distY, but);
213
214         V.mouseX = x;
215         V.mouseY = y;
216 }
217
218 static void view_idle(void)
219 {
220         if(V.redraw) {
221                 V.redraw = false;
222                 glutPostRedisplay();
223         }
224
225         time_sleep(0.1);
226 }
227
228 void view_main_loop(const char *title, int width, int height,
229         ViewInitFunc initf, ViewExitFunc exitf,
230         ViewResizeFunc resize, ViewDisplayFunc display,
231         ViewKeyboardFunc keyboard, ViewMotionFunc motion)
232 {
233         const char *name = "app";
234         char *argv = (char*)name;
235         int argc = 1;
236
237         memset(&V, 0, sizeof(V));
238         V.width = width;
239         V.height = height;
240         V.first_display = true;
241         V.redraw = false;
242         V.initf = initf;
243         V.exitf = exitf;
244         V.resize = resize;
245         V.display = display;
246         V.keyboard = keyboard;
247         V.motion = motion;
248
249         glutInit(&argc, &argv);
250         glutInitWindowSize(width, height);
251         glutInitWindowPosition(0, 0);
252         glutInitDisplayMode(GLUT_RGB|GLUT_DOUBLE|GLUT_DEPTH);
253         glutCreateWindow(title);
254
255         mxMakeCurrentContext(mxCreateContext());
256
257         view_reshape(width, height);
258
259         glutDisplayFunc(view_display);
260         glutIdleFunc(view_idle);
261         glutReshapeFunc(view_reshape);
262         glutKeyboardFunc(view_keyboard);
263         glutMouseFunc(view_mouse);
264         glutMotionFunc(view_motion);
265
266         glutMainLoop();
267 }
268
269 void view_redraw()
270 {
271         V.redraw = true;
272 }
273
274 CCL_NAMESPACE_END
275