public override void ApplyMaterial(Mesh mesh, Material mat, bool textured, bool shaded) { ShaderGen.GenFlags flags = 0; if (textured) { flags |= ShaderGen.GenFlags.ColorMap; } if (shaded) { flags |= ShaderGen.GenFlags.Lighting; } Shader shader = _shaderGen.GenerateOrGetFromCache(flags); shader.BindIfNecessary(); }
public override void ApplyMaterial(Mesh mesh, Material mat, bool textured, bool shaded, bool twoSided) { RenderControl.GLError("StartMaterialMapper"); ShaderGen.GenFlags flags = 0; var hasAlpha = false; var hasTexture = false; // note: keep this up-to-date with the code in MaterialMapper.UploadTextures() for (int i = 0; i < Renderer.modernGLUsedTextureTypeCount; i++) { TextureType currTextureType = (TextureType)((int)TextureType.Diffuse + i); GL.ActiveTexture((TextureUnit)((int)TextureUnit.Texture0 + i)); GL.BindTexture(TextureTarget.Texture2D, Renderer.modernGLTextureType[i]); //we use own texture always, even when textures off to supply preset values if (textured && mat.GetMaterialTextureCount(currTextureType) > 0) { hasTexture = true; //flags |= ShaderGen.GenFlags.Texture; flag not used, we always have some texture TextureSlot tex; mat.GetMaterialTexture(currTextureType, 0, out tex); var gtex = _scene.TextureSet.GetOriginalOrReplacement(tex.FilePath); hasAlpha = hasAlpha || gtex.HasAlpha == Texture.AlphaState.HasAlpha; if (gtex.State == Texture.TextureState.GlTextureCreated) { gtex.BindGlTexture(); } } } GL.ActiveTexture(TextureUnit.Texture0); RenderControl.GLError("EndTextureSettings"); if (shaded) { flags |= ShaderGen.GenFlags.Lighting; } var hasColors = mesh != null && mesh.HasVertexColors(0); if (hasColors) { flags |= ShaderGen.GenFlags.VertexColor; } if (_UseSceneLights) { flags |= ShaderGen.GenFlags.PhongSpecularShading; } if ((mat.IsTwoSided) || (twoSided)) { flags |= ShaderGen.GenFlags.TwoSide; } Shader shader = _shaderGen.GenerateOrGetFromCache(flags, _LightCount > 0 ? _LightCount : 1); shader.BindIfNecessary(); Matrix4 curView = Matrix4.CreateScale(_scene.Scale) * _View; shader.SetMat4("WorldViewProjection", _World * curView * _Perspective); Matrix4 cameraPos = _View.ClearRotation(); Matrix4 cameraRotation = _View.ClearTranslation(); Matrix4 cam = Matrix4.Identity; cam = cameraPos * cameraRotation; Vector3 cameraPosition = -cam.ExtractTranslation() / _scene.Scale;//does not work for orbitcontroller // cameraPosition = new Vector3(200,100,-100); //1m = 100units and positive cameraPosition.Z = -cameraPosition.Z; shader.SetVec3("CameraPosition", -cameraPosition); shader.SetMat4("World", _World); shader.SetMat4("WorldView", _World * curView); //_world* curView keeps light source at "fixed" position during rotating of the model shader.SetFloat("SceneBrightness", _SceneBrightness); shader.SetFloat("Material.diffuse", 0); shader.SetFloat("Material.ambient", 1); shader.SetFloat("Material.specular", 2); shader.SetFloat("Material.emissive", 3); shader.SetFloat("Material.height", 4); shader.SetFloat("Material.normal", 5); shader.SetLights(_GLLights, _LightCount); RenderControl.GLError("UniformSettings"); // note: keep semantics of hasAlpha consistent with IsAlphaMaterial() var alpha = 1.0f; if (mat.HasOpacity) { alpha = mat.Opacity; if (alpha < AlphaSuppressionThreshold) // suppress zero opacity, this is likely wrong input data { alpha = 1.0f; } } var color = new Color4(.8f, .8f, .8f, 1.0f); if (mat.HasColorDiffuse) { color = AssimpToOpenTk.FromColor(mat.ColorDiffuse); if (color.A < AlphaSuppressionThreshold) // s.a. { color.A = 1.0f; } } color.A *= alpha; hasAlpha = hasAlpha || color.A < 1.0f; if (shaded) { // if the material has a texture but the diffuse color texture is all black, // then heuristically assume that this is an import/export flaw and substitute // white. if (hasTexture && color.R < 1e-3f && color.G < 1e-3f && color.B < 1e-3f) { color = Color4.White; } shader.SetCol4("MaterialDiffuse_Alpha", color); color = new Color4(0, 0, 0, 1.0f); if (mat.HasColorSpecular) { color = AssimpToOpenTk.FromColor(mat.ColorSpecular); } shader.SetCol4("MaterialSpecular", color); color = new Color4(.2f, .2f, .2f, 1.0f); if (mat.HasColorAmbient) { color = AssimpToOpenTk.FromColor(mat.ColorAmbient); } shader.SetCol4("MaterialAmbient", color); color = new Color4(0, 0, 0, 1.0f); if (mat.HasColorEmissive) { color = AssimpToOpenTk.FromColor(mat.ColorEmissive); } shader.SetCol4("MaterialEmissive", color); float shininess = 1; float strength = 1; if (mat.HasShininess) { shininess = mat.Shininess; } // todo: I don't even remember how shininess strength was supposed to be handled in assimp .. Scales the specular color of the material.Not implemented here. if (mat.HasShininessStrength) { strength = mat.ShininessStrength; } var exp = shininess; if (exp >= 128.0f) // 128 is the maximum exponent as per the Gl spec { exp = 128.0f; } shader.SetFloat("MaterialShininess", exp); //Shininess may be at mat.ColorSpecular.a too..?? but in FBX is } else if (!hasColors) { shader.SetCol4("MaterialDiffuse_Alpha", color); } if (hasAlpha) { GL.Enable(EnableCap.Blend); GL.BlendFuncSeparate(BlendingFactorSrc.SrcAlpha, BlendingFactorDest.OneMinusSrcAlpha, BlendingFactorSrc.SrcAlpha, BlendingFactorDest.One); GL.DepthMask(false); } else { GL.DepthMask(true); GL.Disable(EnableCap.Blend); } RenderControl.GLError("EndMaterialMapper"); }