Exemplo n.º 1
0
 public void Bind(GLContext gl)
 {
     if( this.percentageMode )
     {
         int tempLeft = (int)(this.left * gl.canvasWidth);
         int tempTop = (int)(this.top * gl.canvasHeight);
         int tempWidth = (int)(this.width * gl.canvasWidth);
         int tempHeight = (int)(this.height * gl.canvasHeight);
         GL.Viewport(tempLeft, tempTop, tempWidth, tempHeight);
     }
     else
     {
         GL.Viewport((int)this.left, (int)this.top, (int)this.width, (int)this.height);
     }
 }
Exemplo n.º 2
0
        public Scene(string sceneXMLFile, int width, int height)
        {
            renderPasses = new List<RenderPass>();
            updatePasses = new List<UpdatePass>();
            renderObjects = new List<RenderObject>();
            viewports = new List<Viewport>();
            cameras = new List<Camera>();
            frameBuffers = new List<FrameBuffer>();
            meshes = new List<Mesh>();
            shaders = new List<Shader>();
            lights = new List<Light>();
            textures = new List<Texture>();

            gl = new GLContext();
            gl.canvasWidth = width;
            gl.canvasHeight = height;

            if (sceneXMLFile != null && sceneXMLFile.Length > 0)
            {
                try
                {
                    XmlDocument xml = new XmlDocument();
                    xml.Load(sceneXMLFile);
                    foreach (XmlNode child in xml.DocumentElement.ChildNodes)
                    {
                        if (child.NodeType == XmlNodeType.Element)
                        {
                            if (child.Name == "renderPass" || child.Name == "updatePass")
                            {
                                string passName = child.Attributes.GetNamedItem("name").Value;
                                string srcFile = child.Attributes.GetNamedItem("src").Value;

                                Pass thePass = null;
                                if (child.Name == "renderPass")
                                    thePass = GetRenderPass(passName, srcFile);
                                else
                                    thePass = GetUpdatePass(passName, srcFile);

                                foreach( XmlNode node in child.ChildNodes )
                                {
                                    if (node.NodeType == XmlNodeType.Element)
                                    {
                                        if (node.Name == "renderObject")
                                        {
                                            string objName = node.Attributes.GetNamedItem("name").Value;
                                            string objSrc = node.Attributes.GetNamedItem("src").Value;

                                            // Try to find this render object if its already loaded
                                            RenderObject renderObj = GetRenderObject(objName, objSrc);

                                            // Reference this object in the render pass
                                            thePass.renderObjects.Add(renderObj);
                                        }
                                        else if (node.Name == "light")
                                        {
                                            string objName = node.Attributes.GetNamedItem("name").Value;
                                            string objSrc = node.Attributes.GetNamedItem("src").Value;

                                            // Try to find this render object if its already loaded
                                            Light light = GetLight(objName, objSrc);

                                            thePass.lights.Add(light);
                                            thePass.lightsDirty = true;
                                        }
                                        else if (node.Name == "camera")
                                        {
                                            string objName = node.Attributes.GetNamedItem("name").Value;
                                            string objSrc = node.Attributes.GetNamedItem("src").Value;
                                            Camera cam = GetCamera(objName, objSrc);

                                            thePass.cameras.Add(cam);
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
                catch(Exception)
                {
                    System.Windows.Forms.MessageBox.Show("Failed to read scene file: " + sceneXMLFile);
                }

                Resize(width, height);
            }
        }
Exemplo n.º 3
0
        public void Draw(GLContext gl)
        {
            if (vb != 0)
            {
                GL.BindBuffer(BufferTarget.ArrayBuffer, vb);

                switch (vertFormat)
                {
                    case "P3":
                        GL.EnableVertexAttribArray(gl.shaderPositionLocation);
                        GL.VertexAttribPointer(gl.shaderPositionLocation, 3, VertexAttribPointerType.Float, false, 0, 0);
                        break;
                    case "P3N3":
                        GL.EnableVertexAttribArray(gl.shaderPositionLocation);
                        GL.VertexAttribPointer(gl.shaderPositionLocation, 3, VertexAttribPointerType.Float, false, 24, 0);

                        if( gl.shaderNormalLocation >= 0 )
                        {
                            GL.EnableVertexAttribArray(gl.shaderNormalLocation);
                            GL.VertexAttribPointer(gl.shaderNormalLocation, 3, VertexAttribPointerType.Float, false, 24, 12);
                        }
                        break;
                    case "P3T2":
                        GL.EnableVertexAttribArray(gl.shaderPositionLocation);
                        GL.VertexAttribPointer(gl.shaderPositionLocation, 3, VertexAttribPointerType.Float, false, 20, 0);

                        if( gl.shaderUVLocattion >= 0 )
                        {
                            GL.EnableVertexAttribArray(gl.shaderUVLocattion);
                            GL.VertexAttribPointer(gl.shaderUVLocattion, 2, VertexAttribPointerType.Float, false, 20, 12);
                        }
                        break;
                    default:
                        System.Windows.Forms.MessageBox.Show("unsupported vertex format: " + vertFormat);
                        break;
                }
                //GL.BindVertexArray(vb);

                if (ib > 0)
                {
                    // Indexed draw
                    GL.BindBuffer(BufferTarget.ElementArrayBuffer, ib);
                    GL.DrawElements(BeginMode.Triangles, triangleCount * 3, DrawElementsType.UnsignedShort, 0);
                    GL.BindBuffer(BufferTarget.ElementArrayBuffer, 0);
                }
                else
                {
                    // Non indexed draw
                    GL.DrawArrays(BeginMode.Triangles, 0, triangleCount * 3);
                }

                switch (vertFormat)
                {
                    case "P3":
                        GL.DisableVertexAttribArray(gl.shaderPositionLocation);
                        break;
                    case "P3N3":
                        GL.DisableVertexAttribArray(gl.shaderPositionLocation);

                        if (gl.shaderNormalLocation >= 0)
                            GL.DisableVertexAttribArray(gl.shaderNormalLocation);
                        break;
                    case "P3T2":
                        GL.DisableVertexAttribArray(gl.shaderPositionLocation);
                        if (gl.shaderUVLocattion >= 0)
                            GL.DisableVertexAttribArray(gl.shaderUVLocattion);
                        break;
                    default:
                        System.Windows.Forms.MessageBox.Show("unsupported vertex format: " + vertFormat);
                        break;
                }
                GL.BindBuffer(BufferTarget.ArrayBuffer, 0);
            }
        }
Exemplo n.º 4
0
 public void Bind(GLContext gl)
 {
     gl.view = view;
     gl.proj = proj;
     gl.viewProj = Matrix4.Mult(gl.view, gl.proj);
 }
Exemplo n.º 5
0
        public void Draw(GLContext gl)
        {
            // Bind shader
            shader.Bind(gl);

            // Update shader params
            if( gl.uMVP >= 0 )
            {
                Matrix4 mvp = Matrix4.Mult(worldMatrix, gl.viewProj);
                GL.UniformMatrix4(gl.uMVP, false, ref mvp);
            }

            if( gl.uWorldMtx >= 0 )
            {
                GL.UniformMatrix4(gl.uWorldMtx, false, ref worldMatrix);
            }

            if (gl.uViewMtx >= 0)
            {
                GL.UniformMatrix4(gl.uViewMtx, false, ref gl.view);
            }

            if (gl.uProjMtx >= 0)
            {
                GL.UniformMatrix4(gl.uProjMtx, false, ref gl.proj);
            }

            if( gl.uNrmMtx >= 0 )
            {
                GL.UniformMatrix3(gl.uNrmMtx, 1, false, normalMatrix);
            }

            if( shadowCamera != null && gl.uShadowMtx > 0 )
            {
                GL.UniformMatrix4(gl.uShadowMtx, false, ref shadowCamera.shadowMatrix);
            }

            // Bind Textures
            for( var i = 0; i < textures.Count; i++ )
            {
                if( textures[i] != null )
                {
                    textures[i].Bind(i);
                }
            }

            // Draw mesh
            if (mesh != null)
            {
                mesh.Draw(gl);
            }
        }
Exemplo n.º 6
0
 public void UpdateLights(GLContext gl)
 {
     lightUpdateToken++;
     foreach( RenderObject ro in renderObjects )
     {
         if (ro.shader.lightUpdateToken != lightUpdateToken)
         {
             ro.shader.lightUpdateToken = lightUpdateToken;
             ro.shader.lightCount = 0;
             ro.shader.Bind(gl);
             for (var j = 0; j < lights.Count; j++)
             {
                 ro.shader.AddLight(lights[j]);
             }
         }
     }
     lightsDirty = false;
 }
Exemplo n.º 7
0
        public void Draw(GLContext gl)
        {
            GL.Enable(EnableCap.DepthTest);

            if (lightsDirty)
                UpdateLights(gl);

            // Bind render target
            FrameBuffer.Bind(frameBuffer);

            // Set viewport
            viewport.Bind(gl);

            // Set camera
            camera.Bind(gl);

            // Clear
            DoClear();

            // Bind override shader
            if( overrideShader != null )
                overrideShader.BindOverride(gl);

            // Draw objects
            for (var i = 0; i < renderObjects.Count; i++)
            {
                renderObjects[i].Draw(gl);
            }

            if( overrideShader != null )
                overrideShader.UnbindOverride(gl);
        }
Exemplo n.º 8
0
 public void UnbindOverride(GLContext gl)
 {
     gl.overrideShader = null;
 }
Exemplo n.º 9
0
 public void BindOverride(GLContext gl)
 {
     UnbindOverride(gl);
     Bind(gl);
     gl.overrideShader = this;
 }
Exemplo n.º 10
0
        public void Bind(GLContext gl)
        {
            if( gl.overrideShader == null)
            {
                GL.UseProgram(shaderProgram);

                gl.shaderPositionLocation = positionAttribute;
                gl.shaderNormalLocation = normalAttribute;
                gl.shaderUVLocattion = uvAttribute;

                gl.uMVP = mvpUniform;
                gl.uNrmMtx = normalUniform;
                gl.uWorldMtx = worldUniform;
                gl.uViewMtx = viewUniform;
                gl.uProjMtx = projUniform;
                gl.uShadowMtx = shadowMtxUniform;
            }
        }