Skip to content

Commit

Permalink
updated to FreeD v0.0.23
Browse files Browse the repository at this point in the history
added classes Model, ModelCreator, ModelRenderer, Shader, StaticShader
and files static/fragmentShader.glsl, static/vertexShader.glsl
  • Loading branch information
DesertCookie committed Feb 4, 2018
1 parent 5763f1a commit 8af4418
Show file tree
Hide file tree
Showing 16 changed files with 495 additions and 70 deletions.
11 changes: 11 additions & 0 deletions res/shaders/static/fragmentShader.glsl
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#version 400 core

in vec4 passedColor;

out vec4 outColor;

void main(void) {

outColor = passedColor;

}
13 changes: 13 additions & 0 deletions res/shaders/static/vertexShader.glsl
Original file line number Diff line number Diff line change
@@ -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;

}
3 changes: 2 additions & 1 deletion src/desertcookie/freed/core/Display.java
Original file line number Diff line number Diff line change
Expand Up @@ -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( ) {
Expand Down
18 changes: 9 additions & 9 deletions src/desertcookie/freed/core/Game.java
Original file line number Diff line number Diff line change
Expand Up @@ -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( );
}


Expand Down Expand Up @@ -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 ) {
Expand Down
19 changes: 18 additions & 1 deletion src/desertcookie/freed/core/MasterRenderer.java
Original file line number Diff line number Diff line change
Expand Up @@ -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( );
}


Expand Down
4 changes: 2 additions & 2 deletions src/desertcookie/freed/core/ResourceLoader.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,15 +28,15 @@
public class ResourceLoader {


public ResourceLoader() {
public ResourceLoader( ) {

}


///////////////////////// INTERNAL METHODS /////////////////////////


public void exit() {
public void exit( ) {

}

Expand Down
4 changes: 1 addition & 3 deletions src/desertcookie/freed/core/Settings.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
61 changes: 61 additions & 0 deletions src/desertcookie/freed/model/Model.java
Original file line number Diff line number Diff line change
@@ -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 );
}


}
106 changes: 106 additions & 0 deletions src/desertcookie/freed/model/ModelCreator.java
Original file line number Diff line number Diff line change
@@ -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<Integer> vaoIdList = new ArrayList<>( );
private static ArrayList<Integer> 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 );
}


}
62 changes: 62 additions & 0 deletions src/desertcookie/freed/model/ModelRenderer.java
Original file line number Diff line number Diff line change
@@ -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( );
}


}
2 changes: 1 addition & 1 deletion src/desertcookie/freed/scenes/GameScene.java
Original file line number Diff line number Diff line change
Expand Up @@ -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 );

Expand Down
Loading

0 comments on commit 8af4418

Please sign in to comment.