diff --git a/samples/resources/lowPoly/data/basicPalette.frag b/samples/resources/lowPoly/data/basicPalette.frag index a5fe9f5..3cd9d62 100644 --- a/samples/resources/lowPoly/data/basicPalette.frag +++ b/samples/resources/lowPoly/data/basicPalette.frag @@ -5,14 +5,83 @@ precision mediump float; precision mediump int; #endif -in vec2 io_textureCoords; +struct Light { + vec3 color; + vec3 position; + vec3 attenuation; +}; + +const int MAX_LIGHT_NUMBER = 8; + + +in vec2 io_textureCoords; +in vec3 io_surfaceNormal; +in vec3 io_toCameraVector; +in vec3 io_toLightVector[MAX_LIGHT_NUMBER]; +// FOW: Fog Of War result calculation +in float io_fowVisibility; + +// texture properties uniform sampler2D in_textureBase; +// Material +//uniform Material in_material; +// 2 light for suns and other for locals ... +uniform Light in_lights[MAX_LIGHT_NUMBER]; +// global color of the sky ... needed to have a better color for the FOW +//uniform vec3 in_sky_color; +const vec3 in_sky_color = vec3(1.0,1.0,1.0); // output: out vec4 out_Color; void main(void) { - out_Color = texture(in_textureBase, io_textureCoords); - //out_Color = vec4(1,0,0,1); + // disable transparency elements in the texture ... + // Can be set at the start of the shader ... + /* + vec4 textureColour = texture(in_textureBase, io_textureCoords); + if (textureColour.a < 0.5) { + discard; + } + */ + //vec4 textureColour = vec4(1.0,1.0,1.0,1.0); + // keep material: + vec3 tex_ambientFactor = texture(in_textureBase, vec2(io_textureCoords.x, 4.5/8.0)).xyz; + //vec3 tex_diffuseFactor = texture(in_textureBase, vec2(io_textureCoords.x, 0.5/8.0)).xyz; + //vec4 textureColour = texture(in_textureBase, vec2(io_textureCoords.x, 0.5/8.0)); + vec4 textureColour = texture(in_textureBase, io_textureCoords); + vec3 tex_specularFactor = texture(in_textureBase, vec2(io_textureCoords.x, 2.5/8.0)).xyz; + float tex_shininess = texture(in_textureBase, vec2(io_textureCoords.x, 6.5/8.0)).x; + + vec3 unitNormal = normalize(io_surfaceNormal); + vec3 unitVectorToCamera = normalize(io_toCameraVector); + vec3 totalDiffuse = vec3(0.0); + vec3 totalSpecular = vec3(0.0); + for(int iii=0; iii maybe set an uniform for this + //totalDiffuse = max(totalDiffuse, 1.0); + //totalDiffuse = min(totalDiffuse, 0.4); + + //////out_Color = vec4(totalDiffuse,1.0) * textureColour + vec4(totalSpecular, 1.0); + out_Color = (vec4(totalDiffuse,1.0)*0.5+0.5) * textureColour; + /////out_Color = mix(vec4(in_sky_color,1.0), out_Color, io_fowVisibility); } + + + + diff --git a/samples/resources/lowPoly/data/basicPalette.vert b/samples/resources/lowPoly/data/basicPalette.vert index 62bc6b6..a707aa7 100644 --- a/samples/resources/lowPoly/data/basicPalette.vert +++ b/samples/resources/lowPoly/data/basicPalette.vert @@ -5,17 +5,55 @@ precision mediump float; precision mediump int; #endif +struct Light { + vec3 color; + vec3 position; + vec3 attenuation; +}; +const int MAX_LIGHT_NUMBER = 8; + // Input: layout (location = 0) in vec3 in_position; layout (location = 1) in vec2 in_textureCoords; +layout (location = 2) in vec3 in_normal; +// 2 light for suns and other for locals ... +uniform Light in_lights[MAX_LIGHT_NUMBER]; + uniform mat4 in_matrixTransformation; uniform mat4 in_matrixProjection; uniform mat4 in_matrixView; +//uniform float in_numberOfRows; +//uniform vec2 in_offset; +const float in_numberOfRows = 1; +const vec2 in_offset = vec2(0.0,0.0); + +// Configuration of the FOV ==> TODO: Set it in parameter uniform ... +const float c_density = 0.007; +const float c_gradient = 1.5; + // output: out vec2 io_textureCoords; +out vec3 io_surfaceNormal; +out vec3 io_toCameraVector; +out vec3 io_toLightVector[MAX_LIGHT_NUMBER]; +// FOW: Fog Of War result calculation +out float io_fowVisibility; void main(void) { - gl_Position = in_matrixProjection * in_matrixView * in_matrixTransformation * vec4(in_position, 1.0); - io_textureCoords = in_textureCoords; + vec4 worldPosition = in_matrixTransformation * vec4(in_position, 1.0); + vec4 positionRelativeToCam = in_matrixView * worldPosition; + gl_Position = in_matrixProjection * positionRelativeToCam; + io_textureCoords = (in_textureCoords/in_numberOfRows) + in_offset; + + io_surfaceNormal = (in_matrixTransformation * vec4(in_normal, 0.0)).xyz; + for(int iii=0;iii signalPosition = new Signal(); protected Transform3D transform; @@ -35,6 +36,7 @@ public class ComponentPosition extends Component { * set a new transformation * @return Transformation of the position */ + @Override public Transform3D getTransform() { return this.transform; } diff --git a/src/org/atriasoft/ege/components/ComponentRenderColoredStaticMesh.java b/src/org/atriasoft/ege/components/ComponentRenderColoredStaticMesh.java index 5432f5a..c176212 100644 --- a/src/org/atriasoft/ege/components/ComponentRenderColoredStaticMesh.java +++ b/src/org/atriasoft/ege/components/ComponentRenderColoredStaticMesh.java @@ -2,65 +2,56 @@ package org.atriasoft.ege.components; import org.atriasoft.ege.Component; +import org.atriasoft.ege.components.part.PositionningInterface; +import org.atriasoft.ege.components.part.TransformRender; import org.atriasoft.etk.Uri; -import org.atriasoft.etk.math.Matrix4f; import org.atriasoft.gale.backend3d.OpenGL; import org.atriasoft.gale.resource.ResourceProgram; public class ComponentRenderColoredStaticMesh extends ComponentRender { ComponentStaticMesh mesh = null; - private int oGLMatrixProjection; - private int oGLMatrixTransformation; - private int oGLMatrixView; - ComponentPosition position = null; ResourceProgram program = null; + TransformRender renderTransform = null; - public ComponentRenderColoredStaticMesh(Uri vertexShader, Uri fragmentShader) { + public ComponentRenderColoredStaticMesh(final Uri vertexShader, final Uri fragmentShader) { + this.renderTransform = new TransformRender(); this.program = ResourceProgram.create(vertexShader, fragmentShader); if (this.program != null) { - this.oGLMatrixTransformation = this.program.getUniform("in_matrixTransformation"); - this.oGLMatrixProjection = this.program.getUniform("in_matrixProjection"); - this.oGLMatrixView = this.program.getUniform("in_matrixView"); + this.renderTransform.init(this.program); } } @Override - public void addFriendComponent(Component component) { - if (component.getType().contentEquals("static-mesh")) { - this.mesh = (ComponentStaticMesh) component; + public void addFriendComponent(final Component component) { + if (component instanceof ComponentStaticMesh refTyped) { + this.mesh = refTyped; } - if (component.getType().contentEquals("position")) { - this.position = (ComponentPosition) component; + if (component instanceof PositionningInterface refTyped) { + this.renderTransform.setPositionning(refTyped); } } @Override - public void removeFriendComponent(Component component) { + public void removeFriendComponent(final Component component) { // nothing to do. } @Override public void render() { + // Select the program: this.program.use(); - - final Matrix4f projectionMatrix = OpenGL.getMatrix(); - final Matrix4f viewMatrix = OpenGL.getCameraMatrix(); - //Log.warning("position 22 " + this.position.getTransform()); - final Matrix4f transformationMatrix = this.position.getTransform().getOpenGLMatrix(); + // Bind all the element for the rendering: + this.renderTransform.bindForRendering(this.program); this.mesh.bindForRendering(); - - this.program.uniformMatrix(this.oGLMatrixView, viewMatrix); - this.program.uniformMatrix(this.oGLMatrixProjection, projectionMatrix); - // Change the position for each element with the same pipeline you need to render ... - this.program.uniformMatrix(this.oGLMatrixTransformation, transformationMatrix); // update of flags is done asyncronously ==> need update befor drawing... OpenGL.updateAllFlags(); // Request the draw od the elements: this.mesh.render(); - + // remove all element to render: + this.renderTransform.unBindForRendering(); this.mesh.unBindForRendering(); - + // Disable program: this.program.unUse(); } } diff --git a/src/org/atriasoft/ege/components/ComponentRenderMeshPalette.java b/src/org/atriasoft/ege/components/ComponentRenderMeshPalette.java index 83b6304..b275f3c 100644 --- a/src/org/atriasoft/ege/components/ComponentRenderMeshPalette.java +++ b/src/org/atriasoft/ege/components/ComponentRenderMeshPalette.java @@ -1,44 +1,50 @@ package org.atriasoft.ege.components; import org.atriasoft.ege.Component; +import org.atriasoft.ege.components.part.LightRender; +import org.atriasoft.ege.components.part.PositionningInterface; +import org.atriasoft.ege.components.part.TransformRender; +import org.atriasoft.ege.engines.EngineLight; import org.atriasoft.etk.Uri; -import org.atriasoft.etk.math.Matrix4f; import org.atriasoft.gale.backend3d.OpenGL; import org.atriasoft.gale.resource.ResourceProgram; public class ComponentRenderMeshPalette extends ComponentRender { - private int GLMatrixProjection; - private int GLMatrixTransformation; - private int GLMatrixView; ComponentMesh mesh = null; - private ComponentPhysics playerPhysics = null; - ComponentPosition position = null; ResourceProgram program = null; ComponentTexturePalette texture = null; - public ComponentRenderMeshPalette(final Uri vertexShader, final Uri fragmentShader) { + LightRender renderLight = null; + TransformRender renderTransform = null; + + public ComponentRenderMeshPalette(final Uri vertexShader, final Uri fragmentShader, final EngineLight lightEngine) { + if (lightEngine != null) { + this.renderLight = new LightRender(lightEngine); + } + this.renderTransform = new TransformRender(); this.program = ResourceProgram.create(vertexShader, fragmentShader); if (this.program != null) { - this.GLMatrixTransformation = this.program.getUniform("in_matrixTransformation"); - this.GLMatrixProjection = this.program.getUniform("in_matrixProjection"); - this.GLMatrixView = this.program.getUniform("in_matrixView"); + this.renderTransform.init(this.program); + if (this.renderLight != null) { + this.renderLight.init(this.program); + } } } @Override public void addFriendComponent(final Component component) { - if (component.getType().contentEquals("mesh")) { - this.mesh = (ComponentMesh) component; + if (component instanceof ComponentMesh refTyped) { + this.mesh = refTyped; } - if (component.getType().contentEquals("texture")) { - this.texture = (ComponentTexturePalette) component; + if (component instanceof ComponentTexturePalette refTyped) { + this.texture = refTyped; } - if (component.getType().contentEquals("position")) { - this.position = (ComponentPosition) component; - } - if (component.getType().contentEquals("physics")) { - this.playerPhysics = (ComponentPhysics) component; + if (component instanceof PositionningInterface refTyped) { + this.renderTransform.setPositionning(refTyped); + if (this.renderLight != null) { + this.renderLight.setPositionning(refTyped); + } } } @@ -49,30 +55,27 @@ public class ComponentRenderMeshPalette extends ComponentRender { @Override public void render() { + // Select the program: this.program.use(); - final Matrix4f projectionMatrix = OpenGL.getMatrix(); - final Matrix4f viewMatrix = OpenGL.getCameraMatrix(); - Matrix4f transformationMatrix = null; - if (this.position != null) { - //Log.warning("position " + this.position.getTransform()); - transformationMatrix = this.position.getTransform().getOpenGLMatrix(); - } else if (this.playerPhysics != null) { - //Log.warning("playerPosition " + this.playerPhysics.getTransform()); - transformationMatrix = this.playerPhysics.getTransform().getOpenGLMatrix(); - } + // Bind all the element for the rendering: this.mesh.bindForRendering(); this.texture.bindForRendering(); - this.program.uniformMatrix(this.GLMatrixView, viewMatrix); - this.program.uniformMatrix(this.GLMatrixProjection, projectionMatrix); - // Change the position for each element with the same pipeline you need to render ... - this.program.uniformMatrix(this.GLMatrixTransformation, transformationMatrix); + if (this.renderLight != null) { + this.renderLight.bindForRendering(this.program); + } + this.renderTransform.bindForRendering(this.program); // update of flags is done asynchronously ==> need update before drawing... OpenGL.updateAllFlags(); // Request the draw all the elements: this.mesh.renderArrays(); - + // remove all element to render: + this.renderTransform.unBindForRendering(); + if (this.renderLight != null) { + this.renderLight.unBindForRendering(); + } this.texture.unBindForRendering(); this.mesh.unBindForRendering(); + // Disable program: this.program.unUse(); } } diff --git a/src/org/atriasoft/ege/components/ComponentRenderTexturedMaterialsDynamicMeshs.java b/src/org/atriasoft/ege/components/ComponentRenderTexturedMaterialsDynamicMeshs.java index b57ee93..c41dda1 100644 --- a/src/org/atriasoft/ege/components/ComponentRenderTexturedMaterialsDynamicMeshs.java +++ b/src/org/atriasoft/ege/components/ComponentRenderTexturedMaterialsDynamicMeshs.java @@ -2,107 +2,79 @@ package org.atriasoft.ege.components; import java.util.Set; -import org.atriasoft.ege.internal.Log; import org.atriasoft.ege.Component; -import org.atriasoft.ege.Light; -import org.atriasoft.ege.Material; +import org.atriasoft.ege.components.part.LightRender; +import org.atriasoft.ege.components.part.MaterialsRender; +import org.atriasoft.ege.components.part.PositionningInterface; +import org.atriasoft.ege.components.part.TransformRender; +import org.atriasoft.ege.engines.EngineLight; import org.atriasoft.etk.Uri; -import org.atriasoft.etk.math.Matrix4f; -import org.atriasoft.etk.math.Vector3f; import org.atriasoft.gale.backend3d.OpenGL; import org.atriasoft.gale.resource.ResourceProgram; -import org.atriasoft.ege.engines.EngineLight; public class ComponentRenderTexturedMaterialsDynamicMeshs extends ComponentRender { - private static final int numberOfLight = 8; ComponentDynamicMeshs meshs = null; ComponentTextures textures = null; - ComponentMaterials materials = null; ComponentPosition position = null; ResourceProgram program = null; - EngineLight lightEngine; - private int GLMatrixTransformation; - private int GLMatrixProjection; - private int GLMatrixView; - private int GLambientFactor; - private int GLdiffuseFactor; - private int GLspecularFactor; - private int GLshininess; - private GlLightIndex[] GLlights; + + LightRender renderLight = null; + MaterialsRender renderMaterials = null; + TransformRender renderTransform = null; - public ComponentRenderTexturedMaterialsDynamicMeshs(Uri vertexShader, Uri fragmentShader, EngineLight lightEngine) { - this.lightEngine = lightEngine; + public ComponentRenderTexturedMaterialsDynamicMeshs(final Uri vertexShader, final Uri fragmentShader, final EngineLight lightEngine) { + if (lightEngine != null) { + this.renderLight = new LightRender(lightEngine); + } + this.renderTransform = new TransformRender(); + this.renderMaterials = new MaterialsRender(); this.program = ResourceProgram.create(vertexShader, fragmentShader); if (this.program != null) { - this.GLMatrixTransformation = this.program.getUniform("in_matrixTransformation"); - this.GLMatrixProjection = this.program.getUniform("in_matrixProjection"); - this.GLMatrixView = this.program.getUniform("in_matrixView"); - this.GLambientFactor = this.program.getUniform("in_material.ambientFactor"); - this.GLdiffuseFactor = this.program.getUniform("in_material.diffuseFactor"); - this.GLspecularFactor = this.program.getUniform("in_material.specularFactor"); - this.GLshininess = this.program.getUniform("in_material.shininess"); - this.GLlights = new GlLightIndex[numberOfLight]; - for (int iii=0; iii keys = this.meshs.getKeys(); - - for (int iii=0; iii keys = this.meshs.getKeys(); for (String key : keys) { this.meshs.bindForRendering(key); this.textures.bindForRendering(key); - Material mat = this.materials.getMaterial(key); - this.program.uniformVector(GLambientFactor, mat.getAmbientFactor()); - this.program.uniformVector(GLdiffuseFactor, mat.getDiffuseFactor()); - this.program.uniformVector(GLspecularFactor, mat.getSpecularFactor()); - this.program.uniformFloat(GLshininess, mat.getShininess()); + this.renderMaterials.bindForRendering(this.program, key); // update of flags is done asynchronously ==> need update before drawing... OpenGL.updateAllFlags(); // Request the draw all the elements: @@ -110,6 +82,11 @@ public class ComponentRenderTexturedMaterialsDynamicMeshs extends ComponentRende this.textures.unBindForRendering(key); this.meshs.unBindForRendering(key); } + // remove all element to render: + this.renderTransform.unBindForRendering(); + if (this.renderLight != null) { + this.renderLight.unBindForRendering(); + } this.program.unUse(); } } diff --git a/src/org/atriasoft/ege/components/ComponentRenderTexturedMaterialsStaticMesh.java b/src/org/atriasoft/ege/components/ComponentRenderTexturedMaterialsStaticMesh.java index 46d770b..ff7abcb 100644 --- a/src/org/atriasoft/ege/components/ComponentRenderTexturedMaterialsStaticMesh.java +++ b/src/org/atriasoft/ege/components/ComponentRenderTexturedMaterialsStaticMesh.java @@ -1,71 +1,62 @@ package org.atriasoft.ege.components; import org.atriasoft.ege.Component; -import org.atriasoft.ege.Light; -import org.atriasoft.ege.Material; +import org.atriasoft.ege.components.part.LightRender; +import org.atriasoft.ege.components.part.MaterialRender; +import org.atriasoft.ege.components.part.PositionningInterface; +import org.atriasoft.ege.components.part.TransformRender; import org.atriasoft.ege.engines.EngineLight; import org.atriasoft.etk.Uri; -import org.atriasoft.etk.math.Matrix4f; -import org.atriasoft.etk.math.Vector3f; import org.atriasoft.gale.backend3d.OpenGL; import org.atriasoft.gale.resource.ResourceProgram; public class ComponentRenderTexturedMaterialsStaticMesh extends ComponentRender { - private static final int numberOfLight = 8; - private int GLambientFactor; - private int GLdiffuseFactor; - private GlLightIndex[] GLlights; - private int GLMatrixProjection; - private int GLMatrixTransformation; - private int GLMatrixView; - private int GLshininess; - private int GLspecularFactor; - EngineLight lightEngine; - ComponentMaterial material = null; + ComponentStaticMesh mesh = null; - private ComponentPhysics playerPhysics = null; - ComponentPosition position = null; ResourceProgram program = null; ComponentTexture texture = null; + LightRender renderLight = null; + MaterialRender renderMaterial = null; + TransformRender renderTransform = null; + + public ComponentRenderTexturedMaterialsStaticMesh(final Uri vertexShader, final Uri fragmentShader) { + this(vertexShader, fragmentShader, null); + } + public ComponentRenderTexturedMaterialsStaticMesh(final Uri vertexShader, final Uri fragmentShader, final EngineLight lightEngine) { - this.lightEngine = lightEngine; + if (lightEngine != null) { + this.renderLight = new LightRender(lightEngine); + } + this.renderTransform = new TransformRender(); + this.renderMaterial = new MaterialRender(); this.program = ResourceProgram.create(vertexShader, fragmentShader); if (this.program != null) { - this.GLMatrixTransformation = this.program.getUniform("in_matrixTransformation"); - this.GLMatrixProjection = this.program.getUniform("in_matrixProjection"); - this.GLMatrixView = this.program.getUniform("in_matrixView"); - this.GLambientFactor = this.program.getUniform("in_material.ambientFactor"); - this.GLdiffuseFactor = this.program.getUniform("in_material.diffuseFactor"); - this.GLspecularFactor = this.program.getUniform("in_material.specularFactor"); - this.GLshininess = this.program.getUniform("in_material.shininess"); - this.GLlights = new GlLightIndex[ComponentRenderTexturedMaterialsStaticMesh.numberOfLight]; - for (int iii = 0; iii < ComponentRenderTexturedMaterialsStaticMesh.numberOfLight; iii++) { - final int color = this.program.getUniform("in_lights[" + iii + "].color"); - final int position = this.program.getUniform("in_lights[" + iii + "].position"); - final int attenuation = this.program.getUniform("in_lights[" + iii + "].attenuation"); - this.GLlights[iii] = new GlLightIndex(color, position, attenuation); + this.renderTransform.init(this.program); + if (this.renderLight != null) { + this.renderLight.init(this.program); } + this.renderMaterial.init(this.program); } } @Override public void addFriendComponent(final Component component) { - if (component.getType().contentEquals("static-mesh")) { - this.mesh = (ComponentStaticMesh) component; + if (component instanceof ComponentStaticMesh refTyped) { + this.mesh = refTyped; } - if (component.getType().contentEquals("texture")) { - this.texture = (ComponentTexture) component; + if (component instanceof ComponentTexture refTyped) { + this.texture = refTyped; } - if (component.getType().contentEquals("material")) { - this.material = (ComponentMaterial) component; + if (component instanceof ComponentMaterial refTyped) { + this.renderMaterial.setMaterial(refTyped); } - if (component.getType().contentEquals("position")) { - this.position = (ComponentPosition) component; - } - if (component.getType().contentEquals("physics")) { - this.playerPhysics = (ComponentPhysics) component; + if (component instanceof PositionningInterface refTyped) { + this.renderTransform.setPositionning(refTyped); + if (this.renderLight != null) { + this.renderLight.setPositionning(refTyped); + } } } @@ -76,49 +67,29 @@ public class ComponentRenderTexturedMaterialsStaticMesh extends ComponentRender @Override public void render() { + // Select the program: this.program.use(); - Light[] lights = null; - Matrix4f transformationMatrix = null; - if (this.position != null) { - //Log.warning("position " + this.position.getTransform()); - lights = this.lightEngine.getNearest(this.position.getTransform().getPosition()); - transformationMatrix = this.position.getTransform().getOpenGLMatrix(); - } else if (this.playerPhysics != null) { - //Log.warning("playerPosition " + this.playerPhysics.getTransform()); - lights = this.lightEngine.getNearest(this.playerPhysics.getTransform().getPosition()); - transformationMatrix = this.playerPhysics.getTransform().getOpenGLMatrix(); - } - final Matrix4f projectionMatrix = OpenGL.getMatrix(); - final Matrix4f viewMatrix = OpenGL.getCameraMatrix(); + // Bind all the element for the rendering: this.mesh.bindForRendering(); this.texture.bindForRendering(); - - final Material mat = this.material.getMaterial(); - this.program.uniformVector(this.GLambientFactor, mat.getAmbientFactor()); - this.program.uniformVector(this.GLdiffuseFactor, mat.getDiffuseFactor()); - this.program.uniformVector(this.GLspecularFactor, mat.getSpecularFactor()); - this.program.uniformFloat(this.GLshininess, mat.getShininess()); - for (int iii = 0; iii < ComponentRenderTexturedMaterialsStaticMesh.numberOfLight; iii++) { - if (lights[iii] != null) { - this.program.uniformVector(this.GLlights[iii].oGLposition, lights[iii].getPositionDelta()); - this.program.uniformVector(this.GLlights[iii].oGLcolor, lights[iii].getColor()); - this.program.uniformVector(this.GLlights[iii].oGLattenuation, lights[iii].getAttenuation()); - } else { - this.program.uniformVector(this.GLlights[iii].oGLposition, Vector3f.ZERO); - this.program.uniformVector(this.GLlights[iii].oGLcolor, Vector3f.ZERO); - this.program.uniformVector(this.GLlights[iii].oGLattenuation, new Vector3f(1, 0, 0)); - } + this.renderMaterial.bindForRendering(this.program); + if (this.renderLight != null) { + this.renderLight.bindForRendering(this.program); } - this.program.uniformMatrix(this.GLMatrixView, viewMatrix); - this.program.uniformMatrix(this.GLMatrixProjection, projectionMatrix); - // Change the position for each element with the same pipeline you need to render ... - this.program.uniformMatrix(this.GLMatrixTransformation, transformationMatrix); + this.renderTransform.bindForRendering(this.program); // update of flags is done asynchronously ==> need update before drawing... OpenGL.updateAllFlags(); // Request the draw all the elements: this.mesh.render(); + // remove all element to render: + this.renderTransform.unBindForRendering(); + if (this.renderLight != null) { + this.renderLight.unBindForRendering(); + } + this.renderMaterial.unBindForRendering(); this.texture.unBindForRendering(); this.mesh.unBindForRendering(); + // Disable program: this.program.unUse(); } } diff --git a/src/org/atriasoft/ege/components/ComponentRenderTexturedMaterialsStaticMeshs.java b/src/org/atriasoft/ege/components/ComponentRenderTexturedMaterialsStaticMeshs.java index 5428e01..056e98a 100644 --- a/src/org/atriasoft/ege/components/ComponentRenderTexturedMaterialsStaticMeshs.java +++ b/src/org/atriasoft/ege/components/ComponentRenderTexturedMaterialsStaticMeshs.java @@ -3,112 +3,91 @@ package org.atriasoft.ege.components; import java.util.Set; import org.atriasoft.ege.Component; -import org.atriasoft.ege.Light; -import org.atriasoft.ege.Material; +import org.atriasoft.ege.components.part.LightRender; +import org.atriasoft.ege.components.part.MaterialsRender; +import org.atriasoft.ege.components.part.PositionningInterface; +import org.atriasoft.ege.components.part.TransformRender; +import org.atriasoft.ege.engines.EngineLight; import org.atriasoft.etk.Uri; -import org.atriasoft.etk.math.Matrix4f; -import org.atriasoft.etk.math.Vector3f; import org.atriasoft.gale.backend3d.OpenGL; import org.atriasoft.gale.resource.ResourceProgram; -import org.atriasoft.ege.engines.EngineLight; public class ComponentRenderTexturedMaterialsStaticMeshs extends ComponentRender { - private static final int numberOfLight = 8; ComponentStaticMeshs meshs = null; ComponentTextures textures = null; ComponentMaterials materials = null; - ComponentPosition position = null; ResourceProgram program = null; - EngineLight lightEngine; - private int GLMatrixTransformation; - private int GLMatrixProjection; - private int GLMatrixView; - private int GLambientFactor; - private int GLdiffuseFactor; - private int GLspecularFactor; - private int GLshininess; - private GlLightIndex[] GLlights; + LightRender renderLight = null; + MaterialsRender renderMaterials = null; + TransformRender renderTransform = null; - public ComponentRenderTexturedMaterialsStaticMeshs(Uri vertexShader, Uri fragmentShader, EngineLight lightEngine) { - this.lightEngine = lightEngine; + public ComponentRenderTexturedMaterialsStaticMeshs(final Uri vertexShader, final Uri fragmentShader, final EngineLight lightEngine) { + if (lightEngine != null) { + this.renderLight = new LightRender(lightEngine); + } + this.renderTransform = new TransformRender(); + this.renderMaterials = new MaterialsRender(); this.program = ResourceProgram.create(vertexShader, fragmentShader); if (this.program != null) { - this.GLMatrixTransformation = this.program.getUniform("in_matrixTransformation"); - this.GLMatrixProjection = this.program.getUniform("in_matrixProjection"); - this.GLMatrixView = this.program.getUniform("in_matrixView"); - this.GLambientFactor = this.program.getUniform("in_material.ambientFactor"); - this.GLdiffuseFactor = this.program.getUniform("in_material.diffuseFactor"); - this.GLspecularFactor = this.program.getUniform("in_material.specularFactor"); - this.GLshininess = this.program.getUniform("in_material.shininess"); - this.GLlights = new GlLightIndex[numberOfLight]; - for (int iii=0; iii keys = this.meshs.getKeys(); - - for (int iii=0; iii keys = this.meshs.getKeys(); for (String key : keys) { this.meshs.bindForRendering(key); this.textures.bindForRendering(key); - Material mat = this.materials.getMaterial(key); - this.program.uniformVector(GLambientFactor, mat.getAmbientFactor()); - this.program.uniformVector(GLdiffuseFactor, mat.getDiffuseFactor()); - this.program.uniformVector(GLspecularFactor, mat.getSpecularFactor()); - this.program.uniformFloat(GLshininess, mat.getShininess()); + this.renderMaterials.bindForRendering(this.program, key); // update of flags is done asynchronously ==> need update before drawing... OpenGL.updateAllFlags(); // Request the draw all the elements: this.meshs.render(key); + // remove all element to render: + this.renderMaterials.unBindForRendering(); this.textures.unBindForRendering(key); this.meshs.unBindForRendering(key); } + // remove all element to render: + this.renderTransform.unBindForRendering(); + if (this.renderLight != null) { + this.renderLight.unBindForRendering(); + } this.program.unUse(); } } diff --git a/src/org/atriasoft/ege/components/ComponentRenderTexturedStaticMesh.java b/src/org/atriasoft/ege/components/ComponentRenderTexturedStaticMesh.java index ef198a0..22071d8 100644 --- a/src/org/atriasoft/ege/components/ComponentRenderTexturedStaticMesh.java +++ b/src/org/atriasoft/ege/components/ComponentRenderTexturedStaticMesh.java @@ -1,44 +1,40 @@ package org.atriasoft.ege.components; import org.atriasoft.ege.Component; +import org.atriasoft.ege.components.part.PositionningInterface; +import org.atriasoft.ege.components.part.TransformRender; import org.atriasoft.etk.Uri; -import org.atriasoft.etk.math.Matrix4f; import org.atriasoft.gale.backend3d.OpenGL; import org.atriasoft.gale.resource.ResourceProgram; public class ComponentRenderTexturedStaticMesh extends ComponentRender { - private int GLMatrixProjection; - private int GLMatrixTransformation; - private int GLMatrixView; ComponentStaticMesh mesh = null; - private ComponentPhysics playerPhysics = null; + private final ComponentPhysics playerPhysics = null; ComponentPosition position = null; ResourceProgram program = null; ComponentTexture texture = null; + TransformRender renderTransform = null; + public ComponentRenderTexturedStaticMesh(final Uri vertexShader, final Uri fragmentShader) { + this.renderTransform = new TransformRender(); this.program = ResourceProgram.create(vertexShader, fragmentShader); if (this.program != null) { - this.GLMatrixTransformation = this.program.getUniform("in_matrixTransformation"); - this.GLMatrixProjection = this.program.getUniform("in_matrixProjection"); - this.GLMatrixView = this.program.getUniform("in_matrixView"); + this.renderTransform.init(this.program); } } @Override public void addFriendComponent(final Component component) { - if (component.getType().contentEquals("static-mesh")) { - this.mesh = (ComponentStaticMesh) component; + if (component instanceof ComponentStaticMesh refTyped) { + this.mesh = refTyped; } - if (component.getType().contentEquals("texture")) { - this.texture = (ComponentTexture) component; + if (component instanceof ComponentTexture refTyped) { + this.texture = refTyped; } - if (component.getType().contentEquals("position")) { - this.position = (ComponentPosition) component; - } - if (component.getType().contentEquals("physics")) { - this.playerPhysics = (ComponentPhysics) component; + if (component instanceof PositionningInterface refTyped) { + this.renderTransform.setPositionning(refTyped); } } @@ -49,27 +45,18 @@ public class ComponentRenderTexturedStaticMesh extends ComponentRender { @Override public void render() { + // Select the program: this.program.use(); - final Matrix4f projectionMatrix = OpenGL.getMatrix(); - final Matrix4f viewMatrix = OpenGL.getCameraMatrix(); - Matrix4f transformationMatrix = null; - if (this.position != null) { - //Log.warning("position " + this.position.getTransform()); - transformationMatrix = this.position.getTransform().getOpenGLMatrix(); - } else if (this.playerPhysics != null) { - //Log.warning("playerPosition " + this.playerPhysics.getTransform()); - transformationMatrix = this.playerPhysics.getTransform().getOpenGLMatrix(); - } + // Bind all the element for the rendering: this.mesh.bindForRendering(); this.texture.bindForRendering(); - this.program.uniformMatrix(this.GLMatrixView, viewMatrix); - this.program.uniformMatrix(this.GLMatrixProjection, projectionMatrix); - // Change the position for each element with the same pipeline you need to render ... - this.program.uniformMatrix(this.GLMatrixTransformation, transformationMatrix); + this.renderTransform.bindForRendering(this.program); // update of flags is done asynchronously ==> need update before drawing... OpenGL.updateAllFlags(); // Request the draw all the elements: this.mesh.render(); + // remove all element to render: + this.renderTransform.unBindForRendering(); this.texture.unBindForRendering(); this.mesh.unBindForRendering(); this.program.unUse(); diff --git a/src/org/atriasoft/ege/components/ComponentTexturePalette.java b/src/org/atriasoft/ege/components/ComponentTexturePalette.java index a1294b7..a9dd74b 100644 --- a/src/org/atriasoft/ege/components/ComponentTexturePalette.java +++ b/src/org/atriasoft/ege/components/ComponentTexturePalette.java @@ -1,14 +1,10 @@ package org.atriasoft.ege.components; -import java.awt.Image; - import org.atriasoft.egami.ImageByte; -import org.atriasoft.egami.ToolImage; import org.atriasoft.ege.Component; import org.atriasoft.ege.internal.Log; import org.atriasoft.etk.Uri; import org.atriasoft.gale.resource.ResourceTexture2; -import org.atriasoft.iogami.IOgami; import org.atriasoft.loader3d.resources.ResourcePaletteFile; public class ComponentTexturePalette extends Component { @@ -16,7 +12,7 @@ public class ComponentTexturePalette extends Component { private final ResourcePaletteFile palette; private final ResourceTexture2 texture; - public ComponentTexturePalette(Uri paletteName) { + public ComponentTexturePalette(final Uri paletteName) { this.palette = ResourcePaletteFile.create(paletteName); this.texture = ResourceTexture2.createNamed("TEXTURE_OF_PALETTE:" + paletteName.toString()); if (this.texture == null) { @@ -33,7 +29,7 @@ public class ComponentTexturePalette extends Component { public void updateFromPalette() { Log.warning("update palet environnement"); final ImageByte img = this.palette.getImageByte(); - IOgami.storePNG(new Uri("/home/heero/000000000aaaaplopppp.png"), img); + //IOgami.storePNG(new Uri("/home/heero/000000000aaaaplopppp.png"), img); this.texture.set(img); } diff --git a/src/org/atriasoft/ege/components/GlLightIndex.java b/src/org/atriasoft/ege/components/GlLightIndex.java index 10e41ca..f320b73 100644 --- a/src/org/atriasoft/ege/components/GlLightIndex.java +++ b/src/org/atriasoft/ege/components/GlLightIndex.java @@ -1,9 +1,9 @@ package org.atriasoft.ege.components; public class GlLightIndex { - int oGLcolor; - int oGLposition; - int oGLattenuation; + public int oGLcolor; + public int oGLposition; + public int oGLattenuation; public GlLightIndex(int gLcolor, int gLposition, int gLattenuation) { oGLcolor = gLcolor; oGLposition = gLposition; diff --git a/src/org/atriasoft/ege/components/part/LightRender.java b/src/org/atriasoft/ege/components/part/LightRender.java new file mode 100644 index 0000000..99c70cb --- /dev/null +++ b/src/org/atriasoft/ege/components/part/LightRender.java @@ -0,0 +1,59 @@ +package org.atriasoft.ege.components.part; + +import org.atriasoft.ege.Light; +import org.atriasoft.ege.components.GlLightIndex; +import org.atriasoft.ege.engines.EngineLight; +import org.atriasoft.etk.math.Vector3f; +import org.atriasoft.gale.resource.ResourceProgram; + +public class LightRender implements PartRenderInterface { + + private static final int numberOfLight = 8; + private GlLightIndex[] GLlights; + EngineLight lightEngine; + PositionningInterface position = null; + + public LightRender(final EngineLight lightEngine) { + this.lightEngine = lightEngine; + } + + @Override + public void init(final ResourceProgram program) { + this.GLlights = new GlLightIndex[LightRender.numberOfLight]; + for (int iii = 0; iii < LightRender.numberOfLight; iii++) { + final int color = program.getUniform("in_lights[" + iii + "].color"); + final int position = program.getUniform("in_lights[" + iii + "].position"); + final int attenuation = program.getUniform("in_lights[" + iii + "].attenuation"); + this.GLlights[iii] = new GlLightIndex(color, position, attenuation); + } + } + + @Override + public void bindForRendering(final ResourceProgram program) { + // preparing stage + Vector3f positionObject = this.position.getTransform().getPosition(); + Light[] lights = this.lightEngine.getNearest(positionObject); + // injection stage + for (int iii = 0; iii < LightRender.numberOfLight; iii++) { + if (lights[iii] != null) { + //Log.warning("Set light : [" + iii + "] " + lights[iii]); + program.uniformVector(this.GLlights[iii].oGLposition, lights[iii].getPositionDelta()); + program.uniformColorRGB(this.GLlights[iii].oGLcolor, lights[iii].getColor()); + program.uniformVector(this.GLlights[iii].oGLattenuation, lights[iii].getAttenuation()); + } else { + program.uniformVector(this.GLlights[iii].oGLposition, Vector3f.ZERO); + program.uniformVector(this.GLlights[iii].oGLcolor, Vector3f.ZERO); + //program.uniformColorRGB(this.GLlights[iii].oGLcolor, Color.NONE); + program.uniformVector(this.GLlights[iii].oGLattenuation, new Vector3f(1, 0, 0)); + } + } + } + @Override + public void unBindForRendering() { + + } + + public void setPositionning(final PositionningInterface component) { + this.position = component; + } +} diff --git a/src/org/atriasoft/ege/components/part/MaterialRender.java b/src/org/atriasoft/ege/components/part/MaterialRender.java new file mode 100644 index 0000000..6472e80 --- /dev/null +++ b/src/org/atriasoft/ege/components/part/MaterialRender.java @@ -0,0 +1,19 @@ +package org.atriasoft.ege.components.part; + +import org.atriasoft.ege.components.ComponentMaterial; +import org.atriasoft.gale.resource.ResourceProgram; + +public class MaterialRender extends MaterialRenderBase { + + ComponentMaterial material = null; + + @Override + public void bindForRendering(final ResourceProgram program) { + bindForRendering(program, this.material.getMaterial()); + } + + public void setMaterial(final ComponentMaterial component) { + this.material = component; + } + +} diff --git a/src/org/atriasoft/ege/components/part/MaterialRenderBase.java b/src/org/atriasoft/ege/components/part/MaterialRenderBase.java new file mode 100644 index 0000000..3d9ea2b --- /dev/null +++ b/src/org/atriasoft/ege/components/part/MaterialRenderBase.java @@ -0,0 +1,36 @@ +package org.atriasoft.ege.components.part; + +import org.atriasoft.ege.Material; +import org.atriasoft.gale.resource.ResourceProgram; + +public abstract class MaterialRenderBase implements PartRenderInterface { + + protected int GLambientFactor; + protected int GLdiffuseFactor; + protected int GLshininess; + protected int GLspecularFactor; + + public MaterialRenderBase() { + + } + + @Override + public void init(final ResourceProgram program) { + this.GLambientFactor = program.getUniform("in_material.ambientFactor"); + this.GLdiffuseFactor = program.getUniform("in_material.diffuseFactor"); + this.GLspecularFactor = program.getUniform("in_material.specularFactor"); + this.GLshininess = program.getUniform("in_material.shininess"); + } + + public void bindForRendering(final ResourceProgram program, final Material mat) { + program.uniformVector(this.GLambientFactor, mat.getAmbientFactor()); + program.uniformVector(this.GLdiffuseFactor, mat.getDiffuseFactor()); + program.uniformVector(this.GLspecularFactor, mat.getSpecularFactor()); + program.uniformFloat(this.GLshininess, mat.getShininess()); + } + + @Override + public void unBindForRendering() { + + } +} diff --git a/src/org/atriasoft/ege/components/part/MaterialsRender.java b/src/org/atriasoft/ege/components/part/MaterialsRender.java new file mode 100644 index 0000000..15544d1 --- /dev/null +++ b/src/org/atriasoft/ege/components/part/MaterialsRender.java @@ -0,0 +1,23 @@ +package org.atriasoft.ege.components.part; + +import org.atriasoft.ege.components.ComponentMaterials; +import org.atriasoft.gale.resource.ResourceProgram; + +public class MaterialsRender extends MaterialRenderBase { + + ComponentMaterials material = null; + + public void bindForRendering(final ResourceProgram program, final String type) { + bindForRendering(program, this.material.getMaterial(type)); + } + + @Override + public void bindForRendering(final ResourceProgram program) { + bindForRendering(program, this.material.getMaterial("default")); + } + + + public void setMaterial(final ComponentMaterials component) { + this.material = component; + } +} diff --git a/src/org/atriasoft/ege/components/part/PartRenderInterface.java b/src/org/atriasoft/ege/components/part/PartRenderInterface.java new file mode 100644 index 0000000..7c252e1 --- /dev/null +++ b/src/org/atriasoft/ege/components/part/PartRenderInterface.java @@ -0,0 +1,24 @@ +package org.atriasoft.ege.components.part; + +import org.atriasoft.gale.resource.ResourceProgram; + +/** + * Element that permit to add prat element on the Shader rendering (permit to reduce code and normalize IO naming of the shader + */ +public interface PartRenderInterface { + /** + * Initialize the render part with the program elements + * @param program Open GL program + */ + void init(final ResourceProgram program); + /** + * Bing this part in the shader + * @param program Program that manage the rendering + */ + void bindForRendering(final ResourceProgram program); + /** + * Remove element from the shader. + */ + void unBindForRendering(); + +} diff --git a/src/org/atriasoft/ege/components/part/PositionningInterface.java b/src/org/atriasoft/ege/components/part/PositionningInterface.java new file mode 100644 index 0000000..c08adfe --- /dev/null +++ b/src/org/atriasoft/ege/components/part/PositionningInterface.java @@ -0,0 +1,11 @@ +package org.atriasoft.ege.components.part; + +import org.atriasoft.etk.math.Transform3D; + +public interface PositionningInterface { + /** + * Get the current positionning on an object + * @return + */ + Transform3D getTransform(); +} diff --git a/src/org/atriasoft/ege/components/part/TransformRender.java b/src/org/atriasoft/ege/components/part/TransformRender.java new file mode 100644 index 0000000..67f45c2 --- /dev/null +++ b/src/org/atriasoft/ege/components/part/TransformRender.java @@ -0,0 +1,47 @@ +package org.atriasoft.ege.components.part; + +import org.atriasoft.etk.math.Matrix4f; +import org.atriasoft.gale.backend3d.OpenGL; +import org.atriasoft.gale.resource.ResourceProgram; + +public class TransformRender implements PartRenderInterface { + + private int GLMatrixProjection; + private int GLMatrixTransformation; + private int GLMatrixView; + private PositionningInterface position = null; + + public TransformRender() { + + } + + @Override + public void init(final ResourceProgram program) { + this.GLMatrixTransformation = program.getUniform("in_matrixTransformation"); + this.GLMatrixProjection = program.getUniform("in_matrixProjection"); + this.GLMatrixView = program.getUniform("in_matrixView"); + } + + @Override + public void bindForRendering(final ResourceProgram program) { + // preparing stage + final Matrix4f projectionMatrix = OpenGL.getMatrix(); + final Matrix4f viewMatrix = OpenGL.getCameraMatrix(); + Matrix4f transformationMatrix = this.position.getTransform().getOpenGLMatrix(); + // injection stage + program.uniformMatrix(this.GLMatrixView, viewMatrix); + program.uniformMatrix(this.GLMatrixProjection, projectionMatrix); + // Change the position for each element with the same pipeline you need to render ... + program.uniformMatrix(this.GLMatrixTransformation, transformationMatrix); + } + + + @Override + public void unBindForRendering() { + + } + + public void setPositionning(final PositionningInterface component) { + this.position = component; + } +} diff --git a/src/org/atriasoft/ege/engines/EngineLight.java b/src/org/atriasoft/ege/engines/EngineLight.java index 46088b7..f549669 100644 --- a/src/org/atriasoft/ege/engines/EngineLight.java +++ b/src/org/atriasoft/ege/engines/EngineLight.java @@ -6,70 +6,73 @@ import org.atriasoft.ege.Component; import org.atriasoft.ege.Engine; import org.atriasoft.ege.Environement; import org.atriasoft.ege.Light; -import org.atriasoft.etk.math.Vector3f; -import org.atriasoft.ege.internal.Log; import org.atriasoft.ege.camera.Camera; -import org.atriasoft.ege.components.ComponentAI; import org.atriasoft.ege.components.ComponentLight; import org.atriasoft.ege.components.ComponentLightSun; +import org.atriasoft.ege.internal.Log; +import org.atriasoft.etk.math.Vector3f; public class EngineLight extends Engine { public static final String ENGINE_NAME = "light"; - private Vector componentLights = new Vector(); - private Vector componentSuns = new Vector(); - public EngineLight(Environement env) { + private final Vector componentLights = new Vector(); + private final Vector componentSuns = new Vector(); + public EngineLight(final Environement env) { super(env); // TODO Auto-generated constructor stub } @Override - public void componentRemove(Component ref) { - componentLights.remove(ref); - componentSuns.remove(ref); + public void componentRemove(final Component ref) { + this.componentLights.remove(ref); + this.componentSuns.remove(ref); } @Override - public void componentAdd(Component ref) { - if (ref instanceof ComponentLightSun == true) { - componentSuns.add((ComponentLightSun)ref); + public void componentAdd(final Component ref) { + if (ref instanceof ComponentLightSun refTyped) { + this.componentSuns.add(refTyped); return; } - if (ref instanceof ComponentLight == true) { - componentLights.add((ComponentLight)ref); - return; + if (ref instanceof ComponentLight refTyped) { + this.componentLights.add(refTyped); } } @Override - public void update(long deltaMili) { + public void update(final long deltaMili) { // nothing to do ... } @Override - public void render(long deltaMili, Camera camera) { + public void render(final long deltaMili, final Camera camera) { // nothing to do ... } @Override - public void renderDebug(long deltaMili, Camera camera) { + public void renderDebug(final long deltaMili, final Camera camera) { // nothing to do ... } @Override public String getType() { // TODO Auto-generated method stub - return ENGINE_NAME; + return EngineLight.ENGINE_NAME; } - public Light[] getNearest(Vector3f position) { + public Light[] getNearest(final Vector3f position) { Light[] out = new Light[8]; int count = 0; - for (ComponentLightSun elem: componentSuns) { + for (ComponentLightSun elem: this.componentSuns) { out[count] = new Light(elem.getLight().getColor(), elem.getPosition(), elem.getLight().getAttenuation()); + if (count>=8) { + Log.error("need to update ligth count"); + return out; + } count++; } + //Log.warning("Get " + count + "/" + out.length + " lights (SUN) ..."); float maxDistance = 50*50; - for (ComponentLight elem: componentLights) { + for (ComponentLight elem: this.componentLights) { Vector3f pos = elem.getPosition(); if (count>=8) { Log.error("need to update ligth count"); @@ -80,6 +83,7 @@ public class EngineLight extends Engine { count++; } } + //Log.warning("Get " + count + "/" + out.length + " lights..."); return out; }