[SOLVED] Very slow Window execution

Started by darkman19320, August 11, 2010, 14:06:35

Previous topic - Next topic

darkman19320

Good afternoon everyone!
I have a problem to launch a simple window...
I'm on Ubuntu 10.04 (32bits), Eclipse 3.5.2, swt 3.5.2, java-sun 6 and LWJGL 2.5.

This is my code to create my window:
//A loaderAbstract is loader of my dataSet. But in this example, I use the value on the dataset bounding box.
  public Window2(LoaderAbstract loader, Composite parent, int heightWindow,
	    int widthWindow) {
	setScreenRatio(heightWindow, widthWindow);

	parent.setLayout(new FillLayout());

	initGLData();
	initGLCanvas(parent);

	setLoader(loader);
	initGL();
    }

    private void setScreenRatio(int heightWindow, int widthWindow) {
	screenRatio = (double) widthWindow / (double)heightWindow;
	this.heightWindow = heightWindow;
	this.widthWindow = widthWindow;
    }

    private void initGLData() {
	data = new GLData();
	data.doubleBuffer = true;
	data.depthSize = 1;
    }

    private void initGLCanvas(Composite parent) {
	canvas = new GLCanvas(parent, parent.getStyle(), data);
	canvas.setCurrent();
	try {
	    GLContext.useContext(canvas);
	} catch (LWJGLException e) {
	    e.printStackTrace();
	}

	canvas.addKeyListener(listener);
	canvas.addMouseListener(listener);
	canvas.addMouseMoveListener(listener);
	canvas.addMouseWheelListener(listener);
	canvas.addMouseTrackListener(listener);

	canvas.addListener(SWT.Resize, new Listener() {
	    public void handleEvent(Event event) {
		Rectangle bounds = canvas.getBounds();
		resizeWindow(bounds.height, bounds.width);
	    }
	});
    }

    public GLCanvas getGLCanvas() {
	return canvas;
    }

    public void setLoader(LoaderAbstract loader) {
	this.loader = loader;
	try {
	    dataSetGravity = loader.getGravityDataSet();
	    dataSetRatio = loader.getDataSetRatio();
	    listener.setDataSetRatio(dataSetRatio);

	    fillVBO();
	    fillBoundingBoxDisplayList();
	} catch(NullPointerException e) {
	    e.printStackTrace();
	}
    }

    private void initGL() {
	GL11.glClear(GL11.GL_COLOR_BUFFER_BIT | GL11.GL_DEPTH_BUFFER_BIT);
	GL11.glShadeModel(GL11.GL_SMOOTH);
	GL11.glClearColor(backGroundColor[0], backGroundColor[1], //background = {1,1,1,1}
		backGroundColor[2], backGroundColor[3]);
	GL11.glClearDepth(1.0f);
	GL11.glEnable(GL11.GL_DEPTH_TEST);
	GL11.glDepthFunc(GL11.GL_LEQUAL);
	GL11.glHint(GL11.GL_PERSPECTIVE_CORRECTION_HINT, GL11.GL_NICEST); 

	GL11.glEnable(GL11.GL_LIGHT0); // Enable Light One
	ByteBuffer temp = ByteBuffer.allocateDirect(16);
	temp.order(ByteOrder.nativeOrder());
	GL11.glLight(GL11.GL_LIGHT0, GL11.GL_AMBIENT, // Setup The Ambient Light
		(FloatBuffer) temp.asFloatBuffer().put(LightAmbient).flip());
	GL11.glLight(GL11.GL_LIGHT0, GL11.GL_DIFFUSE, // Setup The Diffuse Light
		(FloatBuffer) temp.asFloatBuffer().put(LightDiffuse).flip());
	GL11.glLight(GL11.GL_LIGHT0, GL11.GL_POSITION, // Position The Light
		(FloatBuffer) temp.asFloatBuffer().put(LightPosition).flip());

	GL11.glEnable(GL11.GL_COLOR_MATERIAL); // Enable Material Coloring
	GL11.glEnable(GL11.GL_BLEND);
	GL11.glBlendFunc(GL11.GL_SRC_ALPHA, GL11.GL_ONE_MINUS_SRC_ALPHA);

	GL11.glViewport(0, 0, widthWindow, heightWindow);
	GL11.glPolygonMode(GL11.GL_FRONT_AND_BACK, GL11.GL_FILL);
	GL11.glMatrixMode(GL11.GL_PROJECTION);
	GL11.glLoadIdentity();

	setProjectionTransformations();
	projectionOrtho();

	GL11.glMatrixMode(GL11.GL_MODELVIEW);
	GL11.glLoadIdentity();
    }

    public void resizeWindow(int height, int width) {
	setScreenRatio(height, width);

	GL11.glViewport(0, 0, widthWindow, heightWindow);
	GL11.glMatrixMode(GL11.GL_PROJECTION);
	GL11.glLoadIdentity();

	setProjectionTransformations();
	projectionOrtho();

	GL11.glMatrixMode(GL11.GL_MODELVIEW);
	GL11.glLoadIdentity();
    }

    private void setProjectionTransformations() {
	    double gravityX;
	    double gravityY;
	    double gravityZ;

	    if (dataSetGravity != null) {
		gravityX = dataSetGravity.x();
		gravityY = dataSetGravity.y();
		gravityZ = dataSetGravity.z();
	    } else {
		gravityX = 0;
		gravityY = 0;
		gravityZ = 0;
	    }

	    // Zoom 3D
	    GL11.glTranslated(gravityX, gravityY, gravityZ);
	    GL11.glScaled(transZ, transZ, 1.0f);
	    GL11.glTranslated(-gravityX, -gravityY, -gravityZ);

	    // Translation
	    GL11.glTranslated(gravityX, gravityY, gravityZ);
	    GL11.glTranslatef((float) transX, (float) transY, 0.0f);
	    GL11.glTranslated(-gravityX, -gravityY, -gravityZ);

	    // Rotation
	    GL11.glTranslated(gravityX, gravityY, gravityZ);
	    GL11.glRotatef((float) rotX, 0.0f, 1.0f, 0.0f);
	    GL11.glRotatef((float) rotY, 1.0f, 0.0f, 0.0f);
	    GL11.glRotatef((float) rotZ, 0.0f, 0.0f, 1.0f);
	    GL11.glTranslated(-gravityX, -gravityY, -gravityZ);
    }

    private void projectionOrtho() {
	    double left, right, top, bottom, zNear, zFar;

	    if (screenRatio > dataSetRatio) {
		left = loader.getXmin() - ((
			(screenRatio * loader.getHeightDataSet()) - 
			loader.getWidthDataSet()) / 2.0) - 
			(loader.getDiagonal2D() / 20.0);
		right = loader.getXmax() + ((
			(screenRatio * loader.getHeightDataSet()) - 
			loader.getWidthDataSet()) / 2.0) + 
			(loader.getDiagonal2D() / 20.0);
		bottom = loader.getYmin() - (loader.getDiagonal2D() / 20.0);
		top = loader.getYmax() + (loader.getDiagonal2D() / 20.0);
		zNear = loader.getZmin() - loader.getDiagonal3D();
		zFar = loader.getZmax() + loader.getDiagonal3D();
	    } else {
		left = loader.getXmin() - (loader.getDiagonal2D() / 20.0);
		right = loader.getXmax() + (loader.getDiagonal2D() / 20.0);
		bottom = loader.getYmin() - ((
			(loader.getWidthDataSet() / screenRatio) - 
			loader.getHeightDataSet()) / 2.0) - 
			(loader.getDiagonal2D() / 20.0);
		top = loader.getYmax() + ((
			(loader.getWidthDataSet() / screenRatio) - 
			loader.getHeightDataSet()) / 2.0) + 
			(loader.getDiagonal2D() / 20.0);
		zNear = loader.getZmin() - loader.getDiagonal3D();
		zFar = loader.getZmax() + loader.getDiagonal3D();
	    }

	    if (zNear < 0 && zFar < 0) {
		zNear *= -1;
		zFar *= -1;
	    }

	    if (zNear >= zFar) {
		double tmp = zNear;
		zNear = zFar;
		zFar = tmp;
	    }

	    GL11.glOrtho(left, right, bottom, top, zNear, zFar);
    }

    private void fillBoundingBoxDisplayList() {
	boundingBoxDisplayList = GL11.glGenLists(1);

	GL11.glNewList(boundingBoxDisplayList, GL11.GL_COMPILE);

	GL11.glDisable(GL11.GL_LIGHTING);
	GL11.glLineWidth(2f);
	GL11.glColor4d(0, 0, 0, 1.0);

	//Face 1
	GL11.glLoadName(0);
	GL11.glBegin(GL11.GL_QUADS);
	GL11.glVertex3f(loader.getXmin(), loader.getYmin(), loader.getZmin());
	GL11.glVertex3f(loader.getXmax(), loader.getYmin(), loader.getZmin());
	GL11.glVertex3f(loader.getXmax(), loader.getYmax(), loader.getZmin());
	GL11.glVertex3f(loader.getXmin(), loader.getYmax(), loader.getZmin());
	GL11.glEnd();

	//Face 2
	GL11.glLoadName(1);
	GL11.glBegin(GL11.GL_QUADS);
	GL11.glVertex3f(loader.getXmin(), loader.getYmin(), loader.getZmin());
	GL11.glVertex3f(loader.getXmin(), loader.getYmin(), loader.getZmax());
	GL11.glVertex3f(loader.getXmin(), loader.getYmax(), loader.getZmax());
	GL11.glVertex3f(loader.getXmin(), loader.getYmax(), loader.getZmin());
	GL11.glEnd();

	//Face 3
	GL11.glLoadName(2);
	GL11.glBegin(GL11.GL_QUADS);
	GL11.glVertex3f(loader.getXmin(), loader.getYmin(), loader.getZmin());
	GL11.glVertex3f(loader.getXmin(), loader.getYmin(), loader.getZmax());
	GL11.glVertex3f(loader.getXmax(), loader.getYmin(), loader.getZmax());
	GL11.glVertex3f(loader.getXmax(), loader.getYmin(), loader.getZmin());
	GL11.glEnd();

	//Face 4
	GL11.glLoadName(3);
	GL11.glBegin(GL11.GL_QUADS);
	GL11.glVertex3f(loader.getXmax(), loader.getYmin(), loader.getZmin());
	GL11.glVertex3f(loader.getXmax(), loader.getYmax(), loader.getZmin());
	GL11.glVertex3f(loader.getXmax(), loader.getYmax(), loader.getZmax());
	GL11.glVertex3f(loader.getXmax(), loader.getYmin(), loader.getZmax());
	GL11.glEnd();

	//Face 5
	GL11.glLoadName(4);
	GL11.glBegin(GL11.GL_QUADS);
	GL11.glVertex3f(loader.getXmax(), loader.getYmax(), loader.getZmin());
	GL11.glVertex3f(loader.getXmax(), loader.getYmax(), loader.getZmax());
	GL11.glVertex3f(loader.getXmin(), loader.getYmax(), loader.getZmax());
	GL11.glVertex3f(loader.getXmin(), loader.getYmax(), loader.getZmin());
	GL11.glEnd();

	//Face 6
	GL11.glLoadName(5);
	GL11.glBegin(GL11.GL_QUADS);
	GL11.glVertex3f(loader.getXmin(), loader.getYmin(), loader.getZmax());
	GL11.glVertex3f(loader.getXmin(), loader.getYmax(), loader.getZmax());
	GL11.glVertex3f(loader.getXmax(), loader.getYmax(), loader.getZmax());
	GL11.glVertex3f(loader.getXmax(), loader.getYmin(), loader.getZmax());
	GL11.glEnd();

	GL11.glEnable(GL11.GL_LIGHTING);

	GL11.glEndList();
    }

    public void display() {
	GL11.glClear(GL11.GL_COLOR_BUFFER_BIT | GL11.GL_DEPTH_BUFFER_BIT);
	GL11.glMatrixMode(GL11.GL_PROJECTION);
	GL11.glLoadIdentity();
	setProjectionTransformations();
	projectionOrtho();

	GL11.glMatrixMode(GL11.GL_MODELVIEW);
	GL11.glLoadIdentity();

	if(isLight) {
	    GL11.glEnable(GL11.GL_LIGHTING);
	} else {
	    GL11.glDisable(GL11.GL_LIGHTING);
	}
         gl11.glCallList(boundingBoxDisplayList);
    }
}


I launch this window with:
package tests;

import loaders.vtk.LoaderVTK;

import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.lwjgl.LWJGLException;
import org.lwjgl.opengl.GL20;
import org.lwjgl.opengl.GLContext;

import visu.window.Window2;

public class TestWindow2 {
    static Display display;
    static Shell shell;
    static Composite composite;
    static Window2 window;

    public static void main(String[] args) {
	LoaderVTK loader = new LoaderVTK();
	loader.loadDataSet("/media/Donnees/tetra.vtk"); //"N:\\papillon.vtk")
	display = new Display();
	shell = new Shell(display);
	shell.setLayout(new FillLayout());
	composite = new Composite(shell, SWT.NONE);
	composite.setLayout(new FillLayout());

	window = new Window2(loader, composite, 640, 480);
	
	shell.setText("LWJGL Example");
	shell.setSize(640, 480);
	shell.open();

	display.asyncExec(new Runnable() {
	    public void run() {
		if (!window.getGLCanvas().isDisposed()) {
		    window.getGLCanvas().setCurrent();
		    try {
			GLContext.useContext(window.getGLCanvas());
		    } catch(LWJGLException e) { e.printStackTrace(); }

		    window.display();

		    display.asyncExec(this);
		}
	    }
	});

	while (!shell.isDisposed()) {
	    if (!display.readAndDispatch())
		display.sleep();
	}
	display.dispose();

    }
}


This code displays normaly a bounding box of my model ("/media/Donnees/tetra.vtk"):
# vtk DataFile Version 2.0

Unstructured Grid Example

ASCII

DATASET UNSTRUCTURED_GRID

POINTS 6 float

0 0 0

2 0 0

1 2 0

1 1 1

-5 -5 -5

5 5 5



CELLS 3 10

4 0 1 2 3

4 0 1 2 4

4 1 2 3 5



CELL_TYPES 3

10

10

10



CELL_DATA 3

SCALARS Material int 1

LOOKUP_TABLE default

2

1

0


Unfortunately, the window opens itself but the background is not cleared and the my computer becomes very slow (it's a xeon w3520, 3.5Go DDR3 and a Quadro FX1800).
I don't understand why this code doesn't work...
Anyone has got an idea?

Thanks.

Matthias

Looks like you transfer the GL context between threads very often. This is a very slow operation. You should just try plain LWJGL without SWT.

darkman19320

Thanks for your answer.
Unfortunately, I must create an Eclispe plugin so I must use swt...
Moreover, I have do another window using SWT with LWJGL and it's worked just fine.
It's the code of this window:
package visu.window;

import static common.RotationVariable.rotX;
import static common.RotationVariable.rotY;
import static common.RotationVariable.rotZ;
import static common.TranslationVariable.transX;
import static common.TranslationVariable.transY;
import static common.TranslationVariable.transZ;

import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.InputStream;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.DoubleBuffer;
import java.nio.FloatBuffer;
import java.nio.IntBuffer;
import java.util.ArrayList;

import loaders.common.LoaderAbstract;
import mouse.MyMouseListenner;

import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.opengl.GLCanvas;
import org.eclipse.swt.opengl.GLData;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Listener;
import org.lwjgl.BufferUtils;
import org.lwjgl.LWJGLException;
import org.lwjgl.opengl.ARBShaderObjects;
import org.lwjgl.opengl.GL11;
import org.lwjgl.opengl.GL15;
import org.lwjgl.opengl.GL20;
import org.lwjgl.opengl.GLContext;
import org.lwjgl.opengl.Util;
import org.lwjgl.util.glu.GLU;

import utils.FPSCounter;
import visu.function.VertexBufferObject;
import elements.cells.common.Cell;
import elements.colors.ColorRGBA;
import elements.colors.sampling.SamplingScalarsColor;
import elements.points.Point;
import elements.points.vertices.Vertex;
import elements.scalars.list.ScalarFloatList;
import elements.vectors.Vector;
import geometry.BoxIntersection;
import geometry.Line;
import geometry.Plane;
import geometry.Ray;

public class Window {
    private static final int NB_CLIP_PLANE = 6;
    private int widthWindow = 640;
    private int heightWindow = 480;
    private double screenRatio;

    private GLData data;
    private GLCanvas canvas;

    private float[] backGroundColor = { 1.0f, 1.0f, 1.0f, 1.0f };
    private float xLightPos = 0.0f;
    private float yLightPos = 0.0f;
    private float zLightPos = 2.0f;
    private float[] LightAmbient = { 0.5f, 0.5f, 0.5f, 1.0f };
    private float[] LightDiffuse = { 1.0f, 1.0f, 1.0f, 1.0f };
    private float[] LightPosition = { xLightPos, yLightPos, zLightPos, 1.0f };
    private static boolean isLight = true;

    private VertexBufferObject indexVBO;
    private VertexBufferObject vertexVBO;
    private VertexBufferObject colorVBO;

    private ColorRGBA beginColor = new ColorRGBA(1., 0, 0, 1);
    private ColorRGBA endColor = new ColorRGBA(0, 0, 1., 1);
    private SamplingScalarsColor samplingScalars = new SamplingScalarsColor(
	    beginColor, endColor, null, 2000000);

    private FPSCounter fps = new FPSCounter();

    private LoaderAbstract loader;
    private MyMouseListenner mouseListener = new MyMouseListenner();
    private boolean isBufferFill = false;

    public Window(Composite parent) {
	updateScreenRatio();

	parent.setLayout(new FillLayout());

	data = createGLData(true);
	canvas = createGLCanvas(parent, data);
	initGL();

	indexVBO = new VertexBufferObject();
	vertexVBO = new VertexBufferObject();
	colorVBO = new VertexBufferObject();
    }

    /**
     * Create a GLData with double buffering or not. A GLData is a
     * device-independent description of the pixel format attributes of a GL
     * drawable.
     * 
     * @param doubleBuffer
     * @return the GLData.
     */
    private GLData createGLData(boolean doubleBuffer) {
	GLData data = new GLData();
	data.doubleBuffer = doubleBuffer;
	data.depthSize = 1;
	data.sampleBuffers = 1;
	data.samples = 20;

	return data;
    }

    /**
     * Create a GLCanvas. A GLCanvas is a widget capable of displaying OpenGL
     * content.
     * 
     * @param parent
     * @param data
     * @return the GLCanvas
     */
    private GLCanvas createGLCanvas(Composite parent, GLData data) {
	if (data == null)
	    data = createGLData(true);

	final GLCanvas canvas;
	canvas = new GLCanvas(parent, SWT.NONE, data);

	canvas.setCurrent();
	try {
	    GLContext.useContext(canvas);
	} catch (LWJGLException e) {
	    e.printStackTrace();
	}

	canvas.addMouseListener(mouseListener);
	canvas.addMouseWheelListener(mouseListener);
	canvas.addMouseMoveListener(mouseListener);
	canvas.addMouseTrackListener(mouseListener);
	canvas.addKeyListener(mouseListener);

	canvas.addListener(SWT.Resize, new Listener() {
	    public void handleEvent(Event event) {
		Rectangle bounds = canvas.getBounds();
		resize(bounds.width, bounds.height);
	    }
	});
	return canvas;
    }

    public void resize(int width, int height) {
	widthWindow = width;
	heightWindow = height;

	updateScreenRatio();

	GL11.glViewport(0, 0, widthWindow, heightWindow);
	GL11.glMatrixMode(GL11.GL_PROJECTION);
	GL11.glLoadIdentity();

	setScene(screenRatio, loader.getDataSetRatio());

	GL11.glMatrixMode(GL11.GL_MODELVIEW);
	GL11.glLoadIdentity();
	setCamera();
    }

    private void updateScreenRatio() {
	screenRatio = (double) widthWindow / (double) heightWindow;
    }

    protected void setScene(double screenRatio, double modelRatio) {
	projectOrtho(screenRatio, modelRatio);
    }

    protected void projectOrtho(double screenRatio, double modelRatio) {
	if (loader == null)
	    return;
	double left, right, top, bottom, zNear, zFar;

	if (screenRatio > modelRatio) {
	    left = loader.getXmin()
	    - (((screenRatio * loader.getHeightDataSet()) - loader
		    .getWidthDataSet()) / 2.0)
		    - (loader.getDiagonal2D() / 20.0);
	    right = loader.getXmax()
	    + (((screenRatio * loader.getHeightDataSet()) - loader
		    .getWidthDataSet()) / 2.0)
		    + (loader.getDiagonal2D() / 20.0);
	    bottom = loader.getYmin() - (loader.getDiagonal2D() / 20.0);
	    top = loader.getYmax() + (loader.getDiagonal2D() / 20.0);
	    zNear = loader.getZmin() - loader.getDiagonal3D();
	    zFar = loader.getZmax() + loader.getDiagonal3D();
	} else {
	    left = loader.getXmin() - (loader.getDiagonal2D() / 20.0);
	    right = loader.getXmax() + (loader.getDiagonal2D() / 20.0);
	    bottom = loader.getYmin()
	    - (((loader.getWidthDataSet() / screenRatio) - loader
		    .getHeightDataSet()) / 2.0)
		    - (loader.getDiagonal2D() / 20.0);
	    top = loader.getYmax()
	    + (((loader.getWidthDataSet() / screenRatio) - loader
		    .getHeightDataSet()) / 2.0)
		    + (loader.getDiagonal2D() / 20.0);
	    zNear = loader.getZmin() - loader.getDiagonal3D();
	    zFar = loader.getZmax() + loader.getDiagonal3D();
	}

	if (zNear < 0 && zFar < 0) {
	    zNear *= -1;
	    zFar *= -1;
	}

	if (zNear >= zFar) {
	    double tmp = zNear;
	    zNear = zFar;
	    zFar = tmp;
	}

	GL11.glOrtho(left, right, bottom, top, zNear, zFar);
    }

    protected void setCamera() {
	Vertex gravity = loader.getGravityDataSet();

	double gravityX;
	double gravityY;
	double gravityZ;

	if (gravity != null) {
	    gravityX = gravity.x();
	    gravityY = gravity.y();
	    gravityZ = gravity.z();
	} else {
	    gravityX = 0;
	    gravityY = 0;
	    gravityZ = 0;
	}

	// Zoom 3D
	GL11.glTranslated(gravityX, gravityY, gravityZ);
	GL11.glScaled(transZ, transZ, 1.0f);
	GL11.glTranslated(-gravityX, -gravityY, -gravityZ);

	// Translation
	GL11.glTranslated(gravityX, gravityY, gravityZ);
	GL11.glTranslatef((float) transX, (float) transY, 0.0f);
	GL11.glTranslated(-gravityX, -gravityY, -gravityZ);

	// Rotation
	GL11.glTranslated(gravityX, gravityY, gravityZ);
	GL11.glRotatef((float) rotX, 0.0f, 1.0f, 0.0f);
	GL11.glRotatef((float) rotY, 1.0f, 0.0f, 0.0f);
	GL11.glRotatef((float) rotZ, 0.0f, 0.0f, 1.0f);
	GL11.glTranslated(-gravityX, -gravityY, -gravityZ);
    }

    public GLCanvas getGLCanvas() {
	return canvas;
    }

    public void initGL() {
	setShaders();

	GL11.glClear(GL11.GL_COLOR_BUFFER_BIT | GL11.GL_DEPTH_BUFFER_BIT);
	GL11.glLoadIdentity();
	GL11.glShadeModel(GL11.GL_SMOOTH);
	GL11.glClearColor(backGroundColor[0], backGroundColor[1],
		backGroundColor[2], backGroundColor[3]);
	GL11.glClearDepth(1.0f);
	GL11.glEnable(GL11.GL_DEPTH_TEST);
	GL11.glDepthFunc(GL11.GL_LEQUAL);
	GL11.glHint(GL11.GL_PERSPECTIVE_CORRECTION_HINT, GL11.GL_NICEST); // Really
	// Nice
	// Perspective
	// Calculations
	GL11.glViewport(0, 0, widthWindow, heightWindow);

	GL11.glEnable(GL11.GL_LIGHT0); // Enable Light One
	ByteBuffer temp = ByteBuffer.allocateDirect(16);
	temp.order(ByteOrder.nativeOrder());
	GL11.glLight(GL11.GL_LIGHT0, GL11.GL_AMBIENT, // Setup The Ambient Light
		(FloatBuffer) temp.asFloatBuffer().put(LightAmbient).flip());
	GL11.glLight(GL11.GL_LIGHT0, GL11.GL_DIFFUSE, // Setup The Diffuse Light
		(FloatBuffer) temp.asFloatBuffer().put(LightDiffuse).flip());
	GL11.glLight(GL11.GL_LIGHT0, GL11.GL_POSITION, // Position The Light
		(FloatBuffer) temp.asFloatBuffer().put(LightPosition).flip());

	GL11.glEnable(GL11.GL_COLOR_MATERIAL); // Enable Material Coloring
	GL11.glEnable(GL11.GL_BLEND);
	GL11.glBlendFunc(GL11.GL_SRC_ALPHA, GL11.GL_ONE_MINUS_SRC_ALPHA);
    }

    boolean firstDisplay = true;

    public void display() {
	mouseListener.isMoved = false;
	firstDisplay = false;
	fps.begin();

	GL11.glClear(GL11.GL_COLOR_BUFFER_BIT | GL11.GL_DEPTH_BUFFER_BIT);
	GL11.glMatrixMode(GL11.GL_PROJECTION);
	GL11.glLoadIdentity();
	setScene(screenRatio, loader.getDataSetRatio());
	setCamera();
	GL11.glMatrixMode(GL11.GL_MODELVIEW);
	GL11.glLoadIdentity();

	if (isLight)
	    GL11.glEnable(GL11.GL_LIGHTING);
	else
	    GL11.glDisable(GL11.GL_LIGHTING);

	GL11.glEnable(GL11.GL_BLEND);
	GL11.glBlendFunc(GL11.GL_SRC_ALPHA, GL11.GL_ONE_MINUS_SRC_ALPHA);
	
	GL11.glPolygonMode(GL11.GL_FRONT_AND_BACK, GL11.GL_FILL);

       vertexBufferObjectMode();

	GL11.glFlush();

	canvas.swapBuffers();
    }

    private void printLogInfo(int obj) {
	IntBuffer iVal = BufferUtils.createIntBuffer(1);
	ARBShaderObjects.glGetObjectParameterARB(obj,
		ARBShaderObjects.GL_OBJECT_INFO_LOG_LENGTH_ARB, iVal);

	int length = iVal.get();
	System.out.println("Info log length:" + length);
	if (length > 0) {
	    // We have some info we need to output.
	    ByteBuffer infoLog = BufferUtils.createByteBuffer(length);
	    iVal.flip();
	    ARBShaderObjects.glGetInfoLogARB(obj, iVal, infoLog);
	    byte[] infoBytes = new byte[length];
	    infoLog.get(infoBytes);
	    String out = new String(infoBytes);

	    System.out.println("Info log:\n" + out);
	}

	Util.checkGLError();
    }

    private static ByteBuffer getProgramCode(String filename) {
	InputStream fileInputStream = null;
	byte[] shaderCode = null;

	try {
	    if (fileInputStream == null)
		fileInputStream = new FileInputStream(filename);
	    DataInputStream dataStream = new DataInputStream(fileInputStream);
	    dataStream.readFully(shaderCode = new byte[fileInputStream
	                                               .available()]);
	    fileInputStream.close();
	    dataStream.close();
	} catch (Exception e) {
	    System.out.println(e.getMessage());
	}
	ByteBuffer shaderPro = BufferUtils.createByteBuffer(shaderCode.length);

	shaderPro.put(shaderCode);
	shaderPro.flip();

	return shaderPro;
    }

    private void setShaders() {

	ByteBuffer vs = null;
	ByteBuffer fs = null;

	int v, f, v2, f2, p;

	v = GL20.glCreateShader(GL20.GL_VERTEX_SHADER);
	f = GL20.glCreateShader(GL20.GL_FRAGMENT_SHADER);
	v2 = GL20.glCreateShader(GL20.GL_VERTEX_SHADER);
	f2 = GL20.glCreateShader(GL20.GL_FRAGMENT_SHADER);

	vs = getProgramCode("./shaders/color.vert");
	fs = getProgramCode("./shaders/color.frag");

	GL20.glShaderSource(v, vs);
	GL20.glShaderSource(f, fs);

	int isCompile = 0;

	GL20.glCompileShader(v);
	GL20.glCompileShader(f);

	// GL20.glCompileShader(f2);

	isCompile = GL20.glGetShader(v, GL20.GL_COMPILE_STATUS);
	if (isCompile != 1) {
	    System.err.println("Warning Color vertex shader is not compiled!");
	    printLogInfo(v);
	}
	isCompile = GL20.glGetShader(f, GL20.GL_COMPILE_STATUS);
	if (isCompile != 1) {
	    System.err
	    .println("Warning Color fragment shader is not compiled!");
	    printLogInfo(f);
	}

	ByteBuffer vs2 = null;
	ByteBuffer fs2 = null;

	vs2 = getProgramCode("./shaders/light_per_vertex.vert");
	fs2 = getProgramCode("./shaders/light_per_vertex.frag");

	GL20.glShaderSource(v2, vs2);
	GL20.glShaderSource(f2, fs2);

	GL20.glCompileShader(v2);
	GL20.glCompileShader(f2);

	isCompile = GL20.glGetShader(v2, GL20.GL_COMPILE_STATUS);
	if (isCompile != 1) {
	    System.err.println("Warning Light vertex shader is not compiled!");
	    printLogInfo(v2);
	}
	isCompile = GL20.glGetShader(f2, GL20.GL_COMPILE_STATUS);
	if (isCompile != 1) {
	    System.err
	    .println("Warning Light fragment shader is not compiled!");
	    printLogInfo(f2);
	}

	p = GL20.glCreateProgram();
	GL20.glAttachShader(p, f);
	GL20.glAttachShader(p, f2);
	GL20.glAttachShader(p, v);
	GL20.glAttachShader(p, v2);

	GL20.glLinkProgram(p);
	GL20.glUseProgram(p);

    }

    private void fillBufferObject() {
	if (loader != null) {
	    ArrayList<Vertex> vertexList = new ArrayList<Vertex>();
	    ArrayList<Integer> vertexIndexList = new ArrayList<Integer>();
	    for(Cell c : loader.getCells()) {
		for(int[] faces : c.getVertexIndicesList()) {
		    for(int indexVertex : faces) {
			vertexIndexList.add(indexVertex);
		    }
		}
	    }
	    for(Vertex v : loader.getVertices()) {
		vertexList.add(v);
	    }

	    double[] vertices = new double[vertexList.size() * 3];
	    int index = 0;
	    for (Vertex v : vertexList) {
		vertices[index++] = v.x();
		vertices[index++] = v.y();
		vertices[index++] = v.z();
	    }
	    vertexList = null;
	    vertexVBO.bufferData(vertices, GL15.GL_STATIC_DRAW);
	    vertices = null;
	    System.out.println("Vertex: " + vertexVBO.getSizeBuffer() / 3);

	    int[] indices = new int[vertexIndexList.size()];
	    index = 0;
	    for (Integer i : vertexIndexList) {
		indices[index++] = i;
	    }
	    vertexIndexList = null;
	    indexVBO.bufferElementData(indices, GL15.GL_STATIC_DRAW);
	    indices = null;
	    System.out.println("Index: " + indexVBO.getSizeBuffer() / 3);

	    ScalarFloatList scalarCell = ((ArrayList<ScalarFloatList>)loader.getScalarCellManagement()).get(0);
	    ArrayList<Cell> cells = ((ArrayList<Cell>)loader.getCells());
	    double[] colors = samplingScalars.getSamplesFromCellScalar(scalarCell, cells);
	    colorVBO.bufferData(colors, GL15.GL_DYNAMIC_DRAW);
	    colors = null;
	    System.out.println("Colors: " + colorVBO.getSizeBuffer() / 4);
	}
    }

    private int boundingBoxDisplayList = -1;

    private int boundingBoxDisplayList() {
	if(loader == null)
	    return -1;

	int displayList = GL11.glGenLists(1);

	GL11.glNewList(displayList, GL11.GL_COMPILE);

	GL11.glDisable(GL11.GL_LIGHTING);
	GL11.glLineWidth(2f);
	GL11.glColor4d(0, 0, 0, 1.0);

	//Face 1
	GL11.glLoadName(0);
	GL11.glBegin(GL11.GL_QUADS);
	GL11.glVertex3f(loader.getXmin(), loader.getYmin(), loader.getZmin());
	GL11.glVertex3f(loader.getXmax(), loader.getYmin(), loader.getZmin());
	GL11.glVertex3f(loader.getXmax(), loader.getYmax(), loader.getZmin());
	GL11.glVertex3f(loader.getXmin(), loader.getYmax(), loader.getZmin());
	GL11.glEnd();

	//Face 2
	GL11.glLoadName(1);
	GL11.glBegin(GL11.GL_QUADS);
	GL11.glVertex3f(loader.getXmin(), loader.getYmin(), loader.getZmin());
	GL11.glVertex3f(loader.getXmin(), loader.getYmin(), loader.getZmax());
	GL11.glVertex3f(loader.getXmin(), loader.getYmax(), loader.getZmax());
	GL11.glVertex3f(loader.getXmin(), loader.getYmax(), loader.getZmin());
	GL11.glEnd();

	//Face 3
	GL11.glLoadName(2);
	GL11.glBegin(GL11.GL_QUADS);
	GL11.glVertex3f(loader.getXmin(), loader.getYmin(), loader.getZmin());
	GL11.glVertex3f(loader.getXmin(), loader.getYmin(), loader.getZmax());
	GL11.glVertex3f(loader.getXmax(), loader.getYmin(), loader.getZmax());
	GL11.glVertex3f(loader.getXmax(), loader.getYmin(), loader.getZmin());
	GL11.glEnd();

	//Face 4
	GL11.glLoadName(3);
	GL11.glBegin(GL11.GL_QUADS);
	GL11.glVertex3f(loader.getXmax(), loader.getYmin(), loader.getZmin());
	GL11.glVertex3f(loader.getXmax(), loader.getYmax(), loader.getZmin());
	GL11.glVertex3f(loader.getXmax(), loader.getYmax(), loader.getZmax());
	GL11.glVertex3f(loader.getXmax(), loader.getYmin(), loader.getZmax());
	GL11.glEnd();

	//Face 5
	GL11.glLoadName(4);
	GL11.glBegin(GL11.GL_QUADS);
	GL11.glVertex3f(loader.getXmax(), loader.getYmax(), loader.getZmin());
	GL11.glVertex3f(loader.getXmax(), loader.getYmax(), loader.getZmax());
	GL11.glVertex3f(loader.getXmin(), loader.getYmax(), loader.getZmax());
	GL11.glVertex3f(loader.getXmin(), loader.getYmax(), loader.getZmin());
	GL11.glEnd();

	//Face 6
	GL11.glLoadName(5);
	GL11.glBegin(GL11.GL_QUADS);
	GL11.glVertex3f(loader.getXmin(), loader.getYmin(), loader.getZmax());
	GL11.glVertex3f(loader.getXmin(), loader.getYmax(), loader.getZmax());
	GL11.glVertex3f(loader.getXmax(), loader.getYmax(), loader.getZmax());
	GL11.glVertex3f(loader.getXmax(), loader.getYmin(), loader.getZmax());
	GL11.glEnd();

	GL11.glEnable(GL11.GL_LIGHTING);

	GL11.glEndList();

	return displayList;
    }

    private void vertexBufferObjectMode() {
	if (loader != null) {
	    if (!isBufferFill) {
		isBufferFill = true;
		if (!GLContext.getCapabilities().GL_ARB_vertex_buffer_object) {
		    System.out.println("VertexBufferObject not supported");
		    return;
		}
		fillBufferObject();
		boundingBoxDisplayList = boundingBoxDisplayList();
	    }

	    GL11.glEnable(GL11.GL_COLOR_ARRAY);
	    GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, colorVBO.getVBOId());
	    GL11.glColorPointer(4, GL11.GL_DOUBLE, 0, 0);

	    GL11.glEnable(GL11.GL_VERTEX_ARRAY);
	    GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, vertexVBO.getVBOId());
	    GL11.glVertexPointer(3, GL11.GL_DOUBLE, 0, 0);

	    GL11.glEnable(GL15.GL_ELEMENT_ARRAY_BUFFER);
	    GL15.glBindBuffer(GL15.GL_ELEMENT_ARRAY_BUFFER, 
		    indexVBO.getVBOId());
	    GL11.glVertexPointer(3, GL11.GL_UNSIGNED_INT, 0, 0);

	    GL11.glPolygonMode(GL11.GL_FRONT_AND_BACK, GL11.GL_FILL);
	    GL11.glDrawElements(GL11.GL_TRIANGLES, indexVBO.getSizeBuffer(),
		    GL11.GL_UNSIGNED_INT, 0);

	    GL11.glDisableClientState(GL11.GL_VERTEX_ARRAY);
	    GL11.glDisableClientState(GL15.GL_ELEMENT_ARRAY_BUFFER);

	    GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, 0);
	    GL15.glBindBuffer(GL15.GL_ELEMENT_ARRAY_BUFFER, 0);
	}
    }

    public void setLoader(LoaderAbstract loader) {
	this.loader = loader;
	if (this.loader != null) {
	    mouseListener.setDiag2D(loader.getDiagonal2D());
	    modelDisplayList = initDisplayList();
	    selectBuffer = BufferUtils.createIntBuffer(loader.getNbCells()*4);
	}
    }
}


I don't see any difference between the two versions (unless the shader implementation but I try to remove this function but no change, my previous window doesn't work...)

Edit: This window doesn't work now.... I didn't do any change but yesterday it worked great...

Edit2: I found the source of the problem: the last nvidia driver (256.xx)....

Matthias

Are you sure that you don't do something which defeats the multi threaded GL driver from nVidia - like calling glGet* every frame? You can test this by disabling the multi threaded GL driver in the control panel (Windows).

darkman19320

Sorry I don't have windows. I'm just working  on linux system.