コード例 #1
0
    public void generateMesh(DrawableShape drawShape, Transform parent, Point[] points, Vector3 up, float spacing, Vector2 completion, bool closed = false)
    {
        GameObject meshObj = new GameObject(drawShape.name);

        meshObj.transform.parent = parent;
        MeshFilter   mf = meshObj.AddComponent <MeshFilter>();
        MeshRenderer mr = meshObj.AddComponent <MeshRenderer>();

        if (mf.sharedMesh == null)
        {
            mf.sharedMesh = new Mesh();
        }

        Mesh mesh = mf.sharedMesh;

        Shape shape = enumToShape(drawShape);

        Vector3[] vertices = getMeshVertices(points, shape, drawShape.offset, up, completion, closed);
        Vector3[] normals  = pointToMeshNormals(points, shape, up, drawShape.winding, completion, closed);
        Vector2[] UVs      = getMeshUV(vertices.Length, shape);
        int[]     tris     = getMeshTris(vertices.Length, shape, drawShape.winding, completion, closed);

        mesh.Clear();
        mesh.vertices  = vertices;
        mesh.normals   = normals;
        mesh.uv        = UVs;
        mesh.triangles = tris;

        mr.material = drawShape.meshMaterial;
        float tileVal = drawShape.tiling * points.Length * spacing * 0.05f;

        mr.sharedMaterial.mainTextureScale = new Vector2(1, tileVal);
    }
コード例 #2
0
        public void FindEmptySpot()
        {
            DrawableShape shape = null;
            int           tempx;
            int           tempy;

            do
            {
                tempx = rng.Next(Program.ScreenWidth);
                tempy = rng.Next(Program.ScreenHeight);
            } while ((shape = Program.GameScreen.FindTopmostShapeForTile(tempx, tempy)) != null);
            x = tempx;
            y = tempy;
        }
コード例 #3
0
    public static Shape enumToShape(DrawableShape shape)
    {
        switch (shape.shape)
        {
        case ShapeToDraw.WALL:
            return(new Wall(shape.size));

        case ShapeToDraw.CUBE:
            return(new Cube(shape.size));

        case ShapeToDraw.TUNNEL:
            return(new Tunnel(shape.size));

        default:
            return(new RoadMesh(shape.size));
        }
    }
コード例 #4
0
ファイル: Test.cs プロジェクト: ppanchal97/Design-Patterns
        public static void Run()
        {
            var api2d = new ShapeDrawing2d();
            var api3d = new ShapeDrawing3d();

            var shapesArr = new DrawableShape[] {
                new Circle(api2d, 1, 3, 7),
                new Circle(api3d, 1, 3, 7),
                new Cube(api3d, 5, 7, 11),
                new Cube(api2d, 15, 57, 11)
            };

            foreach (var shape in shapesArr)
            {
                shape.Draw();
            }
        }
コード例 #5
0
        protected override void OnLoad(EventArgs e)
        {
            #region Abort on platforms which will not be able to execute the ops properly

            /*
             * if (!GL.SupportsExtension("VERSION_1_2"))
             * {
             *  Trace.WriteLine("Aborting. OpenGL 1.2 or later required.");
             *  this.Exit();
             * }
             *
             * int[] t = new int[2];
             * GL.GetInteger(GetPName.MajorVersion, out t[0]);
             * GL.GetInteger(GetPName.MinorVersion, out t[1]);
             * Trace.WriteLine("OpenGL Context Version: " + t[0] + "." + t[1]);
             *
             * GL.GetInteger(GetPName.DepthBits, out t[0]);
             * Trace.WriteLine("Depth Bits: " + t[0]);
             * GL.GetInteger(GetPName.StencilBits, out t[1]);
             * Trace.WriteLine("Stencil Bits: " + t[1]);
             *
             * if (t[0] < 16)
             * {
             *  Trace.WriteLine("Aborting. Need at least 16 depth bits, only " + t[0] + " available.");
             *  this.Exit();
             * }
             *
             * if (t[1] < 1)
             * {
             *  Trace.WriteLine("Aborting. Need at least 1 stencil bit, only " + t[1] + " available.");
             *  this.Exit();
             * }
             */
            #endregion Abort on platforms which will not be able to execute the ops properly

            WindowTitle = "Cube-Sphere Stencil CSG  " + GL.GetString(StringName.Renderer) + " (GL " + GL.GetString(StringName.Version) + ")";

            #region GL States
            GL.ClearColor(.08f, .12f, .16f, 1f);

            GL.Enable(EnableCap.DepthTest);
            GL.DepthFunc(DepthFunction.Less);
            GL.ClearDepth(1.0);

            GL.Enable(EnableCap.StencilTest);
            GL.ClearStencil(0);
            GL.StencilMask(0xFFFFFFFF); // read&write

            GL.Enable(EnableCap.CullFace);
            GL.FrontFace(FrontFaceDirection.Ccw);
            GL.CullFace(CullFaceMode.Back);

            GL.PolygonMode(MaterialFace.FrontAndBack, PolygonMode.Fill);

            GL.Color4(1f, 1f, 1f, 1f);

            GL.Enable(EnableCap.Lighting);
            GL.Enable(EnableCap.Light0);
            GL.ShadeModel(ShadingModel.Smooth);

            #endregion GL States

            #region Load Texture
            Bitmap bitmap = new Bitmap("Data/Textures/logo-dark.jpg");
            bitmap.RotateFlip(RotateFlipType.RotateNoneFlipY);

            GL.GenTextures(1, out Texture);
            GL.BindTexture(TextureTarget.Texture2D, Texture);

            BitmapData data = bitmap.LockBits(new System.Drawing.Rectangle(0, 0, bitmap.Width, bitmap.Height), ImageLockMode.ReadOnly, System.Drawing.Imaging.PixelFormat.Format32bppArgb);
            GL.TexImage2D(TextureTarget.Texture2D, 0, PixelInternalFormat.Rgba, data.Width, data.Height, 0, OpenTK.Graphics.OpenGL.PixelFormat.Bgra, PixelType.UnsignedByte, data.Scan0);
            GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMinFilter, (int)TextureMinFilter.Linear);
            GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMagFilter, (int)TextureMagFilter.Linear);
            GL.Finish();
            bitmap.UnlockBits(data);
            #endregion Load Texture

            OperandA = new ChamferCube(1.5, 2.0, 2.5, ChamferCube.SubDivs.Four, 0.42, true);
            OperandB = new SlicedSphere(2.0f, Vector3d.Zero,
                                        SlicedSphere.eSubdivisions.Three,
                                        new SlicedSphere.eDir[] { SlicedSphere.eDir.All },
                                        true);

            #region Invert Operand B's Normals
            // only the inside of the operand is ever drawn to color buffers and lighting requires this.
            PrimitiveType     tempPrimMode;
            VertexT2dN3dV3d[] tempVertices;
            uint[]            tempIndices;

            OperandB.GetArraysforVBO(out tempPrimMode, out tempVertices, out tempIndices);
            OperandB.Dispose();

            for (int i = 0; i < tempVertices.Length; i++)
            {
                tempVertices[i].Normal *= -1.0;
                tempVertices[i].Normal.Normalize();
            }

            OperandB = new VboShape(ref tempPrimMode, ref tempVertices, ref tempIndices, true);
            #endregion Invert Operand B's Normals
        }
コード例 #6
0
        /// <summary>Setup OpenGL and load resources here.</summary>
        /// <param name="e">Not used.</param>
        protected override void OnLoad(EventArgs e)
        {
            this.VSync = VSyncMode.Off;

            // Check for necessary capabilities:
            string extensions = GL.GetString(StringName.Extensions);

            if (!GL.GetString(StringName.Extensions).Contains("GL_ARB_shading_language"))
            {
                throw new NotSupportedException(String.Format("This example requires OpenGL 2.0. Found {0}. Aborting.",
                                                              GL.GetString(StringName.Version).Substring(0, 3)));
            }

            if (!extensions.Contains("GL_ARB_texture_compression") ||
                !extensions.Contains("GL_EXT_texture_compression_s3tc"))
            {
                throw new NotSupportedException("This example requires support for texture compression. Aborting.");
            }

            #region GL State

            GL.ClearColor(0f, 0f, 0f, 0f);

            GL.Disable(EnableCap.Dither);

            GL.Enable(EnableCap.CullFace);
            GL.FrontFace(FrontFaceDirection.Ccw);
            GL.PolygonMode(MaterialFace.Front, PolygonMode.Fill);
            //  GL.PolygonMode( MaterialFace.Back, PolygonMode.Line );

            #endregion GL State

            #region Shaders

            string LogInfo;

            // Load&Compile Vertex Shader

            using (StreamReader sr = new StreamReader(VertexShaderFilename))
            {
                VertexShaderObject = GL.CreateShader(ShaderType.VertexShader);
                GL.ShaderSource(VertexShaderObject, sr.ReadToEnd( ));
                GL.CompileShader(VertexShaderObject);
            }

            GL.GetShaderInfoLog(VertexShaderObject, out LogInfo);
            if (LogInfo.Length > 0 && !LogInfo.Contains("hardware"))
            {
                Trace.WriteLine("Vertex Shader failed!\nLog:\n" + LogInfo);
            }
            else
            {
                Trace.WriteLine("Vertex Shader compiled without complaint.");
            }

            // Load&Compile Fragment Shader

            using (StreamReader sr = new StreamReader(FragmentShaderFilename))
            {
                FragmentShaderObject = GL.CreateShader(ShaderType.FragmentShader);
                GL.ShaderSource(FragmentShaderObject, sr.ReadToEnd( ));
                GL.CompileShader(FragmentShaderObject);
            }
            GL.GetShaderInfoLog(FragmentShaderObject, out LogInfo);

            if (LogInfo.Length > 0 && !LogInfo.Contains("hardware"))
            {
                Trace.WriteLine("Fragment Shader failed!\nLog:\n" + LogInfo);
            }
            else
            {
                Trace.WriteLine("Fragment Shader compiled without complaint.");
            }

            // Link the Shaders to a usable Program
            ProgramObject = GL.CreateProgram( );
            GL.AttachShader(ProgramObject, VertexShaderObject);
            GL.AttachShader(ProgramObject, FragmentShaderObject);

            // link it all together
            GL.LinkProgram(ProgramObject);

            // flag ShaderObjects for delete when not used anymore
            GL.DeleteShader(VertexShaderObject);
            GL.DeleteShader(FragmentShaderObject);

            int[] temp = new int[1];
            GL.GetProgram(ProgramObject, GetProgramParameterName.LinkStatus, out temp[0]);
            Trace.WriteLine("Linking Program (" + ProgramObject + ") " + ((temp[0] == 1) ? "succeeded." : "FAILED!"));
            if (temp[0] != 1)
            {
                GL.GetProgramInfoLog(ProgramObject, out LogInfo);
                Trace.WriteLine("Program Log:\n" + LogInfo);
            }

            GL.GetProgram(ProgramObject, GetProgramParameterName.ActiveAttributes, out temp[0]);
            Trace.WriteLine("Program registered " + temp[0] + " Attributes. (Should be 4: Pos, UV, Normal, Tangent)");

            Trace.WriteLine("Tangent attribute bind location: " + GL.GetAttribLocation(ProgramObject, "AttributeTangent"));

            Trace.WriteLine("End of Shader build. GL Error: " + GL.GetError( ));

            #endregion Shaders

            #region Textures

            TextureLoaderParameters.FlipImages          = false;
            TextureLoaderParameters.MagnificationFilter = TextureMagFilter.Linear;
            TextureLoaderParameters.MinificationFilter  = TextureMinFilter.Linear;
            TextureLoaderParameters.WrapModeS           = TextureWrapMode.ClampToEdge;
            TextureLoaderParameters.WrapModeT           = TextureWrapMode.ClampToEdge;
            TextureLoaderParameters.EnvMode             = TextureEnvMode.Modulate;

            ImageDDS.LoadFromDisk(TMU0_Filename, out TMU0_Handle, out TMU0_Target);
            Trace.WriteLine("Loaded " + TMU0_Filename + " with handle " + TMU0_Handle + " as " + TMU0_Target);

            #endregion Textures

            Trace.WriteLine("End of Texture Loading. GL Error: " + GL.GetError( ));
            Trace.WriteLine("");

            sphere = new SlicedSphere(1.5f, Vector3d.Zero, SlicedSphere.eSubdivisions.Four, new SlicedSphere.eDir[] { SlicedSphere.eDir.All }, true);
        }
コード例 #7
0
 public void Draw(DrawableShape shape)
 {
     Console.WriteLine("-- Drawing 3D Shape -- ");
     Console.WriteLine(shape);
 }