The following program is assumed:
- Create invisible
GLFWwindow* masterWindow - Load the texture from the file, while the active OpenGL context is the one associated with
masterWindow - create
GLFWwindow* childWindow, while sharing is activated between this OpenGL context window and the contextmasterWindow - display a fullscreen square with texture in
childWindow
However, this does not work, i.e. instead of texture, I get random fragments of graphic memory displayed in childWindow. Texture mapping works if I make the context childWindowcurrent before loading, so I think the problem is not that my problem is not related to my shaders or my texture loading program (which I took here, https://github.com/ DavidEGrayson / ahrs-visualizer / blob / master / png_texture.cpp ). I understand that I need to share the texture (see the answer to this question SO: OpenGL - share existing textures with future contexts? ), So what am I doing wrong? In case that matters, I use a late 2008 Macbook with NVIDIA 9400m and Mavericks / OpenGL 3.3 installed.
#include <iostream>
#define GLEW_STATIC
#include <glew.h>
#include <glfw3.h>
const char* vShader =
"#version 150 core\n"
"in vec2 vertex;"
"in vec2 vertexUV;"
"out vec2 UV;"
"void main() {gl_Position = vec4(vertex,0,1);UV=vertexUV;}";
const char* fShader =
"#version 150 core\n"
"uniform sampler2D sampler;"
"in vec2 UV;"
"out vec4 color;"
"void main() {color = texture(sampler, UV);}";
GLuint png_texture_load(const char * file_name, unsigned int * width=NULL, unsigned int * height=NULL);
GLuint make_program(const char* vShader, const char* fShader);
GLFWwindow* masterWindow(NULL);
void create_window_with_texture(GLuint texture)
{
GLFWwindow* childWindow(glfwCreateWindow(256,256, "", NULL, masterWindow));
glfwMakeContextCurrent(childWindow);
GLfloat vertices[]={-1,-1,1,-1,-1,1,1,1,
0,0,1,0,0,1,1,1};
GLubyte indices[]={0,2,1,3};
GLuint vArray,vBuffer,iBuffer;
glGenVertexArrays(1,&vArray);glBindVertexArray(vArray);
glGenBuffers(1,&vBuffer);glBindBuffer(GL_ARRAY_BUFFER,vBuffer);
glGenBuffers(1,&iBuffer);glBindBuffer(GL_ELEMENT_ARRAY_BUFFER,iBuffer);
glBufferData(GL_ARRAY_BUFFER,sizeof(vertices),vertices,GL_DYNAMIC_DRAW);
glBufferData(GL_ELEMENT_ARRAY_BUFFER,sizeof(indices),indices,GL_DYNAMIC_DRAW);
GLuint program(make_program(vShader,fShader));
glUseProgram(program);
glUniform1i(glGetUniformLocation(program,"sampler"),0);
GLuint vertexPtr(GLuint(glGetAttribLocation(program,"vertex")));
glEnableVertexAttribArray(vertexPtr);
GLuint vertexUVPtr(GLuint(glGetAttribLocation(program, "vertexUV")));
glEnableVertexAttribArray(vertexUVPtr);
glVertexAttribPointer(vertexPtr, 2, GL_FLOAT, GL_FALSE, 0, (const GLvoid*)0 );
glVertexAttribPointer(vertexUVPtr, 2, GL_FLOAT, GL_FALSE, 0, (const GLvoid*)32);
glActiveTexture(GL_TEXTURE0);
glDisable(GL_DEPTH_TEST);
glDisable(GL_BLEND);
glBindTexture(GL_TEXTURE_2D,texture);
glDrawElements(GL_TRIANGLE_STRIP,4, GL_UNSIGNED_BYTE, (GLvoid*)0);
glfwSwapBuffers(childWindow);
}
int main()
{
if (!glfwInit())
return -1;
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR,3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR,2);
glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT,GL_TRUE);
glfwWindowHint(GLFW_OPENGL_PROFILE,GLFW_OPENGL_CORE_PROFILE);
glfwWindowHint(GLFW_VISIBLE,GL_FALSE);
masterWindow=glfwCreateWindow(10,10, "", NULL,NULL);
glfwWindowHint(GLFW_VISIBLE,GL_TRUE);
glfwWindowHint(GLFW_SAMPLES, 4);
glfwMakeContextCurrent(masterWindow);
glewExperimental = GL_TRUE;
GLenum err = glewInit();
if(GLEW_OK!=err)
return -1;
glGetError();
GLuint texture(0);
texture=png_texture_load("rose.png");
create_window_with_texture(texture);
std::cin.get();
return 0;
}