Last active
October 20, 2017 15:16
-
-
Save hanslovsky/6dcad29151387970422217236ad56a15 to your computer and use it in GitHub Desktop.
Simple example for scenery and javafx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.nio.ByteBuffer; | |
import java.util.concurrent.CountDownLatch; | |
import com.sun.javafx.application.PlatformImpl; | |
import cleargl.GLTypeEnum; | |
import cleargl.GLVector; | |
import graphics.scenery.Box; | |
import graphics.scenery.Camera; | |
import graphics.scenery.DetachedHeadCamera; | |
import graphics.scenery.GenericTexture; | |
import graphics.scenery.Material; | |
import graphics.scenery.PointLight; | |
import graphics.scenery.SceneryBase; | |
import graphics.scenery.SceneryElement; | |
import graphics.scenery.backends.Renderer; | |
import graphics.scenery.utils.SceneryPanel; | |
import javafx.application.Platform; | |
import javafx.scene.Scene; | |
import javafx.scene.layout.HBox; | |
import javafx.scene.layout.Priority; | |
import javafx.stage.Stage; | |
public class SimpleSceneryExampleGradientTextureJavaFX extends SceneryBase | |
{ | |
private final SceneryPanel[] panel = new SceneryPanel[ 1 ]; | |
public SimpleSceneryExampleGradientTextureJavaFX( final String applicationName, final int windowWidth, final int windowHeight, final boolean wantREPL ) | |
{ | |
super( applicationName, windowWidth, windowHeight, wantREPL ); | |
} | |
@Override | |
public void init() | |
{ | |
PlatformImpl.startup( () -> {} ); | |
final CountDownLatch latch = new CountDownLatch( 1 ); | |
Platform.runLater( () -> { | |
panel[ 0 ] = new SceneryPanel( getWindowWidth(), getWindowHeight() ); | |
final HBox root = new HBox(); | |
HBox.setHgrow( panel[ 0 ], Priority.ALWAYS ); | |
root.getChildren().add( panel[ 0 ] ); | |
final Stage stage = new Stage(); | |
stage.setTitle( getApplicationName() ); | |
final Scene scene = new Scene( root ); | |
stage.setScene( scene ); | |
stage.setOnCloseRequest( event -> { | |
getRenderer().setShouldClose( true ); | |
Platform.runLater( Platform::exit ); | |
} ); | |
stage.show(); | |
latch.countDown(); | |
} ); | |
try | |
{ | |
latch.await(); | |
} | |
catch ( final InterruptedException e ) | |
{ | |
e.printStackTrace(); | |
} | |
setRenderer( | |
Renderer.createRenderer( getHub(), getApplicationName(), getScene(), getWindowWidth(), getWindowHeight(), panel[ 0 ] ) ); | |
getHub().add( SceneryElement.Renderer, getRenderer() ); | |
final int numChannels = 4; | |
final int w = 300, h = 300; | |
final byte[] texture = new byte[ w * h * numChannels ]; | |
for ( int y = 0, i = 0; y < h; ++y ) | |
for ( int x = 0; x < w; ++x, i += numChannels ) | |
{ | |
final int r = ( int ) ( x * 255.0 / h ); | |
final int g = r; | |
final int b = r; | |
final int a = 255; | |
texture[ i + 0 ] = ( byte ) r; | |
texture[ i + 1 ] = ( byte ) g; | |
texture[ i + 2 ] = ( byte ) b; | |
texture[ i + 3 ] = ( byte ) a; | |
} | |
final Box box = new Box( new GLVector( 1.0f, 1.0f, 1.0f ), true ); | |
final Material m = new Material(); | |
m.setAmbient( new GLVector( 1.0f, 1.0f, 1.0f ) ); | |
m.setDiffuse( new GLVector( 1.0f, 1.0f, 1.0f ) ); | |
m.setSpecular( new GLVector( 1.0f, 1.0f, 1.0f ) ); | |
m.getTransferTextures().put( "random", new GenericTexture( "random", new GLVector( w, h, 1.0f ), numChannels, GLTypeEnum.Byte, ByteBuffer.wrap( texture ), true, true ) ); | |
m.getTextures().put( "diffuse", "fromBuffer:random" ); | |
box.setMaterial( m ); | |
getScene().addChild( box ); | |
System.out.println( "camera... " ); | |
final Camera cam = new DetachedHeadCamera(); | |
cam.setPosition( new GLVector( 0f, 0f, 5.0f ) ); | |
cam.perspectiveCamera( 50.0f, getWindowWidth(), getWindowHeight(), 0.1f, 1000.f ); | |
cam.setActive( true ); | |
getScene().addChild( cam ); | |
final PointLight[] lights = new PointLight[ 10 ]; | |
for ( int i = 0; i < lights.length; i++ ) | |
{ | |
lights[ i ] = new PointLight(); | |
lights[ i ].setPosition( new GLVector( 2.0f * i, 2.0f * i, 2.0f * i ) ); | |
lights[ i ].setEmissionColor( new GLVector( 1.0f, 1.0f, 1.0f ) ); | |
lights[ i ].setIntensity( 500.2f * ( i + 1 ) ); | |
getScene().addChild( lights[ i ] ); | |
} | |
new Thread( () -> { | |
while ( true ) | |
{ | |
box.getRotation().rotateByAngleY( 0.01f ); | |
box.setNeedsUpdate( true ); | |
try | |
{ | |
Thread.sleep( 20 ); | |
} | |
catch ( final InterruptedException e ) | |
{ | |
e.printStackTrace(); | |
} | |
} | |
} ).start(); | |
} | |
public static void main( final String[] args ) | |
{ | |
final SimpleSceneryExampleGradientTextureJavaFX example = new SimpleSceneryExampleGradientTextureJavaFX( "texture example", 500, 500, false ); | |
example.main(); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.concurrent.CountDownLatch; | |
import com.sun.javafx.application.PlatformImpl; | |
import cleargl.GLVector; | |
import graphics.scenery.Box; | |
import graphics.scenery.Camera; | |
import graphics.scenery.DetachedHeadCamera; | |
import graphics.scenery.Material; | |
import graphics.scenery.PointLight; | |
import graphics.scenery.SceneryBase; | |
import graphics.scenery.SceneryElement; | |
import graphics.scenery.backends.Renderer; | |
import graphics.scenery.utils.SceneryPanel; | |
import javafx.application.Platform; | |
import javafx.scene.Scene; | |
import javafx.scene.layout.HBox; | |
import javafx.scene.layout.Priority; | |
import javafx.stage.Stage; | |
public class SimpleSceneryExampleJavaFX extends SceneryBase | |
{ | |
private final SceneryPanel[] panel = new SceneryPanel[ 1 ]; | |
public SimpleSceneryExampleJavaFX( final String applicationName, final int windowWidth, final int windowHeight, final boolean wantREPL ) | |
{ | |
super( applicationName, windowWidth, windowHeight, wantREPL ); | |
} | |
@Override | |
public void init() | |
{ | |
PlatformImpl.startup( () -> {} ); | |
final CountDownLatch latch = new CountDownLatch( 1 ); | |
Platform.runLater( () -> { | |
panel[ 0 ] = new SceneryPanel( getWindowWidth(), getWindowHeight() ); | |
final HBox root = new HBox(); | |
HBox.setHgrow( panel[ 0 ], Priority.ALWAYS ); | |
root.getChildren().add( panel[ 0 ] ); | |
final Stage stage = new Stage(); | |
stage.setTitle( getApplicationName() ); | |
final Scene scene = new Scene( root ); | |
stage.setScene( scene ); | |
stage.setOnCloseRequest( event -> { | |
getRenderer().setShouldClose( true ); | |
Platform.runLater( Platform::exit ); | |
} ); | |
stage.show(); | |
latch.countDown(); | |
} ); | |
try | |
{ | |
latch.await(); | |
} | |
catch ( final InterruptedException e ) | |
{ | |
e.printStackTrace(); | |
} | |
setRenderer( | |
Renderer.createRenderer( getHub(), getApplicationName(), getScene(), getWindowWidth(), getWindowHeight(), panel[ 0 ] ) ); | |
getHub().add( SceneryElement.Renderer, getRenderer() ); | |
final Box box = new Box( new GLVector( 1.0f, 1.0f, 1.0f ), true ); | |
final Material m = new Material(); | |
m.setAmbient( new GLVector( 1.0f, 1.0f, 1.0f ) ); | |
m.setDiffuse( new GLVector( 1.0f, 1.0f, 1.0f ) ); | |
m.setSpecular( new GLVector( 1.0f, 1.0f, 1.0f ) ); | |
// m.setDoubleSided( true ); | |
box.setMaterial( m ); | |
getScene().addChild( box ); | |
System.out.println( "camera... " ); | |
final Camera cam = new DetachedHeadCamera(); | |
cam.setPosition( new GLVector( 0f, 0f, 5.0f ) ); | |
cam.perspectiveCamera( 50.0f, getWindowWidth(), getWindowHeight(), 0.1f, 1000.f ); | |
cam.setActive( true ); | |
getScene().addChild( cam ); | |
final PointLight[] lights = new PointLight[ 10 ]; | |
for ( int i = 0; i < lights.length; i++ ) | |
{ | |
lights[ i ] = new PointLight(); | |
lights[ i ].setPosition( new GLVector( 2.0f * i, 2.0f * i, 2.0f * i ) ); | |
lights[ i ].setEmissionColor( new GLVector( 1.0f, 0.0f, 1.0f ) ); | |
lights[ i ].setIntensity( 500.2f * ( i + 1 ) ); | |
getScene().addChild( lights[ i ] ); | |
} | |
new Thread( () -> { | |
while ( true ) | |
{ | |
box.getRotation().rotateByAngleY( 0.01f ); | |
box.setNeedsUpdate( true ); | |
try | |
{ | |
Thread.sleep( 20 ); | |
} | |
catch ( final InterruptedException e ) | |
{ | |
e.printStackTrace(); | |
} | |
} | |
} ).start(); | |
} | |
public static void main( final String[] args ) | |
{ | |
final SimpleSceneryExampleJavaFX example = new SimpleSceneryExampleJavaFX( "texture example", 500, 500, false ); | |
example.main(); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.nio.ByteBuffer; | |
import java.util.Random; | |
import java.util.concurrent.CountDownLatch; | |
import com.sun.javafx.application.PlatformImpl; | |
import cleargl.GLTypeEnum; | |
import cleargl.GLVector; | |
import graphics.scenery.Box; | |
import graphics.scenery.Camera; | |
import graphics.scenery.DetachedHeadCamera; | |
import graphics.scenery.GenericTexture; | |
import graphics.scenery.Material; | |
import graphics.scenery.PointLight; | |
import graphics.scenery.SceneryBase; | |
import graphics.scenery.SceneryElement; | |
import graphics.scenery.backends.Renderer; | |
import graphics.scenery.utils.SceneryPanel; | |
import javafx.application.Platform; | |
import javafx.scene.Scene; | |
import javafx.scene.layout.HBox; | |
import javafx.scene.layout.Priority; | |
import javafx.stage.Stage; | |
public class SimpleSceneryExampleRandomTextureJavaFX extends SceneryBase | |
{ | |
private final SceneryPanel[] panel = new SceneryPanel[ 1 ]; | |
public SimpleSceneryExampleRandomTextureJavaFX( final String applicationName, final int windowWidth, final int windowHeight, final boolean wantREPL ) | |
{ | |
super( applicationName, windowWidth, windowHeight, wantREPL ); | |
} | |
@Override | |
public void init() | |
{ | |
PlatformImpl.startup( () -> {} ); | |
final CountDownLatch latch = new CountDownLatch( 1 ); | |
Platform.runLater( () -> { | |
panel[ 0 ] = new SceneryPanel( getWindowWidth(), getWindowHeight() ); | |
final HBox root = new HBox(); | |
HBox.setHgrow( panel[ 0 ], Priority.ALWAYS ); | |
root.getChildren().add( panel[ 0 ] ); | |
final Stage stage = new Stage(); | |
stage.setTitle( getApplicationName() ); | |
final Scene scene = new Scene( root ); | |
stage.setScene( scene ); | |
stage.setOnCloseRequest( event -> { | |
getRenderer().setShouldClose( true ); | |
Platform.runLater( Platform::exit ); | |
} ); | |
stage.show(); | |
latch.countDown(); | |
} ); | |
try | |
{ | |
latch.await(); | |
} | |
catch ( final InterruptedException e ) | |
{ | |
e.printStackTrace(); | |
} | |
setRenderer( | |
Renderer.createRenderer( getHub(), getApplicationName(), getScene(), getWindowWidth(), getWindowHeight(), panel[ 0 ] ) ); | |
getHub().add( SceneryElement.Renderer, getRenderer() ); | |
final Random rng = new Random(); | |
final int numChannels = 4; | |
final int w = 300, h = 300; | |
final byte[] texture = new byte[ w * h * numChannels ]; | |
rng.nextBytes( texture ); | |
final Box box = new Box( new GLVector( 1.0f, 1.0f, 1.0f ), true ); | |
final Material m = new Material(); | |
m.setAmbient( new GLVector( 1.0f, 1.0f, 1.0f ) ); | |
m.setDiffuse( new GLVector( 1.0f, 1.0f, 1.0f ) ); | |
m.setSpecular( new GLVector( 1.0f, 1.0f, 1.0f ) ); | |
m.getTransferTextures().put( "random", new GenericTexture( "random", new GLVector( w, h, 1.0f ), numChannels, GLTypeEnum.Byte, ByteBuffer.wrap( texture ), true, true ) ); | |
m.getTextures().put( "diffuse", "fromBuffer:random" ); | |
// m.setDoubleSided( true ); | |
box.setMaterial( m ); | |
getScene().addChild( box ); | |
System.out.println( "camera... " ); | |
final Camera cam = new DetachedHeadCamera(); | |
cam.setPosition( new GLVector( 0f, 0f, 5.0f ) ); | |
cam.perspectiveCamera( 50.0f, getWindowWidth(), getWindowHeight(), 0.1f, 1000.f ); | |
cam.setActive( true ); | |
getScene().addChild( cam ); | |
final PointLight[] lights = new PointLight[ 10 ]; | |
for ( int i = 0; i < lights.length; i++ ) | |
{ | |
lights[ i ] = new PointLight(); | |
lights[ i ].setPosition( new GLVector( 2.0f * i, 2.0f * i, 2.0f * i ) ); | |
lights[ i ].setEmissionColor( new GLVector( 1.0f, 1.0f, 1.0f ) ); | |
lights[ i ].setIntensity( 500.2f * ( i + 1 ) ); | |
getScene().addChild( lights[ i ] ); | |
} | |
new Thread( () -> { | |
while ( true ) | |
{ | |
box.getRotation().rotateByAngleY( 0.01f ); | |
box.setNeedsUpdate( true ); | |
try | |
{ | |
Thread.sleep( 20 ); | |
} | |
catch ( final InterruptedException e ) | |
{ | |
e.printStackTrace(); | |
} | |
} | |
} ).start(); | |
} | |
public static void main( final String[] args ) | |
{ | |
final SimpleSceneryExampleRandomTextureJavaFX example = new SimpleSceneryExampleRandomTextureJavaFX( "texture example", 500, 500, false ); | |
example.main(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment