sharing.c
Go to the documentation of this file.
1 //========================================================================
2 // Context sharing example
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 #include <glad/glad.h>
27 #include <GLFW/glfw3.h>
28 
29 #include <stdio.h>
30 #include <stdlib.h>
31 
32 #include "getopt.h"
33 #include "linmath.h"
34 
35 static const char* vertex_shader_text =
36 "#version 110\n"
37 "uniform mat4 MVP;\n"
38 "attribute vec2 vPos;\n"
39 "varying vec2 texcoord;\n"
40 "void main()\n"
41 "{\n"
42 " gl_Position = MVP * vec4(vPos, 0.0, 1.0);\n"
43 " texcoord = vPos;\n"
44 "}\n";
45 
46 static const char* fragment_shader_text =
47 "#version 110\n"
48 "uniform sampler2D texture;\n"
49 "uniform vec3 color;\n"
50 "varying vec2 texcoord;\n"
51 "void main()\n"
52 "{\n"
53 " gl_FragColor = vec4(color * texture2D(texture, texcoord).rgb, 1.0);\n"
54 "}\n";
55 
56 static const vec2 vertices[4] =
57 {
58  { 0.f, 0.f },
59  { 1.f, 0.f },
60  { 1.f, 1.f },
61  { 0.f, 1.f }
62 };
63 
64 static void error_callback(int error, const char* description)
65 {
66  fprintf(stderr, "Error: %s\n", description);
67 }
68 
69 static void key_callback(GLFWwindow* window, int key, int scancode, int action, int mods)
70 {
71  if (action == GLFW_PRESS && key == GLFW_KEY_ESCAPE)
73 }
74 
75 int main(int argc, char** argv)
76 {
77  GLFWwindow* windows[2];
78  GLuint texture, program, vertex_buffer;
79  GLint mvp_location, vpos_location, color_location, texture_location;
80 
82 
83  if (!glfwInit())
84  exit(EXIT_FAILURE);
85 
88 
89  windows[0] = glfwCreateWindow(400, 400, "First", NULL, NULL);
90  if (!windows[0])
91  {
92  glfwTerminate();
93  exit(EXIT_FAILURE);
94  }
95 
96  glfwSetKeyCallback(windows[0], key_callback);
97 
98  glfwMakeContextCurrent(windows[0]);
99 
100  // Only enable vsync for the first of the windows to be swapped to
101  // avoid waiting out the interval for each window
102  glfwSwapInterval(1);
103 
104  // The contexts are created with the same APIs so the function
105  // pointers should be re-usable between them
107 
108  // Create the OpenGL objects inside the first context, created above
109  // All objects will be shared with the second context, created below
110  {
111  int x, y;
112  char pixels[16 * 16];
113  GLuint vertex_shader, fragment_shader;
114 
115  glGenTextures(1, &texture);
116  glBindTexture(GL_TEXTURE_2D, texture);
117 
118  srand((unsigned int) glfwGetTimerValue());
119 
120  for (y = 0; y < 16; y++)
121  {
122  for (x = 0; x < 16; x++)
123  pixels[y * 16 + x] = rand() % 256;
124  }
125 
129 
130  vertex_shader = glCreateShader(GL_VERTEX_SHADER);
131  glShaderSource(vertex_shader, 1, &vertex_shader_text, NULL);
132  glCompileShader(vertex_shader);
133 
134  fragment_shader = glCreateShader(GL_FRAGMENT_SHADER);
135  glShaderSource(fragment_shader, 1, &fragment_shader_text, NULL);
136  glCompileShader(fragment_shader);
137 
138  program = glCreateProgram();
139  glAttachShader(program, vertex_shader);
140  glAttachShader(program, fragment_shader);
141  glLinkProgram(program);
142 
143  mvp_location = glGetUniformLocation(program, "MVP");
144  color_location = glGetUniformLocation(program, "color");
145  texture_location = glGetUniformLocation(program, "texture");
146  vpos_location = glGetAttribLocation(program, "vPos");
147 
148  glGenBuffers(1, &vertex_buffer);
149  glBindBuffer(GL_ARRAY_BUFFER, vertex_buffer);
151  }
152 
153  glUseProgram(program);
154  glUniform1i(texture_location, 0);
155 
157  glBindTexture(GL_TEXTURE_2D, texture);
158 
159  glBindBuffer(GL_ARRAY_BUFFER, vertex_buffer);
160  glEnableVertexAttribArray(vpos_location);
161  glVertexAttribPointer(vpos_location, 2, GL_FLOAT, GL_FALSE,
162  sizeof(vertices[0]), (void*) 0);
163 
164  windows[1] = glfwCreateWindow(400, 400, "Second", NULL, windows[0]);
165  if (!windows[1])
166  {
167  glfwTerminate();
168  exit(EXIT_FAILURE);
169  }
170 
171  // Place the second window to the right of the first
172  {
173  int xpos, ypos, left, right, width;
174 
175  glfwGetWindowSize(windows[0], &width, NULL);
176  glfwGetWindowFrameSize(windows[0], &left, NULL, &right, NULL);
177  glfwGetWindowPos(windows[0], &xpos, &ypos);
178 
179  glfwSetWindowPos(windows[1], xpos + width + left + right, ypos);
180  }
181 
182  glfwSetKeyCallback(windows[1], key_callback);
183 
184  glfwMakeContextCurrent(windows[1]);
185 
186  // While objects are shared, the global context state is not and will
187  // need to be set up for each context
188 
189  glUseProgram(program);
190 
192  glBindTexture(GL_TEXTURE_2D, texture);
193 
194  glBindBuffer(GL_ARRAY_BUFFER, vertex_buffer);
195  glEnableVertexAttribArray(vpos_location);
196  glVertexAttribPointer(vpos_location, 2, GL_FLOAT, GL_FALSE,
197  sizeof(vertices[0]), (void*) 0);
198 
199  while (!glfwWindowShouldClose(windows[0]) &&
200  !glfwWindowShouldClose(windows[1]))
201  {
202  int i;
203  const vec3 colors[2] =
204  {
205  { 0.8f, 0.4f, 1.f },
206  { 0.3f, 0.4f, 1.f }
207  };
208 
209  for (i = 0; i < 2; i++)
210  {
211  int width, height;
212  mat4x4 mvp;
213 
214  glfwGetFramebufferSize(windows[i], &width, &height);
215  glfwMakeContextCurrent(windows[i]);
216 
217  glViewport(0, 0, width, height);
218 
219  mat4x4_ortho(mvp, 0.f, 1.f, 0.f, 1.f, 0.f, 1.f);
220  glUniformMatrix4fv(mvp_location, 1, GL_FALSE, (const GLfloat*) mvp);
221  glUniform3fv(color_location, 1, colors[i]);
223 
224  glfwSwapBuffers(windows[i]);
225  }
226 
227  glfwWaitEvents();
228  }
229 
230  glfwTerminate();
231  exit(EXIT_SUCCESS);
232 }
233 
#define GL_TEXTURE_MAG_FILTER
#define glBufferData
#define glCreateProgram
GLint y
GLFWAPI void glfwGetWindowSize(GLFWwindow *window, int *width, int *height)
Retrieves the size of the client area of the specified window.
Definition: window.c:544
#define GL_VERTEX_SHADER
static const vec2 vertices[4]
Definition: sharing.c:56
static void error_callback(int error, const char *description)
Definition: sharing.c:64
vec4 mat4x4[4]
Definition: linmath.h:83
#define GL_STATIC_DRAW
khronos_float_t GLfloat
The header of the GLFW 3 API.
GLFWAPI GLFWglproc glfwGetProcAddress(const char *procname)
Returns the address of the specified function for the current context.
Definition: context.c:741
#define GL_TEXTURE_2D
static void mat4x4_ortho(mat4x4 M, float l, float r, float b, float t, float n, float f)
Definition: linmath.h:341
#define glGetAttribLocation
void *(* GLADloadproc)(const char *name)
#define GL_UNSIGNED_BYTE
#define GLFW_CONTEXT_VERSION_MAJOR
Context client API major version hint and attribute.
Definition: glfw3.h:921
#define glDrawArrays
GLFWAPI GLFWerrorfun glfwSetErrorCallback(GLFWerrorfun cbfun)
Sets the error callback.
Definition: init.c:309
#define glUniformMatrix4fv
GLFWAPI uint64_t glfwGetTimerValue(void)
Returns the current value of the raw timer.
Definition: input.c:1296
#define glUseProgram
#define glTexImage2D
#define glEnable
#define GL_LUMINANCE
#define glCreateShader
GLuint64 key
Definition: glext.h:8966
#define glUniform1i
GLFWAPI int glfwInit(void)
Initializes the GLFW library.
Definition: init.c:198
GLdouble f
#define glVertexAttribPointer
float3 colors[]
Definition: rs-pcl.cpp:44
#define glGenTextures
#define GL_FLOAT
#define glEnableVertexAttribArray
#define glTexParameteri
GLbitfield GLuint program
Definition: glext.h:1889
int GLint
GLdouble x
GLFWAPI void glfwSwapInterval(int interval)
Sets the swap interval for the current context.
Definition: context.c:658
#define glBindBuffer
#define GLFW_KEY_ESCAPE
Definition: glfw3.h:412
GLint GLsizei GLsizei height
GLFWAPI void glfwSwapBuffers(GLFWwindow *window)
Swaps the front and back buffers of the specified window.
Definition: context.c:641
GLFWAPI void glfwMakeContextCurrent(GLFWwindow *window)
Makes the context of the specified window current for the calling thread.
Definition: context.c:611
#define GL_TEXTURE_MIN_FILTER
GLFWAPI void glfwGetWindowFrameSize(GLFWwindow *window, int *left, int *top, int *right, int *bottom)
Retrieves the size of the frame of the window.
Definition: window.c:661
GLint left
Definition: glext.h:1963
static const char * fragment_shader_text
Definition: sharing.c:46
static const char * vertex_shader_text
Definition: sharing.c:35
#define glGetUniformLocation
#define GL_FRAGMENT_SHADER
#define glShaderSource
GLint GLint GLsizei GLint GLenum GLenum const void * pixels
#define glUniform3fv
action
Definition: enums.py:62
#define glViewport
GLAPI int gladLoadGLLoader(GLADloadproc)
Definition: glad/glad.c:1697
static const textual_icon exit
Definition: model-views.h:254
GLFWAPI void glfwSetWindowPos(GLFWwindow *window, int xpos, int ypos)
Sets the position of the client area of the specified window.
Definition: window.c:531
#define glAttachShader
GLFWAPI void glfwSetWindowShouldClose(GLFWwindow *window, int value)
Sets the close flag of the specified window.
Definition: window.c:486
#define glCompileShader
GLFWAPI void glfwWaitEvents(void)
Waits until events are queued and processes them.
Definition: window.c:1078
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
#define GLFW_TRUE
One.
Definition: glfw3.h:279
#define GL_FALSE
static void key_callback(GLFWwindow *window, int key, int scancode, int action, int mods)
Definition: sharing.c:69
GLFWAPI void glfwGetWindowPos(GLFWwindow *window, int *xpos, int *ypos)
Retrieves the position of the client area of the specified window.
Definition: window.c:517
unsigned int GLuint
GLdouble right
#define glGenBuffers
static double xpos
Definition: splitview.c:33
GLFWAPI void glfwGetFramebufferSize(GLFWwindow *window, int *width, int *height)
Retrieves the size of the framebuffer of the specified window.
Definition: window.c:647
#define glBindTexture
#define glLinkProgram
#define GL_TRIANGLE_FAN
GLFWAPI void glfwTerminate(void)
Terminates the GLFW library.
Definition: init.c:243
GLFWAPI GLFWkeyfun glfwSetKeyCallback(GLFWwindow *window, GLFWkeyfun cbfun)
Sets the key callback.
Definition: input.c:791
#define NULL
Definition: tinycthread.c:47
GLuint texture
int i
#define GLFW_CONTEXT_VERSION_MINOR
Context client API minor version hint and attribute.
Definition: glfw3.h:927
#define GL_NEAREST
#define GLFW_PRESS
The key or mouse button was pressed.
Definition: glfw3.h:304
#define GL_ARRAY_BUFFER
int main(int argc, char **argv)
Definition: sharing.c:75
struct GLFWwindow GLFWwindow
GLFWAPI void glfwWindowHint(int hint, int value)
Sets the specified window hint to the desired value.
Definition: window.c:291
GLint GLsizei width
static double ypos
Definition: splitview.c:33
GLFWAPI int glfwWindowShouldClose(GLFWwindow *window)
Checks the close flag of the specified window.
Definition: window.c:477


librealsense2
Author(s): Sergey Dorodnicov , Doron Hirshberg , Mark Horn , Reagan Lopez , Itay Carpis
autogenerated on Mon May 3 2021 02:47:41