1
0
Fork 0
mirror of https://github.com/gwm17/glfw.git synced 2024-11-23 18:58:52 -05:00
glfw/examples/sharing.c

234 lines
6.9 KiB
C
Raw Normal View History

//========================================================================
2017-11-05 18:32:23 -05:00
// Context sharing example
2016-11-21 10:23:59 -05:00
// Copyright (c) Camilla Löwy <elmindreda@glfw.org>
//
// This software is provided 'as-is', without any express or implied
// warranty. In no event will the authors be held liable for any damages
// arising from the use of this software.
//
// Permission is granted to anyone to use this software for any purpose,
// including commercial applications, and to alter it and redistribute it
// freely, subject to the following restrictions:
//
// 1. The origin of this software must not be misrepresented; you must not
// claim that you wrote the original software. If you use this software
// in a product, an acknowledgment in the product documentation would
// be appreciated but is not required.
//
// 2. Altered source versions must be plainly marked as such, and must not
// be misrepresented as being the original software.
//
// 3. This notice may not be removed or altered from any source
// distribution.
//
//========================================================================
#include <glad/gl.h>
#include <GLFW/glfw3.h>
#include <stdio.h>
#include <stdlib.h>
#include "getopt.h"
2016-09-20 08:12:15 -04:00
#include "linmath.h"
static const char* vertex_shader_text =
"#version 110\n"
2016-09-20 08:12:15 -04:00
"uniform mat4 MVP;\n"
"attribute vec2 vPos;\n"
"varying vec2 texcoord;\n"
"void main()\n"
"{\n"
" gl_Position = MVP * vec4(vPos, 0.0, 1.0);\n"
" texcoord = vPos;\n"
"}\n";
static const char* fragment_shader_text =
"#version 110\n"
2016-09-20 08:12:15 -04:00
"uniform sampler2D texture;\n"
"uniform vec3 color;\n"
"varying vec2 texcoord;\n"
"void main()\n"
"{\n"
" gl_FragColor = vec4(color * texture2D(texture, texcoord).rgb, 1.0);\n"
2016-09-20 08:12:15 -04:00
"}\n";
static const vec2 vertices[4] =
{
{ 0.f, 0.f },
{ 1.f, 0.f },
{ 1.f, 1.f },
{ 0.f, 1.f }
};
2012-08-03 10:20:52 -04:00
static void error_callback(int error, const char* description)
{
fprintf(stderr, "Error: %s\n", description);
}
2013-05-30 11:19:12 -04:00
static void key_callback(GLFWwindow* window, int key, int scancode, int action, int mods)
2010-10-04 18:12:25 -04:00
{
if (action == GLFW_PRESS && key == GLFW_KEY_ESCAPE)
2015-08-23 13:30:04 -04:00
glfwSetWindowShouldClose(window, GLFW_TRUE);
2012-08-03 10:20:52 -04:00
}
2016-09-20 08:12:15 -04:00
int main(int argc, char** argv)
{
2016-09-20 08:12:15 -04:00
GLFWwindow* windows[2];
2016-09-21 09:40:27 -04:00
GLuint texture, program, vertex_buffer;
2016-09-20 08:12:15 -04:00
GLint mvp_location, vpos_location, color_location, texture_location;
2016-09-20 08:12:15 -04:00
glfwSetErrorCallback(error_callback);
2016-09-20 08:12:15 -04:00
if (!glfwInit())
exit(EXIT_FAILURE);
2016-09-20 08:12:15 -04:00
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 2);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 0);
2016-09-20 08:12:15 -04:00
windows[0] = glfwCreateWindow(400, 400, "First", NULL, NULL);
if (!windows[0])
{
2016-09-20 08:12:15 -04:00
glfwTerminate();
exit(EXIT_FAILURE);
}
2016-09-20 08:12:15 -04:00
glfwSetKeyCallback(windows[0], key_callback);
2016-09-20 08:12:15 -04:00
glfwMakeContextCurrent(windows[0]);
2016-09-20 08:12:15 -04:00
// Only enable vsync for the first of the windows to be swapped to
// avoid waiting out the interval for each window
glfwSwapInterval(1);
2010-10-04 17:13:33 -04:00
2016-09-20 08:12:15 -04:00
// The contexts are created with the same APIs so the function
// pointers should be re-usable between them
gladLoadGL(glfwGetProcAddress);
2016-09-20 08:12:15 -04:00
// Create the OpenGL objects inside the first context, created above
// All objects will be shared with the second context, created below
{
int x, y;
2016-09-20 20:43:44 -04:00
char pixels[16 * 16];
2016-09-20 08:12:15 -04:00
GLuint vertex_shader, fragment_shader;
glGenTextures(1, &texture);
glBindTexture(GL_TEXTURE_2D, texture);
2017-11-05 18:32:23 -05:00
srand((unsigned int) glfwGetTimerValue());
2016-09-20 20:43:44 -04:00
for (y = 0; y < 16; y++)
2016-09-20 08:12:15 -04:00
{
2016-09-20 20:43:44 -04:00
for (x = 0; x < 16; x++)
pixels[y * 16 + x] = rand() % 256;
2016-09-20 08:12:15 -04:00
}
2016-09-20 20:43:44 -04:00
glTexImage2D(GL_TEXTURE_2D, 0, GL_LUMINANCE, 16, 16, 0, GL_LUMINANCE, GL_UNSIGNED_BYTE, pixels);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
2016-09-20 08:12:15 -04:00
vertex_shader = glCreateShader(GL_VERTEX_SHADER);
glShaderSource(vertex_shader, 1, &vertex_shader_text, NULL);
glCompileShader(vertex_shader);
fragment_shader = glCreateShader(GL_FRAGMENT_SHADER);
glShaderSource(fragment_shader, 1, &fragment_shader_text, NULL);
glCompileShader(fragment_shader);
program = glCreateProgram();
glAttachShader(program, vertex_shader);
glAttachShader(program, fragment_shader);
glLinkProgram(program);
mvp_location = glGetUniformLocation(program, "MVP");
color_location = glGetUniformLocation(program, "color");
texture_location = glGetUniformLocation(program, "texture");
vpos_location = glGetAttribLocation(program, "vPos");
glGenBuffers(1, &vertex_buffer);
glBindBuffer(GL_ARRAY_BUFFER, vertex_buffer);
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
}
2010-10-04 17:13:33 -04:00
2016-09-20 08:12:15 -04:00
glUseProgram(program);
glUniform1i(texture_location, 0);
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, texture);
2016-09-20 08:12:15 -04:00
glBindBuffer(GL_ARRAY_BUFFER, vertex_buffer);
glEnableVertexAttribArray(vpos_location);
glVertexAttribPointer(vpos_location, 2, GL_FLOAT, GL_FALSE,
sizeof(vertices[0]), (void*) 0);
2016-09-20 08:12:15 -04:00
windows[1] = glfwCreateWindow(400, 400, "Second", NULL, windows[0]);
if (!windows[1])
{
glfwTerminate();
exit(EXIT_FAILURE);
}
2016-09-20 08:12:15 -04:00
// Place the second window to the right of the first
{
int xpos, ypos, left, right, width;
2016-09-20 08:12:15 -04:00
glfwGetWindowSize(windows[0], &width, NULL);
glfwGetWindowFrameSize(windows[0], &left, NULL, &right, NULL);
glfwGetWindowPos(windows[0], &xpos, &ypos);
2016-09-20 08:12:15 -04:00
glfwSetWindowPos(windows[1], xpos + width + left + right, ypos);
}
2016-09-20 08:12:15 -04:00
glfwSetKeyCallback(windows[1], key_callback);
glfwMakeContextCurrent(windows[1]);
2016-09-20 08:12:15 -04:00
// While objects are shared, the global context state is not and will
// need to be set up for each context
glUseProgram(program);
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, texture);
glBindBuffer(GL_ARRAY_BUFFER, vertex_buffer);
glEnableVertexAttribArray(vpos_location);
glVertexAttribPointer(vpos_location, 2, GL_FLOAT, GL_FALSE,
sizeof(vertices[0]), (void*) 0);
while (!glfwWindowShouldClose(windows[0]) &&
!glfwWindowShouldClose(windows[1]))
{
2016-09-20 08:12:15 -04:00
int i;
const vec3 colors[2] =
{
2017-11-05 18:32:23 -05:00
{ 0.8f, 0.4f, 1.f },
{ 0.3f, 0.4f, 1.f }
2016-09-20 08:12:15 -04:00
};
for (i = 0; i < 2; i++)
{
int width, height;
mat4x4 mvp;
glfwGetFramebufferSize(windows[i], &width, &height);
glfwMakeContextCurrent(windows[i]);
glViewport(0, 0, width, height);
mat4x4_ortho(mvp, 0.f, 1.f, 0.f, 1.f, 0.f, 1.f);
glUniformMatrix4fv(mvp_location, 1, GL_FALSE, (const GLfloat*) mvp);
glUniform3fv(color_location, 1, colors[i]);
glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
glfwSwapBuffers(windows[i]);
}
glfwWaitEvents();
}
glfwTerminate();
exit(EXIT_SUCCESS);
}