Started using custom shaders with model batch.

This commit is contained in:
2014-05-23 17:00:14 -04:30
parent 9a67df8f8a
commit 169d0e7c43
9 changed files with 232 additions and 36 deletions

View File

@@ -0,0 +1,31 @@
/*
* Copyright (C) 2014 Miguel Angel Astor Romero
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package ve.ucv.ciens.ccg.nxtar.components;
import ve.ucv.ciens.ccg.nxtar.graphics.shaders.CustomShaderBase;
import com.artemis.Component;
public class CustomShaderComponent extends Component {
public CustomShaderBase shader;
public CustomShaderComponent(CustomShaderBase shader) throws IllegalArgumentException{
if(shader == null)
throw new IllegalArgumentException("Shader cannot be null.");
this.shader = shader;
}
}

View File

@@ -15,16 +15,15 @@
*/
package ve.ucv.ciens.ccg.nxtar.components;
import ve.ucv.ciens.ccg.nxtar.graphics.shaders.CustomShaderBase;
import com.artemis.Component;
import com.badlogic.gdx.graphics.g3d.Shader;
public class ShaderComponent extends Component {
public CustomShaderBase shader;
public class ShaderComponent extends Component{
public Shader shader;
public ShaderComponent(CustomShaderBase shader) throws IllegalArgumentException{
public ShaderComponent(Shader shader) throws IllegalArgumentException{
if(shader == null)
throw new IllegalArgumentException("Shader cannot be null.");
throw new IllegalArgumentException("Shader is null.");
this.shader = shader;
}

View File

@@ -15,14 +15,16 @@
*/
package ve.ucv.ciens.ccg.nxtar.entities;
import ve.ucv.ciens.ccg.nxtar.components.CustomShaderComponent;
import ve.ucv.ciens.ccg.nxtar.components.EnvironmentComponent;
import ve.ucv.ciens.ccg.nxtar.components.ModelComponent;
import ve.ucv.ciens.ccg.nxtar.components.ShaderComponent;
import ve.ucv.ciens.ccg.nxtar.components.GeometryComponent;
import ve.ucv.ciens.ccg.nxtar.components.MarkerCodeComponent;
import ve.ucv.ciens.ccg.nxtar.components.MeshComponent;
import ve.ucv.ciens.ccg.nxtar.components.ModelComponent;
import ve.ucv.ciens.ccg.nxtar.components.ShaderComponent;
import ve.ucv.ciens.ccg.nxtar.exceptions.ShaderFailedToLoadException;
import ve.ucv.ciens.ccg.nxtar.graphics.shaders.CustomShaderBase;
import ve.ucv.ciens.ccg.nxtar.graphics.shaders.SingleLightPerPixelShader;
import ve.ucv.ciens.ccg.nxtar.graphics.shaders.SingleLightPhongShader;
import com.artemis.Entity;
@@ -36,7 +38,6 @@ import com.badlogic.gdx.graphics.VertexAttributes.Usage;
import com.badlogic.gdx.graphics.g3d.Environment;
import com.badlogic.gdx.graphics.g3d.Model;
import com.badlogic.gdx.graphics.g3d.attributes.ColorAttribute;
import com.badlogic.gdx.graphics.g3d.attributes.DepthTestAttribute;
import com.badlogic.gdx.graphics.g3d.environment.DirectionalLight;
import com.badlogic.gdx.graphics.g3d.loader.G3dModelLoader;
import com.badlogic.gdx.graphics.g3d.utils.MeshBuilder;
@@ -52,6 +53,7 @@ public class MarkerTestEntityCreator extends EntityCreatorBase {
private Mesh boxMesh;
private Model bombModel;
private CustomShaderBase phongShader;
private SingleLightPerPixelShader ppShader;
private Mesh bombMesh;
@Override
@@ -90,8 +92,10 @@ public class MarkerTestEntityCreator extends EntityCreatorBase {
Gdx.app.exit();
}
ppShader = new SingleLightPerPixelShader();
ppShader.init();
environment = new Environment();
environment.set(new DepthTestAttribute(GL20.GL_LEQUAL, true));
environment.set(new ColorAttribute(ColorAttribute.AmbientLight, 0.3f, 0.3f, 0.3f, 1.0f));
environment.add(new DirectionalLight().set(new Color(1, 1, 1, 1), new Vector3(-2, -2, -2)));
@@ -100,7 +104,7 @@ public class MarkerTestEntityCreator extends EntityCreatorBase {
bomb = world.createEntity();
bomb.addComponent(new GeometryComponent(new Vector3(0.0f, 0.0f, 0.0f), new Matrix3().idt(), new Vector3(1.0f, 1.0f, 1.0f)));
bomb.addComponent(new MeshComponent(bombMesh));
bomb.addComponent(new ShaderComponent(phongShader));
bomb.addComponent(new CustomShaderComponent(phongShader));
bomb.addComponent(new MarkerCodeComponent(1023));
bombModelBatch = world.createEntity();
@@ -108,17 +112,18 @@ public class MarkerTestEntityCreator extends EntityCreatorBase {
bombModelBatch.addComponent(new ModelComponent(bombModel));
bombModelBatch.addComponent(new EnvironmentComponent(environment));
bombModelBatch.addComponent(new MarkerCodeComponent(89));
bombModelBatch.addComponent(new ShaderComponent(ppShader));
sphere = world.createEntity();
sphere.addComponent(new GeometryComponent(new Vector3(0.0f, 0.0f, 0.0f), new Matrix3().idt(), new Vector3(1.0f, 1.0f, 1.0f)));
sphere.addComponent(new MeshComponent(sphereMesh));
sphere.addComponent(new ShaderComponent(phongShader));
sphere.addComponent(new CustomShaderComponent(phongShader));
sphere.addComponent(new MarkerCodeComponent(10));
box = world.createEntity();
box.addComponent(new GeometryComponent(new Vector3(-1.0f, 0.0f, 0.0f), new Matrix3().idt(), new Vector3(1.0f, 1.0f, 1.0f)));
box.addComponent(new MeshComponent(boxMesh));
box.addComponent(new ShaderComponent(phongShader));
box.addComponent(new CustomShaderComponent(phongShader));
// Add the entities to the world.
Gdx.app.log(TAG, CLASS_NAME + ".createAllEntities(): Adding entities to the world.");
@@ -142,5 +147,8 @@ public class MarkerTestEntityCreator extends EntityCreatorBase {
if(bombModel != null)
bombModel.dispose();
if(ppShader != null)
ppShader.dispose();
}
}

View File

@@ -17,7 +17,7 @@ package ve.ucv.ciens.ccg.nxtar.entities;
import ve.ucv.ciens.ccg.nxtar.components.GeometryComponent;
import ve.ucv.ciens.ccg.nxtar.components.MeshComponent;
import ve.ucv.ciens.ccg.nxtar.components.ShaderComponent;
import ve.ucv.ciens.ccg.nxtar.components.CustomShaderComponent;
import ve.ucv.ciens.ccg.nxtar.exceptions.ShaderFailedToLoadException;
import ve.ucv.ciens.ccg.nxtar.graphics.shaders.CustomShaderBase;
import ve.ucv.ciens.ccg.nxtar.graphics.shaders.SingleLightPhongShader;
@@ -89,22 +89,22 @@ public class TestGameEntityCreator extends EntityCreatorBase {
sphere = world.createEntity();
sphere.addComponent(new GeometryComponent(new Vector3(0.5f, 0.5f, 0.0f), identity, new Vector3(1.0f, 1.0f, 1.0f)));
sphere.addComponent(new MeshComponent(sphereMesh));
sphere.addComponent(new ShaderComponent(singleLightPhongShader));
sphere.addComponent(new CustomShaderComponent(singleLightPhongShader));
cube = world.createEntity();
cube.addComponent(new GeometryComponent(new Vector3(-0.5f, -0.5f, 0.0f), identity, new Vector3(1.0f, 1.0f, 1.0f)));
cube.addComponent(new MeshComponent(cubeMesh));
cube.addComponent(new ShaderComponent(singleLightPhongShader));
cube.addComponent(new CustomShaderComponent(singleLightPhongShader));
capsule1 = world.createEntity();
capsule1.addComponent(new GeometryComponent(new Vector3(-0.5f, 0.5f, 0.0f), identity, new Vector3(1.5f, 1.0f, 1.0f)));
capsule1.addComponent(new MeshComponent(capsuleMesh));
capsule1.addComponent(new ShaderComponent(singleLightPhongShader));
capsule1.addComponent(new CustomShaderComponent(singleLightPhongShader));
capsule2 = world.createEntity();
capsule2.addComponent(new GeometryComponent(new Vector3(0.5f, -0.5f, 0.0f), identity, new Vector3(1.0f, 1.5f, 1.0f)));
capsule2.addComponent(new MeshComponent(capsuleMesh));
capsule2.addComponent(new ShaderComponent(singleLightPhongShader));
capsule2.addComponent(new CustomShaderComponent(singleLightPhongShader));
// Add the entities to the world.
Gdx.app.log(TAG, CLASS_NAME + ".createAllEntities(): Adding entities to the world.");

View File

@@ -0,0 +1,156 @@
/*
* Copyright (C) 2014 Miguel Angel Astor Romero
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package ve.ucv.ciens.ccg.nxtar.graphics.shaders;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.Camera;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.g3d.Renderable;
import com.badlogic.gdx.graphics.g3d.Shader;
import com.badlogic.gdx.graphics.g3d.attributes.ColorAttribute;
import com.badlogic.gdx.graphics.g3d.attributes.FloatAttribute;
import com.badlogic.gdx.graphics.g3d.utils.RenderContext;
import com.badlogic.gdx.graphics.glutils.ShaderProgram;
import com.badlogic.gdx.math.Vector3;
import com.badlogic.gdx.utils.GdxRuntimeException;
public class SingleLightPerPixelShader implements Shader{
private static final String TAG = "SINGLE_LIGHT_PER_PIXEL_SHADER";
private static final String CLASS_NAME = SingleLightPerPixelShader.class.getSimpleName();
private static final String VERTEX_SHADER_PATH = "shaders/directionalPerPixelSingleLight/directionalPerPixel_vert.glsl";
private static final String FRAGMENT_SHADER_PATH = "shaders/directionalPerPixelSingleLight/directionalPerPixel_frag.glsl";
private ShaderProgram program;
private Camera camera;
private RenderContext context;
// Uniform locations.
private int u_geomTrans;
private int u_projTrans;
private int u_lightPos;
private int u_lightDiffuse;
private int u_specular;
private int u_ambient;
private int u_shiny;
private int u_cameraPos;
private int u_materialDiffuse;
public SingleLightPerPixelShader(){
program = null;
camera = null;
context = null;
}
@Override
public void init() throws GdxRuntimeException{
// Compile the shader.
program = new ShaderProgram(Gdx.files.internal(VERTEX_SHADER_PATH), Gdx.files.internal(FRAGMENT_SHADER_PATH));
if(!program.isCompiled()){
Gdx.app.log(TAG, CLASS_NAME + ".init(): Shader failed to compile.");
throw new GdxRuntimeException(program.getLog());
}
// Cache uniform locations.
u_projTrans = program.getUniformLocation("u_projTrans");
u_geomTrans = program.getUniformLocation("u_geomTrans");
u_lightPos = program.getUniformLocation("u_lightPos");
u_lightDiffuse = program.getUniformLocation("u_lightDiffuse");
u_specular = program.getUniformLocation("u_specular");
u_ambient = program.getUniformLocation("u_ambient");
u_shiny = program.getUniformLocation("u_shiny");
u_cameraPos = program.getUniformLocation("u_cameraPos");
u_materialDiffuse = program.getUniformLocation("u_materialDiffuse");
}
@Override
public void dispose(){
if(program != null)
program.dispose();
}
@Override
public int compareTo(Shader other){
return 0;
}
@Override
public boolean canRender(Renderable renderable){
// Check for all needed lighting and material attributes.
if(renderable.environment.directionalLights.size < 1)
return false;
if(!renderable.environment.has(ColorAttribute.AmbientLight))
return false;
if(!renderable.material.has(ColorAttribute.Diffuse))
return false;
if(!renderable.material.has(ColorAttribute.Specular))
return false;
if(!renderable.material.has(FloatAttribute.Shininess))
return false;
return true;
}
@Override
public void begin(Camera camera, RenderContext context) throws GdxRuntimeException{
if(this.camera != null || this.context != null)
throw new GdxRuntimeException("Called begin twice before calling end.");
this.camera = camera;
this.context = context;
program.begin();
// Set camera dependant uniforms.
program.setUniformMatrix(u_projTrans, this.camera.combined);
program.setUniformf(u_cameraPos, this.camera.position);
// Set render context.
this.context.setDepthTest(GL20.GL_LEQUAL);
this.context.setDepthMask(true);
}
@Override
public void render(Renderable renderable){
// Get material colors.
Vector3 lightPosition = renderable.environment.directionalLights.get(0).direction;
Color diffuseLightColor = renderable.environment.directionalLights.get(0).color;
Color diffuseColor = ((ColorAttribute)renderable.material.get(ColorAttribute.Diffuse)).color;
Color specularColor = ((ColorAttribute)renderable.material.get(ColorAttribute.Specular)).color;
Color ambientColor = ((ColorAttribute)renderable.environment.get(ColorAttribute.AmbientLight)).color;
float shininess = ((FloatAttribute)renderable.material.get(FloatAttribute.Shininess)).value;
// Set model dependant uniforms.
program.setUniformMatrix(u_geomTrans, renderable.worldTransform);
program.setUniformf(u_lightPos, lightPosition);
program.setUniformf(u_lightDiffuse, diffuseLightColor);
program.setUniformf(u_materialDiffuse, diffuseColor);
program.setUniformf(u_specular, specularColor);
program.setUniformf(u_ambient, ambientColor);
program.setUniformf(u_shiny, shininess);
// Render.
renderable.mesh.render(program, renderable.primitiveType, renderable.meshPartOffset, renderable.meshPartSize);
}
@Override
public void end(){
program.end();
this.camera = null;
this.context = null;
}
}

View File

@@ -270,9 +270,9 @@ public class InGameState extends BaseState{
// Set the 3D frame buffer for rendering.
frameBuffer.begin();{
// Set OpenGL state.
Gdx.gl.glEnable(GL20.GL_DEPTH_TEST);
Gdx.gl.glClearColor(0, 0, 0, 0);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT | GL20.GL_DEPTH_BUFFER_BIT);
Gdx.gl.glDisable(GL20.GL_TEXTURE_2D);
// Build the projection matrix.
focalPointX = core.cvProc.getFocalPointX();
@@ -314,8 +314,6 @@ public class InGameState extends BaseState{
gameWorld.getSystem(ModelBatchMarkerRenderingSystem.class).end();
gameWorld.getSystem(ObjectRenderingSystem.class).process();
Gdx.gl.glDisable(GL20.GL_DEPTH_TEST);
}frameBuffer.end();
// Set the frame buffer object texture to a renderable sprite.

View File

@@ -15,7 +15,7 @@
*/
package ve.ucv.ciens.ccg.nxtar.systems;
import ve.ucv.ciens.ccg.nxtar.components.ShaderComponent;
import ve.ucv.ciens.ccg.nxtar.components.CustomShaderComponent;
import ve.ucv.ciens.ccg.nxtar.components.GeometryComponent;
import ve.ucv.ciens.ccg.nxtar.components.MarkerCodeComponent;
import ve.ucv.ciens.ccg.nxtar.components.MeshComponent;
@@ -35,7 +35,7 @@ import com.badlogic.gdx.math.Matrix4;
public class MarkerRenderingSystem extends EntityProcessingSystem {
@Mapper ComponentMapper<MarkerCodeComponent> markerMapper;
@Mapper ComponentMapper<GeometryComponent> geometryMapper;
@Mapper ComponentMapper<ShaderComponent> shaderMapper;
@Mapper ComponentMapper<CustomShaderComponent> shaderMapper;
@Mapper ComponentMapper<MeshComponent> meshMapper;
private static final String TAG = "MARKER_RENDERING_SYSTEM";
@@ -65,7 +65,7 @@ public class MarkerRenderingSystem extends EntityProcessingSystem {
@SuppressWarnings("unchecked")
public MarkerRenderingSystem(){
super(Aspect.getAspectForAll(MarkerCodeComponent.class, GeometryComponent.class, ShaderComponent.class, MeshComponent.class));
super(Aspect.getAspectForAll(MarkerCodeComponent.class, GeometryComponent.class, CustomShaderComponent.class, MeshComponent.class));
markers = null;
translationMatrix = new Matrix4().setToTranslation(0.0f, 0.0f, 0.0f);
@@ -82,7 +82,7 @@ public class MarkerRenderingSystem extends EntityProcessingSystem {
protected void process(Entity e) {
MarkerCodeComponent marker;
GeometryComponent geometry;
ShaderComponent shaderComp;
CustomShaderComponent shaderComp;
MeshComponent meshComp;
if(markers == null)

View File

@@ -19,6 +19,7 @@ import ve.ucv.ciens.ccg.nxtar.components.EnvironmentComponent;
import ve.ucv.ciens.ccg.nxtar.components.GeometryComponent;
import ve.ucv.ciens.ccg.nxtar.components.MarkerCodeComponent;
import ve.ucv.ciens.ccg.nxtar.components.ModelComponent;
import ve.ucv.ciens.ccg.nxtar.components.ShaderComponent;
import ve.ucv.ciens.ccg.nxtar.interfaces.ImageProcessor.MarkerData;
import ve.ucv.ciens.ccg.nxtar.utils.ProjectConstants;
@@ -37,6 +38,7 @@ public class ModelBatchMarkerRenderingSystem extends EntityProcessingSystem {
@Mapper ComponentMapper<GeometryComponent> geometryMapper;
@Mapper ComponentMapper<ModelComponent> modelMapper;
@Mapper ComponentMapper<EnvironmentComponent> environmentMapper;
@Mapper ComponentMapper<ShaderComponent> shaderMapper;
private static final String TAG = "MODEL_BATCH_MARKER_RENDERING_SYSTEM";
private static final String CLASS_NAME = ModelBatchMarkerRenderingSystem.class.getSimpleName();
@@ -64,7 +66,7 @@ public class ModelBatchMarkerRenderingSystem extends EntityProcessingSystem {
@SuppressWarnings("unchecked")
public ModelBatchMarkerRenderingSystem(){
super(Aspect.getAspectForAll(MarkerCodeComponent.class, GeometryComponent.class, EnvironmentComponent.class, ModelComponent.class));
super(Aspect.getAspectForAll(MarkerCodeComponent.class, GeometryComponent.class, ShaderComponent.class, EnvironmentComponent.class, ModelComponent.class));
markers = null;
camera = null;
@@ -102,6 +104,7 @@ public class ModelBatchMarkerRenderingSystem extends EntityProcessingSystem {
GeometryComponent geometry;
EnvironmentComponent environment;
ModelComponent model;
ShaderComponent shader;
if(markers == null || camera == null)
return;
@@ -111,6 +114,7 @@ public class ModelBatchMarkerRenderingSystem extends EntityProcessingSystem {
geometry = geometryMapper.get(e);
model = modelMapper.get(e);
environment = environmentMapper.get(e);
shader = shaderMapper.get(e);
Gdx.app.log(TAG, CLASS_NAME + ".process(): Processing markers.");
for(int i = 0; i < ProjectConstants.MAXIMUM_NUMBER_OF_MARKERS; i++){
@@ -147,7 +151,7 @@ public class ModelBatchMarkerRenderingSystem extends EntityProcessingSystem {
model.instance.calculateTransforms();
// Render the marker;
batch.render(model.instance, environment.environment);
batch.render(model.instance, environment.environment, shader.shader);
}
}else{
Gdx.app.log(TAG, CLASS_NAME + ".process(): Skipping marker number " + Integer.toString(i) + ".");

View File

@@ -18,7 +18,7 @@ package ve.ucv.ciens.ccg.nxtar.systems;
import ve.ucv.ciens.ccg.nxtar.components.GeometryComponent;
import ve.ucv.ciens.ccg.nxtar.components.MarkerCodeComponent;
import ve.ucv.ciens.ccg.nxtar.components.MeshComponent;
import ve.ucv.ciens.ccg.nxtar.components.ShaderComponent;
import ve.ucv.ciens.ccg.nxtar.components.CustomShaderComponent;
import ve.ucv.ciens.ccg.nxtar.graphics.RenderParameters;
import com.artemis.Aspect;
@@ -35,7 +35,7 @@ import com.badlogic.gdx.math.Matrix4;
*/
public class ObjectRenderingSystem extends EntityProcessingSystem {
@Mapper ComponentMapper<GeometryComponent> geometryMapper;
@Mapper ComponentMapper<ShaderComponent> shaderMapper;
@Mapper ComponentMapper<CustomShaderComponent> shaderMapper;
@Mapper ComponentMapper<MeshComponent> modelMapper;
/**
@@ -60,7 +60,7 @@ public class ObjectRenderingSystem extends EntityProcessingSystem {
@SuppressWarnings("unchecked")
public ObjectRenderingSystem() {
super(Aspect.getAspectForAll(GeometryComponent.class, ShaderComponent.class, MeshComponent.class).exclude(MarkerCodeComponent.class));
super(Aspect.getAspectForAll(GeometryComponent.class, CustomShaderComponent.class, MeshComponent.class).exclude(MarkerCodeComponent.class));
translationMatrix = new Matrix4().setToTranslation(0.0f, 0.0f, 0.0f);
rotationMatrix = new Matrix4().idt();
@@ -77,13 +77,13 @@ public class ObjectRenderingSystem extends EntityProcessingSystem {
@Override
protected void process(Entity e) {
GeometryComponent geometryComponent;
ShaderComponent shaderComponent;
CustomShaderComponent customShaderComponent;
MeshComponent meshComponent;
// Get the necessary components.
geometryComponent = geometryMapper.get(e);
meshComponent = modelMapper.get(e);
shaderComponent = shaderMapper.get(e);
customShaderComponent = shaderMapper.get(e);
// Calculate the geometric transformation for this entity.
translationMatrix.setToTranslation(geometryComponent.position);
@@ -95,9 +95,9 @@ public class ObjectRenderingSystem extends EntityProcessingSystem {
RenderParameters.setTransformationMatrix(combinedTransformationMatrix);
// Render this entity.
shaderComponent.shader.getShaderProgram().begin();{
shaderComponent.shader.setUniforms();
meshComponent.model.render(shaderComponent.shader.getShaderProgram(), GL20.GL_TRIANGLES);
}shaderComponent.shader.getShaderProgram().end();
customShaderComponent.shader.getShaderProgram().begin();{
customShaderComponent.shader.setUniforms();
meshComponent.model.render(customShaderComponent.shader.getShaderProgram(), GL20.GL_TRIANGLES);
}customShaderComponent.shader.getShaderProgram().end();
}
}