Saturday, 15 August 2015

c++ - multitexture additive blending in opengl -



c++ - multitexture additive blending in opengl -

i want effect metal surface has strong sun reflection virtually turns white in light. worked pretty 2 pass rendering , additive blending sec pass (with white texture , highmost specularlight) on first 1 (metallic).

however, performance, want switch multitexturing, there no such thing additive blending there,

gltexenvi(gl_texture_env, gl_texture_env_mode, gl_modulate); gltexenvi(gl_texture_env, gl_texture_env_mode, gl_decal); gltexenvi(gl_texture_env, gl_texture_env_mode, gl_blend); gltexenvi(gl_texture_env, gl_texture_env_mode, gl_replace);

none of these works additive blend. :-(

my sec question: there possibility have sec texture more shiny first texture on same triangle? seems using glmaterial overrides material of other texture.

could solution gltexenvi - hack simulate effect of texture strong shininess?

guys, made writing shader purpose! oddly enough, didn't utilize additive blending (just alter 1 line in shader accomplish seriously) extended thought sec texture specular map - texture seen when specular lite hits it!

most of effect achieved through fact own shader can process value specularity of much above 1 - evil opengl clip 1 without asking :-(

glfloat mat_specular[] ={ 2, 2, 2, 1 }; glmaterialfv(gl_front, gl_specular,mat_specular); //works here :-)

the vertex shader:

vs = const_cast<char *>( "varying vec4 diffuse, ambient;" "varying vec3 normal, lightdir, halfvector;" "uniform float time;" "void main(void){" "normal = normalize(gl_normalmatrix * gl_normal);" //transform normal eye space "lightdir = normalize(vec3(gl_lightsource[0].position));" "halfvector = normalize(gl_lightsource[0].halfvector.xyz);" "diffuse = gl_frontmaterial.diffuse * gl_lightsource[0].diffuse;" "ambient = gl_frontmaterial.ambient * gl_lightsource[0].ambient + gl_frontmaterial.ambient * gl_lightmodel.ambient;" "gl_texcoord[0] = gl_multitexcoord0;" "gl_position = ftransform();" //apply changes "}") ;

and fragment shader:

fs =const_cast<char *>( "uniform sampler2d tex1;" "uniform sampler2d tex2;" "varying vec4 diffuse,ambient;" "varying vec3 normal, lightdir, halfvector;" "void main() {" "vec3 n,halfv,colortexel,colorfragment;" "vec4 texel,specularmap;" "float ndotl,ndothv,alphatexel,alphafragment;\n" "colorfragment = ambient.rgb;" //initialize color ambient part "alphafragment = gl_frontmaterial.diffuse.a;" "n = normalize(normal);" //copy write "ndotl = max(dot(normal, lightdir), -0.1);" "specularmap = texture2d(tex2,gl_texcoord[0].st);" //must started first! "texel = texture2d(tex1,gl_texcoord[0].st*2.0);" "if (ndotl>=-0.1) {" "colorfragment += diffuse.rgb * ndotl;" "halfv = normalize(halfvector);" "ndothv = max (dot(n,halfv),0.0);" "colorfragment += gl_frontmaterial.specular.rgb * specularmap.rgb * gl_lightsource[0].specular.rgb * pow(ndothv, gl_frontmaterial.shininess);" "}" "colortexel = texel.rgb; alphatexel = texel.a;" "gl_fragcolor = vec4(colortexel * colorfragment, alphatexel * alphafragment);" "}");

it not @ optimised , @ moment proof-of-concept without artistic skill, here result:

render without shaders, multitexture on "modulate". can see dirt on vessel, poor specular light:

rendered shaders - dirt there although not seen on shot. heavy specular light, can strenghtened rising specularity-value in glmaterialfv :

rendered exaggerated 4x shininess. can see dirt texture in glow:

one question remains - why on earth cannot utilize gl_texcoord(1) - result no texture pixel render artefacts although multitexturing works fine without shader. help appreciated.

c++ opengl alpha blending

No comments:

Post a Comment