Window goes black after render (solved)

This is strange behavior. I’ve used openGL on several platforms, but I’ve never seen this.

I am setting up a window, the setting up openGL, then calling:
glClearColor( red
glClear(GL_COLOR_BUFFER_BIT)

What happens is the window will clear with red (flicker-ee like) and then quickly get rendered over with black. No matter what I do, I can’t get rid of this behavior. I am thinking that it might be my system (how the drivers are set or something).

Here is the code and makefile that I’m using. Could someone copy to a file and build to see if they get the same behavior? Or if you see something obvious in my code…


#include <stdio.h>
#include <stdlib.h>
#include <iostream>
#include <string.h>
#include <X11/Xlib.h>
#include <GL/glxew.h>
#include <GL/glx.h>
#include <GL/gl.h>

using namespace std;

#define LOG_FUNCTION printf("executing %s
", __FUNCTION__);
#define WIDTH 400
#define HEIGHT 300
Window win;
Display* dpy;
int major;
int minor;
GLXContext context;
XVisualInfo* visualInfo;



void createContext(){
	LOG_FUNCTION
	context = glXCreateContext(dpy, visualInfo, 0, true);
	if(NULL == context){
		//failed to create context
		printf("ERROR:%d failed to create context
", __LINE__);
		printf("debug: dpy=%d, visualInfo=%d
", &dpy, &visualInfo);
		return;
	}
	if(false == glXMakeCurrent(dpy, win, context)){
		printf("ERROR:%d failed to set context to current
", __LINE__);
		printf("debug: dpy=%d, win=%d, context=%d
", &dpy, &visualInfo, context);
	}
}

void initGL(){
	LOG_FUNCTION
	glViewport(0, 0, WIDTH, HEIGHT);
	glScissor(0, 0, WIDTH, HEIGHT);
	glClearColor(1.0f, 0.0f, 0.0f, 1.0f);
	glClear(GL_COLOR_BUFFER_BIT);
	glMatrixMode(GL_MODELVIEW);
	glLoadIdentity();
	glMatrixMode(GL_PROJECTION);
	glLoadIdentity();
	glFrustum(-1.0f, 1.0f, -1.0f, 1.0f, -1.0f, 1.0f);
	glXSwapBuffers(dpy, win);
}

void drawShape(int count){
	LOG_FUNCTION
	if(false == glXMakeCurrent(dpy, win, context)){
		printf("ERROR:%d failed to set context to current
", __LINE__);
		printf("debug: dpy=%d, win=%d, context=%d
", &dpy, &visualInfo, context);
	}
	//static float x = 0;	
	//glClearColor(1.0f, x, 0.0f, 1.0f);

	/*
	static bool maxHit = false;
	static bool minHit = true;
	
	if(minHit && x >= 1.0){
		minHit = false;
		maxHit = true;
	}
	else if(maxHit && x <= 0.0){
		minHit = true;
		maxHit = false;
	}

	if(minHit)
		x += 0.001;
	else if (maxHit)
		x -= 0.001;
	
		//x-=0.001;
		//x+=0.001;
	*/
	/*
	glBegin(GL_LINES);
	{
		glVertex3f(-0.5f, 0.5f, 0.0f);
		glVertex3f(0.5f, 0.5f, 0.0f);
		glVertex3f(0.5f, -0.5f, 0.0f);
		glVertex3f(-0.5f, -0.5f, 0.0f);
	}
	glEnd();
	*/
	//glScalef(1.1, 1.05, 1.0);
	switch(count)
	{
		case 0:
			glClearColor(0.0, 0.0, 0.0, 1.0);
			break;
		case 1:
			glClearColor(0.0, 0.0, 1.0, 1.0);
			break;
		case 2:
			glClearColor(0.0, 1.0, 0.0, 1.0);
			break;
		case 3:
			glClearColor(0.0, 1.0, 1.0, 1.0);
			break;
		case 4:
			glClearColor(1.0, 0.0, 0.0, 1.0);
			break;
		case 5:
			glClearColor(1.0, 0.0, 1.0, 1.0);
			break;
		case 6:
			glClearColor(1.0, 1.0, 0.0, 1.0);
			break;
		case 7:
			glClearColor(1.0, 1.0, 1.0, 1.0);
			break;
	}

	
	glClear(GL_COLOR_BUFFER_BIT);
	glXSwapBuffers(dpy, win);
	XFlush(dpy);
	glFlush();
	glFinish();
	sleep(1);
}

int main(int, char**){
	LOG_FUNCTION
	dpy = XOpenDisplay(getenv("DISPLAY"));
	if(!dpy){
		printf("failed to open display
");
	}	
	else{
		printf("open display SUCCESS
");
	}

	glXQueryVersion(dpy, &major, &minor);
	printf("glx v%d.%d
", major, minor);
	if(major == 1 && minor < 2){
		printf("incompatible version
");
		XCloseDisplay(dpy);
		exit(0);
	}

	static int attributes[] = {	GLX_RGBA,
								GLX_DOUBLEBUFFER,
								GLX_RED_SIZE, 8,
								GLX_BLUE_SIZE, 8,
								GLX_GREEN_SIZE, 8,
								GL_NONE};
	/*
	static int attributes[] = { GLX_RENDER_TYPE, GLX_RGBA_BIT,
								GLX_DRAWABLE_TYPE, GLX_WINDOW_BIT,
								GLX_DOUBLEBUFFER,
								GLX_DEPTH_SIZE, 16,
								GLX_RED_SIZE, 8,
								GLX_BLUE_SIZE, 8,
								GLX_GREEN_SIZE, 8,
								GL_NONE};
	*/
	/*
	static int attributes[] =
		{
				GLX_X_RENDERABLE    , True,
				GLX_DRAWABLE_TYPE   , GLX_WINDOW_BIT,
				GLX_RENDER_TYPE     , GLX_RGBA_BIT,
				//GLX_X_VISUAL_TYPE   , GLX_TRUE_COLOR,
				GLX_RED_SIZE        , 8,
				GLX_GREEN_SIZE      , 8,
				GLX_BLUE_SIZE       , 8,
				//GLX_ALPHA_SIZE      , 8,
				GLX_DEPTH_SIZE      , 16,
				GLX_DOUBLEBUFFER    , True,
				//GLX_SAMPLE_BUFFERS  , 1,
				//GLX_SAMPLES         , 4,
				None
		};
	*/

	visualInfo = glXChooseVisual(dpy, DefaultScreen(dpy), attributes);

	//XSetWindowAttributes winAttributes;
	//winAttributes.event_mask = ExposureMask | VisibilityChangeMask | KeyPressMask | PointerMotionMask | StructureNotifyMask;
	//winAttributes.border_pixel = 0;
	//winAttributes.colormap = XCreateColormap(dpy, RootWindow(dpy, visualInfo->screen), visualInfo->visual, AllocNone);
	//unsigned long winmask = CWBorderPixel | CWBitGravity | CWEventMask | CWColormap;
	//win = XCreateWindow(dpy, DefaultRootWindow(dpy), 20, 20, 400, 300, 0, visualInfo->depth, InputOutput, visualInfo->visual, winmask, &winAttributes);

	win = XCreateSimpleWindow(dpy, DefaultRootWindow(dpy), 20, 20, WIDTH, HEIGHT, 0, 0, 0);
	XSelectInput(dpy, win, StructureNotifyMask);
	XMapWindow(dpy, win);
	
	for(;;){
		XEvent e;
		XNextEvent(dpy, &e);
		if(e.type == MapNotify) break;
	}

	XFlush(dpy);	
	

	createContext();
	initGL();

	//int x = 0;
	//cin >> x;

	printf("calling drawShape()
");
	
	drawShape(1);
	sleep(1);
	drawShape(2);
	sleep(1);
	drawShape(3);
	sleep(1);
	drawShape(4);
	sleep(1);


	glXMakeCurrent(dpy, 0, 0);
	glXDestroyContext(dpy, context);
	//XCloseDisplay(dpy);


}


be sure to change the cpp filename in the makefile :slight_smile:


LIBDIRS = -L/usr/X11R6/lib -L/usr/local/lib -L/usr/lib -L/usr/include 
INCDIRS = -I/usr/include -I/usr/local/include -I/usr/X11R6/include -I/usr/include/X11 -I/usr/include/GL
COMPILERFLAGS = -ggdb -Wall
CC = g++
CFLAGS = $(COMPILERFLAGS) -g $(INCDIRS)
LIBS = -lX11 -lGL 

all: 
	$(CC) $(CFLAGS) -o testBin $(LIBDIRS) testBin.cpp $(LIBS)
	
clean:
	rm -f testBin


1000 appreciations!

Solved. It was my system drivers :slight_smile: Stoked about that

This topic was automatically closed 183 days after the last reply. New replies are no longer allowed.