Swt and lwjgl 2.9.3 problem with changing color into glClearColor()

Started by sourceskyboxer, May 21, 2018, 13:13:15

Previous topic - Next topic

sourceskyboxer

This is quote from old thread:
Quote from: tris on June 24, 2010, 16:26:39
You can use multiple canvases. Just set the current cavas to be the current gl contex before you draw each one like so:


package com.tscribble.game.examples.swtopengl;

import static org.lwjgl.opengl.GL11.*;

import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.*;

import com.tscribble.game.opengl.swt.GLComposite2D;

/**
 * @author triston
 * 
 */
public class SWTMultiComposite {

	private Display display;
	private GLComposite2D canvas,canvas2;
	private float ang,ang2 = 0;
	
	public static void main(String[] args) {
		SWTMultiComposite window = new SWTMultiComposite();
		window.open();
	}

	public void open() {
		display = Display.getDefault();
		Shell shell = new Shell();
		shell.setSize(1024, 600);
		shell.setText("SWT GL Mutiple Composites");
		shell.setLayout(new FillLayout(SWT.HORIZONTAL));
		
		Menu mBar = new Menu(shell, SWT.BAR);
		shell.setMenuBar(mBar);
		
		MenuItem fileItem = new MenuItem(mBar, SWT.CASCADE);
		fileItem.setText("File");
		
		Menu fileMenu = new Menu(fileItem);
		fileItem.setMenu(fileMenu);
		
		for (int i = 0; i < 5; i++) {
			final MenuItem importItem = new MenuItem(fileMenu, SWT.NONE);
			importItem.setText("Item " + i);
		}
		
		canvas = new GLComposite2D(shell);	
		canvas.addListener(SWT.Resize, new Listener() {
			public void handleEvent(Event event) {				
				doResize(canvas);
		}});	
		
		canvas2 = new GLComposite2D(shell);			
		canvas2.addListener(SWT.Resize, new Listener() {
			public void handleEvent(Event event) {				
				doResize(canvas2);
		}});	

		shell.open();		
		start(); // render loop thread

		while (!shell.isDisposed()) {
			if (!display.readAndDispatch()) {
				display.sleep();
			}
		}
		display.dispose();
	}
	protected void initGL() {		
		glShadeModel(GL_SMOOTH); // Enable Smooth Shading, GL_SMOOTH (the default) or GL_FLAT. 
	}

	private void doResize(GLComposite2D comp) {
		Rectangle rec = comp.getBounds();
		float w = Math.max(rec.width, 1);
		float h = Math.max(rec.height, 1);
		
		comp.makeCurrent();

		glViewport(0, 0, (int) w, (int) h);

		glMatrixMode(GL_PROJECTION);
		glLoadIdentity();

		glOrtho(0, w, h, 0, 1, -1);

		glMatrixMode(GL_MODELVIEW);
		glLoadIdentity();
	}
	
	public void start() {
		display.syncExec(new Runnable() {
			public void run() {
				if (!canvas.isDisposed() || !canvas2.isDisposed()) {					
					try {Thread.sleep(10);}catch (InterruptedException e){}
					logic();
					logic2();					
					
					canvas.makeCurrent();
					draw();					
				
					canvas2.makeCurrent();
					draw2();
					
					canvas.swapBuffers();
					canvas2.swapBuffers();
					
					display.asyncExec(this);
				}
			}
		});
	}

	public void logic() {
		ang += 0.5f % 360;
	}
	
	public void logic2() {
		ang2 -= 0.5f % 360;
	}

	private void draw() {
		glClear(GL_COLOR_BUFFER_BIT);
		glClearColor(1.0f, 0.0f, 0.0f, 0.0f);  // Red Background

		glPushMatrix();
		{
			glTranslatef(10, 10, 0);
			glBegin(GL_TRIANGLES);
			glColor3f(1.0f, 0.0f, 0.0f); // red
			glVertex2f(5.0f, 5.0f);
			
			glColor3f(0.0f, 1.0f, 0.0f); // green
			glVertex2f(400.0f, 5.0f);
			
			glColor3f(0.0f, 0.0f, 1.0f); // blue
			glVertex2f(5.0f, 400.0f);
			glEnd();
		}
		glPopMatrix();
	}
	
	private void draw2() {
		glClear(GL_COLOR_BUFFER_BIT);
		glClearColor(0.0f, 0.0f, 1.0f, 0.0f);  // Blue Background

		glPushMatrix();
		{
			glTranslatef(10, 10, 0);
			glBegin(GL_TRIANGLES);
			glColor3f(1.0f, 0.0f, 0.0f); // red
			glVertex2f(5.0f, 5.0f);
			glColor3f(0.0f, 1.0f, 0.0f); // green
			glVertex2f(100.0f, 5.0f);
			glColor3f(0.0f, 0.0f, 1.0f); // blue
			glVertex2f(5.0f, 100.0f);
			glEnd();
		}
		glPopMatrix();
	}
		
}


The GLComposite
package com.tscribble.game.opengl.swt;

import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.opengl.GLCanvas;
import org.eclipse.swt.opengl.GLData;
import org.eclipse.swt.widgets.*;
import org.lwjgl.LWJGLException;

import org.lwjgl.opengl.*;

/**
 * @author triston
 */
public class GLComposite2D extends GLCanvas {
	private static GLData gd1;
	protected Rectangle rec;

	static {
		gd1 = new GLData();
		gd1.doubleBuffer = true;
	}

	public GLComposite2D(Composite parent) {
		super(parent, SWT.NONE, gd1);	
	}

	public void makeCurrent() {
		setCurrent();
		try {
			GLContext.useContext(this);
		} catch (LWJGLException e) {
			e.printStackTrace();
		}
	}	
}


hope that helps

I am sorry - It is not spam just I want continue that because Excelsior works with lwjgl 2.9.3 + swt 4.7.2 fine

And I want know how do I change color of glClearColor()

I have problem with changing color - I have tested with lwjgl 3.1 = no problem with changing color of background's glClearColor()
But how does it work for lwjgl 2.9.3?

Because I really want create powerful program alternative to Xenko GameStudio, Unity3D, MonoGame or WaveEngine. Because I feel sad that C# coders don't believe that because Java is really richer than C# because Java has a lot of features like toRadiant() from Math class, FloatBuffer() etc..

I really want know how does color change of glClearColor() by clicking

Here is code:
import static org.lwjgl.opengl.GL11.*;
 
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.RGB;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.*;
  
/**
 * @author triston
 * 
 */
public class SwtDemo {
 
    private Display display;
    private GLComposite2D canvas;
     
    public static void main(String[] args) {
    	SwtDemo window = new SwtDemo();
        window.open();
    }
 
    public void open() {
        display = Display.getDefault();
        Shell shell = new Shell();
        shell.setSize(1024, 600);
        shell.setText("SWT GL Changing color for background");
        shell.setLayout(new FillLayout(SWT.HORIZONTAL));
         
        Menu menubar = new Menu(shell, SWT.BAR);
		
		MenuItem fileItem = new MenuItem(menubar, SWT.CASCADE);
		fileItem.setText("File");
		
		Menu fileMenu = new Menu(fileItem);
		fileItem.setMenu(fileMenu);
		
		MenuItem exitItem = new MenuItem(fileMenu, SWT.NONE);
		exitItem.setText("Exit");
		
		exitItem.addListener(SWT.Selection, event-> {
            shell.getDisplay().dispose();
            System.exit(0);
        });
		
		MenuItem setupItem = new MenuItem(menubar, SWT.CASCADE);
		setupItem.setText("Setup");
		
		Menu setupMenu = new Menu(setupItem);
		setupItem.setMenu(setupMenu);
		
		MenuItem changebackgroundItem = new MenuItem(setupMenu, SWT.NONE);
		changebackgroundItem.setText("Change background");
		
		new MenuItem(setupMenu, SWT.SEPARATOR);
		
		MenuItem defaultColorForBackgroundItem = new MenuItem(setupMenu, SWT.NONE);
		defaultColorForBackgroundItem.setText("Default background color");
		
		changebackgroundItem.addListener(SWT.Selection, event-> {
        	ColorDialog dialog = new ColorDialog(shell);
			RGB rgb = dialog.open();
	        
			if(rgb != null)
			{
	            Color col = new Color(shell.getDisplay(), rgb);
	            glClearColor(col.getRed()/255.0f, col.getGreen()/255.0f, col.getBlue()/255.0f, 1.0f);
	            col.dispose();
			}
		});
        
        defaultColorForBackgroundItem.addListener(SWT.Selection, event-> {
			glClearColor(1.0f, 0.0f, 0.0f, 0.0f);
		});
		
		shell.setMenuBar(menubar);
         
        canvas = new GLComposite2D(shell);  
        canvas.addListener(SWT.Resize, new Listener() {
            public void handleEvent(Event event) {              
                doResize(canvas);
        }});     
 
        shell.open();
        
        
        display.syncExec(new Runnable() {
            public void run() {   
            	
                if (!canvas.isDisposed()) {                    
                    logic();
                     
                    canvas.makeCurrent();
                    draw();          
                     
                    canvas.swapBuffers();
                     
                    display.asyncExec(this);
                }
            }
        });
 
        while (!shell.isDisposed()) {
            if (!display.readAndDispatch()) {
                display.sleep();
            }
        }
        display.dispose();
    }
    protected void initGL() {       
        glShadeModel(GL_SMOOTH); // Enable Smooth Shading, GL_SMOOTH (the default) or GL_FLAT. 
    }
 
    private void doResize(GLComposite2D comp) {
        Rectangle rec = comp.getBounds();
        float w = Math.max(rec.width, 1);
        float h = Math.max(rec.height, 1);
         
        comp.makeCurrent();
 
        glViewport(0, 0, (int) w, (int) h);
 
        glMatrixMode(GL_PROJECTION);
        glLoadIdentity();
 
        glOrtho(0, w, h, 0, 1, -1);
 
        glMatrixMode(GL_MODELVIEW);
        glLoadIdentity();
    }
 
    @SuppressWarnings("unused")
	public void logic() {
    	int ans = 0;
    	ans += 0.5f % 360;
    }
 
    private void draw() {
        glClear(GL_COLOR_BUFFER_BIT);
        glClearColor(1.0f, 0.0f, 0.0f, 0.0f);
 
        glPushMatrix();
        {
            glTranslatef(10, 10, 0);
            glBegin(GL_TRIANGLES);
            glColor3f(1.0f, 0.0f, 0.0f); // red
            glVertex2f(5.0f, 5.0f);
             
            glColor3f(0.0f, 1.0f, 0.0f); // green
            glVertex2f(400.0f, 5.0f);
             
            glColor3f(0.0f, 0.0f, 1.0f); // blue
            glVertex2f(5.0f, 400.0f);
            glEnd();
        }
        glPopMatrix();
    }         
}


How do I get working with lwjgl 2.9.3?

Thanks!

KaiHH

Simply do not reset the clear color in line 141 in the draw() method: glClearColor(1.0f, 0.0f, 0.0f, 0.0f);

sourceskyboxer

Thanks I have moved glClearColor() to in method open() after shell.open()

It works thanks!

I am really excited to Java programming because I know much about Java. Because C# is very poor and has not more features. Thanks ! I will continue my development