Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
   * The MIT License (MIT)
   * Copyright (c) 2014 Christian Gärtner
   * Permission is hereby granted, free of charge, to any person obtaining a copy
   * of this software and associated documentation files (the "Software"), to deal
   * in the Software without restriction, including without limitation the rights
   * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  * copies of the Software, and to permit persons to whom the Software is
  * furnished to do so, subject to the following conditions:
  * The above copyright notice and this permission notice shall be included in all
  * copies or substantial portions of the Software.
 package org.achtern.AchternEngine.core.rendering.shader;
 import lombok.Getter;
 import lombok.Setter;
A Shader wraps a org.achtern.AchternEngine.core.resource.fileparser.GLSLProgram, this class is meant to be extended by all shader one might write. This allows the sub-classes to load the GLSLProgram and inject it all by themselves.
This Shader class (abstract) also handles uniform setting aka getting the correct values from the org.achtern.AchternEngine.core.rendering.Material, org.achtern.AchternEngine.core.rendering.RenderEngine and org.achtern.AchternEngine.core.scenegraph.Node. The actual sending of the data to the GPU is getting handled by the implementation.
 public abstract class Shader {
     public static final Logger LOGGER = LoggerFactory.getLogger(Shader.class);
     @Getter @Setter private GLSLParser parser = new GLSLParser();
     @Getter protected GLSLProgram program;

For subclasses.
     public Shader() {
     public Shader(GLSLProgram program) {
         this. = program;
     public void updateUniforms(RenderEngine renderEngineFigure figure) {
         Material material = figure.getMaterial();
         Transform transform = figure.getTransform();
         Matrix4f worldMat = transform.getTransformation();
         Matrix4f projection = renderEngine.getCamera().getViewProjection().mul(worldMat);
         // Keeps track of the current SamplerSlot in use.
         int samplerSlot = 0;
         for (Uniform u : this..getUniforms()) {
             String n = u.getName(); // Just a quick access to the name!
             // textures aka sampler2Ds
             if (u.getType().equalsIgnoreCase("sampler2D")) {
                 // material takes precedence over the renderengine
                 if (material.hasTexture(n)) {
                     // Bind it to the sampler slot. this sampler slot comes from the RenderEngine
                     renderEngine.getDataBinder().bind(material.getTexture(n), samplerSlot);
                 } else if (renderEngine.hasTexture(n)) {
                     renderEngine.getDataBinder().bind(renderEngine.getTexture(n), samplerSlot);
                 } else if (!figure.getMesh().getData().getMode().equals(..)) {
                     // we do not need a texture anyway.
                 } else {
                     .warn("{}: texture '{}' not found in material nor RenderEngine.",
                            this.getClass().getSimpleName(), n);
                    // If the texture has not been found, set the missing texture from
                    // Material
                    renderEngine.getDataBinder().bind(material.getTexture(n), samplerSlot);
                    n = "diffuse"// Default to diffuse!
                // and set the value of the uniform to the sampler slot
                // Now common structs like DirectionalLight, PointLight, SpotLight, AmbientLight
            } else if (u.getType().equalsIgnoreCase("DirectionalLight") ||
                    u.getType().equalsIgnoreCase("SpotLight") ||
                    u.getType().equalsIgnoreCase("PointLight") ||
            } else if (u.getType().equalsIgnoreCase("Fog")) {
                GlobalEntity<FoggE = renderEngine.getGlobal(Fog.class);
                if (gE != null) {
                } else {
                    // use disabled fog
                // other stuff below
                // currently supporting
                // color, MVP, (shadowMatrix=TEMP)
            } else if (n.equalsIgnoreCase("color")) {
            } else if (n.equalsIgnoreCase("MVP")) {
            } else if (n.equalsIgnoreCase("model")) {
            } else if (n.equalsIgnoreCase("modelView")) {
            } else if (n.equalsIgnoreCase("eyePos")) {
            } else if (n.equalsIgnoreCase("shadowMatrix")) {
                Matrix4f m = new Matrix4f().initIdentiy();
                if (renderEngine.getMatrix("shadowMatrix") != null) {
                    m = renderEngine.getMatrix("shadowMatrix").mul(transform.getTransformation());
            } else {
                // In the last step we try to find the value in the material,
                // otherwise leave it to the user!
                if (u.getType().equalsIgnoreCase("float")) {
                } else if ((u.getType().equalsIgnoreCase("vec3") && material.hasVector(n))) {
                } else if ((u.getType().equalsIgnoreCase("mat4") && material.hasMatrix(n))) {
                } else if ((u.getType().equalsIgnoreCase("vec4") && material.hasColor(n))) {
                } else if (u.getType().equalsIgnoreCase("int")) {
            // If the value is null, the developer has to fill it,
            // and has the change override values.
            // Finally set it.
    protected void handle(Uniform uniformTransform transformMaterial material,
                          RenderEngine renderEngineMatrix4f projection) {
New to GrepCode? Check out our FAQ X