Exemplo n.º 1
0
        internal void Init()
        {
            Glfw.ConfigureNativesDirectory(GlfwLibraryPath);

            // Check glfw init
            Debug.Assert(() => Glfw.Init(), "Glfw Could not be initialised", LogLayer.Application, LogState.Error,
                         () =>
            {
                Environment.Exit(-1);
            }, "Glfw has been Initialised");

            // Create window
            window = Glfw.CreateWindow(Width, Height, Name);
            Debug.Assert(() => window, "Window could not be created", LogLayer.Application, LogState.Error,
                         () =>
            {
                Glfw.Terminate();
                Environment.Exit(-1);
            }, $"Window has been created {Width}x{Height}");

            Glfw.SwapInterval(SwapInterval);

            Glfw.MakeContextCurrent(window);

            // Set character callback
            Glfw.SetCharCallback(window, new Glfw.CharFunc((w, code) =>
            {
            }));
        }
Exemplo n.º 2
0
        static void Main(string[] args)
        {
            // Initialize OpenGL
            Gl.Initialize();


            // If the library isn't in the environment path we need to set it
            Glfw.ConfigureNativesDirectory("..\\..\\libs");

            // Initialize the GLFW
            if (!Glfw.Init())
            {
                Environment.Exit(-1);
            }

            // Create a windowed mode window and its OpenGL context
            var window = Glfw.CreateWindow(width, height, "OpenGL/Glfw");

            if (!window)
            {
                Glfw.Terminate();
                Environment.Exit(-1);
            }

            // Make the window's context current
            Glfw.MakeContextCurrent(window);



            // Loop until the user closes the window
            while (!Glfw.WindowShouldClose(window))
            {
                // Render here
                ////

                //Swap front and back buffers
                Glfw.SwapBuffers(window);

                // Poll for and process events
                Glfw.PollEvents();
            }


            // terminate program
            Glfw.Terminate();
        }
Exemplo n.º 3
0
    static void Main(string[] args)
    {
        // If the library isn't in the environment path we need to set it
        Glfw.ConfigureNativesDirectory("External/");
        Gl.Initialize();
        // Initialize the library
        if (!Glfw.Init())
        {
            Environment.Exit(-1);
        }

        // Create a windowed mode window and its OpenGL context
        var window = Glfw.CreateWindow(640, 480, "Hello World");

        if (!window)
        {
            Glfw.Terminate();
            Environment.Exit(-1);
        }

        // Make the window's context current
        Glfw.MakeContextCurrent(window);

        Gl.Viewport(0, 0, 800, 600);
        // Loop until the user closes the window
        while (!Glfw.WindowShouldClose(window))
        {
            // Render here
            Gl.ClearColor(0.2f, 0.3f, 0.3f, 1.0f);
            Gl.Clear(ClearBufferMask.ColorBufferBit);

            // Swap front and back buffers
            Glfw.SwapBuffers(window);

            // Poll for and process events
            Glfw.PollEvents();
        }

        Glfw.Terminate();
    }
Exemplo n.º 4
0
        /// <summary>
        /// Initialises the Glfw library and creates a window
        /// </summary>
        internal void Init()
        {
            // Get the specified Glfw.dll's directory
            Glfw.ConfigureNativesDirectory("..//..//Vendor/");

            // Init library
            if (!Glfw.Init())
            {
                Debug.Log("Could not initialise Glfw", Debug.DebugLayer.Application, Debug.DebugLevel.Error);
            }

            // Get monitor settings
            monitor = Glfw.GetPrimaryMonitor();

            // Create window
            window = Glfw.CreateWindow(width, height, title, Glfw.Monitor.None, Glfw.Window.None);
            if (!window)
            {
                Debug.Log("Could not create window", Debug.DebugLayer.Application, Debug.DebugLevel.Error);
            }

            // Make the current window the context
            Glfw.MakeContextCurrent(window);

            // Let glfw swap the buffers as fast as possible - 0 for fast 1 - for v sync
            Glfw.SwapInterval(0);

            // Set callbacks
            keyboardDel    = KeyboardCallBack;
            mouseButtonDel = MouseButtonCallBack;
            cursorPosDel   = CursorPosCallBack;

            Glfw.SetKeyCallback(window, keyboardDel);
            Glfw.SetMouseButtonCallback(window, mouseButtonDel);
            Glfw.SetCursorPosCallback(window, cursorPosDel);


            Debug.Log("Window Created size " + width + "x" + height, Debug.DebugLayer.Application, Debug.DebugLevel.Information);
        }
Exemplo n.º 5
0
        static void Main(string[] args)
        {
            // Initialize OpenGL
            Gl.Initialize();


            // If the library isn't in the environment path we need to set it
            Glfw.ConfigureNativesDirectory("..\\..\\libs");

            // Initialize the GLFW
            if (!Glfw.Init())
            {
                Environment.Exit(-1);
            }

            // Create a windowed mode window and its OpenGL context
            var window = Glfw.CreateWindow(width, height, "OpenGL/Glfw");

            if (!window)
            {
                Glfw.Terminate();
                Environment.Exit(-1);
            }

            // Make the window's context current
            Glfw.MakeContextCurrent(window);

            // create shaders after creating opengl context
            StaticShader shader = new StaticShader();

            // create loder and renderer
            Renderer renderer = new Renderer();
            Loader   loader   = new Loader();


            // set data

            float[] vertices =
            {
                -0.5f,  0.5f, 0f, //v0
                -0.5f, -0.5f, 0f, //v1
                0.5f,  -0.5f, 0f, //v2
                0.5f,   0.5f, 0f, //v3
            };

            uint[] indices =
            {
                0, 1, 3, //top left triangle (v0, v1, v3)
                3, 1, 2  //bottom right triangle (v3, v1, v2)
            };


            // create model
            RawModel model = loader.LoadToVao(vertices, indices);


            // Loop until the user closes the window
            while (!Glfw.WindowShouldClose(window))
            {
                // Render here
                renderer.prepare();
                shader.start();
                renderer.render(model);
                shader.stop();

                //Swap front and back buffers
                Glfw.SwapBuffers(window);

                // Poll for and process events
                Glfw.PollEvents();
            }

            // clean memory
            shader.cleanUP();
            loader.CleanUp();

            // terminate program
            Glfw.Terminate();
        }
Exemplo n.º 6
0
 internal static void Init()
 {
     Glfw.ConfigureNativesDirectory("External/");
     Glfw.SetErrorCallback(ErrorCallback);
 }
Exemplo n.º 7
0
        static void Main(string[] args)
        {
            // Initialize OpenGL
            Gl.Initialize();

            // If the library isn't in the environment path we need to set it
            Glfw.ConfigureNativesDirectory("..\\..\\libs/glfw-3.2.1.bin.WIN32/lib-mingw");

            // Initialize the GLFW
            if (!Glfw.Init())
            {
                Environment.Exit(-1);
            }

            // Create a windowed mode window and its OpenGL context
            var window = Glfw.CreateWindow(width, height, "Unreal Engine 4");


            if (!window)
            {
                Glfw.Terminate();
                Environment.Exit(-1);
            }

            // set window icon
            Glfw.Image icon = Utils.getImage("..\\..\\res/logo.png");
            Glfw.SetWindowIcon(window, icon);

            // Make the window's context current
            Glfw.MakeContextCurrent(window);

            // create loder and renderer
            Loader loader = new Loader();


            // create model
            RawModel      fern         = OBJLoader.loadObjModel("fern", loader);
            ModelTexture  fernTexture  = new ModelTexture(loader.loadTexture("..\\..\\res/flower.png"));
            TexturedModel texturedFern = new TexturedModel(fern, fernTexture);

            texturedFern.modelTexture.shineDamper       = 10;
            texturedFern.modelTexture.reflectivity      = 0;
            texturedFern.modelTexture.isHasTransparency = true;

            RawModel      grass         = OBJLoader.loadObjModel("grassModel", loader);
            ModelTexture  grassTexture  = new ModelTexture(loader.loadTexture("..\\..\\res/flower.png"));
            TexturedModel texturedGrass = new TexturedModel(grass, grassTexture);

            texturedGrass.modelTexture.shineDamper       = 10;
            texturedGrass.modelTexture.reflectivity      = 0;
            texturedGrass.modelTexture.isHasTransparency = true;
            texturedGrass.modelTexture.isUseFakeLighting = true;

            RawModel      model       = OBJLoader.loadObjModel("lowPolyTree", loader);
            ModelTexture  texture     = new ModelTexture(loader.loadTexture("..\\..\\res/lowPolyTree.png"));
            TexturedModel staticModel = new TexturedModel(model, texture);

            staticModel.modelTexture.shineDamper  = 10;
            staticModel.modelTexture.reflectivity = 0;
            Entity entity = new Entity(staticModel, new Vertex3f(0, 0, -25), 0, 0, 0, 1);
            Light  light  = new Light(new Vertex3f(0, 0, -20), new Vertex3f(1f, 1f, 1f));

            Random       rand           = new Random();
            ModelTexture terrainTexture = new ModelTexture(loader.loadTexture("..\\..\\res/grass.png"));

            terrainTexture.shineDamper  = 10;
            terrainTexture.reflectivity = 0;
            Terrain terrain  = new Terrain(800, 0, loader, terrainTexture);
            Terrain terrain2 = new Terrain(800, -800, loader, terrainTexture);

            RawModel      tree         = OBJLoader.loadObjModel("normal_tree", loader);
            ModelTexture  treeTexture  = new ModelTexture(loader.loadTexture("..\\..\\res/normal_tree.png"));
            TexturedModel texturedTree = new TexturedModel(tree, treeTexture);


            Random random = new Random();

            Camera camera = new Camera();

            List <Entity> allCubes = new List <Entity>();
            List <Entity> allGrass = new List <Entity>();
            List <Entity> allFerns = new List <Entity>();
            List <Entity> allTrees = new List <Entity>();

            for (int i = 0; i < 1000; i++)
            {
                double x = random.NextDouble() * -1600 + 800;
                double y = 0;
                double z = random.NextDouble() * -2410;
                allCubes.Add(new Entity(staticModel, new Vertex3f((float)x, (float)y, (float)z), 0, 0, 0, rand.Next(1, 4)));
            }
            for (int i = 0; i < 1000; i++)
            {
                double x = random.NextDouble() * -1600 + 800;
                double y = 0;
                double z = random.NextDouble() * -2410;
                allGrass.Add(new Entity(texturedGrass, new Vertex3f((float)x, (float)y, (float)z), 0, 0, 0, 3));
            }
            for (int i = 0; i < 1000; i++)
            {
                double x = random.NextDouble() * -1600 + 800;
                double y = 0;
                double z = random.NextDouble() * -2410;
                allFerns.Add(new Entity(texturedFern, new Vertex3f((float)x, (float)y, (float)z), 0, 0, 0, 3));
            }

            MasterRenderer renderer = new MasterRenderer(new WinowInfo(width, height));

            // Loop until the user closes the window
            while (!Glfw.WindowShouldClose(window))
            {
                //entity.increasePosition(0, 0, -0.1f);
                entity.increaseRotation(0, 1, 0);

                // Render here
                camera.move(window);

                renderer.processTerrain(terrain);
                renderer.processTerrain(terrain2);

                foreach (Entity cube in allCubes)
                {
                    renderer.processEntity(cube);
                }
                foreach (Entity gr in allGrass)
                {
                    renderer.processEntity(gr);
                }
                foreach (Entity fr in allFerns)
                {
                    renderer.processEntity(fr);
                }
                foreach (Entity tr in allTrees)
                {
                    renderer.processEntity(tr);
                }


                renderer.render(light, camera);

                //Swap front and back buffers
                Glfw.SwapBuffers(window);

                // Poll for and process events
                Glfw.PollEvents();
            }

            // clean memory
            renderer.cleanUP();
            loader.CleanUp();

            // terminate program
            Glfw.Terminate();
        }
Exemplo n.º 8
0
        static void Main(string[] args)
        {
            // Initialize OpenGL
            Gl.Initialize();

            // If the library isn't in the environment path we need to set it
            Glfw.ConfigureNativesDirectory("..\\..\\libs/glfw-3.2.1.bin.WIN32/lib-mingw");

            // Initialize the GLFW
            if (!Glfw.Init())
            {
                Environment.Exit(-1);
            }

            // Create a windowed mode window and its OpenGL context
            var window = Glfw.CreateWindow(width, height, "Unreal Engine 4");


            if (!window)
            {
                Glfw.Terminate();
                Environment.Exit(-1);
            }

            // set window icon
            Glfw.Image icon = Utils.getImage("..\\..\\res/logo.png");
            Glfw.SetWindowIcon(window, icon);

            // Make the window's context current
            Glfw.MakeContextCurrent(window);

            // create shaders after creating opengl context
            StaticShader shader = new StaticShader();


            // create loder and renderer
            Loader   loader   = new Loader();
            Renderer renderer = new Renderer(shader, new WinowInfo(width, height));

            // create model
            RawModel      model       = OBJLoader.loadObjModel("dragon", loader);
            ModelTexture  texture     = new ModelTexture(loader.loadTexture("..\\..\\res/white.jpg"));
            TexturedModel staticModel = new TexturedModel(model, texture);

            Entity entity = new Entity(staticModel, new Vertex3f(0, 0, -25), 0, 0, 0, 2);
            Light  light  = new Light(new Vertex3f(0, 0, -20), new Vertex3f(1f, 1f, 1f));

            Camera camera = new Camera();

            // Loop until the user closes the window
            while (!Glfw.WindowShouldClose(window))
            {
                entity.increaseRotation(0, 1, 0);

                // Render here
                camera.move(window);
                renderer.prepare();
                shader.start();
                shader.loadLight(light);
                shader.loadViewMatrix(camera);
                renderer.render(entity, shader);

                shader.stop();


                //Swap front and back buffers
                Glfw.SwapBuffers(window);

                // Poll for and process events
                Glfw.PollEvents();
            }

            // clean memory
            shader.cleanUP();
            loader.CleanUp();

            // terminate program
            Glfw.Terminate();
        }
Exemplo n.º 9
0
        static void CreateContext(/*params string[] RequiredExtensions*/)
        {
            GConsole.WriteLine("Initializing OpenGL");

#if DEBUG
            Khronos.KhronosApi.Log += (S, E) => {
                if (E.Name == "glGetError")
                {
                    return;
                }

                Console.ForegroundColor = ConsoleColor.Yellow;
                Console.WriteLine("OpenGL> {0}", string.Format("{0}({1}) = {2}", E.Name, string.Join(", ", E.Args), E.ReturnValue ?? "null"));
                Console.ResetColor();
            };
            Khronos.KhronosApi.LogEnabled = false;            //*/
#endif

            GConsole.WriteLine("Initializing GLFW");
            Glfw.ConfigureNativesDirectory("native/glfw3_64");
            if (!Glfw.Init())
            {
                FatalError("Could not initialize GLFW");
            }

            Glfw.SetErrorCallback((Err, Msg) => {
                FatalError("GLFW({0}) {1}", Err, Msg);
            });

            Width  = CVar.GetInt("width", 800);
            Height = CVar.GetInt("height", 600);

            Glfw.WindowHint(Glfw.Hint.Resizable, CVar.GetBool("resizable"));

            //Glfw.WindowHint(Glfw.Hint.ClientApi, Glfw.ClientApi.None);
            Glfw.WindowHint(Glfw.Hint.ClientApi, Glfw.ClientApi.OpenGL);
            Glfw.WindowHint(Glfw.Hint.ContextCreationApi, Glfw.ContextApi.Native);
            Glfw.WindowHint(Glfw.Hint.Doublebuffer, CVar.GetBool("gl_doublebuffer"));
            Glfw.WindowHint(Glfw.Hint.ContextVersionMajor, CVar.GetInt("gl_major"));
            Glfw.WindowHint(Glfw.Hint.ContextVersionMinor, CVar.GetInt("gl_minor"));

            Glfw.WindowHint(Glfw.Hint.Samples, CVar.GetInt("gl_samples"));
            Glfw.WindowHint(Glfw.Hint.OpenglForwardCompat, CVar.GetBool("gl_forwardcompat"));
            Glfw.WindowHint(Glfw.Hint.OpenglProfile, Glfw.OpenGLProfile.Core);
#if DEBUG
            Glfw.WindowHint(Glfw.Hint.OpenglDebugContext, true);
#endif

            GConsole.WriteLine("Creating window");
            Window = Glfw.CreateWindow(Width, Height, "libTech");
            if (!Window)
            {
                FatalError("Could not create window({0}x{1})", Width, Height);
            }

            Gl.Initialize();
            Glfw.MakeContextCurrent(Window);

            /*bool AllSupported = true;
             *
             * for (int i = 0; i < RequiredExtensions.Length; i++) {
             *      if (!Glfw.ExtensionSupported(RequiredExtensions[i])) {
             *              GConsole.WriteLine("{0} not supported", RequiredExtensions[i]);
             *              AllSupported = false;
             *      }
             * }
             *
             * if (!AllSupported)
             *      while (true)
             *              Thread.Sleep(10);*/

#if DEBUG
            Gl.DebugMessageCallback((Src, DbgType, ID, Severity, Len, Buffer, UserPtr) => {
                if (Severity == Gl.DebugSeverity.Notification)
                {
                    return;
                }

                GConsole.WriteLine("OpenGL {0} {1} {2}, {3}", Src, DbgType, ID, Severity);
                GConsole.WriteLine(Encoding.ASCII.GetString((byte *)Buffer, Len));

                if ((/*Severity == Gl.DebugSeverity.Medium ||*/ Severity == Gl.DebugSeverity.High) && Debugger.IsAttached)
                {
                    Debugger.Break();
                }
            }, IntPtr.Zero);

            Gl.Enable((EnableCap)Gl.DEBUG_OUTPUT);
            Gl.Enable((EnableCap)Gl.DEBUG_OUTPUT_SYNCHRONOUS);
#endif

            GConsole.WriteLine("{0}, {1}", Gl.GetString(StringName.Vendor), Gl.GetString(StringName.Renderer));
            GConsole.WriteLine("OpenGL {0}", Gl.GetString(StringName.Version));
            GConsole.WriteLine("GLSL {0}", Gl.GetString(StringName.ShadingLanguageVersion));

            Gl.ClearColor(69 / 255.0f, 112 / 255.0f, 56 / 255.0f, 1.0f);

            // F**k the police
            Gl.Enable((EnableCap)Gl.DEPTH_CLAMP);
            Gl.Enable(EnableCap.Blend);
            //Gl.VERTEX_PROGRAM_POINT_SIZE;
            Gl.Enable((EnableCap)Gl.VERTEX_PROGRAM_POINT_SIZE);
            //Gl.Enable((EnableCap)Gl.SAMPLE_SHADING);
            //Gl.Enable(EnableCap.FramebufferSrgb);

            Gl.BlendEquationSeparate(BlendEquationMode.FuncAdd, BlendEquationMode.FuncAdd);
            Gl.BlendFuncSeparate(BlendingFactor.SrcAlpha, BlendingFactor.OneMinusSrcAlpha, BlendingFactor.SrcAlpha, BlendingFactor.OneMinusSrcAlpha);

            //Gl.BlendEquation(BlendEquationMode.FuncAdd);
            //Gl.BlendFunc(BlendingFactor.One, BlendingFactor.One);

            ShaderProgram.LoadDefaultShaders();
        }
Exemplo n.º 10
0
        static void Main(string[] args)
        {
            // Initialize OpenGL
            Gl.Initialize();

            // If the library isn't in the environment path we need to set it
            Glfw.ConfigureNativesDirectory("..\\..\\libs/glfw-3.2.1.bin.WIN32/lib-mingw");

            // Initialize the GLFW
            if (!Glfw.Init())
            {
                Environment.Exit(-1);
            }

            // Create a windowed mode window and its OpenGL context
            var window = Glfw.CreateWindow(width, height, "Unreal Engine 4");


            if (!window)
            {
                Glfw.Terminate();
                Environment.Exit(-1);
            }

            // set window icon
            Glfw.Image icon = Utils.getImage("..\\..\\res/logo.png");
            Glfw.SetWindowIcon(window, icon);

            // Make the window's context current
            Glfw.MakeContextCurrent(window);

            // create loder and renderer
            Loader loader = new Loader();


            // create model

            RawModel      model       = OBJLoader.loadObjModel("cube", loader);
            ModelTexture  texture     = new ModelTexture(loader.loadTexture("..\\..\\res/rgba.png"));
            TexturedModel staticModel = new TexturedModel(model, texture);

            staticModel.modelTexture.shineDamper  = 10;
            staticModel.modelTexture.reflectivity = 1;
            Entity entity = new Entity(staticModel, new Vertex3f(0, 0, -25), 0, 0, 0, 2);
            Light  light  = new Light(new Vertex3f(0, 0, -20), new Vertex3f(1f, 1f, 1f));

            Camera camera = new Camera();

            List <Entity> allCubes = new List <Entity>();

            Random random = new Random();

            for (int i = 0; i < 200; i++)
            {
                double x = random.NextDouble() * 100 - 50;
                double y = random.NextDouble() * 100 - 50;
                double z = random.NextDouble() * -300;
                allCubes.Add(new Entity(staticModel, new Vertex3f((float)x, (float)y, (float)z), (float)random.NextDouble() * 180,
                                        (float)random.NextDouble() * 180, 0, 1));
            }

            MasterRenderer renderer = new MasterRenderer(new WinowInfo(width, height));

            // Loop until the user closes the window
            while (!Glfw.WindowShouldClose(window))
            {
                camera.move(window);

                // Render here

                foreach (Entity cube in allCubes)
                {
                    renderer.processEntity(cube);
                }

                renderer.render(light, camera);

                //Swap front and back buffers
                Glfw.SwapBuffers(window);

                // Poll for and process events
                Glfw.PollEvents();
            }

            // clean memory
            renderer.cleanUP();
            loader.CleanUp();

            // terminate program
            Glfw.Terminate();
        }
Exemplo n.º 11
0
        static void Main(string[] args)
        {
            Gl.Initialize();
            Glfw.ConfigureNativesDirectory("../../../libs/");
            Glfw.Init();


            window = Glfw.CreateWindow(800, 600, "Window");

            if (!window)
            {
                Glfw.Terminate();
                Environment.Exit(-1);
            }

            Glfw.WindowHint(Glfw.Hint.ContextVersionMajor, 3);
            Glfw.WindowHint(Glfw.Hint.ContextVersionMinor, 2);
            Glfw.WindowHint(Glfw.Hint.OpenglProfile, Glfw.OpenGLProfile.Core);

            Glfw.SetWindowCloseCallback(window, WindowCloseCallback);
            Glfw.SetFramebufferSizeCallback(window, FramebufferResizeCallback);
            Glfw.SetKeyCallback(window, KeyCallback);

            Glfw.MakeContextCurrent(window);

            string shaderFolder = "../../../Media/Shaders/";

            Shader vertex   = new Shader(ShaderType.VertexShader, System.IO.File.ReadAllText(shaderFolder + vertexShaderSource));
            Shader fragment = new Shader(ShaderType.FragmentShader, System.IO.File.ReadAllText(shaderFolder + fragmentShaderSource));

            ShaderProgram shaderProgram = new ShaderProgram(vertex, fragment);

            shaderProgram.DetachShaders();

            VertexArray          vertexArray  = new VertexArray();
            BufferObject <uint>  indexBuffer  = new BufferObject <uint>(BufferTarget.ElementArrayBuffer, BufferUsage.StaticDraw);
            BufferObject <float> vertexBuffer = new BufferObject <float>(BufferTarget.ArrayBuffer, BufferUsage.StaticDraw);

            vertexArray.Bind();

            vertexBuffer.Bind();
            vertexBuffer.SetData(vertices);

            indexBuffer.Bind();
            indexBuffer.SetData(indices);

            vertexArray.AttributePointer(0, 3, VertexAttribType.Float, false, 8, 0);
            vertexArray.AttributePointer(1, 3, VertexAttribType.Float, false, 8, 3);
            vertexArray.AttributePointer(2, 2, VertexAttribType.Float, false, 8, 6);

            vertexBuffer.Unbind();

            string  textureFolder = "../../../Media/Textures/crate.jpg";
            Texture texture       = new Texture(textureFolder);

            while (!Glfw.WindowShouldClose(window))
            {
                Gl.ClearColor(0.6f, 0.6f, 0.6f, 1.0f);
                Gl.Clear(ClearBufferMask.ColorBufferBit);

                shaderProgram.Bind();
                vertexArray.Bind();
                Gl.DrawElements(PrimitiveType.Triangles, indices.Length, DrawElementsType.UnsignedInt, (IntPtr)0);

                Glfw.SwapBuffers(window);
                Glfw.PollEvents();
            }

            shaderProgram.Delete();
            vertexArray.Delete();
            vertexBuffer.Delete();
            indexBuffer.Delete();

            Glfw.Terminate();
        }
Exemplo n.º 12
0
        static void Main(string[] args)
        {
            // Initialize OpenGL
            Gl.Initialize();

            // If the library isn't in the environment path we need to set it
            Glfw.ConfigureNativesDirectory("..\\..\\libs/glfw-3.2.1.bin.WIN32/lib-mingw");

            // Initialize the GLFW
            if (!Glfw.Init())
            {
                Environment.Exit(-1);
            }

            // Create a windowed mode window and its OpenGL context
            var window = Glfw.CreateWindow(width, height, "OpenGL/Glfw");

            if (!window)
            {
                Glfw.Terminate();
                Environment.Exit(-1);
            }

            // Make the window's context current
            Glfw.MakeContextCurrent(window);

            // create shaders after creating opengl context
            StaticShader shader = new StaticShader();


            // create loder and renderer
            Loader   loader   = new Loader();
            Renderer renderer = new Renderer(shader, new WinowInfo(width, height));

            // set data

            float[] vertices =
            {
                -0.5f,  0.5f, -0.5f,
                -0.5f, -0.5f, -0.5f,
                0.5f,  -0.5f, -0.5f,
                0.5f,   0.5f, -0.5f,

                -0.5f,  0.5f,  0.5f,
                -0.5f, -0.5f,  0.5f,
                0.5f,  -0.5f,  0.5f,
                0.5f,   0.5f,  0.5f,

                0.5f,   0.5f, -0.5f,
                0.5f,  -0.5f, -0.5f,
                0.5f,  -0.5f,  0.5f,
                0.5f,   0.5f,  0.5f,

                -0.5f,  0.5f, -0.5f,
                -0.5f, -0.5f, -0.5f,
                -0.5f, -0.5f,  0.5f,
                -0.5f,  0.5f,  0.5f,

                -0.5f,  0.5f,  0.5f,
                -0.5f,  0.5f, -0.5f,
                0.5f,   0.5f, -0.5f,
                0.5f,   0.5f,  0.5f,

                -0.5f, -0.5f,  0.5f,
                -0.5f, -0.5f, -0.5f,
                0.5f,  -0.5f, -0.5f,
                0.5f,  -0.5f, 0.5f
            };

            float[] textureCoords =
            {
                0, 0,
                0, 1,
                1, 1,
                1, 0,
                0, 0,
                0, 1,
                1, 1,
                1, 0,
                0, 0,
                0, 1,
                1, 1,
                1, 0,
                0, 0,
                0, 1,
                1, 1,
                1, 0,
                0, 0,
                0, 1,
                1, 1,
                1, 0,
                0, 0,
                0, 1,
                1, 1,
                1, 0
            };

            uint[] indices =
            {
                0,   1,  3,
                3,   1,  2,
                4,   5,  7,
                7,   5,  6,
                8,   9, 11,
                11,  9, 10,
                12, 13, 15,
                15, 13, 14,
                16, 17, 19,
                19, 17, 18,
                20, 21, 23,
                23, 21, 22
            };

            // create model
            RawModel      model       = loader.LoadToVao(vertices, textureCoords, indices);
            ModelTexture  texture     = new ModelTexture(loader.loadTexture("..\\..\\res/c.jpeg"));
            TexturedModel staticModel = new TexturedModel(model, texture);

            Entity entity = new Entity(staticModel, new Vertex3f(0, 0, -5), 0, 0, 0, 1);


            Camera camera = new Camera();

            // Loop until the user closes the window
            while (!Glfw.WindowShouldClose(window))
            {
                entity.increaseRotation(1, 1, 0);

                // Render here
                camera.move(window);
                renderer.prepare();
                shader.start();
                shader.loadViewMatrix(camera);
                renderer.render(entity, shader);
                shader.stop();

                //Swap front and back buffers
                Glfw.SwapBuffers(window);

                // Poll for and process events
                Glfw.PollEvents();
            }

            // clean memory
            shader.cleanUP();
            loader.CleanUp();

            // terminate program
            Glfw.Terminate();
        }
Exemplo n.º 13
0
        static void Main(string[] args)
        {
            // Initialize OpenGL
            Gl.Initialize();

            // If the library isn't in the environment path we need to set it
            Glfw.ConfigureNativesDirectory("..\\..\\libs/glfw-3.2.1.bin.WIN32/lib-mingw");

            // Initialize the GLFW
            if (!Glfw.Init())
            {
                Environment.Exit(-1);
            }

            // Create a windowed mode window and its OpenGL context
            var window = Glfw.CreateWindow(width, height, "Unreal Engine 4");


            if (!window)
            {
                Glfw.Terminate();
                Environment.Exit(-1);
            }

            // set window icon
            Glfw.Image icon = Utils.getImage("..\\..\\res/logo.png");
            Glfw.SetWindowIcon(window, icon);

            // Make the window's context current
            Glfw.MakeContextCurrent(window);

            // create loder and renderer
            Loader loader = new Loader();

            // create terrain texture pack
            TerrainTexture     backgroundTexture = new TerrainTexture(loader.loadTexture("..\\..\\res/grassy2.png"));
            TerrainTexture     rTexture          = new TerrainTexture(loader.loadTexture("..\\..\\res/mud.png"));
            TerrainTexture     gTexture          = new TerrainTexture(loader.loadTexture("..\\..\\res/grassFlowers.png"));
            TerrainTexture     bTexture          = new TerrainTexture(loader.loadTexture("..\\..\\res/path.png"));
            TerrainTexturePack texturePack       = new TerrainTexturePack(backgroundTexture, rTexture, gTexture, bTexture);

            // create blend map
            TerrainTexture blendMap = new TerrainTexture(loader.loadTexture("..\\..\\res/blendMap.png"));

            // create model
            RawModel      fern         = OBJLoader.loadObjModel("fern", loader);
            ModelTexture  fernTexture  = new ModelTexture(loader.loadTexture("..\\..\\res/flower.png"));
            TexturedModel texturedFern = new TexturedModel(fern, fernTexture);

            texturedFern.modelTexture.shineDamper       = 10;
            texturedFern.modelTexture.reflectivity      = 0;
            texturedFern.modelTexture.isHasTransparency = true;

            RawModel      grass         = OBJLoader.loadObjModel("grassModel", loader);
            ModelTexture  grassTexture  = new ModelTexture(loader.loadTexture("..\\..\\res/flower.png"));
            TexturedModel texturedGrass = new TexturedModel(grass, grassTexture);

            texturedGrass.modelTexture.shineDamper       = 10;
            texturedGrass.modelTexture.reflectivity      = 0;
            texturedGrass.modelTexture.isHasTransparency = true;
            texturedGrass.modelTexture.isUseFakeLighting = true;

            RawModel      tree         = OBJLoader.loadObjModel("normal_tree", loader);
            ModelTexture  treeTexture  = new ModelTexture(loader.loadTexture("..\\..\\res/normal_tree.png"));
            TexturedModel texturedTree = new TexturedModel(tree, treeTexture);

            texturedTree.modelTexture.shineDamper  = 10;
            texturedTree.modelTexture.reflectivity = 0;

            RawModel      tree2         = OBJLoader.loadObjModel("Spruce", loader);
            ModelTexture  treeTexture2  = new ModelTexture(loader.loadTexture("..\\..\\res/branch.png"));
            TexturedModel texturedTree2 = new TexturedModel(tree2, treeTexture2);

            texturedTree2.modelTexture.shineDamper  = 10;
            texturedTree2.modelTexture.reflectivity = 0;

            RawModel      model       = OBJLoader.loadObjModel("lowPolyTree", loader);
            ModelTexture  texture     = new ModelTexture(loader.loadTexture("..\\..\\res/lowPolyTree.png"));
            TexturedModel staticModel = new TexturedModel(model, texture);

            staticModel.modelTexture.shineDamper  = 10;
            staticModel.modelTexture.reflectivity = 0;
            Entity entity = new Entity(staticModel, new Vertex3f(0, 0, -25), 0, 0, 0, 1);
            Light  light  = new Light(new Vertex3f(0, 0, -20), new Vertex3f(1f, 1f, 1f));

            Random       rand           = new Random();
            ModelTexture terrainTexture = new ModelTexture(loader.loadTexture("..\\..\\res/grass.png"));

            terrainTexture.shineDamper  = 10;
            terrainTexture.reflectivity = 0;
            Terrain terrain  = new Terrain(800, 0, loader, texturePack, blendMap);
            Terrain terrain2 = new Terrain(800, -1600, loader, texturePack, blendMap);

            RawModel     stanfordBunny        = OBJLoader.loadObjModel("person", loader);
            ModelTexture stanfordBunnyTexture = new ModelTexture(loader.loadTexture("..\\..\\res/PlayerTexture.png"));

            stanfordBunnyTexture.shineDamper  = 3;
            stanfordBunnyTexture.reflectivity = 0.00000000001f;
            TexturedModel texturedStanfordBunny = new TexturedModel(stanfordBunny, stanfordBunnyTexture);
            Player        player = new Player(texturedStanfordBunny, new Vertex3f(0, 0, -100), 0, 0, 0, 3);


            Random random = new Random();
            Random r      = new Random();

            Camera camera = new Camera(player, window, winowInfo);

            List <Entity> allCubes       = new List <Entity>();
            List <Entity> allGrass       = new List <Entity>();
            List <Entity> allFerns       = new List <Entity>();
            List <Entity> allTrees       = new List <Entity>();
            List <Entity> allNormalTrees = new List <Entity>();
            List <Entity> allTrees2      = new List <Entity>();


            for (int i = 0; i < 1000; i++)
            {
                double x  = random.NextDouble() * -1600 + 800;
                double y  = 0;
                double z  = random.NextDouble() * -2410;
                float  ra = r.Next(0, 23);
                if (ra > 0 & ra < 5)
                {
                    allCubes.Add(new Entity(staticModel, new Vertex3f((float)x, (float)y, (float)z), 0, 0, 0, rand.Next(1, 3)));
                }
                else if (ra > 5 & ra < 20)
                {
                    allTrees2.Add(new Entity(texturedTree2, new Vertex3f((float)x, (float)y, (float)z), 90, 0, 0, rand.Next(5, 9)));
                }
                else
                {
                    allNormalTrees.Add(new Entity(texturedTree, new Vertex3f((float)x, (float)y, (float)z), 0, 0, 0, rand.Next(20, 25)));
                }
            }

            for (int i = 0; i < 1000; i++)
            {
                double x = random.NextDouble() * -1600 + 800;
                double y = 0;
                double z = random.NextDouble() * -2410;
                allGrass.Add(new Entity(texturedGrass, new Vertex3f((float)x, (float)y, (float)z), 0, 0, 0, 3));
            }
            for (int i = 0; i < 1000; i++)
            {
                double x = random.NextDouble() * -1600 + 800;
                double y = 0;
                double z = random.NextDouble() * -2410;
                allFerns.Add(new Entity(texturedFern, new Vertex3f((float)x, (float)y, (float)z), 0, 0, 0, 3));
            }

            MasterRenderer renderer = new MasterRenderer(new WinowInfo(width, height));

            lastFrameTime = getCurrentTime();

            // Loop until the user closes the window
            while (!Glfw.WindowShouldClose(window))
            {
                //entity.increasePosition(0, 0, -0.1f);
                entity.increaseRotation(0, 1, 0);
                // Render here
                camera.move();
                player.move(window);

                renderer.processTerrain(terrain);
                renderer.processTerrain(terrain2);

                foreach (Entity cube in allCubes)
                {
                    renderer.processEntity(cube);
                }
                foreach (Entity gr in allGrass)
                {
                    renderer.processEntity(gr);
                }
                foreach (Entity fr in allFerns)
                {
                    renderer.processEntity(fr);
                }
                foreach (Entity tr in allTrees)
                {
                    renderer.processEntity(tr);
                }
                foreach (Entity ntr in allNormalTrees)
                {
                    renderer.processEntity(ntr);
                }
                foreach (Entity tr2 in allTrees2)
                {
                    renderer.processEntity(tr2);
                }

                renderer.processEntity(player);


                renderer.render(light, camera);


                //Swap front and back buffers
                Glfw.SwapBuffers(window);

                // Poll for and process events
                Glfw.PollEvents();

                long currentFrameTime = getCurrentTime();
                delta = (currentFrameTime - lastFrameTime) / 1000f;
                //Console.WriteLine(currentFrameTime + "  " + delta + "  " + lastFrameTime);
                lastFrameTime = currentFrameTime;
            }

            // clean memory
            renderer.cleanUP();
            loader.CleanUp();

            // terminate program
            Glfw.Terminate();
        }
Exemplo n.º 14
0
        static void Main(string[] args)
        {
            // Initialize OpenGL
            Gl.Initialize();


            // If the library isn't in the environment path we need to set it
            Glfw.ConfigureNativesDirectory("..\\..\\libs");

            // Initialize the GLFW
            if (!Glfw.Init())
            {
                Environment.Exit(-1);
            }

            // Create a windowed mode window and its OpenGL context
            var window = Glfw.CreateWindow(width, height, "OpenGL/Glfw");

            if (!window)
            {
                Glfw.Terminate();
                Environment.Exit(-1);
            }

            // Make the window's context current
            Glfw.MakeContextCurrent(window);


            Renderer renderer = new Renderer();
            Loader   loader   = new Loader();

            float[] vertices =
            {
                -0.5f,  0.5f, 0f,
                -0.5f, -0.5f, 0f,
                0.5f,  -0.5f, 0f,
                0.5f,  -0.5f, 0f,
                0.5f,   0.5f, 0f,
                -0.5f,  0.5f, 0f
            };


            RawModel model = loader.LoadToVao(vertices);

            // Loop until the user closes the window
            while (!Glfw.WindowShouldClose(window))
            {
                // Render here
                renderer.prepare();
                renderer.render(model);

                //Swap front and back buffers
                Glfw.SwapBuffers(window);

                // Poll for and process events
                Glfw.PollEvents();
            }

            // clean memory
            loader.CleanUp();

            // terminate program
            Glfw.Terminate();
        }