From 43abb21f8e6b206df0d5b21d6796d9fbbe915d7e Mon Sep 17 00:00:00 2001 From: RCXcrafter Date: Tue, 22 Nov 2016 10:24:58 +0100 Subject: [PATCH] Wip shader stuff --- .../eternalsingularity/proxy/ClientProxy.java | 2 + .../render/CosmicRenderStuffs.java | 116 +++++++++ .../render/EternalItemRenderer.java | 33 ++- .../render/ShaderHelper.java | 8 +- .../eternalsingularity/shader/cosmic.frag | 178 ++++++++++++++ .../eternalsingularity/shader/cosmic.vert | 224 ++++++++++++++++++ 6 files changed, 540 insertions(+), 21 deletions(-) create mode 100644 src/main/java/singulariteam/eternalsingularity/render/CosmicRenderStuffs.java create mode 100644 src/main/resources/assets/eternalsingularity/shader/cosmic.frag create mode 100644 src/main/resources/assets/eternalsingularity/shader/cosmic.vert diff --git a/src/main/java/singulariteam/eternalsingularity/proxy/ClientProxy.java b/src/main/java/singulariteam/eternalsingularity/proxy/ClientProxy.java index 4350452..7e3061f 100644 --- a/src/main/java/singulariteam/eternalsingularity/proxy/ClientProxy.java +++ b/src/main/java/singulariteam/eternalsingularity/proxy/ClientProxy.java @@ -4,6 +4,7 @@ import net.minecraftforge.client.MinecraftForgeClient; import singulariteam.eternalsingularity.item.CombinedSingularityItem; import singulariteam.eternalsingularity.item.EternalSingularityItem; import singulariteam.eternalsingularity.render.EternalItemRenderer; +import singulariteam.eternalsingularity.render.ShaderHelper; public final class ClientProxy extends CommonProxy { @@ -13,5 +14,6 @@ public final class ClientProxy extends CommonProxy EternalItemRenderer fancies = new EternalItemRenderer(); MinecraftForgeClient.registerItemRenderer(EternalSingularityItem.instance, fancies); MinecraftForgeClient.registerItemRenderer(CombinedSingularityItem.instance, fancies); + ShaderHelper.initShaders(); } } diff --git a/src/main/java/singulariteam/eternalsingularity/render/CosmicRenderStuffs.java b/src/main/java/singulariteam/eternalsingularity/render/CosmicRenderStuffs.java new file mode 100644 index 0000000..43f99e4 --- /dev/null +++ b/src/main/java/singulariteam/eternalsingularity/render/CosmicRenderStuffs.java @@ -0,0 +1,116 @@ +package singulariteam.eternalsingularity.render; + +import java.lang.reflect.Field; + +import net.minecraft.client.Minecraft; +import net.minecraft.client.renderer.EntityRenderer; +import net.minecraft.client.renderer.texture.TextureMap; +import net.minecraft.world.World; + +import org.apache.logging.log4j.Level; +import org.lwjgl.opengl.ARBShaderObjects; + +import cpw.mods.fml.relauncher.ReflectionHelper; +import fox.spiteful.avaritia.render.LudicrousRenderEvents; + +public class CosmicRenderStuffs { + public static final ShaderCallback shaderCallback; + + public static float[] lightlevel = new float[3]; + + public static String[] lightmapobf = new String[] {"lightmapColors", "field_78504_Q", "U"}; + public static boolean inventoryRender = false; + public static float cosmicOpacity = 1.0f; + + static { + shaderCallback = new ShaderCallback() { + @Override + public void call(int shader) { + Minecraft mc = Minecraft.getMinecraft(); + + float yaw = 0; + float pitch = 0; + float scale = 1.0f; + + if (!inventoryRender) { + yaw = (float)((mc.thePlayer.rotationYaw * 2 * Math.PI) / 360.0); + pitch = - (float)((mc.thePlayer.rotationPitch * 2 * Math.PI) / 360.0); + } else { + scale = 25.0f; + } + + int x = ARBShaderObjects.glGetUniformLocationARB(shader, "yaw"); + ARBShaderObjects.glUniform1fARB(x, yaw); + + int z = ARBShaderObjects.glGetUniformLocationARB(shader, "pitch"); + ARBShaderObjects.glUniform1fARB(z, pitch); + + int l = ARBShaderObjects.glGetUniformLocationARB(shader, "lightlevel"); + ARBShaderObjects.glUniform3fARB(l, lightlevel[0], lightlevel[1], lightlevel[2]); + + int lightmix = ARBShaderObjects.glGetUniformLocationARB(shader, "lightmix"); + ARBShaderObjects.glUniform1fARB(lightmix, 0.2f); + + int uvs = ARBShaderObjects.glGetUniformLocationARB(shader, "cosmicuvs"); + ARBShaderObjects.glUniformMatrix2ARB(uvs, false, LudicrousRenderEvents.cosmicUVs); + + int s = ARBShaderObjects.glGetUniformLocationARB(shader, "externalScale"); + ARBShaderObjects.glUniform1fARB(s, scale); + + int o = ARBShaderObjects.glGetUniformLocationARB(shader, "opacity"); + ARBShaderObjects.glUniform1fARB(o, cosmicOpacity); + } + }; + } + + public static void useShader() { + ShaderHelper.useShader(ShaderHelper.cosmicShader, shaderCallback); + } + public static void releaseShader() { + ShaderHelper.releaseShader(); + } + + private static Field mapfield = ReflectionHelper.findField(EntityRenderer.class, lightmapobf); + public static void setLightFromLocation(World world, int x, int y, int z) { + if(world == null){ + setLightLevel(1.0f); + return; + } + + int coord = world.getLightBrightnessForSkyBlocks(x, y, z, 0); + + int[] map = null; + try { + map = (int[]) mapfield.get(Minecraft.getMinecraft().entityRenderer); + } catch (Exception e) {} + if (map == null) { + setLightLevel(1.0f); + return; + } + + int mx = (coord % 65536) / 16; + int my = (coord / 65536) / 16; + + int lightcolour = map[my*16+mx]; + + setLightLevel( + 1.0F, + 1.0F, + 1.0F + ); + } + + public static void setLightLevel(float level) { + setLightLevel(level, level, level); + } + public static void setLightLevel(float r, float g, float b) { + lightlevel[0] = Math.max(0.0f, Math.min(1.0f, r)); + lightlevel[1] = Math.max(0.0f, Math.min(1.0f, g)); + lightlevel[2] = Math.max(0.0f, Math.min(1.0f, b)); + } + + public static void bindItemTexture() { + Minecraft mc = Minecraft.getMinecraft(); + mc.renderEngine.bindTexture(TextureMap.locationItemsTexture); + } +} \ No newline at end of file diff --git a/src/main/java/singulariteam/eternalsingularity/render/EternalItemRenderer.java b/src/main/java/singulariteam/eternalsingularity/render/EternalItemRenderer.java index 54ae406..c479fb2 100644 --- a/src/main/java/singulariteam/eternalsingularity/render/EternalItemRenderer.java +++ b/src/main/java/singulariteam/eternalsingularity/render/EternalItemRenderer.java @@ -1,6 +1,5 @@ package singulariteam.eternalsingularity.render; -import fox.spiteful.avaritia.render.CosmicRenderShenanigans; import fox.spiteful.avaritia.render.ICosmicRenderItem; import fox.spiteful.avaritia.render.IHaloRenderItem; import net.minecraft.client.Minecraft; @@ -158,9 +157,9 @@ public class EternalItemRenderer implements IItemRenderer { ICosmicRenderItem icri = (ICosmicRenderItem)(item.getItem()); - CosmicRenderShenanigans.cosmicOpacity = icri.getMaskMultiplier(item, null); - CosmicRenderShenanigans.inventoryRender = true; - CosmicRenderShenanigans.useShader(); + CosmicRenderStuffs.cosmicOpacity = icri.getMaskMultiplier(item, null); + CosmicRenderStuffs.inventoryRender = true; + CosmicRenderStuffs.useShader(); IIcon cosmicicon = icri.getMaskTexture(item, null); @@ -178,8 +177,8 @@ public class EternalItemRenderer implements IItemRenderer { t.addVertexWithUV(16, 0, 0, maxu, minv); t.draw(); - CosmicRenderShenanigans.releaseShader(); - CosmicRenderShenanigans.inventoryRender = false; + CosmicRenderStuffs.releaseShader(); + CosmicRenderStuffs.inventoryRender = false; } GL11.glEnable(GL11.GL_ALPHA_TEST); @@ -231,10 +230,10 @@ public class EternalItemRenderer implements IItemRenderer { f3 = icon.getMaxV(); int colour = item.getItem().getColorFromItemStack(item, i); - r = (float)(colour >> 16 & 255) / 255.0F; - g = (float)(colour >> 8 & 255) / 255.0F; + r = (float)(colour & 255) / 255.0F; + g = (float)(colour & 255) / 255.0F; b = (float)(colour & 255) / 255.0F; - GL11.glColor4f(r, g, b, 1.0F); + GL11.glColor4f(1.0F, 1.0F, 1.0F, 1.0F); ItemRenderer.renderItemIn2D(Tessellator.instance, f1, f2, f, f3, icon.getIconWidth(), icon.getIconHeight(), scale); } @@ -242,8 +241,8 @@ public class EternalItemRenderer implements IItemRenderer { GL11.glDisable(GL11.GL_ALPHA_TEST); GL11.glDepthFunc(GL11.GL_EQUAL); ICosmicRenderItem icri = (ICosmicRenderItem)(item.getItem()); - CosmicRenderShenanigans.cosmicOpacity = icri.getMaskMultiplier(item, player); - CosmicRenderShenanigans.useShader(); + CosmicRenderStuffs.cosmicOpacity = icri.getMaskMultiplier(item, player); + CosmicRenderStuffs.useShader(); IIcon cosmicicon = icri.getMaskTexture(item, player); @@ -252,7 +251,7 @@ public class EternalItemRenderer implements IItemRenderer { float minv = cosmicicon.getMinV(); float maxv = cosmicicon.getMaxV(); ItemRenderer.renderItemIn2D(Tessellator.instance, maxu, minv, minu, maxv, cosmicicon.getIconWidth(), cosmicicon.getIconHeight(), scale); - CosmicRenderShenanigans.releaseShader(); + CosmicRenderStuffs.releaseShader(); GL11.glDepthFunc(GL11.GL_LEQUAL); GL11.glEnable(GL11.GL_ALPHA_TEST); } @@ -268,30 +267,30 @@ public class EternalItemRenderer implements IItemRenderer { case ENTITY : { EntityItem ent = (EntityItem)(data[1]); if (ent != null) { - CosmicRenderShenanigans.setLightFromLocation(ent.worldObj, MathHelper.floor_double(ent.posX), MathHelper.floor_double(ent.posY), MathHelper.floor_double(ent.posZ)); + CosmicRenderStuffs.setLightFromLocation(ent.worldObj, MathHelper.floor_double(ent.posX), MathHelper.floor_double(ent.posY), MathHelper.floor_double(ent.posZ)); } break; } case EQUIPPED : { EntityLivingBase ent = (EntityLivingBase)(data[1]); if (ent != null) { - CosmicRenderShenanigans.setLightFromLocation(ent.worldObj, MathHelper.floor_double(ent.posX), MathHelper.floor_double(ent.posY), MathHelper.floor_double(ent.posZ)); + CosmicRenderStuffs.setLightFromLocation(ent.worldObj, MathHelper.floor_double(ent.posX), MathHelper.floor_double(ent.posY), MathHelper.floor_double(ent.posZ)); } break; } case EQUIPPED_FIRST_PERSON : { EntityLivingBase ent = (EntityLivingBase)(data[1]); if (ent != null) { - CosmicRenderShenanigans.setLightFromLocation(ent.worldObj, MathHelper.floor_double(ent.posX), MathHelper.floor_double(ent.posY), MathHelper.floor_double(ent.posZ)); + CosmicRenderStuffs.setLightFromLocation(ent.worldObj, MathHelper.floor_double(ent.posX), MathHelper.floor_double(ent.posY), MathHelper.floor_double(ent.posZ)); } break; } case INVENTORY: { - CosmicRenderShenanigans.setLightLevel(1.2f); + CosmicRenderStuffs.setLightLevel(1.2f); return; } default : { - CosmicRenderShenanigans.setLightLevel(1.0f); + CosmicRenderStuffs.setLightLevel(1.0f); return; } } diff --git a/src/main/java/singulariteam/eternalsingularity/render/ShaderHelper.java b/src/main/java/singulariteam/eternalsingularity/render/ShaderHelper.java index 8a5e8aa..2eec7ee 100644 --- a/src/main/java/singulariteam/eternalsingularity/render/ShaderHelper.java +++ b/src/main/java/singulariteam/eternalsingularity/render/ShaderHelper.java @@ -1,6 +1,6 @@ package singulariteam.eternalsingularity.render; -import fox.spiteful.avaritia.Lumberjack; +//import fox.spiteful.avaritia.Lumberjack; import net.minecraft.client.Minecraft; import net.minecraft.client.renderer.OpenGlHelper; import org.apache.logging.log4j.Level; @@ -16,7 +16,7 @@ import java.io.InputStreamReader; public final class ShaderHelper { private static final int VERT = ARBVertexShader.GL_VERTEX_SHADER_ARB; private static final int FRAG = ARBFragmentShader.GL_FRAGMENT_SHADER_ARB; - private static final String PREFIX = "/assets/uiesingularities/shader/"; + private static final String PREFIX = "/assets/eternalsingularity/shader/"; public static int cosmicShader = 0; @@ -78,13 +78,13 @@ public final class ShaderHelper { ARBShaderObjects.glLinkProgramARB(program); if(ARBShaderObjects.glGetObjectParameteriARB(program, ARBShaderObjects.GL_OBJECT_LINK_STATUS_ARB) == GL11.GL_FALSE) { - Lumberjack.log(Level.ERROR, getLogInfo(program)); + //Lumberjack.log(Level.ERROR, getLogInfo(program)); return 0; } ARBShaderObjects.glValidateProgramARB(program); if (ARBShaderObjects.glGetObjectParameteriARB(program, ARBShaderObjects.GL_OBJECT_VALIDATE_STATUS_ARB) == GL11.GL_FALSE) { - Lumberjack.log(Level.ERROR, getLogInfo(program)); + //Lumberjack.log(Level.ERROR, getLogInfo(program)); return 0; } diff --git a/src/main/resources/assets/eternalsingularity/shader/cosmic.frag b/src/main/resources/assets/eternalsingularity/shader/cosmic.frag new file mode 100644 index 0000000..fcbef89 --- /dev/null +++ b/src/main/resources/assets/eternalsingularity/shader/cosmic.frag @@ -0,0 +1,178 @@ +#version 130 + +#define M_PI 3.1415926535897932384626433832795 + +const int cosmiccount = 10; +const int cosmicoutof = 101; + +uniform sampler2D texture0; +uniform vec3 lightlevel; + +uniform int time; + +uniform float yaw; +uniform float pitch; +uniform float externalScale; + +uniform float lightmix; +uniform float opacity; + +uniform mat2 cosmicuvs[cosmiccount]; + +varying vec3 position; + +mat4 rotationMatrix(vec3 axis, float angle) +{ + + axis = normalize(axis); + float s = sin(angle); + float c = cos(angle); + float oc = 1.0 - c; + + return mat4(oc * axis.x * axis.x + c, oc * axis.x * axis.y - axis.z * s, oc * axis.z * axis.x + axis.y * s, 0.0, + oc * axis.x * axis.y + axis.z * s, oc * axis.y * axis.y + c, oc * axis.y * axis.z - axis.x * s, 0.0, + oc * axis.z * axis.x - axis.y * s, oc * axis.y * axis.z + axis.x * s, oc * axis.z * axis.z + c, 0.0, + 0.0, 0.0, 0.0, 1.0); +} + +void main (void) +{ + vec4 light = gl_Color; + vec4 mask = texture2D(texture0, gl_TexCoord[0].xy); + light.rgb *= lightlevel; + + float oneOverExternalScale = 1.0/externalScale; + + int uvtiles = 16; + + // background colour + vec4 col = vec4(0.1,0.0,0.0,1.0); + + float pulse = mod(time,400)/400.0; + + col.g = sin(pulse*M_PI*2) * 0.075 + 0.225; + col.b = cos(pulse*M_PI*2) * 0.05 + 0.3; + + // get ray from camera to fragment + vec4 dir = normalize(vec4( -position, 0)); + + // rotate the ray to show the right bit of the sphere for the angle + float sb = sin(pitch); + float cb = cos(pitch); + dir = normalize(vec4(dir.x, dir.y * cb - dir.z * sb, dir.y * sb + dir.z * cb, 0)); + + float sa = sin(-yaw); + float ca = cos(-yaw); + dir = normalize(vec4(dir.z * sa + dir.x * ca, dir.y, dir.z * ca - dir.x * sa, 0)); + + vec4 ray; + + // draw the layers + for (int i=0; i<16; i++) { + int mult = 16-i; + + // get semi-random stuff + int j = i + 7; + float rand1 = (j * j * 4321 + j * 8) * 2.0F; + int k = j + 1; + float rand2 = (k * k * k * 239 + k * 37) * 3.6F; + float rand3 = rand1 * 347.4 + rand2 * 63.4; + + // random rotation matrix by random rotation around random axis + vec3 axis = normalize(vec3(sin(rand1), sin(rand2) , cos(rand3))); + + // apply + ray = dir * rotationMatrix(axis, mod(rand3, 2*M_PI)); + + // calcuate the UVs from the final ray + float rawu = 0.5 + (atan(ray.z,ray.x)/(2*M_PI)); + float rawv = 0.5 + (asin(ray.y)/M_PI); + + // get UV scaled for layers and offset by time; + float scale = mult*0.5 + 2.75; + float u = rawu * scale * externalScale; + //float v = (rawv + time * 0.00006) * scale * 0.6; + float v = (rawv + time * 0.0002 * oneOverExternalScale) * scale * 0.6 * externalScale; + + vec2 tex = vec2( u, v ); + + // tile position of the current uv + int tu = int(mod(floor(u*uvtiles),uvtiles)); + int tv = int(mod(floor(v*uvtiles),uvtiles)); + + // get pseudorandom variants + int position = ((1777541 * tu) + (7649689 * tv) + (3612703 * (i+31)) + 1723609 ) ^ 50943779; + int symbol = int(mod(position, cosmicoutof)); + int rotation = int(mod(pow(tu,float(tv)) + tu + 3 + tv*i, 8)); + bool flip = false; + if (rotation >= 4) { + rotation -= 4; + flip = true; + } + + // if it's an icon, then add the colour! + if (symbol >= 0 && symbol < cosmiccount) { + + vec2 cosmictex = vec2(1.0,1.0); + vec4 tcol = vec4(1.0,0.0,0.0,1.0); + + // get uv within the tile + float ru = clamp(mod(u,1.0)*uvtiles - tu, 0.0, 1.0); + float rv = clamp(mod(v,1.0)*uvtiles - tv, 0.0, 1.0); + + if (flip) { + ru = 1.0 - ru; + } + + float oru = ru; + float orv = rv; + + // rotate uvs if necessary + if (rotation == 1) { + oru = 1.0-rv; + orv = ru; + } else if (rotation == 2) { + oru = 1.0-ru; + orv = 1.0-rv; + } else if (rotation == 3) { + oru = rv; + orv = 1.0-ru; + } + + // get the iicon uvs for the tile + float umin = cosmicuvs[symbol][0][0]; + float umax = cosmicuvs[symbol][1][0]; + float vmin = cosmicuvs[symbol][0][1]; + float vmax = cosmicuvs[symbol][1][1]; + + // interpolate based on tile uvs + cosmictex.x = umin * (1.0-oru) + umax * oru; + cosmictex.y = vmin * (1.0-orv) + vmax * orv; + + tcol = texture2D(texture0, cosmictex); + + // set the alpha, blending out at the bunched ends + float a = tcol.r * (0.5 + (1.0/mult) * 1.0) * (1.0-smoothstep(0.15, 0.48, abs(rawv-0.5))); + + // get fancy colours + float r = (mod(rand1, 29.0)/29.0) * 0.3 + 0.4; + float g = (mod(rand2, 35.0)/35.0) * 0.4 + 0.6; + float b = (mod(rand1, 17.0)/17.0) * 0.3 + 0.7; + + // mix the colours + //col = col*(1-a) + vec4(r,g,b,1)*a; + col = col + vec4(r,g,b,1)*a; + } + } + + // apply lighting + vec3 shade = light.rgb * (lightmix) + vec3(1.0-lightmix,1.0-lightmix,1.0-lightmix); + col.rgb *= shade; + + // apply mask + col.a *= mask.r * opacity; + + col = clamp(col,0.0,1.0); + + gl_FragColor = col; +} \ No newline at end of file diff --git a/src/main/resources/assets/eternalsingularity/shader/cosmic.vert b/src/main/resources/assets/eternalsingularity/shader/cosmic.vert new file mode 100644 index 0000000..9b319d5 --- /dev/null +++ b/src/main/resources/assets/eternalsingularity/shader/cosmic.vert @@ -0,0 +1,224 @@ +#version 120 + +vec4 Ambient; +vec4 Diffuse; +vec4 Specular; + +attribute float activelights; + +varying vec3 position; + +void pointLight(in int i, in vec3 normal, in vec3 eye, in vec3 ecPosition3) +{ + float nDotVP; // normal . light direction + float nDotHV; // normal . light half vector + float pf; // power factor + float attenuation; // computed attenuation factor + float d; // distance from surface to light source + vec3 VP; // direction from surface to light position + vec3 halfVector; // direction of maximum highlights + + // Compute vector from surface to light position + VP = vec3 (gl_LightSource[i].position) - ecPosition3; + + // Compute distance between surface and light position + d = length(VP); + + // Normalize the vector from surface to light position + VP = normalize(VP); + + // Compute attenuation + attenuation = 1.0 / (gl_LightSource[i].constantAttenuation + + gl_LightSource[i].linearAttenuation * d + + gl_LightSource[i].quadraticAttenuation * d * d); + + halfVector = normalize(VP + eye); + + nDotVP = max(0.0, dot(normal, VP)); + //nDotHV = max(0.0, dot(normal, halfVector)); + + //if (nDotVP == 0.0) + //{ + pf = 0.0; + //} + //else + //{ + // pf = pow(nDotHV, gl_FrontMaterial.shininess); + //} + Ambient += gl_LightSource[i].ambient * attenuation; + Diffuse += gl_LightSource[i].diffuse * nDotVP * attenuation; + //Specular += gl_LightSource[i].specular * pf * attenuation; +} + +void spotLight(in int i, in vec3 normal, in vec3 eye, in vec3 ecPosition3) +{ + float nDotVP; // normal . light direction + float nDotHV; // normal . light half vector + float pf; // power factor + float spotDot; // cosine of angle between spotlight + float spotAttenuation; // spotlight attenuation factor + float attenuation; // computed attenuation factor + float d; // distance from surface to light source + vec3 VP; // direction from surface to light position + vec3 halfVector; // direction of maximum highlights + + // Compute vector from surface to light position + VP = vec3 (gl_LightSource[i].position) - ecPosition3; + + // Compute distance between surface and light position + d = length(VP); + + // Normalize the vector from surface to light position + VP = normalize(VP); + + // Compute attenuation + attenuation = 1.0 / (gl_LightSource[i].constantAttenuation + + gl_LightSource[i].linearAttenuation * d + + gl_LightSource[i].quadraticAttenuation * d * d); + + // See if point on surface is inside cone of illumination + spotDot = dot(-VP, normalize(gl_LightSource[i].spotDirection)); + + if (spotDot < gl_LightSource[i].spotCosCutoff) + { + spotAttenuation = 0.0; // light adds no contribution + } + else + { + spotAttenuation = pow(spotDot, gl_LightSource[i].spotExponent); + + } + // Combine the spotlight and distance attenuation. + attenuation *= spotAttenuation; + + halfVector = normalize(VP + eye); + + nDotVP = max(0.0, dot(normal, VP)); + //nDotHV = max(0.0, dot(normal, halfVector)); + + //if (nDotVP == 0.0) + //{ + pf = 0.0; + //} + //else + //{ +// pf = pow(nDotHV, gl_FrontMaterial.shininess); +// + // } + Ambient += gl_LightSource[i].ambient * attenuation; + Diffuse += gl_LightSource[i].diffuse * nDotVP * attenuation; + //Specular += gl_LightSource[i].specular * pf * attenuation; + +} + +void directionalLight(in int i, in vec3 normal) +{ + float nDotVP; // normal . light direction + float nDotHV; // normal . light half vector + float pf; // power factor + + nDotVP = max(0.0, dot(normal, normalize(vec3 (gl_LightSource[i].position)))); + //nDotHV = max(0.0, dot(normal, vec3 (gl_LightSource[i].halfVector))); + + //if (nDotVP == 0.0) + //{ + pf = 0.0; + //} + //else + //{ +// pf = pow(nDotHV, gl_FrontMaterial.shininess); + //} + Ambient += gl_LightSource[i].ambient; + Diffuse += gl_LightSource[i].diffuse * nDotVP; + //Specular += gl_LightSource[i].specular * pf; +} + +vec3 fnormal(void) +{ + //Compute the normal + vec3 normal = gl_NormalMatrix * gl_Normal; + normal = normalize(normal); + return normal; +} + +void ProcessLight(in int i, in vec3 normal, in vec3 eye, in vec3 ecPosition3) +{ + if (gl_LightSource[i].spotCutoff==180.0) + { + if (gl_LightSource[i].position.w==0.0) + { + directionalLight(i, normal); + } + else + { + pointLight(i, normal, eye, ecPosition3); + } + } + else + { + spotLight(i,normal,eye,ecPosition3); + } +} + +void flight(in vec3 normal, in vec4 ecPosition, float alphaFade) +{ + vec4 color; + vec3 ecPosition3; + vec3 eye; + int i; + + ecPosition3 = (vec3 (ecPosition)) / ecPosition.w; + eye = vec3 (0.0, 0.0, 1.0); + + // Clear the light intensity accumulators + Ambient = vec4 (0.0); + Diffuse = vec4 (0.0); + Specular = vec4 (0.0); + + if (activelights>0) + { + ProcessLight(0,normal,eye,ecPosition3); + } + if (activelights>1) + { + ProcessLight(1,normal,eye,ecPosition3); + } + //if (activelights>2) + //{ + // ProcessLight(2,normal,eye,ecPosition3); + //} + //if (activelights>3) + //{ + // ProcessLight(3,normal,eye,ecPosition3); + //} + + color = gl_FrontLightModelProduct.sceneColor + + Ambient * gl_FrontMaterial.ambient + + Diffuse * gl_FrontMaterial.diffuse; + color += Specular * gl_FrontMaterial.specular; + color = clamp( color, 0.0, 1.0 ); + gl_FrontColor = color; + gl_FrontColor.a *= alphaFade; +} + +void main (void) +{ + vec3 transformedNormal; + float alphaFade = 1.0; + + // Eye-coordinate position of vertex, needed in various calculations + vec4 ecPosition = gl_ModelViewMatrix * gl_Vertex; + + // Do fixed functionality vertex transform + gl_Position = ftransform(); + transformedNormal = fnormal(); + flight(transformedNormal, ecPosition, alphaFade); + + //Enable texture coordinates + gl_TexCoord[0] = gl_MultiTexCoord0; + //gl_TexCoord[1] = gl_MultiTexCoord1; + //gl_TexCoord[2] = gl_MultiTexCoord2; + //gl_TexCoord[3] = gl_MultiTexCoord3; + + position = (gl_ModelViewMatrix * gl_Vertex).xyz; +} \ No newline at end of file