public void DrawImage(double x, double y, double w, double h, string imageFileName) { if (!_texturesByFileName.ContainsKey(imageFileName)) { Texture _texture = Texture.FromFile(imageFileName); _texturesByFileName.Add(imageFileName, _texture); } DrawImage(x, y, w, h, _texturesByFileName[imageFileName]); }
public void DrawModel(ModelObjectModel model) { if (model == null) { return; } string basedir = Environment.CurrentDirectory; if (model.Accessor is FileAccessor) { basedir = System.IO.Path.GetDirectoryName((model.Accessor as FileAccessor).FileName); } if (mvarRenderModels) { #region Load Materials if (!model.MaterialsLoaded && !model.MaterialsLoading) { model.MaterialsLoading = true; if (model.Accessor is FileAccessor) { Console.WriteLine("Loading materials for model: \"" + (model.Accessor as FileAccessor).FileName + "\""); } else { Console.WriteLine("Loading materials for model"); } foreach (ModelMaterial mat1 in model.Materials) { Console.Write("Loading material " + model.Materials.IndexOf(mat1).ToString() + " / " + model.Materials.Count.ToString() + "... "); foreach (ModelTexture texture in mat1.Textures) { if (texture.TexturePicture != null) { // Image has already been preloaded if (texture.TextureID == null) { // Store texture ID for this texture Texture t1 = Texture.FromPicture(texture.TexturePicture); texture.TextureID = t1.ID; } } else { if (!String.IsNullOrEmpty(texture.TextureFileName)) { if (texture.Flags != ModelTextureFlags.None) { if (texture.TextureID == null) { // Store texture ID for this texture string textureImageFullFileName = UniversalEditor.Common.Path.MakeAbsolutePath(texture.TextureFileName, basedir); if (!System.IO.File.Exists(textureImageFullFileName)) { Console.WriteLine("texture image not found: " + textureImageFullFileName); continue; } Texture t1 = Texture.FromFile(textureImageFullFileName); texture.TextureID = t1.ID; } } } } if ((texture.Flags & (ModelTextureFlags.Map | ModelTextureFlags.AddMap)) != ModelTextureFlags.None) { if (texture.TexturePicture != null) { // Image has already been preloaded if (texture.MapID == null) { // Store texture ID for this texture Texture t1 = Texture.FromPicture(texture.TexturePicture); texture.MapID = t1.ID; } } else { if (!String.IsNullOrEmpty(texture.MapFileName)) { if (texture.Flags != ModelTextureFlags.None) { if (texture.TextureID == null) { // Store texture ID for this texture string textureImageFullFileName = UniversalEditor.Common.Path.MakeAbsolutePath(texture.MapFileName, basedir); if (!System.IO.File.Exists(textureImageFullFileName)) { Console.WriteLine("texture image not found: " + textureImageFullFileName); continue; } Texture t1 = Texture.FromFile(textureImageFullFileName); texture.MapID = t1.ID; } } } } } } Console.WriteLine("done!"); } model.MaterialsLoading = false; model.MaterialsLoaded = true; } #endregion int vertexIndex = 0; if (model.Materials.Count > 0) { foreach (ModelMaterial mat in model.Materials) { // update the texture index if (mat.TextureIndex < (mat.Textures.Count - 1)) { mat.TextureIndex++; } else { mat.TextureIndex = 0; } // 輪郭・影有無で色指定方法を変える // Contour - How to specify color change with or without shadow // 半透明でなければポリゴン裏面を無効にする // To disable the reverse side must be semi-transparent polygons /* * if (mat.DiffuseColor.Alpha >= 255) * { * CullingMode = Caltron.CullingMode.Disabled; * } * else * { * CullingMode = Caltron.CullingMode.Front; * } */ // テクスチャ・スフィアマップの処理 // Processing of the texture map sphere ModelTextureFlags fTexFlag = ModelTextureFlags.None; if (mat.TextureIndex > -1 && mat.Textures.Count > 0) { fTexFlag = mat.Textures[mat.TextureIndex].Flags; } if (((fTexFlag & ModelTextureFlags.Texture) == ModelTextureFlags.Texture) && mat.Textures[mat.TextureIndex].TextureID != null) { // テクスチャありならBindする // Bind the texture to be there Texture = Texture.FromID(mat.Textures[mat.TextureIndex].TextureID.Value); EnableTexturing = true; EnableTextureGenerationS = false; EnableTextureGenerationT = false; } else if ((((fTexFlag & ModelTextureFlags.Map) == ModelTextureFlags.Map) || ((fTexFlag & ModelTextureFlags.AddMap) == ModelTextureFlags.AddMap)) && (mat.Textures[mat.TextureIndex].MapID != null)) { // スフィアマップありならBindする // Bind sphere map, if it exists // Texture = Texture.FromID(mat.MapID.Value); Texture = Texture.FromID(mat.Textures[mat.TextureIndex].MapID.Value); EnableTexturing = false; EnableTextureGenerationS = true; EnableTextureGenerationT = true; } else { // テクスチャもスフィアマップもなし // A texture map sphere without any EnableTexturing = false; EnableTextureGenerationS = false; EnableTextureGenerationT = false; } if (!mat.AlwaysLight && (mat.EdgeFlag || model.IgnoreEdgeFlag)) { // 輪郭・影有りのときは照明を有効にする // Contour - When the shadow is there to enable the lighting Internal.OpenGL.Methods.glMaterialfv(Internal.OpenGL.Constants.GLFace.Both, Internal.OpenGL.Constants.GL_DIFFUSE, new float[] { (float)mat.DiffuseColor.Red, (float)mat.DiffuseColor.Green, (float)mat.DiffuseColor.Blue, (float)mat.DiffuseColor.Alpha }); Internal.OpenGL.Methods.glMaterialfv(Internal.OpenGL.Constants.GLFace.Both, Internal.OpenGL.Constants.GL_AMBIENT, new float[] { (float)mat.AmbientColor.Red, (float)mat.AmbientColor.Green, (float)mat.AmbientColor.Blue, (float)mat.AmbientColor.Alpha }); Internal.OpenGL.Methods.glMaterialfv(Internal.OpenGL.Constants.GLFace.Both, Internal.OpenGL.Constants.GL_SPECULAR, new float[] { (float)mat.SpecularColor.Red, (float)mat.SpecularColor.Green, (float)mat.SpecularColor.Blue, (float)mat.SpecularColor.Alpha }); Internal.OpenGL.Methods.glMaterialf(Internal.OpenGL.Constants.GLFace.Both, Internal.OpenGL.Constants.GL_SHININESS, (float)mat.Shininess); EnableLighting = true; } else { // 輪郭・影無しのときは照明を無効にする // Contour - When you disable the lighting without shadows Internal.OpenGL.Methods.glColor4f((float)((mat.AmbientColor.Red + mat.DiffuseColor.Red)), (float)((mat.AmbientColor.Green + mat.DiffuseColor.Green)), (float)((mat.AmbientColor.Blue + mat.DiffuseColor.Blue)), (float)((mat.AmbientColor.Alpha + mat.DiffuseColor.Alpha))); EnableLighting = false; } // 頂点インデックスを指定してポリゴン描画 // Specifies the index vertex polygon drawing Begin(Caltron.RenderMode.Triangles); foreach (ModelTriangle tri in mat.Triangles) { DrawTriangle(tri); } End(); } } } else { EnableLighting = false; EnableTexturing = false; Internal.OpenGL.Methods.glColor4f(1, 1, 1, 1); for (int i = 0; i < model.Surfaces[0].Vertices.Count; i += 3) { DrawTriangle(new ModelTriangle(model.Surfaces[0].Vertices[i], model.Surfaces[0].Vertices[i + 1], model.Surfaces[0].Vertices[i + 2])); } } if (mvarRenderBones) { Internal.OpenGL.Methods.glDisable(Internal.OpenGL.Constants.GLCapabilities.CullFace); // Internal.OpenGL.Methods.glFrontFace(Internal.OpenGL.Constants.GLFaceOrientation.Clockwise); // Internal.OpenGL.Methods.glCullFace(Internal.OpenGL.Constants.GL_BACK); EnableTexturing = false; foreach (ModelBone bone in model.Bones) { Matrix.Push(); float[] ary = bone.Position.ToFloatArray(); Matrix.Multiply(ary); Color = Color.FromRGBA(1.0f, 0.0f, 1.0f, 1.0f); //glutSolidCube( 0.3f ); float fSize = 0.3f; Begin(Caltron.RenderMode.Quads); DrawVertex(-fSize / 2.0f, fSize / 2.0f, -fSize / 2.0f); DrawVertex(fSize / 2.0f, fSize / 2.0f, -fSize / 2.0f); DrawVertex(fSize / 2.0f, -fSize / 2.0f, -fSize / 2.0f); DrawVertex(-fSize / 2.0f, -fSize / 2.0f, -fSize / 2.0f); DrawVertex(fSize / 2.0f, fSize / 2.0f, -fSize / 2.0f); DrawVertex(fSize / 2.0f, fSize / 2.0f, fSize / 2.0f); DrawVertex(fSize / 2.0f, -fSize / 2.0f, fSize / 2.0f); DrawVertex(fSize / 2.0f, -fSize / 2.0f, -fSize / 2.0f); DrawVertex(fSize / 2.0f, fSize / 2.0f, fSize / 2.0f); DrawVertex(-fSize / 2.0f, fSize / 2.0f, fSize / 2.0f); DrawVertex(-fSize / 2.0f, -fSize / 2.0f, fSize / 2.0f); DrawVertex(fSize / 2.0f, -fSize / 2.0f, fSize / 2.0f); DrawVertex(fSize / 2.0f, fSize / 2.0f, -fSize / 2.0f); DrawVertex(fSize / 2.0f, fSize / 2.0f, fSize / 2.0f); DrawVertex(fSize / 2.0f, -fSize / 2.0f, fSize / 2.0f); DrawVertex(fSize / 2.0f, -fSize / 2.0f, -fSize / 2.0f); End(); Matrix.Pop(); Matrix.Push(); Color = Color.FromRGBA(1.0f, 1.0f, 1.0f, 1.0f); if (bone.ParentBone != null) { Begin(Caltron.RenderMode.Lines); DrawVertex(bone.ParentBone.Position.ToFloatArray()); DrawVertex(bone.Position.ToFloatArray()); End(); } Matrix.Pop(); } } }
public void DrawText(string text, double x, double y, double size = 0.1, float weight = 1.0f, Font font = null) { if (font == null) { font = Fonts.StrokeRoman; } Internal.OpenGL.Methods.glPushMatrix(); Translate(x, y); // Scale(size); // Scale(0.8, -1, 1); Scale(4); Internal.OpenGL.Methods.glLineWidth(weight); // Internal.OpenGL.Methods.glRasterPos2d(x, y); if (font is SystemFont) { SystemFont sysfont = (font as SystemFont); switch (sysfont.FontType) { case SystemFontType.Stroke: { Internal.FreeGLUT.Methods.glutStrokeString(sysfont.Handle, text); break; } case SystemFontType.Bitmap: { Internal.FreeGLUT.Methods.glutBitmapString(sysfont.Handle, text); break; } } } else if (font is BitmapFont) { BitmapFont bmpfont = (font as BitmapFont); string filename = bmpfont.Font.TextureFileName; filename = filename.Replace("~/", System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetEntryAssembly().Location) + "/"); if (!System.IO.File.Exists(filename)) { Console.WriteLine("bitmap font file not found: \"" + bmpfont.Font.TextureFileName + "\""); return; } EnableTexturing = true; EnableBlending = true; Texture texture = Texture.FromFile(filename); Texture oldTexture = this.Texture; this.Texture = texture; Internal.OpenGL.Methods.glTexEnv(Caltron.Internal.OpenGL.Constants.TextureEnvironmentTarget.TextureEnvironment, Caltron.Internal.OpenGL.Constants.TextureEnvironmentParameterName.TextureEnvironmentMode, Caltron.Internal.OpenGL.Constants.TextureEnvironmentParameterValue.Modulate); Internal.OpenGL.Methods.glBlendFunc(Caltron.Internal.OpenGL.Constants.GLBlendFunc.SourceAlpha, Caltron.Internal.OpenGL.Constants.GLBlendFunc.OneMinusSourceAlpha); int rows = (int)(1024 / bmpfont.Font.GlyphWidth); int cols = (int)(1024 / bmpfont.Font.GlyphHeight); for (int i = 0; i < text.Length; i++) { double b = y + bmpfont.Font.GlyphHeight; double r = x + bmpfont.Font.GlyphWidth; char chr = text[i]; TextureFontCharacter chara = bmpfont.Font.Characters[chr]; if (chara == null) { continue; } int index = chara.Index; int row = (int)(index / cols) + 1; int col = (int)(index % rows); double xlbound = (double)(col - 1) / 1024, xubound = ((double)bmpfont.Font.GlyphWidth / 1024) * row, ylbound = (double)row / 1024, yubound = ((double)bmpfont.Font.GlyphHeight / 1024) * col; Begin(RenderMode.Quads); Internal.OpenGL.Methods.glTexCoord(xlbound, yubound); Internal.OpenGL.Methods.glVertex2d(x, b); // The bottom left corner Internal.OpenGL.Methods.glTexCoord(xlbound, ylbound); Internal.OpenGL.Methods.glVertex2d(x, y); // The top left corner Internal.OpenGL.Methods.glTexCoord(xubound, ylbound); Internal.OpenGL.Methods.glVertex2d(r, y); // The top right corner Internal.OpenGL.Methods.glTexCoord(xubound, yubound); Internal.OpenGL.Methods.glVertex2d(r, b); // The bottom right corner End(); x += bmpfont.Font.GlyphWidth; } EnableBlending = false; EnableTexturing = false; this.Texture = oldTexture; } Internal.OpenGL.Methods.glPopMatrix(); }