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