From 8af44189d19069a80976bc681deca7001f6814ef Mon Sep 17 00:00:00 2001 From: DesertCookie Date: Sun, 4 Feb 2018 14:37:19 +0100 Subject: [PATCH] updated to FreeD v0.0.23 added classes Model, ModelCreator, ModelRenderer, Shader, StaticShader and files static/fragmentShader.glsl, static/vertexShader.glsl --- res/shaders/static/fragmentShader.glsl | 11 ++ res/shaders/static/vertexShader.glsl | 13 ++ src/desertcookie/freed/core/Display.java | 3 +- src/desertcookie/freed/core/Game.java | 18 +-- .../freed/core/MasterRenderer.java | 19 ++- .../freed/core/ResourceLoader.java | 4 +- src/desertcookie/freed/core/Settings.java | 4 +- src/desertcookie/freed/model/Model.java | 61 +++++++++ .../freed/model/ModelCreator.java | 106 ++++++++++++++++ .../freed/model/ModelRenderer.java | 62 ++++++++++ src/desertcookie/freed/scenes/GameScene.java | 2 +- .../freed/scenes/GameSceneHandler.java | 50 ++++---- src/desertcookie/freed/shaders/Shader.java | 116 ++++++++++++++++++ .../freed/shaders/StaticShader.java | 47 +++++++ test/TestGame.java | 10 +- test/TestScene.java | 39 +++--- 16 files changed, 495 insertions(+), 70 deletions(-) create mode 100644 res/shaders/static/fragmentShader.glsl create mode 100644 res/shaders/static/vertexShader.glsl create mode 100644 src/desertcookie/freed/model/Model.java create mode 100644 src/desertcookie/freed/model/ModelCreator.java create mode 100644 src/desertcookie/freed/model/ModelRenderer.java create mode 100644 src/desertcookie/freed/shaders/Shader.java create mode 100644 src/desertcookie/freed/shaders/StaticShader.java diff --git a/res/shaders/static/fragmentShader.glsl b/res/shaders/static/fragmentShader.glsl new file mode 100644 index 0000000..97ba280 --- /dev/null +++ b/res/shaders/static/fragmentShader.glsl @@ -0,0 +1,11 @@ +#version 400 core + +in vec4 passedColor; + +out vec4 outColor; + +void main(void) { + + outColor = passedColor; + +} \ No newline at end of file diff --git a/res/shaders/static/vertexShader.glsl b/res/shaders/static/vertexShader.glsl new file mode 100644 index 0000000..8a214f9 --- /dev/null +++ b/res/shaders/static/vertexShader.glsl @@ -0,0 +1,13 @@ +#version 400 core + +in vec3 position; +in vec4 color; + +out vec4 passedColor; + +void main(void) { + + gl_Position = vec4(position,1.0); + passedColor = color; + +} \ No newline at end of file diff --git a/src/desertcookie/freed/core/Display.java b/src/desertcookie/freed/core/Display.java index c2b2a3d..ea79dd5 100644 --- a/src/desertcookie/freed/core/Display.java +++ b/src/desertcookie/freed/core/Display.java @@ -201,7 +201,8 @@ public void createDisplay( long sharedWindow ) { GL.createCapabilities( ); GL11.glClearColor( 1f,1f,1f,1f ); - + GL11.glEnable( GL11.GL_BLEND ); + GL11.glBlendFunc( GL11.GL_SRC_ALPHA,GL11.GL_ONE_MINUS_SRC_ALPHA ); } public void updateDisplay( ) { diff --git a/src/desertcookie/freed/core/Game.java b/src/desertcookie/freed/core/Game.java index d2a4a1b..8ccebc8 100644 --- a/src/desertcookie/freed/core/Game.java +++ b/src/desertcookie/freed/core/Game.java @@ -66,21 +66,21 @@ public static void stopGame( ) { public void start( ) { display.createDisplay( 0 ); inputHandler.registerInputHandler( display.getWindowId( ) ); - resourceLoader = new ResourceLoader(); - gameSceneHandler.initializeRegisteredScenes(resourceLoader); - Thread updateThread = new Thread( updateLoop() ); + resourceLoader = new ResourceLoader( ); + gameSceneHandler.initializeRegisteredScenes( resourceLoader ); + Thread updateThread = new Thread( updateLoop( ) ); updateThread.setDaemon( true ); run = true; - updateThread.start(); + updateThread.start( ); display.setVisible( true ); - renderLoop(); + renderLoop( ); } public void exit( ) { gameSceneHandler.exit( ); - resourceLoader.exit(); - display.exit(); + resourceLoader.exit( ); + display.exit( ); } @@ -114,9 +114,9 @@ private void renderLoop( ) { gameSceneHandler.renderActiveScene( masterRenderer ); display.updateDisplay( ); - run = !display.isClosed(); + run = !display.isClosed( ); } - exit(); + exit( ); } private void sleep( long time ) { diff --git a/src/desertcookie/freed/core/MasterRenderer.java b/src/desertcookie/freed/core/MasterRenderer.java index d69a4c0..07e8dc1 100644 --- a/src/desertcookie/freed/core/MasterRenderer.java +++ b/src/desertcookie/freed/core/MasterRenderer.java @@ -25,11 +25,28 @@ package desertcookie.freed.core; +import desertcookie.freed.model.Model; +import desertcookie.freed.model.ModelRenderer; +import desertcookie.freed.shaders.StaticShader; + + public class MasterRenderer { - public MasterRenderer() { + private StaticShader staticShader; + private ModelRenderer modelRenderer; + + + public MasterRenderer( ) { + staticShader = new StaticShader( ); + modelRenderer = new ModelRenderer( ); + } + + public void renderModel( Model model ) { + staticShader.startShader( ); + modelRenderer.render( model ); + staticShader.stopShader( ); } diff --git a/src/desertcookie/freed/core/ResourceLoader.java b/src/desertcookie/freed/core/ResourceLoader.java index e7db51f..6df1b6f 100644 --- a/src/desertcookie/freed/core/ResourceLoader.java +++ b/src/desertcookie/freed/core/ResourceLoader.java @@ -28,7 +28,7 @@ public class ResourceLoader { - public ResourceLoader() { + public ResourceLoader( ) { } @@ -36,7 +36,7 @@ public ResourceLoader() { ///////////////////////// INTERNAL METHODS ///////////////////////// - public void exit() { + public void exit( ) { } diff --git a/src/desertcookie/freed/core/Settings.java b/src/desertcookie/freed/core/Settings.java index 1044351..8bc1042 100644 --- a/src/desertcookie/freed/core/Settings.java +++ b/src/desertcookie/freed/core/Settings.java @@ -28,15 +28,13 @@ public class Settings { - static double maxTPS; private Settings( ) { } - - public static void setMaxTPS(int maxTPS) { + public static void setMaxTPS( int maxTPS ) { Settings.maxTPS = maxTPS; } diff --git a/src/desertcookie/freed/model/Model.java b/src/desertcookie/freed/model/Model.java new file mode 100644 index 0000000..6387adc --- /dev/null +++ b/src/desertcookie/freed/model/Model.java @@ -0,0 +1,61 @@ +/* Copyright (c) 2017, Ruben Hahn + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR + * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +package desertcookie.freed.model; + + +import org.lwjgl.opengl.GL30; + + +public class Model { + + + private int vaoId; + private int vertexCount; + + + Model( int vaoId,int vertexCount ) { + this.vaoId = vaoId; + this.vertexCount = vertexCount; + } + + + public int getVaoId( ) { + return vaoId; + } + + public int getVertexCount( ) { + return vertexCount; + } + + public void bind( ) { + GL30.glBindVertexArray( vaoId ); + } + + public void unbind( ) { + GL30.glBindVertexArray( vaoId ); + } + + +} diff --git a/src/desertcookie/freed/model/ModelCreator.java b/src/desertcookie/freed/model/ModelCreator.java new file mode 100644 index 0000000..eaef2ed --- /dev/null +++ b/src/desertcookie/freed/model/ModelCreator.java @@ -0,0 +1,106 @@ +/* Copyright (c) 2017, Ruben Hahn + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR + * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +package desertcookie.freed.model; + + +import org.lwjgl.BufferUtils; +import org.lwjgl.opengl.GL11; +import org.lwjgl.opengl.GL15; +import org.lwjgl.opengl.GL20; +import org.lwjgl.opengl.GL30; + +import java.nio.FloatBuffer; +import java.nio.IntBuffer; +import java.util.ArrayList; + + +public class ModelCreator { + + + private static ArrayList vaoIdList = new ArrayList<>( ); + private static ArrayList vboIdList = new ArrayList<>( ); + + + public ModelCreator( ) { } + + + public Model createModel( float[] vertices,int[] indices,float[] colors ) { + int vaoId = createVao( ); + GL30.glBindVertexArray( vaoId ); + + bindIndices( indices ); + storeDataInAttributeList( 0,3,vertices ); // vertex positions + storeDataInAttributeList( 1,4,colors ); // per vertex color data + + GL30.glBindVertexArray( 0 ); + return new Model( vaoId,indices.length ); + } + + + private void bindIndices( int[] indices ) { + int vboId = createVbo( ); + GL15.glBindBuffer( GL15.GL_ELEMENT_ARRAY_BUFFER,vboId ); + + IntBuffer buffer = BufferUtils.createIntBuffer( indices.length ); + buffer.put( indices ); + buffer.flip( ); + GL15.glBufferData( GL15.GL_ELEMENT_ARRAY_BUFFER,buffer,GL15.GL_STATIC_DRAW ); + } + + private void storeDataInAttributeList( int attributeNr,int size,float[] data ) { + int vboId = createVbo( ); + GL15.glBindBuffer( GL15.GL_ARRAY_BUFFER,vboId ); + + FloatBuffer buffer = BufferUtils.createFloatBuffer( data.length ); + buffer.put( data ); + buffer.flip( ); + GL15.glBufferData( GL15.GL_ARRAY_BUFFER,buffer,GL15.GL_STATIC_DRAW ); + GL20.glVertexAttribPointer( attributeNr,size,GL11.GL_FLOAT,false,0,0 ); + + GL15.glBindBuffer( GL15.GL_ARRAY_BUFFER,0 ); + } + + private int createVao( ) { + int vaoId = GL30.glGenVertexArrays( ); + vaoIdList.add( vaoId ); + return vaoId; + } + + private int createVbo( ) { + int vboId = GL15.glGenBuffers( ); + vboIdList.add( vboId ); + return vboId; + } + + + public void exit( ) { + for( Integer id : vaoIdList ) + GL30.glDeleteVertexArrays( id ); + for( Integer id : vboIdList ) + GL15.glDeleteBuffers( id ); + } + + +} diff --git a/src/desertcookie/freed/model/ModelRenderer.java b/src/desertcookie/freed/model/ModelRenderer.java new file mode 100644 index 0000000..902c339 --- /dev/null +++ b/src/desertcookie/freed/model/ModelRenderer.java @@ -0,0 +1,62 @@ +/* Copyright (c) 2017, Ruben Hahn + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR + * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +package desertcookie.freed.model; + + +import org.lwjgl.opengl.GL11; +import org.lwjgl.opengl.GL20; + + +public class ModelRenderer { + + + public ModelRenderer( ) {} + + + public void render( Model model ) { + bindModel( model ); + renderModel( model ); + unbindModel( model ); + } + + + private void bindModel( Model model ) { + model.bind( ); + GL20.glEnableVertexAttribArray( 0 ); // vertex positions + GL20.glEnableVertexAttribArray( 1 ); // per vertex color data + } + + private void renderModel( Model model ) { + GL11.glDrawElements( GL11.GL_TRIANGLES,model.getVertexCount( ),GL11.GL_UNSIGNED_INT,0 ); + } + + private void unbindModel( Model model ) { + GL20.glDisableVertexAttribArray( 0 ); + GL20.glDisableVertexAttribArray( 1 ); + model.unbind( ); + } + + +} diff --git a/src/desertcookie/freed/scenes/GameScene.java b/src/desertcookie/freed/scenes/GameScene.java index 6f1d10a..ff85d99 100644 --- a/src/desertcookie/freed/scenes/GameScene.java +++ b/src/desertcookie/freed/scenes/GameScene.java @@ -41,7 +41,7 @@ public GameScene( Enum sceneId ) { } - public abstract void initialize(ResourceLoader loader); + public abstract void initialize( ResourceLoader loader ); public abstract void update( double deltaTime,InputHandler input ); diff --git a/src/desertcookie/freed/scenes/GameSceneHandler.java b/src/desertcookie/freed/scenes/GameSceneHandler.java index 3881a9c..96d8ff8 100644 --- a/src/desertcookie/freed/scenes/GameSceneHandler.java +++ b/src/desertcookie/freed/scenes/GameSceneHandler.java @@ -39,41 +39,41 @@ public class GameSceneHandler { private GameScene activeScene; - public GameSceneHandler() { - registeredScenes = new HashMap<>( ); + public GameSceneHandler( ) { + registeredScenes = new HashMap<>( ); - GameScene splashScreen = new SplashScreen(); - registeredScenes.put( splashScreen.getSceneId(),splashScreen ); - splashScreen.enter(); + GameScene splashScreen = new SplashScreen( ); + registeredScenes.put( splashScreen.getSceneId( ),splashScreen ); + splashScreen.enter( ); activeScene = splashScreen; } - public void registerScene(GameScene scene) { - Enum sceneId = scene.getSceneId(); - if(registeredScenes.containsKey( sceneId )) + public void registerScene( GameScene scene ) { + Enum sceneId = scene.getSceneId( ); + if( registeredScenes.containsKey( sceneId ) ) throw new IllegalArgumentException( "There is already a scene registered with the ID " + sceneId ); - + registeredScenes.put( sceneId,scene ); } - public void removeScene(Enum sceneId) { - if(!registeredScenes.containsKey( sceneId )) - throw new IllegalArgumentException( "There is no scene registered with the ID "+sceneId ); + public void removeScene( Enum sceneId ) { + if( !registeredScenes.containsKey( sceneId ) ) + throw new IllegalArgumentException( "There is no scene registered with the ID " + sceneId ); registeredScenes.remove( sceneId ); } - public void setScene(Enum sceneId) { - if(!registeredScenes.containsKey( sceneId )) - throw new IllegalArgumentException( "There is no scene registered with the ID "+sceneId ); + public void setScene( Enum sceneId ) { + if( !registeredScenes.containsKey( sceneId ) ) + throw new IllegalArgumentException( "There is no scene registered with the ID " + sceneId ); GameScene oldScene = activeScene; GameScene newScene = registeredScenes.get( sceneId ); activeScene = null; - oldScene.leave(); - newScene.enter(); + oldScene.leave( ); + newScene.enter( ); activeScene = newScene; } @@ -87,23 +87,23 @@ public void initializeRegisteredScenes( ResourceLoader resourceLoader ) { } } - public void updateActiveScene( double deltaTime,InputHandler inputHandler) { - if(activeScene!=null) + public void updateActiveScene( double deltaTime,InputHandler inputHandler ) { + if( activeScene != null ) activeScene.update( deltaTime,inputHandler ); } - public void renderActiveScene( MasterRenderer masterRenderer) { - if(activeScene != null) + public void renderActiveScene( MasterRenderer masterRenderer ) { + if( activeScene != null ) activeScene.render( masterRenderer ); } - public void exit() { - activeScene.leave(); + public void exit( ) { + activeScene.leave( ); activeScene = null; for( GameScene scene : registeredScenes.values( ) ) { - scene.exit(); + scene.exit( ); } - registeredScenes.clear(); + registeredScenes.clear( ); } diff --git a/src/desertcookie/freed/shaders/Shader.java b/src/desertcookie/freed/shaders/Shader.java new file mode 100644 index 0000000..fa16ad9 --- /dev/null +++ b/src/desertcookie/freed/shaders/Shader.java @@ -0,0 +1,116 @@ +/* Copyright (c) 2017, Ruben Hahn + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR + * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +package desertcookie.freed.shaders; + + +import org.lwjgl.opengl.GL11; +import org.lwjgl.opengl.GL20; + +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStream; +import java.io.InputStreamReader; + + +public abstract class Shader { + + + private int programId; + private int vertexShaderId, fragmentShaderId; + + + public Shader( String vertexShaderFile,String fragmentShaderFile ) { + programId = GL20.glCreateProgram( ); + vertexShaderId = loadShader( vertexShaderFile,GL20.GL_VERTEX_SHADER ); + fragmentShaderId = loadShader( fragmentShaderFile,GL20.GL_FRAGMENT_SHADER ); + + GL20.glAttachShader( programId,vertexShaderId ); + GL20.glAttachShader( programId,fragmentShaderId ); + bindAttributes( ); + GL20.glLinkProgram( programId ); + GL20.glValidateProgram( programId ); + } + + + protected abstract void bindAttributes( ); + + + protected void bindAttributeLocation( int attributeNr,String variableName ) { + GL20.glBindAttribLocation( programId,attributeNr,variableName ); + } + + + public void startShader( ) { + GL20.glUseProgram( programId ); + } + + public void stopShader( ) { + GL20.glUseProgram( 0 ); + } + + + private static int loadShader( String file,int type ) { + InputStream in = Shader.class.getResourceAsStream( file ); + StringBuilder string = new StringBuilder( ); + try { + BufferedReader reader = new BufferedReader( new InputStreamReader( in ) ); + String line; + while( ( line = reader.readLine( ) ) != null ) + string.append( line + "//\n" ); + reader.close( ); + } catch( IOException e ) { + if( type == GL20.GL_VERTEX_SHADER ) + System.err.println( "Couldn't load vertex shader file: " + file ); + else + System.err.println( "Couldn't load fragment shader file: " + file ); + e.printStackTrace( ); + } + + int shaderId = GL20.glCreateShader( type ); + GL20.glShaderSource( shaderId,string ); + GL20.glCompileShader( shaderId ); + if( GL20.glGetShaderi( shaderId,GL20.GL_COMPILE_STATUS ) == GL11.GL_FALSE ) { + if( type == GL20.GL_VERTEX_SHADER ) + System.err.println( "Failed to compile vertex shader: " + file ); + else + System.err.println( "Failed to compile fragment shader: " + file ); + System.out.println( GL20.glGetShaderInfoLog( shaderId,250 ) ); + System.exit( -1 ); + } + return shaderId; + } + + + public void exit( ) { + stopShader( ); + GL20.glDetachShader( programId,vertexShaderId ); + GL20.glDetachShader( programId,fragmentShaderId ); + GL20.glDeleteShader( vertexShaderId ); + GL20.glDeleteShader( fragmentShaderId ); + GL20.glDeleteProgram( programId ); + } + + +} diff --git a/src/desertcookie/freed/shaders/StaticShader.java b/src/desertcookie/freed/shaders/StaticShader.java new file mode 100644 index 0000000..9976cfe --- /dev/null +++ b/src/desertcookie/freed/shaders/StaticShader.java @@ -0,0 +1,47 @@ +/* Copyright (c) 2017, Ruben Hahn + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR + * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +package desertcookie.freed.shaders; + + +public class StaticShader extends Shader { + + + private static final String VERTEX_SHADER_FILE = "/shaders/static/vertexShader.glsl"; + private static final String FRAGMENT_SHADER_FILE = "/shaders/static/fragmentShader.glsl"; + + + public StaticShader( ) { + super( VERTEX_SHADER_FILE,FRAGMENT_SHADER_FILE ); + } + + + @Override + protected void bindAttributes( ) { + super.bindAttributeLocation( 0,"position" ); // vertex position + super.bindAttributeLocation( 1,"color" ); // per vertex color data + } + + +} diff --git a/test/TestGame.java b/test/TestGame.java index d2d23b6..d338ccd 100644 --- a/test/TestGame.java +++ b/test/TestGame.java @@ -33,17 +33,17 @@ public class TestGame extends Game { public static void main( String[] args ) { - Game testGame = new TestGame(); + Game testGame = new TestGame( ); - Display display = TestGame.getDisplay(); + Display display = TestGame.getDisplay( ); display.setTitle( "Test Game" ); display.setSize( 1280,720 ); - GameSceneHandler sceneHandler = TestGame.getGameSceneHandler(); - sceneHandler.registerScene( new TestScene( ) ); + GameSceneHandler sceneHandler = TestGame.getGameSceneHandler( ); + sceneHandler.registerScene( new TestScene( ) ); sceneHandler.setScene( DefaultSceneIDs.Main_Menu ); - testGame.start(); + testGame.start( ); } diff --git a/test/TestScene.java b/test/TestScene.java index 551d264..787350a 100644 --- a/test/TestScene.java +++ b/test/TestScene.java @@ -26,19 +26,15 @@ import desertcookie.freed.core.MasterRenderer; import desertcookie.freed.core.ResourceLoader; import desertcookie.freed.input.InputHandler; -import desertcookie.freed.models.Model; +import desertcookie.freed.model.Model; +import desertcookie.freed.model.ModelCreator; import desertcookie.freed.scenes.DefaultSceneIDs; import desertcookie.freed.scenes.GameScene; -import desertcookie.freed.shaders.ShaderProgram; -import desertcookie.freed.shaders.StaticShader; -import org.lwjgl.opengl.GL; -import org.lwjgl.opengl.GL11; public class TestScene extends GameScene { - private StaticShader shader; private Model model; @@ -49,26 +45,25 @@ public TestScene( ) { @Override public void initialize( ResourceLoader loader ) { - GL11.glEnable(GL11.GL_BLEND); - GL11.glBlendFunc(GL11.GL_SRC_ALPHA, GL11.GL_ONE_MINUS_SRC_ALPHA); - shader = new StaticShader(); - float[] positions = new float[]{ - -0.5f, 0.5f, 0f, - -0.5f, -0.5f, 0f, - 0.5f, -0.5f, 0f, - 0.5f, 0.5f, 0f + ModelCreator modelCreator = new ModelCreator( ); + + float[] vertices = new float[] { + -0.5f,0.5f,0f, + -0.5f,-0.5f,0f, + 0.5f,-0.5f,0f, + 0.5f,0.5f,0f }; int[] indices = new int[] { - 0, 1, 3, - 3, 1, 2 + 0,1,3, + 3,1,2 }; float[] colors = new float[] { - 1, 0, 0, 1, - 1, 0, 0, 0.1f, - 0, 1, 0, 0.1f, - 0, 1, 0, 1 + 1,0,0,1, + 1,0,0,0.1f, + 0,1,0,0.1f, + 0,1,0,1 }; - model = loader.createModel( positions,indices,colors ); + model = modelCreator.createModel( vertices,indices,colors ); } @Override @@ -78,9 +73,7 @@ public void update( double deltaTime,InputHandler input ) { @Override public void render( MasterRenderer renderer ) { - shader.start(); renderer.renderModel( model ); - shader.stop(); }