sharing.c 7.0 KB

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