hello,
i cannot seem to make texture sharing work in linux (although i got it working on win32 using wglShareLists).
i am creating 2 GLXContext’s and sharing their display lists using glXCreateContext.
then i create a texture in the 1st context and render a triangle in each context using the texture.
the texture shows up in the 1st context however in the 2nd context i get a white triangle.
i get NO errors using glGetError.
can anyone say tell me where is the problem??
i would really appreciate any help or tips!
here is the problem code:
(also available at http://www.dvoyka.com/wwwfivemagics2/share.c))
//build with: gcc share.c -o share -L/usr/X11R6/lib -lX11 -lGL -lGLU
#include <X11/Xlib.h>
#include <X11/X.h>
#include <GL/gl.h>
#include <GL/glx.h>
#define texwidth 16
#define texheight 16
Window SetupWindow(Display* dpy, char* name, int width, int height, GC* Context,
Bool Direct, GLXContext* glContext, GLXContext share)
{
Window ret = 0;
int blackColor = BlackPixel(dpy, DefaultScreen(dpy));
ret = XCreateSimpleWindow(dpy, DefaultRootWindow(dpy), 0, 0, width, height, 0, blackColor, blackColor);
XStoreName(dpy, ret, name);
XSelectInput(dpy, ret, StructureNotifyMask);
XMapWindow(dpy, ret);
*Context = XCreateGC(dpy, ret, 0, NULL);
XVisualInfo* vi = NULL;
int attr[] = {GLX_RGBA, GLX_RED_SIZE, 4, GLX_GREEN_SIZE, 4, GLX_BLUE_SIZE,4, None};
vi = glXChooseVisual(dpy, 0, attr);
glXMakeCurrent(dpy, None, NULL);
printf("share=%d
", share);
*glContext = glXCreateContext(dpy, vi, share, Direct);
glXMakeCurrent(dpy, ret, *glContext);
glDisable(GL_CULL_FACE);
glEnable(GL_TEXTURE_2D);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0.0f, width, 0.0f, height, 0.0f, 1.0f);
return (ret);
}
//pattern[6] contains the rgb colors for 2 pixels
GLuint CreateTexture(Display* dpy, GLXContext ctx, Window w, GLfloat pattern[6])
{
GLuint ret = 0;
GLfloat texfill[texwidth * texheight * 3];
//fill temp array with pattern
int i = 0;
for (i = 0 ; i < texwidth * texheight * 3; i += 6) {
memcpy(&(texfill[i]), pattern, 6 * sizeof(GLfloat));
}
//generate texture and fill it with pattern
glXMakeCurrent(dpy, w, ctx);
glGenTextures(1, &ret);
glBindTexture(GL_TEXTURE_2D, ret);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB16, texwidth, texheight, 0, GL_RGB, GL_FLOAT, texfill);
GLenum error = glGetError();
if (error != GL_NO_ERROR) {
printf("Error while creating texture = %s …,
", gluErrorString(error));
}
return ret;
}
void render(Display* dpy, GLXContext ctx, Window w)
{
//render a triangle using texture
glXMakeCurrent(dpy, w, ctx);
glClear(GL_COLOR_BUFFER_BIT);
glBegin(GL_TRIANGLES);
glTexCoord2f(0.0f, 0.0f); glVertex2f(0.0f, 0.0f);
glTexCoord2f(0.0f, 1.0f); glVertex2f(300.0f, 300.0f);
glTexCoord2f(1.0f, 1.0f); glVertex2f(300.0f, 0.0f);
glEnd();
glXSwapBuffers(dpy, w);
}
int main()
{
Display* dpy = XOpenDisplay(0);
GC Context;
Window w1, w2;
GLXContext ctx1, ctx2;
Bool direct = False;
w1 = SetupWindow(dpy, “test1”, 320, 320, &Context, direct, &ctx1, NULL);
//ctx2 shares ctx1
w2 = SetupWindow(dpy, “test2”, 320, 320, &Context, direct, &ctx2, ctx1);
GLuint tex1, tex2;
GLuint list1, list2;
GLfloat pattern[6];
glXMakeCurrent(dpy, w1, ctx1);
glClearColor(1.0, 1.0, 0.0, 0.0);
//red green pattern
pattern[0] = 1.0; pattern[1] = 0.0; pattern[2] = 0.0;
pattern[3] = 0.0; pattern[4] = 1.0; pattern[5] = 0.0;
tex1 = CreateTexture(dpy, ctx1, w1, pattern);
list1= glGenLists(1);
glXMakeCurrent(dpy, w2, ctx2);
glClearColor(1.0, 0.0, 1.0, 0.0);
list2 = glGenLists(1);
tex2 = tex1;
printf("ctx1=%d ctx2=%d tex1=%d tex2=%d list1=%d list2=%d
", ctx1, ctx2, tex1, tex2, list1, list2);
/*
if (tex1 == tex2) {
printf("TEXTURES ARE NOOOOT SHARED!!! tex1=%u, tex2=%u…
", tex1, tex2);
} else {
printf("TEXTURES ARE SHARED OK!!! tex1=%u, tex2=%u…
", tex1, tex2);
}
*/
//infinite loop ok for testing
XEvent evt;
int active = 1;
long counter = 0;
while(active){
counter++;
render(dpy, ctx1, w1);
render(dpy, ctx2, w2);
//print glError if any
GLenum error = glGetError();
if (error != GL_NO_ERROR) {
printf("Error = %s ..,
", gluErrorString(error));
}
}
XCloseDisplay(dpy);
return(0);
}
[This message has been edited by jadnohra (edited 06-24-2003).]