Blender V4.3
window.cpp
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2011-2022 Blender Foundation
2 *
3 * SPDX-License-Identifier: Apache-2.0 */
4
5#include <stdio.h>
6#include <stdlib.h>
7
8#include "app/opengl/window.h"
9
10#include "util/string.h"
11#include "util/thread.h"
12#include "util/time.h"
13#include "util/version.h"
14
15#include <SDL.h>
16#include <epoxy/gl.h>
17
19
20/* structs */
21
22struct Window {
26 WindowDisplayFunc display = nullptr;
28 WindowMotionFunc motion = nullptr;
29
30 bool first_display = true;
31 bool redraw = false;
32
33 int mouseX = 0, mouseY = 0;
34 int mouseBut0 = 0, mouseBut2 = 0;
35
36 int width = 0, height = 0;
37
38 SDL_Window *window = nullptr;
39 SDL_GLContext gl_context = nullptr;
41} V;
42
43/* public */
44
45static void window_display_text(int x, int y, const char *text)
46{
47/* Not currently supported, need to add text rendering support. */
48#if 0
49 const char *c;
50
51 glRasterPos3f(x, y, 0);
52 glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
53
54 printf("display %s\n", text);
55
56 for (c = text; *c != '\0'; c++) {
57 const uint8_t *bitmap = helvetica10_character_map[*c];
58 glBitmap(bitmap[0],
59 helvetica10_height,
60 helvetica10_x_offset,
61 helvetica10_y_offset,
62 bitmap[0],
63 0.0f,
64 bitmap + 1);
65 }
66#else
67 static string last_text = "";
68
69 if (text != last_text) {
70 printf("%s\n", text);
71 last_text = text;
72 }
73#endif
74}
75
76void window_display_info(const char *info)
77{
78 const int height = 20;
79
80#if 0
81 glEnable(GL_BLEND);
82 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
83 glColor4f(0.1f, 0.1f, 0.1f, 0.8f);
84 glRectf(0.0f, V.height - height, V.width, V.height);
85 glDisable(GL_BLEND);
86
87 glColor3f(0.5f, 0.5f, 0.5f);
88#endif
89
90 window_display_text(10, 7 + V.height - height, info);
91
92#if 0
93 glColor3f(1.0f, 1.0f, 1.0f);
94#endif
95}
96
98{
99 const int w = (int)((float)V.width / 1.15f);
100 const int h = (int)((float)V.height / 1.15f);
101
102 const int x1 = (V.width - w) / 2;
103#if 0
104 const int x2 = x1 + w;
105#endif
106
107 const int y1 = (V.height - h) / 2;
108 const int y2 = y1 + h;
109
110#if 0
111 glEnable(GL_BLEND);
112 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
113 glColor4f(0.5f, 0.5f, 0.5f, 0.8f);
114 glRectf(x1, y1, x2, y2);
115 glDisable(GL_BLEND);
116
117 glColor3f(0.8f, 0.8f, 0.8f);
118#endif
119
120 string info = string("Cycles Renderer ") + CYCLES_VERSION_STRING;
121
122 window_display_text(x1 + 20, y2 - 20, info.c_str());
123 window_display_text(x1 + 20, y2 - 40, "(C) 2011-2016 Blender Foundation");
124 window_display_text(x1 + 20, y2 - 80, "Controls:");
125 window_display_text(x1 + 20, y2 - 100, "h: Info/Help");
126 window_display_text(x1 + 20, y2 - 120, "r: Reset");
127 window_display_text(x1 + 20, y2 - 140, "p: Pause");
128 window_display_text(x1 + 20, y2 - 160, "esc: Cancel");
129 window_display_text(x1 + 20, y2 - 180, "q: Quit program");
130
131 window_display_text(x1 + 20, y2 - 210, "i: Interactive mode");
132 window_display_text(x1 + 20, y2 - 230, "Left mouse: Move camera");
133 window_display_text(x1 + 20, y2 - 250, "Right mouse: Rotate camera");
134 window_display_text(x1 + 20, y2 - 270, "W/A/S/D: Move camera");
135 window_display_text(x1 + 20, y2 - 290, "0/1/2/3: Set max bounces");
136
137#if 0
138 glColor3f(1.0f, 1.0f, 1.0f);
139#endif
140}
141
142static void window_display()
143{
144 if (V.first_display) {
145 if (V.initf) {
146 V.initf();
147 }
148 if (V.exitf) {
149 atexit(V.exitf);
150 }
151
152 V.first_display = false;
153 }
154
156
157 glViewport(0, 0, V.width, V.height);
158
159 glMatrixMode(GL_PROJECTION);
160 glLoadIdentity();
161
162 glMatrixMode(GL_MODELVIEW);
163 glLoadIdentity();
164
165 glClearColor(0.05f, 0.05f, 0.05f, 0.0f);
166 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
167
168 glMatrixMode(GL_PROJECTION);
169 glLoadIdentity();
170 glOrtho(0, V.width, 0, V.height, -1, 1);
171
172 glMatrixMode(GL_MODELVIEW);
173 glLoadIdentity();
174
175 glRasterPos3f(0, 0, 0);
176
177 if (V.display) {
178 V.display();
179 }
180
181 SDL_GL_SwapWindow(V.window);
183}
184
185static void window_reshape(int width, int height)
186{
187 if (V.width != width || V.height != height) {
188 if (V.resize) {
189 V.resize(width, height);
190 }
191 }
192
193 V.width = width;
194 V.height = height;
195}
196
197static bool window_keyboard(unsigned char key)
198{
199 if (V.keyboard) {
200 V.keyboard(key);
201 }
202
203 if (key == 'q') {
204 if (V.exitf) {
205 V.exitf();
206 }
207 return true;
208 }
209
210 return false;
211}
212
213static void window_mouse(int button, int state, int x, int y)
214{
215 if (button == SDL_BUTTON_LEFT) {
216 if (state == SDL_MOUSEBUTTONDOWN) {
217 V.mouseX = x;
218 V.mouseY = y;
219 V.mouseBut0 = 1;
220 }
221 else if (state == SDL_MOUSEBUTTONUP) {
222 V.mouseBut0 = 0;
223 }
224 }
225 else if (button == SDL_BUTTON_RIGHT) {
226 if (state == SDL_MOUSEBUTTONDOWN) {
227 V.mouseX = x;
228 V.mouseY = y;
229 V.mouseBut2 = 1;
230 }
231 else if (state == SDL_MOUSEBUTTONUP) {
232 V.mouseBut2 = 0;
233 }
234 }
235}
236
237static void window_motion(int x, int y)
238{
239 const int but = V.mouseBut0 ? 0 : 2;
240 const int distX = x - V.mouseX;
241 const int distY = y - V.mouseY;
242
243 if (V.motion) {
244 V.motion(distX, distY, but);
245 }
246
247 V.mouseX = x;
248 V.mouseY = y;
249}
250
252{
253 V.gl_context_mutex.lock();
254 SDL_GL_MakeCurrent(V.window, V.gl_context);
255 return true;
256}
257
259{
260 SDL_GL_MakeCurrent(V.window, nullptr);
261 V.gl_context_mutex.unlock();
262}
263
264void window_main_loop(const char *title,
265 int width,
266 int height,
268 WindowExitFunc exitf,
269 WindowResizeFunc resize,
270 WindowDisplayFunc display,
271 WindowKeyboardFunc keyboard,
272 WindowMotionFunc motion)
273{
274 V.width = width;
275 V.height = height;
276 V.first_display = true;
277 V.redraw = false;
278 V.initf = initf;
279 V.exitf = exitf;
280 V.resize = resize;
281 V.display = display;
282 V.keyboard = keyboard;
283 V.motion = motion;
284
285 SDL_Init(SDL_INIT_VIDEO);
286 SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE);
287 SDL_GL_SetAttribute(SDL_GL_SHARE_WITH_CURRENT_CONTEXT, 1);
288 V.window = SDL_CreateWindow(title,
289 SDL_WINDOWPOS_UNDEFINED,
290 SDL_WINDOWPOS_UNDEFINED,
291 width,
292 height,
293 SDL_WINDOW_RESIZABLE | SDL_WINDOW_OPENGL | SDL_WINDOW_SHOWN);
294 if (V.window == nullptr) {
295 fprintf(stderr, "Failed to create window: %s\n", SDL_GetError());
296 return;
297 }
298
299 SDL_RaiseWindow(V.window);
300
301 V.gl_context = SDL_GL_CreateContext(V.window);
302 SDL_GL_MakeCurrent(V.window, nullptr);
303
304 window_reshape(width, height);
306
307 while (true) {
308 bool quit = false;
309 SDL_Event event;
310 while (!quit && SDL_PollEvent(&event)) {
311 if (event.type == SDL_TEXTINPUT) {
312 quit = window_keyboard(event.text.text[0]);
313 }
314 else if (event.type == SDL_MOUSEMOTION) {
315 window_motion(event.motion.x, event.motion.y);
316 }
317 else if (event.type == SDL_MOUSEBUTTONDOWN || event.type == SDL_MOUSEBUTTONUP) {
318 window_mouse(event.button.button, event.button.state, event.button.x, event.button.y);
319 }
320 else if (event.type == SDL_WINDOWEVENT) {
321 if (event.window.event == SDL_WINDOWEVENT_RESIZED ||
322 event.window.event == SDL_WINDOWEVENT_SIZE_CHANGED)
323 {
324 window_reshape(event.window.data1, event.window.data2);
325 }
326 }
327 else if (event.type == SDL_QUIT) {
328 if (V.exitf) {
329 V.exitf();
330 }
331 quit = true;
332 }
333 }
334
335 if (quit) {
336 break;
337 }
338
339 if (V.redraw) {
340 V.redraw = false;
342 }
343
344 SDL_WaitEventTimeout(NULL, 100);
345 }
346
347 SDL_GL_DeleteContext(V.gl_context);
348 SDL_DestroyWindow(V.window);
349 SDL_Quit();
350}
351
353{
354 V.redraw = true;
355}
356
SIMD_FORCE_INLINE const btScalar & w() const
Return the w value.
Definition btQuadWord.h:119
#define printf
#define CCL_NAMESPACE_END
#define NULL
draw_view push_constant(Type::INT, "radiance_src") .push_constant(Type capture_info_buf storage_buf(1, Qualifier::READ, "ObjectBounds", "bounds_buf[]") .push_constant(Type draw_view int
static ulong state[N]
static int initf
unsigned char uint8_t
Definition stdint.h:78
WindowKeyboardFunc keyboard
Definition window.cpp:27
WindowResizeFunc resize
Definition window.cpp:25
int mouseBut2
Definition window.cpp:34
WindowMotionFunc motion
Definition window.cpp:28
WindowDisplayFunc display
Definition window.cpp:26
WindowExitFunc exitf
Definition window.cpp:24
SDL_GLContext gl_context
Definition window.cpp:39
thread_mutex gl_context_mutex
Definition window.cpp:40
int mouseY
Definition window.cpp:33
WindowInitFunc initf
Definition window.cpp:23
bool redraw
Definition window.cpp:31
int mouseBut0
Definition window.cpp:34
int mouseX
Definition window.cpp:33
bool first_display
Definition window.cpp:30
SDL_Window * window
Definition window.cpp:38
int height
Definition window.cpp:36
int width
Definition window.cpp:36
CCL_NAMESPACE_BEGIN typedef std::mutex thread_mutex
Definition thread.h:29
#define CYCLES_VERSION_STRING
Definition version.h:18
void window_main_loop(const char *title, int width, int height, WindowInitFunc initf, WindowExitFunc exitf, WindowResizeFunc resize, WindowDisplayFunc display, WindowKeyboardFunc keyboard, WindowMotionFunc motion)
Definition window.cpp:264
void window_opengl_context_disable()
Definition window.cpp:258
static void window_reshape(int width, int height)
Definition window.cpp:185
bool window_opengl_context_enable()
Definition window.cpp:251
void window_display_info(const char *info)
Definition window.cpp:76
static void window_display()
Definition window.cpp:142
static void window_mouse(int button, int state, int x, int y)
Definition window.cpp:213
static void window_display_text(int x, int y, const char *text)
Definition window.cpp:45
CCL_NAMESPACE_BEGIN struct Window V
void window_redraw()
Definition window.cpp:352
static bool window_keyboard(unsigned char key)
Definition window.cpp:197
void window_display_help()
Definition window.cpp:97
static void window_motion(int x, int y)
Definition window.cpp:237
void(* WindowKeyboardFunc)(unsigned char key)
Definition window.h:16
void(* WindowExitFunc)()
Definition window.h:13
CCL_NAMESPACE_BEGIN typedef void(* WindowInitFunc)()
Definition window.h:12
void(* WindowDisplayFunc)()
Definition window.h:15
void(* WindowMotionFunc)(int x, int y, int button)
Definition window.h:17
void(* WindowResizeFunc)(int width, int height)
Definition window.h:14