-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
added classes Model, ModelCreator, ModelRenderer, Shader, StaticShader and files static/fragmentShader.glsl, static/vertexShader.glsl
- Loading branch information
1 parent
5763f1a
commit 8af4418
Showing
16 changed files
with
495 additions
and
70 deletions.
There are no files selected for viewing
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
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; | ||
|
||
} |
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
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; | ||
|
||
} |
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
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
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
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
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
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
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 ); | ||
} | ||
|
||
|
||
} |
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
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 ); | ||
} | ||
|
||
|
||
} |
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
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( ); | ||
} | ||
|
||
|
||
} |
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
Oops, something went wrong.