sharing.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  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. #include <glad/gl.h>
  26. #define GLFW_INCLUDE_NONE
  27. #include <GLFW/glfw3.h>
  28. #include <stdio.h>
  29. #include <stdlib.h>
  30. #include "getopt.h"
  31. #include "linmath.h"
  32. static const char* vertex_shader_text =
  33. "#version 110\n"
  34. "uniform mat4 MVP;\n"
  35. "attribute vec2 vPos;\n"
  36. "varying vec2 texcoord;\n"
  37. "void main()\n"
  38. "{\n"
  39. " gl_Position = MVP * vec4(vPos, 0.0, 1.0);\n"
  40. " texcoord = vPos;\n"
  41. "}\n";
  42. static const char* fragment_shader_text =
  43. "#version 110\n"
  44. "uniform sampler2D texture;\n"
  45. "uniform vec3 color;\n"
  46. "varying vec2 texcoord;\n"
  47. "void main()\n"
  48. "{\n"
  49. " gl_FragColor = vec4(color * texture2D(texture, texcoord).rgb, 1.0);\n"
  50. "}\n";
  51. static const vec2 vertices[4] =
  52. {
  53. { 0.f, 0.f },
  54. { 1.f, 0.f },
  55. { 1.f, 1.f },
  56. { 0.f, 1.f }
  57. };
  58. static void error_callback(int error, const char* description)
  59. {
  60. fprintf(stderr, "Error: %s\n", description);
  61. }
  62. static void key_callback(GLFWwindow* window, int key, int scancode, int action, int mods)
  63. {
  64. if (action == GLFW_PRESS && key == GLFW_KEY_ESCAPE)
  65. glfwSetWindowShouldClose(window, GLFW_TRUE);
  66. }
  67. int main(int argc, char** argv)
  68. {
  69. GLFWwindow* windows[2];
  70. GLuint texture, program, vertex_buffer;
  71. GLint mvp_location, vpos_location, color_location, texture_location;
  72. glfwSetErrorCallback(error_callback);
  73. if (!glfwInit())
  74. exit(EXIT_FAILURE);
  75. glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 2);
  76. glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 0);
  77. windows[0] = glfwCreateWindow(400, 400, "First", NULL, NULL);
  78. if (!windows[0])
  79. {
  80. glfwTerminate();
  81. exit(EXIT_FAILURE);
  82. }
  83. glfwSetKeyCallback(windows[0], key_callback);
  84. glfwMakeContextCurrent(windows[0]);
  85. // Only enable vsync for the first of the windows to be swapped to
  86. // avoid waiting out the interval for each window
  87. glfwSwapInterval(1);
  88. // The contexts are created with the same APIs so the function
  89. // pointers should be re-usable between them
  90. gladLoadGL(glfwGetProcAddress);
  91. // Create the OpenGL objects inside the first context, created above
  92. // All objects will be shared with the second context, created below
  93. {
  94. int x, y;
  95. char pixels[16 * 16];
  96. GLuint vertex_shader, fragment_shader;
  97. glGenTextures(1, &texture);
  98. glBindTexture(GL_TEXTURE_2D, texture);
  99. srand((unsigned int) glfwGetTimerValue());
  100. for (y = 0; y < 16; y++)
  101. {
  102. for (x = 0; x < 16; x++)
  103. pixels[y * 16 + x] = rand() % 256;
  104. }
  105. glTexImage2D(GL_TEXTURE_2D, 0, GL_LUMINANCE, 16, 16, 0, GL_LUMINANCE, GL_UNSIGNED_BYTE, pixels);
  106. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
  107. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
  108. vertex_shader = glCreateShader(GL_VERTEX_SHADER);
  109. glShaderSource(vertex_shader, 1, &vertex_shader_text, NULL);
  110. glCompileShader(vertex_shader);
  111. fragment_shader = glCreateShader(GL_FRAGMENT_SHADER);
  112. glShaderSource(fragment_shader, 1, &fragment_shader_text, NULL);
  113. glCompileShader(fragment_shader);
  114. program = glCreateProgram();
  115. glAttachShader(program, vertex_shader);
  116. glAttachShader(program, fragment_shader);
  117. glLinkProgram(program);
  118. mvp_location = glGetUniformLocation(program, "MVP");
  119. color_location = glGetUniformLocation(program, "color");
  120. texture_location = glGetUniformLocation(program, "texture");
  121. vpos_location = glGetAttribLocation(program, "vPos");
  122. glGenBuffers(1, &vertex_buffer);
  123. glBindBuffer(GL_ARRAY_BUFFER, vertex_buffer);
  124. glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
  125. }
  126. glUseProgram(program);
  127. glUniform1i(texture_location, 0);
  128. glEnable(GL_TEXTURE_2D);
  129. glBindTexture(GL_TEXTURE_2D, texture);
  130. glBindBuffer(GL_ARRAY_BUFFER, vertex_buffer);
  131. glEnableVertexAttribArray(vpos_location);
  132. glVertexAttribPointer(vpos_location, 2, GL_FLOAT, GL_FALSE,
  133. sizeof(vertices[0]), (void*) 0);
  134. windows[1] = glfwCreateWindow(400, 400, "Second", NULL, windows[0]);
  135. if (!windows[1])
  136. {
  137. glfwTerminate();
  138. exit(EXIT_FAILURE);
  139. }
  140. // Place the second window to the right of the first
  141. {
  142. int xpos, ypos, left, right, width;
  143. glfwGetWindowSize(windows[0], &width, NULL);
  144. glfwGetWindowFrameSize(windows[0], &left, NULL, &right, NULL);
  145. glfwGetWindowPos(windows[0], &xpos, &ypos);
  146. glfwSetWindowPos(windows[1], xpos + width + left + right, ypos);
  147. }
  148. glfwSetKeyCallback(windows[1], key_callback);
  149. glfwMakeContextCurrent(windows[1]);
  150. // While objects are shared, the global context state is not and will
  151. // need to be set up for each context
  152. glUseProgram(program);
  153. glEnable(GL_TEXTURE_2D);
  154. glBindTexture(GL_TEXTURE_2D, texture);
  155. glBindBuffer(GL_ARRAY_BUFFER, vertex_buffer);
  156. glEnableVertexAttribArray(vpos_location);
  157. glVertexAttribPointer(vpos_location, 2, GL_FLOAT, GL_FALSE,
  158. sizeof(vertices[0]), (void*) 0);
  159. while (!glfwWindowShouldClose(windows[0]) &&
  160. !glfwWindowShouldClose(windows[1]))
  161. {
  162. int i;
  163. const vec3 colors[2] =
  164. {
  165. { 0.8f, 0.4f, 1.f },
  166. { 0.3f, 0.4f, 1.f }
  167. };
  168. for (i = 0; i < 2; i++)
  169. {
  170. int width, height;
  171. mat4x4 mvp;
  172. glfwGetFramebufferSize(windows[i], &width, &height);
  173. glfwMakeContextCurrent(windows[i]);
  174. glViewport(0, 0, width, height);
  175. mat4x4_ortho(mvp, 0.f, 1.f, 0.f, 1.f, 0.f, 1.f);
  176. glUniformMatrix4fv(mvp_location, 1, GL_FALSE, (const GLfloat*) mvp);
  177. glUniform3fv(color_location, 1, colors[i]);
  178. glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
  179. glfwSwapBuffers(windows[i]);
  180. }
  181. glfwWaitEvents();
  182. }
  183. glfwTerminate();
  184. exit(EXIT_SUCCESS);
  185. }