Hello Guest

Really funky dragon rendered using OpenGL :D

  • 1 Replies
  • 6604 Views
Really funky dragon rendered using OpenGL :D
« on: May 03, 2018, 01:46:03 »
Here's the funky dragon http://prntscr.com/jd0fla.

Vertex Code:
Code: [Select]
#version 400 core

layout (location = 0) in vec3 vertPos;
layout (location = 1) in vec2 texCords;
layout (location = 2) in vec3 normals;

out vec2 passTexCords;
out vec3 vNormal;
out vec3 lightPointerVector;

//Matrices
uniform mat4 projMatrix;
uniform mat4 modelMatrix;
uniform mat4 viewMatrix;

//Light stuff
uniform vec3 lightPos;

void main() {
    vec4 worldMatrix = modelMatrix * vec4(vertPos, 1.0);

    gl_Position = projMatrix * viewMatrix  * worldMatrix;
    passTexCords = texCords;

    vNormal = (modelMatrix * vec4(normals, 0.0)).xyz;
    lightPointerVector = lightPos - worldMatrix.xyz;
}

Fragment Code:
Code: [Select]
#version 400 core

in vec2 passTexCords;

in vec3 vNormal;
in vec3 lightPointerVector;

out vec4 result;

//Light stuff
uniform vec3 lightColor;
uniform float brightnessMultiplier;

//Others
uniform vec3 diffuseColor;
uniform sampler2D texSampler;

void main() {
    vec3 uNormal = normalize(vNormal);
    vec3 uLPV = normalize(lightPointerVector);

    //Dot Product
    float nDot = dot(uNormal, uLPV);
    //Luminosidade final da luz.
    float brightness = max(nDot, 0.0);

    vec3 brightColor = brightness * lightColor;

     vec4 texColor = texture(texSampler, passTexCords);
     result = vec4(brightnessMultiplier *  (diffuseColor + brightColor), 1.0) * texColor;
}

Material File:

Code: [Select]
## AEGIS Engine Material v1.0 ##

materialDiffuseColor=0.25,0.0,0.25
materialSpecularColor=1.0,1.0,1.0

materialDiffuseTexture=data/textures/dragon_tex.png

materialReflectivity=0.5
materialShininess=0.5
materialShineDamper=1.0
materialBrightnessMultiplier=2.5


Material Loader Method:
Code: [Select]
public ModelMaterial loadModelMaterial(String materialPath) {
        File f = new File(materialPath);
        if(!f.exists()) {
            throw new RuntimeException("Material '" + materialPath + "' nao encontrado!");
        } else {
            float dcX = 0, dcY = 0, dcZ = 0;                //Diffuse Color
            float scX = 0, scY = 0, scZ = 0;                //Specular Color
            float matRef = 0, matShine = 0, matShini = 0;   //Material Reflectivity, ShineDamper, Shininess.
            float matBrightMul = 0;
            String dTexture = null;                         //Diffuse Texture

            try (BufferedReader br = new BufferedReader(new FileReader(f))) {
                String line;
                while((line = br.readLine()) != null) {
                    if(line.startsWith("#"))
                        continue;

                    if(!line.contains("="))
                        continue;

                    String[] linei = line.split("=");
                    if(linei[0].contains("DiffuseColor")) {
                        String[] args = linei[1].split(",");
                        if(args.length != 3)
                            throw new RuntimeException("Erro ao carregar material! Argumentos invalidos" +
                                    "para o parametro 'DiffuseColor'!");

                        dcX = Float.parseFloat(args[0]);
                        dcY = Float.parseFloat(args[1]);
                        dcZ = Float.parseFloat(args[2]);
                    } else if(linei[0].contains("SpecularColor")) {
                        String[] args = linei[1].split(",");
                        if(args.length != 3)
                            throw new RuntimeException("Erro ao carregar material! Argumentos invalidos" +
                                    "para o parametro 'SpecularColor'!");

                        scX = Float.parseFloat(args[0]);
                        scY = Float.parseFloat(args[1]);
                        scZ = Float.parseFloat(args[2]);
                    } else if(linei[0].contains("DiffuseTexture")) {
                        dTexture = linei[1];
                    } else if(linei[0].contains("Reflectivity")) {
                        matRef = Float.parseFloat(linei[1]);
                    }  else if(linei[0].contains("Shininess")) {
                        matShini = Float.parseFloat(linei[1]);
                    } else if(linei[0].contains("ShineDamper")) {
                        matShine = Float.parseFloat(linei[1]);
                    } else if(linei[0].contains("BrightnessMultiplier")) {
                        matBrightMul = Float.parseFloat(linei[1]);
                    }
                }
            } catch (IOException ex) {ex.printStackTrace();}

            System.out.println(dcX + ", " + dcY + ", " + dcZ);
            System.out.println(scX + ", " + scY + ", " + scZ);
            System.out.println("Reflectivity: " + matRef);
            System.out.println("Shine Damper: " + matShine);
            System.out.println("Shininess: " + matShini);
            System.out.println("Brightness Multiplier: " + matBrightMul);
            System.out.println("Diffuse Texture: " + dTexture);

            Vector3f diffuseColor = new Vector3f(dcX, dcY, dcZ);
            Vector3f specularColor = new Vector3f(scX, scY, scZ);
            ModelTexture diffuseTexture;

            if(dTexture.equals("null")) {
                return new ModelMaterial(diffuseColor, specularColor, null, matRef, matShini, matShine, matBrightMul);
            } else {
                diffuseTexture = TextureLoader.loadTexture(dTexture);
                return new ModelMaterial(diffuseColor, specularColor, diffuseTexture, matRef, matShini, matShine, matBrightMul);
            }
        }
    }

ModelMaterial class code:
Code: [Select]

public class ModelMaterial {
    private Vector3f diffuseColor, specularColor;
    private ModelTexture diffuseTexture;
    private float reflectivity, shininess, shineDamper;
    private float brightnessMultiplier;

    public ModelMaterial(Vector3f diffuseColor, Vector3f specularColor, ModelTexture diffuseTexture, float reflectivity,
                         float shininess, float shineDamper, float brightnessMultiplier) {
        this.diffuseColor = diffuseColor;
        this.specularColor = specularColor;
        this.diffuseTexture = diffuseTexture;
        this.reflectivity = reflectivity;
        this.shininess = shininess;
        this.shineDamper = shineDamper;
        this.brightnessMultiplier = brightnessMultiplier;

        if(diffuseTexture == null) {
            this.diffuseTexture = TextureLoader.loadTexture("data/textures/missing_texture.png");
        }
    }

   // Getters and Setters for all fields

}


Enjoy :D

Re: Really funky dragon rendered using OpenGL :D
« Reply #1 on: May 23, 2018, 01:19:06 »
I also want a dragon of my own, I will try those code to see if I can create one.