cursor.c
Go to the documentation of this file.
1 //========================================================================
2 // Cursor & input mode tests
3 // Copyright (c) Camilla Löwy <elmindreda@glfw.org>
4 //
5 // This software is provided 'as-is', without any express or implied
6 // warranty. In no event will the authors be held liable for any damages
7 // arising from the use of this software.
8 //
9 // Permission is granted to anyone to use this software for any purpose,
10 // including commercial applications, and to alter it and redistribute it
11 // freely, subject to the following restrictions:
12 //
13 // 1. The origin of this software must not be misrepresented; you must not
14 // claim that you wrote the original software. If you use this software
15 // in a product, an acknowledgment in the product documentation would
16 // be appreciated but is not required.
17 //
18 // 2. Altered source versions must be plainly marked as such, and must not
19 // be misrepresented as being the original software.
20 //
21 // 3. This notice may not be removed or altered from any source
22 // distribution.
23 //
24 //========================================================================
25 //
26 // This test provides an interface to the cursor image and cursor mode
27 // parts of the API.
28 //
29 // Custom cursor image generation by urraka.
30 //
31 //========================================================================
32 
33 #include <glad/glad.h>
34 #include <GLFW/glfw3.h>
35 
36 #if defined(_MSC_VER)
37  // Make MS math.h define M_PI
38  #define _USE_MATH_DEFINES
39 #endif
40 
41 #include <math.h>
42 #include <stdio.h>
43 #include <stdlib.h>
44 
45 #include "linmath.h"
46 
47 #define CURSOR_FRAME_COUNT 60
48 
49 static const char* vertex_shader_text =
50 "#version 110\n"
51 "uniform mat4 MVP;\n"
52 "attribute vec2 vPos;\n"
53 "void main()\n"
54 "{\n"
55 " gl_Position = MVP * vec4(vPos, 0.0, 1.0);\n"
56 "}\n";
57 
58 static const char* fragment_shader_text =
59 "#version 110\n"
60 "void main()\n"
61 "{\n"
62 " gl_FragColor = vec4(1.0);\n"
63 "}\n";
64 
65 static double cursor_x;
66 static double cursor_y;
67 static int swap_interval = 1;
68 static int wait_events = GLFW_TRUE;
70 static int track_cursor = GLFW_FALSE;
72 
73 static void error_callback(int error, const char* description)
74 {
75  fprintf(stderr, "Error: %s\n", description);
76 }
77 
78 static float star(int x, int y, float t)
79 {
80  const float c = 64 / 2.f;
81 
82  const float i = (0.25f * (float) sin(2.f * M_PI * t) + 0.75f);
83  const float k = 64 * 0.046875f * i;
84 
85  const float dist = (float) sqrt((x - c) * (x - c) + (y - c) * (y - c));
86 
87  const float salpha = 1.f - dist / c;
88  const float xalpha = (float) x == c ? c : k / (float) fabs(x - c);
89  const float yalpha = (float) y == c ? c : k / (float) fabs(y - c);
90 
91  return (float) fmax(0.f, fmin(1.f, i * salpha * 0.2f + salpha * xalpha * yalpha));
92 }
93 
95 {
96  int i = 0, x, y;
97  unsigned char buffer[64 * 64 * 4];
98  const GLFWimage image = { 64, 64, buffer };
99 
100  for (y = 0; y < image.width; y++)
101  {
102  for (x = 0; x < image.height; x++)
103  {
104  buffer[i++] = 255;
105  buffer[i++] = 255;
106  buffer[i++] = 255;
107  buffer[i++] = (unsigned char) (255 * star(x, y, t));
108  }
109  }
110 
111  return glfwCreateCursor(&image, image.width / 2, image.height / 2);
112 }
113 
114 static void cursor_position_callback(GLFWwindow* window, double x, double y)
115 {
116  printf("%0.3f: Cursor position: %f %f (%+f %+f)\n",
117  glfwGetTime(),
118  x, y, x - cursor_x, y - cursor_y);
119 
120  cursor_x = x;
121  cursor_y = y;
122 }
123 
124 static void key_callback(GLFWwindow* window, int key, int scancode, int action, int mods)
125 {
126  if (action != GLFW_PRESS)
127  return;
128 
129  switch (key)
130  {
131  case GLFW_KEY_A:
132  {
134  if (!animate_cursor)
136 
137  break;
138  }
139 
140  case GLFW_KEY_ESCAPE:
141  {
143  {
145  break;
146  }
147 
148  /* FALLTHROUGH */
149  }
150 
151  case GLFW_KEY_N:
153  printf("(( cursor is normal ))\n");
154  break;
155 
156  case GLFW_KEY_D:
158  printf("(( cursor is disabled ))\n");
159  break;
160 
161  case GLFW_KEY_H:
163  printf("(( cursor is hidden ))\n");
164  break;
165 
166  case GLFW_KEY_SPACE:
168  printf("(( swap interval: %i ))\n", swap_interval);
170  break;
171 
172  case GLFW_KEY_W:
174  printf("(( %sing for events ))\n", wait_events ? "wait" : "poll");
175  break;
176 
177  case GLFW_KEY_T:
179  break;
180 
181  case GLFW_KEY_0:
183  break;
184 
185  case GLFW_KEY_1:
187  break;
188 
189  case GLFW_KEY_2:
191  break;
192 
193  case GLFW_KEY_3:
195  break;
196 
197  case GLFW_KEY_4:
199  break;
200 
201  case GLFW_KEY_5:
203  break;
204 
205  case GLFW_KEY_6:
207  break;
208  }
209 }
210 
211 int main(void)
212 {
213  int i;
215  GLFWcursor* star_cursors[CURSOR_FRAME_COUNT];
216  GLFWcursor* current_frame = NULL;
217  GLuint vertex_buffer, vertex_shader, fragment_shader, program;
218  GLint mvp_location, vpos_location;
219 
221 
222  if (!glfwInit())
223  exit(EXIT_FAILURE);
224 
225  for (i = 0; i < CURSOR_FRAME_COUNT; i++)
226  {
227  star_cursors[i] = create_cursor_frame(i / (float) CURSOR_FRAME_COUNT);
228  if (!star_cursors[i])
229  {
230  glfwTerminate();
231  exit(EXIT_FAILURE);
232  }
233  }
234 
235  for (i = 0; i < sizeof(standard_cursors) / sizeof(standard_cursors[0]); i++)
236  {
237  const int shapes[] = {
244  };
245 
247  if (!standard_cursors[i])
248  {
249  glfwTerminate();
250  exit(EXIT_FAILURE);
251  }
252  }
253 
256 
257  window = glfwCreateWindow(640, 480, "Cursor Test", NULL, NULL);
258  if (!window)
259  {
260  glfwTerminate();
261  exit(EXIT_FAILURE);
262  }
263 
266 
267  glGenBuffers(1, &vertex_buffer);
268  glBindBuffer(GL_ARRAY_BUFFER, vertex_buffer);
269 
270  vertex_shader = glCreateShader(GL_VERTEX_SHADER);
271  glShaderSource(vertex_shader, 1, &vertex_shader_text, NULL);
272  glCompileShader(vertex_shader);
273 
274  fragment_shader = glCreateShader(GL_FRAGMENT_SHADER);
275  glShaderSource(fragment_shader, 1, &fragment_shader_text, NULL);
276  glCompileShader(fragment_shader);
277 
278  program = glCreateProgram();
279  glAttachShader(program, vertex_shader);
280  glAttachShader(program, fragment_shader);
281  glLinkProgram(program);
282 
283  mvp_location = glGetUniformLocation(program, "MVP");
284  vpos_location = glGetAttribLocation(program, "vPos");
285 
286  glEnableVertexAttribArray(vpos_location);
287  glVertexAttribPointer(vpos_location, 2, GL_FLOAT, GL_FALSE,
288  sizeof(vec2), (void*) 0);
289  glUseProgram(program);
290 
292  printf("Cursor position: %f %f\n", cursor_x, cursor_y);
293 
296 
297  while (!glfwWindowShouldClose(window))
298  {
300 
301  if (track_cursor)
302  {
303  int wnd_width, wnd_height, fb_width, fb_height;
304  float scale;
305  vec2 vertices[4];
306  mat4x4 mvp;
307 
308  glfwGetWindowSize(window, &wnd_width, &wnd_height);
309  glfwGetFramebufferSize(window, &fb_width, &fb_height);
310 
311  glViewport(0, 0, fb_width, fb_height);
312 
313  scale = (float) fb_width / (float) wnd_width;
314  vertices[0][0] = 0.f;
315  vertices[0][1] = (float) (fb_height - cursor_y * scale);
316  vertices[1][0] = (float) fb_width;
317  vertices[1][1] = (float) (fb_height - cursor_y * scale);
318  vertices[2][0] = (float) (cursor_x * scale);
319  vertices[2][1] = 0.f;
320  vertices[3][0] = (float) (cursor_x * scale);
321  vertices[3][1] = (float) fb_height;
322 
324  sizeof(vertices),
325  vertices,
327 
328  mat4x4_ortho(mvp, 0.f, (float) fb_width, 0.f, (float) fb_height, 0.f, 1.f);
329  glUniformMatrix4fv(mvp_location, 1, GL_FALSE, (const GLfloat*) mvp);
330 
331  glDrawArrays(GL_LINES, 0, 4);
332  }
333 
335 
336  if (animate_cursor)
337  {
338  const int i = (int) (glfwGetTime() * 30.0) % CURSOR_FRAME_COUNT;
339  if (current_frame != star_cursors[i])
340  {
341  glfwSetCursor(window, star_cursors[i]);
342  current_frame = star_cursors[i];
343  }
344  }
345  else
346  current_frame = NULL;
347 
348  if (wait_events)
349  {
350  if (animate_cursor)
351  glfwWaitEventsTimeout(1.0 / 30.0);
352  else
353  glfwWaitEvents();
354  }
355  else
356  glfwPollEvents();
357 
358  // Workaround for an issue with msvcrt and mintty
359  fflush(stdout);
360  }
361 
363 
364  for (i = 0; i < CURSOR_FRAME_COUNT; i++)
365  glfwDestroyCursor(star_cursors[i]);
366 
367  for (i = 0; i < sizeof(standard_cursors) / sizeof(standard_cursors[0]); i++)
369 
370  glfwTerminate();
371  exit(EXIT_SUCCESS);
372 }
373 
glfwCreateCursor
GLFWAPI GLFWcursor * glfwCreateCursor(const GLFWimage *image, int xhot, int yhot)
Creates a custom cursor.
Definition: input.c:692
GLfloat
khronos_float_t GLfloat
Definition: glad/glad/glad.h:108
glfwSwapInterval
GLFWAPI void glfwSwapInterval(int interval)
Sets the swap interval for the current context.
Definition: context.c:658
test-ts-eof.stdout
stdout
Definition: test-ts-eof.py:101
GLFW_CURSOR_DISABLED
#define GLFW_CURSOR_DISABLED
Definition: glfw3.h:1008
GL_COLOR_BUFFER_BIT
#define GL_COLOR_BUFFER_BIT
Definition: glad/glad/glad.h:144
GLFW_CURSOR_NORMAL
#define GLFW_CURSOR_NORMAL
Definition: glfw3.h:1006
GLFW_CURSOR
#define GLFW_CURSOR
Definition: glfw3.h:1001
GLFW_KEY_6
#define GLFW_KEY_6
Definition: glfw3.h:372
glfwSetCursor
GLFWAPI void glfwSetCursor(GLFWwindow *window, GLFWcursor *cursor)
Sets the cursor for the window.
Definition: input.c:778
GLFW_CURSOR_HIDDEN
#define GLFW_CURSOR_HIDDEN
Definition: glfw3.h:1007
GLFW_KEY_2
#define GLFW_KEY_2
Definition: glfw3.h:368
glfwMakeContextCurrent
GLFWAPI void glfwMakeContextCurrent(GLFWwindow *window)
Makes the context of the specified window current for the calling thread.
Definition: context.c:611
GLFW_KEY_T
#define GLFW_KEY_T
Definition: glfw3.h:397
glfwTerminate
GLFWAPI void glfwTerminate(void)
Terminates the GLFW library.
Definition: init.c:243
glUseProgram
#define glUseProgram
Definition: glad/glad/glad.h:2866
GLFW_KEY_D
#define GLFW_KEY_D
Definition: glfw3.h:381
glfwCreateStandardCursor
GLFWAPI GLFWcursor * glfwCreateStandardCursor(int shape)
Creates a cursor with a standard shape.
Definition: input.c:713
GLFW_ARROW_CURSOR
#define GLFW_ARROW_CURSOR
The regular arrow cursor shape.
Definition: glfw3.h:1030
swap_interval
static int swap_interval
Definition: cursor.c:67
GL_STREAM_DRAW
#define GL_STREAM_DRAW
Definition: glad/glad/glad.h:874
glfwPollEvents
GLFWAPI void glfwPollEvents(void)
Processes all pending events.
Definition: window.c:1072
glDrawArrays
#define glDrawArrays
Definition: glad/glad/glad.h:2303
GLFW_PRESS
#define GLFW_PRESS
The key or mouse button was pressed.
Definition: glfw3.h:304
track_cursor
static int track_cursor
Definition: cursor.c:70
glGenBuffers
#define glGenBuffers
Definition: glad/glad/glad.h:2730
glfw3.h
The header of the GLFW 3 API.
glEnableVertexAttribArray
#define glEnableVertexAttribArray
Definition: glad/glad/glad.h:2803
GL_ARRAY_BUFFER
#define GL_ARRAY_BUFFER
Definition: glad/glad/glad.h:863
glShaderSource
#define glShaderSource
Definition: glad/glad/glad.h:2863
window
static GLFWwindow * window
Definition: joysticks.c:55
GL_FALSE
#define GL_FALSE
Definition: glad/glad/glad.h:145
GLFW_CONTEXT_VERSION_MINOR
#define GLFW_CONTEXT_VERSION_MINOR
Context client API minor version hint and attribute.
Definition: glfw3.h:927
GLFW_KEY_W
#define GLFW_KEY_W
Definition: glfw3.h:400
glViewport
#define glViewport
Definition: glad/glad/glad.h:1522
GLFW_KEY_SPACE
#define GLFW_KEY_SPACE
Definition: glfw3.h:360
GLFW_KEY_4
#define GLFW_KEY_4
Definition: glfw3.h:370
glfwSetCursorPosCallback
GLFWAPI GLFWcursorposfun glfwSetCursorPosCallback(GLFWwindow *window, GLFWcursorposfun cbfun)
Sets the cursor position callback.
Definition: input.c:832
mat4x4_ortho
static void mat4x4_ortho(mat4x4 M, float l, float r, float b, float t, float n, float f)
Definition: linmath.h:341
standard_cursors
static GLFWcursor * standard_cursors[6]
Definition: cursor.c:71
GL_LINES
#define GL_LINES
Definition: glad/glad/glad.h:148
glCompileShader
#define glCompileShader
Definition: glad/glad/glad.h:2782
GLFW_KEY_A
#define GLFW_KEY_A
Definition: glfw3.h:378
python-tutorial-1-depth.dist
dist
Definition: python-tutorial-1-depth.py:33
glfwSetErrorCallback
GLFWAPI GLFWerrorfun glfwSetErrorCallback(GLFWerrorfun cbfun)
Sets the error callback.
Definition: init.c:309
linmath.h
glfwWaitEvents
GLFWAPI void glfwWaitEvents(void)
Waits until events are queued and processes them.
Definition: window.c:1078
GL_VERTEX_SHADER
#define GL_VERTEX_SHADER
Definition: glad/glad/glad.h:941
GL_FLOAT
#define GL_FLOAT
Definition: glad/glad/glad.h:262
GLFW_KEY_1
#define GLFW_KEY_1
Definition: glfw3.h:367
glfwGetWindowSize
GLFWAPI void glfwGetWindowSize(GLFWwindow *window, int *width, int *height)
Retrieves the size of the client area of the specified window.
Definition: window.c:544
glfwGetCursorPos
GLFWAPI void glfwGetCursorPos(GLFWwindow *window, double *xpos, double *ypos)
Retrieves the position of the cursor relative to the client area of the window.
Definition: input.c:637
glfwSwapBuffers
GLFWAPI void glfwSwapBuffers(GLFWwindow *window)
Swaps the front and back buffers of the specified window.
Definition: context.c:641
GLFW_IBEAM_CURSOR
#define GLFW_IBEAM_CURSOR
The text input I-beam cursor shape.
Definition: glfw3.h:1035
GLFW_HAND_CURSOR
#define GLFW_HAND_CURSOR
The hand shape.
Definition: glfw3.h:1045
gladLoadGLLoader
GLAPI int gladLoadGLLoader(GLADloadproc)
Definition: glad/glad.c:1690
GLFW_CROSSHAIR_CURSOR
#define GLFW_CROSSHAIR_CURSOR
The crosshair shape.
Definition: glfw3.h:1040
GLFW_FALSE
#define GLFW_FALSE
Zero.
Definition: glfw3.h:287
NULL
#define NULL
Definition: tinycthread.c:47
vertex_shader_text
static const char * vertex_shader_text
Definition: cursor.c:49
test-ts-eof.stderr
stderr
Definition: test-ts-eof.py:102
buffer
GLenum GLfloat * buffer
Definition: glad/glad/glad.h:2066
wait_events
static int wait_events
Definition: cursor.c:68
glLinkProgram
#define glLinkProgram
Definition: glad/glad/glad.h:2860
GLFW_TRUE
#define GLFW_TRUE
One.
Definition: glfw3.h:279
f
GLdouble f
Definition: glad/glad/glad.h:1517
i
int i
Definition: rs-pcl-color.cpp:54
GLFW_CONTEXT_VERSION_MAJOR
#define GLFW_CONTEXT_VERSION_MAJOR
Context client API major version hint and attribute.
Definition: glfw3.h:921
GLint
int GLint
Definition: glad/glad/glad.h:104
fragment_shader_text
static const char * fragment_shader_text
Definition: cursor.c:58
cursor_x
static double cursor_x
Definition: cursor.c:65
GLFWwindow
struct GLFWwindow GLFWwindow
Definition: rs_processing_gl.h:43
GLADloadproc
void *(* GLADloadproc)(const char *name)
Definition: glad/glad/glad.h:64
glBufferData
#define glBufferData
Definition: glad/glad/glad.h:2736
vertices
static const struct @17 vertices[3]
glfwInit
GLFWAPI int glfwInit(void)
Initializes the GLFW library.
Definition: init.c:198
glfwGetProcAddress
GLFWAPI GLFWglproc glfwGetProcAddress(const char *procname)
Returns the address of the specified function for the current context.
Definition: context.c:741
error_callback
static void error_callback(int error, const char *description)
Definition: cursor.c:73
net_viewer.k
int k
Definition: net_viewer.py:64
window
Definition: example.hpp:513
main
int main(void)
Definition: cursor.c:211
glfwGetTime
GLFWAPI double glfwGetTime(void)
Returns the value of the GLFW timer.
Definition: input.c:1275
rspy.devices.action
string action
Definition: devices.py:737
GLFW_VRESIZE_CURSOR
#define GLFW_VRESIZE_CURSOR
The vertical resize arrow shape.
Definition: glfw3.h:1055
glfwDestroyWindow
GLFWAPI void glfwDestroyWindow(GLFWwindow *window)
Destroys the specified window and its context.
Definition: window.c:444
cursor_position_callback
static void cursor_position_callback(GLFWwindow *window, double x, double y)
Definition: cursor.c:114
GLFW_KEY_N
#define GLFW_KEY_N
Definition: glfw3.h:391
realsense_device_manager.c
c
Definition: realsense_device_manager.py:330
CURSOR_FRAME_COUNT
#define CURSOR_FRAME_COUNT
Definition: cursor.c:47
GLFW_KEY_3
#define GLFW_KEY_3
Definition: glfw3.h:369
key
void key(GLFWwindow *window, int k, int s, int action, int mods)
Definition: gears.c:213
GLFW_KEY_ESCAPE
#define GLFW_KEY_ESCAPE
Definition: glfw3.h:412
glfwGetInputMode
GLFWAPI int glfwGetInputMode(GLFWwindow *window, int mode)
Returns the value of an input option for the specified window.
Definition: input.c:461
GLFWcursor
struct GLFWcursor GLFWcursor
Opaque cursor object.
Definition: glfw3.h:1149
t
GLdouble t
Definition: glad/glad/glad.h:1829
cursor_y
static double cursor_y
Definition: cursor.c:66
animate_cursor
static int animate_cursor
Definition: cursor.c:69
GLFWimage
Image data.
Definition: glfw3.h:1588
GLFW_HRESIZE_CURSOR
#define GLFW_HRESIZE_CURSOR
The horizontal resize arrow shape.
Definition: glfw3.h:1050
glfwSetKeyCallback
GLFWAPI GLFWkeyfun glfwSetKeyCallback(GLFWwindow *window, GLFWkeyfun cbfun)
Sets the key callback.
Definition: input.c:791
glClear
#define glClear
Definition: glad/glad/glad.h:1423
glfwCreateWindow
GLFWAPI GLFWwindow * glfwCreateWindow(int width, int height, const char *title, GLFWmonitor *monitor, GLFWwindow *share)
Creates a window and its associated context.
Definition: window.c:151
rs2::textual_icons::exit
static const textual_icon exit
Definition: model-views.h:255
glfwWaitEventsTimeout
GLFWAPI void glfwWaitEventsTimeout(double timeout)
Waits with timeout until events are queued and processes them.
Definition: window.c:1088
image
GLenum GLenum GLsizei void * image
Definition: glad/glad/glad.h:3587
GLFW_KEY_H
#define GLFW_KEY_H
Definition: glfw3.h:385
GLFW_KEY_5
#define GLFW_KEY_5
Definition: glfw3.h:371
glCreateShader
#define glCreateShader
Definition: glad/glad/glad.h:2788
glfwSetInputMode
GLFWAPI void glfwSetInputMode(GLFWwindow *window, int mode, int value)
Sets an input option for the specified window.
Definition: input.c:484
glfwWindowHint
GLFWAPI void glfwWindowHint(int hint, int value)
Sets the specified window hint to the desired value.
Definition: window.c:291
glCreateProgram
#define glCreateProgram
Definition: glad/glad/glad.h:2785
x
GLdouble x
Definition: glad/glad/glad.h:2279
glGetAttribLocation
#define glGetAttribLocation
Definition: glad/glad/glad.h:2815
glfwWindowShouldClose
GLFWAPI int glfwWindowShouldClose(GLFWwindow *window)
Checks the close flag of the specified window.
Definition: window.c:477
key_callback
static void key_callback(GLFWwindow *window, int key, int scancode, int action, int mods)
Definition: cursor.c:124
glVertexAttribPointer
#define glVertexAttribPointer
Definition: glad/glad/glad.h:3037
glfwGetFramebufferSize
GLFWAPI void glfwGetFramebufferSize(GLFWwindow *window, int *width, int *height)
Retrieves the size of the framebuffer of the specified window.
Definition: window.c:647
glfwDestroyCursor
GLFWAPI void glfwDestroyCursor(GLFWcursor *cursor)
Destroys a cursor.
Definition: input.c:743
star
static float star(int x, int y, float t)
Definition: cursor.c:78
GLuint
unsigned int GLuint
Definition: glad/glad/glad.h:105
mat4x4
vec4 mat4x4[4]
Definition: linmath.h:83
glGetUniformLocation
#define glGetUniformLocation
Definition: glad/glad/glad.h:2833
GL_FRAGMENT_SHADER
#define GL_FRAGMENT_SHADER
Definition: glad/glad/glad.h:940
glUniformMatrix4fv
#define glUniformMatrix4fv
Definition: glad/glad/glad.h:2923
glfwSetWindowShouldClose
GLFWAPI void glfwSetWindowShouldClose(GLFWwindow *window, int value)
Sets the close flag of the specified window.
Definition: window.c:486
glBindBuffer
#define glBindBuffer
Definition: glad/glad/glad.h:2724
create_cursor_frame
static GLFWcursor * create_cursor_frame(float t)
Definition: cursor.c:94
y
GLint y
Definition: glad/glad/glad.h:1397
glAttachShader
#define glAttachShader
Definition: glad/glad/glad.h:2776
GLFW_KEY_0
#define GLFW_KEY_0
Definition: glfw3.h:366


librealsense2
Author(s): LibRealSense ROS Team
autogenerated on Thu Dec 22 2022 03:13:15