Advertisement
Lauda

Untitled

Jan 10th, 2015
219
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Diff 11.92 KB | None | 0 0
  1. Index: core/World.cs
  2. ===================================================================
  3. --- core/World.cs   (revision 76)
  4. +++ core/World.cs   (working copy)
  5. @@ -10,6 +10,10 @@
  6.  using System.Diagnostics;
  7.  using System.Windows.Forms;
  8.  using System.Windows;
  9. +using PF1S3._10.core.utils;
  10. +using PF1S3._10.core.CML.converter;
  11. +using System.Drawing;
  12. +using System.Drawing.Imaging;
  13.  
  14.  namespace PF1S3._10.core
  15.  {
  16. @@ -103,12 +107,48 @@
  17.          /// <summary>Pomjeraj broda gore/dole.</summary>
  18.          private float shipMove;
  19.  
  20. +        /// <summary>
  21. +        /// Parser loader
  22. +        /// </summary>
  23. +        private ObjLoaderFactory loader;
  24.  
  25. +        /// <summary>
  26. +        /// Parser data
  27. +        /// </summary>
  28. +        private ParsedData pData;
  29. +
  30.          /// <summary>Kamera.</summary>
  31.          private Camera camera = null;
  32.  
  33. +        /// <summary>
  34. +        /// VBO buffers
  35. +        /// </summary>
  36. +        private int[] mVertexBuffer = new int[1];
  37. +        private int[] mTextureBuffer = new int[1];
  38. +        private int[] mIndexBuffer = new int[1];
  39. +        private int[] mNormalBuffer = new int[1];
  40.  
  41. +        /// <summary>
  42. +        /// Texture objects
  43. +        /// </summary>
  44. +        public enum TextureType { Water = 0, Sand, Brick, Rust };
  45.  
  46. +        /// <summary>
  47. +        /// Texture count
  48. +        /// </summary>
  49. +        private readonly int m_textureCount = Enum.GetNames(typeof(TextureType)).Length;
  50. +
  51. +        /// <summary>
  52. +        ///     Texture IDs
  53. +        /// </summary>
  54. +        private int[] m_textures = null;
  55. +        private int[] m_tent_textures = null;
  56. +
  57. +        /// <summary>
  58. +        ///     Texture resources
  59. +        /// </summary>
  60. +        private string[] m_textureFiles = { "..//..//images//water.jpg", "..//..//images//sand.jpg", "..//..//images//brick.jpg", "..//..//images//rust.jpg" };
  61. +
  62.          #endregion Atributi
  63.  
  64.          #region Properties
  65. @@ -214,13 +254,24 @@
  66.              this.m_scene_jeep = new AssimpScene(Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), "3D_Models\\Jeep"), "Jeep.3DS");
  67.  
  68.              // Sator preko Assimp-a
  69. -            //this.m_scene_tent = new AssimpScene(Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), "3D_Models\\Tent"), "soccerball.obj");
  70. +          //  this.m_scene_tent = new AssimpScene(Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), "3D_Models\\Tent"), "Tent.obj");
  71.  
  72. +            // Parser [start]
  73. +            loader = new ObjLoaderFactory();
  74. +            var parser = loader.Create();
  75. +            FileStream fileStream = new FileStream(@"Tent.obj", FileMode.Open);
  76. +            var result = parser.Load(fileStream);
  77. +            pData = new ParsedData(result);
  78. +            m_tent_textures = new int[1];
  79. +            // Parser [end]
  80. +
  81.              // Korisnicka inicijalizacija OpenGL parametara
  82.              Initialize();
  83.  
  84.              // Podesi projekciju i viewport
  85.              Resize();
  86. +
  87. +         //   testParser();
  88.          }
  89.  
  90.          #endregion
  91. @@ -232,9 +283,7 @@
  92.              Gl.glClearColor(0.0f, 0.0f, 0.0f, 1.0f); // Boja pozadine
  93.  
  94.              // Pozicioniraj kameru
  95. -            camera.Position(0.0f, 0.0f, 1.0f,
  96. -                            0.0f, 0.0f, 0.0f,
  97. -                            0.0f, 1.0f, 0.0f);
  98. +            camera.Position(0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f);
  99.  
  100.              Gl.glEnable(Gl.GL_DEPTH_TEST); // Ukljuci depth testing
  101.              Gl.glEnable(Gl.GL_CULL_FACE); // Sakrivanje nalicja poligona
  102. @@ -255,6 +304,86 @@
  103.              // Color tracking, ukljucivanje i podesavanje
  104.              Gl.glEnable(Gl.GL_COLOR_MATERIAL);
  105.              Gl.glColorMaterial(Gl.GL_FRONT, Gl.GL_AMBIENT_AND_DIFFUSE);
  106. +
  107. +            // Texture
  108. +            Gl.glEnable(Gl.GL_TEXTURE_2D);
  109. +            Gl.glTexEnvi(Gl.GL_TEXTURE_ENV, Gl.GL_TEXTURE_ENV_MODE, Gl.GL_DECAL);
  110. +
  111. +            Gl.glGenTextures(m_textureCount, m_textures);
  112. +
  113. +            Bitmap image;
  114. +            Rectangle rect;
  115. +            BitmapData imageData;
  116. +
  117. +            for (int i = 0; i < m_textureCount; ++i)
  118. +            {
  119. +                // Pridruzi teksturu odgovarajucem identifikatoru
  120. +                Gl.glBindTexture(Gl.GL_TEXTURE_2D, m_textures[i]);
  121. +
  122. +                // Ucitaj sliku i podesi parametre teksture
  123. +                image = new Bitmap(m_textureFiles[i]);
  124. +                // rotiramo sliku zbog koordinantog sistema opengl-a
  125. +                image.RotateFlip(RotateFlipType.RotateNoneFlipY);
  126. +                rect = new Rectangle(0, 0, image.Width, image.Height);
  127. +                // RGBA format (dozvoljena providnost slike tj. alfa kanal)
  128. +                imageData = image.LockBits(rect, System.Drawing.Imaging.ImageLockMode.ReadOnly, System.Drawing.Imaging.PixelFormat.Format32bppArgb);
  129. +
  130. +                Glu.gluBuild2DMipmaps(Gl.GL_TEXTURE_2D, (int)Gl.GL_RGBA8, image.Width, image.Height, Gl.GL_BGRA, Gl.GL_UNSIGNED_BYTE, imageData.Scan0);
  131. +
  132. +                //TODO 3: texture wrapping GL_REPEAT i linearno filtriranje
  133. +                Gl.glTexParameteri((int)Gl.GL_TEXTURE_2D, (int)Gl.GL_TEXTURE_MIN_FILTER, (int)Gl.GL_LINEAR);       // Linear Filtering
  134. +                Gl.glTexParameteri((int)Gl.GL_TEXTURE_2D, (int)Gl.GL_TEXTURE_MAG_FILTER, (int)Gl.GL_LINEAR);       // Linear Filtering
  135. +                Gl.glTexParameteri((int)Gl.GL_TEXTURE_2D, (int)Gl.GL_TEXTURE_WRAP_S, (int)Gl.GL_REPEAT);
  136. +                Gl.glTexParameteri((int)Gl.GL_TEXTURE_2D, (int)Gl.GL_TEXTURE_WRAP_T, (int)Gl.GL_REPEAT);
  137. +
  138. +                image.UnlockBits(imageData);
  139. +                image.Dispose();
  140. +            }
  141. +
  142. +            Gl.glGenTextures(1, m_tent_textures);
  143. +            Gl.glBindTexture(Gl.GL_TEXTURE_2D, m_tent_textures[0]);
  144. +
  145. +            image = new Bitmap(Path.Combine(tent_path, m_tent.findTextureFile()));
  146. +
  147. +            BitmapData textureData = image.LockBits(new Rectangle(0, 0, image.Width, image.Height), ImageLockMode.ReadOnly, PixelFormat.Format32bppArgb);
  148. +            Gl.glTexImage2D(Gl.GL_TEXTURE_2D, 0, Gl.GL_RGBA8, textureData.Width, textureData.Height, 0, Gl.GL_BGRA, Gl.GL_UNSIGNED_BYTE, textureData.Scan0);
  149. +
  150. +            // Podesavanje filtriranja teksture.
  151. +            Gl.glTexParameteri(Gl.GL_TEXTURE_2D, Gl.GL_TEXTURE_MIN_FILTER, Gl.GL_LINEAR);
  152. +            Gl.glTexParameteri(Gl.GL_TEXTURE_2D, Gl.GL_TEXTURE_MAG_FILTER, Gl.GL_LINEAR);
  153. +            Gl.glTexParameteri(Gl.GL_TEXTURE_2D, Gl.GL_TEXTURE_WRAP_S, Gl.GL_REPEAT);
  154. +            Gl.glTexParameteri(Gl.GL_TEXTURE_2D, Gl.GL_TEXTURE_WRAP_T, Gl.GL_REPEAT);
  155. +
  156. +            image.UnlockBits(textureData);
  157. +            image.Dispose();
  158. +
  159. +            //texture
  160. +            Gl.glGenBuffers(1, mTextureBuffer);
  161. +            Gl.glBindBuffer(Gl.GL_ARRAY_BUFFER, mTextureBuffer[0]);
  162. +            Gl.glBufferData(Gl.GL_ARRAY_BUFFER,
  163. +                 (IntPtr)(pData.textures.Length * sizeof(float)), pData.textures, Gl.GL_STATIC_DRAW);
  164. +
  165. +            //normale
  166. +            Gl.glGenBuffers(1, mNormalBuffer);
  167. +            Gl.glBindBuffer(Gl.GL_ARRAY_BUFFER, mNormalBuffer[0]);
  168. +            Gl.glBufferData(Gl.GL_ARRAY_BUFFER,
  169. +                 (IntPtr)(pData.normals.Length * sizeof(float)), pData.normals, Gl.GL_STATIC_DRAW);
  170. +
  171. +            //vertexi
  172. +            Gl.glGenBuffers(1, mVertexBuffer);
  173. +            Gl.glBindBuffer(Gl.GL_ARRAY_BUFFER, mVertexBuffer[0]);
  174. +            Gl.glBufferData(Gl.GL_ARRAY_BUFFER,
  175. +                 (IntPtr)(pData.vertices.Length * sizeof(float)), pData.vertices, Gl.GL_STATIC_DRAW);
  176. +
  177. +            //indexi
  178. +            Gl.glGenBuffers(1, mIndexBuffer);
  179. +            Gl.glBindBuffer(Gl.GL_ELEMENT_ARRAY_BUFFER, mIndexBuffer[0]);
  180. +            Gl.glBufferData(Gl.GL_ELEMENT_ARRAY_BUFFER,
  181. +                 (IntPtr)(pData.vertexIndices.Length * sizeof(uint)), pData.vertexIndices, Gl.GL_STATIC_DRAW);
  182. +
  183. +            //unbindujemo
  184. +            Gl.glBindBuffer(Gl.GL_ARRAY_BUFFER, 0);
  185. +            Gl.glBindBuffer(Gl.GL_ELEMENT_ARRAY_BUFFER, 0);
  186.          }
  187.  
  188.          public void Resize()
  189. @@ -491,6 +620,14 @@
  190.              cf_flag = true; // setujemo flag nakon iscrtavanja teksta
  191.          }
  192.  
  193. +        /// <summary>
  194. +        /// Initialize textures
  195. +        /// </summary>
  196. +        private void initTextures()
  197. +        {
  198. +
  199. +        }
  200. +
  201.          #endregion
  202.  
  203.          #region IDisposable metode
  204. @@ -518,31 +655,60 @@
  205.  
  206.          #region Metoda za testiranje parsera
  207.  
  208. -        public void init()
  209. +        public void testParser()
  210.          {
  211. -            var pi = new ObjLoaderFactory();
  212. -            var objLoader = pi.Create();
  213. +            var objLoaderFactory = new ObjLoaderFactory();
  214. +            var objLoader = objLoaderFactory.Create();
  215. +            FileStream fileStream = new FileStream(@"Tent.obj", FileMode.Open);
  216. +            var result = objLoader.Load(fileStream);
  217.  
  218. -            FileStream str = new FileStream(@"model.obj", FileMode.Open);
  219. +            var log = new sLog("Log initiated!");
  220.  
  221. -            var result = objLoader.Load(str);
  222. -
  223.              foreach (var v in result.Vertices)
  224.              {
  225.                  Console.WriteLine("Vertex X: " + v.X + " " + " Y: " + v.Y + " " + " Z: " + v.Z);
  226. +                log.LogWrite("Vertex X: " + v.X + " " + " Y: " + v.Y + " " + " Z: " + v.Z);
  227.              }
  228.  
  229.              foreach (var n in result.Normals)
  230.              {
  231.                  Console.WriteLine("Normal X: " + n.X + " " + " Y: " + n.Y + " " + " Z: " + n.Z);
  232. +                log.LogWrite("Normal X: " + n.X + " " + " Y: " + n.Y + " " + " Z: " + n.Z);
  233.              }
  234.  
  235.              foreach (var g in result.Groups)
  236.              {
  237.                  Console.WriteLine("Faces: " + g.Faces + " - Name: " + g.Name);
  238. +                log.LogWrite("Faces: " + g.Faces + " - Name: " + g.Name);
  239.              }
  240.  
  241. +            foreach (var t in result.Textures)
  242. +                Console.WriteLine("Texture X: " + t.X + " " + " Texture Y: " + t.Y);
  243. +
  244. +            Console.WriteLine("MATERIALS INFO!");
  245. +            foreach (var m in result.Materials)
  246. +            {
  247. +                Console.WriteLine("Name: " + m.Name + System.Environment.NewLine + " Ambient color [Ka] (x, y, z): (" + m.AmbientColor.X + ", " + m.AmbientColor.Y + ", " + m.AmbientColor.Z + ")" + System.Environment.NewLine + " Ambient color texture map: " + m.AmbientTextureMap + System.Environment.NewLine + " Diffuse color [Kd] (x, y, z): (" + m.DiffuseColor.X + ", " + m.DiffuseColor.Y + ", " + m.DiffuseColor.Z + ")" + System.Environment.NewLine + " Diffuese color texture map: " + m.DiffuseTextureMap + System.Environment.NewLine + " Specular color [Ks] (x, y, z): " + m.SpecularColor.X + ", " + m.SpecularColor.Y + ", " + m.SpecularColor.Z + System.Environment.NewLine + " Specular color coef [Ns] (0 - 1000): " + m.SpecularCoefficient + System.Environment.NewLine + " Specular color texture map: " + m.SpecularTextureMap + System.Environment.NewLine + " Specular color highlight texture map [map_Ns]: " + m.SpecularHighlightTextureMap + System.Environment.NewLine + " Bump texture map: " + m.BumpMap + System.Environment.NewLine + " Dissolve factor (pseudo-transparency) [d]: " + m.Transparency + System.Environment.NewLine + " Refraction index [Ni]: " + m.RefractionIndex + System.Environment.NewLine + " Illum (0, 1 or 2): " + m.IlluminationModel);
  248. +            }
  249. +
  250. +            ParsedData pd = new ParsedData(result);
  251. +
  252. +     /*       for (int i = 0; i < pd.vertices.Length; i++)
  253. +                Console.WriteLine("float vertices: " + pd.vertices[i]);
  254. +
  255. +            for (int i = 0; i < pd.normals.Length; i++)
  256. +                Console.WriteLine("float normals: " + pd.normals[i]);
  257. +
  258. +            for (int i = 0; i < pd.textures.Length; i++)
  259. +                Console.WriteLine("float textures: " + pd.textures[i]); */
  260. +
  261. +            for (int i = 0; i < pd.vertexIndices.Length; i++)
  262. +                Console.WriteLine("float vertexIndices: " + pd.vertexIndices[i]);
  263. +
  264.              Console.ReadLine();
  265. +
  266. +
  267. +
  268.              return;
  269.          }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement