예제 #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 static void RenderLightVisualization()
        {
            for (int i = 1; i < Lights.Length; i++)
            {
                if (!Lights[i].Enabled)
                {
                    continue;
                }

                /* Render light marker line */
                glHelper.Shaders.ActivateProgram(LightLineShaderName);
                glHelper.Shaders.SetUniformMatrix(LightLineShaderName, "projectionMatrix", false, glHelper.CreateProjectionMatrix());
                glHelper.Shaders.SetUniformMatrix(LightLineShaderName, "modelviewMatrix", false, Matrix4.Identity);
                glHelper.Shaders.SetUniformMatrix(LightLineShaderName, "objectMatrix", false, Matrix4.CreateTranslation(Lights[i].Position.Xyz));
                glHelper.Shaders.SetUniformMatrix(LightLineShaderName, "baseMatrix", false, Matrix4.CreateTranslation(LightOffset));
                glHelper.Shaders.SetUniform(LightLineShaderName, "surfaceColor", new OpenTK.Graphics.Color4(0.0f, 0.0f, 0.0f, 0.5f));
                glHelper.Buffers.Render(LightMarkerObjectName);

                /* Render light marker */
                glHelper.Shaders.ActivateProgram(LightMarkerShaderName);
                glHelper.Shaders.SetUniformMatrix(LightMarkerShaderName, "projectionMatrix", false, glHelper.CreateProjectionMatrix());
                glHelper.Shaders.SetUniformMatrix(LightMarkerShaderName, "modelviewMatrix", false, Matrix4.Identity);
                glHelper.Shaders.SetUniformMatrix(LightMarkerShaderName, "objectMatrix", false, Matrix4.CreateTranslation(Lights[i].Position.Xyz));
                glHelper.Shaders.SetUniformMatrix(LightMarkerShaderName, "baseMatrix", false, Matrix4.CreateTranslation(LightOffset));
                glHelper.Shaders.SetUniform(LightMarkerShaderName, "surfaceColor", new OpenTK.Graphics.Color4(Lights[i].Intensities.X, Lights[i].Intensities.Y, Lights[i].Intensities.Z, 0.75f));
                glHelper.Buffers.Render(LightMarkerObjectName);

                /* Render light marker center */
                glHelper.Shaders.ActivateProgram(LightMarkerShaderName);
                glHelper.Shaders.SetUniformMatrix(LightMarkerShaderName, "objectMatrix", false, Matrix4.CreateTranslation(LightOffset));
                glHelper.Shaders.SetUniformMatrix(LightMarkerShaderName, "baseMatrix", false, Matrix4.Identity);
                glHelper.Shaders.SetUniform(LightMarkerShaderName, "surfaceColor", new OpenTK.Graphics.Color4(0.5f, 0.5f, 0.5f, 0.5f));
                glHelper.Buffers.Render(LightMarkerObjectName);
            }
        }