sharing.c 7.0 KB

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