Пример #1
0
        private void Render()
        {
            if (selectedObj != null)
            {
                if (render3D)
                {
                    glHelper.Camera.Update();

                    /* Set projection, modelview */
                    glHelper.ProjectionType = ProjectionType.Perspective;
                    glHelper.ZNear          = 0.01f;
                    glHelper.ZFar           = 1000.0f;

                    /* Activate default shader */
                    glHelper.Shaders.ActivateProgram(Rendering.DefaultShaderName);

                    /* Set shader uniforms */
                    glHelper.Shaders.SetUniformMatrix(Rendering.DefaultShaderName, "projectionMatrix", false, glHelper.CreateProjectionMatrix());
                    glHelper.Shaders.SetUniformMatrix(Rendering.DefaultShaderName, "modelviewMatrix", false, Matrix4.Identity);
                    glHelper.Shaders.SetUniformMatrix(Rendering.DefaultShaderName, "objectMatrix", false, Matrix4.Identity);
                    glHelper.Shaders.SetUniform(Rendering.DefaultShaderName, "cameraPosition", glHelper.Camera.Position);
                    glHelper.Shaders.SetUniform(Rendering.DefaultShaderName, "enableLight", Convert.ToInt32(Properties.Settings.Default.EnableLighting));

                    if (Properties.Settings.Default.EnableLighting)
                    {
                        Rendering.ApplyLightRotation();
                        Rendering.UpdateLightUniforms(Rendering.DefaultShaderName, "numLights", "lights");
                    }

                    /* Render each known object */
                    foreach (string glObjectName in glObjectNames)
                    {
                        /* Activate object's texture */
                        glHelper.Textures.ActivateTexture(glObjectName, TextureUnit.Texture0);

                        if (glObjectMatrixMap.ContainsKey(glObjectName))
                        {
                            glHelper.Shaders.SetUniformMatrix(Rendering.DefaultShaderName, "objectMatrix", false, glObjectMatrixMap[glObjectName]);
                        }

                        /* Render */
                        glHelper.Buffers.Render(glObjectName);
                    }

                    /* Render light visualization */
                    if (Properties.Settings.Default.EnableLighting)
                    {
                        Rendering.RenderLightVisualization();
                    }
                }
                else
                {
                    Matrix4 modelviewMatrix2D        = Matrix4.CreateTranslation((glHelper.Viewport.Width / 2), (glHelper.Viewport.Height / 2), 0.0f);
                    Matrix4 modelviewWithImageOffset = Matrix4.Mult(modelviewMatrix2D, Matrix4.CreateTranslation(imageOffset.X, imageOffset.Y, 0.0f));

                    /* Set projection, modelview */
                    glHelper.ProjectionType = ProjectionType.Orthographic;
                    glHelper.ZNear          = -10.0f;
                    glHelper.ZFar           = 10.0f;

                    /* Activate empty dummy texture */
                    glHelper.Textures.ActivateTexture(Rendering.EmptyTextureName, TextureUnit.Texture0);

                    /* Activate default shader */
                    glHelper.Shaders.ActivateProgram(Rendering.DefaultShaderName);

                    /* Set shader uniforms */
                    glHelper.Shaders.SetUniformMatrix(Rendering.DefaultShaderName, "projectionMatrix", false, glHelper.CreateProjectionMatrix());
                    glHelper.Shaders.SetUniformMatrix(Rendering.DefaultShaderName, "modelviewMatrix", false, modelviewMatrix2D);
                    glHelper.Shaders.SetUniformMatrix(Rendering.DefaultShaderName, "objectMatrix", false, Matrix4.Identity);
                    glHelper.Shaders.SetUniform(Rendering.DefaultShaderName, "enableLight", 0);

                    /* Render */
                    glHelper.Buffers.Render(Rendering.AxisMarkerObjectName);

                    if (selectedObj != null)
                    {
                        /* Set shader uniforms */
                        glHelper.Shaders.SetUniformMatrix(Rendering.DefaultShaderName, "modelviewMatrix", false, modelviewWithImageOffset);

                        if (selectedObj is AnimationData)
                        {
                            /* Render animation */
                            AnimationData anim = (selectedObj as AnimationData);
                            if (anim.FirstNode != null)
                            {
                                currentMatrix = Matrix4.Identity;
                                RenderAnimationNode(anim.FirstNode);
                            }
                        }
                        else if (selectedObj is AnimationFrameData)
                        {
                            AnimationFrameData animFrame = (selectedObj as AnimationFrameData);
                            currentMatrix = Matrix4.Identity;
                            RenderAnimationFrame(animFrame, Vector2.Zero);
                        }
                        else if (selectedObj is ImageInformation || selectedObj is SpriteData || selectedObj is TxpImage)
                        {
                            foreach (string glObjectName in glObjectNames)
                            {
                                /* Activate object's texture */
                                glHelper.Textures.ActivateTexture(glObjectName, TextureUnit.Texture0);

                                /* Set matrices */
                                Matrix4 translationMatrix = Matrix4.Identity;
                                if (selectedObj is ImageInformation)
                                {
                                    ImageInformation image = (selectedObj as ImageInformation);
                                    translationMatrix = Matrix4.CreateTranslation(-(image.Bitmap.Width / 2), -(image.Bitmap.Height / 2), 0.0f);
                                }
                                else if (selectedObj is SpriteData)
                                {
                                    SpriteData sprite = (selectedObj as SpriteData);
                                    translationMatrix = Matrix4.CreateTranslation(-(sprite.Image.Width / 2), -(sprite.Image.Height / 2), 0.0f);
                                }
                                else if (selectedObj is TxpImage)
                                {
                                    TxpImage txp = (selectedObj as TxpImage);
                                    translationMatrix = Matrix4.CreateTranslation(-(txp.Image.Width / 2), -(txp.Image.Height / 2), 0.0f);
                                }
                                glHelper.Shaders.SetUniformMatrix(Rendering.DefaultShaderName, "objectMatrix", false, translationMatrix);

                                /* Render object */
                                glHelper.Buffers.Render(glObjectName);
                            }
                        }
                    }
                }
            }
        }
Пример #2
0
        public MainForm()
        {
            InitializeComponent();

            glHelper = new GLHelper(glControl, new Action(Render))
            {
                ClearColor = Color.SkyBlue,
            };
            glControl.Resize += ((s, e) =>
            {
                glHelper.Viewport = (s as GLControl).ClientRectangle;
            });
            glControl.Load += ((s, e) =>
            {
                Rendering.Initialize(glHelper);

                glHelper.Shaders.SetUniform(Rendering.DefaultShaderName, "materialTexture", 0);
                glHelper.Shaders.SetUniform(Rendering.DefaultShaderName, "materialShininess", 80.0f);
                glHelper.Shaders.SetUniform(Rendering.DefaultShaderName, "materialSpecularColor", new Vector3(1.0f, 1.0f, 1.0f));
            });

            render3D  = false;
            stopWatch = new Stopwatch();

            currentMatrix     = Matrix4.Identity;
            glObjectNames     = new List <string>();
            glObjectMatrixMap = new Dictionary <string, Matrix4>();

            SetFormTitle();

            debugDrawToolStripMenuItem.Checked      = Properties.Settings.Default.DebugDraw;
            enableLightingToolStripMenuItem.Checked = Properties.Settings.Default.EnableLighting;

            mouseCenter = imageOffset = Vector2.Zero;

            animCounter = 0;
            maxCounter  = 0;

            /* TODO uh, make better */
            timer          = new Timer();
            timer.Interval = 15;
            timer.Tick    += ((s, e) =>
            {
                animCounter++;
                if (animCounter >= maxCounter)
                {
                    animCounter = 0;
                }
            });

            timer.Start();

            Application.Idle += ((s, e) =>
            {
                stopWatch.Start();

                if (glControl.IsIdle)
                {
                    glControl.Invalidate();
                }

                stopWatch.Stop();

                double milliseconds = stopWatch.Elapsed.TotalMilliseconds;
                stopWatch.Reset();

                if (Properties.Settings.Default.EnableLighting)
                {
                    Rendering.RotateLights(milliseconds);
                }
            });

            tsslStatus.Text = "Ready";
        }