Author Topic: Shaders for the MediaVPs  (Read 42953 times)

0 Members and 1 Guest are viewing this topic.

Offline The E

  • He's Ebeneezer Goode
  • 213
  • Nothing personal, just tech support.
    • Steam
    • Twitter
Shaders for the MediaVPs
As those of you on IRC are aware, Zacam and I are working on new Shaders for the mediavps. While we are somewhat satisfied with the results so far, I wonder if anyone else here would be able and interested to work on these things. So, if you know GLSL, here's your chance to impress us. We're looking for Shaders that are preferably compatible with Shader Model 2, that can do per-pixel lighting, and that can process a maximum number of lights, as well as use all the textures FSO uses.

Technical stuff:
These Shaders should be used and tested with Antipodes 6.
The following information is passed to shaders by the engine:

  • Preprocessor Directives:
    • FLAG_LIGHT -- If lighting is enabled
    • FLAG_DIFFUSE_MAP -- If diffuse maps are enabled
    • FLAG_GLOW_MAP -- Glow maps
    • FLAG_SPEC_MAP -- Spec maps
    • FLAG_ENV_MAP -- Environment mapping
    • FLAG_NORMAL_MAP -- Normal maps
    • FLAG_HEIGHT_MAP -- Height maps
    • FLAG_FOG -- Fog rendering (think nebulae)
    • SHADER_MODEL (antipodes 6 only) -- Has a value of 2, 3 or 4, depending on what the driver reports
  • Uniforms:
    • int n_lights -- Number of lights in the current scene (Only present if FLAG_LIGHT is set)
    • sampler2D sBasemap -- The diffuse map (only present with FLAG_DIFFUSE_MAP)
    • sampler2D sGlowmap -- The Glowmap (linked to FLAG_GLOW_MAP)
    • sampler2D sSpecmap -- The specular map (linked to FLAG_SPEC_MAP)
    • samplerCube sEnvmap -- The environment map (FLAG_ENV_MAP)
    • bool alpha_spec -- whether or not the spec map has an alpha channel (FLAG_ENV_MAP)
    • mat4 envMatrix -- Environment matrix (FLAG_ENV_MAP)
    • sampler2D sNormalmap -- Normal map (FLAG_NORMAL_MAP)
    • sampler2D sHeightmap -- Height map (FLAG_HEIGHT_MAP)

The resulting shader should be saved as main-f.sdr for the fragment, and main-v.sdr for the vertex shader.

Note: Shader Model 2 compatibility is entirely optional.
« Last Edit: December 21, 2010, 02:20:37 pm by Zacam »
If I'm just aching this can't go on
I came from chasing dreams to feel alone
There must be changes, miss to feel strong
I really need lifе to touch me
--Evergrey, Where August Mourns

 

Offline Woolie Wool

  • 211
  • Fire main batteries
Re: Shaders for the MediaVPs
This is exciting! Will SSAO be eventually included? Because that would be badass.
16:46   Quanto   ****, a mosquito somehow managed to bite the side of my palm
16:46   Quanto   it itches like hell
16:46   Woolie   !8ball does Quanto have malaria
16:46   BotenAnna   Woolie: The outlook is good.
16:47   Quanto   D:

"did they use anesthetic when they removed your sense of humor or did you have to weep and struggle like a tiny baby"
--General Battuta

 

Offline The E

  • He's Ebeneezer Goode
  • 213
  • Nothing personal, just tech support.
    • Steam
    • Twitter
Re: Shaders for the MediaVPs
Not planned at the moment.

However, I'd like you people to give these shaders here a try: http://blueplanet.fsmods.net/E/effects_ppl2.7z

Unpack them, and place the .sdr files in mediavps_3612\data\effects.


As an example, here's the shader source. Code is based on a concept by KeldorKatarn, tweaked to fit in with the antipodes builds by me and Zacam.

main-v.sdr:
Code: [Select]
#ifdef FLAG_ENV_MAP
uniform mat4 envMatrix;
varying vec3 envReflect;
#endif

#ifdef FLAG_NORMAL_MAP
varying mat3 tbnMatrix;
#endif

#ifdef FLAG_FOG
varying float fogDist;
#endif

varying vec4 position;
varying vec3 lNormal;

void main()
{
gl_TexCoord[0] = gl_MultiTexCoord0;
gl_Position = ftransform();

 // Transform the normal into eye space and normalize the result.
position = gl_ModelViewMatrix * gl_Vertex;
vec3 normal = normalize(gl_NormalMatrix * gl_Normal);
lNormal = normal;

 #ifdef FLAG_NORMAL_MAP
 // Setup stuff for normal maps
vec3 t = normalize(gl_NormalMatrix * gl_MultiTexCoord1.xyz);
vec3 b = cross(normal, t) * gl_MultiTexCoord1.w;
tbnMatrix = mat3(t, b, normal);
 #endif

 #ifdef FLAG_ENV_MAP
 // Environment mapping reflection vector.
envReflect = reflect(position.xyz, normal);
envReflect = vec3(envMatrix * vec4(envReflect, 0.0));
envReflect = normalize(envReflect);
 #endif

gl_FrontColor = gl_Color;

 #ifdef FLAG_FOG
fogDist = clamp((gl_Position.z - gl_Fog.start) * 0.75 * gl_Fog.scale, 0.0, 1.0);
 #endif

 #ifdef __GLSL_CG_DATA_TYPES
 // Check necessary for ATI specific behavior
gl_ClipVertex = (gl_ModelViewMatrix * gl_Vertex);
 #endif
}

main-f.sdr:
Code: [Select]
#ifdef FLAG_LIGHT
uniform int n_lights;
#endif

#ifdef FLAG_DIFFUSE_MAP
uniform sampler2D sBasemap;
#endif

#ifdef FLAG_GLOW_MAP
uniform sampler2D sGlowmap;
#endif

#ifdef FLAG_SPEC_MAP
uniform sampler2D sSpecmap;
#endif

#ifdef FLAG_ENV_MAP
uniform samplerCube sEnvmap;
uniform bool alpha_spec;
varying vec3 envReflect;
#endif

#ifdef FLAG_NORMAL_MAP
uniform sampler2D sNormalmap;
varying mat3 tbnMatrix;
#endif

#ifdef FLAG_FOG
varying float fogDist;
#endif

varying vec4 position;
varying vec3 lNormal;

#if SHADER_MODEL == 2
  #define MAX_LIGHTS 2
#else
  #define MAX_LIGHTS 8
#endif

void main()
{
float attenuation;
float NdotL;
float NdotHV;
vec3 halfV;
vec3 lightDir;

vec3 eyeDir = vec3(normalize(-position).xyz); // Camera is at (0,0,0) in ModelView space
vec4 lightAmbientDiffuse = vec4(0.0, 0.0, 0.0, 1.0);
vec4 lightDiffuse = vec4(0.0, 0.0, 0.0, 1.0);
vec4 lightAmbient = gl_FrontMaterial.emission + (gl_LightModel.ambient * gl_FrontMaterial.ambient);
vec4 lightSpecular = vec4(0.0, 0.0, 0.0, 1.0);

 #ifdef FLAG_LIGHT
  #ifdef FLAG_NORMAL_MAP
// Normal map - convert from DXT5nm
vec3 normal;
normal.rg = (texture2D(sNormalmap, gl_TexCoord[0].xy).ag * 2.0) - 1.0;
normal.b = sqrt(1.0 - dot(normal.rg, normal.rg));
normal = normalize(tbnMatrix * normal);
  #else
vec3 normal = lNormal;
  #endif

#pragma optionNV unroll all
for (int i = 0; i < MAX_LIGHTS; ++i) {
  #if SHADER_MODEL > 2
if (i > n_lights)
break;
  #endif
float specularIntensity = 1.0;

// Attenuation and light direction
if (gl_LightSource[i].position.w != 0.0 && i == 0) {
// Positional light source
float dist = distance(gl_LightSource[i].position.xyz, position.xyz);

float spotEffect = 1.0;

if (gl_LightSource[i].spotCutoff < 91.0) {
spotEffect = dot(normalize(gl_LightSource[i].spotDirection), normalize(-position.xyz));

if (spotEffect < gl_LightSource[i].spotCosCutoff) {
spotEffect = 0.0;
}
}

attenuation = spotEffect / (gl_LightSource[i].constantAttenuation + (gl_LightSource[i].linearAttenuation * dist) + (gl_LightSource[i].quadraticAttenuation * dist) * dist);

lightDir = normalize(gl_LightSource[i].position.xyz - position.xyz);

specularIntensity = 8.6; // Point light
} else {
// Directional light source
attenuation = 1.0;
lightDir = normalize(gl_LightSource[i].position.xyz);

specularIntensity = 3.0; // Directional light
}

// Ambient and Diffuse
NdotL = max(dot(normal, lightDir), 0.0);
if (NdotL > 0.0)
lightAmbient += (gl_FrontLightProduct[i].ambient * attenuation);

lightDiffuse += ((gl_FrontLightProduct[i].diffuse * NdotL) * attenuation);

// Specular
halfV = normalize(eyeDir + lightDir);
NdotHV = clamp(dot(normal, halfV), 0.0, 1.0);

lightSpecular += ((gl_FrontLightProduct[i].specular * pow(NdotHV, gl_FrontMaterial.shininess)) * attenuation) * specularIntensity;
}

lightAmbientDiffuse = lightAmbient + lightDiffuse;
 #else
lightAmbientDiffuse = gl_Color;
lightSpecular = gl_SecondaryColor;
 #endif

 #ifdef FLAG_DIFFUSE_MAP
 // Base color
vec4 baseColor = gl_Color * texture2D(sBasemap, gl_TexCoord[0].xy);
 #else
vec4 baseColor = gl_Color;
 #endif

vec4 fragmentColor = baseColor * max(lightAmbientDiffuse, gl_LightModel.ambient - 0.425);
fragmentColor.a = baseColor.a + (dot(lightSpecular, lightSpecular) * 0.1);

 #ifdef FLAG_SPEC_MAP
 // Spec color
float specularFactor = 1.75;
fragmentColor.rgb += specularFactor * (texture2D(sSpecmap, gl_TexCoord[0].xy).rgb * lightSpecular.rgb);
 #endif

 #ifdef FLAG_ENV_MAP
 // Env color
vec3 envIntensity = (alpha_spec) ? vec3(texture2D(sSpecmap, gl_TexCoord[0].xy).a) : texture2D(sSpecmap, gl_TexCoord[0].xy).rgb;
fragmentColor.a += dot(textureCube(sEnvmap, envReflect).rgb, textureCube(sEnvmap, envReflect).rgb) * 0.4;
fragmentColor.rgb += textureCube(sEnvmap, envReflect).rgb * envIntensity;
 #endif

 #ifdef FLAG_GLOW_MAP
 // Glow color
fragmentColor.rgb += texture2D(sGlowmap, gl_TexCoord[0].xy).rgb * 1.5;
 #endif

 #ifdef FLAG_FOG
fragmentColor.rgb = mix(fragmentColor.rgb, gl_Fog.color.rgb, fogDist);
 #endif

gl_FragColor = fragmentColor;
}
« Last Edit: October 06, 2010, 02:33:50 am by The E »
If I'm just aching this can't go on
I came from chasing dreams to feel alone
There must be changes, miss to feel strong
I really need lifе to touch me
--Evergrey, Where August Mourns

 

Offline Raven2001

  • Machina Terra Reborn
  • 211
  • Im not the droid your looking for, move along
Re: Shaders for the MediaVPs
More shaders! Sounds great!

Could you give a rough description of what those shaders are supposed to do, for those of us who are crap at understanding code? :)
Yeah, I know you were waiting for a very nice sig, in which I was quoting some very famous scientist or philosopher... guess what?!? I wont indulge you...

Why, you ask? What, do I look like a Shivan to you?!?


Raven is a god.

 

Offline The E

  • He's Ebeneezer Goode
  • 213
  • Nothing personal, just tech support.
    • Steam
    • Twitter
Re: Shaders for the MediaVPs
Well, basically, Shaders are programs that run on your GPU that are responsible for producing pretty pictures from raw data. The original mediavps shaders are built to emulate the fixed render pipeline as closely as possible (with the addition of normal maps, obviously).

These shaders, on the other hand, move the lighting processing from a per-Vertex to a per-Pixel model. Previously, lighting values were only generated for every vertex in the on-screen geometry and then interpolated between adjacent vertices. These ones process lighting much more accurately. In addition, the previous shaders only did normal mapping calculations for the strongest light in the scene, these ones use normal maps for all lights.

Now, to get these working on SM2-level cards, it was unfortunately necessary to cut down on the number of lights being processed. On SM3 or better cards, these shaders will render all 8 lights, on SM2, only the 2 strongest ones are used. That's the point I would like to see addressed; the more lights we can get on SM2, the better (with the added hope that any optimizations done for SM2 will allow these thingies to run faster on SM3 as well).

But the point of this topic is for others to post shaders as well; just to get some variety going.
« Last Edit: October 06, 2010, 06:05:18 am by The E »
If I'm just aching this can't go on
I came from chasing dreams to feel alone
There must be changes, miss to feel strong
I really need lifе to touch me
--Evergrey, Where August Mourns

 

Offline Kolgena

  • 211
Re: Shaders for the MediaVPs
Sounds impressive. (I too, know jack all about code, though I understand a bit of the theory of how shaders work)

What kind of performance are we looking to get from these shaders? (Also, how do the ones you just posted perform compared to current ones?)

 

Offline The E

  • He's Ebeneezer Goode
  • 213
  • Nothing personal, just tech support.
    • Steam
    • Twitter
Re: Shaders for the MediaVPs
More accurate lighting means more math needed, as a result, these might be a bit slower. I haven't done any benchmarking on them, but the performance hit should be minimal on modern hardware.
« Last Edit: October 06, 2010, 09:25:45 am by The E »
If I'm just aching this can't go on
I came from chasing dreams to feel alone
There must be changes, miss to feel strong
I really need lifе to touch me
--Evergrey, Where August Mourns

 

Offline Kolgena

  • 211
Re: Shaders for the MediaVPs
I just tested them in massivebattle2. Lighting is much more detailed now, especially in the way the cap ships respond to explosion lights. They're no longer awash in yellow with no normal map detail.

Flicker as the engine decides which lights to draw is pretty bad though, and isn't something my screenshots can capture. It's the same as it was before, but since the lighting is much more vivid, it becomes more obvious. If there's a way, it'd be nice to fix the behavior now that we're tweaking light rendering anyways. Part of it is probably due to my lighting settings, which aren't customized around the new lighting behavior. It makes the shivan stuff look too shiny and something like crinkled tinfoil.

Screenshots were taken after Orion and Hecate died. That means that all green lights are prometheus, all purple lights are subach. Orange is either flak or exploding fighters. White is the sun.

-fov 0.60 -spec_exp 4.5 -spec_tube 1.8 -spec_point 2.2 -spec_static 3 -ambient_factor 105 -ogl_spec 85 -no_emissive_light -bloom_intensity 80

Overall, it's a huge improvement. In less busy "real" missions, the lighting should appear less buggy than it is.

Edit: You've probably come across this: http://www.opengl.org/registry/specs/ARB/shadow_ambient.txt. Can we expect shadows as an eventuality of your work? :D

[attachment deleted by admin]
« Last Edit: October 06, 2010, 09:47:16 am by Kolgena »

 

Offline Zacam

  • Magnificent Bastard
  • Administrator
  • 211
  • I go Sledge-O-Matic on Spammers
    • Minecraft
    • Steam
    • Twitter
    • ModDB Feature
Re: Shaders for the MediaVPs
Shadowing and/or Ambient Occlusion are planned (at least by me) to be in the works at some point.

If we can get a shader library developed for the shaders to access rather than being limited by Vertex->Fragment (as not all GPU OpenGL compilers will support passing back from Fragment->Vertex) then this means we can see more lights by building our own light structures and then passing those. Then it can select to either run some lights in the Vertex shader, and the rest in the fragment shader. This might also allow for more lights on SM2.0 due to execution limits, but I'm not sure (it's been a long time since dealing with non-pooled unified arch)

Why our own light's structure? Well, I have a theory that says the reason why the normal mapping didn't work on the old shaders on ATi cards is 22-fold: exposure to n_lights from the engine (though upon reflection, I'm wrong on that one) and the fact that under the old shaders, -normal was only applying to a fixed gl_LightSource, which may or may not be created.

Additionally, we need to look at building our own structures as several things in the existing shaders are deprecated, which forces relying on compatibility being present. If you look at the above concept shaders, we had to add a "#pragma optionNV unroll all" so that an nVidia card running the 2xx.xx series drives could actually use the light loop without throwing a relative-addressedarrays error to the Cg Compiler. We shouldn't _have_ to be relying on a #pragma option that in itself could end up disappearing on us.

But to address your screenshots Kolgena, those are pretty nice. And yes, there is a Specular highlighting issue with weapons fire reflectivity as it also apparently has some creation "hang-time" (it seems to highlights for the lifetime of the effect rather than by proximity to surface) which is another thing that could be addressed by defining our own structures in a library system.

In regards to the specular specifically:
Code: (main-f.sdr) [Select]
specularIntensity = 8.6; // Point light
specularIntensity = 3.0; // Directional light

If you set those two to 4.3 and 1.5 respectively, you'll get a near perfect Spec amount as the existing shaders, but the highlighting effect "hang-time" will still persist.

I personally use:
-bloom_intensity 90 -ambient_factor 35 -no_emissive_light -spec_exp 11 -spec_point 0.6 -spec_static 0.8 -spec_tube 0.4 -ogl_spec 30
« Last Edit: October 06, 2010, 11:52:16 am by Zacam »
Report MediaVP issues, now on the MediaVP Mantis! Read all about it Here!
Talk with the community on Discord
"If you can keep a level head in all this confusion, you just don't understand the situation"

¤[D+¬>

[08/01 16:53:11] <sigtau> EveningTea: I have decided that I am a 32-bit registerkin.  Pronouns are eax, ebx, ecx, edx.
[08/01 16:53:31] <EveningTea> dhauidahh
[08/01 16:53:32] <EveningTea> sak
[08/01 16:53:40] * EveningTea froths at the mouth
[08/01 16:53:40] <sigtau> i broke him, boys

 

Offline Kolgena

  • 211
Re: Shaders for the MediaVPs
I don't think the shader is giving insignia any lighting. Either that, or it's something else making it look strange.

 

Offline Zacam

  • Magnificent Bastard
  • Administrator
  • 211
  • I go Sledge-O-Matic on Spammers
    • Minecraft
    • Steam
    • Twitter
    • ModDB Feature
Re: Shaders for the MediaVPs
If in ref to MediaVPs models: Not all ships that should have insignia have them. And in many, either something is flipped or not set right.

For the Ulysses, for example, one wing displays fine, but the other one is more subdued and more transparent. This is simply because of how the model was set up and will likely be corrected at some point.

If you'd like to provide a screenshot of any FSU material that you think is rendering wrong, I can confirm if it is the shader or not.
Report MediaVP issues, now on the MediaVP Mantis! Read all about it Here!
Talk with the community on Discord
"If you can keep a level head in all this confusion, you just don't understand the situation"

¤[D+¬>

[08/01 16:53:11] <sigtau> EveningTea: I have decided that I am a 32-bit registerkin.  Pronouns are eax, ebx, ecx, edx.
[08/01 16:53:31] <EveningTea> dhauidahh
[08/01 16:53:32] <EveningTea> sak
[08/01 16:53:40] * EveningTea froths at the mouth
[08/01 16:53:40] <sigtau> i broke him, boys

 

Offline The E

  • He's Ebeneezer Goode
  • 213
  • Nothing personal, just tech support.
    • Steam
    • Twitter
Re: Shaders for the MediaVPs
In case of the decals, be aware that those never, ever get passed to the shaders, I believe (Or if they do, they are only treated with the basic lighting shader, without the benefit of Normals, shines, etc).
If I'm just aching this can't go on
I came from chasing dreams to feel alone
There must be changes, miss to feel strong
I really need lifе to touch me
--Evergrey, Where August Mourns

 

Offline Zacam

  • Magnificent Bastard
  • Administrator
  • 211
  • I go Sledge-O-Matic on Spammers
    • Minecraft
    • Steam
    • Twitter
    • ModDB Feature
Re: Shaders for the MediaVPs
Hmm. Quite right. That should be interesting to try and "fix".
Report MediaVP issues, now on the MediaVP Mantis! Read all about it Here!
Talk with the community on Discord
"If you can keep a level head in all this confusion, you just don't understand the situation"

¤[D+¬>

[08/01 16:53:11] <sigtau> EveningTea: I have decided that I am a 32-bit registerkin.  Pronouns are eax, ebx, ecx, edx.
[08/01 16:53:31] <EveningTea> dhauidahh
[08/01 16:53:32] <EveningTea> sak
[08/01 16:53:40] * EveningTea froths at the mouth
[08/01 16:53:40] <sigtau> i broke him, boys

  

Offline Macfie

  • 210
  • If somebody made a campaign I've probably got it
Re: Shaders for the MediaVPs
I found a couple of problems with the shaders.  
One, the weapons effects appear to be too exaggerated.  They cause an excessive flash on the large ships present in the mission.  This was in an FSPort mission so there were no beams being used, only lasers and missiles.  
Secondly on the Orion in the mission the tiles in the hull appear to be surrounded by a transparent line that allows you to see through the ship.

I'm using Windows Vista and a Nvidia GT120 video card.
I tried both the Antipodes 6d and the latest nightly build.  The problem goes away when the shaders are removed

[attachment deleted by admin]
« Last Edit: October 07, 2010, 05:58:02 am by Macfie »
Normal people believe that if it isn't broke, don't fix it. Engineers believe that if it isn't broke, it doesn't have enough features yet.
The difference between Mechanical Engineers and Civil Engineers is:
Mechanical Engineers build weapons.  Civil Engineers build targets
An optimist sees the glass half full; the pessimist sees it half empty. An engineer sees that the glass is twice as big as it needs to be.

 

Offline The E

  • He's Ebeneezer Goode
  • 213
  • Nothing personal, just tech support.
    • Steam
    • Twitter
Re: Shaders for the MediaVPs

Secondly on the Orion in the mission the tiles in the hull appear to be surrounded by a transparent line that allows you to see through the ship.

We can haz screenshots?
If I'm just aching this can't go on
I came from chasing dreams to feel alone
There must be changes, miss to feel strong
I really need lifе to touch me
--Evergrey, Where August Mourns

 

Offline pecenipicek

  • Roast Chicken
  • 211
  • Powered by copious amounts of coffee and nicotine
    • Minecraft
    • Skype
    • Steam
    • Twitter
    • PeceniPicek's own deviantart page
Re: Shaders for the MediaVPs
as a side note, this overall increases the shinyness of already shiny-as-hell ships.
Skype: vrganjko
Ho, ho, ho, to the bottle I go
to heal my heart and drown my woe!
Rain may fall and wind may blow,
and many miles be still to go,
but under a tall tree I will lie!

The Apocalypse Project needs YOU! - recruiting info thread.

 

Offline Kolgena

  • 211
Re: Shaders for the MediaVPs
That it does. Still, with changed lighting tags, you can make it look closer to the original look. Try light intensities that are 1/4 of what you have now, and drop oglspec by a fair bit.

I mean, with what is essentially a lighting system haulover, you can't expect settings for the old system to keep working.

 

Offline pecenipicek

  • Roast Chicken
  • 211
  • Powered by copious amounts of coffee and nicotine
    • Minecraft
    • Skype
    • Steam
    • Twitter
    • PeceniPicek's own deviantart page
Re: Shaders for the MediaVPs
I dont, i'm just commenting.



I'm quite aware of the fact that this throws most of the old values out the window, and requires proper tweaking.
Skype: vrganjko
Ho, ho, ho, to the bottle I go
to heal my heart and drown my woe!
Rain may fall and wind may blow,
and many miles be still to go,
but under a tall tree I will lie!

The Apocalypse Project needs YOU! - recruiting info thread.

 

Offline Kolgena

  • 211
Re: Shaders for the MediaVPs
My reply was more directed at Macfie, or people like me who didn't tweak their light settings after installing the shaders.

 
Re: Shaders for the MediaVPs
Updating the wiki with settings that apply to these shaders would be a good idea.
Did you hear that fellas? She says I have a Meritorious Unit.