Example #1
0
        public Mesh(MeshData data)
        {
            Camera3D.CameraChanged += UpdateShaderMatrix;
            children = new LinkedList<Mesh>();

            Data = data;

            vertexShader = new VertexShader();
            vertexShader.LoadFromSource(vertexShaderSource);
            fragmentShader = new FragmentShader();
            fragmentShader.LoadFromSource(fragmentShaderSource);

            shaderProgram = new ShaderProgram(vertexShader, fragmentShader);
            shaderProgram.Link();
            shaderProgram.Use();

            shaderProgram.AddAttribLocation("position");
            shaderProgram.AddAttribLocation("color");
            shaderProgram.AddAttribLocation("normal");
            shaderProgram.AddUniformLocation("matrix");
            shaderProgram.AddUniformLocation("rotation");

            rotation = Matrix4.Identity;
            scale = Matrix4.Identity;
            translation = Matrix4.Identity;
            projection = Matrix4.CreatePerspectiveFieldOfView((float)Math.PI / 2.5F, 1, 0.1F, 100F);
            shaderModelMatrix = Matrix4.Identity;
            shaderRotationMatrix = Matrix4.Identity;

            window = Matrix4.Scale(Firefly.Window.Height / (float)Firefly.Window.Width, 1, 1);

            UpdateShaderMatrix();
        }
Example #2
0
        public static Stage Initialize(int width, int height, string title, OnLoadHandler loadHandler,
									  bool useLatestAvailable = false)
        {
            System.Threading.Thread.CurrentThread.CurrentCulture = CultureInfo.InvariantCulture;

            window = new Window(width, height, title, useLatestAvailable);

            Title = title;

            if (useLatestAvailable)
            {
                Console.WriteLine(@"Using latest available version: " + LatestAvailableVersion / 10F);
            }
            else
            {
                Console.WriteLine(@"Using OpenGL 2.0, latest version available on the system is: " + LatestAvailableVersion / 10F);
            }

            if (useLatestAvailable && LatestAvailableVersion >= 40)
            {
                defaultTextureVertexShader = new VertexShader();
                defaultTextureVertexShader.LoadFromSource(DefaultShaders.OGL4TexturedVert);
                defaultTexturedFragmentShader = new FragmentShader();
                defaultTexturedFragmentShader.LoadFromSource(DefaultShaders.OGL4TexturedFrag);
                defaultShapeVertexShader = new VertexShader();
                defaultShapeVertexShader.LoadFromSource(DefaultShaders.OGL4ShapeVert);
                defaultShapeFragmentShader = new FragmentShader();
                defaultShapeFragmentShader.LoadFromSource(DefaultShaders.OGL4ShapeFrag);
            }
            else
            {
                defaultTextureVertexShader = new VertexShader();
                defaultTextureVertexShader.LoadFromSource(DefaultShaders.TexturedVert);
                defaultTexturedFragmentShader = new FragmentShader();
                defaultTexturedFragmentShader.LoadFromSource(DefaultShaders.TexturedFrag);
                defaultShapeVertexShader = new VertexShader();
                defaultShapeVertexShader.LoadFromSource(DefaultShaders.ShapeVert);
                defaultShapeFragmentShader = new FragmentShader();
                defaultShapeFragmentShader.LoadFromSource(DefaultShaders.ShapeFrag);
            }

            DefaultShapeProgram = new ShaderProgram(defaultShapeVertexShader, defaultShapeFragmentShader);
            DefaultShapeProgram.Link();
            DefaultShapeProgram.AddUniformLocation("window_matrix");
            DefaultShapeProgram.AddUniformLocation("model_matrix");
            DefaultShapeProgram.AddUniformLocation("projection_matrix");
            DefaultShapeProgram.AddUniformLocation("camera_matrix");
            DefaultShapeProgram.AddUniformLocation("alpha");
            DefaultShapeProgram.AddUniformLocation("tintR");
            DefaultShapeProgram.AddUniformLocation("tintG");
            DefaultShapeProgram.AddUniformLocation("tintB");
            DefaultShapeProgram.AddUniformLocation("tintA");
            DefaultShapeProgram.AddAttribLocation("vertex_coord");
            DefaultShapeProgram.AddAttribLocation("vertex_color");

            DefaultTextureProgram = new ShaderProgram(defaultTextureVertexShader, defaultTexturedFragmentShader);
            DefaultTextureProgram.Link();
            DefaultTextureProgram.AddUniformLocation("window_matrix");
            DefaultTextureProgram.AddUniformLocation("model_matrix");
            DefaultTextureProgram.AddUniformLocation("projection_matrix");
            DefaultTextureProgram.AddUniformLocation("camera_matrix");
            DefaultTextureProgram.AddUniformLocation("texture");
            DefaultTextureProgram.AddUniformLocation("alpha");
            DefaultTextureProgram.AddUniformLocation("tintR");
            DefaultTextureProgram.AddUniformLocation("tintG");
            DefaultTextureProgram.AddUniformLocation("tintB");
            DefaultTextureProgram.AddUniformLocation("tintA");
            DefaultTextureProgram.AddAttribLocation("vertex_coord");
            DefaultTextureProgram.AddAttribLocation("vertex_texcoord");

            if (LatestAvailableVersion >= 40 && useLatestAvailable)
            {
                GL.BindFragDataLocation(DefaultShapeProgram.Id, 0, "FragColor");
                GL.BindFragDataLocation(DefaultTextureProgram.Id, 0, "FragColor");
            }

            WindowMatrix = Matrix4.Identity;
            ProjectionMatrix = new Matrix4(new Vector4(2F / window.Width, 0, 0, 0),
                                           new Vector4(0, -2F / window.Height, 0, 0),
                                           new Vector4(0, 0, 1, 0),
                                           new Vector4(0, 0, 0, 1)) * Matrix4.CreateTranslation(-1, 1, 0);

            var newCam = new Camera();
            newCam.Activate();

            renderList = new LinkedList<IRenderable>();
            updateList = new LinkedList<IUpdatable>();
            renderRemoveList = new LinkedList<IRenderable>();
            updateRemoveList = new LinkedList<IUpdatable>();

            Utility.Utility.ProcessOGLErrors();

            CreatePrimitives();

            ChangeViewPort(window.Width, window.Height);
            Camera.CurrentCamera.X = ViewportWidth / 2;
            Camera.CurrentCamera.Y = ViewportHeight / 2;

            var stage = new Stage();
            AddEntity(stage);
            window.GameWindow.Load += ((x, y) => loadHandler(stage));
            window.GameWindow.Load += MainLoop;

            window.GameWindow.Run();

            return stage;
        }