public static void Init(ZApplication app) { defaultMaterial = new Material(null); GUIMaterial = new Material(null); GUIMaterial.Lighting = false; defaultTexture = new Texture(null); defaultFont = new Font(null); GL.MatrixMode(All.MODELVIEW); GL.Enable(All.DEPTH_TEST); //Enable Light //Use default position GL.Enable(All.LIGHTING); GL.Enable(All.LIGHT0); //Use glColor to color materials. //This eliminates the need to set ambient and diffuse separately. GL.Enable(All.COLOR_MATERIAL); GL.ColorMaterial(All.FRONT_AND_BACK, All.AMBIENT_AND_DIFFUSE); //glMaterialfv(GL_FRONT, GL_SPECULAR, Specular); //glMaterialf(GL_FRONT, GL_SHININESS, LowShininess); // IMPORTANT: Otherwise light will arise with scaled vectors, e.g. with Scale=0.1 then it will be too bright // This is because gl scales the normals with glScale //GL.Enable(All.NORMALIZE); var shape = new UnitQuad(); unitQuad = new Mesh(null) { Name = "UnitQuad" }; unitQuad.CreateVBO(shape); defaultMaterial.Apply(null); }
public void Apply(Material oldMat) { GL.Color4(Color); //Test for equal material after setting color //This is because r}ersetcolor may have been called so we need reset material.color if (oldMat == this) return; bool oldNull = (oldMat == null); Renderer.currentMaterial = this; //GL.Material(All.FRONT, All.SPECULAR, SpecularColor); //GL.Material(All.FRONT, All.EMISSION, EmissionColor); //GL.Material(All.FRONT, All.SHININESS, Shininess); if (oldNull || Shading != oldMat.Shading) GL.ShadeModel(Shading); if (oldNull || Wireframe != oldMat.Wireframe) { if (Wireframe) GL.PolygonMode(All.FRONT_AND_BACK, All.LINE); else GL.PolygonMode(All.FRONT_AND_BACK, All.FILL); } if (oldNull || WireframeWidth != oldMat.WireframeWidth) GL.LineWidth(WireframeWidth); if (oldNull || Lighting != oldMat.Lighting) { if (Lighting && App.Lighting) GL.Enable(All.LIGHTING); else GL.Disable(All.LIGHTING); } if (oldNull || ZBuffer != oldMat.ZBuffer) { if (ZBuffer) { GL.DepthFunc(All.LEQUAL); GL.DepthMask(true); } else { //Disable z-buffer: Skip depth-test GL.DepthFunc(All.ALWAYS); GL.DepthMask(false); } } /*if (oldNull || DrawBackFace != oldMat.DrawBackFace) { if (DrawBackFace) GL.Disable(All.CULL_FACE); else GL.Enable(All.CULL_FACE); }*/ /*if (oldNull || (Blend!=oldMat.Blend) { if Blend=mbNoBlend GL.Disable(All.BLEND) else { GL.Enable(All.BLEND); case Blend of mbA_1MSA : GL.BlendFunc(All.SRC_ALPHA, All.ONE_MINUS_SRC_ALPHA); mbA_1 : GL.BlendFunc(All.SRC_ALPHA,All.ONE); mbC_1MSC : GL.BlendFunc(All.SRC_COLOR,All.ONE_MINUS_SRC_COLOR); mbAlphaSat_1 : GL.BlendFunc(All.SRC_ALPHA_SATURATE,All.ONE); } } }*/ int texCount = Textures.Count; if (!oldNull && oldMat.Textures.Count > texCount) texCount = oldMat.Textures.Count; //Count backwards so that activetexture is zero on loop exit for (int i = texCount - 1; i >= 0; i--) { /*if MultiTextureSupported GL.ActiveTexture($84C0 + I) else if I>0 Continue;*/ // The textures from the previous material must be disabled if (i >= Textures.Count) { GL.BindTexture(All.TEXTURE_2D, 0); GL.Disable(All.TEXTURE_2D); continue; } Texture tex = Textures[i]; if (tex != null) { GL.Enable(All.TEXTURE_2D); tex.Bind(); } /*else if Tex.RenderTarget!=nil { GL.Enable(All.TEXTURE_2D); Tex.RenderTarget.UseTextureBegin; }*/ //Texture matrix //Denna ||dning är nödvändig för att scale och rotate ska ske kring texture center (0.5) /*GL.MatrixMode(All.TEXTURE); GL.LoadIdentity(); GL.Translatef(Tex.TextureX+0.5,Tex.TextureY+0.5,0); GL.Scalef(Tex.TextureScale[0],Tex.TextureScale[1],1); GL.Rotatef(Tex.TextureRotate*360,0,0,1); GL.Translatef(-0.5,-0.5,0); GL.MatrixMode(All.MODELVIEW); if (Tex.TexCoords==Generated) { GL.Enable(All.TEXTURE_GEN_S); GL.Enable(All.TEXTURE_GEN_T); GL.TexGeni(All.S,All.TEXTURE_GEN_MODE, All.OBJECT_LINEAR); GL.TexGeni(All.T,All.TEXTURE_GEN_MODE, All.OBJECT_LINEAR); } else { GL.Disable(All.TEXTURE_GEN_S); GL.Disable(All.TEXTURE_GEN_T); }*/ //This is a local parameter for every texture GL.TexParameter(All.TEXTURE_2D, All.TEXTURE_WRAP_S, (int) tex.WrapMode); GL.TexParameter(All.TEXTURE_2D, All.TEXTURE_WRAP_T, (int) tex.WrapMode); } if (texCount == 0) { GL.BindTexture(All.TEXTURE_2D, 0); GL.Disable(All.TEXTURE_2D); } /*if (ShadersSupported && (oldNull || (Shader!=oldMat.Shader)) { if (!oldNull) && (oldMat.Shader!=nil) oldMat.Shader.DetachArrayVariables; if Shader!=nil Shader.UseShader else GL.UseProgram(0); }*/ }